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 - 2022
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
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 */
142extern const U8 sharkssl_oid_CN[3];
143extern const U8 sharkssl_oid_serial[3];
144extern const U8 sharkssl_oid_country[3];
145extern const U8 sharkssl_oid_locality[3];
146extern const U8 sharkssl_oid_province[3];
147extern const U8 sharkssl_oid_organization[3];
148extern const U8 sharkssl_oid_unit[3];
149extern const U8 sharkssl_oid_emailAddress[9];
150extern const U8 sharkssl_oid_csr_ext_req[9];
151extern const U8 sharkssl_oid_signedData[9];
152extern const U8 sharkssl_oid_ns_cert_type[9];
153extern const U8 sharkssl_oid_key_usage[3];
154extern const U8 sharkssl_oid_san[3];
155extern const U8 sharkssl_oid_basic_constraints[3];
156extern const U8 sharkssl_oid_ecPublicKey[7];
157extern const U8 sharkssl_oid_prime192v1[8];
158extern const U8 sharkssl_oid_prime256v1[8];
159extern const U8 sharkssl_oid_ecdsaWithSHA1[7];
160extern const U8 sharkssl_oid_ecdsaWithSHA256[8];
161extern const U8 sharkssl_oid_ecdsaWithSHA384[8];
162extern const U8 sharkssl_oid_ecdsaWithSHA512[8];
163extern const U8 sharkssl_oid_secp224r1[5];
164extern const U8 sharkssl_oid_secp384r1[5];
165extern const U8 sharkssl_oid_secp521r1[5];
166extern const U8 sharkssl_oid_brainpoolP256r1[9];
167extern const U8 sharkssl_oid_brainpoolP384r1[9];
168extern const U8 sharkssl_oid_brainpoolP512r1[9];
169extern const U8 sharkssl_oid_curve25519[9];
170extern const U8 sharkssl_oid_rsaEncryption[9];
171extern const U8 sharkssl_oid_md2withRSAEncryption[9];
172extern const U8 sharkssl_oid_md5withRSAEncryption[9];
173extern const U8 sharkssl_oid_sha1withRSAEncryption[9];
174extern const U8 sharkssl_oid_sha256withRSAEncryption[9];
175extern const U8 sharkssl_oid_sha384withRSAEncryption[9];
176extern const U8 sharkssl_oid_sha512withRSAEncryption[9];
177extern const U8 sharkssl_oid_md5[8];
178extern const U8 sharkssl_oid_sha1[5];
179extern const U8 sharkssl_oid_sha256[9];
180extern const U8 sharkssl_oid_sha384[9];
181extern const U8 sharkssl_oid_sha512[9];
182extern const U8 sharkssl_oid_pkcs5PBES2[9];
183extern const U8 sharkssl_oid_pkcs5PBKDF2[9];
184extern const U8 sharkssl_oid_HMACWithSHA256[8];
185#if SHARKSSL_ENABLE_AES_CBC
186extern const U8 sharkssl_oid_aes128cbc[9];
187extern const U8 sharkssl_oid_aes256cbc[9];
188#endif
189
190
191#define sharkssl_oid_ex(type) (const U8*)sharkssl_oid_ ## type, (sizeof(sharkssl_oid_ ## type)/sizeof(sharkssl_oid_ ## type[0]))
192
193
194
195/* never call these functions directly but only through the macros below */
196int SharkSslParseASN1_getLength(SharkSslParseASN1 *o);
197SHARKSSL_API int SharkSslParseASN1_getType(SharkSslParseASN1 *o, U8 ASN1type); /* dataptr, datalen are valid */
198SHARKSSL_API int SharkSslParseASN1_getSetSeq(SharkSslParseASN1 *o, U8 ASN1id);
199
200#define SharkSslParseASN1_getBool(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_BOOLEAN)
201#define SharkSslParseASN1_getInt(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_INTEGER)
202#define SharkSslParseASN1_getBitString(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_BIT_STRING)
203#define SharkSslParseASN1_getOctetString(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_OCTET_STRING)
204#define SharkSslParseASN1_getNULL(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_NULL)
205#define SharkSslParseASN1_getOID(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_OID)
206#define SharkSslParseASN1_getUTCTime(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_UTC_TIME)
207#define SharkSslParseASN1_getGenTime(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_GENERALIZED_TIME)
208#define SharkSslParseASN1_getIssuerUniqueID(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_ISSUERUNIQUEID)
209#define SharkSslParseASN1_getSubjectUniqueID(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_SUBJECTUNIQUEID)
210#define SharkSslParseASN1_getExtensions(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_EXTENSIONS)
211#define SharkSslParseASN1_getCSRAttributes(o) SharkSslParseASN1_getType(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_CSR_TAG_ATTRIBUTES)
212
213#define SharkSslParseASN1_getECParameters(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ECC_PRIVKEY_TAG_PARAMETERS)
214#define SharkSslParseASN1_getECPublicKey(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ECC_PRIVKEY_TAG_PUBLIC_KEY)
215#define SharkSslParseASN1_getVersion(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_VERSION)
216#define SharkSslParseASN1_getSet(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ASN1_SET)
217#define SharkSslParseASN1_getSequence(o) SharkSslParseASN1_getSetSeq(o, SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ASN1_SEQUENCE)
218
219int SharkSslParseASN1_getContextSpecific(SharkSslParseASN1 *o, U8 *tag);
220
221
227typedef struct SharkSslASN1Create
228{
229 U8 *start;
230 U8 *end;
231 U8 *ptr;
233
238typedef struct SharkSslBitExtReq
239{
240 U8 bits;
242
243
244#define SharkSslASN1Create_getLen(o) ((int)((o)->end - (o)->ptr))
245#define SharkSslASN1Create_getData(o) ((o)->ptr)
246
252#define SharkSslASN1Create_getDataLen(o, startOfDataPtr) (*(startOfDataPtr) = (o)->ptr, (int)((o)->end - (o)->ptr))
253
254SHARKSSL_API void SharkSslASN1Create_constructor(SharkSslASN1Create *o, U8 *buf, int bufSize);
255SHARKSSL_API int SharkSslASN1Create_int(SharkSslASN1Create *o, const U8 *intptr, int len);
256
257SHARKSSL_API int SharkSslASN1Create_length(SharkSslASN1Create *o, int len);
258SHARKSSL_API int SharkSslASN1Create_tag(SharkSslASN1Create *o, U8 ASN1type);
259
260#define SharkSslASN1Create_bitString(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_BIT_STRING)
261#define SharkSslASN1Create_octetString(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_OCTET_STRING)
262#define SharkSslASN1Create_printableString(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_PRINTABLE_STRING)
263#define SharkSslASN1Create_IA5String(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_IA5_STRING)
264#define SharkSslASN1Create_extensions(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_EXTENSIONS)
265#define SharkSslASN1Create_version(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_X509_TAG_VERSION)
266#define SharkSslASN1Create_CSRAttributes(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_CSR_TAG_ATTRIBUTES)
267#define SharkSslASN1Create_ECParameters(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ECC_PRIVKEY_TAG_PARAMETERS)
268#define SharkSslASN1Create_ECPublicKey(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONTEXT_SPECIFIC | SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ECC_PRIVKEY_TAG_PUBLIC_KEY)
269
270#define SharkSslASN1Create_set(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ASN1_SET)
271#define SharkSslASN1Create_sequence(o) SharkSslASN1Create_tag(o, SHARKSSL_ASN1_CONSTRUCTED | SHARKSSL_ASN1_SEQUENCE)
272
273#if (SHARKSSL_ENABLE_CSR_CREATION || SHARKSSL_ENABLE_CSR_SIGNING || SHARKSSL_ENABLE_ASN1_KEY_CREATION)
274SHARKSSL_API int SharkSslASN1Create_oid(SharkSslASN1Create *o, const U8 *oid, int oidLen);
275SHARKSSL_API int SharkSslASN1Create_raw(SharkSslASN1Create *o, const void *data, int size);
276#endif
277#if (SHARKSSL_ENABLE_CSR_CREATION || SHARKSSL_ENABLE_CSR_SIGNING)
278SHARKSSL_API int SharkSslASN1Create_email(SharkSslASN1Create *o, const U8 *oid, int oidLen, const U8 *email, int emailLen);
279SHARKSSL_API int SharkSslASN1Create_name(SharkSslASN1Create *o, const U8 *oid, int oidLen, const U8 *name, int nameLen);
280#endif
281#if (SHARKSSL_ENABLE_CSR_SIGNING)
282SHARKSSL_API int SharkSslASN1Create_boolean(SharkSslASN1Create *o, U8 boolean);
283#endif
284
285
286/* Semi public (non documented) types and functions */
287SHARKSSL_API int sharkssl_PEM_getSeqVersion(SharkSslParseASN1 *parseKey, U32 len);
288SHARKSSL_API U8 SharkSslParseASN1_getAlgoID(const SharkSslParseASN1 *o);
289SHARKSSL_API U8 SharkSslParseASN1_getCurveID(const SharkSslParseASN1 *o);
290
291/* heterogeneous usage of _getAlgoID to detect ecPublicKey OID in EC
292 * public certs
293*/
294#define ALGO_OID_EC_PUBLIC_KEY SHARKSSL_OID_EC_PUBLIC_KEY
295
296#define SHARKSSL_EC_CURVE_ID_UNKNOWN 0
297#define SHARKSSL_EC_POINT_UNCOMPRESSED 0x04
298
299
300#endif
struct SharkSslBitExtReq SharkSslBitExtReq
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/nss_tech_notes/nss_tech_note3 http://to...
struct SharkSslASN1Create SharkSslASN1Create
Opaque object used when creating ASN.1 encoded data.
Opaque object used when creating ASN.1 encoded data.
Definition: SharkSslASN1.h:228
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/nss_tech_notes/nss_tech_note3 http://to...
Definition: SharkSslASN1.h:239
SubjectAltName Iterator.
Definition: SharkSslEx.h:52