asn.h 96 KB

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