asn.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /* asn.h
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #ifndef NO_ASN
  22. #ifndef CTAO_CRYPT_ASN_H
  23. #define CTAO_CRYPT_ASN_H
  24. #include <cyassl/ctaocrypt/types.h>
  25. #include <cyassl/ctaocrypt/rsa.h>
  26. #include <cyassl/ctaocrypt/dh.h>
  27. #include <cyassl/ctaocrypt/dsa.h>
  28. #include <cyassl/ctaocrypt/sha.h>
  29. #include <cyassl/ctaocrypt/md5.h>
  30. #include <cyassl/ctaocrypt/asn_public.h> /* public interface */
  31. #ifdef HAVE_ECC
  32. #include <cyassl/ctaocrypt/ecc.h>
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. enum {
  38. ISSUER = 0,
  39. SUBJECT = 1,
  40. EXTERNAL_SERIAL_SIZE = 32,
  41. BEFORE = 0,
  42. AFTER = 1
  43. };
  44. /* ASN Tags */
  45. enum ASN_Tags {
  46. ASN_BOOLEAN = 0x01,
  47. ASN_INTEGER = 0x02,
  48. ASN_BIT_STRING = 0x03,
  49. ASN_OCTET_STRING = 0x04,
  50. ASN_TAG_NULL = 0x05,
  51. ASN_OBJECT_ID = 0x06,
  52. ASN_ENUMERATED = 0x0a,
  53. ASN_UTF8STRING = 0x0c,
  54. ASN_SEQUENCE = 0x10,
  55. ASN_SET = 0x11,
  56. ASN_UTC_TIME = 0x17,
  57. ASN_OTHER_TYPE = 0x00,
  58. ASN_RFC822_TYPE = 0x01,
  59. ASN_DNS_TYPE = 0x02,
  60. ASN_DIR_TYPE = 0x04,
  61. ASN_GENERALIZED_TIME = 0x18,
  62. CRL_EXTENSIONS = 0xa0,
  63. ASN_EXTENSIONS = 0xa3,
  64. ASN_LONG_LENGTH = 0x80
  65. };
  66. enum ASN_Flags{
  67. ASN_CONSTRUCTED = 0x20,
  68. ASN_CONTEXT_SPECIFIC = 0x80
  69. };
  70. enum DN_Tags {
  71. ASN_COMMON_NAME = 0x03, /* CN */
  72. ASN_SUR_NAME = 0x04, /* SN */
  73. ASN_SERIAL_NUMBER = 0x05, /* serialNumber */
  74. ASN_COUNTRY_NAME = 0x06, /* C */
  75. ASN_LOCALITY_NAME = 0x07, /* L */
  76. ASN_STATE_NAME = 0x08, /* ST */
  77. ASN_ORG_NAME = 0x0a, /* O */
  78. ASN_ORGUNIT_NAME = 0x0b /* OU */
  79. };
  80. enum PBES {
  81. PBE_MD5_DES = 0,
  82. PBE_SHA1_DES = 1,
  83. PBE_SHA1_DES3 = 2,
  84. PBE_SHA1_RC4_128 = 3,
  85. PBES2 = 13 /* algo ID */
  86. };
  87. enum ENCRYPTION_TYPES {
  88. DES_TYPE = 0,
  89. DES3_TYPE = 1,
  90. RC4_TYPE = 2
  91. };
  92. enum ECC_TYPES {
  93. ECC_PREFIX_0 = 160,
  94. ECC_PREFIX_1 = 161
  95. };
  96. enum Misc_ASN {
  97. ASN_NAME_MAX = 256,
  98. MAX_SALT_SIZE = 64, /* MAX PKCS Salt length */
  99. MAX_IV_SIZE = 64, /* MAX PKCS Iv length */
  100. MAX_KEY_SIZE = 64, /* MAX PKCS Key length */
  101. PKCS5 = 5, /* PKCS oid tag */
  102. PKCS5v2 = 6, /* PKCS #5 v2.0 */
  103. PKCS12 = 12, /* PKCS #12 */
  104. MAX_UNICODE_SZ = 256,
  105. ASN_BOOL_SIZE = 2, /* including type */
  106. ASN_ECC_HEADER_SZ = 2, /* String type + 1 byte len */
  107. ASN_ECC_CONTEXT_SZ = 2, /* Content specific type + 1 byte len */
  108. SHA_SIZE = 20,
  109. RSA_INTS = 8, /* RSA ints in private key */
  110. MIN_DATE_SIZE = 13,
  111. MAX_DATE_SIZE = 32,
  112. ASN_GEN_TIME_SZ = 15, /* 7 numbers * 2 + Zulu tag */
  113. MAX_ENCODED_SIG_SZ = 512,
  114. MAX_SIG_SZ = 256,
  115. MAX_ALGO_SZ = 20,
  116. MAX_SEQ_SZ = 5, /* enum(seq | con) + length(4) */
  117. MAX_SET_SZ = 5, /* enum(set | con) + length(4) */
  118. MAX_OCTET_STR_SZ = 5, /* enum(set | con) + length(4) */
  119. MAX_EXP_SZ = 5, /* enum(contextspec|con|exp) + length(4) */
  120. MAX_PRSTR_SZ = 5, /* enum(prstr) + length(4) */
  121. MAX_VERSION_SZ = 5, /* enum + id + version(byte) + (header(2))*/
  122. MAX_ENCODED_DIG_SZ = 73, /* sha512 + enum(bit or octet) + legnth(4) */
  123. MAX_RSA_INT_SZ = 517, /* RSA raw sz 4096 for bits + tag + len(4) */
  124. MAX_NTRU_KEY_SZ = 610, /* NTRU 112 bit public key */
  125. MAX_NTRU_ENC_SZ = 628, /* NTRU 112 bit DER public encoding */
  126. MAX_LENGTH_SZ = 4, /* Max length size for DER encoding */
  127. MAX_RSA_E_SZ = 16, /* Max RSA public e size */
  128. MAX_CA_SZ = 32, /* Max encoded CA basic constraint length */
  129. MAX_SN_SZ = 35, /* Max encoded serial number (INT) length */
  130. #ifdef CYASSL_CERT_GEN
  131. #ifdef CYASSL_CERT_REQ
  132. /* Max encoded cert req attributes length */
  133. MAX_ATTRIB_SZ = MAX_SEQ_SZ * 3 + (11 + MAX_SEQ_SZ) * 2 +
  134. MAX_PRSTR_SZ + CTC_NAME_SIZE, /* 11 is the OID size */
  135. #endif
  136. #ifdef CYASSL_ALT_NAMES
  137. MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + CTC_MAX_ALT_SIZE,
  138. #else
  139. MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + MAX_CA_SZ,
  140. #endif
  141. /* Max total extensions, id + len + others */
  142. #endif
  143. MAX_OCSP_EXT_SZ = 58, /* Max OCSP Extension length */
  144. MAX_OCSP_NONCE_SZ = 18, /* OCSP Nonce size */
  145. EIGHTK_BUF = 8192, /* Tmp buffer size */
  146. MAX_PUBLIC_KEY_SZ = MAX_NTRU_ENC_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ * 2
  147. /* use bigger NTRU size */
  148. };
  149. enum Oid_Types {
  150. hashType = 0,
  151. sigType = 1,
  152. keyType = 2,
  153. curveType = 3,
  154. blkType = 4
  155. };
  156. enum Hash_Sum {
  157. MD2h = 646,
  158. MD5h = 649,
  159. SHAh = 88,
  160. SHA256h = 414,
  161. SHA384h = 415,
  162. SHA512h = 416
  163. };
  164. enum Block_Sum {
  165. DESb = 69,
  166. DES3b = 652
  167. };
  168. enum Key_Sum {
  169. DSAk = 515,
  170. RSAk = 645,
  171. NTRUk = 274,
  172. ECDSAk = 518
  173. };
  174. enum Ecc_Sum {
  175. ECC_256R1 = 526,
  176. ECC_384R1 = 210,
  177. ECC_521R1 = 211,
  178. ECC_160R1 = 184,
  179. ECC_192R1 = 520,
  180. ECC_224R1 = 209
  181. };
  182. enum KDF_Sum {
  183. PBKDF2_OID = 660
  184. };
  185. enum Extensions_Sum {
  186. BASIC_CA_OID = 133,
  187. ALT_NAMES_OID = 131,
  188. CRL_DIST_OID = 145,
  189. AUTH_INFO_OID = 69,
  190. CA_ISSUER_OID = 117,
  191. AUTH_KEY_OID = 149,
  192. SUBJ_KEY_OID = 128,
  193. CERT_POLICY_OID = 146,
  194. KEY_USAGE_OID = 129, /* 2.5.29.15 */
  195. INHIBIT_ANY_OID = 168, /* 2.5.29.54 */
  196. EXT_KEY_USAGE_OID = 151, /* 2.5.29.37 */
  197. NAME_CONS_OID = 144 /* 2.5.29.30 */
  198. };
  199. enum CertificatePolicy_Sum {
  200. CP_ANY_OID = 146 /* id-ce 32 0 */
  201. };
  202. enum SepHardwareName_Sum {
  203. HW_NAME_OID = 79 /* 1.3.6.1.5.5.7.8.4 from RFC 4108*/
  204. };
  205. enum AuthInfo_Sum {
  206. AIA_OCSP_OID = 116, /* 1.3.6.1.5.5.7.48.1 */
  207. AIA_CA_ISSUER_OID = 117 /* 1.3.6.1.5.5.7.48.2 */
  208. };
  209. enum ExtKeyUsage_Sum { /* From RFC 5280 */
  210. EKU_ANY_OID = 151, /* 2.5.29.37.0, anyExtendedKeyUsage */
  211. EKU_SERVER_AUTH_OID = 71, /* 1.3.6.1.5.5.7.3.1, id-kp-serverAuth */
  212. EKU_CLIENT_AUTH_OID = 72, /* 1.3.6.1.5.5.7.3.2, id-kp-clientAuth */
  213. EKU_OCSP_SIGN_OID = 79 /* 1.3.6.1.5.5.7.3.9, OCSPSigning */
  214. };
  215. enum VerifyType {
  216. NO_VERIFY = 0,
  217. VERIFY = 1
  218. };
  219. /* Key usage extension bits */
  220. #define KEYUSE_DIGITAL_SIG 0x0100
  221. #define KEYUSE_CONTENT_COMMIT 0x0080
  222. #define KEYUSE_KEY_ENCIPHER 0x0040
  223. #define KEYUSE_DATA_ENCIPHER 0x0020
  224. #define KEYUSE_KEY_AGREE 0x0010
  225. #define KEYUSE_KEY_CERT_SIGN 0x0008
  226. #define KEYUSE_CRL_SIGN 0x0004
  227. #define KEYUSE_ENCIPHER_ONLY 0x0002
  228. #define KEYUSE_DECIPHER_ONLY 0x0001
  229. #define EXTKEYUSE_ANY 0x08
  230. #define EXTKEYUSE_OCSP_SIGN 0x04
  231. #define EXTKEYUSE_CLIENT_AUTH 0x02
  232. #define EXTKEYUSE_SERVER_AUTH 0x01
  233. typedef struct DNS_entry DNS_entry;
  234. struct DNS_entry {
  235. DNS_entry* next; /* next on DNS list */
  236. char* name; /* actual DNS name */
  237. };
  238. typedef struct Base_entry Base_entry;
  239. struct Base_entry {
  240. Base_entry* next; /* next on name base list */
  241. char* name; /* actual name base */
  242. int nameSz; /* name length */
  243. byte type; /* Name base type (DNS or RFC822) */
  244. };
  245. struct DecodedName {
  246. char* fullName;
  247. int fullNameLen;
  248. int entryCount;
  249. int cnIdx;
  250. int cnLen;
  251. int snIdx;
  252. int snLen;
  253. int cIdx;
  254. int cLen;
  255. int lIdx;
  256. int lLen;
  257. int stIdx;
  258. int stLen;
  259. int oIdx;
  260. int oLen;
  261. int ouIdx;
  262. int ouLen;
  263. int emailIdx;
  264. int emailLen;
  265. int uidIdx;
  266. int uidLen;
  267. int serialIdx;
  268. int serialLen;
  269. };
  270. typedef struct DecodedCert DecodedCert;
  271. typedef struct DecodedName DecodedName;
  272. typedef struct Signer Signer;
  273. struct DecodedCert {
  274. byte* publicKey;
  275. word32 pubKeySize;
  276. int pubKeyStored;
  277. word32 certBegin; /* offset to start of cert */
  278. word32 sigIndex; /* offset to start of signature */
  279. word32 sigLength; /* length of signature */
  280. word32 signatureOID; /* sum of algorithm object id */
  281. word32 keyOID; /* sum of key algo object id */
  282. int version; /* cert version, 1 or 3 */
  283. DNS_entry* altNames; /* alt names list of dns entries */
  284. #ifndef IGNORE_NAME_CONSTRAINTS
  285. DNS_entry* altEmailNames; /* alt names list of RFC822 entries */
  286. Base_entry* permittedNames; /* Permitted name bases */
  287. Base_entry* excludedNames; /* Excluded name bases */
  288. #endif /* IGNORE_NAME_CONSTRAINTS */
  289. byte subjectHash[SHA_SIZE]; /* hash of all Names */
  290. byte issuerHash[SHA_SIZE]; /* hash of all Names */
  291. #ifdef HAVE_OCSP
  292. byte issuerKeyHash[SHA_SIZE]; /* hash of the public Key */
  293. #endif /* HAVE_OCSP */
  294. byte* signature; /* not owned, points into raw cert */
  295. char* subjectCN; /* CommonName */
  296. int subjectCNLen; /* CommonName Length */
  297. char subjectCNEnc; /* CommonName Encoding */
  298. int subjectCNStored; /* have we saved a copy we own */
  299. char issuer[ASN_NAME_MAX]; /* full name including common name */
  300. char subject[ASN_NAME_MAX]; /* full name including common name */
  301. int verify; /* Default to yes, but could be off */
  302. byte* source; /* byte buffer holder cert, NOT owner */
  303. word32 srcIdx; /* current offset into buffer */
  304. word32 maxIdx; /* max offset based on init size */
  305. void* heap; /* for user memory overrides */
  306. byte serial[EXTERNAL_SERIAL_SIZE]; /* raw serial number */
  307. int serialSz; /* raw serial bytes stored */
  308. byte* extensions; /* not owned, points into raw cert */
  309. int extensionsSz; /* length of cert extensions */
  310. word32 extensionsIdx; /* if want to go back and parse later */
  311. byte* extAuthInfo; /* Authority Information Access URI */
  312. int extAuthInfoSz; /* length of the URI */
  313. byte* extCrlInfo; /* CRL Distribution Points */
  314. int extCrlInfoSz; /* length of the URI */
  315. byte extSubjKeyId[SHA_SIZE]; /* Subject Key ID */
  316. byte extSubjKeyIdSet; /* Set when the SKID was read from cert */
  317. byte extAuthKeyId[SHA_SIZE]; /* Authority Key ID */
  318. byte extAuthKeyIdSet; /* Set when the AKID was read from cert */
  319. #ifndef IGNORE_NAME_CONSTRAINTS
  320. byte extNameConstraintSet;
  321. #endif /* IGNORE_NAME_CONSTRAINTS */
  322. byte isCA; /* CA basic constraint true */
  323. byte extKeyUsageSet;
  324. word16 extKeyUsage; /* Key usage bitfield */
  325. byte extExtKeyUsageSet; /* Extended Key Usage */
  326. byte extExtKeyUsage; /* Extended Key usage bitfield */
  327. #ifdef OPENSSL_EXTRA
  328. byte extBasicConstSet;
  329. byte extBasicConstCrit;
  330. byte extBasicConstPlSet;
  331. word32 pathLength; /* CA basic constraint path length, opt */
  332. byte extSubjAltNameSet;
  333. byte extSubjAltNameCrit;
  334. byte extAuthKeyIdCrit;
  335. #ifndef IGNORE_NAME_CONSTRAINTS
  336. byte extNameConstraintCrit;
  337. #endif /* IGNORE_NAME_CONSTRAINTS */
  338. byte extSubjKeyIdCrit;
  339. byte extKeyUsageCrit;
  340. byte extExtKeyUsageCrit;
  341. byte* extExtKeyUsageSrc;
  342. word32 extExtKeyUsageSz;
  343. word32 extExtKeyUsageCount;
  344. byte* extAuthKeyIdSrc;
  345. word32 extAuthKeyIdSz;
  346. byte* extSubjKeyIdSrc;
  347. word32 extSubjKeyIdSz;
  348. #endif
  349. #ifdef HAVE_ECC
  350. word32 pkCurveOID; /* Public Key's curve OID */
  351. #endif /* HAVE_ECC */
  352. byte* beforeDate;
  353. int beforeDateLen;
  354. byte* afterDate;
  355. int afterDateLen;
  356. #ifdef HAVE_PKCS7
  357. byte* issuerRaw; /* pointer to issuer inside source */
  358. int issuerRawLen;
  359. #endif
  360. #ifndef IGNORE_NAME_CONSTRAINT
  361. byte* subjectRaw; /* pointer to subject inside source */
  362. int subjectRawLen;
  363. #endif
  364. #if defined(CYASSL_CERT_GEN)
  365. /* easy access to subject info for other sign */
  366. char* subjectSN;
  367. int subjectSNLen;
  368. char subjectSNEnc;
  369. char* subjectC;
  370. int subjectCLen;
  371. char subjectCEnc;
  372. char* subjectL;
  373. int subjectLLen;
  374. char subjectLEnc;
  375. char* subjectST;
  376. int subjectSTLen;
  377. char subjectSTEnc;
  378. char* subjectO;
  379. int subjectOLen;
  380. char subjectOEnc;
  381. char* subjectOU;
  382. int subjectOULen;
  383. char subjectOUEnc;
  384. char* subjectEmail;
  385. int subjectEmailLen;
  386. #endif /* CYASSL_CERT_GEN */
  387. #ifdef OPENSSL_EXTRA
  388. DecodedName issuerName;
  389. DecodedName subjectName;
  390. #endif /* OPENSSL_EXTRA */
  391. #ifdef CYASSL_SEP
  392. int deviceTypeSz;
  393. byte* deviceType;
  394. int hwTypeSz;
  395. byte* hwType;
  396. int hwSerialNumSz;
  397. byte* hwSerialNum;
  398. #ifdef OPENSSL_EXTRA
  399. byte extCertPolicySet;
  400. byte extCertPolicyCrit;
  401. #endif /* OPENSSL_EXTRA */
  402. #endif /* CYASSL_SEP */
  403. };
  404. #ifdef SHA_DIGEST_SIZE
  405. #define SIGNER_DIGEST_SIZE SHA_DIGEST_SIZE
  406. #else
  407. #define SIGNER_DIGEST_SIZE 20
  408. #endif
  409. /* CA Signers */
  410. /* if change layout change PERSIST_CERT_CACHE functions too */
  411. struct Signer {
  412. word32 pubKeySize;
  413. word32 keyOID; /* key type */
  414. word16 keyUsage;
  415. byte* publicKey;
  416. int nameLen;
  417. char* name; /* common name */
  418. #ifndef IGNORE_NAME_CONSTRAINTS
  419. Base_entry* permittedNames;
  420. Base_entry* excludedNames;
  421. #endif /* IGNORE_NAME_CONSTRAINTS */
  422. byte subjectNameHash[SIGNER_DIGEST_SIZE];
  423. /* sha hash of names in certificate */
  424. #ifndef NO_SKID
  425. byte subjectKeyIdHash[SIGNER_DIGEST_SIZE];
  426. /* sha hash of names in certificate */
  427. #endif
  428. Signer* next;
  429. };
  430. /* not for public consumption but may use for testing sometimes */
  431. #ifdef CYASSL_TEST_CERT
  432. #define CYASSL_TEST_API CYASSL_API
  433. #else
  434. #define CYASSL_TEST_API CYASSL_LOCAL
  435. #endif
  436. CYASSL_TEST_API void FreeAltNames(DNS_entry*, void*);
  437. #ifndef IGNORE_NAME_CONSTRAINTS
  438. CYASSL_TEST_API void FreeNameSubtrees(Base_entry*, void*);
  439. #endif /* IGNORE_NAME_CONSTRAINTS */
  440. CYASSL_TEST_API void InitDecodedCert(DecodedCert*, byte*, word32, void*);
  441. CYASSL_TEST_API void FreeDecodedCert(DecodedCert*);
  442. CYASSL_TEST_API int ParseCert(DecodedCert*, int type, int verify, void* cm);
  443. CYASSL_LOCAL int ParseCertRelative(DecodedCert*, int type, int verify,void* cm);
  444. CYASSL_LOCAL int DecodeToKey(DecodedCert*, int verify);
  445. CYASSL_LOCAL Signer* MakeSigner(void*);
  446. CYASSL_LOCAL void FreeSigner(Signer*, void*);
  447. CYASSL_LOCAL void FreeSignerTable(Signer**, int, void*);
  448. CYASSL_LOCAL int ToTraditional(byte* buffer, word32 length);
  449. CYASSL_LOCAL int ToTraditionalEnc(byte* buffer, word32 length,const char*, int);
  450. CYASSL_LOCAL int ValidateDate(const byte* date, byte format, int dateType);
  451. /* ASN.1 helper functions */
  452. CYASSL_LOCAL int GetLength(const byte* input, word32* inOutIdx, int* len,
  453. word32 maxIdx);
  454. CYASSL_LOCAL int GetSequence(const byte* input, word32* inOutIdx, int* len,
  455. word32 maxIdx);
  456. CYASSL_LOCAL int GetSet(const byte* input, word32* inOutIdx, int* len,
  457. word32 maxIdx);
  458. CYASSL_LOCAL int GetMyVersion(const byte* input, word32* inOutIdx,
  459. int* version);
  460. CYASSL_LOCAL int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx,
  461. word32 maxIdx);
  462. CYASSL_LOCAL int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
  463. word32 maxIdx);
  464. CYASSL_LOCAL word32 SetLength(word32 length, byte* output);
  465. CYASSL_LOCAL word32 SetSequence(word32 len, byte* output);
  466. CYASSL_LOCAL word32 SetOctetString(word32 len, byte* output);
  467. CYASSL_LOCAL word32 SetImplicit(byte tag, byte number, word32 len,byte* output);
  468. CYASSL_LOCAL word32 SetExplicit(byte number, word32 len, byte* output);
  469. CYASSL_LOCAL word32 SetSet(word32 len, byte* output);
  470. CYASSL_LOCAL word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz);
  471. CYASSL_LOCAL int SetMyVersion(word32 version, byte* output, int header);
  472. CYASSL_LOCAL int SetSerialNumber(const byte* sn, word32 snSz, byte* output);
  473. CYASSL_LOCAL int GetNameHash(const byte* source, word32* idx, byte* hash,
  474. int maxIdx);
  475. #ifdef HAVE_ECC
  476. /* ASN sig helpers */
  477. CYASSL_LOCAL int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r,
  478. mp_int* s);
  479. CYASSL_LOCAL int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen,
  480. mp_int* r, mp_int* s);
  481. #endif
  482. #ifdef CYASSL_CERT_GEN
  483. enum cert_enums {
  484. NAME_ENTRIES = 8,
  485. JOINT_LEN = 2,
  486. EMAIL_JOINT_LEN = 9,
  487. RSA_KEY = 10,
  488. NTRU_KEY = 11,
  489. ECC_KEY = 12
  490. };
  491. #ifndef CYASSL_PEMCERT_TODER_DEFINED
  492. #ifndef NO_FILESYSTEM
  493. /* forward from CyaSSL */
  494. CYASSL_API
  495. int CyaSSL_PemCertToDer(const char* fileName, unsigned char* derBuf, int derSz);
  496. #define CYASSL_PEMCERT_TODER_DEFINED
  497. #endif
  498. #endif
  499. #endif /* CYASSL_CERT_GEN */
  500. /* for pointer use */
  501. typedef struct CertStatus CertStatus;
  502. #ifdef HAVE_OCSP
  503. enum Ocsp_Response_Status {
  504. OCSP_SUCCESSFUL = 0, /* Response has valid confirmations */
  505. OCSP_MALFORMED_REQUEST = 1, /* Illegal confirmation request */
  506. OCSP_INTERNAL_ERROR = 2, /* Internal error in issuer */
  507. OCSP_TRY_LATER = 3, /* Try again later */
  508. OCSP_SIG_REQUIRED = 5, /* Must sign the request (4 is skipped) */
  509. OCSP_UNAUTHROIZED = 6 /* Request unauthorized */
  510. };
  511. enum Ocsp_Cert_Status {
  512. CERT_GOOD = 0,
  513. CERT_REVOKED = 1,
  514. CERT_UNKNOWN = 2
  515. };
  516. enum Ocsp_Sums {
  517. OCSP_BASIC_OID = 117,
  518. OCSP_NONCE_OID = 118
  519. };
  520. typedef struct OcspRequest OcspRequest;
  521. typedef struct OcspResponse OcspResponse;
  522. struct CertStatus {
  523. CertStatus* next;
  524. byte serial[EXTERNAL_SERIAL_SIZE];
  525. int serialSz;
  526. int status;
  527. byte thisDate[MAX_DATE_SIZE];
  528. byte nextDate[MAX_DATE_SIZE];
  529. byte thisDateFormat;
  530. byte nextDateFormat;
  531. };
  532. struct OcspResponse {
  533. int responseStatus; /* return code from Responder */
  534. byte* response; /* Pointer to beginning of OCSP Response */
  535. word32 responseSz; /* length of the OCSP Response */
  536. byte producedDate[MAX_DATE_SIZE];
  537. /* Date at which this response was signed */
  538. byte producedDateFormat; /* format of the producedDate */
  539. byte* issuerHash;
  540. byte* issuerKeyHash;
  541. byte* cert;
  542. word32 certSz;
  543. byte* sig; /* Pointer to sig in source */
  544. word32 sigSz; /* Length in octets for the sig */
  545. word32 sigOID; /* OID for hash used for sig */
  546. CertStatus* status; /* certificate status to fill out */
  547. byte* nonce; /* pointer to nonce inside ASN.1 response */
  548. int nonceSz; /* length of the nonce string */
  549. byte* source; /* pointer to source buffer, not owned */
  550. word32 maxIdx; /* max offset based on init size */
  551. };
  552. struct OcspRequest {
  553. DecodedCert* cert;
  554. byte useNonce;
  555. byte nonce[MAX_OCSP_NONCE_SZ];
  556. int nonceSz;
  557. byte* issuerHash; /* pointer to issuerHash in source cert */
  558. byte* issuerKeyHash; /* pointer to issuerKeyHash in source cert */
  559. byte* serial; /* pointer to serial number in source cert */
  560. int serialSz; /* length of the serial number */
  561. byte* dest; /* pointer to the destination ASN.1 buffer */
  562. word32 destSz; /* length of the destination buffer */
  563. };
  564. CYASSL_LOCAL void InitOcspResponse(OcspResponse*, CertStatus*, byte*, word32);
  565. CYASSL_LOCAL int OcspResponseDecode(OcspResponse*);
  566. CYASSL_LOCAL void InitOcspRequest(OcspRequest*, DecodedCert*,
  567. byte, byte*, word32);
  568. CYASSL_LOCAL int EncodeOcspRequest(OcspRequest*);
  569. CYASSL_LOCAL int CompareOcspReqResp(OcspRequest*, OcspResponse*);
  570. #endif /* HAVE_OCSP */
  571. /* for pointer use */
  572. typedef struct RevokedCert RevokedCert;
  573. #ifdef HAVE_CRL
  574. struct RevokedCert {
  575. byte serialNumber[EXTERNAL_SERIAL_SIZE];
  576. int serialSz;
  577. RevokedCert* next;
  578. };
  579. typedef struct DecodedCRL DecodedCRL;
  580. struct DecodedCRL {
  581. word32 certBegin; /* offset to start of cert */
  582. word32 sigIndex; /* offset to start of signature */
  583. word32 sigLength; /* length of signature */
  584. word32 signatureOID; /* sum of algorithm object id */
  585. byte* signature; /* pointer into raw source, not owned */
  586. byte issuerHash[SHA_DIGEST_SIZE]; /* issuer hash */
  587. byte crlHash[SHA_DIGEST_SIZE]; /* raw crl data hash */
  588. byte lastDate[MAX_DATE_SIZE]; /* last date updated */
  589. byte nextDate[MAX_DATE_SIZE]; /* next update date */
  590. byte lastDateFormat; /* format of last date */
  591. byte nextDateFormat; /* format of next date */
  592. RevokedCert* certs; /* revoked cert list */
  593. int totalCerts; /* number on list */
  594. };
  595. CYASSL_LOCAL void InitDecodedCRL(DecodedCRL*);
  596. CYASSL_LOCAL int ParseCRL(DecodedCRL*, const byte* buff, word32 sz, void* cm);
  597. CYASSL_LOCAL void FreeDecodedCRL(DecodedCRL*);
  598. #endif /* HAVE_CRL */
  599. #ifdef __cplusplus
  600. } /* extern "C" */
  601. #endif
  602. #endif /* CTAO_CRYPT_ASN_H */
  603. #endif /* !NO_ASN */