asn.h 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548
  1. /* asn.h
  2. *
  3. * Copyright (C) 2006-2022 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.h
  23. */
  24. /*
  25. DESCRIPTION
  26. This library provides the interface to Abstract Syntax Notation One (ASN.1) objects.
  27. ASN.1 is a standard interface description language for defining data structures
  28. that can be serialized and deserialized in a cross-platform way.
  29. */
  30. #ifndef WOLF_CRYPT_ASN_H
  31. #define WOLF_CRYPT_ASN_H
  32. #include <wolfssl/wolfcrypt/types.h>
  33. #ifndef NO_ASN
  34. #if !defined(NO_ASN_TIME) && defined(NO_TIME_H)
  35. #define NO_ASN_TIME /* backwards compatibility with NO_TIME_H */
  36. #endif
  37. #include <wolfssl/wolfcrypt/integer.h>
  38. /* fips declare of RsaPrivateKeyDecode @wc_fips */
  39. #if defined(HAVE_FIPS) && !defined(NO_RSA) && \
  40. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  41. #include <cyassl/ctaocrypt/rsa.h>
  42. #endif
  43. #ifndef NO_DH
  44. #include <wolfssl/wolfcrypt/dh.h>
  45. #endif
  46. #ifndef NO_DSA
  47. #include <wolfssl/wolfcrypt/dsa.h>
  48. #endif
  49. #ifndef NO_SHA
  50. #include <wolfssl/wolfcrypt/sha.h>
  51. #endif
  52. #ifndef NO_MD5
  53. #include <wolfssl/wolfcrypt/md5.h>
  54. #endif
  55. #include <wolfssl/wolfcrypt/sha256.h>
  56. #include <wolfssl/wolfcrypt/asn_public.h> /* public interface */
  57. #if defined(NO_SHA) && defined(NO_SHA256)
  58. #define WC_SHA256_DIGEST_SIZE 32
  59. #endif
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. #ifndef EXTERNAL_SERIAL_SIZE
  64. #define EXTERNAL_SERIAL_SIZE 32
  65. #endif
  66. enum {
  67. ISSUER = 0,
  68. SUBJECT = 1,
  69. BEFORE = 0,
  70. AFTER = 1
  71. };
  72. /* ASN Tags */
  73. enum ASN_Tags {
  74. ASN_EOC = 0x00,
  75. ASN_BOOLEAN = 0x01,
  76. ASN_INTEGER = 0x02,
  77. ASN_BIT_STRING = 0x03,
  78. ASN_OCTET_STRING = 0x04,
  79. ASN_TAG_NULL = 0x05,
  80. ASN_OBJECT_ID = 0x06,
  81. ASN_ENUMERATED = 0x0a,
  82. ASN_UTF8STRING = 0x0c,
  83. ASN_SEQUENCE = 0x10,
  84. ASN_SET = 0x11,
  85. ASN_PRINTABLE_STRING = 0x13,
  86. ASN_T61STRING = 0x14,
  87. ASN_IA5_STRING = 0x16,
  88. ASN_UTC_TIME = 0x17,
  89. ASN_GENERALIZED_TIME = 0x18,
  90. ASN_UNIVERSALSTRING = 0x1c,
  91. ASN_BMPSTRING = 0x1e,
  92. ASN_TYPE_MASK = 0x1f,
  93. ASN_LONG_LENGTH = 0x80,
  94. ASN_INDEF_LENGTH = 0x80,
  95. /* ASN_Flags - Bitmask */
  96. ASN_CONSTRUCTED = 0x20,
  97. ASN_APPLICATION = 0x40,
  98. ASN_CONTEXT_SPECIFIC = 0x80,
  99. ASN_PRIVATE = 0xC0,
  100. CRL_EXTENSIONS = 0xa0,
  101. ASN_EXTENSIONS = 0xa3,
  102. /* GeneralName types */
  103. ASN_OTHER_TYPE = 0x00,
  104. ASN_RFC822_TYPE = 0x01,
  105. ASN_DNS_TYPE = 0x02,
  106. ASN_DIR_TYPE = 0x04,
  107. ASN_URI_TYPE = 0x06, /* the value 6 is from GeneralName OID */
  108. ASN_IP_TYPE = 0x07, /* the value 7 is from GeneralName OID */
  109. /* PKCS #7 types */
  110. ASN_ENC_CONTENT = 0x00,
  111. ASN_OTHERNAME_VALUE = 0x00,
  112. /* AuthorityKeyIdentifier fields */
  113. ASN_AUTHKEYID_KEYID = 0x00,
  114. ASN_AUTHKEYID_ISSUER = 0x01,
  115. ASN_AUTHKEYID_SERIAL = 0x02,
  116. /* GeneralSubtree fields */
  117. ASN_SUBTREE_MIN = 0x00,
  118. ASN_SUBTREE_MAX = 0x01,
  119. /* x509 Cert Fields */
  120. ASN_X509_CERT_VERSION = 0x00,
  121. /* x509 Cert Extension Fields */
  122. ASN_AKID_KEYID = 0x00,
  123. /* ECC Key Fields */
  124. ASN_ECC_PARAMS = 0x00,
  125. ASN_ECC_PUBKEY = 0x01,
  126. /* OneAsymmetricKey Fields */
  127. ASN_ASYMKEY_ATTRS = 0x00,
  128. ASN_ASYMKEY_PUBKEY = 0x01,
  129. };
  130. /* NOTE: If ASN_UTC_TIME_SIZE or ASN_GENERALIZED_TIME_SIZE are ever modified
  131. * one needs to update the logic in asn.c function GetAsnTimeString()
  132. * which depends on the size 14 and/or 16 to determine which format to
  133. * place in the "buf" (output)
  134. */
  135. #define ASN_UTC_TIME_SIZE 14 /* Read note above before modifying */
  136. #define ASN_GENERALIZED_TIME_SIZE 16 /* Read note above before modifying */
  137. #define ASN_GENERALIZED_TIME_MAX 68
  138. #ifdef WOLFSSL_ASN_TEMPLATE
  139. /* Different data types that can be stored in ASNGetData/ASNSetData. */
  140. enum ASNItem_DataType {
  141. /* Default for tag type. */
  142. ASN_DATA_TYPE_NONE = 0,
  143. /* 8-bit integer value. */
  144. ASN_DATA_TYPE_WORD8 = 1,
  145. /* 16-bit integer value. */
  146. ASN_DATA_TYPE_WORD16 = 2,
  147. /* 32-bit integer value. */
  148. ASN_DATA_TYPE_WORD32 = 4,
  149. /* Buffer with data and length. */
  150. ASN_DATA_TYPE_BUFFER = 5,
  151. /* An expected/required buffer with data and length. */
  152. ASN_DATA_TYPE_EXP_BUFFER = 6,
  153. /* Replace the item with buffer (data and length). */
  154. ASN_DATA_TYPE_REPLACE_BUFFER = 7,
  155. /* Big number as an mp_int. */
  156. ASN_DATA_TYPE_MP = 8,
  157. /* Big number as a positive or negative mp_int. */
  158. ASN_DATA_TYPE_MP_POS_NEG = 9,
  159. /* ASN.1 CHOICE. A 0 terminated list of tags that are valid. */
  160. ASN_DATA_TYPE_CHOICE = 10,
  161. };
  162. /* A template entry describing an ASN.1 item. */
  163. typedef struct ASNItem {
  164. /* Depth of ASN.1 item - how many constructed ASN.1 items above. */
  165. byte depth;
  166. /* BER/DER tag to expect. */
  167. byte tag;
  168. /* Whether the ASN.1 item is constructed. */
  169. byte constructed:1;
  170. /* Whether to parse the header only or skip data. If
  171. * ASNSetData.data.buffer.data is supplied then this option gets
  172. * overwritten and the child nodes get ignored. */
  173. byte headerOnly:1;
  174. /* Whether ASN.1 item is optional.
  175. * - 0 means not optional
  176. * - 1 means is optional
  177. * - 2+ means one of these at the same level with same value must appear.
  178. */
  179. byte optional;
  180. } ASNItem;
  181. /* Dynamic data for setting (encoding) an ASN.1 item. */
  182. typedef struct ASNSetData {
  183. /* Reverse offset into buffer of ASN.1 item - calculated in SizeASN_Items().
  184. * SetASN_Items() subtracts from total length to get usable value.
  185. */
  186. word32 offset;
  187. /* Length of data in ASN.1 item - calculated in SizeASN_Items(). */
  188. word32 length;
  189. /* Different data type representation. */
  190. union {
  191. /* 8-bit integer value. */
  192. byte u8;
  193. /* 16-bit integer value. */
  194. word16 u16;
  195. /* 32-bit integer value. */
  196. word32 u32;
  197. /* Big number as an mp_int. */
  198. mp_int* mp;
  199. /* Buffer as data pointer and length. */
  200. struct {
  201. /* Data to write out. */
  202. const byte* data;
  203. /* Length of data to write out. */
  204. word32 length;
  205. } buffer;
  206. } data;
  207. /* Type of data stored in data field - enum ASNItem_DataType. */
  208. byte dataType;
  209. /* Don't write this ASN.1 item out.
  210. * Optional items are dependent on the data being encoded.
  211. */
  212. byte noOut;
  213. } ASNSetData;
  214. /* Dynamic data for getting (decoding) an ASN.1 item. */
  215. typedef struct ASNGetData {
  216. /* Offset into buffer where encoding starts. */
  217. word32 offset;
  218. /* Total length of data in ASN.1 item.
  219. * BIT_STRING and INTEGER lengths include leading byte. */
  220. word32 length;
  221. union {
  222. /* Pointer to 8-bit integer. */
  223. byte* u8;
  224. /* Pointer to 16-bit integer. */
  225. word16* u16;
  226. /* Pointer to 32-bit integer. */
  227. word32* u32;
  228. /* Pointer to mp_int for big number. */
  229. mp_int* mp;
  230. /* List of possible tags. Useful for CHOICE ASN.1 items. */
  231. const byte* choice;
  232. /* Buffer to copy into. */
  233. struct {
  234. /* Buffer to hold ASN.1 data. */
  235. byte* data;
  236. /* Maximum length of buffer. */
  237. word32* length;
  238. } buffer;
  239. /* Refernce to ASN.1 item's data. */
  240. struct {
  241. /* Pointer reference into input buffer. */
  242. const byte* data;
  243. /* Length of data. */
  244. word32 length;
  245. } ref;
  246. /* Data of an OBJECT_ID. */
  247. struct {
  248. /* OID data reference into input buffer. */
  249. const byte* data;
  250. /* Length of OID data. */
  251. word32 length;
  252. /* Type of OID expected. */
  253. word32 type;
  254. /* OID sum - 32-bit id. */
  255. word32 sum;
  256. } oid;
  257. } data;
  258. /* Type of data stored in data field - enum ASNItem_DataType. */
  259. byte dataType;
  260. /* Tag found in BER/DER item. */
  261. byte tag;
  262. } ASNGetData;
  263. WOLFSSL_LOCAL int SizeASN_Items(const ASNItem* asn, ASNSetData *data,
  264. int count, int* encSz);
  265. WOLFSSL_LOCAL int SetASN_Items(const ASNItem* asn, ASNSetData *data, int count,
  266. byte* output);
  267. WOLFSSL_LOCAL int GetASN_Items(const ASNItem* asn, ASNGetData *data, int count,
  268. int complete, const byte* input, word32* inOutIdx, word32 maxIdx);
  269. #ifdef WOLFSSL_ASN_TEMPLATE_TYPE_CHECK
  270. WOLFSSL_LOCAL void GetASN_Int8Bit(ASNGetData *dataASN, byte* num);
  271. WOLFSSL_LOCAL void GetASN_Int16Bit(ASNGetData *dataASN, word16* num);
  272. WOLFSSL_LOCAL void GetASN_Int32Bit(ASNGetData *dataASN, word32* num);
  273. WOLFSSL_LOCAL void GetASN_Buffer(ASNGetData *dataASN, byte* data,
  274. word32* length);
  275. WOLFSSL_LOCAL void GetASN_ExpBuffer(ASNGetData *dataASN, const byte* data,
  276. word32 length);
  277. WOLFSSL_LOCAL void GetASN_MP(ASNGetData *dataASN, mp_int* num);
  278. WOLFSSL_LOCAL void GetASN_MP_PosNeg(ASNGetData *dataASN, mp_int* num);
  279. WOLFSSL_LOCAL void GetASN_Choice(ASNGetData *dataASN, const byte* options);
  280. WOLFSSL_LOCAL void GetASN_Boolean(ASNGetData *dataASN, byte* num);
  281. WOLFSSL_LOCAL void GetASN_OID(ASNGetData *dataASN, int oidType);
  282. WOLFSSL_LOCAL void GetASN_GetConstRef(ASNGetData * dataASN, const byte** data,
  283. word32* length);
  284. WOLFSSL_LOCAL void GetASN_GetRef(ASNGetData * dataASN, byte** data,
  285. word32* length);
  286. WOLFSSL_LOCAL void GetASN_OIDData(ASNGetData * dataASN, byte** data,
  287. word32* length);
  288. WOLFSSL_LOCAL void SetASN_Boolean(ASNSetData *dataASN, byte val);
  289. WOLFSSL_LOCAL void SetASN_Int8Bit(ASNSetData *dataASN, byte num);
  290. WOLFSSL_LOCAL void SetASN_Int16Bit(ASNSetData *dataASN, word16 num);
  291. WOLFSSL_LOCAL void SetASN_Buffer(ASNSetData *dataASN, const byte* data,
  292. word32 length);
  293. WOLFSSL_LOCAL void SetASN_ReplaceBuffer(ASNSetData *dataASN, const byte* data,
  294. word32 length);
  295. WOLFSSL_LOCAL void SetASN_MP(ASNSetData *dataASN, mp_int* num);
  296. WOLFSSL_LOCAL void SetASN_OID(ASNSetData *dataASN, int oid, int oidType);
  297. #else
  298. /* Setup ASN data item to get an 8-bit number.
  299. *
  300. * @param [in] dataASN Dynamic ASN data item.
  301. * @param [in] num Pointer to an 8-bit variable.
  302. */
  303. #define GetASN_Int8Bit(dataASN, num) \
  304. do { \
  305. (dataASN)->dataType = ASN_DATA_TYPE_WORD8; \
  306. (dataASN)->data.u8 = num; \
  307. } while (0)
  308. /* Setup ASN data item to get a 16-bit number.
  309. *
  310. * @param [in] dataASN Dynamic ASN data item.
  311. * @param [in] num Pointer to a 16-bit variable.
  312. */
  313. #define GetASN_Int16Bit(dataASN, num) \
  314. do { \
  315. (dataASN)->dataType = ASN_DATA_TYPE_WORD16; \
  316. (dataASN)->data.u16 = num; \
  317. } while (0)
  318. /* Setup ASN data item to get a 32-bit number.
  319. *
  320. * @param [in] dataASN Dynamic ASN data item.
  321. * @param [in] num Pointer to a 32-bit variable.
  322. */
  323. #define GetASN_Int32Bit(dataASN, num) \
  324. do { \
  325. (dataASN)->dataType = ASN_DATA_TYPE_WORD32; \
  326. (dataASN)->data.u32 = num; \
  327. } while (0)
  328. /* Setup ASN data item to get data into a buffer of a specific length.
  329. *
  330. * @param [in] dataASN Dynamic ASN data item.
  331. * @param [in] d Buffer to hold data.
  332. * @param [in] l Length of buffer in bytes.
  333. */
  334. #define GetASN_Buffer(dataASN, d, l) \
  335. do { \
  336. (dataASN)->dataType = ASN_DATA_TYPE_BUFFER; \
  337. (dataASN)->data.buffer.data = d; \
  338. (dataASN)->data.buffer.length = l; \
  339. } while (0)
  340. /* Setup ASN data item to check parsed data against expected buffer.
  341. *
  342. * @param [in] dataASN Dynamic ASN data item.
  343. * @param [in] d Buffer containing expected data.
  344. * @param [in] l Length of buffer in bytes.
  345. */
  346. #define GetASN_ExpBuffer(dataASN, d, l) \
  347. do { \
  348. (dataASN)->dataType = ASN_DATA_TYPE_EXP_BUFFER; \
  349. (dataASN)->data.ref.data = d; \
  350. (dataASN)->data.ref.length = l; \
  351. } while (0)
  352. /* Setup ASN data item to get a number into an mp_int.
  353. *
  354. * @param [in] dataASN Dynamic ASN data item.
  355. * @param [in] num Multi-precision number object.
  356. */
  357. #define GetASN_MP(dataASN, num) \
  358. do { \
  359. (dataASN)->dataType = ASN_DATA_TYPE_MP; \
  360. (dataASN)->data.mp = num; \
  361. } while (0)
  362. /* Setup ASN data item to get a positive or negative number into an mp_int.
  363. *
  364. * @param [in] dataASN Dynamic ASN data item.
  365. * @param [in] num Multi-precision number object.
  366. */
  367. #define GetASN_MP_PosNeg(dataASN, num) \
  368. do { \
  369. (dataASN)->dataType = ASN_DATA_TYPE_MP_POS_NEG; \
  370. (dataASN)->data.mp = num; \
  371. } while (0)
  372. /* Setup ASN data item to be a choice of tags.
  373. *
  374. * @param [in] dataASN Dynamic ASN data item.
  375. * @param [in] choice 0 terminated list of tags that are valid.
  376. */
  377. #define GetASN_Choice(dataASN, options) \
  378. do { \
  379. (dataASN)->dataType = ASN_DATA_TYPE_CHOICE; \
  380. (dataASN)->data.choice = options; \
  381. } while (0)
  382. /* Setup ASN data item to get a boolean value.
  383. *
  384. * @param [in] dataASN Dynamic ASN data item.
  385. * @param [in] num Pointer to an 8-bit variable.
  386. */
  387. #define GetASN_Boolean(dataASN, num) \
  388. do { \
  389. (dataASN)->dataType = ASN_DATA_TYPE_NONE; \
  390. (dataASN)->data.u8 = num; \
  391. } while (0)
  392. /* Setup ASN data item to be a an OID of a specific type.
  393. *
  394. * @param [in] dataASN Dynamic ASN data item.
  395. * @param [in] oidType Type of OID to expect.
  396. */
  397. #define GetASN_OID(dataASN, oidType) \
  398. (dataASN)->data.oid.type = oidType
  399. /* Get the data and length from an ASN data item.
  400. *
  401. * @param [in] dataASN Dynamic ASN data item.
  402. * @param [out] d Pointer to data of item.
  403. * @param [out] l Length of buffer in bytes.
  404. */
  405. #define GetASN_GetConstRef(dataASN, d, l) \
  406. do { \
  407. *(d) = (dataASN)->data.ref.data; \
  408. *(l) = (dataASN)->data.ref.length; \
  409. } while (0)
  410. /* Get the data and length from an ASN data item.
  411. *
  412. * @param [in] dataASN Dynamic ASN data item.
  413. * @param [out] d Pointer to data of item.
  414. * @param [out] l Length of buffer in bytes.
  415. */
  416. #define GetASN_GetRef(dataASN, d, l) \
  417. do { \
  418. *(d) = (byte*)(dataASN)->data.ref.data; \
  419. *(l) = (dataASN)->data.ref.length; \
  420. } while (0)
  421. /* Get the data and length from an ASN data item that is an OID.
  422. *
  423. * @param [in] dataASN Dynamic ASN data item.
  424. * @param [out] d Pointer to .
  425. * @param [out] l Length of buffer in bytes.
  426. */
  427. #define GetASN_OIDData(dataASN, d, l) \
  428. do { \
  429. *(d) = (byte*)(dataASN)->data.oid.data; \
  430. *(l) = (dataASN)->data.oid.length; \
  431. } while (0)
  432. /* Setup an ASN data item to set a boolean.
  433. *
  434. * @param [in] dataASN Dynamic ASN data item.
  435. * @param [in] val Boolean value.
  436. */
  437. #define SetASN_Boolean(dataASN, val) \
  438. do { \
  439. (dataASN)->dataType = ASN_DATA_TYPE_NONE; \
  440. (dataASN)->data.u8 = val; \
  441. } while (0)
  442. /* Setup an ASN data item to set an 8-bit number.
  443. *
  444. * @param [in] dataASN Dynamic ASN data item.
  445. * @param [in] num 8-bit number to set.
  446. */
  447. #define SetASN_Int8Bit(dataASN, num) \
  448. do { \
  449. (dataASN)->dataType = ASN_DATA_TYPE_WORD8; \
  450. (dataASN)->data.u8 = num; \
  451. } while (0)
  452. /* Setup an ASN data item to set a 16-bit number.
  453. *
  454. * @param [in] dataASN Dynamic ASN data item.
  455. * @param [in] num 16-bit number to set.
  456. */
  457. #define SetASN_Int16Bit(dataASN, num) \
  458. do { \
  459. (dataASN)->dataType = ASN_DATA_TYPE_WORD16; \
  460. (dataASN)->data.u16 = num; \
  461. } while (0)
  462. /* Setup an ASN data item to set the data in a buffer.
  463. *
  464. * @param [in] dataASN Dynamic ASN data item.
  465. * @param [in] d Buffer containing data to set.
  466. * @param [in] l Length of data in buffer in bytes.
  467. */
  468. #define SetASN_Buffer(dataASN, d, l) \
  469. do { \
  470. (dataASN)->data.buffer.data = d; \
  471. (dataASN)->data.buffer.length = l; \
  472. } while (0)
  473. /* Setup an ASN data item to set the DER encode data in a buffer.
  474. *
  475. * @param [in] dataASN Dynamic ASN data item.
  476. * @param [in] d Buffer containing BER encoded data to set.
  477. * @param [in] l Length of data in buffer in bytes.
  478. */
  479. #define SetASN_ReplaceBuffer(dataASN, d, l) \
  480. do { \
  481. (dataASN)->dataType = ASN_DATA_TYPE_REPLACE_BUFFER; \
  482. (dataASN)->data.buffer.data = d; \
  483. (dataASN)->data.buffer.length = l; \
  484. } while (0)
  485. /* Setup an ASN data item to set an muli-precision number.
  486. *
  487. * @param [in] dataASN Dynamic ASN data item.
  488. * @param [in] num Multi-precision number.
  489. */
  490. #define SetASN_MP(dataASN, num) \
  491. do { \
  492. (dataASN)->dataType = ASN_DATA_TYPE_MP; \
  493. (dataASN)->data.mp = num; \
  494. } while (0)
  495. /* Setup an ASN data item to set an OID based on id and type.
  496. *
  497. * oid and oidType pair are unique.
  498. *
  499. * @param [in] dataASN Dynamic ASN data item.
  500. * @param [in] oid OID identifier.
  501. * @param [in] oidType Type of OID.
  502. */
  503. #define SetASN_OID(dataASN, oid, oidType) \
  504. (dataASN)->data.buffer.data = OidFromId(oid, oidType, \
  505. &(dataASN)->data.buffer.length)
  506. #endif /* WOLFSSL_ASN_TEMPLATE_TYPE_CHECK */
  507. /* Get address at the start of the BER item.
  508. *
  509. * @param [in] dataASN Dynamic ASN data item.
  510. * @param [in] in Input buffer.
  511. * @return Address at start of BER item.
  512. */
  513. #define GetASNItem_Addr(dataASN, in) \
  514. ((in) + (dataASN).offset)
  515. /* Get length of a BER item - including tag and length.
  516. *
  517. * @param [in] dataASN Dynamic ASN data item.
  518. * @param [in] in Input buffer.
  519. * @return Length of a BER item.
  520. */
  521. #define GetASNItem_Length(dataASN, in) \
  522. ((dataASN).length + (word32)((dataASN).data.buffer.data - (in)) - \
  523. (dataASN).offset)
  524. /* Get the index of a BER item's data.
  525. *
  526. * @param [in] dataASN Dynamic ASN data item.
  527. * @param [in] in Input buffer.
  528. * @return Index of a BER item's data.
  529. */
  530. #define GetASNItem_DataIdx(dataASN, in) \
  531. (word32)((dataASN).data.ref.data - (in))
  532. /* Get the end index of a BER item - index of the start of the next item.
  533. *
  534. * @param [in] dataASN Dynamic ASN data item.
  535. * @param [in] in Input buffer.
  536. * @return End index of a BER item.
  537. */
  538. #define GetASNItem_EndIdx(dataASN, in) \
  539. ((word32)((dataASN).data.ref.data - (in)) + \
  540. (dataASN).data.ref.length)
  541. /* For a BIT_STRING, get the unused bits byte.
  542. *
  543. * @param [in] dataASN Dynamic ASN data item.
  544. * @return Unused bits byte in BIT_STRING.
  545. */
  546. #define GetASNItem_UnusedBits(dataASN) \
  547. (*((dataASN).data.ref.data - 1))
  548. /* Set the data items at indices start to end inclusive to not be encoded.
  549. *
  550. * @param [in] dataASN Dynamic ASN data item.
  551. * @param [in] start First item not to be encoded.
  552. * @param [in] end Last item not to be encoded.
  553. */
  554. #define SetASNItem_NoOut(dataASN, start, end) \
  555. do { \
  556. int ii; \
  557. for (ii = (start); ii <= (end); ii++) { \
  558. (dataASN)[ii].noOut = 1; \
  559. } \
  560. } \
  561. while (0)
  562. /* Set the data items below node to not be encoded.
  563. *
  564. * @param [in] dataASN Dynamic ASN data item.
  565. * @param [in] node Node who's children should not be encoded.
  566. * @param [in] dataASNLen Number of items in dataASN.
  567. */
  568. #define SetASNItem_NoOutBelow(dataASN, asn, node, dataASNLen) \
  569. do { \
  570. int ii; \
  571. for (ii = (node) + 1; ii < (int)(dataASNLen); ii++) { \
  572. if ((asn)[ii].depth <= (asn)[node].depth) \
  573. break; \
  574. (dataASN)[ii].noOut = 1; \
  575. } \
  576. } \
  577. while (0)
  578. /* Set the node and all nodes below to not be encoded.
  579. *
  580. * @param [in] dataASN Dynamic ASN data item.
  581. * @param [in] node Node which should not be encoded. Child nodes will
  582. * also not be encoded.
  583. * @param [in] dataASNLen Number of items in dataASN.
  584. */
  585. #define SetASNItem_NoOutNode(dataASN, asn, node, dataASNLen) \
  586. do { \
  587. int ii; \
  588. (dataASN)[node].noOut = 1; \
  589. for (ii = (node) + 1; ii < (int)(dataASNLen); ii++) { \
  590. if ((asn)[ii].depth <= (asn)[node].depth) \
  591. break; \
  592. (dataASN)[ii].noOut = 1; \
  593. } \
  594. } \
  595. while (0)
  596. #endif /* WOLFSSL_ASN_TEMPLATE */
  597. enum DN_Tags {
  598. ASN_DN_NULL = 0x00,
  599. ASN_COMMON_NAME = 0x03, /* CN */
  600. ASN_SUR_NAME = 0x04, /* SN */
  601. ASN_SERIAL_NUMBER = 0x05, /* serialNumber */
  602. ASN_COUNTRY_NAME = 0x06, /* C */
  603. ASN_LOCALITY_NAME = 0x07, /* L */
  604. ASN_STATE_NAME = 0x08, /* ST */
  605. ASN_STREET_ADDR = 0x09, /* street */
  606. ASN_ORG_NAME = 0x0a, /* O */
  607. ASN_ORGUNIT_NAME = 0x0b, /* OU */
  608. ASN_BUS_CAT = 0x0f, /* businessCategory */
  609. ASN_POSTAL_CODE = 0x11, /* postalCode */
  610. ASN_USER_ID = 0x12, /* UserID */
  611. #ifdef WOLFSSL_CERT_NAME_ALL
  612. ASN_NAME = 0x2a, /* name */
  613. ASN_GIVEN_NAME = 0x29, /* GN */
  614. ASN_INITIALS = 0x2b, /* initials */
  615. ASN_DNQUALIFIER = 0x2e, /* dnQualifier */
  616. #endif /* WOLFSSL_CERT_NAME_ALL */
  617. ASN_EMAIL_NAME = 0x98, /* not actual OID (see attrEmailOid) */
  618. ASN_CUSTOM_NAME = 0x99, /* not actual OID (see CertOidField) */
  619. /* pilot attribute types
  620. * OID values of 0.9.2342.19200300.100.1.* */
  621. ASN_FAVOURITE_DRINK = 0x05, /* favouriteDrink */
  622. ASN_DOMAIN_COMPONENT = 0x19 /* DC */
  623. };
  624. /* This is the size of the smallest possible PEM header and footer */
  625. extern const int pem_struct_min_sz;
  626. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  627. typedef struct WOLFSSL_ObjectInfo {
  628. int nid;
  629. int id;
  630. word32 type;
  631. const char* sName;
  632. const char* lName;
  633. } WOLFSSL_ObjectInfo;
  634. extern const size_t wolfssl_object_info_sz;
  635. extern const WOLFSSL_ObjectInfo wolfssl_object_info[];
  636. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) */
  637. /* DN Tag Strings */
  638. #define WOLFSSL_COMMON_NAME "/CN="
  639. #define WOLFSSL_LN_COMMON_NAME "/commonName="
  640. #define WOLFSSL_SUR_NAME "/SN="
  641. #ifdef WOLFSSL_CERT_NAME_ALL
  642. #define WOLFSSL_NAME "/N="
  643. #define WOLFSSL_INITIALS "/initials="
  644. #define WOLFSSL_GIVEN_NAME "/GN="
  645. #define WOLFSSL_DNQUALIFIER "/dnQualifier="
  646. #endif /* WOLFSSL_CERT_NAME_ALL */
  647. #define WOLFSSL_SERIAL_NUMBER "/serialNumber="
  648. #define WOLFSSL_COUNTRY_NAME "/C="
  649. #define WOLFSSL_LN_COUNTRY_NAME "/countryName="
  650. #define WOLFSSL_LOCALITY_NAME "/L="
  651. #define WOLFSSL_LN_LOCALITY_NAME "/localityName="
  652. #define WOLFSSL_STATE_NAME "/ST="
  653. #define WOLFSSL_LN_STATE_NAME "/stateOrProvinceName="
  654. #define WOLFSSL_STREET_ADDR_NAME "/street="
  655. #define WOLFSSL_LN_STREET_ADDR_NAME "/streetAddress="
  656. #define WOLFSSL_POSTAL_NAME "/postalCode="
  657. #define WOLFSSL_ORG_NAME "/O="
  658. #define WOLFSSL_LN_ORG_NAME "/organizationName="
  659. #define WOLFSSL_ORGUNIT_NAME "/OU="
  660. #define WOLFSSL_LN_ORGUNIT_NAME "/organizationalUnitName="
  661. #define WOLFSSL_DOMAIN_COMPONENT "/DC="
  662. #define WOLFSSL_LN_DOMAIN_COMPONENT "/domainComponent="
  663. #define WOLFSSL_BUS_CAT "/businessCategory="
  664. #define WOLFSSL_JOI_C "/jurisdictionC="
  665. #define WOLFSSL_JOI_ST "/jurisdictionST="
  666. #define WOLFSSL_EMAIL_ADDR "/emailAddress="
  667. #define WOLFSSL_USER_ID "/UID="
  668. #define WOLFSSL_DOMAIN_COMPONENT "/DC="
  669. #define WOLFSSL_FAVOURITE_DRINK "/favouriteDrink="
  670. #if defined(WOLFSSL_APACHE_HTTPD)
  671. /* otherName strings */
  672. #define WOLFSSL_SN_MS_UPN "msUPN"
  673. #define WOLFSSL_LN_MS_UPN "Microsoft User Principal Name"
  674. #define WOLFSSL_MS_UPN_SUM 265
  675. #define WOLFSSL_SN_DNS_SRV "id-on-dnsSRV"
  676. #define WOLFSSL_LN_DNS_SRV "SRVName"
  677. /* TLS features extension strings */
  678. #define WOLFSSL_SN_TLS_FEATURE "tlsfeature"
  679. #define WOLFSSL_LN_TLS_FEATURE "TLS Feature"
  680. #define WOLFSSL_TLS_FEATURE_SUM 92
  681. #endif
  682. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  683. /* NIDs */
  684. enum
  685. {
  686. NID_undef = 0,
  687. NID_netscape_cert_type = NID_undef,
  688. NID_des = 66,
  689. NID_des3 = 67,
  690. NID_sha256 = 672,
  691. NID_sha384 = 673,
  692. NID_sha512 = 674,
  693. NID_sha512_224 = 1094,
  694. NID_sha512_256 = 1095,
  695. NID_pkcs7_signed = 22,
  696. NID_pkcs7_enveloped = 23,
  697. NID_pkcs7_signedAndEnveloped = 24,
  698. NID_pkcs9_unstructuredName = 49,
  699. NID_pkcs9_contentType = 50, /* 1.2.840.113549.1.9.3 */
  700. NID_pkcs9_challengePassword = 54,
  701. NID_hw_name_oid = 73,
  702. NID_id_pkix_OCSP_basic = 74,
  703. NID_any_policy = 75,
  704. NID_anyExtendedKeyUsage = 76,
  705. NID_givenName = 99, /* 2.5.4.42 */
  706. NID_initials = 101, /* 2.5.4.43 */
  707. NID_title = 106,
  708. NID_description = 107,
  709. NID_basic_constraints = 133,
  710. NID_key_usage = 129, /* 2.5.29.15 */
  711. NID_ext_key_usage = 151, /* 2.5.29.37 */
  712. NID_subject_key_identifier = 128,
  713. NID_authority_key_identifier = 149,
  714. NID_private_key_usage_period = 130, /* 2.5.29.16 */
  715. NID_subject_alt_name = 131,
  716. NID_issuer_alt_name = 132,
  717. NID_info_access = 69,
  718. NID_sinfo_access = 79, /* id-pe 11 */
  719. NID_name_constraints = 144, /* 2.5.29.30 */
  720. NID_crl_distribution_points = 145, /* 2.5.29.31 */
  721. NID_certificate_policies = 146,
  722. NID_policy_mappings = 147,
  723. NID_policy_constraints = 150,
  724. NID_inhibit_any_policy = 168, /* 2.5.29.54 */
  725. NID_tlsfeature = 1020, /* id-pe 24 */
  726. NID_buildingName = 1494,
  727. NID_dnQualifier = 174, /* 2.5.4.46 */
  728. NID_commonName = 14, /* CN Changed to not conflict
  729. * with PBE_SHA1_DES3 */
  730. NID_name = 173, /* N , OID = 2.5.4.41 */
  731. NID_surname = 0x04, /* SN */
  732. NID_serialNumber = 0x05, /* serialNumber */
  733. NID_countryName = 0x06, /* C */
  734. NID_localityName = 0x07, /* L */
  735. NID_stateOrProvinceName = 0x08, /* ST */
  736. NID_streetAddress = ASN_STREET_ADDR, /* street */
  737. NID_organizationName = 0x0a, /* O */
  738. NID_organizationalUnitName = 0x0b, /* OU */
  739. NID_jurisdictionCountryName = 0xc,
  740. NID_jurisdictionStateOrProvinceName = 0xd,
  741. NID_businessCategory = ASN_BUS_CAT,
  742. NID_domainComponent = ASN_DOMAIN_COMPONENT,
  743. NID_postalCode = ASN_POSTAL_CODE, /* postalCode */
  744. NID_favouriteDrink = 462,
  745. NID_userId = 458,
  746. NID_emailAddress = 0x30, /* emailAddress */
  747. NID_id_on_dnsSRV = 82, /* 1.3.6.1.5.5.7.8.7 */
  748. NID_ms_upn = 265, /* 1.3.6.1.4.1.311.20.2.3 */
  749. NID_X9_62_prime_field = 406 /* 1.2.840.10045.1.1 */
  750. };
  751. #endif /* OPENSSL_EXTRA */
  752. enum ECC_TYPES
  753. {
  754. ECC_PREFIX_0 = 160,
  755. ECC_PREFIX_1 = 161
  756. };
  757. #ifdef WOLFSSL_CERT_PIV
  758. enum PIV_Tags {
  759. ASN_PIV_CERT = 0x0A,
  760. ASN_PIV_NONCE = 0x0B,
  761. ASN_PIV_SIGNED_NONCE = 0x0C,
  762. ASN_PIV_TAG_CERT = 0x70,
  763. ASN_PIV_TAG_CERT_INFO = 0x71,
  764. ASN_PIV_TAG_MSCUID = 0x72,
  765. ASN_PIV_TAG_ERR_DET = 0xFE,
  766. /* certificate info masks */
  767. ASN_PIV_CERT_INFO_COMPRESSED = 0x03,
  768. ASN_PIV_CERT_INFO_ISX509 = 0x04,
  769. /* GZIP is 0x01 */
  770. ASN_PIV_CERT_INFO_GZIP = 0x01,
  771. };
  772. #endif /* WOLFSSL_CERT_PIV */
  773. #define ASN_JOI_PREFIX_SZ 10
  774. #define ASN_JOI_PREFIX "\x2b\x06\x01\x04\x01\x82\x37\x3c\x02\x01"
  775. #define ASN_JOI_C 0x3
  776. #define ASN_JOI_ST 0x2
  777. #ifndef WC_ASN_NAME_MAX
  778. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  779. defined(WOLFSSL_CERT_EXT)
  780. #define WC_ASN_NAME_MAX 330
  781. #else
  782. #define WC_ASN_NAME_MAX 256
  783. #endif
  784. #endif
  785. enum Misc_ASN {
  786. MAX_SALT_SIZE = 64, /* MAX PKCS Salt length */
  787. MAX_IV_SIZE = 64, /* MAX PKCS Iv length */
  788. ASN_BOOL_SIZE = 2, /* including type */
  789. ASN_ECC_HEADER_SZ = 2, /* String type + 1 byte len */
  790. ASN_ECC_CONTEXT_SZ = 2, /* Content specific type + 1 byte len */
  791. #ifdef NO_SHA
  792. KEYID_SIZE = WC_SHA256_DIGEST_SIZE,
  793. #else
  794. KEYID_SIZE = WC_SHA_DIGEST_SIZE,
  795. #endif
  796. RSA_INTS = 8, /* RSA ints in private key */
  797. DSA_PARAM_INTS = 3, /* DSA paramater ints */
  798. RSA_PUB_INTS = 2, /* RSA ints in public key */
  799. DSA_PUB_INTS = 4, /* DSA ints in public key */
  800. DSA_INTS = 5, /* DSA ints in private key */
  801. MIN_DATE_SIZE = 12,
  802. MAX_DATE_SIZE = 32,
  803. ASN_GEN_TIME_SZ = 15, /* 7 numbers * 2 + Zulu tag */
  804. #ifndef NO_RSA
  805. #ifdef WOLFSSL_HAPROXY
  806. MAX_ENCODED_SIG_SZ = 1024, /* Supports 8192 bit keys */
  807. #else
  808. MAX_ENCODED_SIG_SZ = 512, /* Supports 4096 bit keys */
  809. #endif
  810. #elif defined(HAVE_ECC)
  811. MAX_ENCODED_SIG_SZ = 140,
  812. #elif defined(HAVE_CURVE448)
  813. MAX_ENCODED_SIG_SZ = 114,
  814. #else
  815. MAX_ENCODED_SIG_SZ = 64,
  816. #endif
  817. MAX_SIG_SZ = 256,
  818. MAX_ALGO_SZ = 20,
  819. MAX_SHORT_SZ = 6, /* asn int + byte len + 4 byte length */
  820. MAX_LENGTH_SZ = 4, /* Max length size for DER encoding */
  821. MAX_SEQ_SZ = 5, /* enum(seq | con) + length(4) */
  822. MAX_SET_SZ = 5, /* enum(set | con) + length(4) */
  823. MAX_OCTET_STR_SZ = 5, /* enum(set | con) + length(4) */
  824. MAX_EXP_SZ = 5, /* enum(contextspec|con|exp) + length(4) */
  825. MAX_PRSTR_SZ = 5, /* enum(prstr) + length(4) */
  826. MAX_VERSION_SZ = 5, /* enum + id + version(byte) + (header(2))*/
  827. MAX_ENCODED_DIG_ASN_SZ= 9, /* enum(bit or octet) + length(4) */
  828. MAX_ENCODED_DIG_SZ = 64 + MAX_ENCODED_DIG_ASN_SZ, /* asn header + sha512 */
  829. MAX_RSA_INT_SZ = 517, /* RSA raw sz 4096 for bits + tag + len(4) */
  830. MAX_DSA_INT_SZ = 389, /* DSA raw sz 3072 for bits + tag + len(4) */
  831. MAX_DSA_PUBKEY_SZ = (DSA_PUB_INTS * MAX_DSA_INT_SZ) + (2 * MAX_SEQ_SZ) +
  832. 2 + MAX_LENGTH_SZ, /* Maximum size of a DSA public
  833. key taken from wc_SetDsaPublicKey. */
  834. MAX_DSA_PRIVKEY_SZ = (DSA_INTS * MAX_DSA_INT_SZ) + MAX_SEQ_SZ +
  835. MAX_VERSION_SZ, /* Maximum size of a DSA Private
  836. key taken from DsaKeyIntsToDer. */
  837. MAX_RSA_E_SZ = 16, /* Max RSA public e size */
  838. MAX_CA_SZ = 32, /* Max encoded CA basic constraint length */
  839. MAX_SN_SZ = 35, /* Max encoded serial number (INT) length */
  840. MAX_DER_DIGEST_SZ = MAX_ENCODED_DIG_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ,
  841. /* Maximum DER digest size */
  842. MAX_DER_DIGEST_ASN_SZ = MAX_ENCODED_DIG_ASN_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ,
  843. /* Maximum DER digest ASN header size */
  844. /* Max X509 header length indicates the max length + 2 ('\n', '\0') */
  845. MAX_X509_HEADER_SZ = (37 + 2), /* Maximum PEM Header/Footer Size */
  846. #ifdef WOLFSSL_CERT_GEN
  847. #ifdef WOLFSSL_CERT_REQ
  848. /* Max encoded cert req attributes length */
  849. MAX_ATTRIB_SZ = MAX_SEQ_SZ * 4 + (11 + MAX_SEQ_SZ) * 3 +
  850. MAX_PRSTR_SZ * 2 + CTC_NAME_SIZE * 2,
  851. /* 11 is the OID size */
  852. #endif
  853. #if defined(WOLFSSL_ALT_NAMES) || defined(WOLFSSL_CERT_EXT)
  854. MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + CTC_MAX_ALT_SIZE,
  855. #else
  856. MAX_EXTENSIONS_SZ = 1 + MAX_LENGTH_SZ + MAX_CA_SZ,
  857. #endif
  858. /* Max total extensions, id + len + others */
  859. #endif
  860. #if defined(WOLFSSL_CERT_EXT) || defined(OPENSSL_EXTRA) || \
  861. defined(HAVE_PKCS7) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  862. defined(HAVE_OID_DECODING) || defined(HAVE_OID_ENCODING)
  863. MAX_OID_SZ = 32, /* Max DER length of OID*/
  864. MAX_OID_STRING_SZ = 64, /* Max string length representation of OID*/
  865. #endif
  866. #ifdef WOLFSSL_CERT_EXT
  867. MAX_KID_SZ = 45, /* Max encoded KID length (SHA-256 case) */
  868. MAX_KEYUSAGE_SZ = 18, /* Max encoded Key Usage length */
  869. MAX_EXTKEYUSAGE_SZ = 12 + (6 * (8 + 2)) +
  870. CTC_MAX_EKU_OID_SZ, /* Max encoded ExtKeyUsage
  871. (SEQ/LEN + OBJID + OCTSTR/LEN + SEQ +
  872. (6 * (SEQ + OID))) */
  873. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  874. MAX_NSCERTTYPE_SZ = MAX_SEQ_SZ + 17, /* SEQ + OID + OCTET STR +
  875. * NS BIT STR */
  876. #endif
  877. MAX_CERTPOL_NB = CTC_MAX_CERTPOL_NB,/* Max number of Cert Policy */
  878. MAX_CERTPOL_SZ = CTC_MAX_CERTPOL_SZ,
  879. #endif
  880. MAX_AIA_SZ = 2, /* Max Authority Info Access extension size*/
  881. OCSP_NONCE_EXT_SZ = 35, /* OCSP Nonce Extension size */
  882. MAX_OCSP_EXT_SZ = 58, /* Max OCSP Extension length */
  883. MAX_OCSP_NONCE_SZ = 16, /* OCSP Nonce size */
  884. MAX_PUBLIC_KEY_SZ = MAX_DSA_PUBKEY_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ * 2,
  885. #ifdef WOLFSSL_ENCRYPTED_KEYS
  886. HEADER_ENCRYPTED_KEY_SIZE = 88,/* Extra header size for encrypted key */
  887. #else
  888. HEADER_ENCRYPTED_KEY_SIZE = 0,
  889. #endif
  890. TRAILING_ZERO = 1, /* Used for size of zero pad */
  891. ASN_TAG_SZ = 1, /* single byte ASN.1 tag */
  892. MIN_VERSION_SZ = 3, /* Min bytes needed for GetMyVersion */
  893. MAX_X509_VERSION = 3, /* Max X509 version allowed */
  894. MIN_X509_VERSION = 0, /* Min X509 version allowed */
  895. WOLFSSL_X509_V1 = 0,
  896. WOLFSSL_X509_V2 = 1,
  897. WOLFSSL_X509_V3 = 2,
  898. #if defined(OPENSSL_ALL) || defined(WOLFSSL_MYSQL_COMPATIBLE) || \
  899. defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  900. defined(OPENSSL_EXTRA) || defined(HAVE_PKCS7)
  901. MAX_TIME_STRING_SZ = 25, /* Max length of formatted time string */
  902. #endif
  903. PKCS5_SALT_SZ = 8,
  904. PEM_LINE_SZ = 64, /* Length of Base64 encoded line, not including new line */
  905. PEM_LINE_LEN = PEM_LINE_SZ + 12, /* PEM line max + fudge */
  906. COUNTRY_CODE_LEN = 2, /* RFC 3739 */
  907. };
  908. #ifndef WC_MAX_NAME_ENTRIES
  909. /* entries added to x509 name struct */
  910. #ifdef OPENSSL_EXTRA
  911. #define WC_MAX_NAME_ENTRIES 16
  912. #else
  913. #define WC_MAX_NAME_ENTRIES 14
  914. #endif
  915. #endif
  916. #define MAX_NAME_ENTRIES WC_MAX_NAME_ENTRIES
  917. enum Oid_Types {
  918. oidHashType = 0,
  919. oidSigType = 1,
  920. oidKeyType = 2,
  921. oidCurveType = 3,
  922. oidBlkType = 4,
  923. oidOcspType = 5,
  924. oidCertExtType = 6,
  925. oidCertAuthInfoType = 7,
  926. oidCertPolicyType = 8,
  927. oidCertAltNameType = 9,
  928. oidCertKeyUseType = 10,
  929. oidKdfType = 11,
  930. oidKeyWrapType = 12,
  931. oidCmsKeyAgreeType = 13,
  932. oidPBEType = 14,
  933. oidHmacType = 15,
  934. oidCompressType = 16,
  935. oidCertNameType = 17,
  936. oidTlsExtType = 18,
  937. oidCrlExtType = 19,
  938. oidCsrAttrType = 20,
  939. #ifdef WOLFSSL_SUBJ_DIR_ATTR
  940. oidSubjDirAttrType = 21,
  941. #endif
  942. oidIgnoreType
  943. };
  944. enum Hash_Sum {
  945. MD2h = 646,
  946. MD5h = 649,
  947. SHAh = 88,
  948. SHA224h = 417,
  949. SHA256h = 414,
  950. SHA384h = 415,
  951. SHA512h = 416,
  952. SHA512_224h = 418,
  953. SHA512_256h = 419,
  954. SHA3_224h = 420,
  955. SHA3_256h = 421,
  956. SHA3_384h = 422,
  957. SHA3_512h = 423,
  958. SHAKE128h = 424,
  959. SHAKE256h = 425
  960. };
  961. #if !defined(NO_DES3) || !defined(NO_AES)
  962. enum Block_Sum {
  963. #ifdef WOLFSSL_AES_128
  964. AES128CBCb = 414,
  965. AES128GCMb = 418,
  966. AES128CCMb = 419,
  967. #endif
  968. #ifdef WOLFSSL_AES_192
  969. AES192CBCb = 434,
  970. AES192GCMb = 438,
  971. AES192CCMb = 439,
  972. #endif
  973. #ifdef WOLFSSL_AES_256
  974. AES256CBCb = 454,
  975. AES256GCMb = 458,
  976. AES256CCMb = 459,
  977. #endif
  978. #ifndef NO_DES3
  979. DESb = 69,
  980. DES3b = 652
  981. #endif
  982. };
  983. #endif /* !NO_DES3 || !NO_AES */
  984. enum Key_Sum {
  985. DSAk = 515,
  986. RSAk = 645,
  987. RSAPSSk = 654,
  988. ECDSAk = 518,
  989. ED25519k = 256, /* 1.3.101.112 */
  990. X25519k = 254, /* 1.3.101.110 */
  991. ED448k = 257, /* 1.3.101.113 */
  992. X448k = 255, /* 1.3.101.111 */
  993. DHk = 647, /* dhKeyAgreement OID: 1.2.840.113549.1.3.1 */
  994. FALCON_LEVEL1k = 268, /* 1.3.9999.3.1 */
  995. FALCON_LEVEL5k = 271, /* 1.3.9999.3.4 */
  996. DILITHIUM_LEVEL2k = 213, /* 1.3.6.1.4.1.2.267.7.4.4 */
  997. DILITHIUM_LEVEL3k = 216, /* 1.3.6.1.4.1.2.267.7.6.5 */
  998. DILITHIUM_LEVEL5k = 220, /* 1.3.6.1.4.1.2.267.7.8.7 */
  999. DILITHIUM_AES_LEVEL2k = 217,/* 1.3.6.1.4.1.2.267.11.4.4 */
  1000. DILITHIUM_AES_LEVEL3k = 221,/* 1.3.6.1.4.1.2.267.11.6.5 + 1 (See GetOID() in asn.c) */
  1001. DILITHIUM_AES_LEVEL5k = 224,/* 1.3.6.1.4.1.2.267.11.8.7 */
  1002. SPHINCS_FAST_LEVEL1k = 281, /* 1 3 9999 6 7 4 */
  1003. SPHINCS_FAST_LEVEL3k = 283, /* 1 3 9999 6 8 3 + 2 (See GetOID() in asn.c) */
  1004. SPHINCS_FAST_LEVEL5k = 282, /* 1 3 9999 6 9 3 */
  1005. SPHINCS_SMALL_LEVEL1k = 287, /* 1 3 9999 6 7 10 */
  1006. SPHINCS_SMALL_LEVEL3k = 285, /* 1 3 9999 6 8 7 */
  1007. SPHINCS_SMALL_LEVEL5k = 286, /* 1 3 9999 6 9 7 */
  1008. };
  1009. #if !defined(NO_AES) || defined(HAVE_PKCS7)
  1010. enum KeyWrap_Sum {
  1011. #ifdef WOLFSSL_AES_128
  1012. AES128_WRAP = 417,
  1013. #endif
  1014. #ifdef WOLFSSL_AES_192
  1015. AES192_WRAP = 437,
  1016. #endif
  1017. #ifdef WOLFSSL_AES_256
  1018. AES256_WRAP = 457,
  1019. #endif
  1020. #ifdef HAVE_PKCS7
  1021. PWRI_KEK_WRAP = 680 /*id-alg-PWRI-KEK, 1.2.840.113549.1.9.16.3.9 */
  1022. #endif
  1023. };
  1024. #endif /* !NO_AES || PKCS7 */
  1025. enum Key_Agree {
  1026. dhSinglePass_stdDH_sha1kdf_scheme = 464,
  1027. dhSinglePass_stdDH_sha224kdf_scheme = 188,
  1028. dhSinglePass_stdDH_sha256kdf_scheme = 189,
  1029. dhSinglePass_stdDH_sha384kdf_scheme = 190,
  1030. dhSinglePass_stdDH_sha512kdf_scheme = 191,
  1031. };
  1032. enum KDF_Sum {
  1033. PBKDF2_OID = 660,
  1034. MGF1_OID = 652,
  1035. };
  1036. enum HMAC_Sum {
  1037. HMAC_SHA224_OID = 652,
  1038. HMAC_SHA256_OID = 653,
  1039. HMAC_SHA384_OID = 654,
  1040. HMAC_SHA512_OID = 655,
  1041. HMAC_SHA3_224_OID = 426,
  1042. HMAC_SHA3_256_OID = 427,
  1043. HMAC_SHA3_384_OID = 428,
  1044. HMAC_SHA3_512_OID = 429
  1045. };
  1046. enum Extensions_Sum {
  1047. BASIC_CA_OID = 133, /* 2.5.29.19 */
  1048. ALT_NAMES_OID = 131, /* 2.5.29.17 */
  1049. CRL_DIST_OID = 145, /* 2.5.29.31 */
  1050. AUTH_INFO_OID = 69, /* 1.3.6.1.5.5.7.1.1 */
  1051. AUTH_KEY_OID = 149, /* 2.5.29.35 */
  1052. SUBJ_KEY_OID = 128, /* 2.5.29.14 */
  1053. CERT_POLICY_OID = 146, /* 2.5.29.32 */
  1054. CRL_NUMBER_OID = 134, /* 2.5.29.20 */
  1055. KEY_USAGE_OID = 129, /* 2.5.29.15 */
  1056. INHIBIT_ANY_OID = 168, /* 2.5.29.54 */
  1057. EXT_KEY_USAGE_OID = 151, /* 2.5.29.37 */
  1058. NAME_CONS_OID = 144, /* 2.5.29.30 */
  1059. PRIV_KEY_USAGE_PERIOD_OID = 130, /* 2.5.29.16 */
  1060. SUBJ_INFO_ACC_OID = 79, /* 1.3.6.1.5.5.7.1.11 */
  1061. POLICY_MAP_OID = 147, /* 2.5.29.33 */
  1062. POLICY_CONST_OID = 150, /* 2.5.29.36 */
  1063. ISSUE_ALT_NAMES_OID = 132, /* 2.5.29.18 */
  1064. TLS_FEATURE_OID = 92, /* 1.3.6.1.5.5.7.1.24 */
  1065. NETSCAPE_CT_OID = 753, /* 2.16.840.1.113730.1.1 */
  1066. OCSP_NOCHECK_OID = 121, /* 1.3.6.1.5.5.7.48.1.5
  1067. id-pkix-ocsp-nocheck */
  1068. SUBJ_DIR_ATTR_OID = 123, /* 2.5.29.9 */
  1069. AKEY_PACKAGE_OID = 1048, /* 2.16.840.1.101.2.1.2.78.5
  1070. RFC 5958 - Asymmetric Key Packages */
  1071. FASCN_OID = 419, /* 2.16.840.1.101.3.6.6 Federal PKI Policy FASC-N */
  1072. UPN_OID = 265 /* 1.3.6.1.4.1.311.20.2.3 UPN */
  1073. };
  1074. enum CertificatePolicy_Sum {
  1075. CP_ANY_OID = 146, /* id-ce 32 0 */
  1076. #ifdef WOLFSSL_FPKI
  1077. CP_FPKI_COMMON_AUTH_OID = 426, /* 2.16.840.1.101.3.2.1.3.13 */
  1078. CP_FPKI_PIV_AUTH_OID = 453, /* 2.16.840.1.101.3.2.1.3.40 */
  1079. CP_FPKI_PIV_AUTH_HW_OID = 454, /* 2.16.840.1.101.3.2.1.3.41 */
  1080. CP_FPKI_PIVI_AUTH_OID = 458 /* 2.16.840.1.101.3.2.1.3.45 */
  1081. #endif /* WOLFSSL_FPKI */
  1082. };
  1083. enum SepHardwareName_Sum {
  1084. HW_NAME_OID = 79 /* 1.3.6.1.5.5.7.8.4 from RFC 4108*/
  1085. };
  1086. enum AuthInfo_Sum {
  1087. AIA_OCSP_OID = 116, /* 1.3.6.1.5.5.7.48.1, id-ad-ocsp */
  1088. AIA_CA_ISSUER_OID = 117, /* 1.3.6.1.5.5.7.48.2, id-ad-caIssuers */
  1089. #ifdef WOLFSSL_SUBJ_INFO_ACC
  1090. AIA_CA_REPO_OID = 120 /* 1.3.6.1.5.5.7.48.5, id-ad-caRepository */
  1091. #endif /* WOLFSSL_SUBJ_INFO_ACC */
  1092. };
  1093. #define ID_PKIX(num) (67+(num)) /* 1.3.6.1.5.5.7.num, id-pkix num */
  1094. #define ID_KP(num) (ID_PKIX(3)+(num)) /* 1.3.6.1.5.5.7.3.num, id-kp num */
  1095. enum ExtKeyUsage_Sum { /* From RFC 5280 */
  1096. EKU_ANY_OID = 151, /* 2.5.29.37.0, anyExtendedKeyUsage */
  1097. EKU_SERVER_AUTH_OID = 71, /* 1.3.6.1.5.5.7.3.1, id-kp-serverAuth */
  1098. EKU_CLIENT_AUTH_OID = 72, /* 1.3.6.1.5.5.7.3.2, id-kp-clientAuth */
  1099. EKU_CODESIGNING_OID = 73, /* 1.3.6.1.5.5.7.3.3, id-kp-codeSigning */
  1100. EKU_EMAILPROTECT_OID = 74, /* 1.3.6.1.5.5.7.3.4, id-kp-emailProtection */
  1101. EKU_TIMESTAMP_OID = 78, /* 1.3.6.1.5.5.7.3.8, id-kp-timeStamping */
  1102. EKU_OCSP_SIGN_OID = 79, /* 1.3.6.1.5.5.7.3.9, id-kp-OCSPSigning */
  1103. /* From RFC 6187: X.509v3 Certificates for Secure Shell Authenticaiton */
  1104. EKU_SSH_CLIENT_AUTH_OID = ID_KP(21), /* id-kp-secureShellClient */
  1105. EKU_SSH_MSCL_OID = 264,
  1106. /* 1.3.6.1.4.1.311.20.2.2, MS Smart Card Logon */
  1107. EKU_SSH_KP_CLIENT_AUTH_OID = 64
  1108. /* 1.3.6.1.5.2.3.4, id-pkinit-KPClientAuth*/
  1109. };
  1110. #ifdef WOLFSSL_SUBJ_DIR_ATTR
  1111. #define ID_PDA(num) (ID_PKIX(9)+(num)) /* 1.3.6.1.5.5.7.9.num, id-pda num */
  1112. enum SubjDirAttr_Sum { /* From RFC 3739, section 3.3.2 */
  1113. SDA_DOB_OID = ID_PDA(1), /* id-pda-dateOfBirth */
  1114. SDA_POB_OID = ID_PDA(2), /* id-pda-placeOfBirth */
  1115. SDA_GENDER_OID = ID_PDA(3), /* id-pda-gender */
  1116. SDA_COC_OID = ID_PDA(4), /* id-pda-countryOfCitizenship */
  1117. SDA_COR_OID = ID_PDA(5) /* id-pda-countryOfResidence */
  1118. };
  1119. #endif /* WOLFSSL_SUBJ_DIR_ATTR */
  1120. #ifdef HAVE_LIBZ
  1121. enum CompressAlg_Sum {
  1122. ZLIBc = 679 /* 1.2.840.113549.1.9.16.3.8, id-alg-zlibCompress */
  1123. };
  1124. #endif
  1125. enum VerifyType {
  1126. NO_VERIFY = 0,
  1127. VERIFY = 1,
  1128. VERIFY_CRL = 2,
  1129. VERIFY_OCSP = 3,
  1130. VERIFY_NAME = 4,
  1131. VERIFY_SKIP_DATE = 5,
  1132. VERIFY_OCSP_CERT = 6,
  1133. };
  1134. #ifdef WOLFSSL_CERT_EXT
  1135. enum KeyIdType {
  1136. SKID_TYPE = 0,
  1137. AKID_TYPE = 1
  1138. };
  1139. #endif
  1140. #ifdef WOLFSSL_CERT_REQ
  1141. enum CsrAttrType {
  1142. UNSTRUCTURED_NAME_OID = 654,
  1143. PKCS9_CONTENT_TYPE_OID = 655,
  1144. CHALLENGE_PASSWORD_OID = 659,
  1145. SERIAL_NUMBER_OID = 94,
  1146. EXTENSION_REQUEST_OID = 666,
  1147. USER_ID_OID = 865,
  1148. DNQUALIFIER_OID = 135,
  1149. INITIALS_OID = 132,
  1150. SURNAME_OID = 93,
  1151. NAME_OID = 130,
  1152. GIVEN_NAME_OID = 131,
  1153. };
  1154. #endif
  1155. /* Key usage extension bits (based on RFC 5280) */
  1156. #define KEYUSE_DIGITAL_SIG 0x0080
  1157. #define KEYUSE_CONTENT_COMMIT 0x0040
  1158. #define KEYUSE_KEY_ENCIPHER 0x0020
  1159. #define KEYUSE_DATA_ENCIPHER 0x0010
  1160. #define KEYUSE_KEY_AGREE 0x0008
  1161. #define KEYUSE_KEY_CERT_SIGN 0x0004
  1162. #define KEYUSE_CRL_SIGN 0x0002
  1163. #define KEYUSE_ENCIPHER_ONLY 0x0001
  1164. #define KEYUSE_DECIPHER_ONLY 0x8000
  1165. /* Extended Key Usage bits (internal mapping only) */
  1166. #define EXTKEYUSE_USER 0x80
  1167. #define EXTKEYUSE_OCSP_SIGN 0x40
  1168. #define EXTKEYUSE_TIMESTAMP 0x20
  1169. #define EXTKEYUSE_EMAILPROT 0x10
  1170. #define EXTKEYUSE_CODESIGN 0x08
  1171. #define EXTKEYUSE_CLIENT_AUTH 0x04
  1172. #define EXTKEYUSE_SERVER_AUTH 0x02
  1173. #define EXTKEYUSE_ANY 0x01
  1174. #ifdef WOLFSSL_WOLFSSH
  1175. #define EXTKEYUSE_SSH_CLIENT_AUTH 0x01
  1176. #define EXTKEYUSE_SSH_MSCL 0x02
  1177. #define EXTKEYUSE_SSH_KP_CLIENT_AUTH 0x04
  1178. #endif /* WOLFSSL_WOLFSSH */
  1179. #define WC_NS_SSL_CLIENT 0x80
  1180. #define WC_NS_SSL_SERVER 0x40
  1181. #define WC_NS_SMIME 0x20
  1182. #define WC_NS_OBJSIGN 0x10
  1183. #define WC_NS_SSL_CA 0x04
  1184. #define WC_NS_SMIME_CA 0x02
  1185. #define WC_NS_OBJSIGN_CA 0x01
  1186. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  1187. defined(WOLFSSL_WPAS_SMALL) || defined(WOLFSSL_IP_ALT_NAME)
  1188. #ifndef WOLFSSL_MAX_IPSTR
  1189. #define WOLFSSL_MAX_IPSTR 46 /* max ip size IPv4 mapped IPv6 */
  1190. #endif
  1191. #define WOLFSSL_IP4_ADDR_LEN 4
  1192. #define WOLFSSL_IP6_ADDR_LEN 16
  1193. #endif /* OPENSSL_ALL || WOLFSSL_IP_ALT_NAME */
  1194. typedef struct DNS_entry DNS_entry;
  1195. struct DNS_entry {
  1196. DNS_entry* next; /* next on DNS list */
  1197. int type; /* i.e. ASN_DNS_TYPE */
  1198. int len; /* actual DNS len */
  1199. char* name; /* actual DNS name */
  1200. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  1201. char* ipString; /* human readable form of IP address */
  1202. #endif
  1203. #ifdef WOLFSSL_FPKI
  1204. int oidSum; /* provide oid sum for verification */
  1205. #endif
  1206. };
  1207. #ifdef WOLFSSL_FPKI
  1208. /* RFC4122 i.e urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6 */
  1209. #define DEFAULT_UUID_SZ 45
  1210. #endif
  1211. typedef struct Base_entry Base_entry;
  1212. struct Base_entry {
  1213. Base_entry* next; /* next on name base list */
  1214. char* name; /* actual name base */
  1215. int nameSz; /* name length */
  1216. byte type; /* Name base type (DNS or RFC822) */
  1217. };
  1218. enum SignatureState {
  1219. SIG_STATE_BEGIN,
  1220. SIG_STATE_HASH,
  1221. SIG_STATE_KEY,
  1222. SIG_STATE_DO,
  1223. SIG_STATE_CHECK,
  1224. };
  1225. #ifdef HAVE_PK_CALLBACKS
  1226. #ifdef HAVE_ECC
  1227. typedef int (*wc_CallbackEccVerify)(
  1228. const unsigned char* sig, unsigned int sigSz,
  1229. const unsigned char* hash, unsigned int hashSz,
  1230. const unsigned char* keyDer, unsigned int keySz,
  1231. int* result, void* ctx);
  1232. #endif
  1233. #ifndef NO_RSA
  1234. typedef int (*wc_CallbackRsaVerify)(
  1235. unsigned char* sig, unsigned int sigSz,
  1236. unsigned char** out,
  1237. const unsigned char* keyDer, unsigned int keySz,
  1238. void* ctx);
  1239. #endif
  1240. #endif /* HAVE_PK_CALLBACKS */
  1241. #if defined(WOLFSSL_RENESAS_TSIP_TLS) || defined(WOLFSSL_RENESAS_SCEPROTECT) ||\
  1242. defined(HAVE_PK_CALLBACKS)
  1243. typedef struct tagCertAttribute {
  1244. byte verifyByTSIP_SCE;
  1245. word32 certBegin;
  1246. word32 pubkey_n_start;
  1247. word32 pubkey_n_len;
  1248. word32 pubkey_e_start;
  1249. word32 pubkey_e_len;
  1250. int curve_id;
  1251. const byte* cert;
  1252. word32 certSz;
  1253. const byte* keyIndex;
  1254. } CertAttribute;
  1255. #endif
  1256. struct SignatureCtx {
  1257. void* heap;
  1258. byte* digest;
  1259. #ifndef NO_RSA
  1260. byte* out;
  1261. #endif
  1262. #if !(defined(NO_RSA) && defined(NO_DSA))
  1263. byte* sigCpy;
  1264. #endif
  1265. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
  1266. !defined(NO_DSA)
  1267. int verify;
  1268. #endif
  1269. union {
  1270. #ifndef NO_RSA
  1271. struct RsaKey* rsa;
  1272. #endif
  1273. #ifndef NO_DSA
  1274. struct DsaKey* dsa;
  1275. #endif
  1276. #ifdef HAVE_ECC
  1277. struct ecc_key* ecc;
  1278. #endif
  1279. #ifdef HAVE_ED25519
  1280. struct ed25519_key* ed25519;
  1281. #endif
  1282. #ifdef HAVE_ED448
  1283. struct ed448_key* ed448;
  1284. #endif
  1285. #ifdef HAVE_PQC
  1286. struct falcon_key* falcon;
  1287. struct dilithium_key* dilithium;
  1288. struct sphincs_key* sphincs;
  1289. #endif
  1290. void* ptr;
  1291. } key;
  1292. int devId;
  1293. int state;
  1294. int typeH;
  1295. int digestSz;
  1296. word32 keyOID;
  1297. #ifdef WOLFSSL_ASYNC_CRYPT
  1298. WC_ASYNC_DEV* asyncDev;
  1299. void* asyncCtx;
  1300. #endif
  1301. #ifdef HAVE_PK_CALLBACKS
  1302. #ifdef HAVE_ECC
  1303. wc_CallbackEccVerify pkCbEcc;
  1304. void* pkCtxEcc;
  1305. #endif
  1306. #ifndef NO_RSA
  1307. wc_CallbackRsaVerify pkCbRsa;
  1308. void* pkCtxRsa;
  1309. #endif
  1310. #endif /* HAVE_PK_CALLBACKS */
  1311. #ifndef NO_RSA
  1312. #if defined(WOLFSSL_RENESAS_TSIP_TLS) || defined(WOLFSSL_RENESAS_SCEPROTECT) ||\
  1313. defined(HAVE_PK_CALLBACKS)
  1314. CertAttribute CertAtt;
  1315. #endif
  1316. #ifdef WC_RSA_PSS
  1317. enum wc_HashType hash;
  1318. int mgf;
  1319. int saltLen;
  1320. #endif
  1321. #endif
  1322. };
  1323. enum CertSignState {
  1324. CERTSIGN_STATE_BEGIN,
  1325. CERTSIGN_STATE_DIGEST,
  1326. CERTSIGN_STATE_ENCODE,
  1327. CERTSIGN_STATE_DO,
  1328. };
  1329. struct CertSignCtx {
  1330. byte* sig;
  1331. byte* digest;
  1332. #ifndef NO_RSA
  1333. byte* encSig;
  1334. int encSigSz;
  1335. #endif
  1336. int state; /* enum CertSignState */
  1337. };
  1338. #define DOMAIN_COMPONENT_MAX 10
  1339. struct DecodedName {
  1340. char* fullName;
  1341. int fullNameLen;
  1342. int entryCount;
  1343. int cnIdx;
  1344. int cnLen;
  1345. int cnNid;
  1346. int snIdx;
  1347. int snLen;
  1348. int snNid;
  1349. int cIdx;
  1350. int cLen;
  1351. int cNid;
  1352. int lIdx;
  1353. int lLen;
  1354. int lNid;
  1355. int stIdx;
  1356. int stLen;
  1357. int stNid;
  1358. int oIdx;
  1359. int oLen;
  1360. int oNid;
  1361. int ouIdx;
  1362. int ouLen;
  1363. #ifdef WOLFSSL_CERT_EXT
  1364. int bcIdx;
  1365. int bcLen;
  1366. int jcIdx;
  1367. int jcLen;
  1368. int jsIdx;
  1369. int jsLen;
  1370. #endif
  1371. int ouNid;
  1372. int emailIdx;
  1373. int emailLen;
  1374. int emailNid;
  1375. int uidIdx;
  1376. int uidLen;
  1377. int uidNid;
  1378. int serialIdx;
  1379. int serialLen;
  1380. int serialNid;
  1381. int dcIdx[DOMAIN_COMPONENT_MAX];
  1382. int dcLen[DOMAIN_COMPONENT_MAX];
  1383. int dcNum;
  1384. int dcMode;
  1385. };
  1386. /* ASN Encoded Name field */
  1387. typedef struct EncodedName {
  1388. int nameLen; /* actual string value length */
  1389. int totalLen; /* total encoded length */
  1390. int type; /* type of name */
  1391. int used; /* are we actually using this one */
  1392. byte encoded[CTC_NAME_SIZE * 2]; /* encoding */
  1393. } EncodedName;
  1394. #ifndef WOLFSSL_MAX_PATH_LEN
  1395. /* RFC 5280 Section 6.1.2. "Initialization" - item (k) defines
  1396. * (k) max_path_length: this integer is initialized to "n", is
  1397. * decremented for each non-self-issued certificate in the path,
  1398. * and may be reduced to the value in the path length constraint
  1399. * field within the basic constraints extension of a CA
  1400. * certificate.
  1401. *
  1402. * wolfSSL has arbitrarily selected the value 127 for "n" in the above
  1403. * description. Users can modify the maximum path length by setting
  1404. * WOLFSSL_MAX_PATH_LEN to a preferred value at build time
  1405. */
  1406. #define WOLFSSL_MAX_PATH_LEN 127
  1407. #endif
  1408. typedef struct DecodedName DecodedName;
  1409. typedef struct DecodedCert DecodedCert;
  1410. typedef struct Signer Signer;
  1411. #ifdef WOLFSSL_TRUST_PEER_CERT
  1412. typedef struct TrustedPeerCert TrustedPeerCert;
  1413. #endif /* WOLFSSL_TRUST_PEER_CERT */
  1414. typedef struct SignatureCtx SignatureCtx;
  1415. typedef struct CertSignCtx CertSignCtx;
  1416. #if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
  1417. && defined(HAVE_OID_DECODING)
  1418. typedef int (*wc_UnknownExtCallback)(const word16* oid, word32 oidSz, int crit,
  1419. const unsigned char* der, word32 derSz);
  1420. #endif
  1421. struct DecodedCert {
  1422. const byte* publicKey;
  1423. word32 pubKeySize;
  1424. int pubKeyStored;
  1425. word32 certBegin; /* offset to start of cert */
  1426. word32 sigIndex; /* offset to start of signature */
  1427. word32 sigLength; /* length of signature */
  1428. word32 signatureOID; /* sum of algorithm object id */
  1429. word32 keyOID; /* sum of key algo object id */
  1430. #ifdef WC_RSA_PSS
  1431. word32 sigParamsIndex; /* start of signature parameters */
  1432. word32 sigParamsLength; /* length of signature parameters */
  1433. #endif
  1434. int version; /* cert version, 1 or 3 */
  1435. DNS_entry* altNames; /* alt names list of dns entries */
  1436. #ifndef IGNORE_NAME_CONSTRAINTS
  1437. DNS_entry* altEmailNames; /* alt names list of RFC822 entries */
  1438. DNS_entry* altDirNames; /* alt names list of DIR entries */
  1439. Base_entry* permittedNames; /* Permitted name bases */
  1440. Base_entry* excludedNames; /* Excluded name bases */
  1441. #endif /* IGNORE_NAME_CONSTRAINTS */
  1442. byte subjectHash[KEYID_SIZE]; /* hash of all Names */
  1443. byte issuerHash[KEYID_SIZE]; /* hash of all Names */
  1444. #ifdef HAVE_OCSP
  1445. byte subjectKeyHash[KEYID_SIZE]; /* hash of the public Key */
  1446. byte issuerKeyHash[KEYID_SIZE]; /* hash of the public Key */
  1447. #endif /* HAVE_OCSP */
  1448. const byte* signature; /* not owned, points into raw cert */
  1449. char* subjectCN; /* CommonName */
  1450. int subjectCNLen; /* CommonName Length */
  1451. char subjectCNEnc; /* CommonName Encoding */
  1452. char issuer[WC_ASN_NAME_MAX]; /* full name including common name */
  1453. char subject[WC_ASN_NAME_MAX];/* full name including common name */
  1454. int verify; /* Default to yes, but could be off */
  1455. const byte* source; /* byte buffer holder cert, NOT owner */
  1456. word32 srcIdx; /* current offset into buffer */
  1457. word32 maxIdx; /* max offset based on init size */
  1458. void* heap; /* for user memory overrides */
  1459. byte serial[EXTERNAL_SERIAL_SIZE]; /* raw serial number */
  1460. int serialSz; /* raw serial bytes stored */
  1461. const byte* extensions; /* not owned, points into raw cert */
  1462. int extensionsSz; /* length of cert extensions */
  1463. word32 extensionsIdx; /* if want to go back and parse later */
  1464. const byte* extAuthInfo; /* Authority Information Access URI */
  1465. int extAuthInfoSz; /* length of the URI */
  1466. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1467. const byte* extAuthInfoCaIssuer; /* Authority Info Access caIssuer URI */
  1468. int extAuthInfoCaIssuerSz; /* length of the caIssuer URI */
  1469. #endif
  1470. const byte* extCrlInfoRaw; /* Entire CRL Distribution Points
  1471. * Extension. This is useful when
  1472. * re-generating the DER. */
  1473. int extCrlInfoRawSz; /* length of the extension */
  1474. const byte* extCrlInfo; /* CRL Distribution Points */
  1475. int extCrlInfoSz; /* length of the URI */
  1476. byte extSubjKeyId[KEYID_SIZE]; /* Subject Key ID */
  1477. byte extAuthKeyId[KEYID_SIZE]; /* Authority Key ID */
  1478. byte pathLength; /* CA basic constraint path length */
  1479. byte maxPathLen; /* max_path_len see RFC 5280 section
  1480. * 6.1.2 "Initialization" - (k) for
  1481. * description of max_path_len */
  1482. byte policyConstSkip; /* Policy Constraints skip certs value */
  1483. word16 extKeyUsage; /* Key usage bitfield */
  1484. byte extExtKeyUsage; /* Extended Key usage bitfield */
  1485. #ifdef WOLFSSL_WOLFSSH
  1486. byte extExtKeyUsageSsh; /* Extended Key Usage bitfield for SSH */
  1487. #endif /* WOLFSSL_WOLFSSH */
  1488. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  1489. const byte* extExtKeyUsageSrc;
  1490. word32 extExtKeyUsageSz;
  1491. word32 extExtKeyUsageCount;
  1492. #ifdef WOLFSSL_AKID_NAME
  1493. const byte* extRawAuthKeyIdSrc;
  1494. word32 extRawAuthKeyIdSz;
  1495. #endif
  1496. const byte* extAuthKeyIdSrc;
  1497. word32 extAuthKeyIdSz;
  1498. const byte* extSubjKeyIdSrc;
  1499. word32 extSubjKeyIdSz;
  1500. #endif
  1501. #ifdef OPENSSL_ALL
  1502. const byte* extSubjAltNameSrc;
  1503. word32 extSubjAltNameSz;
  1504. #endif
  1505. #ifdef WOLFSSL_SUBJ_DIR_ATTR
  1506. char countryOfCitizenship[COUNTRY_CODE_LEN+1]; /* ISO 3166 Country Code */
  1507. #ifdef OPENSSL_ALL
  1508. const byte* extSubjDirAttrSrc;
  1509. word32 extSubjDirAttrSz;
  1510. #endif
  1511. #endif /* WOLFSSL_SUBJ_DIR_ATTR */
  1512. #ifdef WOLFSSL_SUBJ_INFO_ACC
  1513. const byte* extSubjInfoAccCaRepo;
  1514. word32 extSubjInfoAccCaRepoSz;
  1515. #ifdef OPENSSL_ALL
  1516. const byte* extSubjInfoAccSrc;
  1517. word32 extSubjInfoAccSz;
  1518. #endif
  1519. #endif /* WOLFSSL_SUBJ_INFO_ACC */
  1520. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  1521. word32 pkCurveOID; /* Public Key's curve OID */
  1522. #endif /* HAVE_ECC */
  1523. const byte* beforeDate;
  1524. int beforeDateLen;
  1525. const byte* afterDate;
  1526. int afterDateLen;
  1527. #if defined(HAVE_PKCS7) || defined(WOLFSSL_CERT_EXT)
  1528. const byte* issuerRaw; /* pointer to issuer inside source */
  1529. int issuerRawLen;
  1530. #endif
  1531. #if !defined(IGNORE_NAME_CONSTRAINTS) || defined(WOLFSSL_CERT_EXT)
  1532. const byte* subjectRaw; /* pointer to subject inside source */
  1533. int subjectRawLen;
  1534. #endif
  1535. #if !defined(IGNORE_NAME_CONSTRAINTS) || \
  1536. defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_EXT)
  1537. char* subjectEmail;
  1538. int subjectEmailLen;
  1539. #endif
  1540. #if defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_EXT)
  1541. /* easy access to subject info for other sign */
  1542. char* subjectSN;
  1543. int subjectSNLen;
  1544. char subjectSNEnc;
  1545. #ifdef WOLFSSL_CERT_NAME_ALL
  1546. char* subjectN;
  1547. int subjectNLen;
  1548. char subjectNEnc;
  1549. char* subjectI;
  1550. int subjectILen;
  1551. char subjectIEnc;
  1552. char* subjectGN;
  1553. int subjectGNLen;
  1554. char subjectGNEnc;
  1555. char* subjectDNQ;
  1556. int subjectDNQLen;
  1557. char subjectDNQEnc;
  1558. #endif /*WOLFSSL_CERT_NAME_ALL */
  1559. char* subjectC;
  1560. int subjectCLen;
  1561. char subjectCEnc;
  1562. char* subjectL;
  1563. int subjectLLen;
  1564. char subjectLEnc;
  1565. char* subjectST;
  1566. int subjectSTLen;
  1567. char subjectSTEnc;
  1568. char* subjectO;
  1569. int subjectOLen;
  1570. char subjectOEnc;
  1571. char* subjectOU;
  1572. int subjectOULen;
  1573. char subjectOUEnc;
  1574. char* subjectSND;
  1575. int subjectSNDLen;
  1576. char subjectSNDEnc;
  1577. char* subjectUID;
  1578. int subjectUIDLen;
  1579. char subjectUIDEnc;
  1580. char* subjectStreet;
  1581. int subjectStreetLen;
  1582. char subjectStreetEnc;
  1583. char* subjectBC;
  1584. int subjectBCLen;
  1585. char subjectBCEnc;
  1586. char* subjectJC;
  1587. int subjectJCLen;
  1588. char subjectJCEnc;
  1589. char* subjectJS;
  1590. int subjectJSLen;
  1591. char subjectJSEnc;
  1592. char* subjectPC;
  1593. int subjectPCLen;
  1594. char subjectPCEnc;
  1595. #if defined(WOLFSSL_HAVE_ISSUER_NAMES)
  1596. char* issuerCN;
  1597. int issuerCNLen;
  1598. char issuerCNEnc;
  1599. char* issuerSN;
  1600. int issuerSNLen;
  1601. char issuerSNEnc;
  1602. char* issuerC;
  1603. int issuerCLen;
  1604. char issuerCEnc;
  1605. char* issuerL;
  1606. int issuerLLen;
  1607. char issuerLEnc;
  1608. char* issuerST;
  1609. int issuerSTLen;
  1610. char issuerSTEnc;
  1611. char* issuerO;
  1612. int issuerOLen;
  1613. char issuerOEnc;
  1614. char* issuerOU;
  1615. int issuerOULen;
  1616. char issuerOUEnc;
  1617. char* issuerSND;
  1618. int issuerSNDLen;
  1619. char issuerSNDEnc;
  1620. char* issuerEmail;
  1621. int issuerEmailLen;
  1622. #endif /* WOLFSSL_HAVE_ISSUER_NAMES */
  1623. #endif /* defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_EXT) */
  1624. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  1625. /* WOLFSSL_X509_NAME structures (used void* to avoid including ssl.h) */
  1626. void* issuerName;
  1627. void* subjectName;
  1628. #endif /* OPENSSL_EXTRA */
  1629. #ifdef WOLFSSL_SEP
  1630. int deviceTypeSz;
  1631. byte* deviceType;
  1632. int hwTypeSz;
  1633. byte* hwType;
  1634. int hwSerialNumSz;
  1635. byte* hwSerialNum;
  1636. #endif /* WOLFSSL_SEP */
  1637. #ifdef WOLFSSL_CERT_EXT
  1638. char extCertPolicies[MAX_CERTPOL_NB][MAX_CERTPOL_SZ];
  1639. int extCertPoliciesNb;
  1640. #endif /* WOLFSSL_CERT_EXT */
  1641. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  1642. byte nsCertType;
  1643. #endif
  1644. #ifdef WOLFSSL_CERT_REQ
  1645. /* CSR attributes */
  1646. char* contentType; /* Content Type */
  1647. int contentTypeLen;
  1648. char* cPwd; /* Challenge Password */
  1649. int cPwdLen;
  1650. char* sNum; /* Serial Number */
  1651. int sNumLen;
  1652. char* dnQualifier;
  1653. int dnQualifierLen;
  1654. char* initials;
  1655. int initialsLen;
  1656. char* surname;
  1657. int surnameLen;
  1658. char* givenName;
  1659. int givenNameLen;
  1660. char* unstructuredName;
  1661. int unstructuredNameLen;
  1662. #endif /* WOLFSSL_CERT_REQ */
  1663. Signer* ca;
  1664. #ifndef NO_CERTS
  1665. SignatureCtx sigCtx;
  1666. #endif
  1667. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT)
  1668. byte* sce_tsip_encRsaKeyIdx;
  1669. #endif
  1670. int badDate;
  1671. int criticalExt;
  1672. /* Option Bits */
  1673. byte subjectCNStored : 1; /* have we saved a copy we own */
  1674. byte extSubjKeyIdSet : 1; /* Set when the SKID was read from cert */
  1675. byte extAuthKeyIdSet : 1; /* Set when the AKID was read from cert */
  1676. #ifndef IGNORE_NAME_CONSTRAINTS
  1677. byte extNameConstraintSet : 1;
  1678. #endif
  1679. byte isCA : 1; /* CA basic constraint true */
  1680. byte pathLengthSet : 1; /* CA basic const path length set */
  1681. byte weOwnAltNames : 1; /* altNames haven't been given to copy */
  1682. byte extKeyUsageSet : 1;
  1683. byte extExtKeyUsageSet : 1; /* Extended Key Usage set */
  1684. #ifdef HAVE_OCSP
  1685. byte ocspNoCheckSet : 1; /* id-pkix-ocsp-nocheck set */
  1686. #endif
  1687. byte extCRLdistSet : 1;
  1688. byte extAuthInfoSet : 1;
  1689. byte extBasicConstSet : 1;
  1690. byte extPolicyConstSet : 1;
  1691. byte extPolicyConstRxpSet : 1; /* requireExplicitPolicy set */
  1692. byte extPolicyConstIpmSet : 1; /* inhibitPolicyMapping set */
  1693. byte extSubjAltNameSet : 1;
  1694. byte inhibitAnyOidSet : 1;
  1695. byte selfSigned : 1; /* Indicates subject and issuer are same */
  1696. #if defined(WOLFSSL_SEP) || defined(WOLFSSL_QT)
  1697. byte extCertPolicySet : 1;
  1698. #endif
  1699. byte extCRLdistCrit : 1;
  1700. byte extAuthInfoCrit : 1;
  1701. byte extBasicConstCrit : 1;
  1702. byte extPolicyConstCrit : 1;
  1703. byte extSubjAltNameCrit : 1;
  1704. byte extAuthKeyIdCrit : 1;
  1705. #ifndef IGNORE_NAME_CONSTRAINTS
  1706. byte extNameConstraintCrit : 1;
  1707. #endif
  1708. byte extSubjKeyIdCrit : 1;
  1709. byte extKeyUsageCrit : 1;
  1710. byte extExtKeyUsageCrit : 1;
  1711. #ifdef WOLFSSL_SUBJ_DIR_ATTR
  1712. byte extSubjDirAttrSet : 1;
  1713. #endif
  1714. #ifdef WOLFSSL_SUBJ_INFO_ACC
  1715. byte extSubjInfoAccSet : 1;
  1716. #endif
  1717. #if defined(WOLFSSL_SEP) || defined(WOLFSSL_QT)
  1718. byte extCertPolicyCrit : 1;
  1719. #endif
  1720. #ifdef WOLFSSL_CERT_REQ
  1721. byte isCSR : 1; /* Do we intend on parsing a CSR? */
  1722. #endif
  1723. #if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
  1724. && defined(HAVE_OID_DECODING)
  1725. wc_UnknownExtCallback unknownExtCallback;
  1726. #endif
  1727. };
  1728. #ifdef NO_SHA
  1729. #define SIGNER_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  1730. #else
  1731. #define SIGNER_DIGEST_SIZE WC_SHA_DIGEST_SIZE
  1732. #endif
  1733. /* CA Signers */
  1734. /* if change layout change PERSIST_CERT_CACHE functions too */
  1735. struct Signer {
  1736. word32 pubKeySize;
  1737. word32 keyOID; /* key type */
  1738. word16 keyUsage;
  1739. byte maxPathLen;
  1740. byte pathLength;
  1741. byte pathLengthSet : 1;
  1742. byte selfSigned : 1;
  1743. const byte* publicKey;
  1744. int nameLen;
  1745. char* name; /* common name */
  1746. #ifndef IGNORE_NAME_CONSTRAINTS
  1747. Base_entry* permittedNames;
  1748. Base_entry* excludedNames;
  1749. #endif /* IGNORE_NAME_CONSTRAINTS */
  1750. byte subjectNameHash[SIGNER_DIGEST_SIZE];
  1751. /* sha hash of names in certificate */
  1752. #ifndef NO_SKID
  1753. byte subjectKeyIdHash[SIGNER_DIGEST_SIZE];
  1754. /* sha hash of names in certificate */
  1755. #endif
  1756. #ifdef HAVE_OCSP
  1757. byte subjectKeyHash[KEYID_SIZE];
  1758. #endif
  1759. #ifdef WOLFSSL_SIGNER_DER_CERT
  1760. DerBuffer* derCert;
  1761. #endif
  1762. #if defined(WOLFSSL_RENESAS_TSIP_TLS) || defined(WOLFSSL_RENESAS_SCEPROTECT)
  1763. word32 cm_idx;
  1764. #endif
  1765. Signer* next;
  1766. };
  1767. #ifdef WOLFSSL_TRUST_PEER_CERT
  1768. /* used for having trusted peer certs rather then CA */
  1769. struct TrustedPeerCert {
  1770. int nameLen;
  1771. char* name; /* common name */
  1772. #ifndef IGNORE_NAME_CONSTRAINTS
  1773. Base_entry* permittedNames;
  1774. Base_entry* excludedNames;
  1775. #endif /* IGNORE_NAME_CONSTRAINTS */
  1776. byte subjectNameHash[SIGNER_DIGEST_SIZE];
  1777. /* sha hash of names in certificate */
  1778. #ifndef NO_SKID
  1779. byte subjectKeyIdHash[SIGNER_DIGEST_SIZE];
  1780. /* sha hash of SKID in certificate */
  1781. #endif
  1782. word32 sigLen;
  1783. byte* sig;
  1784. struct TrustedPeerCert* next;
  1785. };
  1786. #endif /* WOLFSSL_TRUST_PEER_CERT */
  1787. /* for testing or custom openssl wrappers */
  1788. #if defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1789. defined(OPENSSL_EXTRA_X509_SMALL) || defined(WOLFSSL_PUBLIC_ASN)
  1790. #define WOLFSSL_ASN_API WOLFSSL_API
  1791. #else
  1792. #define WOLFSSL_ASN_API WOLFSSL_LOCAL
  1793. #endif
  1794. #ifdef HAVE_SMIME
  1795. #define MIME_HEADER_ASCII_MIN 33
  1796. #define MIME_HEADER_ASCII_MAX 126
  1797. typedef struct MimeParam MimeParam;
  1798. typedef struct MimeHdr MimeHdr;
  1799. struct MimeParam
  1800. {
  1801. MimeParam* next;
  1802. char* attribute;
  1803. char* value;
  1804. };
  1805. struct MimeHdr
  1806. {
  1807. MimeHdr* next;
  1808. MimeParam* params;
  1809. char* name;
  1810. char* body;
  1811. };
  1812. typedef enum MimeTypes
  1813. {
  1814. MIME_HDR,
  1815. MIME_PARAM
  1816. } MimeTypes;
  1817. typedef enum MimeStatus
  1818. {
  1819. MIME_NAMEATTR,
  1820. MIME_BODYVAL
  1821. } MimeStatus;
  1822. #endif /* HAVE_SMIME */
  1823. WOLFSSL_LOCAL int CalcHashId(const byte* data, word32 len, byte* hash);
  1824. WOLFSSL_LOCAL int GetName(DecodedCert* cert, int nameType, int maxIdx);
  1825. WOLFSSL_ASN_API int wc_BerToDer(const byte* ber, word32 berSz, byte* der,
  1826. word32* derSz);
  1827. WOLFSSL_ASN_API void FreeAltNames(DNS_entry* altNames, void* heap);
  1828. WOLFSSL_ASN_API DNS_entry* AltNameNew(void* heap);
  1829. #ifndef IGNORE_NAME_CONSTRAINTS
  1830. WOLFSSL_ASN_API void FreeNameSubtrees(Base_entry* names, void* heap);
  1831. #endif /* IGNORE_NAME_CONSTRAINTS */
  1832. WOLFSSL_ASN_API void InitDecodedCert(DecodedCert* cert, const byte* source,
  1833. word32 inSz, void* heap);
  1834. WOLFSSL_ASN_API void FreeDecodedCert(DecodedCert* cert);
  1835. WOLFSSL_ASN_API int ParseCert(DecodedCert* cert, int type, int verify,
  1836. void* cm);
  1837. #if defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ASN_TEMPLATE) \
  1838. && defined(HAVE_OID_DECODING)
  1839. WOLFSSL_ASN_API int wc_SetUnknownExtCallback(DecodedCert* cert,
  1840. wc_UnknownExtCallback cb);
  1841. #endif
  1842. WOLFSSL_LOCAL int DecodePolicyOID(char *out, word32 outSz, const byte *in,
  1843. word32 inSz);
  1844. WOLFSSL_LOCAL int EncodePolicyOID(byte *out, word32 *outSz,
  1845. const char *in, void* heap);
  1846. WOLFSSL_API int CheckCertSignature(const byte*,word32,void*,void* cm);
  1847. WOLFSSL_LOCAL int CheckCertSignaturePubKey(const byte* cert, word32 certSz,
  1848. void* heap, const byte* pubKey, word32 pubKeySz, int pubKeyOID);
  1849. #ifdef OPENSSL_EXTRA
  1850. WOLFSSL_API int wc_CheckCertSigPubKey(const byte* cert, word32 certSz,
  1851. void* heap, const byte* pubKey,
  1852. word32 pubKeySz, int pubKeyOID);
  1853. #endif
  1854. #ifdef WOLFSSL_CERT_REQ
  1855. WOLFSSL_LOCAL int CheckCSRSignaturePubKey(const byte* cert, word32 certSz,
  1856. void* heap, const byte* pubKey, word32 pubKeySz, int pubKeyOID);
  1857. #endif /* WOLFSSL_CERT_REQ */
  1858. WOLFSSL_ASN_API int AddSignature(byte* buf, int bodySz, const byte* sig, int sigSz,
  1859. int sigAlgoType);
  1860. WOLFSSL_LOCAL int ParseCertRelative(DecodedCert* cert, int type, int verify,
  1861. void* cm);
  1862. WOLFSSL_LOCAL int DecodeToKey(DecodedCert* cert, int verify);
  1863. #ifdef WOLFSSL_ASN_TEMPLATE
  1864. WOLFSSL_LOCAL int DecodeCert(DecodedCert* cert, int verify, int* criticalExt);
  1865. #endif
  1866. WOLFSSL_LOCAL int wc_GetPubX509(DecodedCert* cert, int verify, int* badDate);
  1867. WOLFSSL_LOCAL const byte* OidFromId(word32 id, word32 type, word32* oidSz);
  1868. WOLFSSL_LOCAL Signer* MakeSigner(void* heap);
  1869. WOLFSSL_LOCAL void FreeSigner(Signer* signer, void* heap);
  1870. WOLFSSL_LOCAL void FreeSignerTable(Signer** table, int rows, void* heap);
  1871. #ifdef WOLFSSL_TRUST_PEER_CERT
  1872. WOLFSSL_LOCAL void FreeTrustedPeer(TrustedPeerCert* tp, void* heap);
  1873. WOLFSSL_LOCAL void FreeTrustedPeerTable(TrustedPeerCert** table, int rows,
  1874. void* heap);
  1875. #endif /* WOLFSSL_TRUST_PEER_CERT */
  1876. WOLFSSL_ASN_API int ToTraditional(byte* buffer, word32 length);
  1877. WOLFSSL_ASN_API int ToTraditional_ex(byte* buffer, word32 length,
  1878. word32* algId);
  1879. WOLFSSL_LOCAL int ToTraditionalInline(const byte* input, word32* inOutIdx,
  1880. word32 length);
  1881. WOLFSSL_LOCAL int ToTraditionalInline_ex(const byte* input, word32* inOutIdx,
  1882. word32 length, word32* algId);
  1883. WOLFSSL_LOCAL int ToTraditionalEnc(byte* input, word32 sz, const char* password,
  1884. int passwordSz, word32* algId);
  1885. WOLFSSL_ASN_API int UnTraditionalEnc(byte* key, word32 keySz, byte* out,
  1886. word32* outSz, const char* password, int passwordSz, int vPKCS,
  1887. int vAlgo, byte* salt, word32 saltSz, int itt, WC_RNG* rng, void* heap);
  1888. WOLFSSL_ASN_API int TraditionalEnc(byte* key, word32 keySz, byte* out,
  1889. word32* outSz, const char* password, int passwordSz, int vPKCS,
  1890. int vAlgo, int encAlgId, byte* salt, word32 saltSz, int itt,
  1891. WC_RNG* rng, void* heap);
  1892. WOLFSSL_LOCAL int DecryptContent(byte* input, word32 sz,const char* psw,int pswSz);
  1893. WOLFSSL_LOCAL int EncryptContent(byte* input, word32 sz, byte* out, word32* outSz,
  1894. const char* password,int passwordSz, int vPKCS, int vAlgo,
  1895. byte* salt, word32 saltSz, int itt, WC_RNG* rng, void* heap);
  1896. WOLFSSL_LOCAL int wc_GetKeyOID(byte* key, word32 keySz, const byte** curveOID,
  1897. word32* oidSz, int* algoID, void* heap);
  1898. typedef struct tm wolfssl_tm;
  1899. #if defined(OPENSSL_ALL) || defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_EXTRA) || \
  1900. defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  1901. WOLFSSL_LOCAL int GetTimeString(byte* date, int format, char* buf, int len);
  1902. #endif
  1903. #if !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  1904. !defined(TIME_OVERRIDES) && (defined(OPENSSL_EXTRA) || defined(HAVE_PKCS7))
  1905. WOLFSSL_LOCAL int GetFormattedTime(void* currTime, byte* buf, word32 len);
  1906. WOLFSSL_LOCAL int GetAsnTimeString(void* currTime, byte* buf, word32 len);
  1907. #endif
  1908. WOLFSSL_LOCAL int ExtractDate(const unsigned char* date, unsigned char format,
  1909. wolfssl_tm* certTime, int* idx);
  1910. WOLFSSL_LOCAL int DateGreaterThan(const struct tm* a, const struct tm* b);
  1911. WOLFSSL_LOCAL int wc_ValidateDate(const byte* date, byte format, int dateType);
  1912. WOLFSSL_LOCAL int wc_OBJ_sn2nid(const char *sn);
  1913. /* ASN.1 helper functions */
  1914. #ifdef WOLFSSL_CERT_GEN
  1915. WOLFSSL_LOCAL int SetNameEx(byte* output, word32 outputSz, CertName* name, void* heap);
  1916. WOLFSSL_ASN_API int SetName(byte* output, word32 outputSz, CertName* name);
  1917. WOLFSSL_LOCAL const char* GetOneCertName(CertName* name, int idx);
  1918. WOLFSSL_LOCAL byte GetCertNameId(int idx);
  1919. #endif
  1920. WOLFSSL_LOCAL int GetShortInt(const byte* input, word32* inOutIdx, int* number,
  1921. word32 maxIdx);
  1922. WOLFSSL_LOCAL int SetShortInt(byte* input, word32* inOutIdx, word32 number,
  1923. word32 maxIdx);
  1924. WOLFSSL_LOCAL const char* GetSigName(int oid);
  1925. WOLFSSL_LOCAL int GetLength(const byte* input, word32* inOutIdx, int* len,
  1926. word32 maxIdx);
  1927. WOLFSSL_LOCAL int GetLength_ex(const byte* input, word32* inOutIdx, int* len,
  1928. word32 maxIdx, int check);
  1929. WOLFSSL_LOCAL int GetSequence(const byte* input, word32* inOutIdx, int* len,
  1930. word32 maxIdx);
  1931. WOLFSSL_LOCAL int GetSequence_ex(const byte* input, word32* inOutIdx, int* len,
  1932. word32 maxIdx, int check);
  1933. WOLFSSL_LOCAL int GetOctetString(const byte* input, word32* inOutIdx, int* len,
  1934. word32 maxIdx);
  1935. WOLFSSL_LOCAL int CheckBitString(const byte* input, word32* inOutIdx, int* len,
  1936. word32 maxIdx, int zeroBits, byte* unusedBits);
  1937. WOLFSSL_LOCAL int GetSet(const byte* input, word32* inOutIdx, int* len,
  1938. word32 maxIdx);
  1939. WOLFSSL_LOCAL int GetSet_ex(const byte* input, word32* inOutIdx, int* len,
  1940. word32 maxIdx, int check);
  1941. WOLFSSL_LOCAL int GetMyVersion(const byte* input, word32* inOutIdx,
  1942. int* version, word32 maxIdx);
  1943. WOLFSSL_LOCAL int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx,
  1944. word32 maxIdx);
  1945. #ifdef HAVE_OID_ENCODING
  1946. WOLFSSL_LOCAL int EncodeObjectId(const word16* in, word32 inSz,
  1947. byte* out, word32* outSz);
  1948. #endif
  1949. #ifdef HAVE_OID_DECODING
  1950. WOLFSSL_LOCAL int DecodeObjectId(const byte* in, word32 inSz,
  1951. word16* out, word32* outSz);
  1952. #endif
  1953. WOLFSSL_LOCAL int GetASNObjectId(const byte* input, word32* inOutIdx, int* len,
  1954. word32 maxIdx);
  1955. WOLFSSL_LOCAL int SetObjectId(int len, byte* output);
  1956. WOLFSSL_LOCAL int GetObjectId(const byte* input, word32* inOutIdx, word32* oid,
  1957. word32 oidType, word32 maxIdx);
  1958. WOLFSSL_LOCAL int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
  1959. word32 oidType, word32 maxIdx);
  1960. WOLFSSL_LOCAL int GetASNTag(const byte* input, word32* idx, byte* tag,
  1961. word32 inputSz);
  1962. WOLFSSL_LOCAL word32 SetASNLength(word32 length, byte* output);
  1963. WOLFSSL_LOCAL word32 SetASNSequence(word32 len, byte* output);
  1964. WOLFSSL_LOCAL word32 SetASNOctetString(word32 len, byte* output);
  1965. WOLFSSL_LOCAL word32 SetASNImplicit(byte tag,byte number, word32 len,
  1966. byte* output);
  1967. WOLFSSL_LOCAL word32 SetASNExplicit(byte number, word32 len, byte* output);
  1968. WOLFSSL_LOCAL word32 SetASNSet(word32 len, byte* output);
  1969. WOLFSSL_LOCAL word32 SetLength(word32 length, byte* output);
  1970. WOLFSSL_LOCAL word32 SetSequence(word32 len, byte* output);
  1971. WOLFSSL_LOCAL word32 SetOctetString(word32 len, byte* output);
  1972. WOLFSSL_LOCAL int SetASNInt(int len, byte firstByte, byte* output);
  1973. WOLFSSL_LOCAL word32 SetBitString(word32 len, byte unusedBits, byte* output);
  1974. WOLFSSL_LOCAL word32 SetImplicit(byte tag,byte number,word32 len,byte* output);
  1975. WOLFSSL_LOCAL word32 SetExplicit(byte number, word32 len, byte* output);
  1976. WOLFSSL_LOCAL word32 SetSet(word32 len, byte* output);
  1977. WOLFSSL_LOCAL word32 SetAlgoID(int algoOID,byte* output,int type,int curveSz);
  1978. WOLFSSL_LOCAL int SetMyVersion(word32 version, byte* output, int header);
  1979. WOLFSSL_LOCAL int SetSerialNumber(const byte* sn, word32 snSz, byte* output,
  1980. word32 outputSz, int maxSnSz);
  1981. #ifndef WOLFSSL_ASN_TEMPLATE
  1982. WOLFSSL_LOCAL int wc_GetSerialNumber(const byte* input, word32* inOutIdx,
  1983. byte* serial, int* serialSz, word32 maxIdx);
  1984. #endif
  1985. WOLFSSL_LOCAL int GetNameHash(const byte* source, word32* idx, byte* hash,
  1986. int maxIdx);
  1987. WOLFSSL_LOCAL int wc_CheckPrivateKeyCert(const byte* key, word32 keySz, DecodedCert* der);
  1988. WOLFSSL_LOCAL int wc_CheckPrivateKey(const byte* privKey, word32 privKeySz,
  1989. const byte* pubKey, word32 pubKeySz, enum Key_Sum ks);
  1990. WOLFSSL_LOCAL int StoreDHparams(byte* out, word32* outLen, mp_int* p, mp_int* g);
  1991. #ifdef WOLFSSL_DH_EXTRA
  1992. WOLFSSL_API int wc_DhPublicKeyDecode(const byte* input, word32* inOutIdx,
  1993. DhKey* key, word32 inSz);
  1994. #endif
  1995. WOLFSSL_LOCAL int FlattenAltNames(byte* output, word32 outputSz,
  1996. const DNS_entry* names);
  1997. WOLFSSL_LOCAL int wc_EncodeName(EncodedName* name, const char* nameStr,
  1998. char nameType, byte type);
  1999. WOLFSSL_LOCAL int wc_EncodeNameCanonical(EncodedName* name, const char* nameStr,
  2000. char nameType, byte type);
  2001. #if defined(HAVE_ECC) || !defined(NO_DSA)
  2002. /* ASN sig helpers */
  2003. WOLFSSL_LOCAL int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r,
  2004. mp_int* s);
  2005. WOLFSSL_LOCAL int StoreECC_DSA_Sig_Bin(byte* out, word32* outLen,
  2006. const byte* r, word32 rLen, const byte* s, word32 sLen);
  2007. WOLFSSL_LOCAL int DecodeECC_DSA_Sig_Bin(const byte* sig, word32 sigLen,
  2008. byte* r, word32* rLen, byte* s, word32* sLen);
  2009. WOLFSSL_LOCAL int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen,
  2010. mp_int* r, mp_int* s);
  2011. #endif
  2012. #ifndef NO_DSA
  2013. WOLFSSL_LOCAL int StoreDSAParams(byte*, word32*, const mp_int*, const mp_int*,
  2014. const mp_int*);
  2015. #endif
  2016. #if defined HAVE_ECC && (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  2017. WOLFSSL_API int EccEnumToNID(int n);
  2018. #endif
  2019. WOLFSSL_LOCAL void InitSignatureCtx(SignatureCtx* sigCtx, void* heap, int devId);
  2020. WOLFSSL_LOCAL void FreeSignatureCtx(SignatureCtx* sigCtx);
  2021. WOLFSSL_LOCAL int SetAsymKeyDerPublic(const byte* pubKey, word32 pubKeyLen,
  2022. byte* output, word32 outLen, int keyType, int withHeader);
  2023. WOLFSSL_LOCAL int DecodeAsymKeyPublic(const byte* input, word32* inOutIdx, word32 inSz,
  2024. byte* pubKey, word32* pubKeyLen, int keyType);
  2025. #ifndef NO_CERTS
  2026. WOLFSSL_LOCAL int wc_EncryptedInfoParse(EncryptedInfo* info,
  2027. const char** pBuffer, size_t bufSz);
  2028. WOLFSSL_LOCAL int PemToDer(const unsigned char* buff, long sz, int type,
  2029. DerBuffer** pDer, void* heap, EncryptedInfo* info,
  2030. int* eccKey);
  2031. WOLFSSL_LOCAL int AllocDer(DerBuffer** der, word32 length, int type, void* heap);
  2032. WOLFSSL_LOCAL void FreeDer(DerBuffer** der);
  2033. #endif /* !NO_CERTS */
  2034. #ifdef HAVE_SMIME
  2035. WOLFSSL_LOCAL int wc_MIME_parse_headers(char* in, int inLen, MimeHdr** hdrs);
  2036. WOLFSSL_LOCAL int wc_MIME_header_strip(char* in, char** out, size_t start, size_t end);
  2037. WOLFSSL_LOCAL MimeHdr* wc_MIME_find_header_name(const char* name, MimeHdr* hdr);
  2038. WOLFSSL_LOCAL MimeParam* wc_MIME_find_param_attr(const char* attribute, MimeParam* param);
  2039. WOLFSSL_LOCAL char* wc_MIME_single_canonicalize(const char* line, word32* len);
  2040. WOLFSSL_LOCAL int wc_MIME_free_hdrs(MimeHdr* head);
  2041. #endif /* HAVE_SMIME */
  2042. #ifdef WOLFSSL_CERT_GEN
  2043. enum cert_enums {
  2044. RSA_KEY = 10,
  2045. ECC_KEY = 12,
  2046. ED25519_KEY = 13,
  2047. ED448_KEY = 14,
  2048. DSA_KEY = 15,
  2049. FALCON_LEVEL1_KEY = 16,
  2050. FALCON_LEVEL5_KEY = 17,
  2051. DILITHIUM_LEVEL2_KEY = 18,
  2052. DILITHIUM_LEVEL3_KEY = 19,
  2053. DILITHIUM_LEVEL5_KEY = 20,
  2054. DILITHIUM_AES_LEVEL2_KEY = 21,
  2055. DILITHIUM_AES_LEVEL3_KEY = 22,
  2056. DILITHIUM_AES_LEVEL5_KEY = 23,
  2057. SPHINCS_FAST_LEVEL1_KEY = 24,
  2058. SPHINCS_FAST_LEVEL3_KEY = 25,
  2059. SPHINCS_FAST_LEVEL5_KEY = 26,
  2060. SPHINCS_SMALL_LEVEL1_KEY = 27,
  2061. SPHINCS_SMALL_LEVEL3_KEY = 28,
  2062. SPHINCS_SMALL_LEVEL5_KEY = 29,
  2063. };
  2064. #endif /* WOLFSSL_CERT_GEN */
  2065. /* for pointer use */
  2066. typedef struct CertStatus CertStatus;
  2067. #ifdef HAVE_OCSP
  2068. enum Ocsp_Response_Status {
  2069. OCSP_SUCCESSFUL = 0, /* Response has valid confirmations */
  2070. OCSP_MALFORMED_REQUEST = 1, /* Illegal confirmation request */
  2071. OCSP_INTERNAL_ERROR = 2, /* Internal error in issuer */
  2072. OCSP_TRY_LATER = 3, /* Try again later */
  2073. OCSP_SIG_REQUIRED = 5, /* Must sign the request (4 is skipped) */
  2074. OCSP_UNAUTHORIZED = 6 /* Request unauthorized */
  2075. };
  2076. enum Ocsp_Cert_Status {
  2077. CERT_GOOD = 0,
  2078. CERT_REVOKED = 1,
  2079. CERT_UNKNOWN = 2
  2080. };
  2081. enum Ocsp_Sums {
  2082. OCSP_BASIC_OID = 117,
  2083. OCSP_NONCE_OID = 118
  2084. };
  2085. #ifdef OPENSSL_EXTRA
  2086. enum Ocsp_Verify_Error {
  2087. OCSP_VERIFY_ERROR_NONE = 0,
  2088. OCSP_BAD_ISSUER = 1
  2089. };
  2090. #endif
  2091. typedef struct OcspRequest OcspRequest;
  2092. typedef struct OcspResponse OcspResponse;
  2093. struct CertStatus {
  2094. CertStatus* next;
  2095. byte serial[EXTERNAL_SERIAL_SIZE];
  2096. int serialSz;
  2097. #ifdef OPENSSL_EXTRA
  2098. WOLFSSL_ASN1_INTEGER* serialInt;
  2099. #endif
  2100. int status;
  2101. byte thisDate[MAX_DATE_SIZE];
  2102. byte nextDate[MAX_DATE_SIZE];
  2103. byte thisDateFormat;
  2104. byte nextDateFormat;
  2105. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  2106. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  2107. WOLFSSL_ASN1_TIME thisDateParsed;
  2108. WOLFSSL_ASN1_TIME nextDateParsed;
  2109. byte* thisDateAsn;
  2110. byte* nextDateAsn;
  2111. #endif
  2112. byte* rawOcspResponse;
  2113. word32 rawOcspResponseSz;
  2114. };
  2115. typedef struct OcspEntry OcspEntry;
  2116. #ifdef NO_SHA
  2117. #define OCSP_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  2118. #else
  2119. #define OCSP_DIGEST_SIZE WC_SHA_DIGEST_SIZE
  2120. #endif
  2121. struct OcspEntry
  2122. {
  2123. OcspEntry *next; /* next entry */
  2124. word32 hashAlgoOID; /* hash algo ID */
  2125. byte issuerHash[OCSP_DIGEST_SIZE]; /* issuer hash */
  2126. byte issuerKeyHash[OCSP_DIGEST_SIZE]; /* issuer public key hash */
  2127. CertStatus *status; /* OCSP response list */
  2128. int totalStatus; /* number on list */
  2129. byte* rawCertId; /* raw bytes of the CertID */
  2130. int rawCertIdSize; /* num bytes in raw CertID */
  2131. /* option bits - using 32-bit for alignment */
  2132. word32 ownStatus:1; /* do we need to free the status
  2133. * response list */
  2134. word32 isDynamic:1; /* was dynamically allocated */
  2135. word32 used:1; /* entry used */
  2136. };
  2137. /* TODO: Long-term, it would be helpful if we made this struct and other OCSP
  2138. structs conform to the ASN spec as described in RFC 6960. It will help
  2139. with readability and with implementing OpenSSL compatibility API
  2140. functions, because OpenSSL's OCSP data structures conform to the
  2141. RFC. */
  2142. struct OcspResponse {
  2143. int responseStatus; /* return code from Responder */
  2144. byte* response; /* Pointer to beginning of OCSP Response */
  2145. word32 responseSz; /* length of the OCSP Response */
  2146. byte producedDate[MAX_DATE_SIZE];
  2147. /* Date at which this response was signed */
  2148. byte producedDateFormat; /* format of the producedDate */
  2149. byte* cert;
  2150. word32 certSz;
  2151. byte* sig; /* Pointer to sig in source */
  2152. word32 sigSz; /* Length in octets for the sig */
  2153. word32 sigOID; /* OID for hash used for sig */
  2154. OcspEntry* single; /* chain of OCSP single responses */
  2155. byte* nonce; /* pointer to nonce inside ASN.1 response */
  2156. int nonceSz; /* length of the nonce string */
  2157. byte* source; /* pointer to source buffer, not owned */
  2158. word32 maxIdx; /* max offset based on init size */
  2159. #ifdef OPENSSL_EXTRA
  2160. int verifyError;
  2161. #endif
  2162. void* heap;
  2163. };
  2164. struct OcspRequest {
  2165. byte issuerHash[KEYID_SIZE];
  2166. byte issuerKeyHash[KEYID_SIZE];
  2167. byte* serial; /* copy of the serial number in source cert */
  2168. int serialSz;
  2169. #ifdef OPENSSL_EXTRA
  2170. WOLFSSL_ASN1_INTEGER* serialInt;
  2171. #endif
  2172. byte* url; /* copy of the extAuthInfo in source cert */
  2173. int urlSz;
  2174. byte nonce[MAX_OCSP_NONCE_SZ];
  2175. int nonceSz;
  2176. void* heap;
  2177. void* ssl;
  2178. };
  2179. WOLFSSL_LOCAL void InitOcspResponse(OcspResponse* resp, OcspEntry* single,
  2180. CertStatus* status, byte* source, word32 inSz, void* heap);
  2181. WOLFSSL_LOCAL void FreeOcspResponse(OcspResponse* resp);
  2182. WOLFSSL_LOCAL int OcspResponseDecode(OcspResponse* resp, void* cm, void* heap,
  2183. int noVerify);
  2184. WOLFSSL_LOCAL int InitOcspRequest(OcspRequest* req, DecodedCert* cert,
  2185. byte useNonce, void* heap);
  2186. WOLFSSL_LOCAL void FreeOcspRequest(OcspRequest* req);
  2187. WOLFSSL_LOCAL int EncodeOcspRequest(OcspRequest* req, byte* output,
  2188. word32 size);
  2189. WOLFSSL_LOCAL word32 EncodeOcspRequestExtensions(OcspRequest* req, byte* output,
  2190. word32 size);
  2191. WOLFSSL_LOCAL int CompareOcspReqResp(OcspRequest* req, OcspResponse* resp);
  2192. #endif /* HAVE_OCSP */
  2193. /* for pointer use */
  2194. typedef struct RevokedCert RevokedCert;
  2195. #ifdef HAVE_CRL
  2196. struct RevokedCert {
  2197. byte serialNumber[EXTERNAL_SERIAL_SIZE];
  2198. int serialSz;
  2199. RevokedCert* next;
  2200. byte revDate[MAX_DATE_SIZE];
  2201. byte revDateFormat;
  2202. };
  2203. typedef struct DecodedCRL DecodedCRL;
  2204. struct DecodedCRL {
  2205. word32 certBegin; /* offset to start of cert */
  2206. word32 sigIndex; /* offset to start of signature */
  2207. word32 sigLength; /* length of signature */
  2208. word32 signatureOID; /* sum of algorithm object id */
  2209. byte* signature; /* pointer into raw source, not owned */
  2210. byte issuerHash[SIGNER_DIGEST_SIZE]; /* issuer name hash */
  2211. byte crlHash[SIGNER_DIGEST_SIZE]; /* raw crl data hash */
  2212. byte lastDate[MAX_DATE_SIZE]; /* last date updated */
  2213. byte nextDate[MAX_DATE_SIZE]; /* next update date */
  2214. byte lastDateFormat; /* format of last date */
  2215. byte nextDateFormat; /* format of next date */
  2216. RevokedCert* certs; /* revoked cert list */
  2217. #if defined(OPENSSL_EXTRA)
  2218. byte* issuer; /* full name including common name */
  2219. word32 issuerSz; /* length of the issuer */
  2220. #endif
  2221. int totalCerts; /* number on list */
  2222. int version; /* version of cert */
  2223. void* heap;
  2224. #ifndef NO_SKID
  2225. byte extAuthKeyIdSet;
  2226. byte extAuthKeyId[SIGNER_DIGEST_SIZE]; /* Authority Key ID */
  2227. #endif
  2228. int crlNumber; /* CRL number extension */
  2229. };
  2230. WOLFSSL_LOCAL void InitDecodedCRL(DecodedCRL* dcrl, void* heap);
  2231. WOLFSSL_LOCAL int VerifyCRL_Signature(SignatureCtx* sigCtx,
  2232. const byte* toBeSigned, word32 tbsSz,
  2233. const byte* signature, word32 sigSz,
  2234. word32 signatureOID, Signer *ca,
  2235. void* heap);
  2236. WOLFSSL_LOCAL int ParseCRL(DecodedCRL* dcrl, const byte* buff, word32 sz,
  2237. int verify, void* cm);
  2238. WOLFSSL_LOCAL void FreeDecodedCRL(DecodedCRL* dcrl);
  2239. #endif /* HAVE_CRL */
  2240. #ifdef __cplusplus
  2241. } /* extern "C" */
  2242. #endif
  2243. #endif /* !NO_ASN */
  2244. #if ((defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)) \
  2245. || (defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_IMPORT)) \
  2246. || (defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)) \
  2247. || (defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_IMPORT)) \
  2248. || (defined(HAVE_PQC) && defined(HAVE_FALCON)) \
  2249. || (defined(HAVE_PQC) && defined(HAVE_DILITHIUM)) \
  2250. || (defined(HAVE_PQC) && defined(HAVE_SPHINCS)))
  2251. WOLFSSL_LOCAL int DecodeAsymKey(const byte* input, word32* inOutIdx,
  2252. word32 inSz, byte* privKey, word32* privKeyLen, byte* pubKey,
  2253. word32* pubKeyLen, int keyType);
  2254. #endif
  2255. #ifdef WC_ENABLE_ASYM_KEY_EXPORT
  2256. WOLFSSL_LOCAL int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
  2257. const byte* pubKey, word32 pubKeyLen, byte* output, word32 outLen,
  2258. int keyType);
  2259. #endif
  2260. #if !defined(NO_ASN) || !defined(NO_PWDBASED)
  2261. #ifndef PKCS_MAX_KEY_SIZE
  2262. #define PKCS_MAX_KEY_SIZE 64 /* MAX PKCS Key length */
  2263. #endif
  2264. #if !defined(WOLFSSL_GAME_BUILD) && !defined(MAX_KEY_SIZE)
  2265. /* for backwards compatibility */
  2266. #define MAX_KEY_SIZE PKCS_MAX_KEY_SIZE
  2267. #endif
  2268. #ifndef MAX_UNICODE_SZ
  2269. #define MAX_UNICODE_SZ 256
  2270. #endif
  2271. enum PBESTypes {
  2272. PBE_MD5_DES = 0,
  2273. PBE_SHA1_RC4_128 = 1,
  2274. PBE_SHA1_DES = 2,
  2275. PBE_SHA1_DES3 = 3,
  2276. PBE_AES256_CBC = 4,
  2277. PBE_AES128_CBC = 5,
  2278. PBE_SHA1_40RC2_CBC = 6,
  2279. PBE_SHA1_RC4_128_SUM = 657,
  2280. PBE_SHA1_DES3_SUM = 659,
  2281. PBE_MD5_DES_SUM = 651,
  2282. PBE_SHA1_DES_SUM = 658,
  2283. PBES2_SUM = 661,
  2284. PBES2 = 13, /* algo ID */
  2285. PBES1_MD5_DES = 3,
  2286. PBES1_SHA1_DES = 10,
  2287. };
  2288. enum PKCSTypes {
  2289. PKCS5v2 = 6, /* PKCS #5 v2.0 */
  2290. PKCS12v1 = 12, /* PKCS #12 */
  2291. PKCS5 = 5, /* PKCS oid tag */
  2292. PKCS8v0 = 0, /* default PKCS#8 version */
  2293. PKCS8v1 = 1, /* PKCS#8 version including public key */
  2294. PKCS1v0 = 0, /* default PKCS#1 version */
  2295. PKCS1v1 = 1, /* Multi-prime version */
  2296. };
  2297. #endif /* !NO_ASN || !NO_PWDBASED */
  2298. #endif /* WOLF_CRYPT_ASN_H */