ssl_asn1.c 114 KB

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