asn_public.h 36 KB

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