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