ssl_asn1.c 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062
  1. /* ssl_asn1.c
  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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/internal.h>
  26. #ifndef WC_NO_RNG
  27. #include <wolfssl/wolfcrypt/random.h>
  28. #endif
  29. #if !defined(WOLFSSL_SSL_ASN1_INCLUDED)
  30. #ifndef WOLFSSL_IGNORE_FILE_WARN
  31. #warning ssl_asn1.c does not need to be compiled separately from ssl.c
  32. #endif
  33. #else
  34. /*******************************************************************************
  35. * ASN1_item APIs
  36. ******************************************************************************/
  37. #ifndef NO_ASN
  38. #ifdef OPENSSL_EXTRA
  39. #ifdef OPENSSL_ALL
  40. /* Create an ASN1 item of the specified type.
  41. *
  42. * @param [out] item Pointer to location to place new ASN1 item.
  43. * @param [in] type Type of ASN1 item to create.
  44. * @return 0 on success.
  45. * @return 1 when item type not supported.
  46. * @return 1 when item type allocation fails.
  47. */
  48. static int wolfssl_asn1_item_new(void** item, int type)
  49. {
  50. int err = 0;
  51. switch (type) {
  52. case WOLFSSL_X509_ALGOR_ASN1:
  53. *(WOLFSSL_X509_ALGOR**)item = wolfSSL_X509_ALGOR_new();
  54. break;
  55. case WOLFSSL_ASN1_BIT_STRING_ASN1:
  56. *(WOLFSSL_ASN1_BIT_STRING**)item = wolfSSL_ASN1_BIT_STRING_new();
  57. break;
  58. case WOLFSSL_ASN1_INTEGER_ASN1:
  59. *(WOLFSSL_ASN1_INTEGER**)item = wolfSSL_ASN1_INTEGER_new();
  60. break;
  61. default:
  62. WOLFSSL_MSG("Type not supported in wolfSSL_ASN1_item_new");
  63. *(void**)item = NULL;
  64. }
  65. /* Check whether an item was put in. */
  66. if (*(void**)item == NULL) {
  67. err = 1;
  68. }
  69. return err;
  70. }
  71. /* Create a new ASN1 item based on a template.
  72. *
  73. * @param [in] tpl Template of ASN1 items.
  74. * @return A new ASN1 item on success.
  75. * @return NULL when tpl is NULL, dynamic memory allocation fails or ASN1
  76. * item type not supported.
  77. */
  78. void* wolfSSL_ASN1_item_new(const WOLFSSL_ASN1_ITEM* tpl)
  79. {
  80. int err = 0;
  81. void* ret = NULL;
  82. const WOLFSSL_ASN1_TEMPLATE *mem = NULL;
  83. size_t i;
  84. WOLFSSL_ENTER("wolfSSL_ASN1_item_new");
  85. if (tpl != NULL) {
  86. ret = (void *)XMALLOC(tpl->size, NULL, DYNAMIC_TYPE_OPENSSL);
  87. }
  88. if (ret != NULL) {
  89. XMEMSET(ret, 0, tpl->size);
  90. for (mem = tpl->members, i = 0; i < tpl->mcount; mem++, i++) {
  91. if ((err = wolfssl_asn1_item_new(
  92. (void**)(((byte*)ret) + mem->offset), mem->type))) {
  93. break;
  94. }
  95. }
  96. }
  97. if (err) {
  98. wolfSSL_ASN1_item_free(ret, tpl);
  99. ret = NULL;
  100. }
  101. return ret;
  102. }
  103. /* Dispose of an ASN1 item of the specified type.
  104. *
  105. * @param [in, out] item Pointer to an anonymized ASN1 item to free.
  106. * @param [in] type Type of ASN1 item to free.
  107. */
  108. static void wolfssl_asn1_item_free(void** item, int type)
  109. {
  110. switch (type) {
  111. case WOLFSSL_X509_ALGOR_ASN1:
  112. wolfSSL_X509_ALGOR_free(*(WOLFSSL_X509_ALGOR**)item);
  113. break;
  114. case WOLFSSL_ASN1_BIT_STRING_ASN1:
  115. wolfSSL_ASN1_BIT_STRING_free(*(WOLFSSL_ASN1_BIT_STRING**)item);
  116. break;
  117. case WOLFSSL_ASN1_INTEGER_ASN1:
  118. wolfSSL_ASN1_INTEGER_free(*(WOLFSSL_ASN1_INTEGER**)item);
  119. break;
  120. default:
  121. WOLFSSL_MSG("Type not supported in wolfSSL_ASN1_item_free");
  122. }
  123. }
  124. /* Dispose of ASN1 item based on a template.
  125. *
  126. * @param [in, out] val ASN item to free.
  127. * @param [in, tpl Template of ASN1 items.
  128. */
  129. void wolfSSL_ASN1_item_free(void *items, const WOLFSSL_ASN1_ITEM *tpl)
  130. {
  131. const WOLFSSL_ASN1_TEMPLATE *mem = NULL;
  132. size_t i;
  133. WOLFSSL_ENTER("wolfSSL_ASN1_item_free");
  134. if (items != NULL) {
  135. for (mem = tpl->members, i = 0; i < tpl->mcount; mem++, i++) {
  136. wolfssl_asn1_item_free((void**)(((byte*)items) + mem->offset),
  137. mem->type);
  138. }
  139. }
  140. XFREE(items, NULL, DYNAMIC_TYPE_OPENSSL);
  141. }
  142. /* Offset buf if not NULL or NULL. */
  143. #define bufLenOrNull(buf, len) (((buf) != NULL) ? ((buf) + (len)) : NULL)
  144. /* Encode X509 algorithm as DER.
  145. *
  146. * @param [in] algor X509 algorithm object.
  147. * @param [in, out] buf Buffer to encode into. May be NULL.
  148. * @return Length of DER encoding on success.
  149. * @return 0 on failure.
  150. */
  151. static int wolfSSL_i2d_X509_ALGOR(const WOLFSSL_X509_ALGOR* algor, byte* buf)
  152. {
  153. int ret;
  154. word32 oid = 0;
  155. word32 idx = 0;
  156. if (algor->algorithm == 0) {
  157. WOLFSSL_MSG("X509_ALGOR algorithm not set");
  158. ret = 0;
  159. }
  160. else if (GetObjectId(algor->algorithm->obj, &idx, &oid,
  161. (word32)algor->algorithm->grp, algor->algorithm->objSz) < 0) {
  162. WOLFSSL_MSG("Issue getting OID of object");
  163. ret = 0;
  164. }
  165. else {
  166. ret = (int)SetAlgoID((int)oid, buf, algor->algorithm->grp, 0);
  167. }
  168. return ret;
  169. }
  170. /* Encode ASN.1 BIT_STRING as DER.
  171. *
  172. * @param [in] bit_str BIT_STRING object.
  173. * @param [in, out] buf Buffer to encode into. May be NULL.
  174. * @return Length of DER encoding on success.
  175. */
  176. static int wolfSSL_i2d_ASN1_BIT_STRING(const WOLFSSL_ASN1_BIT_STRING* bit_str,
  177. byte* buf)
  178. {
  179. int len;
  180. len = (int)SetBitString((word32)bit_str->length, 0, buf);
  181. if ((buf != NULL) && (bit_str->data != NULL)) {
  182. XMEMCPY(buf + len, bit_str->data, (size_t)bit_str->length);
  183. }
  184. return len + bit_str->length;
  185. }
  186. /* Encode ASN item as DER.
  187. *
  188. * @param [in] item Pointer to anonymized ASN item.
  189. * @param [in, out] buf Buffer to encode into. May be NULL.
  190. * @return Length of DER encoding on success.
  191. * @return 0 on failure.
  192. */
  193. static int wolfssl_i2d_asn1_item(void** item, int type, byte* buf)
  194. {
  195. int len;
  196. switch (type) {
  197. case WOLFSSL_X509_ALGOR_ASN1:
  198. len = wolfSSL_i2d_X509_ALGOR(*(const WOLFSSL_X509_ALGOR**)item,
  199. buf);
  200. break;
  201. case WOLFSSL_ASN1_BIT_STRING_ASN1:
  202. len = wolfSSL_i2d_ASN1_BIT_STRING(
  203. *(const WOLFSSL_ASN1_BIT_STRING**)item, buf);
  204. break;
  205. case WOLFSSL_ASN1_INTEGER_ASN1:
  206. {
  207. byte *tmp_buf = buf;
  208. len = wolfSSL_i2d_ASN1_INTEGER(
  209. *(const WOLFSSL_ASN1_INTEGER**)item, &tmp_buf);
  210. if ((buf == NULL) && (tmp_buf != NULL)) {
  211. XFREE(tmp_buf, NULL, DYNAMIC_TYPE_ASN1);
  212. tmp_buf = NULL;
  213. }
  214. }
  215. break;
  216. default:
  217. WOLFSSL_MSG("Type not support in processMembers");
  218. len = 0;
  219. }
  220. return len;
  221. }
  222. /* Encode members of an ASN.1 SEQUENCE as DER.
  223. *
  224. * @param [in] src ASN1 items to encode.
  225. * @param [in, out] buf Buffer to encode into. May be NULL.
  226. * @param [in] members ASN1 template members.
  227. * @param [in] mcount Count of template members.
  228. * @return Length of DER encoding on success.
  229. * @return 0 on failure.
  230. */
  231. static int wolfssl_i2d_asn1_items(const void* src, byte*buf,
  232. const WOLFSSL_ASN1_TEMPLATE* members, size_t mcount)
  233. {
  234. const WOLFSSL_ASN1_TEMPLATE* mem = NULL;
  235. int len = 0;
  236. int ret;
  237. size_t i;
  238. WOLFSSL_ENTER("wolfssl_i2d_asn1_items");
  239. for (mem = members, i = 0; i < mcount; mem++, i++) {
  240. ret = wolfssl_i2d_asn1_item((void**)(((byte*)src) + mem->offset),
  241. mem->type, bufLenOrNull(buf, len));
  242. if (ret == 0) {
  243. len = 0;
  244. break;
  245. }
  246. len += ret;
  247. }
  248. WOLFSSL_LEAVE("wolfssl_i2d_asn1_items", len);
  249. return len;
  250. }
  251. /* Encode sequence and items under it.
  252. *
  253. * @param [in] src ASN1 items to encode.
  254. * @param [in, out] buf Buffer to encode into. May be NULL.
  255. * @param [in] tpl Template of ASN1 items.
  256. * @return Length of DER encoding on success.
  257. * @return 0 on failure.
  258. */
  259. static int i2d_ASN_SEQUENCE(const void* src, byte* buf,
  260. const WOLFSSL_ASN1_ITEM* tpl)
  261. {
  262. word32 seq_len;
  263. word32 len = 0;
  264. seq_len = (word32)wolfssl_i2d_asn1_items(src, NULL, tpl->members,
  265. tpl->mcount);
  266. if (seq_len != 0) {
  267. len = SetSequence(seq_len, buf);
  268. if (buf != NULL) {
  269. wolfssl_i2d_asn1_items(src, buf + len, tpl->members, tpl->mcount);
  270. }
  271. len += seq_len;
  272. }
  273. return (int)len;
  274. }
  275. /* Encode ASN1 template item.
  276. *
  277. * @param [in] src ASN1 items to encode.
  278. * @param [in, out] buf Buffer to encode into. May be NULL.
  279. * @param [in] tpl Template of ASN1 items.
  280. * @return Length of DER encoding on success.
  281. * @return 0 on failure.
  282. */
  283. static int wolfssl_asn1_item_encode(const void* src, byte* buf,
  284. const WOLFSSL_ASN1_ITEM* tpl)
  285. {
  286. int len;
  287. switch (tpl->type) {
  288. case ASN_SEQUENCE:
  289. len = i2d_ASN_SEQUENCE(src, buf, tpl);
  290. break;
  291. default:
  292. WOLFSSL_MSG("Type not supported in wolfSSL_ASN1_item_i2d");
  293. len = 0;
  294. }
  295. return len;
  296. }
  297. /* Encode ASN1 template.
  298. *
  299. * @param [in] src ASN1 items to encode.
  300. * @param [in, out] dest Pointer to buffer to encode into. May be NULL.
  301. * @param [in] tpl Template of ASN1 items.
  302. * @return Length of DER encoding on success.
  303. * @return 0 on failure.
  304. */
  305. int wolfSSL_ASN1_item_i2d(const void* src, byte** dest,
  306. const WOLFSSL_ASN1_ITEM* tpl)
  307. {
  308. int ret = 1;
  309. int len = 0;
  310. byte* buf = NULL;
  311. WOLFSSL_ENTER("wolfSSL_ASN1_item_i2d");
  312. /* Validate parameters. */
  313. if ((src == NULL) || (tpl == NULL)) {
  314. ret = 0;
  315. }
  316. if ((ret == 1) && ((len = wolfssl_asn1_item_encode(src, NULL, tpl)) == 0)) {
  317. ret = 0;
  318. }
  319. if ((ret == 1) && (dest != NULL)) {
  320. if (*dest == NULL) {
  321. buf = (byte*)XMALLOC((size_t)len, NULL, DYNAMIC_TYPE_ASN1);
  322. if (buf == NULL)
  323. ret = 0;
  324. *dest = buf;
  325. }
  326. if (ret == 1) {
  327. len = wolfssl_asn1_item_encode(src, *dest, tpl);
  328. }
  329. }
  330. if (ret == 0) {
  331. XFREE(buf, NULL, DYNAMIC_TYPE_ASN1);
  332. len = 0;
  333. }
  334. WOLFSSL_LEAVE("wolfSSL_ASN1_item_i2d", len);
  335. return len;
  336. }
  337. #endif /* OPENSSL_ALL */
  338. #endif /* OPENSSL_EXTRA */
  339. /*******************************************************************************
  340. * ASN1_BIT_STRING APIs
  341. ******************************************************************************/
  342. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  343. /* Create a new ASN.1 BIT_STRING object.
  344. *
  345. * @return ASN.1 BIT_STRING object on success.
  346. * @return NULL when dynamic memory allocation fails.
  347. */
  348. WOLFSSL_ASN1_BIT_STRING* wolfSSL_ASN1_BIT_STRING_new(void)
  349. {
  350. WOLFSSL_ASN1_BIT_STRING* bitStr;
  351. bitStr = (WOLFSSL_ASN1_BIT_STRING*)XMALLOC(sizeof(WOLFSSL_ASN1_BIT_STRING),
  352. NULL, DYNAMIC_TYPE_OPENSSL);
  353. if (bitStr) {
  354. XMEMSET(bitStr, 0, sizeof(WOLFSSL_ASN1_BIT_STRING));
  355. }
  356. return bitStr;
  357. }
  358. /* Dispose of ASN.1 BIT_STRING object.
  359. *
  360. * Do not use bitStr after calling this function.
  361. *
  362. * @param [in, out] bitStr ASN.1 BIT_STRING to free. May be NULL.
  363. */
  364. void wolfSSL_ASN1_BIT_STRING_free(WOLFSSL_ASN1_BIT_STRING* bitStr)
  365. {
  366. if (bitStr != NULL) {
  367. /* Dispose of any data allocated in BIT_STRING. */
  368. XFREE(bitStr->data, NULL, DYNAMIC_TYPE_OPENSSL);
  369. }
  370. /* Dispose of the ASN.1 BIT_STRING object. */
  371. XFREE(bitStr, NULL, DYNAMIC_TYPE_OPENSSL);
  372. }
  373. /* Get the value of the bit from the ASN.1 BIT_STRING at specified index.
  374. *
  375. * A NULL object a value of 0 for the bit at all indices.
  376. * A negative index has a value of 0 for the bit.
  377. *
  378. * @param [in] bitStr ASN.1 BIT_STRING object.
  379. * @param [in] i Index of bit.
  380. * @return Value of bit.
  381. */
  382. int wolfSSL_ASN1_BIT_STRING_get_bit(const WOLFSSL_ASN1_BIT_STRING* bitStr,
  383. int i)
  384. {
  385. int bit = 0;
  386. /* Check for data and whether index is in range. */
  387. if ((bitStr != NULL) && (bitStr->data != NULL) && (i >= 0) &&
  388. (bitStr->length > (i / 8))) {
  389. bit = (bitStr->data[i / 8] & (1 << (7 - (i % 8)))) ? 1 : 0;
  390. }
  391. return bit;
  392. }
  393. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  394. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  395. /* Grow data to require length.
  396. *
  397. * @param [in] bitStr ASN.1 BIT_STRING object.
  398. * @param [in] len Length, in bytes, of data required.
  399. * @return 1 on success.
  400. * @return 0 when dynamic memory allocation fails.
  401. */
  402. static int wolfssl_asn1_bit_string_grow(WOLFSSL_ASN1_BIT_STRING* bitStr,
  403. int len)
  404. {
  405. int ret = 1;
  406. byte* tmp;
  407. /* Realloc to length required. */
  408. tmp = (byte*)XREALLOC(bitStr->data, (size_t)len, NULL,
  409. DYNAMIC_TYPE_OPENSSL);
  410. if (tmp == NULL) {
  411. ret = 0;
  412. }
  413. else {
  414. /* Clear out new, top bytes. */
  415. XMEMSET(tmp + bitStr->length, 0, (size_t)(len - bitStr->length));
  416. bitStr->data = tmp;
  417. bitStr->length = len;
  418. }
  419. return ret;
  420. }
  421. /* Set the value of a bit in the ASN.1 BIT_STRING at specified index.
  422. *
  423. * @param [in] bitStr ASN.1 BIT_STRING object.
  424. * @param [in] idx Index of bit to set.
  425. * @param [in] val Value of bit to set. Valid values: 0 or 1.
  426. * @return 1 on success.
  427. * @return 0 when bitStr is NULL, idx is negative, val is not 0 or 1, or
  428. * dynamic memory allocation fails.
  429. */
  430. int wolfSSL_ASN1_BIT_STRING_set_bit(WOLFSSL_ASN1_BIT_STRING* bitStr, int idx,
  431. int val)
  432. {
  433. int ret = 1;
  434. int i = 0;
  435. /* Validate parameters. */
  436. if ((bitStr == NULL) || (idx < 0) || ((val != 0) && (val != 1))) {
  437. ret = 0;
  438. }
  439. if (ret == 1) {
  440. i = idx / 8;
  441. /* Check if we need to extend data range. */
  442. if ((i >= bitStr->length) && (val != 0)) {
  443. /* Realloc data to handle having bit set at index. */
  444. ret = wolfssl_asn1_bit_string_grow(bitStr, i + 1);
  445. }
  446. }
  447. if ((ret == 1) && (i < bitStr->length)) {
  448. /* Bit on at index. */
  449. byte bit = 1 << (7 - (idx % 8));
  450. /* Clear bit and set to value. */
  451. bitStr->data[i] &= ~bit;
  452. bitStr->data[i] |= bit & (byte)(0 - val);
  453. }
  454. return ret;
  455. }
  456. #endif /* OPENSSL_ALL && !NO_CERTS */
  457. /*******************************************************************************
  458. * ASN1_INTEGER APIs
  459. ******************************************************************************/
  460. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  461. /* Create a new empty ASN.1 INTEGER object.
  462. *
  463. * @return ASN.1 INTEGER object on success.
  464. * @return NULL when dynamic memory allocation fails.
  465. */
  466. WOLFSSL_ASN1_INTEGER* wolfSSL_ASN1_INTEGER_new(void)
  467. {
  468. WOLFSSL_ASN1_INTEGER* a;
  469. /* Allocate a new ASN.1 INTEGER object. */
  470. a = (WOLFSSL_ASN1_INTEGER*)XMALLOC(sizeof(WOLFSSL_ASN1_INTEGER), NULL,
  471. DYNAMIC_TYPE_OPENSSL);
  472. if (a != NULL) {
  473. XMEMSET(a, 0, sizeof(WOLFSSL_ASN1_INTEGER));
  474. /* Use fixed buffer field for data. */
  475. a->data = a->intData;
  476. a->isDynamic = 0;
  477. /* Maximum supported by fixed buffer. */
  478. a->dataMax = WOLFSSL_ASN1_INTEGER_MAX;
  479. /* No value set - no data. */
  480. a->length = 0;
  481. }
  482. return a;
  483. }
  484. /* Free the ASN.1 INTEGER object and any dynamically allocated data.
  485. *
  486. * @param [in, out] in ASN.1 INTEGER object.
  487. */
  488. void wolfSSL_ASN1_INTEGER_free(WOLFSSL_ASN1_INTEGER* in)
  489. {
  490. if ((in != NULL) && (in->isDynamic)) {
  491. /* Dispose of any data allocated in INTEGER. */
  492. XFREE(in->data, NULL, DYNAMIC_TYPE_OPENSSL);
  493. }
  494. /* Dispose of the ASN.1 INTEGER object. */
  495. XFREE(in, NULL, DYNAMIC_TYPE_OPENSSL);
  496. }
  497. #if defined(OPENSSL_EXTRA)
  498. /* Reset the data of ASN.1 INTEGER object back to empty fixed array.
  499. *
  500. * @param [in] a ASN.1 INTEGER object.
  501. */
  502. static void wolfssl_asn1_integer_reset_data(WOLFSSL_ASN1_INTEGER* a)
  503. {
  504. /* Don't use dynamic buffer anymore. */
  505. if (a->isDynamic) {
  506. /* Cache pointer to allocated data. */
  507. unsigned char* data = a->data;
  508. /* No longer dynamic. */
  509. a->isDynamic = 0;
  510. /* Point data at fixed array. */
  511. a->data = a->intData;
  512. /* Set maximum length to fixed array size. */
  513. a->dataMax = (unsigned int)sizeof(a->intData);
  514. /* Dispose of dynamically allocated data. */
  515. XFREE(data, NULL, DYNAMIC_TYPE_OPENSSL);
  516. }
  517. /* Clear out data from fixed array. */
  518. XMEMSET(a->intData, 0, sizeof(a->intData));
  519. /* No data, no length. */
  520. a->length = 0;
  521. /* No data, not negative. */
  522. a->negative = 0;
  523. /* Set type to positive INTEGER. */
  524. a->type = V_ASN1_INTEGER;
  525. }
  526. #endif /* OPENSSL_EXTRA */
  527. /* Setup ASN.1 INTEGER object to handle data of required length.
  528. *
  529. * @param [in, out] a ASN.1 INTEGER object.
  530. * @param [in] len Required length in bytes.
  531. * @return 1 on success.
  532. * @return 0 on dynamic memory allocation failure.
  533. */
  534. static int wolfssl_asn1_integer_require_len(WOLFSSL_ASN1_INTEGER* a, int len,
  535. int keepOldData)
  536. {
  537. int ret = 1;
  538. byte* data;
  539. byte* oldData = a->intData;
  540. int oldLen = a->length;
  541. if (a->isDynamic && (len > (int)a->dataMax)) {
  542. oldData = a->data;
  543. a->isDynamic = 0;
  544. a->data = a->intData;
  545. a->dataMax = (unsigned int)sizeof(a->intData);
  546. }
  547. a->length = 0;
  548. if ((!a->isDynamic) && (len > (int)a->dataMax)) {
  549. /* Create a new buffer to hold large integer value. */
  550. data = (byte*)XMALLOC((size_t)len, NULL, DYNAMIC_TYPE_OPENSSL);
  551. if (data == NULL) {
  552. ret = 0;
  553. }
  554. else {
  555. /* Indicate data is dynamic and copy data over. */
  556. a->isDynamic = 1;
  557. a->data = data;
  558. a->dataMax = (word32)len;
  559. }
  560. }
  561. if (keepOldData) {
  562. if (oldData != a->data) {
  563. /* Copy old data into new buffer. */
  564. XMEMCPY(a->data, oldData, (size_t)oldLen);
  565. }
  566. /* Restore old length. */
  567. a->length = oldLen;
  568. }
  569. if (oldData != a->intData) {
  570. /* Dispose of the old dynamic data. */
  571. XFREE(oldData, NULL, DYNAMIC_TYPE_OPENSSL);
  572. }
  573. return ret;
  574. }
  575. /* Duplicate the ASN.1 INTEGER object into a newly allocated one.
  576. *
  577. * @param [in] src ASN.1 INTEGER object to copy.
  578. * @return ASN.1 INTEGER object on success.
  579. * @return NULL when src is NULL or dynamic memory allocation fails.
  580. */
  581. WOLFSSL_ASN1_INTEGER* wolfSSL_ASN1_INTEGER_dup(const WOLFSSL_ASN1_INTEGER* src)
  582. {
  583. WOLFSSL_ASN1_INTEGER* dup = NULL;
  584. WOLFSSL_ENTER("wolfSSL_ASN1_INTEGER_dup");
  585. /* Check for object to duplicate. */
  586. if (src != NULL) {
  587. /* Create a new ASN.1 INTEGER object to be copied into. */
  588. dup = wolfSSL_ASN1_INTEGER_new();
  589. }
  590. /* Check for object to copy into. */
  591. if (dup != NULL) {
  592. /* Copy simple fields. */
  593. dup->length = src->length;
  594. dup->negative = src->negative;
  595. dup->type = src->type;
  596. if (!src->isDynamic) {
  597. /* Copy over data from/to fixed buffer. */
  598. XMEMCPY(dup->intData, src->intData, WOLFSSL_ASN1_INTEGER_MAX);
  599. }
  600. else if (wolfssl_asn1_integer_require_len(dup, src->length, 0) == 0) {
  601. wolfSSL_ASN1_INTEGER_free(dup);
  602. dup = NULL;
  603. }
  604. else {
  605. XMEMCPY(dup->data, src->data, (size_t)src->length);
  606. }
  607. }
  608. return dup;
  609. }
  610. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  611. #if defined(OPENSSL_EXTRA)
  612. /* Compare values in two ASN.1 INTEGER objects.
  613. *
  614. * @param [in] a First ASN.1 INTEGER object.
  615. * @param [in] b Second ASN.1 INTEGER object.
  616. * @return Negative value when a is less than b.
  617. * @return 0 when a equals b.
  618. * @return Positive value when a is greater than b.
  619. * @return -1 when a or b is NULL.
  620. */
  621. int wolfSSL_ASN1_INTEGER_cmp(const WOLFSSL_ASN1_INTEGER* a,
  622. const WOLFSSL_ASN1_INTEGER* b)
  623. {
  624. int ret = 0;
  625. WOLFSSL_ENTER("wolfSSL_ASN1_INTEGER_cmp");
  626. /* Validate parameters. */
  627. if ((a == NULL) || (b == NULL)) {
  628. WOLFSSL_MSG("Bad parameter.");
  629. ret = -1;
  630. }
  631. /* Negative value < Positive value */
  632. else if (a->negative && !b->negative) {
  633. ret = -1;
  634. }
  635. /* Positive value > Negative value */
  636. else if (!a->negative && b->negative) {
  637. ret = 1;
  638. }
  639. else {
  640. /* Check for difference in length. */
  641. if (a->length != b->length) {
  642. ret = a->length - b->length;
  643. }
  644. else {
  645. /* Compare data given they are the same length. */
  646. ret = XMEMCMP(a->data, b->data, (size_t)a->length);
  647. }
  648. /* Reverse comparison result when both negative. */
  649. if (a->negative) {
  650. ret = -ret;
  651. }
  652. }
  653. WOLFSSL_LEAVE("wolfSSL_ASN1_INTEGER_cmp", ret);
  654. return ret;
  655. }
  656. /* Calculate 2's complement of DER encoding.
  657. *
  658. * @param [in] data Array that is number.
  659. * @param [in] length Number of bytes in array.
  660. * @return 0 on success.
  661. * @return -1 when get length from DER header failed.
  662. */
  663. static void wolfssl_twos_compl(byte* data, int length)
  664. {
  665. int i;
  666. /* Invert bits - 1's complement. */
  667. for (i = 0; i < length; ++i) {
  668. data[i] = ~data[i];
  669. }
  670. /* 2's complement - add 1. */
  671. for (i = length - 1; (++data[i]) == 0; --i) {
  672. /* Do nothing. */
  673. }
  674. }
  675. /* Calculate 2's complement of DER encoding.
  676. *
  677. * @param [in] data Array that is number.
  678. * @param [in] length Number of bytes in array.
  679. * @param [out] neg When NULL, 2's complement data.
  680. * When not NULL, check for negative first and return.
  681. * @return 0 on success.
  682. * @return -1 when get length from DER header failed.
  683. */
  684. static int wolfssl_asn1_int_twos_compl(byte* data, int length, byte* neg)
  685. {
  686. int ret = 0;
  687. word32 idx = 1; /* Skip tag. */
  688. int len;
  689. /* Get length from DER header. */
  690. if (GetLength(data, &idx, &len, (word32)length) < 0) {
  691. ret = -1;
  692. }
  693. else {
  694. if (neg != NULL) {
  695. *neg = data[idx] & 0x80;
  696. }
  697. if ((neg == NULL) || (*neg != 0)) {
  698. wolfssl_twos_compl(data + idx, length - (int)idx);
  699. }
  700. }
  701. return ret;
  702. }
  703. /* Encode ASN.1 INTEGER as DER without tag.
  704. *
  705. * When out points to NULL, a new buffer is allocated and returned.
  706. *
  707. * @param [in] a ASN.1 INTEGER object.
  708. * @param [in, out] out Pointer to buffer to hold encoding. May point to NULL.
  709. * @return Length of encoding on success.
  710. * @return -1 when a is NULL or no data, out is NULL, dynamic memory allocation
  711. * fails or encoding length fails.
  712. */
  713. int wolfSSL_i2d_ASN1_INTEGER(const WOLFSSL_ASN1_INTEGER* a, unsigned char** out)
  714. {
  715. int ret = 0;
  716. byte* buf = NULL;
  717. WOLFSSL_ENTER("wolfSSL_i2d_ASN1_INTEGER");
  718. /* Validate parameters. */
  719. if ((a == NULL) || (a->data == NULL) || (a->length <= 0) || (out == NULL)) {
  720. WOLFSSL_MSG("Bad parameter.");
  721. ret = -1;
  722. }
  723. if ((ret == 0) && (*out == NULL)) {
  724. /* Allocate buffer to hold encoding. */
  725. buf = (unsigned char*)XMALLOC((size_t)a->length, NULL,
  726. DYNAMIC_TYPE_ASN1);
  727. if (buf == NULL) {
  728. WOLFSSL_MSG("Failed to allocate output buffer.");
  729. ret = -1;
  730. }
  731. /* Return any allocated buffer. */
  732. *out = buf;
  733. }
  734. if (ret == 0) {
  735. /* Copy the data (including tag and length) into output buffer. */
  736. XMEMCPY(*out, a->data, (size_t)a->length);
  737. /* Only magnitude of the number stored (i.e. the sign isn't encoded).
  738. * The "negative" field is 1 if the value must be interpreted as
  739. * negative and we need to output the 2's complement of the value in
  740. * the DER output.
  741. */
  742. if (a->negative) {
  743. ret = wolfssl_asn1_int_twos_compl(*out, a->length, NULL);
  744. }
  745. }
  746. if (ret == 0) {
  747. ret = a->length;
  748. /* Move pointer on passed encoding when buffer passed in. */
  749. if (buf == NULL) {
  750. *out += a->length;
  751. }
  752. }
  753. /* Dispose of any dynamically allocated data on error. */
  754. else if (buf != NULL) {
  755. /* Dispose of buffer allocated locally on error. */
  756. XFREE(buf, NULL, DYNAMIC_TYPE_ASN1);
  757. /* Don't return freed buffer. */
  758. *out = NULL;
  759. }
  760. WOLFSSL_LEAVE("wolfSSL_i2d_ASN1_INTEGER", ret);
  761. return ret;
  762. }
  763. /* Decode DER encoding of ASN.1 INTEGER.
  764. *
  765. * @param [out] a ASN.1 INTEGER object. May be NULL.
  766. * @param [in, out] in Pointer to buffer containing DER encoding.
  767. * @param [in] inSz Length of data in buffer.
  768. * @return ASN.1 INTEGER object on success.
  769. * @return NULL when in or *in is NULL, inSz is less than or equal to 2 or
  770. * parsing DER failed.
  771. */
  772. WOLFSSL_ASN1_INTEGER* wolfSSL_d2i_ASN1_INTEGER(WOLFSSL_ASN1_INTEGER** a,
  773. const unsigned char** in, long inSz)
  774. {
  775. WOLFSSL_ASN1_INTEGER* ret = NULL;
  776. int err = 0;
  777. word32 idx = 1;
  778. int len = 0;
  779. WOLFSSL_ENTER("wolfSSL_d2i_ASN1_INTEGER");
  780. /* Validate parameters. */
  781. if ((in == NULL) || (*in == NULL) || (inSz <= 2)) {
  782. WOLFSSL_MSG("Bad parameter");
  783. err = 1;
  784. }
  785. /* Check that the tag is correct. */
  786. if ((!err) && (*in)[0] != ASN_INTEGER) {
  787. WOLFSSL_MSG("Tag doesn't indicate integer type.");
  788. err = 1;
  789. }
  790. /* Check that length and use this instead of inSz. */
  791. if ((!err) && (GetLength(*in, &idx, &len, (word32)inSz) <= 0)) {
  792. WOLFSSL_MSG("ASN.1 length not valid.");
  793. err = 1;
  794. }
  795. /* Allocate a new ASN.1 INTEGER object. */
  796. if ((!err) && ((ret = wolfSSL_ASN1_INTEGER_new()) == NULL)) {
  797. err = 1;
  798. }
  799. if ((!err) && (wolfssl_asn1_integer_require_len(ret, (int)idx + len, 0) !=
  800. 1)) {
  801. err = 1;
  802. }
  803. if (!err) {
  804. /* Set type. */
  805. ret->type = V_ASN1_INTEGER;
  806. /* Copy DER encoding and length. */
  807. XMEMCPY(ret->data, *in, (size_t)(idx + (word32)len));
  808. ret->length = (int)idx + len;
  809. /* Do 2's complement if number is negative. */
  810. if (wolfssl_asn1_int_twos_compl(ret->data, ret->length, &ret->negative)
  811. != 0) {
  812. err = 1;
  813. }
  814. }
  815. if ((!err) && ret->negative) {
  816. /* Update type if number was negative. */
  817. ret->type |= V_ASN1_NEG_INTEGER;
  818. }
  819. if (err) {
  820. /* Dispose of dynamically allocated data on error. */
  821. wolfSSL_ASN1_INTEGER_free(ret);
  822. ret = NULL;
  823. }
  824. else {
  825. if (a != NULL) {
  826. /* Return ASN.1 INTEGER through a. */
  827. *a = ret;
  828. }
  829. *in += ret->length;
  830. }
  831. return ret;
  832. }
  833. #ifndef NO_BIO
  834. /* Get length of leading hexadecimal characters.
  835. *
  836. * Looks for continuation character before carriage returns and line feeds.
  837. *
  838. * @param [in] str String with input.
  839. * @param [in] len Length of string.
  840. * @param [out] cont Line continuation character at end of line before
  841. * carriage returns and line feeds.
  842. * @return Number of leading hexadecimal characters in string.
  843. */
  844. static int wolfssl_a2i_asn1_integer_clear_to_eol(char* str, int len, int* cont)
  845. {
  846. byte num;
  847. word32 nLen;
  848. int i;
  849. /* Strip off trailing carriage returns and line feeds. */
  850. while ((len > 0) && ((str[len - 1] == '\n') || (str[len - 1] == '\r'))) {
  851. len--;
  852. }
  853. /* Check for line continuation character. */
  854. if ((len > 0) && (str[len - 1] == '\\')) {
  855. *cont = 1;
  856. len--;
  857. }
  858. else {
  859. *cont = 0;
  860. }
  861. /* Find end of hexadecimal characters. */
  862. nLen = 1;
  863. for (i = 0; i < len; i++) {
  864. /* Check if character is a hexadecimal character. */
  865. if (Base16_Decode((const byte*)str + i, 1, &num, &nLen) == ASN_INPUT_E)
  866. {
  867. /* Found end of hexadecimal characters, return count. */
  868. len = i;
  869. break;
  870. }
  871. }
  872. return len;
  873. }
  874. /* Read number from BIO as a string.
  875. *
  876. * Line continuation character at end of line means next line must be read.
  877. *
  878. * @param [in] bio BIO to read from.
  879. * @param [in] asn1 ASN.1 INTEGER object to put number into.
  880. * @param [in, out] buf Buffer to use when reading.
  881. * @param [in] size Length of buffer in bytes.
  882. * @return 1 on success.
  883. * @return 0 on failure.
  884. */
  885. int wolfSSL_a2i_ASN1_INTEGER(WOLFSSL_BIO *bio, WOLFSSL_ASN1_INTEGER *asn1,
  886. char *buf, int size)
  887. {
  888. int ret = 1;
  889. int readNextLine = 1;
  890. int len;
  891. word32 outLen = 0;
  892. const int hdrSz = 1 + MAX_LENGTH_SZ;
  893. WOLFSSL_ENTER("wolfSSL_a2i_ASN1_INTEGER");
  894. if ((bio == NULL) || (asn1 == NULL) || (buf == NULL) || (size <= 0)) {
  895. WOLFSSL_MSG("Bad parameter");
  896. ret = 0;
  897. }
  898. while ((ret == 1) && readNextLine) {
  899. int lineLen;
  900. /* Assume we won't be reading any more. */
  901. readNextLine = 0;
  902. /* Read a line. */
  903. lineLen = wolfSSL_BIO_gets(bio, buf, size);
  904. if (lineLen <= 0) {
  905. WOLFSSL_MSG("wolfSSL_BIO_gets error");
  906. ret = 0;
  907. }
  908. if (ret == 1) {
  909. /* Find length of hexadecimal digits in string. */
  910. lineLen = wolfssl_a2i_asn1_integer_clear_to_eol(buf, lineLen,
  911. &readNextLine);
  912. /* Check we have a valid hexadecimal string to process. */
  913. if ((lineLen == 0) || ((lineLen % 2) != 0)) {
  914. WOLFSSL_MSG("Invalid line length");
  915. ret = 0;
  916. }
  917. }
  918. if (ret == 1) {
  919. /* Calculate length of complete number so far. */
  920. len = asn1->length + (lineLen / 2);
  921. /* Make sure enough space for number and maximum header. */
  922. if (wolfssl_asn1_integer_require_len(asn1, len + hdrSz, outLen != 0)
  923. != 1) {
  924. ret = 0;
  925. }
  926. }
  927. if (ret == 1) {
  928. /* Decode string and append to data. */
  929. outLen = (word32)(lineLen / 2);
  930. (void)Base16_Decode((byte*)buf, (word32)lineLen,
  931. asn1->data + asn1->length, &outLen);
  932. /* Update length of data. */
  933. asn1->length += (int)outLen;
  934. }
  935. }
  936. if (ret == 1) {
  937. int idx;
  938. /* Get ASN.1 header length. */
  939. idx = SetASNInt(asn1->length, asn1->data[0], NULL);
  940. /* Move data to be after ASN.1 header. */
  941. XMEMMOVE(asn1->data + idx, asn1->data, (size_t)asn1->length);
  942. /* Encode ASN.1 header. */
  943. SetASNInt(asn1->length, asn1->data[idx], asn1->data);
  944. /* Update length of data. */
  945. asn1->length += idx;
  946. }
  947. return ret;
  948. }
  949. /* Write out number in ASN.1 INTEGER object to BIO as string.
  950. *
  951. * @param [in] bp BIO to write to.
  952. * @param [in] a ASN.1 INTEGER object.
  953. * @return Number of characters written on success.
  954. * @return 0 when bp or a is NULL.
  955. * @return 0 DER header in data is invalid.
  956. */
  957. int wolfSSL_i2a_ASN1_INTEGER(BIO *bp, const WOLFSSL_ASN1_INTEGER *a)
  958. {
  959. int err = 0;
  960. word32 idx = 1; /* Skip ASN.1 INTEGER tag byte. */
  961. int len = 0;
  962. byte buf[WOLFSSL_ASN1_INTEGER_MAX * 2 + 1];
  963. word32 bufLen;
  964. WOLFSSL_ENTER("wolfSSL_i2a_ASN1_INTEGER");
  965. /* Validate parameters. */
  966. if ((bp == NULL) || (a == NULL)) {
  967. err = 1;
  968. }
  969. if (!err) {
  970. /* Read DER length - must be at least 1 byte. */
  971. if (GetLength(a->data, &idx, &len, (word32)a->length) <= 0) {
  972. err = 1;
  973. }
  974. }
  975. /* Keep encoding and writing while no error and bytes in data. */
  976. while ((!err) && (idx < (word32)a->length)) {
  977. /* Number of bytes left to encode. */
  978. int encLen = a->length - (int)idx;
  979. /* Reduce to maximum buffer size if necessary. */
  980. if (encLen > (int)sizeof(buf) / 2) {
  981. encLen = (int)sizeof(buf) / 2;
  982. }
  983. /* Encode bytes from data into buffer. */
  984. bufLen = (int)sizeof(buf);
  985. (void)Base16_Encode(a->data + idx, (word32)encLen, buf, &bufLen);
  986. /* Update index to next bytes to encoded. */
  987. idx += (word32)encLen;
  988. /* Write out characters but not NUL char. */
  989. if (wolfSSL_BIO_write(bp, buf, (int)bufLen - 1) != (int)(bufLen - 1)) {
  990. err = 1;
  991. }
  992. }
  993. if (err) {
  994. /* Return 0 on error. */
  995. len = 0;
  996. }
  997. /* Return total number of characters written. */
  998. return len * 2;
  999. }
  1000. #endif /* !NO_BIO */
  1001. #ifndef NO_ASN
  1002. /* Determine if a pad byte is required and its value for a number.
  1003. *
  1004. * Assumes values pointed to by pad and padVal are both 0.
  1005. *
  1006. * @param [in] data Number encoded as big-endian bytes.
  1007. * @param [in] len Length of number in bytes.
  1008. * @param [in, out] neg Indicates number is negative.
  1009. * @param [out] pad Number of padding bytes required.
  1010. * @param [out] padVal Padding byte to prepend.
  1011. */
  1012. static void wolfssl_asn1_integer_pad(unsigned char* data, int len,
  1013. unsigned char* neg, char* pad, unsigned char* padVal)
  1014. {
  1015. /* Check for empty data. */
  1016. if (len == 0) {
  1017. *pad = 1;
  1018. *padVal = 0x00;
  1019. *neg = 0;
  1020. }
  1021. else {
  1022. /* Get first, most significant, byte of encoded number. */
  1023. unsigned char firstByte = data[0];
  1024. /* 0 can't be negative. */
  1025. if ((len == 1) && (firstByte == 0x00)) {
  1026. *neg = 0;
  1027. }
  1028. /* Positive value must not have top bit of first byte set. */
  1029. if ((!*neg) && (firstByte >= 0x80)) {
  1030. *pad = 1;
  1031. *padVal = 0x00;
  1032. }
  1033. /* Negative numbers are two's complemented.
  1034. * Two's complement value must have top bit set.
  1035. */
  1036. else if (*neg && (firstByte > 0x80)) {
  1037. *pad = 1;
  1038. *padVal = 0xff;
  1039. }
  1040. /* Checking for: 0x80[00]*
  1041. * when negative that when two's complemented will be: 0x80[00]*
  1042. * and therefore doesn't require pad byte.
  1043. */
  1044. else if (*neg && (firstByte == 0x80)) {
  1045. int i;
  1046. /* Check rest of bytes. */
  1047. for (i = 1; i < len; i++) {
  1048. if (data[i] != 0x00) {
  1049. /* Not 0x80[00]* */
  1050. *pad = 1;
  1051. *padVal = 0xff;
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. }
  1057. }
  1058. /* Convert ASN.1 INTEGER object into content octets.
  1059. *
  1060. * TODO: compatibility with OpenSSL? OpenSSL assumes data not DER encoded.
  1061. *
  1062. * When pp points to a buffer, on success pp will point to after the encoded
  1063. * data.
  1064. *
  1065. * @param [in] a ASN.1 INTEGER object.
  1066. * @param [in, out] pp Pointer to buffer. May be NULL. Cannot point to NULL.
  1067. * @return Length of encoding on success.
  1068. * @return 0 when a is NULL, pp points to NULL or DER length encoding invalid.
  1069. */
  1070. int wolfSSL_i2c_ASN1_INTEGER(WOLFSSL_ASN1_INTEGER *a, unsigned char **pp)
  1071. {
  1072. int err = 0;
  1073. int len = 0;
  1074. char pad = 0;
  1075. unsigned char padVal = 0;
  1076. word32 idx = 1;
  1077. WOLFSSL_ENTER("wolfSSL_i2c_ASN1_INTEGER");
  1078. /* Validate parameters. */
  1079. if ((a == NULL) || ((pp != NULL) && (*pp == NULL))) {
  1080. err = 1;
  1081. }
  1082. /* Get length from DER encoding. */
  1083. if ((!err) && (GetLength_ex(a->data, &idx, &len, a->dataMax, 0) < 0)) {
  1084. err = 1;
  1085. }
  1086. if (!err) {
  1087. /* Determine pad length and value. */
  1088. wolfssl_asn1_integer_pad(a->data + idx, len, &a->negative, &pad,
  1089. &padVal);
  1090. /* Total encoded length is number length plus one when padding. */
  1091. len += (int)pad;
  1092. }
  1093. /* Check buffer supplied to write into. */
  1094. if ((!err) && (pp != NULL)) {
  1095. /* Put in any pad byte. */
  1096. if (pad) {
  1097. (*pp)[0] = padVal;
  1098. }
  1099. /* Copy remaining bytes into output buffer. */
  1100. XMEMCPY(*pp + pad, a->data + idx, (size_t)(len - pad));
  1101. /* Two's complement copied bytes when negative. */
  1102. if (a->negative) {
  1103. wolfssl_twos_compl(*pp + pad, len - pad);
  1104. }
  1105. /* Move pointer past encoded data. */
  1106. *pp += len;
  1107. }
  1108. return len;
  1109. }
  1110. /* Make a big number with the value in the ASN.1 INTEGER object.
  1111. *
  1112. * A new big number object is allocated when bn is NULL.
  1113. *
  1114. * @param [in] ai ASN.1 INTEGER object.
  1115. * @param [in] bn Big number object. May be NULL.
  1116. * @return Big number object on success.
  1117. * @return NULL when ai is NULL or converting from binary fails.
  1118. */
  1119. WOLFSSL_BIGNUM *wolfSSL_ASN1_INTEGER_to_BN(const WOLFSSL_ASN1_INTEGER *ai,
  1120. WOLFSSL_BIGNUM *bn)
  1121. {
  1122. int err = 0;
  1123. word32 idx = 1;
  1124. int len = 0;
  1125. WOLFSSL_BIGNUM* ret = NULL;
  1126. WOLFSSL_ENTER("wolfSSL_ASN1_INTEGER_to_BN");
  1127. /* Validate parameters. */
  1128. if (ai == NULL) {
  1129. err = 1;
  1130. }
  1131. if (!err) {
  1132. /* Get the length of ASN.1 INTEGER number. */
  1133. if ((ai->data[0] != ASN_INTEGER) || (GetLength(ai->data, &idx, &len,
  1134. (word32)ai->length) <= 0)) {
  1135. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  1136. idx = 0;
  1137. len = ai->length;
  1138. #else
  1139. WOLFSSL_MSG("Data in WOLFSSL_ASN1_INTEGER not DER encoded");
  1140. err = 1;
  1141. #endif
  1142. }
  1143. }
  1144. if (!err) {
  1145. /* Convert binary to big number. */
  1146. ret = wolfSSL_BN_bin2bn(ai->data + idx, len, bn);
  1147. if (ret != NULL) {
  1148. /* Handle negative. */
  1149. (void)wolfssl_bn_set_neg(ret, ai->negative);
  1150. }
  1151. }
  1152. return ret;
  1153. }
  1154. #endif /* !NO_ASN */
  1155. /* Create an ASN.1 INTEGER object from big number.
  1156. *
  1157. * Allocates a new ASN.1 INTEGER object when ai is NULL.
  1158. *
  1159. * @param [in] bn Big number to encode.
  1160. * @param [in] ai ASN.1 INTEGER object. May be NULL.
  1161. * @return ASN.1 INTEGER object on success.
  1162. * @return NULL when dynamic memory allocation fails.
  1163. */
  1164. WOLFSSL_ASN1_INTEGER* wolfSSL_BN_to_ASN1_INTEGER(const WOLFSSL_BIGNUM *bn,
  1165. WOLFSSL_ASN1_INTEGER *ai)
  1166. {
  1167. int err = 0;
  1168. WOLFSSL_ASN1_INTEGER* a = NULL;
  1169. int len = 0;
  1170. int numBits = 0;
  1171. byte firstByte = 0;
  1172. WOLFSSL_ENTER("wolfSSL_BN_to_ASN1_INTEGER");
  1173. /* Validate parameters. */
  1174. if (bn == NULL) {
  1175. err = 1;
  1176. }
  1177. /* Use ASN.1 INTEGER object if provided. */
  1178. else if (ai != NULL) {
  1179. a = ai;
  1180. }
  1181. /* Create an ASN.1 INTEGER object to return. */
  1182. else {
  1183. a = wolfSSL_ASN1_INTEGER_new();
  1184. if (a == NULL) {
  1185. err = 1;
  1186. }
  1187. }
  1188. /* Check we have an ASN.1 INTEGER object to set. */
  1189. if (!err) {
  1190. int length;
  1191. /* Set type and negative. */
  1192. a->type = V_ASN1_INTEGER;
  1193. if (wolfSSL_BN_is_negative(bn) && !wolfSSL_BN_is_zero(bn)) {
  1194. a->negative = 1;
  1195. a->type |= V_ASN1_NEG_INTEGER;
  1196. }
  1197. /* Get length in bytes of encoded number. */
  1198. len = wolfSSL_BN_num_bytes(bn);
  1199. if (len == 0) {
  1200. len = 1;
  1201. }
  1202. /* Get length in bits of encoded number. */
  1203. numBits = wolfSSL_BN_num_bits(bn);
  1204. /* Leading zero required if most-significant byte has top bit set. */
  1205. if ((numBits > 0) && (numBits % 8) == 0) {
  1206. firstByte = 0x80;
  1207. }
  1208. /* Get length of header based on length of number. */
  1209. length = SetASNInt(len, firstByte, NULL);
  1210. /* Add number of bytes to encode number. */
  1211. length += len;
  1212. /* Update data field to handle length. */
  1213. if (wolfssl_asn1_integer_require_len(a, length, 0) != 1) {
  1214. err = 1;
  1215. }
  1216. }
  1217. if (!err) {
  1218. /* Write ASN.1 header. */
  1219. int idx = SetASNInt(len, firstByte, a->data);
  1220. /* Populate data. */
  1221. if (numBits == 0) {
  1222. a->data[idx] = 0;
  1223. }
  1224. else {
  1225. /* Add encoded number. */
  1226. len = wolfSSL_BN_bn2bin(bn, a->data + idx);
  1227. if (len < 0) {
  1228. err = 1;
  1229. }
  1230. }
  1231. /* Set length to encoded length. */
  1232. a->length = idx + len;
  1233. }
  1234. if (err) {
  1235. /* Can't use ASN.1 INTEGER object. */
  1236. if (a != ai) {
  1237. wolfSSL_ASN1_INTEGER_free(a);
  1238. }
  1239. a = NULL;
  1240. }
  1241. return a;
  1242. }
  1243. /* Get the value of the ASN.1 INTEGER as a long.
  1244. *
  1245. * Returning 0 on NULL and -1 on error is consistent with OpenSSL.
  1246. *
  1247. * @param [in] a ASN.1 INTEGER object.
  1248. * @return Value as a long.
  1249. * @return 0 when a is NULL.
  1250. * @return -1 when a big number operation fails.
  1251. */
  1252. long wolfSSL_ASN1_INTEGER_get(const WOLFSSL_ASN1_INTEGER* a)
  1253. {
  1254. long ret = 1;
  1255. WOLFSSL_BIGNUM* bn = NULL;
  1256. WOLFSSL_ENTER("wolfSSL_ASN1_INTEGER_get");
  1257. /* Validate parameter. */
  1258. if (a == NULL) {
  1259. ret = 0;
  1260. }
  1261. if (ret > 0) {
  1262. /* Create a big number from the DER encoding. */
  1263. bn = wolfSSL_ASN1_INTEGER_to_BN(a, NULL);
  1264. if (bn == NULL) {
  1265. ret = -1;
  1266. }
  1267. }
  1268. if (ret > 0) {
  1269. /* Get the big number as a word. */
  1270. ret = (long)wolfSSL_BN_get_word(bn);
  1271. /* Negate number of ASN.1 INTEGER was negative. */
  1272. if (a->negative == 1) {
  1273. ret = -ret;
  1274. }
  1275. }
  1276. /* Dispose of big number as no longer needed. */
  1277. if (bn != NULL) {
  1278. wolfSSL_BN_free(bn);
  1279. }
  1280. WOLFSSL_LEAVE("wolfSSL_ASN1_INTEGER_get", (int)ret);
  1281. return ret;
  1282. }
  1283. /* Sets the value of the ASN.1 INTEGER object to the long value.
  1284. *
  1285. * @param [in, out] a ASN.1 INTEGER object.
  1286. * @param [in] v Value to set.
  1287. * @return 1 on success.
  1288. * @return 0 when a is NULL.
  1289. */
  1290. int wolfSSL_ASN1_INTEGER_set(WOLFSSL_ASN1_INTEGER *a, long v)
  1291. {
  1292. int ret = 1;
  1293. /* Validate parameters. */
  1294. if (a == NULL) {
  1295. ret = 0;
  1296. }
  1297. if (ret == 1) {
  1298. byte j;
  1299. unsigned int i = 0;
  1300. byte tmp[sizeof(long)];
  1301. byte pad = 0;
  1302. wolfssl_asn1_integer_reset_data(a);
  1303. /* Check for negative. */
  1304. if (v < 0) {
  1305. /* Set negative and 2's complement the value. */
  1306. a->negative = 1;
  1307. a->type |= V_ASN1_NEG;
  1308. v = -v;
  1309. }
  1310. /* Put value into temporary buffer - at least one byte encoded. */
  1311. tmp[0] = (byte)(v & 0xff);
  1312. v >>= 8;
  1313. for (j = 1; j < (byte)sizeof(long); j++) {
  1314. if (v == 0) {
  1315. break;
  1316. }
  1317. tmp[j] = (byte)(v & 0xff);
  1318. v >>= 8;
  1319. }
  1320. /* Pad with 0x00 to indicate positive number when top bit set. */
  1321. if ((!a->negative) && (tmp[j-1] & 0x80)) {
  1322. pad = 1;
  1323. }
  1324. /* Set tag. */
  1325. a->data[i++] = ASN_INTEGER;
  1326. /* Set length of encoded value. */
  1327. a->data[i++] = pad + j;
  1328. /* Set length of DER encoding. +2 for tag and length */
  1329. a->length = 2 + pad + j;
  1330. /* Add pad byte if required. */
  1331. if (pad == 1) {
  1332. a->data[i++] = 0;
  1333. }
  1334. /* Copy in data. */
  1335. for (; j > 0; j--) {
  1336. a->data[i++] = tmp[j-1];
  1337. }
  1338. }
  1339. return ret;
  1340. }
  1341. #endif /* OPENSSL_EXTRA */
  1342. /*******************************************************************************
  1343. * ASN1_OBJECT APIs
  1344. ******************************************************************************/
  1345. #if !defined(NO_ASN)
  1346. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  1347. /* Create a new ASN.1 OBJECT_ID object.
  1348. *
  1349. * @return ASN.1 OBJECT_ID object on success.
  1350. * @return NULL when dynamic memory allocation fails.
  1351. */
  1352. WOLFSSL_ASN1_OBJECT* wolfSSL_ASN1_OBJECT_new(void)
  1353. {
  1354. WOLFSSL_ASN1_OBJECT* obj;
  1355. /* Allocate memory for new ASN.1 OBJECT. */
  1356. obj = (WOLFSSL_ASN1_OBJECT*)XMALLOC(sizeof(WOLFSSL_ASN1_OBJECT), NULL,
  1357. DYNAMIC_TYPE_ASN1);
  1358. if (obj != NULL) {
  1359. XMEMSET(obj, 0, sizeof(WOLFSSL_ASN1_OBJECT));
  1360. /* Setup pointers. */
  1361. obj->d.ia5 = &(obj->d.ia5_internal);
  1362. #if defined(OPENSSL_ALL)
  1363. obj->d.iPAddress = &(obj->d.iPAddress_internal);
  1364. #endif
  1365. /* Object was allocated. */
  1366. obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
  1367. }
  1368. return obj;
  1369. }
  1370. /* Dispose of any ASN.1 OBJECT_ID dynamically allocated data.
  1371. *
  1372. * Do not use obj after calling this function.
  1373. *
  1374. * @param [in, out] obj ASN.1 OBJECT_ID object.
  1375. */
  1376. void wolfSSL_ASN1_OBJECT_free(WOLFSSL_ASN1_OBJECT* obj)
  1377. {
  1378. if (obj != NULL) {
  1379. /* Check for dynamically allocated copy of encoded data. */
  1380. if ((obj->dynamic & WOLFSSL_ASN1_DYNAMIC_DATA) != 0) {
  1381. #ifdef WOLFSSL_DEBUG_OPENSSL
  1382. WOLFSSL_MSG("Freeing ASN1 data");
  1383. #endif
  1384. XFREE((void*)obj->obj, obj->heap, DYNAMIC_TYPE_ASN1);
  1385. obj->obj = NULL;
  1386. }
  1387. #if defined(OPENSSL_EXTRA)
  1388. /* Check for path length ASN.1 INTEGER - X.509 extension. */
  1389. if (obj->pathlen != NULL) {
  1390. wolfSSL_ASN1_INTEGER_free(obj->pathlen);
  1391. obj->pathlen = NULL;
  1392. }
  1393. #endif
  1394. /* Check whether object was dynamically allocated. */
  1395. if ((obj->dynamic & WOLFSSL_ASN1_DYNAMIC) != 0) {
  1396. #ifdef WOLFSSL_DEBUG_OPENSSL
  1397. WOLFSSL_MSG("Freeing ASN1 OBJECT");
  1398. #endif
  1399. XFREE(obj, NULL, DYNAMIC_TYPE_ASN1);
  1400. }
  1401. }
  1402. }
  1403. /* Duplicate the ASN.1 OBJECT_ID object.
  1404. *
  1405. * @param [in] obj ASN.1 OBJECT_ID object to copy.
  1406. * @return New ASN.1 OBJECT_ID object on success.
  1407. * @return NULL when obj is NULL or dynamic memory allocation fails.
  1408. */
  1409. WOLFSSL_ASN1_OBJECT* wolfSSL_ASN1_OBJECT_dup(WOLFSSL_ASN1_OBJECT* obj)
  1410. {
  1411. WOLFSSL_ASN1_OBJECT* dupl = NULL;
  1412. WOLFSSL_ENTER("wolfSSL_ASN1_OBJECT_dup");
  1413. /* Validate parameter. */
  1414. if (obj == NULL) {
  1415. WOLFSSL_MSG("Bad parameter");
  1416. }
  1417. /* Create a new ASN.1 OBJECT_ID object to return. */
  1418. else if ((dupl = wolfSSL_ASN1_OBJECT_new()) == NULL) {
  1419. WOLFSSL_MSG("wolfSSL_ASN1_OBJECT_new error");
  1420. }
  1421. if (dupl != NULL) {
  1422. /* Copy short name. */
  1423. XMEMCPY(dupl->sName, obj->sName, WOLFSSL_MAX_SNAME);
  1424. /* Copy simple fields. */
  1425. dupl->type = obj->type;
  1426. dupl->grp = obj->grp;
  1427. dupl->nid = obj->nid;
  1428. dupl->objSz = obj->objSz;
  1429. #ifdef OPENSSL_EXTRA
  1430. dupl->ca = obj->ca;
  1431. #endif
  1432. /* Check for encoding. */
  1433. if (obj->obj) {
  1434. /* Allocate memory for ASN.1 OBJECT_ID DER encoding. */
  1435. dupl->obj = (const unsigned char*)XMALLOC(obj->objSz, NULL,
  1436. DYNAMIC_TYPE_ASN1);
  1437. if (dupl->obj == NULL) {
  1438. WOLFSSL_MSG("ASN1 obj malloc error");
  1439. wolfSSL_ASN1_OBJECT_free(dupl);
  1440. dupl = NULL;
  1441. }
  1442. else {
  1443. /* Encoding buffer was dynamically allocated. */
  1444. dupl->dynamic |= WOLFSSL_ASN1_DYNAMIC_DATA;
  1445. /* Copy DER encoding. */
  1446. XMEMCPY((byte*)dupl->obj, obj->obj, obj->objSz);
  1447. }
  1448. }
  1449. }
  1450. return dupl;
  1451. }
  1452. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  1453. #endif /* !NO_ASN */
  1454. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  1455. /**
  1456. * Parse DER encoding and return header information.
  1457. *
  1458. * *in is moved to the value of the ASN1 object
  1459. *
  1460. * @param [in, out] in Pointer to BER encoded data.
  1461. * @param [out] len Length of parsed ASN1 object
  1462. * @param [out] tag Tag value of parsed ASN1 object
  1463. * @param [out] cls Class of parsed ASN1 object
  1464. * @param [in] inLen Length of *in buffer
  1465. * @return int Depends on which bits are set in the returned int:
  1466. * 0x80 an error occurred during parsing.
  1467. * 0x20 parsed object is constructed.
  1468. * 0x01 the parsed object length is indefinite.
  1469. */
  1470. int wolfSSL_ASN1_get_object(const unsigned char **in, long *len, int *tag,
  1471. int *cls, long inLen)
  1472. {
  1473. int err = 0;
  1474. word32 inOutIdx = 0;
  1475. int l = 0;
  1476. byte t = 0;
  1477. int ret = 0x80;
  1478. WOLFSSL_ENTER("wolfSSL_ASN1_get_object");
  1479. if ((in == NULL) || (*in == NULL) || (len == NULL) || (tag == NULL) ||
  1480. (cls == NULL) || (inLen <= 0)) {
  1481. WOLFSSL_MSG("Bad parameter");
  1482. err = 1;
  1483. }
  1484. if (!err) {
  1485. /* Length at least 1, parameters valid - cannot fail to get tag. */
  1486. GetASNTag(*in, &inOutIdx, &t, (word32)inLen);
  1487. /* Get length in DER encoding. */
  1488. if (GetLength_ex(*in, &inOutIdx, &l, (word32)inLen, 0) < 0) {
  1489. WOLFSSL_MSG("GetLength error");
  1490. err = 1;
  1491. }
  1492. }
  1493. if (!err) {
  1494. /* Return header information. */
  1495. *tag = t & ASN_TYPE_MASK; /* Tag number is 5 lsb */
  1496. *cls = t & ASN_CLASS_MASK; /* Class is 2 msb */
  1497. *len = l;
  1498. ret = t & ASN_CONSTRUCTED;
  1499. if (l > (int)(inLen - inOutIdx)) {
  1500. /* Still return other values but indicate error in msb */
  1501. ret |= 0x80;
  1502. }
  1503. /* Move pointer to after DER header. */
  1504. *in += inOutIdx;
  1505. }
  1506. return ret;
  1507. }
  1508. /* Creates and ASN.1 OBJECT_ID object from DER encoding.
  1509. *
  1510. * @param [out] a Pointer to return new ASN.1 OBJECT_ID through.
  1511. * @param [in, out] der Pointer to buffer holding DER encoding.
  1512. * @param [in] length Length of DER encoding in bytes.
  1513. * @return New ASN.1 OBJECT_ID object on success.
  1514. * @return NULL when der or *der is NULL or length is less than or equal zero.
  1515. * @return NULL when not an OBJECT_ID or decoding fails.
  1516. * @return NULL when dynamic memory allocation fails.
  1517. */
  1518. WOLFSSL_ASN1_OBJECT *wolfSSL_d2i_ASN1_OBJECT(WOLFSSL_ASN1_OBJECT **a,
  1519. const unsigned char **der, long length)
  1520. {
  1521. WOLFSSL_ASN1_OBJECT* ret = NULL;
  1522. int err = 0;
  1523. const unsigned char *d;
  1524. long len = 0;
  1525. int tag = 0;
  1526. int cls;
  1527. WOLFSSL_ENTER("wolfSSL_d2i_ASN1_OBJECT");
  1528. /* Validate parameters. */
  1529. if ((der == NULL) || (*der == NULL) || (length <= 0)) {
  1530. WOLFSSL_MSG("Bad parameter");
  1531. err = 1;
  1532. }
  1533. if (!err) {
  1534. /* Get pointer to be modified along the way. */
  1535. d = *der;
  1536. /* Move d to value and get length and tag. */
  1537. if (wolfSSL_ASN1_get_object(&d, &len, &tag, &cls, length) & 0x80) {
  1538. WOLFSSL_MSG("wolfSSL_ASN1_get_object error");
  1539. err = 1;
  1540. }
  1541. }
  1542. /* Check it DER encoding is of an OBJECT_ID. */
  1543. if ((!err) && (tag != ASN_OBJECT_ID)) {
  1544. WOLFSSL_MSG("Not an ASN object");
  1545. err = 1;
  1546. }
  1547. /* Create an ASN.1 OBJECT_ID_object from value. TODO: not DER encoding? */
  1548. if ((!err) && ((ret = wolfSSL_c2i_ASN1_OBJECT(a, &d, len)) != NULL)) {
  1549. /* Update pointer to after decoded bytes. */
  1550. *der = d;
  1551. }
  1552. return ret;
  1553. }
  1554. /* Write out DER encoding of ASN.1 OBJECT_ID.
  1555. *
  1556. * When pp is NULL, length is returned.
  1557. * When pp points to NULL, a new buffer is allocated and returned through pp.
  1558. * When pp points to a buffer, it is moved on past encoded data on success.
  1559. *
  1560. * @param [in] a ASN.1 OBJECT_ID object.
  1561. * @param [in, out] pp Pointer to buffer to write to. May be NULL.
  1562. * @return Length of encoding on success.
  1563. * @return 0 when a or encoding buffer is NULL.
  1564. * @return 0 when dynamic memory allocation fails.
  1565. */
  1566. int wolfSSL_i2d_ASN1_OBJECT(WOLFSSL_ASN1_OBJECT *a, unsigned char **pp)
  1567. {
  1568. int len = 0;
  1569. WOLFSSL_ENTER("wolfSSL_i2d_ASN1_OBJECT");
  1570. /* Validate parameters */
  1571. if ((a == NULL) || (a->obj == NULL)) {
  1572. WOLFSSL_MSG("Bad parameters");
  1573. }
  1574. /* Only return length when no pointer supplied. */
  1575. else if (pp == NULL) {
  1576. len = (int)a->objSz;
  1577. }
  1578. else {
  1579. byte *p = NULL;
  1580. /* Check if we have a buffer to encode into. */
  1581. if (*pp == NULL) {
  1582. /* Allocate a new buffer to return. */
  1583. p = (byte*)XMALLOC(a->objSz, NULL, DYNAMIC_TYPE_OPENSSL);
  1584. if (p == NULL) {
  1585. WOLFSSL_MSG("Bad malloc");
  1586. }
  1587. else {
  1588. /* Return allocated buffer. */
  1589. *pp = p;
  1590. }
  1591. }
  1592. /* Check we have a buffer to encode into. */
  1593. if (*pp != NULL) {
  1594. /* Copy in DER encoding. */
  1595. XMEMCPY(*pp, a->obj, a->objSz);
  1596. /* Move on pointer if user supplied. */
  1597. if (p == NULL) {
  1598. *pp += a->objSz;
  1599. }
  1600. /* Return length of DER encoding. */
  1601. len = (int)a->objSz;
  1602. }
  1603. }
  1604. return len;
  1605. }
  1606. /* Create an ASN.1 OBJECT_ID object from the content octets.
  1607. *
  1608. * @param [out] a Pointer to return ASN.1 OBJECT_ID object.
  1609. * @param [in, out] pp Pointer to buffer holding content octets.
  1610. * @param [in] len Length of content octets in bytes.
  1611. * @return New ASN.1 OBJECT_ID object on success.
  1612. * @return NULL when pp or *pp is NULL or length is less than or equal zero.
  1613. * @return NULL when dynamic memory allocation fails.
  1614. */
  1615. WOLFSSL_ASN1_OBJECT *wolfSSL_c2i_ASN1_OBJECT(WOLFSSL_ASN1_OBJECT **a,
  1616. const unsigned char **pp, long len)
  1617. {
  1618. int err = 0;
  1619. WOLFSSL_ASN1_OBJECT* ret = NULL;
  1620. WOLFSSL_ENTER("wolfSSL_c2i_ASN1_OBJECT");
  1621. /* Validate parameters. */
  1622. if ((pp == NULL) || (*pp == NULL) || (len <= 0)) {
  1623. WOLFSSL_MSG("Bad parameter");
  1624. err = 1;
  1625. }
  1626. /* Create a new ASN.1 OBJECT_ID object. */
  1627. if ((!err) && ((ret = wolfSSL_ASN1_OBJECT_new()) == NULL)) {
  1628. WOLFSSL_MSG("wolfSSL_ASN1_OBJECT_new error");
  1629. err = 1;
  1630. }
  1631. if (!err) {
  1632. /* Allocate memory for content octets. */
  1633. ret->obj = (const unsigned char*)XMALLOC((size_t)len, NULL,
  1634. DYNAMIC_TYPE_ASN1);
  1635. if (ret->obj == NULL) {
  1636. WOLFSSL_MSG("error allocating asn data memory");
  1637. wolfSSL_ASN1_OBJECT_free(ret);
  1638. ret = NULL;
  1639. err = 1;
  1640. }
  1641. }
  1642. if (!err) {
  1643. /* Content octets buffer was dynamically allocated. */
  1644. ret->dynamic |= WOLFSSL_ASN1_DYNAMIC_DATA;
  1645. /* Copy in content octets and set size. */
  1646. XMEMCPY((byte*)ret->obj, *pp, (size_t)len);
  1647. ret->objSz = (unsigned int)len;
  1648. /* Move pointer to after data copied out. */
  1649. *pp += len;
  1650. /* Return ASN.1 OBJECT_ID object through a if required. */
  1651. if (a != NULL) {
  1652. *a = ret;
  1653. }
  1654. }
  1655. return ret;
  1656. }
  1657. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  1658. #ifdef OPENSSL_EXTRA
  1659. /* Write at most buf_len bytes of textual representation of ASN.1 OBJECT_ID.
  1660. *
  1661. * @param [in, out] buf Buffer to write to.
  1662. * @param [in] buf_len Length of buffer in bytes.
  1663. * @param [in] a ASN.1 OBJECT_ID object.
  1664. * @return Number of bytes written on success.
  1665. * @return 0 on failure.
  1666. */
  1667. int wolfSSL_i2t_ASN1_OBJECT(char *buf, int buf_len, WOLFSSL_ASN1_OBJECT *a)
  1668. {
  1669. WOLFSSL_ENTER("wolfSSL_i2t_ASN1_OBJECT");
  1670. return wolfSSL_OBJ_obj2txt(buf, buf_len, a, 0);
  1671. }
  1672. #ifndef NO_BIO
  1673. /* Write out the text encoding of the ASN.1 OBJECT_ID.
  1674. *
  1675. * @param [in] bp BIO to write to.
  1676. * @param [in] a ASN.1 OBJECT_ID object.
  1677. * @return Number of bytes written on success.
  1678. * @return 0 on failure.
  1679. */
  1680. int wolfSSL_i2a_ASN1_OBJECT(WOLFSSL_BIO *bp, WOLFSSL_ASN1_OBJECT *a)
  1681. {
  1682. int length = 0;
  1683. int cLen = 0;
  1684. word32 idx = 0;
  1685. const char null_str[] = "NULL";
  1686. const char invalid_str[] = "<INVALID>";
  1687. char buf[80];
  1688. WOLFSSL_ENTER("wolfSSL_i2a_ASN1_OBJECT");
  1689. /* Validate parameters. */
  1690. if (bp == NULL) {
  1691. /* Do nothing. */
  1692. }
  1693. /* NULL object is written as "NULL". */
  1694. else if (a == NULL) {
  1695. /* Write "NULL" - as done in OpenSSL. */
  1696. length = wolfSSL_BIO_write(bp, null_str, (int)XSTRLEN(null_str));
  1697. }
  1698. /* Try getting text version and write it out. */
  1699. else if ((length = i2t_ASN1_OBJECT(buf, sizeof(buf), a)) > 0) {
  1700. length = wolfSSL_BIO_write(bp, buf, length);
  1701. }
  1702. /* Look for DER header. */
  1703. else if ((a->obj == NULL) || (a->obj[idx++] != ASN_OBJECT_ID)) {
  1704. WOLFSSL_MSG("Bad ASN1 Object");
  1705. }
  1706. /* Get length from DER header. */
  1707. else if (GetLength((const byte*)a->obj, &idx, &cLen, a->objSz) < 0) {
  1708. length = 0;
  1709. }
  1710. else {
  1711. /* Write out "<INVALID>" and dump content. */
  1712. length = wolfSSL_BIO_write(bp, invalid_str, (int)XSTRLEN(invalid_str));
  1713. length += wolfSSL_BIO_dump(bp, (const char*)(a->obj + idx), cLen);
  1714. }
  1715. return length;
  1716. }
  1717. #endif /* !NO_BIO */
  1718. #endif /* OPENSSL_EXTRA */
  1719. /*******************************************************************************
  1720. * ASN1_SK_OBJECT APIs
  1721. ******************************************************************************/
  1722. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && !defined(NO_ASN)
  1723. /* Create a new WOLFSSL_ASN1_OBJECT stack.
  1724. *
  1725. * @return New WOLFSSL_ASN1_OBJECT stack on success.
  1726. * @return NULL when dynamic memory allocation fails.
  1727. */
  1728. WOLFSSL_STACK* wolfSSL_sk_new_asn1_obj(void)
  1729. {
  1730. WOLFSSL_ENTER("wolfSSL_sk_new_asn1_obj");
  1731. return wolfssl_sk_new_type(STACK_TYPE_OBJ);
  1732. }
  1733. /* Dispose of WOLFSL_ASN1_OBJECT stack.
  1734. *
  1735. * @param [in, out] sk Stack to free nodes in.
  1736. */
  1737. void wolfSSL_sk_ASN1_OBJECT_free(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk)
  1738. {
  1739. /* Dispose of stack. */
  1740. wolfSSL_sk_free(sk);
  1741. }
  1742. /* Dispose of all ASN.1 OBJECT_ID objects in ASN1_OBJECT stack.
  1743. *
  1744. * This is different then wolfSSL_ASN1_OBJECT_free in that it allows for
  1745. * choosing the function to use when freeing an ASN1_OBJECT stack.
  1746. *
  1747. * @param [in, out] sk ASN.1 OBJECT_ID stack to free.
  1748. * @param [in] f Free function to apply to each ASN.1 OBJECT_ID object.
  1749. */
  1750. void wolfSSL_sk_ASN1_OBJECT_pop_free(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk,
  1751. void (*f) (WOLFSSL_ASN1_OBJECT*))
  1752. {
  1753. WOLFSSL_ENTER("wolfSSL_sk_ASN1_OBJECT_pop_free");
  1754. wolfSSL_sk_pop_free(sk, (wolfSSL_sk_freefunc)f);
  1755. }
  1756. /* Push a WOLFSSL_ASN1_OBJECT onto stack.
  1757. *
  1758. * @param [in, out] sk ASN.1 OBJECT_ID stack.
  1759. * @param [in] obj ASN.1 OBJECT_ID object to push on. Cannot be NULL.
  1760. * @return 1 on success.
  1761. * @return 0 when sk or obj is NULL.
  1762. * @return 0 when dynamic memory allocation fails.
  1763. */
  1764. int wolfSSL_sk_ASN1_OBJECT_push(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk,
  1765. WOLFSSL_ASN1_OBJECT* obj)
  1766. {
  1767. int ret = 0;
  1768. WOLFSSL_ENTER("wolfSSL_sk_ASN1_OBJECT_push");
  1769. /* Push on when we have a stack and object to work with. */
  1770. if ((sk != NULL) && (obj != NULL)) {
  1771. ret = wolfSSL_sk_push(sk, obj);
  1772. }
  1773. return ret;
  1774. }
  1775. /* Pop off a WOLFSSL_ASN1_OBJECT from the stack.
  1776. *
  1777. * @param [in, out] sk ASN.1 OBJECT_ID stack.
  1778. * @return ASN.1 OBJECT_ID object on success.
  1779. * @return NULL when stack is NULL or no nodes left in stack.
  1780. */
  1781. WOLFSSL_ASN1_OBJECT* wolfSSL_sk_ASN1_OBJECT_pop(
  1782. WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk)
  1783. {
  1784. return (WOLFSSL_ASN1_OBJECT*)wolfssl_sk_pop_type(sk, STACK_TYPE_OBJ);
  1785. }
  1786. #endif /* (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) && !NO_ASN */
  1787. /*******************************************************************************
  1788. * ASN1_STRING APIs
  1789. ******************************************************************************/
  1790. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  1791. /* Create a new ASN.1 STRING object.
  1792. *
  1793. * @return New ASN.1 STRING object on success.
  1794. * @return NULL when dynamic memory allocation fails.
  1795. */
  1796. WOLFSSL_ASN1_STRING* wolfSSL_ASN1_STRING_new(void)
  1797. {
  1798. WOLFSSL_ASN1_STRING* asn1;
  1799. #ifdef WOLFSSL_DEBUG_OPENSSL
  1800. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_new");
  1801. #endif
  1802. asn1 = (WOLFSSL_ASN1_STRING*)XMALLOC(sizeof(WOLFSSL_ASN1_STRING), NULL,
  1803. DYNAMIC_TYPE_OPENSSL);
  1804. if (asn1 != NULL) {
  1805. XMEMSET(asn1, 0, sizeof(WOLFSSL_ASN1_STRING));
  1806. }
  1807. return asn1;
  1808. }
  1809. /* Create a new ASN.1 STRING object.
  1810. *
  1811. * @param [in] type Encoding type.
  1812. * @return New ASN.1 STRING object on success.
  1813. * @return NULL when dynamic memory allocation fails.
  1814. */
  1815. WOLFSSL_ASN1_STRING* wolfSSL_ASN1_STRING_type_new(int type)
  1816. {
  1817. WOLFSSL_ASN1_STRING* asn1;
  1818. #ifdef WOLFSSL_DEBUG_OPENSSL
  1819. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_type_new");
  1820. #endif
  1821. asn1 = wolfSSL_ASN1_STRING_new();
  1822. if (asn1 != NULL) {
  1823. asn1->type = type;
  1824. }
  1825. return asn1;
  1826. }
  1827. /* Dispose of ASN.1 STRING object.
  1828. *
  1829. * @param [in, out] asn1 ASN.1 STRING object.
  1830. */
  1831. void wolfSSL_ASN1_STRING_free(WOLFSSL_ASN1_STRING* asn1)
  1832. {
  1833. #ifdef WOLFSSL_DEBUG_OPENSSL
  1834. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_free");
  1835. #endif
  1836. /* Check we have an object to free. */
  1837. if (asn1 != NULL) {
  1838. /* Dispose of dynamic data. */
  1839. if ((asn1->length > 0) && asn1->isDynamic) {
  1840. XFREE(asn1->data, NULL, DYNAMIC_TYPE_OPENSSL);
  1841. }
  1842. }
  1843. /* Dispose of ASN.1 STRING object. */
  1844. XFREE(asn1, NULL, DYNAMIC_TYPE_OPENSSL);
  1845. }
  1846. /* Copy an ASN.1 STRING object from src into dest.
  1847. *
  1848. * @param [in, out] dest ASN.1 STRING object to copy into.
  1849. * @param [in] src ASN.1 STRING object to copy from.
  1850. */
  1851. int wolfSSL_ASN1_STRING_copy(WOLFSSL_ASN1_STRING* dest,
  1852. const WOLFSSL_ASN1_STRING* src)
  1853. {
  1854. int ret = 1;
  1855. /* Validate parameters. */
  1856. if ((src == NULL) || (dest == NULL)) {
  1857. ret = 0;
  1858. }
  1859. /* Set the DER encoding. */
  1860. if ((ret == 1) && (wolfSSL_ASN1_STRING_set(dest, src->data, src->length) !=
  1861. 1)) {
  1862. ret = 0;
  1863. }
  1864. if (ret == 1) {
  1865. /* Copy simple fields. */
  1866. dest->type = src->type;
  1867. dest->flags = src->flags;
  1868. }
  1869. return ret;
  1870. }
  1871. /* Duplicate an ASN.1 STRING object.
  1872. *
  1873. * @param [in] asn1 ASN.1 STRING object to duplicate.
  1874. * @return New ASN.1 STRING object on success.
  1875. * @return NULL when asn1 is NULL or dynamic memory allocation fails.
  1876. */
  1877. WOLFSSL_ASN1_STRING* wolfSSL_ASN1_STRING_dup(WOLFSSL_ASN1_STRING* asn1)
  1878. {
  1879. WOLFSSL_ASN1_STRING* dupl = NULL;
  1880. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_dup");
  1881. /* Check we have an object to duplicate. */
  1882. if (asn1 == NULL) {
  1883. WOLFSSL_MSG("Bad parameter");
  1884. }
  1885. else {
  1886. /* Create a new ASN.1 STRING object. */
  1887. dupl = wolfSSL_ASN1_STRING_new();
  1888. if (dupl == NULL) {
  1889. WOLFSSL_MSG("wolfSSL_ASN1_STRING_new error");
  1890. }
  1891. }
  1892. if (dupl != NULL) {
  1893. /* Copy the contents. */
  1894. if (wolfSSL_ASN1_STRING_copy(dupl, asn1) != 1) {
  1895. WOLFSSL_MSG("wolfSSL_ASN1_STRING_copy error");
  1896. /* Dispose of duplicate and return NULL. */
  1897. wolfSSL_ASN1_STRING_free(dupl);
  1898. dupl = NULL;
  1899. }
  1900. }
  1901. return dupl;
  1902. }
  1903. /* Compare two ASN.1 STRING objects.
  1904. *
  1905. * Compares type when data the same.
  1906. *
  1907. * @param [in] a First ASN.1 STRING object.
  1908. * @param [in] b Second ASN.1 STRING object.
  1909. * @return Negative value when a is less than b.
  1910. * @return 0 when a equals b.
  1911. * @return Positive value when a is greater than b.
  1912. * @return -1 when a or b is NULL.
  1913. */
  1914. int wolfSSL_ASN1_STRING_cmp(const WOLFSSL_ASN1_STRING *a,
  1915. const WOLFSSL_ASN1_STRING *b)
  1916. {
  1917. int ret;
  1918. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_cmp");
  1919. /* Validate parameters. */
  1920. if ((a == NULL) || (b == NULL)) {
  1921. ret = -1;
  1922. }
  1923. /* Compare length of data. */
  1924. else if (a->length != b->length) {
  1925. ret = a->length - b->length;
  1926. }
  1927. /* Compare data. */
  1928. else if ((ret = XMEMCMP(a->data, b->data, (size_t)a->length)) == 0) {
  1929. /* Compare ASN.1 types - wolfSSL_ASN1_STRING_type_new(). */
  1930. ret = a->type - b->type;
  1931. }
  1932. return ret;
  1933. }
  1934. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  1935. #if defined(OPENSSL_EXTRA)
  1936. #if !defined(NO_CERTS)
  1937. #ifndef NO_WOLFSSL_STUB
  1938. WOLFSSL_ASN1_STRING* wolfSSL_d2i_DISPLAYTEXT(WOLFSSL_ASN1_STRING **asn,
  1939. const unsigned char **in, long len)
  1940. {
  1941. WOLFSSL_STUB("d2i_DISPLAYTEXT");
  1942. (void)asn;
  1943. (void)in;
  1944. (void)len;
  1945. return NULL;
  1946. }
  1947. #endif
  1948. #endif /* !NO_CERTS */
  1949. #endif /* OPENSSL_EXTRA */
  1950. #ifndef NO_ASN
  1951. #if defined(OPENSSL_EXTRA)
  1952. /* Convert ASN.1 STRING that is UniversalString type to PrintableString type.
  1953. *
  1954. * @param [in, out] s ASN.1 STRING object to convert.
  1955. * @return 1 on success.
  1956. * @return 0 when s is NULL.
  1957. * @return 0 when type is not UniversalString or string is not of that format.
  1958. */
  1959. int wolfSSL_ASN1_UNIVERSALSTRING_to_string(WOLFSSL_ASN1_STRING *s)
  1960. {
  1961. int ret = 1;
  1962. char* p;
  1963. WOLFSSL_ENTER("wolfSSL_ASN1_UNIVERSALSTRING_to_string");
  1964. /* Validate parameter. */
  1965. if (s == NULL) {
  1966. WOLFSSL_MSG("Bad parameter");
  1967. ret = 0;
  1968. }
  1969. /* Check type of ASN.1 STRING. */
  1970. if ((ret == 1) && (s->type != V_ASN1_UNIVERSALSTRING)) {
  1971. WOLFSSL_MSG("Input is not a universal string");
  1972. ret = 0;
  1973. }
  1974. /* Check length is indicative of UNIVERSAL_STRING. */
  1975. if ((ret == 1) && ((s->length % 4) != 0)) {
  1976. WOLFSSL_MSG("Input string must be divisible by 4");
  1977. ret = 0;
  1978. }
  1979. if (ret == 1) {
  1980. /* Ensure each UniversalString character looks right. */
  1981. for (p = s->data; p < s->data + s->length; p += 4)
  1982. if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0'))
  1983. break;
  1984. /* Check whether we failed loop early. */
  1985. if (p != s->data + s->length) {
  1986. WOLFSSL_MSG("Wrong string format");
  1987. ret = 0;
  1988. }
  1989. }
  1990. if (ret == 1) {
  1991. char* copy;
  1992. /* Strip first three bytes of each four byte character. */
  1993. for (copy = p = s->data; p < s->data + s->length; p += 4) {
  1994. *copy++ = p[3];
  1995. }
  1996. /* Place NUL on end. */
  1997. *copy = '\0';
  1998. /* Update length and type. */
  1999. s->length /= 4;
  2000. s->type = V_ASN1_PRINTABLESTRING;
  2001. }
  2002. return ret;
  2003. }
  2004. #endif /* OPENSSL_EXTRA */
  2005. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  2006. /* Convert ASN.1 STRING to UTF8 encoding.
  2007. *
  2008. * Assumes stored encoding is UTF8.
  2009. * Returned buffer should be freed using OPENSSL_free().
  2010. *
  2011. * @param [out] out Pointer to return allocated string.
  2012. * @param [in] asn1 ASN.1 STRING object.
  2013. * @return Length of string, excluding NUL, on success.
  2014. * @return -1 when out or asn1 is NULL.
  2015. * @return -1 when no data to return.
  2016. * @return -1 dynamic memory allocation fails.
  2017. */
  2018. int wolfSSL_ASN1_STRING_to_UTF8(unsigned char **out, WOLFSSL_ASN1_STRING *asn1)
  2019. {
  2020. unsigned char* buf = NULL;
  2021. unsigned char* data = NULL;
  2022. int len = -1;
  2023. /* Validate parameters. */
  2024. if ((out != NULL) && (asn1 != NULL)) {
  2025. /* Get data and length. */
  2026. data = wolfSSL_ASN1_STRING_data(asn1);
  2027. len = wolfSSL_ASN1_STRING_length(asn1);
  2028. /* Check data and length are usable. */
  2029. if ((data == NULL) || (len < 0)) {
  2030. len = -1;
  2031. }
  2032. }
  2033. if (len != -1) {
  2034. /* Allocate buffer to hold string and NUL. */
  2035. buf = (unsigned char*)XMALLOC((size_t)(len + 1), NULL,
  2036. DYNAMIC_TYPE_OPENSSL);
  2037. if (buf == NULL) {
  2038. len = -1;
  2039. }
  2040. }
  2041. if (len != -1) {
  2042. /* Copy in string - NUL always put on end of stored string. */
  2043. XMEMCPY(buf, data, (size_t)(len + 1));
  2044. /* Return buffer. */
  2045. *out = buf;
  2046. }
  2047. return len;
  2048. }
  2049. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  2050. #if defined(OPENSSL_EXTRA)
  2051. /* Encode ASN.1 STRING data as hex digits separated by colon.
  2052. *
  2053. * Assumes length is greater than 0.
  2054. *
  2055. * @param [in] s ASN.1 STRING object.
  2056. * @return Buffer containing string representation on success.
  2057. * @return NULL when dynamic memory allocation fails.
  2058. * @return NULL when encoding a character as hex fails.
  2059. */
  2060. static char* wolfssl_asn1_string_to_hex_chars(const WOLFSSL_ASN1_STRING *s)
  2061. {
  2062. char* tmp;
  2063. int tmpSz = s->length * 3;
  2064. tmp = (char*)XMALLOC((size_t)tmpSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2065. if (tmp == NULL) {
  2066. WOLFSSL_MSG("Memory Error");
  2067. }
  2068. else {
  2069. int i;
  2070. unsigned char* str = (unsigned char*)s->data;
  2071. /* Put out all but last character as a hex digit with ':'. */
  2072. for (i = 0; i < s->length; i++) {
  2073. /* Put in hex digit string at end of tmp. */
  2074. ByteToHexStr(str[i], tmp + i * 3);
  2075. /* Check not last character. */
  2076. if (i < s->length - 1) {
  2077. /* Put in separator: ':'. */
  2078. tmp[i * 3 + 2] = ':';
  2079. }
  2080. /* Last character. */
  2081. else {
  2082. /* Put in NUL to terminate string. */
  2083. tmp[i * 3 + 2] = '\0';
  2084. }
  2085. }
  2086. }
  2087. return tmp;
  2088. }
  2089. /* Create a string encoding of ASN.1 STRING object.
  2090. *
  2091. * @param [in] method Method table. Unused.
  2092. * @param [in] s ASN.1 STRING object.
  2093. * @return Buffer containing string representation on success.
  2094. * @return NULL when s or data is NULL.
  2095. * @return NULL when dynamic memory allocation fails.
  2096. * @return NULL when encoding a character as hex fails.
  2097. */
  2098. char* wolfSSL_i2s_ASN1_STRING(WOLFSSL_v3_ext_method *method,
  2099. const WOLFSSL_ASN1_STRING *s)
  2100. {
  2101. char* ret = NULL;
  2102. WOLFSSL_ENTER("wolfSSL_i2s_ASN1_STRING");
  2103. (void)method;
  2104. /* Validate parameters. */
  2105. if ((s == NULL) || (s->data == NULL)) {
  2106. WOLFSSL_MSG("Bad Function Argument");
  2107. }
  2108. /* Handle 0 length data separately. */
  2109. else if (s->length == 0) {
  2110. ret = (char *)XMALLOC(1, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2111. if (ret != NULL) {
  2112. ret[0] = '\0';
  2113. }
  2114. }
  2115. else {
  2116. /* Convert unreadable strings to hexadecimal. */
  2117. ret = wolfssl_asn1_string_to_hex_chars(s);
  2118. }
  2119. return ret;
  2120. }
  2121. #endif /* OPENSSL_EXTRA */
  2122. #endif /* NO_ASN */
  2123. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  2124. /* Get the type of encoding.
  2125. *
  2126. * @param [in] asn1 ASN.1 STRING object.
  2127. * @return Encoding type on success.
  2128. * @return 0 when asn1 is NULL or no encoding set.
  2129. */
  2130. int wolfSSL_ASN1_STRING_type(const WOLFSSL_ASN1_STRING* asn1)
  2131. {
  2132. int type = 0;
  2133. #ifdef WOLFSSL_DEBUG_OPENSSL
  2134. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_type");
  2135. #endif
  2136. if (asn1 != NULL) {
  2137. type = asn1->type;
  2138. }
  2139. return type;
  2140. }
  2141. #ifndef NO_CERTS
  2142. /* Get the pointer that is the data.
  2143. *
  2144. * @param [in] asn ASN.1 STRING object.
  2145. * @return Buffer with string on success.
  2146. * @return NULL when asn is NULL or no data set.
  2147. */
  2148. const unsigned char* wolfSSL_ASN1_STRING_get0_data(
  2149. const WOLFSSL_ASN1_STRING* asn)
  2150. {
  2151. char* data = NULL;
  2152. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_get0_data");
  2153. if (asn != NULL) {
  2154. data = asn->data;
  2155. }
  2156. return (const unsigned char*)data;
  2157. }
  2158. /* Get the pointer that is the data.
  2159. *
  2160. * @param [in] asn ASN.1 STRING object.
  2161. * @return Buffer with string on success.
  2162. * @return NULL when asn is NULL or no data set.
  2163. */
  2164. unsigned char* wolfSSL_ASN1_STRING_data(WOLFSSL_ASN1_STRING* asn)
  2165. {
  2166. char* data = NULL;
  2167. #ifdef WOLFSSL_DEBUG_OPENSSL
  2168. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_data");
  2169. #endif
  2170. if (asn != NULL) {
  2171. data = asn->data;
  2172. }
  2173. return (unsigned char*)data;
  2174. }
  2175. /* Get the length of the data.
  2176. *
  2177. * @param [in] asn ASN.1 STRING object.
  2178. * @return String length on success.
  2179. * @return 0 when asn is NULL or no data set.
  2180. */
  2181. int wolfSSL_ASN1_STRING_length(WOLFSSL_ASN1_STRING* asn)
  2182. {
  2183. int len = 0;
  2184. #ifdef WOLFSSL_DEBUG_OPENSSL
  2185. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_length");
  2186. #endif
  2187. if (asn) {
  2188. len = asn->length;
  2189. }
  2190. return len;
  2191. }
  2192. #endif /* !NO_CERTS */
  2193. /* Set the string data.
  2194. *
  2195. * When sz is less than 0, the string length will be calculated using XSTRLEN.
  2196. *
  2197. * @param [in, out] asn1 ASN.1 STRING object.
  2198. * @param [in] data String data to set.
  2199. * @param [in] sz Length of data to set in bytes.
  2200. * @return 1 on success.
  2201. * @return 0 when asn1 is NULL or data is NULL and sz is not zero.
  2202. * @return 0 when dynamic memory allocation fails.
  2203. */
  2204. int wolfSSL_ASN1_STRING_set(WOLFSSL_ASN1_STRING* asn1, const void* data, int sz)
  2205. {
  2206. int ret = 1;
  2207. #ifdef WOLFSSL_DEBUG_OPENSSL
  2208. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_set");
  2209. #endif
  2210. /* Validate parameters. */
  2211. if ((asn1 == NULL) || ((data == NULL) && (sz != 0))) {
  2212. ret = 0;
  2213. }
  2214. /* Calculate size from data if not passed in. */
  2215. if ((ret == 1) && (sz < 0)) {
  2216. sz = (int)XSTRLEN((const char*)data);
  2217. if (sz < 0) {
  2218. ret = 0;
  2219. }
  2220. }
  2221. if (ret == 1) {
  2222. /* Dispose of any existing dynamic data. */
  2223. if (asn1->isDynamic) {
  2224. XFREE(asn1->data, NULL, DYNAMIC_TYPE_OPENSSL);
  2225. asn1->data = NULL;
  2226. }
  2227. /* Check string will fit - including NUL. */
  2228. if (sz + 1 > CTC_NAME_SIZE) {
  2229. /* Allocate new buffer. */
  2230. asn1->data = (char*)XMALLOC((size_t)(sz + 1), NULL,
  2231. DYNAMIC_TYPE_OPENSSL);
  2232. if (asn1->data == NULL) {
  2233. ret = 0;
  2234. }
  2235. else {
  2236. /* Ensure buffer will be freed. */
  2237. asn1->isDynamic = 1;
  2238. }
  2239. }
  2240. else {
  2241. /* Clear out fixed array and use it for data. */
  2242. XMEMSET(asn1->strData, 0, CTC_NAME_SIZE);
  2243. asn1->data = asn1->strData;
  2244. asn1->isDynamic = 0;
  2245. }
  2246. }
  2247. if (ret == 1) {
  2248. /* Check if there is a string to copy. */
  2249. if (data != NULL) {
  2250. /* Copy string and append NUL. */
  2251. XMEMCPY(asn1->data, data, (size_t)sz);
  2252. asn1->data[sz] = '\0';
  2253. }
  2254. /* Set size of string. */
  2255. asn1->length = sz;
  2256. }
  2257. return ret;
  2258. }
  2259. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  2260. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  2261. !defined(WOLFCRYPT_ONLY)
  2262. #ifndef NO_CERTS
  2263. /* Make a UTF8 canonical version of ASN.1 STRING object's data.
  2264. *
  2265. * @param [in, out] asn ASN.1 STRING to set.
  2266. */
  2267. static void wolfssl_asn1_string_canonicalize(WOLFSSL_ASN1_STRING* asn)
  2268. {
  2269. char* src = asn->data;
  2270. char* p = asn->data + asn->length - 1;
  2271. int len = asn->length;
  2272. int i;
  2273. /* Trim whitespace from the tail. */
  2274. for (; (len > 0) && (XISSPACE((unsigned char)*p)); len--) {
  2275. p--;
  2276. }
  2277. if (len > 0) {
  2278. /* Trim whitespace from the head. */
  2279. for (; XISSPACE((unsigned char)*src); len--) {
  2280. src++;
  2281. }
  2282. }
  2283. /* Output at the start. */
  2284. p = asn->data;
  2285. /* Process each character in string after trim. */
  2286. for (i = 0; i < len; p++, i++) {
  2287. /* Check for non-ascii character. */
  2288. if (!XISASCII(*src)) {
  2289. /* Keep non-ascii character as-is. */
  2290. *p = *src++;
  2291. }
  2292. /* Check for whitespace. */
  2293. else if (XISSPACE((unsigned char)*src)) {
  2294. /* Only use space character for whitespace. */
  2295. *p = 0x20;
  2296. /* Skip any succeeding whitespace characters. */
  2297. while (XISSPACE((unsigned char)*++src)) {
  2298. i++;
  2299. }
  2300. }
  2301. else {
  2302. /* Convert to lower case. */
  2303. *p = (char)XTOLOWER((unsigned char)*src++);
  2304. }
  2305. }
  2306. /* Set actual length after canonicalization. */
  2307. asn->length = (int)(p - asn->data);
  2308. }
  2309. /* Make a canonical version of ASN.1 STRING object in ASN.1 STRING object.
  2310. *
  2311. * @param [in, out] asn_out ASN.1 STRING object to set.
  2312. * @param [in] asn_in ASN.1 STRING object to get data from.
  2313. * @return 1 on success.
  2314. * @return BAD_FUNC_ARG when asn_out or asn_in is NULL.
  2315. * @return 0 when no data.
  2316. * @return 0 when dynamic memory allocation fails.
  2317. */
  2318. int wolfSSL_ASN1_STRING_canon(WOLFSSL_ASN1_STRING* asn_out,
  2319. const WOLFSSL_ASN1_STRING* asn_in)
  2320. {
  2321. int ret = 1;
  2322. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_canon");
  2323. /* Validate parameters. */
  2324. if ((asn_out == NULL) || (asn_in == NULL)) {
  2325. WOLFSSL_MSG("invalid function arguments");
  2326. ret = BAD_FUNC_ARG;
  2327. }
  2328. if (ret == 1) {
  2329. switch (asn_in->type) {
  2330. case MBSTRING_UTF8:
  2331. case V_ASN1_PRINTABLESTRING:
  2332. /* Set type to UTF8. */
  2333. asn_out->type = MBSTRING_UTF8;
  2334. /* Dispose of any dynamic data already in asn_out. */
  2335. if (asn_out->isDynamic) {
  2336. XFREE(asn_out->data, NULL, DYNAMIC_TYPE_OPENSSL);
  2337. asn_out->data = NULL;
  2338. }
  2339. /* Make ASN.1 STRING into UTF8 buffer. */
  2340. asn_out->length = wolfSSL_ASN1_STRING_to_UTF8(
  2341. (unsigned char**)&asn_out->data,
  2342. (WOLFSSL_ASN1_STRING*)asn_in);
  2343. /* Check for error from creating UTF8 string. */
  2344. if (asn_out->length < 0) {
  2345. ret = 0;
  2346. }
  2347. else {
  2348. /* Data now dynamic after converting to UTF8. */
  2349. asn_out->isDynamic = 1;
  2350. /* Canonicalize the data. */
  2351. wolfssl_asn1_string_canonicalize(asn_out);
  2352. if (asn_out->length == 0) {
  2353. /* Dispose of data if canonicalization removes all
  2354. * characters. */
  2355. XFREE(asn_out->data, NULL, DYNAMIC_TYPE_OPENSSL);
  2356. asn_out->data = NULL;
  2357. asn_out->isDynamic = 0;
  2358. }
  2359. }
  2360. break;
  2361. default:
  2362. /* Unrecognized format - just copy. */
  2363. WOLFSSL_MSG("just copy string");
  2364. ret = wolfSSL_ASN1_STRING_copy(asn_out, asn_in);
  2365. }
  2366. }
  2367. return ret;
  2368. }
  2369. #endif /* !NO_CERTS */
  2370. #endif /* (OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL) && !WOLFCRYPT_ONLY */
  2371. #if defined(OPENSSL_EXTRA)
  2372. #if !defined(NO_ASN)
  2373. #ifndef NO_BIO
  2374. /* Returns boolean indicating character is unprintable.
  2375. *
  2376. * @param [in] c ASCII character.
  2377. * @return 1 when character is unprintable.
  2378. * @return 0 when character is printable.
  2379. */
  2380. static int wolfssl_unprintable_char(char c)
  2381. {
  2382. const unsigned char last_unprintable = 31;
  2383. const unsigned char LF = 10; /* Line Feed */
  2384. const unsigned char CR = 13; /* Carriage Return */
  2385. return (c <= last_unprintable) && (c != LF) && (c != CR);
  2386. }
  2387. /* Print ASN.1 STRING to BIO.
  2388. *
  2389. * TODO: Unprintable characters conversion is destructive.
  2390. *
  2391. * @param [in] bio BIO to print to.
  2392. * @param [in] str ASN.1 STRING to print.
  2393. * @return Length of string written on success.
  2394. * @return 0 when bio or str is NULL.
  2395. * @return 0 when writing to BIO fails.
  2396. */
  2397. int wolfSSL_ASN1_STRING_print(WOLFSSL_BIO *bio, WOLFSSL_ASN1_STRING *str)
  2398. {
  2399. int len = 0;
  2400. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_print");
  2401. /* Validate parameters. */
  2402. if ((bio != NULL) && (str != NULL)) {
  2403. int i;
  2404. len = str->length;
  2405. /* Convert all unprintable characters to '.'. */
  2406. for (i = 0; i < len; i++) {
  2407. if (wolfssl_unprintable_char(str->data[i])) {
  2408. str->data[i] = '.';
  2409. }
  2410. }
  2411. /* Write string to BIO. */
  2412. if (wolfSSL_BIO_write(bio, str->data, len) != len) {
  2413. len = 0;
  2414. }
  2415. }
  2416. return len;
  2417. }
  2418. #endif /* !NO_BIO */
  2419. #endif /* !NO_ASN */
  2420. /* Get a string for the ASN.1 tag.
  2421. *
  2422. * @param [in] tag ASN.1 tag.
  2423. * @return A string.
  2424. */
  2425. const char* wolfSSL_ASN1_tag2str(int tag)
  2426. {
  2427. static const char *const tag_label[31] = {
  2428. "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", "NULL",
  2429. "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", "ENUMERATED",
  2430. "<ASN1 11>", "UTF8STRING", "<ASN1 13>", "<ASN1 14>", "<ASN1 15>",
  2431. "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
  2432. "VIDEOTEXTSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
  2433. "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "UNIVERSALSTRING",
  2434. "<ASN1 29>", "BMPSTRING"
  2435. };
  2436. const char* str = "(unknown)";
  2437. /* Clear negative flag. */
  2438. if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED)) {
  2439. tag &= ~V_ASN1_NEG;
  2440. }
  2441. /* Check for known basic types. */
  2442. if ((tag >= 0) && (tag <= 30)) {
  2443. str = tag_label[tag];
  2444. }
  2445. return str;
  2446. }
  2447. #ifndef NO_BIO
  2448. /* Print out ASN.1 tag for the ASN.1 STRING to the BIO.
  2449. *
  2450. * @param [in] bio BIO to write to.
  2451. * @param [in] str ASN.1 STRING object.
  2452. * @return Number of characters written on success.
  2453. * @return 0 when BIO write fails.
  2454. */
  2455. static int wolfssl_string_print_type(WOLFSSL_BIO *bio, WOLFSSL_ASN1_STRING *str)
  2456. {
  2457. int type_len;
  2458. const char *tag;
  2459. /* Get tag and string length. */
  2460. tag = wolfSSL_ASN1_tag2str(str->type);
  2461. type_len = (int)XSTRLEN(tag);
  2462. /* Write tag to BIO. */
  2463. if (wolfSSL_BIO_write(bio, tag, type_len) != type_len){
  2464. type_len = 0;
  2465. }
  2466. /* Write colon after tag string. */
  2467. else if (wolfSSL_BIO_write(bio, ":", 1) != 1) {
  2468. type_len = 0;
  2469. }
  2470. else {
  2471. /* Written colon - update count. */
  2472. type_len++;
  2473. }
  2474. return type_len;
  2475. }
  2476. /* Dump hex digit representation of each string character to BIO.
  2477. *
  2478. * TODO: Assumes length is only one byte ie less than 128 characters long.
  2479. *
  2480. * @param [in] bio BIO to write to.
  2481. * @param [in] str ASN.1 STRING object.
  2482. * @param [in] asDer Whether to write out as a DER encoding.
  2483. * @return Number of characters written to BIO on success.
  2484. * @return -1 when writing to BIO fails.
  2485. */
  2486. static int wolfssl_asn1_string_dump_hex(WOLFSSL_BIO *bio,
  2487. WOLFSSL_ASN1_STRING *str, int asDer)
  2488. {
  2489. const char* hash="#";
  2490. char hex_tmp[4];
  2491. int str_len = 1;
  2492. /* Write out hash character to indicate hex string. */
  2493. if (wolfSSL_BIO_write(bio, hash, 1) != 1) {
  2494. str_len = -1;
  2495. }
  2496. else {
  2497. /* Check if we are to write out DER header. */
  2498. if (asDer) {
  2499. /* Encode tag and length as hex into temporary. */
  2500. ByteToHexStr((byte)str->type, &hex_tmp[0]);
  2501. ByteToHexStr((byte)str->length, &hex_tmp[2]);
  2502. /* Update count of written characters: tag and length. */
  2503. str_len += 4;
  2504. /* Write out tag and length as hex digits. */
  2505. if (wolfSSL_BIO_write(bio, hex_tmp, 4) != 4) {
  2506. str_len = -1;
  2507. }
  2508. }
  2509. }
  2510. if (str_len != -1) {
  2511. char* p;
  2512. char* end;
  2513. /* Calculate end of string. */
  2514. end = str->data + str->length - 1;
  2515. for (p = str->data; p <= end; p++) {
  2516. /* Encode string character as hex into temporary. */
  2517. ByteToHexStr((byte)*p, hex_tmp);
  2518. /* Update count of written characters. */
  2519. str_len += 2;
  2520. /* Write out character as hex digites. */
  2521. if (wolfSSL_BIO_write(bio, hex_tmp, 2) != 2) {
  2522. str_len = -1;
  2523. break;
  2524. }
  2525. }
  2526. }
  2527. return str_len;
  2528. }
  2529. /* Check whether character needs to be escaped.
  2530. *
  2531. * @param [in] c Character to check for.
  2532. * @param [in] str String to check.
  2533. * @return 1 when character found.
  2534. * @return 0 when character not found.
  2535. */
  2536. static int wolfssl_check_esc_char(char c)
  2537. {
  2538. int ret = 0;
  2539. const char esc_ch[] = "+;<>\\";
  2540. const char* p = esc_ch;
  2541. /* Check if character matches any of those needing escaping. */
  2542. for (; (*p) != '\0'; p++) {
  2543. /* Check if character matches escape character. */
  2544. if (c == (*p)) {
  2545. ret = 1;
  2546. break;
  2547. }
  2548. }
  2549. return ret;
  2550. }
  2551. /* Print out string, with escaping for special characters, to BIO.
  2552. *
  2553. * @param [in] bio BIO to write to.
  2554. * @param [in] str ASN.1 STRING object.
  2555. * @return Number of characters written to BIO on success.
  2556. * @return -1 when writing to BIO fails.
  2557. */
  2558. static int wolfssl_asn1_string_print_esc_2253(WOLFSSL_BIO *bio,
  2559. WOLFSSL_ASN1_STRING *str)
  2560. {
  2561. char* p;
  2562. int str_len = 0;
  2563. /* Write all of string character by character. */
  2564. for (p = str->data; (*p) != '\0'; p++) {
  2565. /* Check if character needs escaping. */
  2566. if (wolfssl_check_esc_char(*p)){
  2567. /* Update count of written characters. */
  2568. str_len++;
  2569. /* Write out escaping character. */
  2570. if (wolfSSL_BIO_write(bio,"\\", 1) != 1) {
  2571. str_len = -1;
  2572. break;
  2573. }
  2574. }
  2575. /* Update count of written characters. */
  2576. str_len++;
  2577. /* Write out character. */
  2578. if (wolfSSL_BIO_write(bio, p, 1) != 1) {
  2579. str_len = -1;
  2580. break;
  2581. }
  2582. }
  2583. return str_len;
  2584. }
  2585. /* Extended print ASN.1 STRING to BIO.
  2586. *
  2587. * @param [in] bio BIO to print to.
  2588. * @param [in] str ASN.1 STRING to print.
  2589. * @param [in] flags Flags describing output format.
  2590. * @return Length of string written on success.
  2591. * @return 0 when bio or str is NULL.
  2592. * @return 0 when writing to BIO fails.
  2593. */
  2594. int wolfSSL_ASN1_STRING_print_ex(WOLFSSL_BIO *bio, WOLFSSL_ASN1_STRING *str,
  2595. unsigned long flags)
  2596. {
  2597. int err = 0;
  2598. int str_len = -1;
  2599. int type_len = 0;
  2600. WOLFSSL_ENTER("wolfSSL_ASN1_STRING_PRINT_ex");
  2601. /* Validate parameters. */
  2602. if ((bio == NULL) || (str == NULL)) {
  2603. err = 1;
  2604. }
  2605. /* Check if ASN.1 type is to be printed. */
  2606. if ((!err) && (flags & ASN1_STRFLGS_SHOW_TYPE)) {
  2607. /* Print type and colon to BIO. */
  2608. type_len = wolfssl_string_print_type(bio, str);
  2609. if (type_len == 0) {
  2610. err = 1;
  2611. }
  2612. }
  2613. if (!err) {
  2614. if (flags & ASN1_STRFLGS_DUMP_ALL) {
  2615. /* Dump hex. */
  2616. str_len = wolfssl_asn1_string_dump_hex(bio, str,
  2617. flags & ASN1_STRFLGS_DUMP_DER);
  2618. }
  2619. else if (flags & ASN1_STRFLGS_ESC_2253) {
  2620. /* Print out string with escaping. */
  2621. str_len = wolfssl_asn1_string_print_esc_2253(bio, str);
  2622. }
  2623. else {
  2624. /* Get number of characters to write. */
  2625. str_len = str->length;
  2626. /* Print out string as is. */
  2627. if (wolfSSL_BIO_write(bio, str->data, str_len) != str_len) {
  2628. err = 1;
  2629. }
  2630. }
  2631. }
  2632. if ((!err) && (str_len != -1)) {
  2633. /* Include any characters written for type. */
  2634. str_len += type_len;
  2635. }
  2636. else {
  2637. str_len = 0;
  2638. }
  2639. return str_len;
  2640. }
  2641. #endif /* !NO_BIO */
  2642. #endif /* OPENSSL_EXTRA */
  2643. /*******************************************************************************
  2644. * ASN1_GENERALIZEDTIME APIs
  2645. ******************************************************************************/
  2646. #ifdef OPENSSL_EXTRA
  2647. /* Free the static ASN.1 GENERALIZED TIME object.
  2648. *
  2649. * Not an OpenSSL compatibility API.
  2650. *
  2651. * @param [in] asn1Time ASN.1 GENERALIZED TIME object.
  2652. */
  2653. void wolfSSL_ASN1_GENERALIZEDTIME_free(WOLFSSL_ASN1_TIME* asn1Time)
  2654. {
  2655. WOLFSSL_ENTER("wolfSSL_ASN1_GENERALIZEDTIME_free");
  2656. if (asn1Time != NULL) {
  2657. XMEMSET(asn1Time->data, 0, sizeof(asn1Time->data));
  2658. }
  2659. }
  2660. #ifndef NO_BIO
  2661. /* Return the month as a string.
  2662. *
  2663. * Assumes n is '01'-'12'.
  2664. *
  2665. * @param [in] n The number of the month as a two characters (1 based).
  2666. * @return Month as a string.
  2667. */
  2668. static WC_INLINE const char* MonthStr(const char* n)
  2669. {
  2670. static const char monthStr[12][4] = {
  2671. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  2672. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  2673. const char* month = "BAD";
  2674. int i;
  2675. i = (n[0] - '0') * 10 + (n[1] - '0') - 1;
  2676. /* Convert string to number and index table. */
  2677. if ((i >= 0) && (i < 12)) {
  2678. month = monthStr[i];
  2679. }
  2680. return month;
  2681. }
  2682. /* Print an ASN.1 GENERALIZED TIME to a BIO.
  2683. *
  2684. * @param [in] bio BIO to write to.
  2685. * @param [in] asnTime ASN.1 GENERALIZED TIME object.
  2686. * @return 1 on success.
  2687. * @return 0 when ASN.1 GENERALIZED TIME type is invalid.
  2688. * @return 0 when writing to BIO fails.
  2689. * @return BAD_FUNC_ARG when bio or asnTime is NULL.
  2690. */
  2691. int wolfSSL_ASN1_GENERALIZEDTIME_print(WOLFSSL_BIO* bio,
  2692. const WOLFSSL_ASN1_GENERALIZEDTIME* asnTime)
  2693. {
  2694. int ret = 1;
  2695. const char* p = NULL;
  2696. WOLFSSL_ENTER("wolfSSL_ASN1_GENERALIZEDTIME_print");
  2697. /* Validate parameters. */
  2698. if ((bio == NULL) || (asnTime == NULL)) {
  2699. ret = BAD_FUNC_ARG;
  2700. }
  2701. /* Check type is GENERALIZED TIME. */
  2702. if ((ret == 1) && (asnTime->type != V_ASN1_GENERALIZEDTIME)) {
  2703. WOLFSSL_MSG("Error, not GENERALIZED_TIME");
  2704. ret = 0;
  2705. }
  2706. if (ret == 1) {
  2707. /* Get the string. */
  2708. p = (const char *)(asnTime->data);
  2709. /* Print month as a 3 letter string. */
  2710. if (wolfSSL_BIO_write(bio, MonthStr(p + 4), 3) != 3) {
  2711. ret = 0;
  2712. }
  2713. }
  2714. /* Print space separator. */
  2715. if ((ret == 1) && (wolfSSL_BIO_write(bio, " ", 1) != 1)) {
  2716. ret = 0;
  2717. }
  2718. /* Print day. */
  2719. if ((ret == 1) && (wolfSSL_BIO_write(bio, p + 6, 2) != 2)) {
  2720. ret = 0;
  2721. }
  2722. /* Print space separator. */
  2723. if ((ret == 1) && (wolfSSL_BIO_write(bio, " ", 1) != 1)) {
  2724. ret = 0;
  2725. }
  2726. /* Print hour. */
  2727. if ((ret == 1) && (wolfSSL_BIO_write(bio, p + 8, 2) != 2)) {
  2728. ret = 0;
  2729. }
  2730. /* Print time separator - colon. */
  2731. if ((ret == 1) && (wolfSSL_BIO_write(bio, ":", 1) != 1)) {
  2732. ret = 0;
  2733. }
  2734. /* Print minutes. */
  2735. if ((ret == 1) && (wolfSSL_BIO_write(bio, p + 10, 2) != 2)) {
  2736. ret = 0;
  2737. }
  2738. /* Print time separator - colon. */
  2739. if ((ret == 1) && (wolfSSL_BIO_write(bio, ":", 1) != 1)) {
  2740. ret = 0;
  2741. }
  2742. /* Print seconds. */
  2743. if ((ret == 1) && (wolfSSL_BIO_write(bio, p + 12, 2) != 2)) {
  2744. ret = 0;
  2745. }
  2746. /* Print space separator. */
  2747. if ((ret == 1) && (wolfSSL_BIO_write(bio, " ", 1) != 1)) {
  2748. ret = 0;
  2749. }
  2750. /* Print year. */
  2751. if ((ret == 1) && (wolfSSL_BIO_write(bio, p, 4) != 4)) {
  2752. ret = 0;
  2753. }
  2754. return ret;
  2755. }
  2756. #endif /* !NO_BIO */
  2757. #endif /* OPENSSL_EXTRA */
  2758. /*******************************************************************************
  2759. * ASN1_TIME APIs
  2760. ******************************************************************************/
  2761. #ifndef NO_ASN_TIME
  2762. #ifdef OPENSSL_EXTRA
  2763. /* Allocate a new ASN.1 TIME object.
  2764. *
  2765. * @return New empty ASN.1 TIME object on success.
  2766. * @return NULL when dynamic memory allocation fails.
  2767. */
  2768. WOLFSSL_ASN1_TIME* wolfSSL_ASN1_TIME_new(void)
  2769. {
  2770. WOLFSSL_ASN1_TIME* ret;
  2771. /* Allocate a new ASN.1 TYPE object. */
  2772. ret = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  2773. DYNAMIC_TYPE_OPENSSL);
  2774. if (ret != NULL) {
  2775. /* Clear out fields. */
  2776. XMEMSET(ret, 0, sizeof(WOLFSSL_ASN1_TIME));
  2777. }
  2778. return ret;
  2779. }
  2780. /* Dispose of ASN.1 TIME object.
  2781. *
  2782. * @param [in, out] t ASN.1 TIME object.
  2783. */
  2784. void wolfSSL_ASN1_TIME_free(WOLFSSL_ASN1_TIME* t)
  2785. {
  2786. /* Dispose of ASN.1 TIME object. */
  2787. XFREE(t, NULL, DYNAMIC_TYPE_OPENSSL);
  2788. }
  2789. #ifndef NO_WOLFSSL_STUB
  2790. /* Set the Unix time GMT into ASN.1 TIME object.
  2791. *
  2792. * Not implemented.
  2793. *
  2794. * @param [in, out] a ASN.1 TIME object.
  2795. * @param [in] t Unix time GMT.
  2796. * @return An ASN.1 TIME object.
  2797. */
  2798. WOLFSSL_ASN1_TIME *wolfSSL_ASN1_TIME_set(WOLFSSL_ASN1_TIME *a, time_t t)
  2799. {
  2800. WOLFSSL_STUB("wolfSSL_ASN1_TIME_set");
  2801. (void)a;
  2802. (void)t;
  2803. return a;
  2804. }
  2805. #endif /* !NO_WOLFSSL_STUB */
  2806. /* Convert time to Unix time (GMT).
  2807. *
  2808. * @param [in] sec Second in minute. 0-59.
  2809. * @param [in] minute Minute in hour. 0-59.
  2810. * @param [in] hour Hour in day. 0-23.
  2811. * @param [in] mday Day of month. 1-31.
  2812. * @param [in] mon Month of year. 0-11
  2813. * @param [in] year Year including century. ie: 1991, 2023, 2048.
  2814. * @return Seconds since 00:00:00 01/01/1970 for the time passed in.
  2815. */
  2816. static long long wolfssl_time_to_unix_time(int sec, int minute, int hour,
  2817. int mday, int mon, int year)
  2818. {
  2819. /* Number of cumulative days from the previous months, starting from
  2820. * beginning of January. */
  2821. static const int monthDaysCumulative [12] = {
  2822. 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  2823. };
  2824. int leapDays = year;
  2825. /* Leap day at end of February. */
  2826. if (mon <= 1) {
  2827. --leapDays;
  2828. }
  2829. /* Calculate leap days. */
  2830. leapDays = leapDays / 4 - leapDays / 100 + leapDays / 400 - 1969 / 4 +
  2831. 1969 / 100 - 1969 / 400;
  2832. /* Calculate number of seconds. */
  2833. return ((((long long) (year - 1970) * 365 + leapDays +
  2834. monthDaysCumulative[mon] + mday - 1) * 24 + hour) * 60 + minute) *
  2835. 60 + sec;
  2836. }
  2837. /* Convert ASN.1 TIME object to Unix time (GMT).
  2838. *
  2839. * @param [in] t ASN.1 TIME object.
  2840. * @param [out] secs Number of seconds since 00:00:00 01/01/1970.
  2841. * @return 1 on success.
  2842. * @return 0 when conversion of time fails.
  2843. */
  2844. static int wolfssl_asn1_time_to_secs(const WOLFSSL_ASN1_TIME* t,
  2845. long long* secs)
  2846. {
  2847. int ret = 1;
  2848. struct tm tm_s;
  2849. struct tm *tmGmt = &tm_s;
  2850. /* Convert ASN.1 TIME to broken-down time. NULL treated as current time. */
  2851. ret = wolfSSL_ASN1_TIME_to_tm(t, tmGmt);
  2852. if (ret != 1) {
  2853. WOLFSSL_MSG("Failed to convert from time to struct tm.");
  2854. }
  2855. else {
  2856. /* We use wolfssl_time_to_unix_time here instead of XMKTIME to avoid the
  2857. * Year 2038 problem on platforms where time_t is 32 bits. struct tm
  2858. * stores the year as years since 1900, so we add 1900 to the year. */
  2859. *secs = wolfssl_time_to_unix_time(tmGmt->tm_sec, tmGmt->tm_min,
  2860. tmGmt->tm_hour, tmGmt->tm_mday, tmGmt->tm_mon,
  2861. tmGmt->tm_year + 1900);
  2862. }
  2863. return ret;
  2864. }
  2865. /* Calculate difference in time of two ASN.1 TIME objects.
  2866. *
  2867. * @param [out] days Number of whole days between from and to.
  2868. * @param [out] secs Number of seconds less than a day between from and to.
  2869. * @param [in] from ASN.1 TIME object as start time.
  2870. * @param [in] to ASN.1 TIME object as end time.
  2871. * @return 1 on success.
  2872. * @return 0 when days or secs is NULL.
  2873. * @return 0 when conversion of time fails.
  2874. */
  2875. int wolfSSL_ASN1_TIME_diff(int *days, int *secs, const WOLFSSL_ASN1_TIME *from,
  2876. const WOLFSSL_ASN1_TIME *to)
  2877. {
  2878. int ret = 1;
  2879. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_diff");
  2880. /* Validate parameters. */
  2881. if (days == NULL) {
  2882. WOLFSSL_MSG("days is NULL");
  2883. ret = 0;
  2884. }
  2885. if ((ret == 1) && (secs == NULL)) {
  2886. WOLFSSL_MSG("secs is NULL");
  2887. ret = 0;
  2888. }
  2889. if ((ret == 1) && ((from == NULL) && (to == NULL))) {
  2890. *days = 0;
  2891. *secs = 0;
  2892. }
  2893. else if (ret == 1) {
  2894. const long long SECS_PER_DAY = 24 * 60 * 60;
  2895. long long fromSecs;
  2896. long long toSecs = 0;
  2897. ret = wolfssl_asn1_time_to_secs(from, &fromSecs);
  2898. if (ret == 1) {
  2899. ret = wolfssl_asn1_time_to_secs(to, &toSecs);
  2900. }
  2901. if (ret == 1) {
  2902. long long diffSecs = toSecs - fromSecs;
  2903. *days = (int) (diffSecs / SECS_PER_DAY);
  2904. *secs = (int) (diffSecs - ((long long)*days * SECS_PER_DAY));
  2905. }
  2906. }
  2907. return ret;
  2908. }
  2909. /* Compare two ASN.1 TIME objects by comparing time value.
  2910. *
  2911. * @param [in] a First ASN.1 TIME object.
  2912. * @param [in] b Second ASN.1 TIME object.
  2913. * @return Negative value when a is less than b.
  2914. * @return 0 when a equals b.
  2915. * @return Positive value when a is greater than b.
  2916. * @return -2 when a or b is invalid.
  2917. */
  2918. int wolfSSL_ASN1_TIME_compare(const WOLFSSL_ASN1_TIME *a,
  2919. const WOLFSSL_ASN1_TIME *b)
  2920. {
  2921. int ret;
  2922. int days;
  2923. int secs;
  2924. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_compare");
  2925. /* Calculate difference in time between a and b. */
  2926. if (wolfSSL_ASN1_TIME_diff(&days, &secs, a, b) != 1) {
  2927. WOLFSSL_MSG("Failed to get time difference.");
  2928. ret = -2;
  2929. }
  2930. else if (days == 0 && secs == 0) {
  2931. /* a and b are the same time. */
  2932. ret = 0;
  2933. }
  2934. else if (days >= 0 && secs >= 0) {
  2935. /* a is before b. */
  2936. ret = -1;
  2937. }
  2938. /* Assume wolfSSL_ASN1_TIME_diff creates coherent values. */
  2939. else {
  2940. ret = 1;
  2941. }
  2942. WOLFSSL_LEAVE("wolfSSL_ASN1_TIME_compare", ret);
  2943. return ret;
  2944. }
  2945. #if !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  2946. /* Adjust the time into an ASN.1 TIME object.
  2947. *
  2948. * @param [in] a ASN.1 TIME object. May be NULL.
  2949. * @param [in] t Time to offset.
  2950. * @param [in] offset_day Number of days to offset. May be negative.
  2951. * @param [in] offset_sec Number of seconds to offset. May be negative.
  2952. * @return ASN.1 TIME object on success.
  2953. * @return NULL when formatting time fails.
  2954. * @return NULL when dynamic memory allocation fails.
  2955. */
  2956. WOLFSSL_ASN1_TIME* wolfSSL_ASN1_TIME_adj(WOLFSSL_ASN1_TIME* a, time_t t,
  2957. int offset_day, long offset_sec)
  2958. {
  2959. WOLFSSL_ASN1_TIME* ret = NULL;
  2960. const time_t sec_per_day = 24*60*60;
  2961. int time_get;
  2962. char time_str[MAX_TIME_STRING_SZ];
  2963. time_t offset_day_sec = offset_day * sec_per_day;
  2964. time_t t_adj = t + offset_day_sec + offset_sec;
  2965. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_adj");
  2966. /* Get time string as either UTC or GeneralizedTime. */
  2967. time_get = GetFormattedTime(&t_adj, (byte*)time_str, MAX_TIME_STRING_SZ);
  2968. if (time_get > 0) {
  2969. ret = a;
  2970. if (ret == NULL) {
  2971. ret = wolfSSL_ASN1_TIME_new();
  2972. }
  2973. /* Set the string into the ASN.1 TIME object. */
  2974. if ((wolfSSL_ASN1_TIME_set_string(ret, time_str) != 1) && (ret != a)) {
  2975. wolfSSL_ASN1_TIME_free(ret);
  2976. ret = NULL;
  2977. }
  2978. }
  2979. return ret;
  2980. }
  2981. #endif /* !USER_TIME && !TIME_OVERRIDES */
  2982. /* Get the length of the ASN.1 TIME data.
  2983. *
  2984. * Not an OpenSSL function - ASN1_TIME is not opaque.
  2985. *
  2986. * @param [in] t ASN.1 TIME object.
  2987. * @return Length of data on success.
  2988. * @return 0 when t is NULL or no time set.
  2989. */
  2990. int wolfSSL_ASN1_TIME_get_length(const WOLFSSL_ASN1_TIME *t)
  2991. {
  2992. int len = 0;
  2993. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_get_length");
  2994. if (t != NULL) {
  2995. len = t->length;
  2996. }
  2997. return len;
  2998. }
  2999. /* Get the data from the ASN.1 TIME object.
  3000. *
  3001. * Not an OpenSSL function - ASN1_TIME is not opaque.
  3002. *
  3003. * @param [in] t ASN.1 TIME object.
  3004. * @return Data buffer on success.
  3005. * @return NULL when t is NULL.
  3006. */
  3007. unsigned char* wolfSSL_ASN1_TIME_get_data(const WOLFSSL_ASN1_TIME *t)
  3008. {
  3009. unsigned char* data = NULL;
  3010. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_get_data");
  3011. if (t != NULL) {
  3012. data = (unsigned char*)t->data;
  3013. }
  3014. return data;
  3015. }
  3016. /* Check format of string in ASN.1 TIME object.
  3017. *
  3018. * @param [in] a ASN.1 TIME object.
  3019. * @return 1 on success.
  3020. * @return 0 when format invalid.
  3021. */
  3022. int wolfSSL_ASN1_TIME_check(const WOLFSSL_ASN1_TIME* a)
  3023. {
  3024. int ret = 1;
  3025. char buf[MAX_TIME_STRING_SZ];
  3026. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_check");
  3027. /* If can convert to human readable then format good. */
  3028. if (wolfSSL_ASN1_TIME_to_string((WOLFSSL_ASN1_TIME*)a, buf,
  3029. MAX_TIME_STRING_SZ) == NULL) {
  3030. ret = 0;
  3031. }
  3032. return ret;
  3033. }
  3034. /* Set the time as a string into ASN.1 TIME object.
  3035. *
  3036. * When t is NULL, str is checked only.
  3037. *
  3038. * @param [in, out] t ASN.1 TIME object.
  3039. * @param [in] str Time as a string.
  3040. * @return 1 on success.
  3041. * @return 0 when str is NULL.
  3042. * @return 0 when str is not formatted correctly.
  3043. */
  3044. int wolfSSL_ASN1_TIME_set_string(WOLFSSL_ASN1_TIME *t, const char *str)
  3045. {
  3046. int ret = 1;
  3047. int slen = 0;
  3048. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_set_string");
  3049. if (str == NULL) {
  3050. WOLFSSL_MSG("Bad parameter");
  3051. ret = 0;
  3052. }
  3053. if (ret == 1) {
  3054. /* Get length of string including NUL terminator. */
  3055. slen = (int)XSTRLEN(str) + 1;
  3056. if (slen > CTC_DATE_SIZE) {
  3057. WOLFSSL_MSG("Date string too long");
  3058. ret = 0;
  3059. }
  3060. }
  3061. if ((ret == 1) && (t != NULL)) {
  3062. /* Copy in string including NUL terminator. */
  3063. XMEMCPY(t->data, str, (size_t)slen);
  3064. /* Do not include NUL terminator in length. */
  3065. t->length = slen - 1;
  3066. /* Set ASN.1 type based on string length. */
  3067. t->type = ((slen == ASN_UTC_TIME_SIZE) ? V_ASN1_UTCTIME :
  3068. V_ASN1_GENERALIZEDTIME);
  3069. }
  3070. return ret;
  3071. }
  3072. /* Convert ASN.1 TIME object to ASN.1 GENERALIZED TIME object.
  3073. *
  3074. * @param [in] t ASN.1 TIME object.
  3075. * @param [in, out] out ASN.1 GENERALIZED TIME object.
  3076. * @return ASN.1 GENERALIZED TIME object on success.
  3077. * @return NULL when t is NULL or t has wrong ASN.1 type.
  3078. * @return NULL when dynamic memory allocation fails.
  3079. */
  3080. WOLFSSL_ASN1_TIME* wolfSSL_ASN1_TIME_to_generalizedtime(WOLFSSL_ASN1_TIME *t,
  3081. WOLFSSL_ASN1_TIME **out)
  3082. {
  3083. WOLFSSL_ASN1_TIME *ret = NULL;
  3084. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_to_generalizedtime");
  3085. /* Validate parameters. */
  3086. if (t == NULL) {
  3087. WOLFSSL_MSG("Invalid ASN_TIME value");
  3088. }
  3089. /* Ensure ASN.1 type is one that is supported. */
  3090. else if ((t->type != V_ASN1_UTCTIME) &&
  3091. (t->type != V_ASN1_GENERALIZEDTIME)) {
  3092. WOLFSSL_MSG("Invalid ASN_TIME type.");
  3093. }
  3094. /* Check for ASN.1 GENERALIZED TIME object being passed in. */
  3095. else if ((out != NULL) && (*out != NULL)) {
  3096. /* Copy into the passed in object. */
  3097. ret = *out;
  3098. }
  3099. else {
  3100. /* Create a new ASN.1 GENERALIZED TIME object. */
  3101. ret = wolfSSL_ASN1_TIME_new();
  3102. if (ret == NULL) {
  3103. WOLFSSL_MSG("memory alloc failed.");
  3104. }
  3105. }
  3106. if (ret != NULL) {
  3107. /* Set the ASN.1 type and length of string. */
  3108. ret->type = V_ASN1_GENERALIZEDTIME;
  3109. ret->length = ASN_GENERALIZED_TIME_SIZE;
  3110. if (t->type == V_ASN1_GENERALIZEDTIME) {
  3111. /* Just copy as data already appropriately formatted. */
  3112. XMEMCPY(ret->data, t->data, ASN_GENERALIZED_TIME_SIZE);
  3113. }
  3114. else {
  3115. /* Convert UTC TIME to GENERALIZED TIME. */
  3116. if (t->data[0] >= '5') {
  3117. /* >= 50 is 1900s. */
  3118. ret->data[0] = '1'; ret->data[1] = '9';
  3119. }
  3120. else {
  3121. /* < 50 is 2000s. */
  3122. ret->data[0] = '2'; ret->data[1] = '0';
  3123. }
  3124. /* Append rest of the data as it is the same. */
  3125. XMEMCPY(&ret->data[2], t->data, ASN_UTC_TIME_SIZE);
  3126. }
  3127. /* Check for pointer to return result through. */
  3128. if (out != NULL) {
  3129. *out = ret;
  3130. }
  3131. }
  3132. return ret;
  3133. }
  3134. #endif /* OPENSSL_EXTRA */
  3135. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_EXTRA)
  3136. /* Get string from ASN.1 TIME object.
  3137. *
  3138. * Not an OpenSSL compatibility API.
  3139. *
  3140. * @param [in] t ASN.1 TIME object.
  3141. * @param [in, out] buf Buffer to put string in.
  3142. * @param [in] len Length of buffer in bytes.
  3143. * @return buf on success.
  3144. * @return NULL when t or buf is NULL, or len is less than 5.
  3145. * @return NULL when ASN.1 TIME length is larger than len.
  3146. * @return NULL when internal time format not valid.
  3147. */
  3148. char* wolfSSL_ASN1_TIME_to_string(WOLFSSL_ASN1_TIME* t, char* buf, int len)
  3149. {
  3150. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_to_string");
  3151. /* Validate parameters. */
  3152. if ((t == NULL) || (buf == NULL) || (len < 5)) {
  3153. WOLFSSL_MSG("Bad argument");
  3154. buf = NULL;
  3155. }
  3156. /* Check internal length against passed in length. */
  3157. if ((buf != NULL) && (t->length > len)) {
  3158. WOLFSSL_MSG("Length of date is longer then buffer");
  3159. buf = NULL;
  3160. }
  3161. /* Get time as human readable string. */
  3162. if ((buf != NULL) && !GetTimeString(t->data, t->type, buf, len)) {
  3163. buf = NULL;
  3164. }
  3165. return buf;
  3166. }
  3167. /* Number of characters in a UTC TIME string. */
  3168. #define UTCTIME_LEN 13
  3169. /* Get year from UTC TIME string.
  3170. *
  3171. * @param [in] str UTC TIME string.
  3172. * @param [in] len Length of string in bytes.
  3173. * @param [out] year Year as extracted from string.
  3174. * @return 1 on success.
  3175. * @return 0 when length is too short for a UTC TIME.
  3176. * @return 0 when not ZULU time.
  3177. */
  3178. static int wolfssl_utctime_year(const unsigned char* str, int len, int* year)
  3179. {
  3180. int ret = 1;
  3181. /* Check minimal length for UTC TIME. */
  3182. if (len < UTCTIME_LEN) {
  3183. WOLFSSL_MSG("WOLFSSL_ASN1_TIME buffer length is invalid.");
  3184. ret = 0;
  3185. }
  3186. /* Only support ZULU time. */
  3187. if ((ret == 1) && (str[UTCTIME_LEN - 1] != 'Z')) {
  3188. WOLFSSL_MSG("Expecting UTC time.");
  3189. ret = 0;
  3190. }
  3191. if (ret == 1) {
  3192. int tm_year;
  3193. /* 2-digit year. */
  3194. tm_year = (str[0] - '0') * 10;
  3195. tm_year += str[1] - '0';
  3196. /* Check for year being in the 2000s. */
  3197. if (tm_year < 50) {
  3198. tm_year += 100;
  3199. }
  3200. *year = tm_year;
  3201. }
  3202. return ret;
  3203. }
  3204. /* Number of characters in a GENERALIZED TIME string. */
  3205. #define GENTIME_LEN 15
  3206. /* Get year from GENERALIZED TIME string.
  3207. *
  3208. * @param [in] str GENERALIZED TIME string.
  3209. * @param [in] len Length of string in bytes.
  3210. * @param [out] year Year as extracted from string.
  3211. * @return 1 on success.
  3212. * @return 0 when length is too short for a GENERALIZED TIME.
  3213. * @return 0 when not ZULU time.
  3214. */
  3215. static int wolfssl_gentime_year(const unsigned char* str, int len, int* year)
  3216. {
  3217. int ret = 1;
  3218. /* Check minimal length for GENERALIZED TIME. */
  3219. if (len < GENTIME_LEN) {
  3220. WOLFSSL_MSG("WOLFSSL_ASN1_TIME buffer length is invalid.");
  3221. ret = 0;
  3222. }
  3223. if ((ret == 1) && (str[GENTIME_LEN - 1] != 'Z')) {
  3224. WOLFSSL_MSG("Expecting Generalized time.");
  3225. ret = 0;
  3226. }
  3227. if (ret == 1) {
  3228. int tm_year;
  3229. /* 4-digit year. */
  3230. tm_year = (str[0] - '0') * 1000;
  3231. tm_year += (str[1] - '0') * 100;
  3232. tm_year += (str[2] - '0') * 10;
  3233. tm_year += str[3] - '0';
  3234. /* Only need value to be years since 1900. */
  3235. tm_year -= 1900;
  3236. *year = tm_year;
  3237. }
  3238. return ret;
  3239. }
  3240. /* Convert an ASN.1 TIME to a struct tm.
  3241. *
  3242. * @param [in] asnTime ASN.1 TIME object.
  3243. * @param [in] tm Broken-down time. Must be non-NULL.
  3244. * @return 1 on success.
  3245. * @return 0 when string format is invalid.
  3246. */
  3247. static int wolfssl_asn1_time_to_tm(const WOLFSSL_ASN1_TIME* asnTime,
  3248. struct tm* tm)
  3249. {
  3250. int ret = 1;
  3251. const unsigned char* asn1TimeBuf;
  3252. int asn1TimeBufLen;
  3253. int i = 0;
  3254. #ifdef XMKTIME
  3255. struct tm localTm;
  3256. XMEMSET(&localTm, 0, sizeof localTm);
  3257. #endif
  3258. /* Get the string buffer - fixed array, can't fail. */
  3259. asn1TimeBuf = wolfSSL_ASN1_TIME_get_data(asnTime);
  3260. /* Get the length of the string. */
  3261. asn1TimeBufLen = wolfSSL_ASN1_TIME_get_length(asnTime);
  3262. if (asn1TimeBufLen <= 0) {
  3263. WOLFSSL_MSG("Failed to get WOLFSSL_ASN1_TIME buffer length.");
  3264. ret = 0;
  3265. }
  3266. if (ret == 1) {
  3267. /* Zero out values in broken-down time. */
  3268. XMEMSET(tm, 0, sizeof(struct tm));
  3269. if (asnTime->type == V_ASN1_UTCTIME) {
  3270. /* Get year from UTC TIME string. */
  3271. int tm_year;
  3272. if ((ret = wolfssl_utctime_year(asn1TimeBuf, asn1TimeBufLen,
  3273. &tm_year)) == 1) {
  3274. tm->tm_year = tm_year;
  3275. /* Month starts after year - 2 characters. */
  3276. i = 2;
  3277. }
  3278. }
  3279. else if (asnTime->type == V_ASN1_GENERALIZEDTIME) {
  3280. /* Get year from GENERALIZED TIME string. */
  3281. int tm_year;
  3282. if ((ret = wolfssl_gentime_year(asn1TimeBuf, asn1TimeBufLen,
  3283. &tm_year)) == 1) {
  3284. tm->tm_year = tm_year;
  3285. /* Month starts after year - 4 characters. */
  3286. i = 4;
  3287. }
  3288. }
  3289. else {
  3290. /* No other time formats known. */
  3291. WOLFSSL_MSG("asnTime->type is invalid.");
  3292. ret = 0;
  3293. }
  3294. }
  3295. if (ret == 1) {
  3296. /* Fill in rest of broken-down time from string. */
  3297. /* January is 0 not 1 */
  3298. tm->tm_mon = (asn1TimeBuf[i] - '0') * 10; i++;
  3299. tm->tm_mon += (asn1TimeBuf[i] - '0') - 1; i++;
  3300. tm->tm_mday = (asn1TimeBuf[i] - '0') * 10; i++;
  3301. tm->tm_mday += (asn1TimeBuf[i] - '0'); i++;
  3302. tm->tm_hour = (asn1TimeBuf[i] - '0') * 10; i++;
  3303. tm->tm_hour += (asn1TimeBuf[i] - '0'); i++;
  3304. tm->tm_min = (asn1TimeBuf[i] - '0') * 10; i++;
  3305. tm->tm_min += (asn1TimeBuf[i] - '0'); i++;
  3306. tm->tm_sec = (asn1TimeBuf[i] - '0') * 10; i++;
  3307. tm->tm_sec += (asn1TimeBuf[i] - '0');
  3308. #ifdef XMKTIME
  3309. XMEMCPY(&localTm, tm, sizeof(struct tm));
  3310. /* Call XMKTIME on tm to get tm_wday and tm_yday fields populated.
  3311. Note that localTm is used here to avoid modifying other fields,
  3312. such as tm_isdst/tm_gmtoff. */
  3313. XMKTIME(&localTm);
  3314. tm->tm_wday = localTm.tm_wday;
  3315. tm->tm_yday = localTm.tm_yday;
  3316. #endif
  3317. }
  3318. return ret;
  3319. }
  3320. /* Get the current time into a broken-down time.
  3321. *
  3322. * @param [out] tm Broken-time time.
  3323. * @return 1 on success.
  3324. * @return 0 when tm is NULL.
  3325. * @return 0 when get current time fails.
  3326. * @return 0 when converting Unix time to broken-down time fails.
  3327. */
  3328. static int wolfssl_get_current_time_tm(struct tm* tm)
  3329. {
  3330. int ret = 1;
  3331. time_t currentTime;
  3332. struct tm *tmpTs;
  3333. #if defined(NEED_TMP_TIME)
  3334. /* for use with gmtime_r */
  3335. struct tm tmpTimeStorage;
  3336. tmpTs = &tmpTimeStorage;
  3337. #else
  3338. tmpTs = NULL;
  3339. #endif
  3340. (void)tmpTs;
  3341. /* Must have a pointer to return result into. */
  3342. if (tm == NULL) {
  3343. WOLFSSL_MSG("asnTime and tm are both NULL");
  3344. ret = 0;
  3345. }
  3346. if (ret == 1) {
  3347. /* Get current Unix Time GMT. */
  3348. currentTime = wc_Time(0);
  3349. if (currentTime <= 0) {
  3350. WOLFSSL_MSG("Failed to get current time.");
  3351. ret = 0;
  3352. }
  3353. }
  3354. if (ret == 1) {
  3355. /* Convert Unix Time GMT into broken-down time. */
  3356. tmpTs = XGMTIME(&currentTime, tmpTs);
  3357. if (tmpTs == NULL) {
  3358. WOLFSSL_MSG("Failed to convert current time to UTC.");
  3359. ret = 0;
  3360. }
  3361. }
  3362. if (ret == 1) {
  3363. /* Copy from the structure returned into parameter. */
  3364. XMEMCPY(tm, tmpTs, sizeof(*tm));
  3365. }
  3366. return ret;
  3367. }
  3368. /* Convert ASN.1 TIME object's time into broken-down representation.
  3369. *
  3370. * Internal time string is checked when tm is NULL.
  3371. *
  3372. * @param [in] asnTime ASN.1 TIME object.
  3373. * @param [out] tm Broken-down time.
  3374. * @return 1 on success.
  3375. * @return 0 when asnTime is NULL and tm is NULL.
  3376. * @return 0 getting current time fails.
  3377. * @return 0 when internal time string is invalid.
  3378. */
  3379. int wolfSSL_ASN1_TIME_to_tm(const WOLFSSL_ASN1_TIME* asnTime, struct tm* tm)
  3380. {
  3381. int ret = 1;
  3382. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_to_tm");
  3383. /* If asnTime is NULL, then the current time is converted. */
  3384. if (asnTime == NULL) {
  3385. ret = wolfssl_get_current_time_tm(tm);
  3386. }
  3387. /* If tm is NULL this function performs a format check on asnTime only. */
  3388. else if (tm == NULL) {
  3389. ret = wolfSSL_ASN1_TIME_check(asnTime);
  3390. }
  3391. else {
  3392. /* Convert ASN.1 TIME to broken-down time. */
  3393. ret = wolfssl_asn1_time_to_tm(asnTime, tm);
  3394. }
  3395. return ret;
  3396. }
  3397. #ifndef NO_BIO
  3398. /* Print the ASN.1 TIME object as a string to BIO.
  3399. *
  3400. * @param [in] bio BIO to write to.
  3401. * @param [in] asnTime ASN.1 TIME object.
  3402. * @return 1 on success.
  3403. * @return 0 when bio or asnTime is NULL.
  3404. * @return 0 when creating human readable string fails.
  3405. * @return 0 when writing to BIO fails.
  3406. */
  3407. int wolfSSL_ASN1_TIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_TIME* asnTime)
  3408. {
  3409. int ret = 1;
  3410. WOLFSSL_ENTER("wolfSSL_ASN1_TIME_print");
  3411. /* Validate parameters. */
  3412. if ((bio == NULL) || (asnTime == NULL)) {
  3413. WOLFSSL_MSG("NULL function argument");
  3414. ret = 0;
  3415. }
  3416. if (ret == 1) {
  3417. char buf[MAX_TIME_STRING_SZ];
  3418. int len;
  3419. /* Create human readable string. */
  3420. if (wolfSSL_ASN1_TIME_to_string((WOLFSSL_ASN1_TIME*)asnTime, buf,
  3421. sizeof(buf)) == NULL) {
  3422. /* Write out something anyway but return error. */
  3423. XMEMSET(buf, 0, MAX_TIME_STRING_SZ);
  3424. XSTRNCPY(buf, "Bad time value", sizeof(buf)-1);
  3425. ret = 0;
  3426. }
  3427. /* Write out string. */
  3428. len = (int)XSTRLEN(buf);
  3429. if (wolfSSL_BIO_write(bio, buf, len) != len) {
  3430. WOLFSSL_MSG("Unable to write to bio");
  3431. ret = 0;
  3432. }
  3433. }
  3434. return ret;
  3435. }
  3436. #endif /* !NO_BIO */
  3437. #endif /* WOLFSSL_MYSQL_COMPATIBLE || OPENSSL_EXTRA */
  3438. #ifdef OPENSSL_EXTRA
  3439. #ifndef NO_BIO
  3440. /* Print the ASN.1 UTC TIME object as a string to BIO.
  3441. *
  3442. * @param [in] bio BIO to write to.
  3443. * @param [in] asnTime ASN.1 UTC TIME object.
  3444. * @return 1 on success.
  3445. * @return 0 when bio or asnTime is NULL.
  3446. * @return 0 when ASN.1 type is not UTC TIME.
  3447. * @return 0 when creating human readable string fails.
  3448. * @return 0 when writing to BIO fails.
  3449. */
  3450. int wolfSSL_ASN1_UTCTIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_UTCTIME* a)
  3451. {
  3452. int ret = 1;
  3453. WOLFSSL_ENTER("wolfSSL_ASN1_UTCTIME_print");
  3454. /* Validate parameters. */
  3455. if ((bio == NULL) || (a == NULL)) {
  3456. ret = 0;
  3457. }
  3458. /* Validate ASN.1 UTC TIME object is of type UTC_TIME. */
  3459. if ((ret == 1) && (a->type != V_ASN1_UTCTIME)) {
  3460. WOLFSSL_MSG("Error, not UTC_TIME");
  3461. ret = 0;
  3462. }
  3463. if (ret == 1) {
  3464. /* Use generic time printing function to do work. */
  3465. ret = wolfSSL_ASN1_TIME_print(bio, a);
  3466. }
  3467. return ret;
  3468. }
  3469. #endif /* !NO_BIO */
  3470. #endif /* OPENSSL_EXTRA */
  3471. #endif /* !NO_ASN_TIME */
  3472. /*******************************************************************************
  3473. * ASN1_TYPE APIs
  3474. ******************************************************************************/
  3475. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  3476. /**
  3477. * Allocate a new ASN.1 TYPE object.
  3478. *
  3479. * @return New empty ASN.1 TYPE object on success.
  3480. * @return NULL when dynamic memory allocation fails.
  3481. */
  3482. WOLFSSL_ASN1_TYPE* wolfSSL_ASN1_TYPE_new(void)
  3483. {
  3484. WOLFSSL_ASN1_TYPE* ret;
  3485. /* Allocate a new ASN.1 TYPE object. */
  3486. ret = (WOLFSSL_ASN1_TYPE*)XMALLOC(sizeof(WOLFSSL_ASN1_TYPE), NULL,
  3487. DYNAMIC_TYPE_OPENSSL);
  3488. if (ret != NULL) {
  3489. /* Clear out fields. */
  3490. XMEMSET(ret, 0, sizeof(WOLFSSL_ASN1_TYPE));
  3491. }
  3492. return ret;
  3493. }
  3494. /* Free the ASN.1 TYPE object's value field.
  3495. *
  3496. * @param [in, out] at ASN.1 TYPE object.
  3497. */
  3498. static void wolfssl_asn1_type_free_value(WOLFSSL_ASN1_TYPE* at)
  3499. {
  3500. switch (at->type) {
  3501. case V_ASN1_NULL:
  3502. break;
  3503. case V_ASN1_OBJECT:
  3504. wolfSSL_ASN1_OBJECT_free(at->value.object);
  3505. break;
  3506. case V_ASN1_UTCTIME:
  3507. #if !defined(NO_ASN_TIME) && defined(OPENSSL_EXTRA)
  3508. wolfSSL_ASN1_TIME_free(at->value.utctime);
  3509. #endif
  3510. break;
  3511. case V_ASN1_GENERALIZEDTIME:
  3512. #if !defined(NO_ASN_TIME) && defined(OPENSSL_EXTRA)
  3513. wolfSSL_ASN1_TIME_free(at->value.generalizedtime);
  3514. #endif
  3515. break;
  3516. case V_ASN1_UTF8STRING:
  3517. case V_ASN1_PRINTABLESTRING:
  3518. case V_ASN1_T61STRING:
  3519. case V_ASN1_IA5STRING:
  3520. case V_ASN1_UNIVERSALSTRING:
  3521. case V_ASN1_SEQUENCE:
  3522. wolfSSL_ASN1_STRING_free(at->value.asn1_string);
  3523. break;
  3524. default:
  3525. break;
  3526. }
  3527. }
  3528. /**
  3529. * Free ASN.1 TYPE object and its value.
  3530. *
  3531. * @param [in, out] at ASN.1 TYPE object.
  3532. */
  3533. void wolfSSL_ASN1_TYPE_free(WOLFSSL_ASN1_TYPE* at)
  3534. {
  3535. if (at != NULL) {
  3536. /* Dispose of value in ASN.1 TYPE object. */
  3537. wolfssl_asn1_type_free_value(at);
  3538. }
  3539. /* Dispose of ASN.1 TYPE object. */
  3540. XFREE(at, NULL, DYNAMIC_TYPE_OPENSSL);
  3541. }
  3542. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  3543. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS) || \
  3544. defined(WOLFSSL_WPAS_SMALL)
  3545. /**
  3546. * Set ASN.1 TYPE object with a type and value.
  3547. *
  3548. * Type of value for different types:
  3549. * V_ASN1_NULL : Value should be NULL.
  3550. * V_ASN1_OBJECT : WOLFSSL_ASN1_OBJECT.
  3551. * V_ASN1_UTCTIME : WOLFSSL_ASN1_TIME.
  3552. * V_ASN1_GENERALIZEDTIME : WOLFSSL_ASN1_TIME.
  3553. * V_ASN1_UTF8STRING : WOLFSSL_ASN1_STRING.
  3554. * V_ASN1_PRINTABLESTRING : WOLFSSL_ASN1_STRING.
  3555. * V_ASN1_T61STRING : WOLFSSL_ASN1_STRING.
  3556. * V_ASN1_IA5STRING : WOLFSSL_ASN1_STRING.
  3557. * V_ASN1_UNINVERSALSTRING: WOLFSSL_ASN1_STRING.
  3558. * V_ASN1_SEQUENCE : WOLFSSL_ASN1_STRING.
  3559. *
  3560. * @param [in, out] a ASN.1 TYPE object to set.
  3561. * @param [in] type ASN.1 type of value.
  3562. * @param [in] value Value to store.
  3563. */
  3564. void wolfSSL_ASN1_TYPE_set(WOLFSSL_ASN1_TYPE *a, int type, void *value)
  3565. {
  3566. if (a != NULL) {
  3567. switch (type) {
  3568. case V_ASN1_NULL:
  3569. if (value != NULL) {
  3570. WOLFSSL_MSG("NULL tag meant to be always empty!");
  3571. /* No way to return error - value will not be used. */
  3572. }
  3573. FALL_THROUGH;
  3574. case V_ASN1_OBJECT:
  3575. case V_ASN1_UTCTIME:
  3576. case V_ASN1_GENERALIZEDTIME:
  3577. case V_ASN1_UTF8STRING:
  3578. case V_ASN1_PRINTABLESTRING:
  3579. case V_ASN1_T61STRING:
  3580. case V_ASN1_IA5STRING:
  3581. case V_ASN1_UNIVERSALSTRING:
  3582. case V_ASN1_SEQUENCE:
  3583. /* Dispose of any value currently set. */
  3584. wolfssl_asn1_type_free_value(a);
  3585. /* Assign anonymously typed input to anonymously typed field. */
  3586. a->value.ptr = (char *)value;
  3587. /* Set the type required. */
  3588. a->type = type;
  3589. break;
  3590. default:
  3591. WOLFSSL_MSG("Unknown or unsupported ASN1_TYPE");
  3592. /* No way to return error. */
  3593. }
  3594. }
  3595. }
  3596. #endif /* OPENSSL_ALL || OPENSSL_EXTRA || WOLFSSL_WPAS */
  3597. #endif /* !NO_ASN */
  3598. #endif /* !WOLFSSL_SSL_ASN1_INCLUDED */