andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifndef _SECDER_H_ andre@0: #define _SECDER_H_ andre@0: andre@0: #include "utilrename.h" andre@0: andre@0: /* andre@0: * secder.h - public data structures and prototypes for the DER encoding and andre@0: * decoding utilities library andre@0: */ andre@0: andre@0: #include andre@0: andre@0: #include "plarena.h" andre@0: #include "prlong.h" andre@0: andre@0: #include "seccomon.h" andre@0: #include "secdert.h" andre@0: #include "prtime.h" andre@0: andre@0: SEC_BEGIN_PROTOS andre@0: andre@0: /* andre@0: ** Encode a data structure into DER. andre@0: ** "dest" will be filled in (and memory allocated) to hold the der andre@0: ** encoded structure in "src" andre@0: ** "t" is a template structure which defines the shape of the andre@0: ** stored data andre@0: ** "src" is a pointer to the structure that will be encoded andre@0: */ andre@0: extern SECStatus DER_Encode(PLArenaPool *arena, SECItem *dest, DERTemplate *t, andre@0: void *src); andre@0: andre@0: extern SECStatus DER_Lengths(SECItem *item, int *header_len_p, andre@0: PRUint32 *contents_len_p); andre@0: andre@0: /* andre@0: ** Lower level der subroutine that stores the standard header into "to". andre@0: ** The header is of variable length, based on encodingLen. andre@0: ** The return value is the new value of "to" after skipping over the header. andre@0: ** "to" is where the header will be stored andre@0: ** "code" is the der code to write andre@0: ** "encodingLen" is the number of bytes of data that will follow andre@0: ** the header andre@0: */ andre@0: extern unsigned char *DER_StoreHeader(unsigned char *to, unsigned int code, andre@0: PRUint32 encodingLen); andre@0: andre@0: /* andre@0: ** Return the number of bytes it will take to hold a der encoded length. andre@0: */ andre@0: extern int DER_LengthLength(PRUint32 len); andre@0: andre@0: /* andre@0: ** Store a der encoded *signed* integer (whose value is "src") into "dst". andre@0: ** XXX This should really be enhanced to take a long. andre@0: */ andre@0: extern SECStatus DER_SetInteger(PLArenaPool *arena, SECItem *dst, PRInt32 src); andre@0: andre@0: /* andre@0: ** Store a der encoded *unsigned* integer (whose value is "src") into "dst". andre@0: ** XXX This should really be enhanced to take an unsigned long. andre@0: */ andre@0: extern SECStatus DER_SetUInteger(PLArenaPool *arena, SECItem *dst, PRUint32 src); andre@0: andre@0: /* andre@0: ** Decode a der encoded *signed* integer that is stored in "src". andre@0: ** If "-1" is returned, then the caller should check the error in andre@0: ** XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER). andre@0: */ andre@0: extern long DER_GetInteger(const SECItem *src); andre@0: andre@0: /* andre@0: ** Decode a der encoded *unsigned* integer that is stored in "src". andre@0: ** If the ULONG_MAX is returned, then the caller should check the error andre@0: ** in XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER). andre@0: */ andre@0: extern unsigned long DER_GetUInteger(SECItem *src); andre@0: andre@0: /* andre@0: ** Convert an NSPR time value to a der encoded time value. andre@0: ** "result" is the der encoded time (memory is allocated) andre@0: ** "time" is the NSPR time value (Since Jan 1st, 1970). andre@0: ** time must be on or after January 1, 1950, and andre@0: ** before January 1, 2050 andre@0: ** The caller is responsible for freeing up the buffer which andre@0: ** result->data points to upon a successful operation. andre@0: */ andre@0: extern SECStatus DER_TimeToUTCTime(SECItem *result, PRTime time); andre@0: extern SECStatus DER_TimeToUTCTimeArena(PLArenaPool* arenaOpt, andre@0: SECItem *dst, PRTime gmttime); andre@0: andre@0: andre@0: /* andre@0: ** Convert an ascii encoded time value (according to DER rules) into andre@0: ** an NSPR time value. andre@0: ** "result" the resulting NSPR time andre@0: ** "string" the der notation ascii value to decode andre@0: */ andre@0: extern SECStatus DER_AsciiToTime(PRTime *result, const char *string); andre@0: andre@0: /* andre@0: ** Same as DER_AsciiToTime except takes an SECItem instead of a string andre@0: */ andre@0: extern SECStatus DER_UTCTimeToTime(PRTime *result, const SECItem *time); andre@0: andre@0: /* andre@0: ** Convert a DER encoded UTC time to an ascii time representation andre@0: ** "utctime" is the DER encoded UTC time to be converted. The andre@0: ** caller is responsible for deallocating the returned buffer. andre@0: */ andre@0: extern char *DER_UTCTimeToAscii(SECItem *utcTime); andre@0: andre@0: /* andre@0: ** Convert a DER encoded UTC time to an ascii time representation, but only andre@0: ** include the day, not the time. andre@0: ** "utctime" is the DER encoded UTC time to be converted. andre@0: ** The caller is responsible for deallocating the returned buffer. andre@0: */ andre@0: extern char *DER_UTCDayToAscii(SECItem *utctime); andre@0: /* same thing for DER encoded GeneralizedTime */ andre@0: extern char *DER_GeneralizedDayToAscii(SECItem *gentime); andre@0: /* same thing for either DER UTCTime or GeneralizedTime */ andre@0: extern char *DER_TimeChoiceDayToAscii(SECItem *timechoice); andre@0: andre@0: /* andre@0: ** Convert a PRTime to a DER encoded Generalized time andre@0: ** gmttime must be on or after January 1, year 1 and andre@0: ** before January 1, 10000. andre@0: */ andre@0: extern SECStatus DER_TimeToGeneralizedTime(SECItem *dst, PRTime gmttime); andre@0: extern SECStatus DER_TimeToGeneralizedTimeArena(PLArenaPool* arenaOpt, andre@0: SECItem *dst, PRTime gmttime); andre@0: andre@0: /* andre@0: ** Convert a DER encoded Generalized time value into an NSPR time value. andre@0: ** "dst" the resulting NSPR time andre@0: ** "string" the der notation ascii value to decode andre@0: */ andre@0: extern SECStatus DER_GeneralizedTimeToTime(PRTime *dst, const SECItem *time); andre@0: andre@0: /* andre@0: ** Convert from a PRTime UTC time value to a formatted ascii value. The andre@0: ** caller is responsible for deallocating the returned buffer. andre@0: */ andre@0: extern char *CERT_UTCTime2FormattedAscii (PRTime utcTime, char *format); andre@0: #define CERT_GeneralizedTime2FormattedAscii CERT_UTCTime2FormattedAscii andre@0: andre@0: /* andre@0: ** Convert from a PRTime Generalized time value to a formatted ascii value. The andre@0: ** caller is responsible for deallocating the returned buffer. andre@0: */ andre@0: extern char *CERT_GenTime2FormattedAscii (PRTime genTime, char *format); andre@0: andre@0: /* andre@0: ** decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME andre@0: ** or a SEC_ASN1_UTC_TIME andre@0: */ andre@0: andre@0: extern SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input); andre@0: andre@0: /* encode a PRTime to an ASN.1 DER SECItem containing either a andre@0: SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */ andre@0: andre@0: extern SECStatus DER_EncodeTimeChoice(PLArenaPool* arena, SECItem* output, andre@0: PRTime input); andre@0: andre@0: SEC_END_PROTOS andre@0: andre@0: #endif /* _SECDER_H_ */ andre@0: