3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 #ifndef CTAO_CRYPT_ASN_H
25 #define CTAO_CRYPT_ASN_H
27 #include <cyassl/ctaocrypt/types.h>
28 #include <cyassl/ctaocrypt/rsa.h>
29 #include <cyassl/ctaocrypt/dh.h>
30 #include <cyassl/ctaocrypt/dsa.h>
31 #include <cyassl/ctaocrypt/sha.h>
32 #include <cyassl/ctaocrypt/md5.h>
33 #include <cyassl/ctaocrypt/asn_public.h> /* public interface */
35 #include <cyassl/ctaocrypt/ecc.h>
47 EXTERNAL_SERIAL_SIZE = 32,
57 ASN_BIT_STRING = 0x03,
58 ASN_OCTET_STRING = 0x04,
61 ASN_ENUMERATED = 0x0a,
62 ASN_UTF8STRING = 0x0c,
66 ASN_OTHER_TYPE = 0x00,
67 ASN_RFC822_TYPE = 0x01,
70 ASN_GENERALIZED_TIME = 0x18,
71 CRL_EXTENSIONS = 0xa0,
72 ASN_EXTENSIONS = 0xa3,
73 ASN_LONG_LENGTH = 0x80
77 ASN_CONSTRUCTED = 0x20,
78 ASN_CONTEXT_SPECIFIC = 0x80
82 ASN_COMMON_NAME = 0x03, /* CN */
83 ASN_SUR_NAME = 0x04, /* SN */
84 ASN_SERIAL_NUMBER = 0x05, /* serialNumber */
85 ASN_COUNTRY_NAME = 0x06, /* C */
86 ASN_LOCALITY_NAME = 0x07, /* L */
87 ASN_STATE_NAME = 0x08, /* ST */
88 ASN_ORG_NAME = 0x0a, /* O */
89 ASN_ORGUNIT_NAME = 0x0b /* OU */
97 PBES2 = 13 /* algo ID */
100 enum ENCRYPTION_TYPES {
113 MAX_SALT_SIZE = 64, /* MAX PKCS Salt length */
114 MAX_IV_SIZE = 64, /* MAX PKCS Iv length */
115 MAX_KEY_SIZE = 64, /* MAX PKCS Key length */
116 PKCS5 = 5, /* PKCS oid tag */
117 PKCS5v2 = 6, /* PKCS #5 v2.0 */
118 PKCS12 = 12, /* PKCS #12 */
119 MAX_UNICODE_SZ = 256,
120 ASN_BOOL_SIZE = 2, /* including type */
122 RSA_INTS = 8, /* RSA ints in private key */
125 ASN_GEN_TIME_SZ = 15, /* 7 numbers * 2 + Zulu tag */
126 MAX_ENCODED_SIG_SZ = 512,
129 MAX_SEQ_SZ = 5, /* enum(seq | con) + length(4) */
130 MAX_SET_SZ = 5, /* enum(set | con) + length(4) */
131 MAX_OCTET_STR_SZ = 5, /* enum(set | con) + length(4) */
132 MAX_EXP_SZ = 5, /* enum(contextspec|con|exp) + length(4) */
133 MAX_PRSTR_SZ = 5, /* enum(prstr) + length(4) */
134 MAX_VERSION_SZ = 5, /* enum + id + version(byte) + (header(2))*/
135 MAX_ENCODED_DIG_SZ = 73, /* sha512 + enum(bit or octet) + legnth(4) */
136 MAX_RSA_INT_SZ = 517, /* RSA raw sz 4096 for bits + tag + len(4) */
137 MAX_NTRU_KEY_SZ = 610, /* NTRU 112 bit public key */
138 MAX_NTRU_ENC_SZ = 628, /* NTRU 112 bit DER public encoding */
139 MAX_LENGTH_SZ = 4, /* Max length size for DER encoding */
140 MAX_RSA_E_SZ = 16, /* Max RSA public e size */
141 MAX_CA_SZ = 32, /* Max encoded CA basic constraint length */
142 MAX_SN_SZ = 35, /* Max encoded serial number (INT) length */
143 #ifdef CYASSL_CERT_GEN
144 #ifdef CYASSL_CERT_REQ
145 /* Max encoded cert req attributes length */
146 MAX_ATTRIB_SZ = MAX_SEQ_SZ * 3 + (11 + MAX_SEQ_SZ) * 2 +
147 MAX_PRSTR_SZ + CTC_NAME_SIZE, /* 11 is the OID size */
149 #ifdef CYASSL_ALT_NAMES
150 MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + CTC_MAX_ALT_SIZE,
152 MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + MAX_CA_SZ,
154 /* Max total extensions, id + len + others */
156 MAX_OCSP_EXT_SZ = 58, /* Max OCSP Extension length */
157 MAX_OCSP_NONCE_SZ = 18, /* OCSP Nonce size */
158 EIGHTK_BUF = 8192, /* Tmp buffer size */
159 MAX_PUBLIC_KEY_SZ = MAX_NTRU_ENC_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ * 2
160 /* use bigger NTRU size */
212 enum Extensions_Sum {
220 CERT_POLICY_OID = 146,
221 KEY_USAGE_OID = 129, /* 2.5.29.15 */
222 INHIBIT_ANY_OID = 168, /* 2.5.29.54 */
223 EXT_KEY_USAGE_OID = 151, /* 2.5.29.37 */
224 NAME_CONS_OID = 144 /* 2.5.29.30 */
227 enum CertificatePolicy_Sum {
228 CP_ANY_OID = 146 /* id-ce 32 0 */
231 enum SepHardwareName_Sum {
232 HW_NAME_OID = 79 /* 1.3.6.1.5.5.7.8.4 from RFC 4108*/
236 AIA_OCSP_OID = 116, /* 1.3.6.1.5.5.7.48.1 */
237 AIA_CA_ISSUER_OID = 117 /* 1.3.6.1.5.5.7.48.2 */
240 enum ExtKeyUsage_Sum { /* From RFC 5280 */
241 EKU_ANY_OID = 151, /* 2.5.29.37.0, anyExtendedKeyUsage */
242 EKU_SERVER_AUTH_OID = 71, /* 1.3.6.1.5.5.7.3.1, id-kp-serverAuth */
243 EKU_CLIENT_AUTH_OID = 72, /* 1.3.6.1.5.5.7.3.2, id-kp-clientAuth */
244 EKU_OCSP_SIGN_OID = 79, /* 1.3.6.1.5.5.7.3.9, OCSPSigning */
254 /* Key usage extension bits */
255 #define KEYUSE_DIGITAL_SIG 0x0100
256 #define KEYUSE_CONTENT_COMMIT 0x0080
257 #define KEYUSE_KEY_ENCIPHER 0x0040
258 #define KEYUSE_DATA_ENCIPHER 0x0020
259 #define KEYUSE_KEY_AGREE 0x0010
260 #define KEYUSE_KEY_CERT_SIGN 0x0008
261 #define KEYUSE_CRL_SIGN 0x0004
262 #define KEYUSE_ENCIPHER_ONLY 0x0002
263 #define KEYUSE_DECIPHER_ONLY 0x0001
265 #define EXTKEYUSE_ANY 0x08
266 #define EXTKEYUSE_OCSP_SIGN 0x04
267 #define EXTKEYUSE_CLIENT_AUTH 0x02
268 #define EXTKEYUSE_SERVER_AUTH 0x01
270 typedef struct DNS_entry DNS_entry;
273 DNS_entry* next; /* next on DNS list */
274 char* name; /* actual DNS name */
278 typedef struct Base_entry Base_entry;
281 Base_entry* next; /* next on name base list */
282 char* name; /* actual name base */
283 int nameSz; /* name length */
284 byte type; /* Name base type (DNS or RFC822) */
315 typedef struct DecodedCert DecodedCert;
316 typedef struct DecodedName DecodedName;
317 typedef struct Signer Signer;
324 word32 certBegin; /* offset to start of cert */
325 word32 sigIndex; /* offset to start of signature */
326 word32 sigLength; /* length of signature */
327 word32 signatureOID; /* sum of algorithm object id */
328 word32 keyOID; /* sum of key algo object id */
329 int version; /* cert version, 1 or 3 */
330 DNS_entry* altNames; /* alt names list of dns entries */
331 #ifndef IGNORE_NAME_CONSTRAINTS
332 DNS_entry* altEmailNames; /* alt names list of RFC822 entries */
333 Base_entry* permittedNames; /* Permitted name bases */
334 Base_entry* excludedNames; /* Excluded name bases */
335 #endif /* IGNORE_NAME_CONSTRAINTS */
336 byte subjectHash[SHA_SIZE]; /* hash of all Names */
337 byte issuerHash[SHA_SIZE]; /* hash of all Names */
339 byte issuerKeyHash[SHA_SIZE]; /* hash of the public Key */
340 #endif /* HAVE_OCSP */
341 byte* signature; /* not owned, points into raw cert */
342 char* subjectCN; /* CommonName */
343 int subjectCNLen; /* CommonName Length */
344 char subjectCNEnc; /* CommonName Encoding */
345 int subjectCNStored; /* have we saved a copy we own */
346 char issuer[ASN_NAME_MAX]; /* full name including common name */
347 char subject[ASN_NAME_MAX]; /* full name including common name */
348 int verify; /* Default to yes, but could be off */
349 byte* source; /* byte buffer holder cert, NOT owner */
350 word32 srcIdx; /* current offset into buffer */
351 word32 maxIdx; /* max offset based on init size */
352 void* heap; /* for user memory overrides */
353 byte serial[EXTERNAL_SERIAL_SIZE]; /* raw serial number */
354 int serialSz; /* raw serial bytes stored */
355 byte* extensions; /* not owned, points into raw cert */
356 int extensionsSz; /* length of cert extensions */
357 word32 extensionsIdx; /* if want to go back and parse later */
358 byte* extAuthInfo; /* Authority Information Access URI */
359 int extAuthInfoSz; /* length of the URI */
360 byte* extCrlInfo; /* CRL Distribution Points */
361 int extCrlInfoSz; /* length of the URI */
362 byte extSubjKeyId[SHA_SIZE]; /* Subject Key ID */
363 byte extSubjKeyIdSet; /* Set when the SKID was read from cert */
364 byte extAuthKeyId[SHA_SIZE]; /* Authority Key ID */
365 byte extAuthKeyIdSet; /* Set when the AKID was read from cert */
366 #ifndef IGNORE_NAME_CONSTRAINTS
367 byte extNameConstraintSet;
368 #endif /* IGNORE_NAME_CONSTRAINTS */
369 byte isCA; /* CA basic constraint true */
371 word16 extKeyUsage; /* Key usage bitfield */
372 byte extExtKeyUsageSet; /* Extended Key Usage */
373 byte extExtKeyUsage; /* Extended Key usage bitfield */
375 byte extBasicConstSet;
376 byte extBasicConstCrit;
377 byte extBasicConstPlSet;
378 word32 pathLength; /* CA basic constraint path length, opt */
379 byte extSubjAltNameSet;
380 byte extSubjAltNameCrit;
381 byte extAuthKeyIdCrit;
382 #ifndef IGNORE_NAME_CONSTRAINTS
383 byte extNameConstraintCrit;
384 #endif /* IGNORE_NAME_CONSTRAINTS */
385 byte extSubjKeyIdCrit;
386 byte extKeyUsageCrit;
387 byte extExtKeyUsageCrit;
388 byte* extExtKeyUsageSrc;
389 word32 extExtKeyUsageSz;
390 word32 extExtKeyUsageCount;
391 byte* extAuthKeyIdSrc;
392 word32 extAuthKeyIdSz;
393 byte* extSubjKeyIdSrc;
394 word32 extSubjKeyIdSz;
397 word32 pkCurveOID; /* Public Key's curve OID */
398 #endif /* HAVE_ECC */
404 byte* issuerRaw; /* pointer to issuer inside source */
407 #ifndef IGNORE_NAME_CONSTRAINT
408 byte* subjectRaw; /* pointer to subject inside source */
411 #if defined(CYASSL_CERT_GEN)
412 /* easy access to subject info for other sign */
433 #endif /* CYASSL_CERT_GEN */
435 DecodedName issuerName;
436 DecodedName subjectName;
437 #endif /* OPENSSL_EXTRA */
446 byte extCertPolicySet;
447 byte extCertPolicyCrit;
448 #endif /* OPENSSL_EXTRA */
449 #endif /* CYASSL_SEP */
453 #ifdef SHA_DIGEST_SIZE
454 #define SIGNER_DIGEST_SIZE SHA_DIGEST_SIZE
456 #define SIGNER_DIGEST_SIZE 20
460 /* if change layout change PERSIST_CERT_CACHE functions too */
463 word32 keyOID; /* key type */
467 char* name; /* common name */
468 #ifndef IGNORE_NAME_CONSTRAINTS
469 Base_entry* permittedNames;
470 Base_entry* excludedNames;
471 #endif /* IGNORE_NAME_CONSTRAINTS */
472 byte subjectNameHash[SIGNER_DIGEST_SIZE];
473 /* sha hash of names in certificate */
475 byte subjectKeyIdHash[SIGNER_DIGEST_SIZE];
476 /* sha hash of names in certificate */
482 /* not for public consumption but may use for testing sometimes */
483 #ifdef CYASSL_TEST_CERT
484 #define CYASSL_TEST_API CYASSL_API
486 #define CYASSL_TEST_API CYASSL_LOCAL
489 CYASSL_TEST_API void FreeAltNames(DNS_entry*, void*);
490 #ifndef IGNORE_NAME_CONSTRAINTS
491 CYASSL_TEST_API void FreeNameSubtrees(Base_entry*, void*);
492 #endif /* IGNORE_NAME_CONSTRAINTS */
493 CYASSL_TEST_API void InitDecodedCert(DecodedCert*, byte*, word32, void*);
494 CYASSL_TEST_API void FreeDecodedCert(DecodedCert*);
495 CYASSL_TEST_API int ParseCert(DecodedCert*, int type, int verify, void* cm);
497 CYASSL_LOCAL int ParseCertRelative(DecodedCert*, int type, int verify,void* cm);
498 CYASSL_LOCAL int DecodeToKey(DecodedCert*, int verify);
500 CYASSL_LOCAL word32 EncodeSignature(byte* out, const byte* digest, word32 digSz,
503 CYASSL_LOCAL Signer* MakeSigner(void*);
504 CYASSL_LOCAL void FreeSigner(Signer*, void*);
505 CYASSL_LOCAL void FreeSignerTable(Signer**, int, void*);
508 CYASSL_LOCAL int ToTraditional(byte* buffer, word32 length);
509 CYASSL_LOCAL int ToTraditionalEnc(byte* buffer, word32 length,const char*, int);
511 CYASSL_LOCAL int ValidateDate(const byte* date, byte format, int dateType);
513 /* ASN.1 helper functions */
514 CYASSL_LOCAL int GetLength(const byte* input, word32* inOutIdx, int* len,
516 CYASSL_LOCAL int GetSequence(const byte* input, word32* inOutIdx, int* len,
518 CYASSL_LOCAL int GetSet(const byte* input, word32* inOutIdx, int* len,
520 CYASSL_LOCAL int GetMyVersion(const byte* input, word32* inOutIdx,
522 CYASSL_LOCAL int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx,
524 CYASSL_LOCAL int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
526 CYASSL_LOCAL word32 SetLength(word32 length, byte* output);
527 CYASSL_LOCAL word32 SetSequence(word32 len, byte* output);
528 CYASSL_LOCAL word32 SetOctetString(word32 len, byte* output);
529 CYASSL_LOCAL word32 SetImplicit(byte tag, byte number, word32 len,byte* output);
530 CYASSL_LOCAL word32 SetExplicit(byte number, word32 len, byte* output);
531 CYASSL_LOCAL word32 SetSet(word32 len, byte* output);
532 CYASSL_LOCAL word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz);
533 CYASSL_LOCAL int SetMyVersion(word32 version, byte* output, int header);
534 CYASSL_LOCAL int SetSerialNumber(const byte* sn, word32 snSz, byte* output);
535 CYASSL_LOCAL int GetNameHash(const byte* source, word32* idx, byte* hash,
539 /* ASN sig helpers */
540 CYASSL_LOCAL int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r,
542 CYASSL_LOCAL int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen,
543 mp_int* r, mp_int* s);
546 #ifdef CYASSL_CERT_GEN
558 #endif /* CYASSL_CERT_GEN */
562 /* for pointer use */
563 typedef struct CertStatus CertStatus;
567 enum Ocsp_Response_Status {
568 OCSP_SUCCESSFUL = 0, /* Response has valid confirmations */
569 OCSP_MALFORMED_REQUEST = 1, /* Illegal confirmation request */
570 OCSP_INTERNAL_ERROR = 2, /* Internal error in issuer */
571 OCSP_TRY_LATER = 3, /* Try again later */
572 OCSP_SIG_REQUIRED = 5, /* Must sign the request (4 is skipped) */
573 OCSP_UNAUTHROIZED = 6 /* Request unauthorized */
577 enum Ocsp_Cert_Status {
585 OCSP_BASIC_OID = 117,
590 typedef struct OcspRequest OcspRequest;
591 typedef struct OcspResponse OcspResponse;
597 byte serial[EXTERNAL_SERIAL_SIZE];
602 byte thisDate[MAX_DATE_SIZE];
603 byte nextDate[MAX_DATE_SIZE];
609 struct OcspResponse {
610 int responseStatus; /* return code from Responder */
612 byte* response; /* Pointer to beginning of OCSP Response */
613 word32 responseSz; /* length of the OCSP Response */
615 byte producedDate[MAX_DATE_SIZE];
616 /* Date at which this response was signed */
617 byte producedDateFormat; /* format of the producedDate */
624 byte* sig; /* Pointer to sig in source */
625 word32 sigSz; /* Length in octets for the sig */
626 word32 sigOID; /* OID for hash used for sig */
628 CertStatus* status; /* certificate status to fill out */
630 byte* nonce; /* pointer to nonce inside ASN.1 response */
631 int nonceSz; /* length of the nonce string */
633 byte* source; /* pointer to source buffer, not owned */
634 word32 maxIdx; /* max offset based on init size */
642 byte nonce[MAX_OCSP_NONCE_SZ];
645 byte* issuerHash; /* pointer to issuerHash in source cert */
646 byte* issuerKeyHash; /* pointer to issuerKeyHash in source cert */
647 byte* serial; /* pointer to serial number in source cert */
648 int serialSz; /* length of the serial number */
650 byte* dest; /* pointer to the destination ASN.1 buffer */
651 word32 destSz; /* length of the destination buffer */
655 CYASSL_LOCAL void InitOcspResponse(OcspResponse*, CertStatus*, byte*, word32);
656 CYASSL_LOCAL int OcspResponseDecode(OcspResponse*);
658 CYASSL_LOCAL void InitOcspRequest(OcspRequest*, DecodedCert*,
659 byte, byte*, word32);
660 CYASSL_LOCAL int EncodeOcspRequest(OcspRequest*);
662 CYASSL_LOCAL int CompareOcspReqResp(OcspRequest*, OcspResponse*);
665 #endif /* HAVE_OCSP */
668 /* for pointer use */
669 typedef struct RevokedCert RevokedCert;
674 byte serialNumber[EXTERNAL_SERIAL_SIZE];
679 typedef struct DecodedCRL DecodedCRL;
682 word32 certBegin; /* offset to start of cert */
683 word32 sigIndex; /* offset to start of signature */
684 word32 sigLength; /* length of signature */
685 word32 signatureOID; /* sum of algorithm object id */
686 byte* signature; /* pointer into raw source, not owned */
687 byte issuerHash[SHA_DIGEST_SIZE]; /* issuer hash */
688 byte crlHash[SHA_DIGEST_SIZE]; /* raw crl data hash */
689 byte lastDate[MAX_DATE_SIZE]; /* last date updated */
690 byte nextDate[MAX_DATE_SIZE]; /* next update date */
691 byte lastDateFormat; /* format of last date */
692 byte nextDateFormat; /* format of next date */
693 RevokedCert* certs; /* revoked cert list */
694 int totalCerts; /* number on list */
697 CYASSL_LOCAL void InitDecodedCRL(DecodedCRL*);
698 CYASSL_LOCAL int ParseCRL(DecodedCRL*, const byte* buff, word32 sz, void* cm);
699 CYASSL_LOCAL void FreeDecodedCRL(DecodedCRL*);
702 #endif /* HAVE_CRL */
709 #endif /* CTAO_CRYPT_ASN_H */