Back to index

lightning-sunbird  0.9+nobinonly
secder.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifndef _SECDER_H_
00038 #define _SECDER_H_
00039 
00040 /*
00041  * secder.h - public data structures and prototypes for the DER encoding and
00042  *           decoding utilities library
00043  *
00044  * $Id: secder.h,v 1.7 2004/04/25 15:03:18 gerv%gerv.net Exp $
00045  */
00046 
00047 #if defined(_WIN32_WCE)
00048 #else
00049 #include <time.h>
00050 #endif
00051 
00052 #include "plarena.h"
00053 #include "prlong.h"
00054 
00055 #include "seccomon.h"
00056 #include "secdert.h"
00057 #include "prtime.h"
00058 
00059 SEC_BEGIN_PROTOS
00060 
00061 /*
00062 ** Decode a piece of der encoded data.
00063 **     "dest" points to a structure that will be filled in with the
00064 **        decoding results.  (NOTE: it should be zeroed before calling;
00065 **        optional/missing fields are not zero-filled by DER_Decode.)
00066 **     "t" is a template structure which defines the shape of the
00067 **        expected data.
00068 **     "src" is the der encoded data.
00069 ** NOTE: substructures of "dest" will be allocated as needed from
00070 ** "arena", but data subfields will point directly into the buffer
00071 ** passed in as src->data.  That is, the resulting "dest" structure
00072 ** will contain pointers back into src->data, which must remain
00073 ** active (allocated) and unmodified for as long as "dest" is active.
00074 ** If this is a potential problem, you may want to just dup the buffer
00075 ** (allocated from "arena", probably) and pass *that* in instead.
00076 */
00077 extern SECStatus DER_Decode(PRArenaPool *arena, void *dest, DERTemplate *t,
00078                         SECItem *src);
00079 
00080 /*
00081 ** Encode a data structure into DER.
00082 **     "dest" will be filled in (and memory allocated) to hold the der
00083 **        encoded structure in "src"
00084 **     "t" is a template structure which defines the shape of the
00085 **        stored data
00086 **     "src" is a pointer to the structure that will be encoded
00087 */
00088 extern SECStatus DER_Encode(PRArenaPool *arena, SECItem *dest, DERTemplate *t,
00089                         void *src);
00090 
00091 extern SECStatus DER_Lengths(SECItem *item, int *header_len_p, uint32 *contents_len_p);
00092 
00093 /*
00094 ** Lower level der subroutine that stores the standard header into "to".
00095 ** The header is of variable length, based on encodingLen.
00096 ** The return value is the new value of "to" after skipping over the header.
00097 **     "to" is where the header will be stored
00098 **     "code" is the der code to write
00099 **     "encodingLen" is the number of bytes of data that will follow
00100 **        the header
00101 */
00102 extern unsigned char *DER_StoreHeader(unsigned char *to, unsigned int code,
00103                                   uint32 encodingLen);
00104 
00105 /*
00106 ** Return the number of bytes it will take to hold a der encoded length.
00107 */
00108 extern int DER_LengthLength(uint32 len);
00109 
00110 /*
00111 ** Store a der encoded *signed* integer (whose value is "src") into "dst".
00112 ** XXX This should really be enhanced to take a long.
00113 */
00114 extern SECStatus DER_SetInteger(PRArenaPool *arena, SECItem *dst, int32 src);
00115 
00116 /*
00117 ** Store a der encoded *unsigned* integer (whose value is "src") into "dst".
00118 ** XXX This should really be enhanced to take an unsigned long.
00119 */
00120 extern SECStatus DER_SetUInteger(PRArenaPool *arena, SECItem *dst, uint32 src);
00121 
00122 /*
00123 ** Decode a der encoded *signed* integer that is stored in "src".
00124 ** If "-1" is returned, then the caller should check the error in
00125 ** XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER).
00126 */
00127 extern long DER_GetInteger(SECItem *src);
00128 
00129 /*
00130 ** Decode a der encoded *unsigned* integer that is stored in "src".
00131 ** If the ULONG_MAX is returned, then the caller should check the error
00132 ** in XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER).
00133 */
00134 extern unsigned long DER_GetUInteger(SECItem *src);
00135 
00136 /*
00137 ** Convert a "UNIX" time value to a der encoded time value.
00138 **     "result" is the der encoded time (memory is allocated)
00139 **     "time" is the "UNIX" time value (Since Jan 1st, 1970).
00140 ** The caller is responsible for freeing up the buffer which
00141 ** result->data points to upon a successfull operation.
00142 */
00143 extern SECStatus DER_TimeToUTCTime(SECItem *result, int64 time);
00144 extern SECStatus DER_TimeToUTCTimeArena(PRArenaPool* arenaOpt,
00145                                         SECItem *dst, int64 gmttime);
00146 
00147 
00148 /*
00149 ** Convert an ascii encoded time value (according to DER rules) into
00150 ** a UNIX time value.
00151 **     "result" the resulting "UNIX" time
00152 **     "string" the der notation ascii value to decode
00153 */
00154 extern SECStatus DER_AsciiToTime(int64 *result, const char *string);
00155 
00156 /*
00157 ** Same as DER_AsciiToTime except takes an SECItem instead of a string
00158 */
00159 extern SECStatus DER_UTCTimeToTime(int64 *result, const SECItem *time);
00160 
00161 /*
00162 ** Convert a DER encoded UTC time to an ascii time representation
00163 ** "utctime" is the DER encoded UTC time to be converted. The
00164 ** caller is responsible for deallocating the returned buffer.
00165 */
00166 extern char *DER_UTCTimeToAscii(SECItem *utcTime);
00167 
00168 /*
00169 ** Convert a DER encoded UTC time to an ascii time representation, but only
00170 ** include the day, not the time.
00171 **     "utctime" is the DER encoded UTC time to be converted.
00172 ** The caller is responsible for deallocating the returned buffer.
00173 */
00174 extern char *DER_UTCDayToAscii(SECItem *utctime);
00175 /* same thing for DER encoded GeneralizedTime */
00176 extern char *DER_GeneralizedDayToAscii(SECItem *gentime);
00177 /* same thing for either DER UTCTime or GeneralizedTime */
00178 extern char *DER_TimeChoiceDayToAscii(SECItem *timechoice);
00179 
00180 /*
00181 ** Convert a int64 time to a DER encoded Generalized time
00182 */
00183 extern SECStatus DER_TimeToGeneralizedTime(SECItem *dst, int64 gmttime);
00184 extern SECStatus DER_TimeToGeneralizedTimeArena(PRArenaPool* arenaOpt,
00185                                                 SECItem *dst, int64 gmttime);
00186 
00187 /*
00188 ** Convert a DER encoded Generalized time value into a UNIX time value.
00189 **     "dst" the resulting "UNIX" time
00190 **     "string" the der notation ascii value to decode
00191 */
00192 extern SECStatus DER_GeneralizedTimeToTime(int64 *dst, const SECItem *time);
00193 
00194 /*
00195 ** Convert from a int64 UTC time value to a formatted ascii value. The
00196 ** caller is responsible for deallocating the returned buffer.
00197 */
00198 extern char *CERT_UTCTime2FormattedAscii (int64 utcTime, char *format);
00199 #define CERT_GeneralizedTime2FormattedAscii CERT_UTCTime2FormattedAscii
00200 
00201 /*
00202 ** Convert from a int64 Generalized time value to a formatted ascii value. The
00203 ** caller is responsible for deallocating the returned buffer.
00204 */
00205 extern char *CERT_GenTime2FormattedAscii (int64 genTime, char *format);
00206 
00207 /*
00208 ** decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME 
00209 ** or a SEC_ASN1_UTC_TIME
00210 */
00211 
00212 extern SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input);
00213 
00214 /* encode a PRTime to an ASN.1 DER SECItem containing either a
00215    SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */
00216 
00217 extern SECStatus DER_EncodeTimeChoice(PRArenaPool* arena, SECItem* output,
00218                                        PRTime input);
00219 
00220 SEC_END_PROTOS
00221 
00222 #endif /* _SECDER_H_ */
00223