asn_public.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /* asn_public.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL 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. * wolfSSL 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-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfcrypt/asn_public.h
  23. */
  24. /*
  25. DESCRIPTION
  26. This library defines the interface APIs for X509 certificates.
  27. */
  28. #ifndef WOLF_CRYPT_ASN_PUBLIC_H
  29. #define WOLF_CRYPT_ASN_PUBLIC_H
  30. #include <wolfssl/wolfcrypt/types.h>
  31. #include <wolfssl/wolfcrypt/dsa.h>
  32. #include <wolfssl/wolfcrypt/random.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* guard on redeclaration */
  37. #ifndef WC_ECCKEY_TYPE_DEFINED
  38. typedef struct ecc_key ecc_key;
  39. #define WC_ECCKEY_TYPE_DEFINED
  40. #endif
  41. #ifndef WC_ED25519KEY_TYPE_DEFINED
  42. typedef struct ed25519_key ed25519_key;
  43. #define WC_ED25519KEY_TYPE_DEFINED
  44. #endif
  45. #ifndef WC_CURVE25519KEY_TYPE_DEFINED
  46. typedef struct curve25519_key curve25519_key;
  47. #define WC_CURVE25519KEY_TYPE_DEFINED
  48. #endif
  49. #ifndef WC_ED448KEY_TYPE_DEFINED
  50. typedef struct ed448_key ed448_key;
  51. #define WC_ED448KEY_TYPE_DEFINED
  52. #endif
  53. #ifndef WC_CURVE448KEY_TYPE_DEFINED
  54. typedef struct curve448_key curve448_key;
  55. #define WC_CURVE448KEY_TYPE_DEFINED
  56. #endif
  57. #ifndef WC_RSAKEY_TYPE_DEFINED
  58. typedef struct RsaKey RsaKey;
  59. #define WC_RSAKEY_TYPE_DEFINED
  60. #endif
  61. #ifndef WC_DH_TYPE_DEFINED
  62. typedef struct DhKey DhKey;
  63. #define WC_DH_TYPE_DEFINED
  64. #endif
  65. #ifndef WC_FALCONKEY_TYPE_DEFINED
  66. typedef struct falcon_key falcon_key;
  67. #define WC_FALCONKEY_TYPE_DEFINED
  68. #endif
  69. #ifndef WC_DILITHIUMKEY_TYPE_DEFINED
  70. typedef struct dilithium_key dilithium_key;
  71. #define WC_DILITHIUMKEY_TYPE_DEFINED
  72. #endif
  73. #ifndef WC_SPHINCSKEY_TYPE_DEFINED
  74. typedef struct sphincs_key sphincs_key;
  75. #define WC_SPHINCSKEY_TYPE_DEFINED
  76. #endif
  77. enum Ecc_Sum {
  78. ECC_SECP112R1_OID = 182,
  79. ECC_SECP112R2_OID = 183,
  80. ECC_SECP128R1_OID = 204,
  81. ECC_SECP128R2_OID = 205,
  82. ECC_SECP160R1_OID = 184,
  83. ECC_SECP160R2_OID = 206,
  84. ECC_SECP160K1_OID = 185,
  85. ECC_BRAINPOOLP160R1_OID = 98,
  86. ECC_SECP192R1_OID = 520,
  87. ECC_PRIME192V2_OID = 521,
  88. ECC_PRIME192V3_OID = 522,
  89. ECC_SECP192K1_OID = 207,
  90. ECC_BRAINPOOLP192R1_OID = 100,
  91. ECC_SECP224R1_OID = 209,
  92. ECC_SECP224K1_OID = 208,
  93. ECC_BRAINPOOLP224R1_OID = 102,
  94. ECC_PRIME239V1_OID = 523,
  95. ECC_PRIME239V2_OID = 524,
  96. ECC_PRIME239V3_OID = 525,
  97. ECC_SECP256R1_OID = 526,
  98. ECC_SECP256K1_OID = 186,
  99. ECC_BRAINPOOLP256R1_OID = 104,
  100. ECC_SM2P256V1_OID = 667,
  101. ECC_X25519_OID = 365,
  102. ECC_ED25519_OID = 256,
  103. ECC_BRAINPOOLP320R1_OID = 106,
  104. ECC_X448_OID = 362,
  105. ECC_ED448_OID = 257,
  106. ECC_SECP384R1_OID = 210,
  107. ECC_BRAINPOOLP384R1_OID = 108,
  108. ECC_BRAINPOOLP512R1_OID = 110,
  109. ECC_SECP521R1_OID = 211
  110. };
  111. enum EncPkcs8Types {
  112. ENC_PKCS8_VER_PKCS12 = 1,
  113. ENC_PKCS8_VER_PKCS5 = 5,
  114. ENC_PKCS8_PBES2 = 13,
  115. ENC_PKCS8_PBE_SHA1_RC4_128 = 1,
  116. ENC_PKCS8_PBE_SHA1_DES = 2,
  117. ENC_PKCS8_PBE_SHA1_DES3 = 3,
  118. ENC_PKCS8_PBE_SHA1_40RC2_CBC = 6,
  119. ENC_PKCS8_PBES1_MD5_DES = 3,
  120. ENC_PKCS8_PBES1_SHA1_DES = 10,
  121. ENC_PKCS8_ALG_AES128CBC = 414,
  122. ENC_PKCS8_ALG_AES256CBC = 454,
  123. ENC_PKCS8_ALG_DES = 69,
  124. ENC_PKCS8_ALG_DES3 = 652
  125. };
  126. /* Certificate file Type */
  127. enum CertType {
  128. CERT_TYPE = 0,
  129. PRIVATEKEY_TYPE,
  130. DH_PARAM_TYPE,
  131. DSA_PARAM_TYPE,
  132. CRL_TYPE,
  133. CA_TYPE,
  134. ECC_PRIVATEKEY_TYPE,
  135. DSA_PRIVATEKEY_TYPE,
  136. CERTREQ_TYPE,
  137. DSA_TYPE,
  138. ECC_TYPE,
  139. RSA_TYPE,
  140. PUBLICKEY_TYPE,
  141. RSA_PUBLICKEY_TYPE,
  142. ECC_PUBLICKEY_TYPE,
  143. TRUSTED_PEER_TYPE,
  144. EDDSA_PRIVATEKEY_TYPE,
  145. ED25519_TYPE,
  146. ED448_TYPE,
  147. PKCS12_TYPE,
  148. PKCS8_PRIVATEKEY_TYPE,
  149. PKCS8_ENC_PRIVATEKEY_TYPE,
  150. DETECT_CERT_TYPE,
  151. DH_PRIVATEKEY_TYPE,
  152. X942_PARAM_TYPE,
  153. FALCON_LEVEL1_TYPE,
  154. FALCON_LEVEL5_TYPE,
  155. DILITHIUM_LEVEL2_TYPE,
  156. DILITHIUM_LEVEL3_TYPE,
  157. DILITHIUM_LEVEL5_TYPE,
  158. SPHINCS_FAST_LEVEL1_TYPE,
  159. SPHINCS_FAST_LEVEL3_TYPE,
  160. SPHINCS_FAST_LEVEL5_TYPE,
  161. SPHINCS_SMALL_LEVEL1_TYPE,
  162. SPHINCS_SMALL_LEVEL3_TYPE,
  163. SPHINCS_SMALL_LEVEL5_TYPE,
  164. ECC_PARAM_TYPE,
  165. CHAIN_CERT_TYPE
  166. };
  167. /* Signature type, by OID sum */
  168. enum Ctc_SigType {
  169. CTC_SHAwDSA = 517,
  170. CTC_SHA256wDSA = 416,
  171. CTC_MD2wRSA = 646,
  172. CTC_MD5wRSA = 648,
  173. CTC_SHAwRSA = 649,
  174. CTC_SHAwECDSA = 520,
  175. CTC_SHA224wRSA = 658,
  176. CTC_SHA224wECDSA = 523,
  177. CTC_SHA256wRSA = 655,
  178. CTC_SHA256wECDSA = 524,
  179. CTC_SHA384wRSA = 656,
  180. CTC_SHA384wECDSA = 525,
  181. CTC_SHA512wRSA = 657,
  182. CTC_SHA512wECDSA = 526,
  183. /* https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration */
  184. CTC_SHA3_224wECDSA = 423,
  185. CTC_SHA3_256wECDSA = 424,
  186. CTC_SHA3_384wECDSA = 425,
  187. CTC_SHA3_512wECDSA = 426,
  188. CTC_SHA3_224wRSA = 427,
  189. CTC_SHA3_256wRSA = 428,
  190. CTC_SHA3_384wRSA = 429,
  191. CTC_SHA3_512wRSA = 430,
  192. CTC_RSASSAPSS = 654,
  193. CTC_SM3wSM2 = 740, /* 1.2.156.10197.1.501 */
  194. CTC_ED25519 = 256,
  195. CTC_ED448 = 257,
  196. CTC_FALCON_LEVEL1 = 268,
  197. CTC_FALCON_LEVEL5 = 271,
  198. CTC_DILITHIUM_LEVEL2 = 213,
  199. CTC_DILITHIUM_LEVEL3 = 216,
  200. CTC_DILITHIUM_LEVEL5 = 220,
  201. CTC_SPHINCS_FAST_LEVEL1 = 281,
  202. CTC_SPHINCS_FAST_LEVEL3 = 283,
  203. CTC_SPHINCS_FAST_LEVEL5 = 282,
  204. CTC_SPHINCS_SMALL_LEVEL1 = 287,
  205. CTC_SPHINCS_SMALL_LEVEL3 = 285,
  206. CTC_SPHINCS_SMALL_LEVEL5 = 286
  207. };
  208. enum Ctc_Encoding {
  209. CTC_UTF8 = 0x0c, /* utf8 */
  210. CTC_PRINTABLE = 0x13 /* printable */
  211. };
  212. #ifndef WC_CTC_NAME_SIZE
  213. #define WC_CTC_NAME_SIZE 64
  214. #endif
  215. #ifndef WC_CTC_MAX_ALT_SIZE
  216. #define WC_CTC_MAX_ALT_SIZE 16384
  217. #endif
  218. #ifdef WOLFSSL_CERT_EXT
  219. #ifndef WC_CTC_MAX_CRLINFO_SZ
  220. #define WC_CTC_MAX_CRLINFO_SZ 200
  221. #endif
  222. #endif
  223. enum Ctc_Misc {
  224. CTC_COUNTRY_SIZE = 2,
  225. CTC_NAME_SIZE = WC_CTC_NAME_SIZE,
  226. CTC_DATE_SIZE = 32,
  227. CTC_MAX_ALT_SIZE = WC_CTC_MAX_ALT_SIZE, /* may be huge, default: 16384 */
  228. CTC_SERIAL_SIZE = 20,
  229. CTC_GEN_SERIAL_SZ = 16,
  230. CTC_FILETYPE_ASN1 = 2,
  231. CTC_FILETYPE_PEM = 1,
  232. CTC_FILETYPE_DEFAULT = 2,
  233. #ifdef WOLFSSL_CERT_EXT
  234. /* AKID could contains: hash + (Option) AuthCertIssuer,AuthCertSerialNum
  235. * We support only hash */
  236. CTC_MAX_SKID_SIZE = 32, /* SHA256_DIGEST_SIZE */
  237. CTC_MAX_AKID_SIZE = 32, /* SHA256_DIGEST_SIZE */
  238. CTC_MAX_CERTPOL_SZ = 200, /* RFC 5280 Section 4.2.1.4 */
  239. CTC_MAX_CERTPOL_NB = 2, /* Max number of Certificate Policy */
  240. CTC_MAX_CRLINFO_SZ = WC_CTC_MAX_CRLINFO_SZ, /* Arbitrary size that should be
  241. * enough for at least two
  242. * distribution points. */
  243. #endif /* WOLFSSL_CERT_EXT */
  244. WOLF_ENUM_DUMMY_LAST_ELEMENT(Ctc_Misc)
  245. };
  246. /* DER buffer */
  247. typedef struct DerBuffer {
  248. byte* buffer;
  249. void* heap;
  250. word32 length;
  251. int type; /* enum CertType */
  252. int dynType; /* DYNAMIC_TYPE_* */
  253. } DerBuffer;
  254. typedef struct WOLFSSL_ASN1_TIME {
  255. unsigned char data[CTC_DATE_SIZE]; /* date bytes */
  256. int length;
  257. int type;
  258. } WOLFSSL_ASN1_TIME;
  259. enum {
  260. IV_SZ = 32, /* max iv sz */
  261. #ifdef OPENSSL_ALL
  262. NAME_SZ = 160, /* larger max one line, allows for longer
  263. encryption password support */
  264. #else
  265. NAME_SZ = 80, /* max one line */
  266. #endif
  267. PEM_PASS_READ = 0,
  268. PEM_PASS_WRITE = 1
  269. };
  270. typedef int (wc_pem_password_cb)(char* passwd, int sz, int rw, void* userdata);
  271. #ifndef OPENSSL_COEXIST
  272. /* In the past, wc_pem_password_cb was called pem_password_cb, which is the same
  273. * name as an identical typedef in OpenSSL. We don't want to break existing code
  274. * that uses the name pem_password_cb, so we define it here as a macro alias for
  275. * wc_pem_password_cb. In cases where a user needs to use both OpenSSL and
  276. * wolfSSL headers in the same code, they should define OPENSSL_COEXIST to
  277. * avoid errors stemming from the typedef being declared twice. */
  278. #define pem_password_cb wc_pem_password_cb
  279. #endif
  280. typedef struct EncryptedInfo {
  281. long consumed; /* tracks PEM bytes consumed */
  282. #ifdef WOLFSSL_ENCRYPTED_KEYS
  283. wc_pem_password_cb* passwd_cb;
  284. void* passwd_userdata;
  285. int cipherType;
  286. word32 keySz;
  287. word32 ivSz; /* salt or encrypted IV size */
  288. char name[NAME_SZ]; /* cipher name, such as "DES-CBC" */
  289. byte iv[IV_SZ]; /* salt or encrypted IV */
  290. word16 set:1; /* if encryption set */
  291. #endif
  292. } EncryptedInfo;
  293. #define WOLFSSL_ASN1_INTEGER_MAX 20
  294. typedef struct WOLFSSL_ASN1_INTEGER {
  295. /* size can be increased set at 20 for tag, length then to hold at least 16
  296. * byte type */
  297. unsigned char intData[WOLFSSL_ASN1_INTEGER_MAX];
  298. /* ASN_INTEGER | LENGTH | hex of number */
  299. unsigned char negative; /* negative number flag */
  300. unsigned char* data;
  301. unsigned int dataMax; /* max size of data buffer */
  302. unsigned int isDynamic:1; /* flag for if data pointer dynamic (1 is yes 0 is no) */
  303. int length; /* Length of DER encoding. */
  304. int type; /* ASN.1 type. Includes negative flag. */
  305. } WOLFSSL_ASN1_INTEGER;
  306. #if defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_EXT)
  307. #ifdef WOLFSSL_EKU_OID
  308. #ifndef CTC_MAX_EKU_NB
  309. #define CTC_MAX_EKU_NB 1
  310. #endif
  311. #ifndef CTC_MAX_EKU_OID_SZ
  312. #define CTC_MAX_EKU_OID_SZ 30
  313. #endif
  314. #else
  315. #undef CTC_MAX_EKU_OID_SZ
  316. #define CTC_MAX_EKU_OID_SZ 0
  317. #endif
  318. #endif /* WOLFSSL_CERT_GEN || WOLFSSL_CERT_EXT */
  319. #if defined(WOLFSSL_CERT_GEN) || defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  320. #ifdef WOLFSSL_MULTI_ATTRIB
  321. #ifndef CTC_MAX_ATTRIB
  322. #define CTC_MAX_ATTRIB 4
  323. #endif
  324. /* ASN Encoded Name field */
  325. typedef struct NameAttrib {
  326. int sz; /* actual string value length */
  327. int id; /* id of name */
  328. int type; /* enc of name */
  329. char value[CTC_NAME_SIZE]; /* name */
  330. } NameAttrib;
  331. #endif /* WOLFSSL_MULTI_ATTRIB */
  332. #endif /* WOLFSSL_CERT_GEN || OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  333. #ifdef WOLFSSL_CUSTOM_OID
  334. typedef struct CertOidField {
  335. byte* oid;
  336. byte* val;
  337. int oidSz;
  338. int valSz;
  339. char enc;
  340. } CertOidField;
  341. typedef struct CertExtension {
  342. char* oid;
  343. byte crit;
  344. byte* val;
  345. int valSz;
  346. } CertExtension;
  347. #endif
  348. #if defined(WOLFSSL_CERT_GEN) || defined(OPENSSL_EXTRA) || \
  349. defined(OPENSSL_EXTRA_X509_SMALL) || defined(WOLFSSL_CERT_EXT) || \
  350. defined(WOLFSSL_CERT_REQ)
  351. typedef struct CertName {
  352. char country[CTC_NAME_SIZE];
  353. char countryEnc;
  354. char state[CTC_NAME_SIZE];
  355. char stateEnc;
  356. char street[CTC_NAME_SIZE];
  357. char streetEnc;
  358. char locality[CTC_NAME_SIZE];
  359. char localityEnc;
  360. char sur[CTC_NAME_SIZE];
  361. char surEnc;
  362. #ifdef WOLFSSL_CERT_NAME_ALL
  363. char givenName[CTC_NAME_SIZE];
  364. char givenNameEnc;
  365. char initials[CTC_NAME_SIZE];
  366. char initialsEnc;
  367. char dnQualifier[CTC_NAME_SIZE];
  368. char dnQualifierEnc;
  369. char dnName[CTC_NAME_SIZE];
  370. char dnNameEnc;
  371. #endif /* WOLFSSL_CERT_NAME_ALL */
  372. char org[CTC_NAME_SIZE];
  373. char orgEnc;
  374. char unit[CTC_NAME_SIZE];
  375. char unitEnc;
  376. char commonName[CTC_NAME_SIZE];
  377. char commonNameEnc;
  378. char serialDev[CTC_NAME_SIZE];
  379. char serialDevEnc;
  380. char userId[CTC_NAME_SIZE];
  381. char userIdEnc;
  382. char postalCode[CTC_NAME_SIZE];
  383. char postalCodeEnc;
  384. #ifdef WOLFSSL_CERT_EXT
  385. char busCat[CTC_NAME_SIZE];
  386. char busCatEnc;
  387. char joiC[CTC_NAME_SIZE];
  388. char joiCEnc;
  389. char joiSt[CTC_NAME_SIZE];
  390. char joiStEnc;
  391. #endif
  392. char email[CTC_NAME_SIZE]; /* !!!! email has to be last !!!! */
  393. #ifdef WOLFSSL_MULTI_ATTRIB
  394. NameAttrib name[CTC_MAX_ATTRIB];
  395. #endif
  396. #ifdef WOLFSSL_CUSTOM_OID
  397. CertOidField custom;
  398. #endif
  399. } CertName;
  400. #endif /* WOLFSSL_CERT_GEN || OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL*/
  401. #ifndef NUM_CUSTOM_EXT
  402. #define NUM_CUSTOM_EXT 16
  403. #endif /* NUM_CUSTOM_EXT */
  404. /* for user to fill for certificate generation */
  405. typedef struct Cert {
  406. int version; /* x509 version */
  407. byte serial[CTC_SERIAL_SIZE]; /* serial number */
  408. int serialSz; /* serial size */
  409. int sigType; /* signature algo type */
  410. #if defined(WOLFSSL_CERT_GEN) || defined(OPENSSL_EXTRA) \
  411. || defined(OPENSSL_EXTRA_X509_SMALL)
  412. CertName issuer; /* issuer info */
  413. CertName subject; /* subject info */
  414. #endif /* WOLFSSL_CERT_GEN || OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  415. int daysValid; /* validity days */
  416. int selfSigned; /* self signed flag */
  417. int isCA; /* is this going to be a CA */
  418. byte pathLen; /* max depth of valid certification
  419. * paths that include this cert */
  420. /* internal use only */
  421. int bodySz; /* pre sign total size */
  422. int keyType; /* public key type of subject */
  423. #ifdef WOLFSSL_ALT_NAMES
  424. byte altNames[CTC_MAX_ALT_SIZE]; /* altNames copy */
  425. int altNamesSz; /* altNames size in bytes */
  426. #endif
  427. byte beforeDate[CTC_DATE_SIZE]; /* before date copy */
  428. int beforeDateSz; /* size of copy */
  429. byte afterDate[CTC_DATE_SIZE]; /* after date copy */
  430. int afterDateSz; /* size of copy */
  431. #ifdef WOLFSSL_CERT_EXT
  432. byte skid[CTC_MAX_SKID_SIZE]; /* Subject Key Identifier */
  433. int skidSz; /* SKID size in bytes */
  434. byte akid[CTC_MAX_AKID_SIZE
  435. #ifdef WOLFSSL_AKID_NAME
  436. + sizeof(CertName) + CTC_SERIAL_SIZE
  437. #endif
  438. ]; /* Authority Key
  439. * Identifier */
  440. int akidSz; /* AKID size in bytes */
  441. #ifdef WOLFSSL_AKID_NAME
  442. byte rawAkid; /* Set to true if akid is a
  443. * AuthorityKeyIdentifier object.
  444. * Set to false if akid is just a
  445. * KeyIdentifier object. */
  446. #endif
  447. word16 keyUsage; /* Key Usage */
  448. byte extKeyUsage; /* Extended Key Usage */
  449. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  450. byte nsCertType; /* Netscape Certificate Type */
  451. #endif
  452. #ifdef WOLFSSL_EKU_OID
  453. /* Extended Key Usage OIDs */
  454. byte extKeyUsageOID[CTC_MAX_EKU_NB][CTC_MAX_EKU_OID_SZ];
  455. byte extKeyUsageOIDSz[CTC_MAX_EKU_NB];
  456. #endif
  457. char certPolicies[CTC_MAX_CERTPOL_NB][CTC_MAX_CERTPOL_SZ];
  458. word16 certPoliciesNb; /* Number of Cert Policy */
  459. byte crlInfo[CTC_MAX_CRLINFO_SZ]; /* CRL Distribution points */
  460. int crlInfoSz;
  461. #endif
  462. #if defined(WOLFSSL_CERT_EXT) || defined(OPENSSL_EXTRA) || \
  463. defined(WOLFSSL_CERT_REQ)
  464. byte issRaw[sizeof(CertName)]; /* raw issuer info */
  465. byte sbjRaw[sizeof(CertName)]; /* raw subject info */
  466. #endif
  467. #ifdef WOLFSSL_CERT_REQ
  468. char challengePw[CTC_NAME_SIZE];
  469. char unstructuredName[CTC_NAME_SIZE];
  470. int challengePwPrintableString; /* encode as PrintableString */
  471. #endif /* WOLFSSL_CERT_REQ */
  472. #ifdef WOLFSSL_CUSTOM_OID
  473. /* user oid and value to go in req extensions */
  474. CertOidField extCustom;
  475. /* Extensions to go into X.509 certificates */
  476. CertExtension customCertExt[NUM_CUSTOM_EXT];
  477. int customCertExtCount;
  478. #endif /* WOLFSSL_CUSTOM_OID */
  479. void* decodedCert; /* internal DecodedCert allocated from heap */
  480. byte* der; /* Pointer to buffer of current DecodedCert cache */
  481. void* heap; /* heap hint */
  482. byte basicConstSet:1; /* Indicator for when Basic Constraint is set */
  483. #ifdef WOLFSSL_ALLOW_ENCODING_CA_FALSE
  484. byte isCaSet:1; /* Indicator for when isCA is set */
  485. #endif
  486. byte pathLenSet:1; /* Indicator for when path length is set */
  487. #ifdef WOLFSSL_ALT_NAMES
  488. byte altNamesCrit:1; /* Indicator of criticality of SAN extension */
  489. #endif
  490. } Cert;
  491. /* Initialize and Set Certificate defaults:
  492. version = 3 (0x2)
  493. serial = 0 (Will be randomly generated)
  494. sigType = SHA_WITH_RSA
  495. issuer = blank
  496. daysValid = 500
  497. selfSigned = 1 (true) use subject as issuer
  498. subject = blank
  499. isCA = 0 (false)
  500. keyType = RSA_KEY (default)
  501. */
  502. WOLFSSL_ABI WOLFSSL_API int wc_InitCert(Cert* cert);
  503. WOLFSSL_ABI WOLFSSL_API Cert* wc_CertNew(void* heap);
  504. WOLFSSL_ABI WOLFSSL_API void wc_CertFree(Cert* cert);
  505. WOLFSSL_API int wc_InitCert_ex(Cert* cert, void* heap, int devId);
  506. WOLFSSL_API int wc_MakeCert_ex(Cert* cert, byte* derBuffer, word32 derSz,
  507. int keyType, void* key, WC_RNG* rng);
  508. WOLFSSL_ABI
  509. WOLFSSL_API int wc_MakeCert(Cert* cert, byte* derBuffer, word32 derSz,
  510. RsaKey* rsaKey, ecc_key* eccKey, WC_RNG* rng);
  511. #ifdef WOLFSSL_CERT_REQ
  512. WOLFSSL_API int wc_MakeCertReq_ex(Cert* cert, byte* derBuffer, word32 derSz,
  513. int keyType, void* key);
  514. WOLFSSL_ABI
  515. WOLFSSL_API int wc_MakeCertReq(Cert* cert, byte* derBuffer, word32 derSz,
  516. RsaKey* rsaKey, ecc_key* eccKey);
  517. #endif
  518. WOLFSSL_API int wc_SignCert_ex(int requestSz, int sType, byte* buf,
  519. word32 buffSz, int keyType, void* key,
  520. WC_RNG* rng);
  521. WOLFSSL_API int wc_SignCert(int requestSz, int sType, byte* buf, word32 buffSz,
  522. RsaKey* rsaKey, ecc_key* eccKey, WC_RNG* rng);
  523. WOLFSSL_ABI
  524. WOLFSSL_API int wc_MakeSelfCert(Cert* cert, byte* buf, word32 buffSz,
  525. RsaKey* key, WC_RNG* rng);
  526. WOLFSSL_ABI WOLFSSL_API int wc_SetIssuer(Cert* cert, const char* issuerFile);
  527. WOLFSSL_ABI WOLFSSL_API int wc_SetSubject(Cert* cert, const char* subjectFile);
  528. #ifdef WOLFSSL_ALT_NAMES
  529. WOLFSSL_ABI WOLFSSL_API int wc_SetAltNames(Cert* cert, const char* file);
  530. #endif
  531. #ifdef WOLFSSL_CERT_GEN_CACHE
  532. WOLFSSL_ABI WOLFSSL_API void wc_SetCert_Free(Cert* cert);
  533. #endif
  534. WOLFSSL_ABI
  535. WOLFSSL_API int wc_SetIssuerBuffer(Cert* cert, const byte* der, int derSz);
  536. WOLFSSL_ABI
  537. WOLFSSL_API int wc_SetSubjectBuffer(Cert* cert, const byte* der, int derSz);
  538. WOLFSSL_ABI
  539. WOLFSSL_API int wc_SetAltNamesBuffer(Cert* cert, const byte* der, int derSz);
  540. WOLFSSL_ABI
  541. WOLFSSL_API int wc_SetDatesBuffer(Cert* cert, const byte* der, int derSz);
  542. #ifndef NO_ASN_TIME
  543. WOLFSSL_API int wc_GetCertDates(Cert* cert, struct tm* before,
  544. struct tm* after);
  545. #endif
  546. #ifdef WOLFSSL_CERT_EXT
  547. WOLFSSL_API int wc_SetAuthKeyIdFromPublicKey_ex(Cert *cert, int keyType,
  548. void* key);
  549. WOLFSSL_API int wc_SetAuthKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
  550. ecc_key *eckey);
  551. WOLFSSL_API int wc_SetAuthKeyIdFromCert(Cert *cert, const byte *der, int derSz);
  552. WOLFSSL_API int wc_SetAuthKeyId(Cert *cert, const char* file);
  553. WOLFSSL_API int wc_SetSubjectKeyIdFromPublicKey_ex(Cert *cert, int keyType,
  554. void* key);
  555. WOLFSSL_API int wc_SetSubjectKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey,
  556. ecc_key *eckey);
  557. WOLFSSL_API int wc_SetSubjectKeyId(Cert *cert, const char* file);
  558. WOLFSSL_ABI WOLFSSL_API int wc_GetSubjectRaw(byte **subjectRaw, Cert *cert);
  559. WOLFSSL_ABI
  560. WOLFSSL_API int wc_SetSubjectRaw(Cert* cert, const byte* der, int derSz);
  561. WOLFSSL_ABI
  562. WOLFSSL_API int wc_SetIssuerRaw(Cert* cert, const byte* der, int derSz);
  563. /* Set the KeyUsage.
  564. * Value is a string separated tokens with ','. Accepted tokens are :
  565. * digitalSignature,nonRepudiation,contentCommitment,keyCertSign,cRLSign,
  566. * dataEncipherment,keyAgreement,keyEncipherment,encipherOnly and decipherOnly.
  567. *
  568. * nonRepudiation and contentCommitment are for the same usage.
  569. */
  570. WOLFSSL_API int wc_SetKeyUsage(Cert *cert, const char *value);
  571. /* Set ExtendedKeyUsage
  572. * Value is a string separated tokens with ','. Accepted tokens are :
  573. * any,serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,OCSPSigning
  574. */
  575. WOLFSSL_API int wc_SetExtKeyUsage(Cert *cert, const char *value);
  576. #ifdef WOLFSSL_EKU_OID
  577. /* Set ExtendedKeyUsage with unique OID
  578. * oid is expected to be in byte representation
  579. */
  580. WOLFSSL_API int wc_SetExtKeyUsageOID(Cert *cert, const char *oid, word32 sz,
  581. byte idx, void* heap);
  582. #endif /* WOLFSSL_EKU_OID */
  583. #if defined(WOLFSSL_ASN_TEMPLATE) && defined(WOLFSSL_CUSTOM_OID) && \
  584. defined(HAVE_OID_ENCODING)
  585. WOLFSSL_API int wc_SetCustomExtension(Cert *cert, int critical, const char *oid,
  586. const byte *der, word32 derSz);
  587. #endif
  588. #endif /* WOLFSSL_CERT_EXT */
  589. WOLFSSL_API int wc_GetDateInfo(const byte* certDate, int certDateSz,
  590. const byte** date, byte* format, int* length);
  591. #ifndef NO_ASN_TIME
  592. WOLFSSL_API int wc_GetDateAsCalendarTime(const byte* date, int length,
  593. byte format, struct tm* timearg);
  594. #endif
  595. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  596. WOLFSSL_API int wc_PemGetHeaderFooter(int type, const char** header,
  597. const char** footer);
  598. #endif
  599. WOLFSSL_API int wc_AllocDer(DerBuffer** pDer, word32 length, int type, void* heap);
  600. WOLFSSL_API void wc_FreeDer(DerBuffer** pDer);
  601. #ifdef WOLFSSL_PEM_TO_DER
  602. WOLFSSL_API int wc_PemToDer(const unsigned char* buff, long longSz, int type,
  603. DerBuffer** pDer, void* heap, EncryptedInfo* info, int* keyFormat);
  604. WOLFSSL_API int wc_KeyPemToDer(const unsigned char* pem, int pemSz,
  605. unsigned char* buff, int buffSz, const char* pass);
  606. WOLFSSL_API int wc_CertPemToDer(const unsigned char* pem, int pemSz,
  607. unsigned char* buff, int buffSz, int type);
  608. #endif /* WOLFSSL_PEM_TO_DER */
  609. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  610. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_PEM_TO_DER)
  611. WOLFSSL_API int wc_PemPubKeyToDer(const char* fileName,
  612. unsigned char* derBuf, int derSz);
  613. WOLFSSL_API int wc_PemPubKeyToDer_ex(const char* fileName, DerBuffer** der);
  614. #endif
  615. WOLFSSL_API int wc_PubKeyPemToDer(const unsigned char* pem, int pemSz,
  616. unsigned char* buff, int buffSz);
  617. #endif /* WOLFSSL_CERT_EXT || WOLFSSL_PUB_PEM_TO_DER */
  618. #ifdef WOLFSSL_CERT_GEN
  619. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_PEM_TO_DER)
  620. WOLFSSL_API int wc_PemCertToDer(const char* fileName,
  621. unsigned char* derBuf, int derSz);
  622. WOLFSSL_API int wc_PemCertToDer_ex(const char* fileName, DerBuffer** der);
  623. #endif
  624. #endif /* WOLFSSL_CERT_GEN */
  625. #ifdef WOLFSSL_DER_TO_PEM
  626. WOLFSSL_ABI
  627. WOLFSSL_API int wc_DerToPem(const byte* der, word32 derSz, byte* output,
  628. word32 outputSz, int type);
  629. WOLFSSL_API int wc_DerToPemEx(const byte* der, word32 derSz, byte* output,
  630. word32 outputSz, byte *cipherIno, int type);
  631. #endif
  632. #ifndef NO_RSA
  633. #if !defined(HAVE_USER_RSA)
  634. WOLFSSL_API int wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx,
  635. word32 inSz, const byte** n, word32* nSz, const byte** e, word32* eSz);
  636. /* For FIPS v1/v2 and selftest this is in rsa.h */
  637. #if (!defined(HAVE_SELFTEST) || \
  638. (defined(HAVE_SELFTEST) && defined(WOLFSSL_CERT_GEN) && \
  639. !defined(WOLFSSL_KEY_GEN))) && \
  640. (!defined(HAVE_FIPS) || \
  641. !defined(HAVE_FIPS_VERSION) || \
  642. ((HAVE_FIPS_VERSION > 2) && \
  643. (! ((HAVE_FIPS_VERSION == 5) && (HAVE_FIPS_VERSION_MINOR == 0)))))
  644. WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey* key, byte* output, word32 inLen);
  645. #endif
  646. #endif /* !HAVE_USER_RSA */
  647. WOLFSSL_API int wc_RsaPublicKeyDerSize(RsaKey* key, int with_header);
  648. WOLFSSL_API int wc_RsaKeyToPublicDer_ex(RsaKey* key, byte* output, word32 inLen,
  649. int with_header);
  650. /* For FIPS v1/v2 and selftest rsa.h is replaced. */
  651. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  652. (!defined(HAVE_FIPS_VERSION) || (FIPS_VERSION_LE(5,2))))
  653. WOLFSSL_API int wc_RsaPrivateKeyValidate(const byte* input,
  654. word32* inOutIdx, int* keySz, word32 inSz);
  655. #endif
  656. #endif
  657. #ifndef NO_DSA
  658. /* DSA parameter DER helper functions */
  659. WOLFSSL_API int wc_DsaParamsDecode(const byte* input, word32* inOutIdx, DsaKey* key,
  660. word32 inSz);
  661. WOLFSSL_API int wc_DsaKeyToParamsDer(DsaKey* key, byte* output,
  662. word32 inLen);
  663. WOLFSSL_API int wc_DsaKeyToParamsDer_ex(DsaKey* key, byte* output,
  664. word32* inLen);
  665. #endif
  666. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  667. WOLFSSL_LOCAL int wc_DhKeyToDer(DhKey* key, byte* output, word32* outSz, int exportPriv);
  668. WOLFSSL_API int wc_DhParamsToDer(DhKey* key, byte* out, word32* outSz);
  669. WOLFSSL_API int wc_DhPubKeyToDer(DhKey* key, byte* out, word32* outSz);
  670. WOLFSSL_API int wc_DhPrivKeyToDer(DhKey* key, byte* out, word32* outSz);
  671. #endif
  672. #ifdef HAVE_ECC
  673. /* private key helpers */
  674. WOLFSSL_ABI
  675. WOLFSSL_API int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx,
  676. ecc_key* key, word32 inSz);
  677. WOLFSSL_LOCAL int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 *inLen,
  678. int pubIn, int curveIn);
  679. WOLFSSL_ABI
  680. WOLFSSL_API int wc_EccKeyToDer(ecc_key* key, byte* output, word32 inLen);
  681. WOLFSSL_API int wc_EccPrivateKeyToDer(ecc_key* key, byte* output,
  682. word32 inLen);
  683. WOLFSSL_API int wc_EccKeyDerSize(ecc_key* key, int pub);
  684. WOLFSSL_API int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output,
  685. word32* outLen);
  686. WOLFSSL_API int wc_EccKeyToPKCS8(ecc_key* key, byte* output,
  687. word32* outLen);
  688. /* public key helper */
  689. WOLFSSL_ABI
  690. WOLFSSL_API int wc_EccPublicKeyDecode(const byte* input, word32* inOutIdx,
  691. ecc_key* key, word32 inSz);
  692. WOLFSSL_ABI WOLFSSL_API int wc_EccPublicKeyToDer(ecc_key* key, byte* output,
  693. word32 inLen, int with_AlgCurve);
  694. WOLFSSL_API int wc_EccPublicKeyToDer_ex(ecc_key* key, byte* output,
  695. word32 inLen, int with_AlgCurve,
  696. int comp);
  697. WOLFSSL_API int wc_EccPublicKeyDerSize(ecc_key* key, int with_AlgCurve);
  698. #endif
  699. /* RFC 5958 (Asymmetric Key Packages) */
  700. #if !defined(WC_ENABLE_ASYM_KEY_EXPORT) && \
  701. ((defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)) || \
  702. (defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)) || \
  703. (defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)) || \
  704. (defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_EXPORT)) || \
  705. (defined(HAVE_PQC) && (defined(HAVE_FALCON) || \
  706. defined(HAVE_DILITHIUM) || defined(HAVE_SPHINCS))))
  707. #define WC_ENABLE_ASYM_KEY_EXPORT
  708. #endif
  709. #if !defined(WC_ENABLE_ASYM_KEY_IMPORT) && \
  710. ((defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)) || \
  711. (defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_IMPORT)) || \
  712. (defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)) || \
  713. (defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_IMPORT)) || \
  714. (defined(HAVE_PQC) && (defined(HAVE_FALCON) || \
  715. defined(HAVE_DILITHIUM) || defined(HAVE_SPHINCS))))
  716. #define WC_ENABLE_ASYM_KEY_IMPORT
  717. #endif
  718. #ifdef HAVE_ED25519
  719. #ifdef HAVE_ED25519_KEY_IMPORT
  720. WOLFSSL_API int wc_Ed25519PrivateKeyDecode(const byte* input, word32* inOutIdx,
  721. ed25519_key* key, word32 inSz);
  722. WOLFSSL_API int wc_Ed25519PublicKeyDecode(const byte* input, word32* inOutIdx,
  723. ed25519_key* key, word32 inSz);
  724. #endif
  725. #ifdef HAVE_ED25519_KEY_EXPORT
  726. WOLFSSL_API int wc_Ed25519KeyToDer(ed25519_key* key, byte* output,
  727. word32 inLen);
  728. WOLFSSL_API int wc_Ed25519PrivateKeyToDer(ed25519_key* key, byte* output,
  729. word32 inLen);
  730. WOLFSSL_API int wc_Ed25519PublicKeyToDer(ed25519_key* key, byte* output,
  731. word32 inLen, int withAlg);
  732. #endif
  733. #endif /* HAVE_ED25519 */
  734. #ifdef HAVE_CURVE25519
  735. #ifdef HAVE_CURVE25519_KEY_IMPORT
  736. WOLFSSL_API int wc_Curve25519PrivateKeyDecode(
  737. const byte* input, word32* inOutIdx, curve25519_key* key, word32 inSz);
  738. WOLFSSL_API int wc_Curve25519PublicKeyDecode(
  739. const byte* input, word32* inOutIdx, curve25519_key* key, word32 inSz);
  740. #endif
  741. #ifdef HAVE_CURVE25519_KEY_EXPORT
  742. WOLFSSL_API int wc_Curve25519PrivateKeyToDer(
  743. curve25519_key* key, byte* output, word32 inLen);
  744. WOLFSSL_API int wc_Curve25519PublicKeyToDer(
  745. curve25519_key* key, byte* output, word32 inLen, int withAlg);
  746. #endif
  747. #endif /* HAVE_CURVE25519 */
  748. #ifdef HAVE_ED448
  749. #ifdef HAVE_ED448_KEY_IMPORT
  750. WOLFSSL_API int wc_Ed448PrivateKeyDecode(
  751. const byte* input, word32* inOutIdx, ed448_key* key, word32 inSz);
  752. WOLFSSL_API int wc_Ed448PublicKeyDecode(
  753. const byte* input, word32* inOutIdx, ed448_key* key, word32 inSz);
  754. #endif
  755. #ifdef HAVE_ED448_KEY_EXPORT
  756. WOLFSSL_API int wc_Ed448KeyToDer(ed448_key* key, byte* output, word32 inLen);
  757. WOLFSSL_API int wc_Ed448PrivateKeyToDer(
  758. ed448_key* key, byte* output, word32 inLen);
  759. WOLFSSL_API int wc_Ed448PublicKeyToDer(
  760. ed448_key* key, byte* output, word32 inLen, int withAlg);
  761. #endif
  762. #endif /* HAVE_ED448 */
  763. #ifdef HAVE_CURVE448
  764. #ifdef HAVE_CURVE448_KEY_IMPORT
  765. WOLFSSL_API int wc_Curve448PrivateKeyDecode(const byte* input, word32* inOutIdx,
  766. curve448_key* key, word32 inSz);
  767. WOLFSSL_API int wc_Curve448PublicKeyDecode(const byte* input, word32* inOutIdx,
  768. curve448_key* key, word32 inSz);
  769. #endif
  770. #ifdef HAVE_CURVE448_KEY_EXPORT
  771. WOLFSSL_API int wc_Curve448PrivateKeyToDer(curve448_key* key, byte* output,
  772. word32 inLen);
  773. WOLFSSL_API int wc_Curve448PublicKeyToDer(curve448_key* key, byte* output,
  774. word32 inLen, int withAlg);
  775. #endif
  776. #endif /* HAVE_CURVE448 */
  777. /* DER encode signature */
  778. WOLFSSL_API word32 wc_EncodeSignature(byte* out, const byte* digest,
  779. word32 digSz, int hashOID);
  780. WOLFSSL_API int wc_GetCTC_HashOID(int type);
  781. WOLFSSL_API int wc_GetPkcs8TraditionalOffset(byte* input,
  782. word32* inOutIdx, word32 sz);
  783. WOLFSSL_API int wc_CreatePKCS8Key(byte* out, word32* outSz,
  784. byte* key, word32 keySz, int algoID, const byte* curveOID,
  785. word32 oidSz);
  786. WOLFSSL_API int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
  787. const char* password, int passwordSz, int vPKCS, int pbeOid,
  788. int encAlgId, byte* salt, word32 saltSz, int itt, WC_RNG* rng,
  789. void* heap);
  790. WOLFSSL_API int wc_DecryptPKCS8Key(byte* input, word32 sz, const char* password,
  791. int passwordSz);
  792. WOLFSSL_API int wc_CreateEncryptedPKCS8Key(byte* key, word32 keySz, byte* out,
  793. word32* outSz, const char* password, int passwordSz, int vPKCS,
  794. int pbeOid, int encAlgId, byte* salt, word32 saltSz, int itt,
  795. WC_RNG* rng, void* heap);
  796. #ifndef NO_ASN_TIME
  797. /* Time */
  798. /* Returns seconds (Epoch/UTC)
  799. * timePtr: is "time_t", which is typically "long"
  800. * Example:
  801. long lTime;
  802. rc = wc_GetTime(&lTime, (word32)sizeof(lTime));
  803. */
  804. WOLFSSL_API int wc_GetTime(void* timePtr, word32 timeSize);
  805. typedef time_t (*wc_time_cb)(time_t* t);
  806. WOLFSSL_API int wc_SetTimeCb(wc_time_cb f);
  807. WOLFSSL_API time_t wc_Time(time_t* t);
  808. #endif
  809. #ifdef WOLFSSL_ENCRYPTED_KEYS
  810. WOLFSSL_API int wc_EncryptedInfoGet(EncryptedInfo* info,
  811. const char* cipherInfo);
  812. #endif
  813. #ifdef WOLFSSL_CERT_PIV
  814. typedef struct _wc_CertPIV {
  815. const byte* cert;
  816. word32 certSz;
  817. const byte* certErrDet;
  818. word32 certErrDetSz;
  819. const byte* nonce; /* Identiv Only */
  820. word32 nonceSz; /* Identiv Only */
  821. const byte* signedNonce; /* Identiv Only */
  822. word32 signedNonceSz; /* Identiv Only */
  823. /* flags */
  824. word16 compression:2;
  825. word16 isX509:1;
  826. word16 isIdentiv:1;
  827. } wc_CertPIV;
  828. WOLFSSL_API int wc_ParseCertPIV(wc_CertPIV* cert, const byte* buf, word32 totalSz);
  829. #endif /* WOLFSSL_CERT_PIV */
  830. /* Forward declaration needed, as DecodedCert is defined in asn.h.*/
  831. struct DecodedCert;
  832. WOLFSSL_API void wc_InitDecodedCert(
  833. struct DecodedCert* cert, const byte* source, word32 inSz, void* heap);
  834. WOLFSSL_API void wc_FreeDecodedCert(struct DecodedCert* cert);
  835. WOLFSSL_API int wc_ParseCert(
  836. struct DecodedCert* cert, int type, int verify, void* cm);
  837. WOLFSSL_API int wc_GetPubKeyDerFromCert(struct DecodedCert* cert,
  838. byte* derKey, word32* derKeySz);
  839. #ifdef WOLFSSL_FPKI
  840. WOLFSSL_API int wc_GetUUIDFromCert(struct DecodedCert* cert,
  841. byte* uuid, word32* uuidSz);
  842. WOLFSSL_API int wc_GetFASCNFromCert(struct DecodedCert* cert,
  843. byte* fascn, word32* fascnSz);
  844. #endif /* WOLFSSL_FPKI */
  845. #if !defined(XFPRINTF) || defined(NO_FILESYSTEM) || \
  846. defined(NO_STDIO_FILESYSTEM) && defined(WOLFSSL_ASN_PRINT)
  847. #undef WOLFSSL_ASN_PRINT
  848. #endif
  849. #ifdef WOLFSSL_ASN_PRINT
  850. enum Asn1PrintOpt {
  851. /* Offset into DER/BER data to start decoding from. */
  852. ASN1_PRINT_OPT_OFFSET,
  853. /* Length of DER/BER encoding to parse. */
  854. ASN1_PRINT_OPT_LENGTH,
  855. /* Number of spaces to indent for each change in depth. */
  856. ASN1_PRINT_OPT_INDENT,
  857. /* Draw branches instead of indenting. */
  858. ASN1_PRINT_OPT_DRAW_BRANCH,
  859. /* Show raw data of primitive types as octets. */
  860. ASN1_PRINT_OPT_SHOW_DATA,
  861. /* Show header data as octets. */
  862. ASN1_PRINT_OPT_SHOW_HEADER_DATA,
  863. /* Show the wolfSSL OID value for OBJECT_ID. */
  864. ASN1_PRINT_OPT_SHOW_OID,
  865. /* Don't show text representations of primitive types. */
  866. ASN1_PRINT_OPT_SHOW_NO_TEXT,
  867. /* Don't show dump text representations of primitive types. */
  868. ASN1_PRINT_OPT_SHOW_NO_DUMP_TEXT,
  869. };
  870. /* ASN.1 print options. */
  871. typedef struct Asn1PrintOptions {
  872. /* Offset into DER/BER encoding to start parsing from. */
  873. word32 offset;
  874. /* Length of DER/BER encoding to parse. */
  875. word32 length;
  876. /* Number of spaces to indent for each change in depth. */
  877. word8 indent;
  878. /* Draw branches instead of indenting. */
  879. word8 draw_branch:1;
  880. /* Show raw data of primitive types as octets. */
  881. word8 show_data:1;
  882. /* Show header data as octets. */
  883. word8 show_header_data:1;
  884. /* Show the wolfSSL OID value for OBJECT_ID. */
  885. word8 show_oid:1;
  886. /* Don't show text representations of primitive types. */
  887. word8 show_no_text:1;
  888. /* Don't show dump text representations of primitive types. */
  889. word8 show_no_dump_text:1;
  890. } Asn1PrintOptions;
  891. /* ASN.1 item data. */
  892. typedef struct Asn1Item {
  893. /* Tag of current item. */
  894. unsigned char tag;
  895. /* Whether current item is constructed. */
  896. unsigned char cons;
  897. /* Length of data in current ASN.1 item. */
  898. word32 len;
  899. /* Index into data of ASN.1 item data. */
  900. word32 data_idx;
  901. } Asn1Item;
  902. /* Maximum supported depth of ASN.1 items. */
  903. #define ASN_MAX_DEPTH 16
  904. /* ASN.1 parsing state. */
  905. typedef struct Asn1 {
  906. /* ASN.1 item data. */
  907. Asn1Item item;
  908. /* Current depth of ASN.1 item. */
  909. unsigned char depth;
  910. /* End indices of ASN.1 items at different depths. */
  911. word32 end_idx[ASN_MAX_DEPTH];
  912. /* Buffer to print. */
  913. unsigned char* data;
  914. /* Maximum number of bytes to process. */
  915. word32 max;
  916. /* Starting offset of current ASN.1 item. */
  917. word32 offset;
  918. /* Current offset into ASN.1 data. */
  919. word32 curr;
  920. /* Next part of ASN.1 item expected. */
  921. unsigned char part;
  922. /* File pointer to print to. */
  923. XFILE file;
  924. } Asn1;
  925. WOLFSSL_API int wc_Asn1PrintOptions_Init(Asn1PrintOptions* opts);
  926. WOLFSSL_API int wc_Asn1PrintOptions_Set(Asn1PrintOptions* opts,
  927. enum Asn1PrintOpt opt, word32 val);
  928. WOLFSSL_API int wc_Asn1_Init(Asn1* asn1);
  929. WOLFSSL_API int wc_Asn1_SetFile(Asn1* asn1, XFILE file);
  930. WOLFSSL_API int wc_Asn1_PrintAll(Asn1* asn1, Asn1PrintOptions* opts,
  931. unsigned char* data, word32 len);
  932. #endif /* WOLFSSL_ASN_PRINT */
  933. #ifdef __cplusplus
  934. } /* extern "C" */
  935. #endif
  936. #endif /* WOLF_CRYPT_ASN_PUBLIC_H */