|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef _SECDER_H_ |
|
6 #define _SECDER_H_ |
|
7 |
|
8 #include "utilrename.h" |
|
9 |
|
10 /* |
|
11 * secder.h - public data structures and prototypes for the DER encoding and |
|
12 * decoding utilities library |
|
13 */ |
|
14 |
|
15 #include <time.h> |
|
16 |
|
17 #include "plarena.h" |
|
18 #include "prlong.h" |
|
19 |
|
20 #include "seccomon.h" |
|
21 #include "secdert.h" |
|
22 #include "prtime.h" |
|
23 |
|
24 SEC_BEGIN_PROTOS |
|
25 |
|
26 /* |
|
27 ** Encode a data structure into DER. |
|
28 ** "dest" will be filled in (and memory allocated) to hold the der |
|
29 ** encoded structure in "src" |
|
30 ** "t" is a template structure which defines the shape of the |
|
31 ** stored data |
|
32 ** "src" is a pointer to the structure that will be encoded |
|
33 */ |
|
34 extern SECStatus DER_Encode(PLArenaPool *arena, SECItem *dest, DERTemplate *t, |
|
35 void *src); |
|
36 |
|
37 extern SECStatus DER_Lengths(SECItem *item, int *header_len_p, |
|
38 PRUint32 *contents_len_p); |
|
39 |
|
40 /* |
|
41 ** Lower level der subroutine that stores the standard header into "to". |
|
42 ** The header is of variable length, based on encodingLen. |
|
43 ** The return value is the new value of "to" after skipping over the header. |
|
44 ** "to" is where the header will be stored |
|
45 ** "code" is the der code to write |
|
46 ** "encodingLen" is the number of bytes of data that will follow |
|
47 ** the header |
|
48 */ |
|
49 extern unsigned char *DER_StoreHeader(unsigned char *to, unsigned int code, |
|
50 PRUint32 encodingLen); |
|
51 |
|
52 /* |
|
53 ** Return the number of bytes it will take to hold a der encoded length. |
|
54 */ |
|
55 extern int DER_LengthLength(PRUint32 len); |
|
56 |
|
57 /* |
|
58 ** Store a der encoded *signed* integer (whose value is "src") into "dst". |
|
59 ** XXX This should really be enhanced to take a long. |
|
60 */ |
|
61 extern SECStatus DER_SetInteger(PLArenaPool *arena, SECItem *dst, PRInt32 src); |
|
62 |
|
63 /* |
|
64 ** Store a der encoded *unsigned* integer (whose value is "src") into "dst". |
|
65 ** XXX This should really be enhanced to take an unsigned long. |
|
66 */ |
|
67 extern SECStatus DER_SetUInteger(PLArenaPool *arena, SECItem *dst, PRUint32 src); |
|
68 |
|
69 /* |
|
70 ** Decode a der encoded *signed* integer that is stored in "src". |
|
71 ** If "-1" is returned, then the caller should check the error in |
|
72 ** XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER). |
|
73 */ |
|
74 extern long DER_GetInteger(const SECItem *src); |
|
75 |
|
76 /* |
|
77 ** Decode a der encoded *unsigned* integer that is stored in "src". |
|
78 ** If the ULONG_MAX is returned, then the caller should check the error |
|
79 ** in XP_GetError() to see if an overflow occurred (SEC_ERROR_BAD_DER). |
|
80 */ |
|
81 extern unsigned long DER_GetUInteger(SECItem *src); |
|
82 |
|
83 /* |
|
84 ** Convert an NSPR time value to a der encoded time value. |
|
85 ** "result" is the der encoded time (memory is allocated) |
|
86 ** "time" is the NSPR time value (Since Jan 1st, 1970). |
|
87 ** time must be on or after January 1, 1950, and |
|
88 ** before January 1, 2050 |
|
89 ** The caller is responsible for freeing up the buffer which |
|
90 ** result->data points to upon a successful operation. |
|
91 */ |
|
92 extern SECStatus DER_TimeToUTCTime(SECItem *result, PRTime time); |
|
93 extern SECStatus DER_TimeToUTCTimeArena(PLArenaPool* arenaOpt, |
|
94 SECItem *dst, PRTime gmttime); |
|
95 |
|
96 |
|
97 /* |
|
98 ** Convert an ascii encoded time value (according to DER rules) into |
|
99 ** an NSPR time value. |
|
100 ** "result" the resulting NSPR time |
|
101 ** "string" the der notation ascii value to decode |
|
102 */ |
|
103 extern SECStatus DER_AsciiToTime(PRTime *result, const char *string); |
|
104 |
|
105 /* |
|
106 ** Same as DER_AsciiToTime except takes an SECItem instead of a string |
|
107 */ |
|
108 extern SECStatus DER_UTCTimeToTime(PRTime *result, const SECItem *time); |
|
109 |
|
110 /* |
|
111 ** Convert a DER encoded UTC time to an ascii time representation |
|
112 ** "utctime" is the DER encoded UTC time to be converted. The |
|
113 ** caller is responsible for deallocating the returned buffer. |
|
114 */ |
|
115 extern char *DER_UTCTimeToAscii(SECItem *utcTime); |
|
116 |
|
117 /* |
|
118 ** Convert a DER encoded UTC time to an ascii time representation, but only |
|
119 ** include the day, not the time. |
|
120 ** "utctime" is the DER encoded UTC time to be converted. |
|
121 ** The caller is responsible for deallocating the returned buffer. |
|
122 */ |
|
123 extern char *DER_UTCDayToAscii(SECItem *utctime); |
|
124 /* same thing for DER encoded GeneralizedTime */ |
|
125 extern char *DER_GeneralizedDayToAscii(SECItem *gentime); |
|
126 /* same thing for either DER UTCTime or GeneralizedTime */ |
|
127 extern char *DER_TimeChoiceDayToAscii(SECItem *timechoice); |
|
128 |
|
129 /* |
|
130 ** Convert a PRTime to a DER encoded Generalized time |
|
131 ** gmttime must be on or after January 1, year 1 and |
|
132 ** before January 1, 10000. |
|
133 */ |
|
134 extern SECStatus DER_TimeToGeneralizedTime(SECItem *dst, PRTime gmttime); |
|
135 extern SECStatus DER_TimeToGeneralizedTimeArena(PLArenaPool* arenaOpt, |
|
136 SECItem *dst, PRTime gmttime); |
|
137 |
|
138 /* |
|
139 ** Convert a DER encoded Generalized time value into an NSPR time value. |
|
140 ** "dst" the resulting NSPR time |
|
141 ** "string" the der notation ascii value to decode |
|
142 */ |
|
143 extern SECStatus DER_GeneralizedTimeToTime(PRTime *dst, const SECItem *time); |
|
144 |
|
145 /* |
|
146 ** Convert from a PRTime UTC time value to a formatted ascii value. The |
|
147 ** caller is responsible for deallocating the returned buffer. |
|
148 */ |
|
149 extern char *CERT_UTCTime2FormattedAscii (PRTime utcTime, char *format); |
|
150 #define CERT_GeneralizedTime2FormattedAscii CERT_UTCTime2FormattedAscii |
|
151 |
|
152 /* |
|
153 ** Convert from a PRTime Generalized time value to a formatted ascii value. The |
|
154 ** caller is responsible for deallocating the returned buffer. |
|
155 */ |
|
156 extern char *CERT_GenTime2FormattedAscii (PRTime genTime, char *format); |
|
157 |
|
158 /* |
|
159 ** decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME |
|
160 ** or a SEC_ASN1_UTC_TIME |
|
161 */ |
|
162 |
|
163 extern SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input); |
|
164 |
|
165 /* encode a PRTime to an ASN.1 DER SECItem containing either a |
|
166 SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */ |
|
167 |
|
168 extern SECStatus DER_EncodeTimeChoice(PLArenaPool* arena, SECItem* output, |
|
169 PRTime input); |
|
170 |
|
171 SEC_END_PROTOS |
|
172 |
|
173 #endif /* _SECDER_H_ */ |
|
174 |