SharkSSL™ Embedded SSL/TLS Stack
SharkSslASN1.h
Go to the documentation of this file.
1 /*
2  * ____ _________ __ _
3  * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4  * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5  * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6  * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7  * /____/
8  *
9  * SharkSSL Embedded SSL/TLS Stack
10  ****************************************************************************
11  * PROGRAM MODULE
12  *
13  * $Id: SharkSslASN1.h 3670 2015-03-28 21:25:15Z gianluca $
14  *
15  * COPYRIGHT: Real Time Logic LLC, 2018 - 2021
16  *
17  * This software is copyrighted by and is the sole property of Real
18  * Time Logic LLC. All rights, title, ownership, or other interests in
19  * the software remain the property of Real Time Logic LLC. This
20  * software may only be used in accordance with the terms and
21  * conditions stipulated in the corresponding license agreement under
22  * which the software has been supplied. Any unauthorized use,
23  * duplication, transmission, distribution, or disclosure of this
24  * software is expressly forbidden.
25  *
26  * This Copyright notice may not be removed or modified without prior
27  * written consent of Real Time Logic LLC.
28  *
29  * Real Time Logic LLC. reserves the right to modify this software
30  * without notice.
31  *
32  * http://www.realtimelogic.com
33  * http://www.sharkssl.com
34  ****************************************************************************
35  *
36  */
37 #ifndef _SharkSslASN1_h
38 #define _SharkSslASN1_h
39 
45 #include "SharkSslEx.h"
46 
53 #define SHARKSSL_ASN1_BOOLEAN 0x01
54 #define SHARKSSL_ASN1_INTEGER 0x02
55 #define SHARKSSL_ASN1_BIT_STRING 0x03
56 #define SHARKSSL_ASN1_OCTET_STRING 0x04
57 #define SHARKSSL_ASN1_NULL 0x05
58 #define SHARKSSL_ASN1_OID 0x06
59 #define SHARKSSL_ASN1_UTF8_STRING 0x0C
60 #define SHARKSSL_ASN1_SEQUENCE 0x10
61 #define SHARKSSL_ASN1_SET 0x11
62 #define SHARKSSL_ASN1_PRINTABLE_STRING 0x13
63 #define SHARKSSL_ASN1_T61_STRING 0x14
64 #define SHARKSSL_ASN1_IA5_STRING 0x16
65 #define SHARKSSL_ASN1_UTC_TIME 0x17
66 #define SHARKSSL_ASN1_GENERALIZED_TIME 0x18
67 #define SHARKSSL_ASN1_BMP_STRING 0x1E
68 #define SHARKSSL_ASN1_CONSTRUCTED 0x20
69 #define SHARKSSL_ASN1_CONTEXT_SPECIFIC 0x80
70 
74 #define SHARKSSL_OID_JIIT_DS 0x55
75 #define SHARKSSL_OID_JIIT_DS_ATTRTYPE 0x04
76 #define SHARKSSL_OID_JIIT_DS_ATTRTYPE_CN 0x03
77 #define SHARKSSL_OID_JIIT_DS_ATTRTYPE_SERIAL 0x05
78 #define SHARKSSL_OID_JIIT_DS_ATTRTYPE_COUNTRY 0x06
79 #define SHARKSSL_OID_JIIT_DS_ATTRTYPE_LOCALITY 0x07
80 #define SHARKSSL_OID_JIIT_DS_ATTRTYPE_PROVINCE 0x08
81 #define SHARKSSL_OID_JIIT_DS_ATTRTYPE_ORGANIZATION 0x0A
82 #define SHARKSSL_OID_JIIT_DS_ATTRTYPE_UNIT 0x0B
83 
86 #define SHARKSSL_OID_JIIT_DS_CERTEXT 0x1D
87 #define SHARKSSL_OID_JIIT_DS_CERTEXT_KEYUSAGE 0x0F
88 #define SHARKSSL_OID_JIIT_DS_CERTEXT_SUBJALTNAMES 0x11
89 #define SHARKSSL_OID_JIIT_DS_CERTEXT_BASICCONSTRAINTS 0x13
90 
96 #define SHARKSSL_CSR_TAG_ATTRIBUTES 0x00
97 
103 #define SHARKSSL_X509_TAG_VERSION 0x00
104 #define SHARKSSL_X509_TAG_ISSUERUNIQUEID 0x01
105 #define SHARKSSL_X509_TAG_SUBJECTUNIQUEID 0x02
106 #define SHARKSSL_X509_TAG_EXTENSIONS 0x03
107 
112 #define SHARKSSL_ECC_PRIVKEY_TAG_PARAMETERS 0x00
113 #define SHARKSSL_ECC_PRIVKEY_TAG_PUBLIC_KEY 0x01
114 
118 #define SHARKSSL_X509_KU_DIGITAL_SIGNATURE 0x80
119 #define SHARKSSL_X509_KU_NON_REPUDIATION 0x40
120 #define SHARKSSL_X509_KU_KEY_ENCIPHERMENT 0x20
121 #define SHARKSSL_X509_KU_DATA_ENCIPHERMENT 0x10
122 #define SHARKSSL_X509_KU_KEY_AGREEMENT 0x08
123 #define SHARKSSL_X509_KU_KEY_CERT_SIGN 0x04
124 #define SHARKSSL_X509_KU_CRL_SIGN 0x02
125 
130 #define SHARKSSL_X509_NS_CERT_TYPE_SSL_CLIENT 0x80
131 #define SHARKSSL_X509_NS_CERT_TYPE_SSL_SERVER 0x40
132 #define SHARKSSL_X509_NS_CERT_TYPE_OBJECT_SIGNING 0x10
133 #define SHARKSSL_X509_NS_CERT_TYPE_SSL_CA 0x04
134 #define SHARKSSL_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA 0x01
135 
136 
139 
140 
141 /* OID's - length has to match the content in SharkSslASN1.c */
142 extern const U8 sharkssl_oid_CN[3];
143 extern const U8 sharkssl_oid_serial[3];
144 extern const U8 sharkssl_oid_country[3];
145 extern const U8 sharkssl_oid_locality[3];
146 extern const U8 sharkssl_oid_province[3];
147 extern const U8 sharkssl_oid_organization[3];
148 extern const U8 sharkssl_oid_unit[3];
149 extern const U8 sharkssl_oid_emailAddress[9];
150 extern const U8 sharkssl_oid_csr_ext_req[9];
151 extern const U8 sharkssl_oid_signedData[9];
152 extern const U8 sharkssl_oid_ns_cert_type[9];
153 extern const U8 sharkssl_oid_key_usage[3];
154 extern const U8 sharkssl_oid_san[3];
155 extern const U8 sharkssl_oid_basic_constraints[3];
156 extern const U8 sharkssl_oid_ecPublicKey[7];
157 extern const U8 sharkssl_oid_prime192v1[8];
158 extern const U8 sharkssl_oid_prime256v1[8];
159 extern const U8 sharkssl_oid_ecdsaWithSHA1[7];
160 extern const U8 sharkssl_oid_ecdsaWithSHA256[8];
161 extern const U8 sharkssl_oid_ecdsaWithSHA384[8];
162 extern const U8 sharkssl_oid_ecdsaWithSHA512[8];
163 extern const U8 sharkssl_oid_secp224r1[5];
164 extern const U8 sharkssl_oid_secp384r1[5];
165 extern const U8 sharkssl_oid_secp521r1[5];
166 extern const U8 sharkssl_oid_brainpoolP256r1[9];
167 extern const U8 sharkssl_oid_brainpoolP384r1[9];
168 extern const U8 sharkssl_oid_brainpoolP512r1[9];
169 extern const U8 sharkssl_oid_rsaEncryption[9];
170 extern const U8 sharkssl_oid_md2withRSAEncryption[9];
171 extern const U8 sharkssl_oid_md5withRSAEncryption[9];
172 extern const U8 sharkssl_oid_sha1withRSAEncryption[9];
173 extern const U8 sharkssl_oid_sha256withRSAEncryption[9];
174 extern const U8 sharkssl_oid_sha384withRSAEncryption[9];
175 extern const U8 sharkssl_oid_sha512withRSAEncryption[9];
176 extern const U8 sharkssl_oid_md5[8];
177 extern const U8 sharkssl_oid_sha1[5];
178 extern const U8 sharkssl_oid_sha256[9];
179 extern const U8 sharkssl_oid_sha384[9];
180 extern const U8 sharkssl_oid_sha512[9];
181 extern const U8 sharkssl_oid_pkcs5PBES2[9];
182 extern const U8 sharkssl_oid_pkcs5PBKDF2[9];
183 extern const U8 sharkssl_oid_HMACWithSHA256[8];
184 #if SHARKSSL_ENABLE_AES_CBC
185 extern const U8 sharkssl_oid_aes128cbc[9];
186 extern const U8 sharkssl_oid_aes256cbc[9];
187 #endif
188 
189 
190 #define sharkssl_oid_ex(type) (const U8*)sharkssl_oid_ ## type, (sizeof(sharkssl_oid_ ## type)/sizeof(sharkssl_oid_ ## type[0]))
191 
192 
193 
194 /* never call these functions directly but only through the macros below */
195 int SharkSslParseASN1_getLength(SharkSslParseASN1 *o);
196 SHARKSSL_API int SharkSslParseASN1_getType(SharkSslParseASN1 *o, U8 ASN1type); /* dataptr, datalen are valid */
197 SHARKSSL_API int SharkSslParseASN1_getSetSeq(SharkSslParseASN1 *o, U8 ASN1id);
198 
199 #define SharkSslParseASN1_getBool(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_BOOLEAN)
200 #define SharkSslParseASN1_getInt(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_INTEGER)
201 #define SharkSslParseASN1_getBitString(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_BIT_STRING)
202 #define SharkSslParseASN1_getOctetString(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_OCTET_STRING)
203 #define SharkSslParseASN1_getNULL(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_NULL)
204 #define SharkSslParseASN1_getOID(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_OID)
205 #define SharkSslParseASN1_getUTCTime(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_UTC_TIME)
206 #define SharkSslParseASN1_getGenTime(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_GENERALIZED_TIME)
207 #define SharkSslParseASN1_getIssuerUniqueID(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_ISSUERUNIQUEID)
208 #define SharkSslParseASN1_getSubjectUniqueID(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_SUBJECTUNIQUEID)
209 #define SharkSslParseASN1_getExtensions(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_EXTENSIONS)
210 #define SharkSslParseASN1_getCSRAttributes(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_CSR_TAG_ATTRIBUTES)
211 
212 #define SharkSslParseASN1_getECParameters(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ECC_PRIVKEY_TAG_PARAMETERS)
213 #define SharkSslParseASN1_getECPublicKey(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ECC_PRIVKEY_TAG_PUBLIC_KEY)
214 #define SharkSslParseASN1_getVersion(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_VERSION)
215 #define SharkSslParseASN1_getSet(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ASN1_SET)
216 #define SharkSslParseASN1_getSequence(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ASN1_SEQUENCE)
217 
218 int SharkSslParseASN1_getContextSpecific(SharkSslParseASN1 *o, U8 *tag);
219 
220 
226 typedef struct SharkSslASN1Create
227 {
228  U8 *start;
229  U8 *end;
230  U8 *ptr;
232 
237 typedef struct SharkSslBitExtReq
238 {
239  U8 bits;
241 
242 
243 #define SharkSslASN1Create_getLen(o) ((int)((o)->end - (o)->ptr))
244 #define SharkSslASN1Create_getData(o) ((o)->ptr)
245 
251 #define SharkSslASN1Create_getDataLen(o, startOfDataPtr) (*(startOfDataPtr) = (o)->ptr, (int)((o)->end - (o)->ptr))
252 
253 SHARKSSL_API void SharkSslASN1Create_constructor(SharkSslASN1Create *o, U8 *buf, int bufSize);
254 SHARKSSL_API int SharkSslASN1Create_int(SharkSslASN1Create *o, const U8 *intptr, int len);
255 
256 SHARKSSL_API int SharkSslASN1Create_length(SharkSslASN1Create *o, int len);
257 SHARKSSL_API int SharkSslASN1Create_tag(SharkSslASN1Create *o, U8 ASN1type);
258 
259 #define SharkSslASN1Create_bitString(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_BIT_STRING)
260 #define SharkSslASN1Create_octetString(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_OCTET_STRING)
261 #define SharkSslASN1Create_printableString(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_PRINTABLE_STRING)
262 #define SharkSslASN1Create_IA5String(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_IA5_STRING)
263 #define SharkSslASN1Create_extensions(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_EXTENSIONS)
264 #define SharkSslASN1Create_version(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_VERSION)
265 #define SharkSslASN1Create_CSRAttributes(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_CSR_TAG_ATTRIBUTES)
266 #define SharkSslASN1Create_ECParameters(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ECC_PRIVKEY_TAG_PARAMETERS)
267 #define SharkSslASN1Create_ECPublicKey(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ECC_PRIVKEY_TAG_PUBLIC_KEY)
268 
269 #define SharkSslASN1Create_set(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ASN1_SET)
270 #define SharkSslASN1Create_sequence(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ASN1_SEQUENCE)
271 
272 #if (SHARKSSL_ENABLE_CSR_CREATION || SHARKSSL_ENABLE_CSR_SIGNING || SHARKSSL_ENABLE_ASN1_KEY_CREATION)
273 SHARKSSL_API int SharkSslASN1Create_oid(SharkSslASN1Create *o, const U8 *oid, int oidLen);
274 SHARKSSL_API int SharkSslASN1Create_raw(SharkSslASN1Create *o, const void *data, int size);
275 #endif
276 #if (SHARKSSL_ENABLE_CSR_CREATION || SHARKSSL_ENABLE_CSR_SIGNING)
277 SHARKSSL_API int SharkSslASN1Create_email(SharkSslASN1Create *o, const U8 *oid, int oidLen, const U8 *email, int emailLen);
278 SHARKSSL_API int SharkSslASN1Create_name(SharkSslASN1Create *o, const U8 *oid, int oidLen, const U8 *name, int nameLen);
279 #endif
280 #if (SHARKSSL_ENABLE_CSR_SIGNING)
281 SHARKSSL_API int SharkSslASN1Create_boolean(SharkSslASN1Create *o, U8 boolean);
282 #endif
283 
284 
285 /* Semi public (non documented) types and functions */
286 SHARKSSL_API int sharkssl_PEM_getSeqVersion(SharkSslParseASN1 *parseKey, U32 len);
287 SHARKSSL_API U8 SharkSslParseASN1_getAlgoID(const SharkSslParseASN1 *o);
288 SHARKSSL_API U8 SharkSslParseASN1_getCurveID(const SharkSslParseASN1 *o);
289 
290 /* heterogeneous usage of _getAlgoID to detect ecPublicKey OID in EC
291  * public certs
292 */
293 #define ALGO_OID_EC_PUBLIC_KEY SHARKSSL_OID_EC_PUBLIC_KEY
294 
295 #define SHARKSSL_EC_CURVE_ID_UNKNOWN 0
296 #define SHARKSSL_EC_POINT_UNCOMPRESSED 0x04
297 
298 
299 #endif
SharkSslBitExtReq
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/nss_tech_notes/nss_tech_note3 http://to...
Definition: SharkSslASN1.h:238
SharkSslASN1Create
struct SharkSslASN1Create SharkSslASN1Create
Opaque object used when creating ASN.1 encoded data.
SharkSslBitExtReq
struct SharkSslBitExtReq SharkSslBitExtReq
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/nss_tech_notes/nss_tech_note3 http://to...
SubjectAltNameEnumerator
SubjectAltName Iterator.
Definition: SharkSslEx.h:52
SharkSslASN1Create
Opaque object used when creating ASN.1 encoded data.
Definition: SharkSslASN1.h:227