pkcs12.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512
  1. /* pkcs12.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* PKCS#12 allows storage of key and certificates into containers */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #if defined(HAVE_PKCS12) && \
  27. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_HMAC)
  28. #include <wolfssl/wolfcrypt/asn.h>
  29. #include <wolfssl/wolfcrypt/asn_public.h>
  30. #include <wolfssl/wolfcrypt/error-crypt.h>
  31. #include <wolfssl/wolfcrypt/hmac.h>
  32. #include <wolfssl/wolfcrypt/logging.h>
  33. #ifdef NO_INLINE
  34. #include <wolfssl/wolfcrypt/misc.h>
  35. #else
  36. #define WOLFSSL_MISC_INCLUDED
  37. #include <wolfcrypt/src/misc.c>
  38. #endif
  39. #include <wolfssl/wolfcrypt/pkcs12.h>
  40. #include <wolfssl/wolfcrypt/pwdbased.h>
  41. #include <wolfssl/wolfcrypt/hash.h>
  42. #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; }
  43. enum {
  44. WC_PKCS12_KeyBag = 667,
  45. WC_PKCS12_ShroudedKeyBag = 668,
  46. WC_PKCS12_CertBag = 669,
  47. WC_PKCS12_CertBag_Type1 = 675,
  48. WC_PKCS12_CrlBag = 670,
  49. WC_PKCS12_SecretBag = 671,
  50. WC_PKCS12_SafeContentsBag = 672,
  51. WC_PKCS12_DATA = 651,
  52. WC_PKCS12_ENCRYPTED_DATA = 656,
  53. WC_PKCS12_DATA_OBJ_SZ = 11,
  54. WC_PKCS12_MAC_SALT_SZ = 8,
  55. };
  56. static const byte WC_PKCS12_ENCRYPTED_OID[] =
  57. {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x06};
  58. static const byte WC_PKCS12_DATA_OID[] =
  59. {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01};
  60. static const byte WC_PKCS12_CertBag_Type1_OID[] =
  61. {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x16, 0x01};
  62. static const byte WC_PKCS12_CertBag_OID[] =
  63. {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x0c, 0x0a, 0x01, 0x03};
  64. static const byte WC_PKCS12_KeyBag_OID[] =
  65. {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x0c, 0x0a, 0x01, 0x01};
  66. static const byte WC_PKCS12_ShroudedKeyBag_OID[] =
  67. {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x0c, 0x0a, 0x01, 0x02};
  68. typedef struct ContentInfo {
  69. byte* data;
  70. struct ContentInfo* next;
  71. word32 encC; /* encryptedContent */
  72. word32 dataSz;
  73. int type; /* DATA / encrypted / enveloped */
  74. } ContentInfo;
  75. typedef struct AuthenticatedSafe {
  76. ContentInfo* CI;
  77. byte* data; /* T contents.... */
  78. word32 oid; /* encrypted or not */
  79. word32 numCI; /* number of Content Info structs */
  80. word32 dataSz;
  81. } AuthenticatedSafe;
  82. typedef struct MacData {
  83. byte* digest;
  84. byte* salt;
  85. word32 oid;
  86. word32 digestSz;
  87. word32 saltSz;
  88. int itt; /* number of iterations when creating HMAC key */
  89. } MacData;
  90. struct WC_PKCS12 {
  91. void* heap;
  92. AuthenticatedSafe* safe;
  93. MacData* signData;
  94. word32 oid; /* DATA / Enveloped DATA ... */
  95. };
  96. /* for friendlyName, localKeyId .... */
  97. typedef struct WC_PKCS12_ATTRIBUTE {
  98. byte* data;
  99. word32 oid;
  100. word32 dataSz;
  101. } WC_PKCS12_ATTRIBUTE;
  102. WC_PKCS12* wc_PKCS12_new(void)
  103. {
  104. WC_PKCS12* pkcs12 = (WC_PKCS12*)XMALLOC(sizeof(WC_PKCS12),
  105. NULL, DYNAMIC_TYPE_PKCS);
  106. if (pkcs12 == NULL) {
  107. WOLFSSL_MSG("Memory issue when creating WC_PKCS12 struct");
  108. return NULL;
  109. }
  110. XMEMSET(pkcs12, 0, sizeof(WC_PKCS12));
  111. return pkcs12;
  112. }
  113. static void freeSafe(AuthenticatedSafe* safe, void* heap)
  114. {
  115. int i;
  116. if (safe == NULL) {
  117. return;
  118. }
  119. /* free content info structs */
  120. for (i = safe->numCI; i > 0; i--) {
  121. ContentInfo* ci = safe->CI;
  122. safe->CI = ci->next;
  123. XFREE(ci, heap, DYNAMIC_TYPE_PKCS);
  124. }
  125. if (safe->data != NULL) {
  126. XFREE(safe->data, heap, DYNAMIC_TYPE_PKCS);
  127. }
  128. XFREE(safe, heap, DYNAMIC_TYPE_PKCS);
  129. (void)heap;
  130. }
  131. void wc_PKCS12_free(WC_PKCS12* pkcs12)
  132. {
  133. void* heap;
  134. /* if null pointer is passed in do nothing */
  135. if (pkcs12 == NULL) {
  136. WOLFSSL_MSG("Trying to free null WC_PKCS12 object");
  137. return;
  138. }
  139. heap = pkcs12->heap;
  140. if (pkcs12->safe != NULL) {
  141. freeSafe(pkcs12->safe, heap);
  142. }
  143. /* free mac data */
  144. if (pkcs12->signData != NULL) {
  145. if (pkcs12->signData->digest != NULL) {
  146. XFREE(pkcs12->signData->digest, heap, DYNAMIC_TYPE_DIGEST);
  147. pkcs12->signData->digest = NULL;
  148. }
  149. if (pkcs12->signData->salt != NULL) {
  150. XFREE(pkcs12->signData->salt, heap, DYNAMIC_TYPE_SALT);
  151. pkcs12->signData->salt = NULL;
  152. }
  153. XFREE(pkcs12->signData, heap, DYNAMIC_TYPE_PKCS);
  154. pkcs12->signData = NULL;
  155. }
  156. XFREE(pkcs12, NULL, DYNAMIC_TYPE_PKCS);
  157. }
  158. /* return 0 on success */
  159. static int GetSafeContent(WC_PKCS12* pkcs12, const byte* input,
  160. word32* idx, int maxIdx)
  161. {
  162. AuthenticatedSafe* safe;
  163. word32 oid;
  164. word32 localIdx = *idx;
  165. int ret;
  166. int size = 0;
  167. byte tag;
  168. safe = (AuthenticatedSafe*)XMALLOC(sizeof(AuthenticatedSafe), pkcs12->heap,
  169. DYNAMIC_TYPE_PKCS);
  170. if (safe == NULL) {
  171. return MEMORY_E;
  172. }
  173. XMEMSET(safe, 0, sizeof(AuthenticatedSafe));
  174. ret = GetObjectId(input, &localIdx, &oid, oidIgnoreType, maxIdx);
  175. if (ret < 0) {
  176. WOLFSSL_LEAVE("Get object id failed", ret);
  177. freeSafe(safe, pkcs12->heap);
  178. return ASN_PARSE_E;
  179. }
  180. safe->oid = oid;
  181. /* check tag, length */
  182. if (GetASNTag(input, &localIdx, &tag, maxIdx) < 0) {
  183. freeSafe(safe, pkcs12->heap);
  184. return ASN_PARSE_E;
  185. }
  186. if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) {
  187. WOLFSSL_MSG("Unexpected tag in PKCS12 DER");
  188. freeSafe(safe, pkcs12->heap);
  189. return ASN_PARSE_E;
  190. }
  191. if (GetLength(input, &localIdx, &size, maxIdx) <= 0) {
  192. freeSafe(safe, pkcs12->heap);
  193. return ASN_PARSE_E;
  194. }
  195. switch (oid) {
  196. case WC_PKCS12_ENCRYPTED_DATA:
  197. WOLFSSL_MSG("Found PKCS12 OBJECT: ENCRYPTED DATA");
  198. break;
  199. case WC_PKCS12_DATA:
  200. WOLFSSL_MSG("Found PKCS12 OBJECT: DATA");
  201. /* get octets holding contents */
  202. if (GetASNTag(input, &localIdx, &tag, maxIdx) < 0) {
  203. freeSafe(safe, pkcs12->heap);
  204. return ASN_PARSE_E;
  205. }
  206. if (tag != ASN_OCTET_STRING) {
  207. WOLFSSL_MSG("Wrong tag with content PKCS12 type DATA");
  208. freeSafe(safe, pkcs12->heap);
  209. return ASN_PARSE_E;
  210. }
  211. if (GetLength(input, &localIdx, &size, maxIdx) <= 0) {
  212. freeSafe(safe, pkcs12->heap);
  213. return ASN_PARSE_E;
  214. }
  215. break;
  216. }
  217. safe->dataSz = size;
  218. safe->data = (byte*)XMALLOC(size, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  219. if (safe->data == NULL) {
  220. freeSafe(safe, pkcs12->heap);
  221. return MEMORY_E;
  222. }
  223. XMEMCPY(safe->data, input + localIdx, size);
  224. *idx = localIdx;
  225. /* an instance of AuthenticatedSafe is created from
  226. * ContentInfo's strung together in a SEQUENCE. Here we iterate
  227. * through the ContentInfo's and add them to our
  228. * AuthenticatedSafe struct */
  229. localIdx = 0;
  230. input = safe->data;
  231. {
  232. int CISz;
  233. ret = GetSequence(input, &localIdx, &CISz, safe->dataSz);
  234. if (ret < 0) {
  235. freeSafe(safe, pkcs12->heap);
  236. return ASN_PARSE_E;
  237. }
  238. CISz += localIdx;
  239. while ((int)localIdx < CISz) {
  240. int curSz = 0;
  241. word32 curIdx;
  242. ContentInfo* ci = NULL;
  243. #ifdef WOLFSSL_DEBUG_PKCS12
  244. printf("\t\tlooking for Content Info.... ");
  245. #endif
  246. if ((ret = GetSequence(input, &localIdx, &curSz, safe->dataSz))
  247. < 0) {
  248. freeSafe(safe, pkcs12->heap);
  249. return ret;
  250. }
  251. if (curSz > CISz) {
  252. /* subset should not be larger than universe */
  253. freeSafe(safe, pkcs12->heap);
  254. return ASN_PARSE_E;
  255. }
  256. curIdx = localIdx;
  257. if ((ret = GetObjectId(input, &localIdx, &oid, oidIgnoreType,
  258. safe->dataSz)) < 0) {
  259. WOLFSSL_LEAVE("Get object id failed", ret);
  260. freeSafe(safe, pkcs12->heap);
  261. return ret;
  262. }
  263. /* create new content info struct ... possible OID sanity check? */
  264. ci = (ContentInfo*)XMALLOC(sizeof(ContentInfo), pkcs12->heap,
  265. DYNAMIC_TYPE_PKCS);
  266. if (ci == NULL) {
  267. freeSafe(safe, pkcs12->heap);
  268. return MEMORY_E;
  269. }
  270. ci->type = oid;
  271. ci->dataSz = curSz - (localIdx-curIdx);
  272. ci->data = (byte*)input + localIdx;
  273. localIdx += ci->dataSz;
  274. #ifdef WOLFSSL_DEBUG_PKCS12
  275. switch (oid) {
  276. case WC_PKCS12_ENCRYPTED_DATA:
  277. printf("CONTENT INFO: ENCRYPTED DATA, size = %d\n", ci->dataSz);
  278. break;
  279. case WC_PKCS12_DATA:
  280. printf("CONTENT INFO: DATA, size = %d\n", ci->dataSz);
  281. break;
  282. default:
  283. printf("CONTENT INFO: UNKNOWN, size = %d\n", ci->dataSz);
  284. }
  285. #endif
  286. /* insert to head of list */
  287. ci->next = safe->CI;
  288. safe->CI = ci;
  289. safe->numCI += 1;
  290. }
  291. }
  292. pkcs12->safe = safe;
  293. *idx += localIdx;
  294. return ret;
  295. }
  296. /* parse optional mac data
  297. * return 0 on success */
  298. static int GetSignData(WC_PKCS12* pkcs12, const byte* mem, word32* idx,
  299. word32 totalSz)
  300. {
  301. MacData* mac;
  302. word32 curIdx = *idx;
  303. word32 oid = 0;
  304. int size, ret;
  305. byte tag;
  306. /* Digest Info : Sequence
  307. * DigestAlgorithmIdentifier
  308. * Digest
  309. */
  310. if (GetSequence(mem, &curIdx, &size, totalSz) <= 0) {
  311. WOLFSSL_MSG("Failed to get PKCS12 sequence");
  312. return ASN_PARSE_E;
  313. }
  314. #ifdef WOLFSSL_DEBUG_PKCS12
  315. printf("\t\tSEQUENCE: DigestInfo size = %d\n", size);
  316. #endif
  317. mac = (MacData*)XMALLOC(sizeof(MacData), pkcs12->heap, DYNAMIC_TYPE_PKCS);
  318. if (mac == NULL) {
  319. return MEMORY_E;
  320. }
  321. XMEMSET(mac, 0, sizeof(MacData));
  322. /* DigestAlgorithmIdentifier */
  323. if ((ret = GetAlgoId(mem, &curIdx, &oid, oidIgnoreType, totalSz)) < 0) {
  324. WOLFSSL_MSG("Failed to get PKCS12 sequence");
  325. XFREE(mac, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  326. return ret;
  327. }
  328. mac->oid = oid;
  329. #ifdef WOLFSSL_DEBUG_PKCS12
  330. printf("\t\tALGO ID = %d\n", oid);
  331. #endif
  332. /* Digest: should be octet type holding digest */
  333. if (GetASNTag(mem, &curIdx, &tag, totalSz) < 0) {
  334. XFREE(mac, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  335. return ASN_PARSE_E;
  336. }
  337. if (tag != ASN_OCTET_STRING) {
  338. WOLFSSL_MSG("Failed to get digest");
  339. XFREE(mac, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  340. return ASN_PARSE_E;
  341. }
  342. if (GetLength(mem, &curIdx, &size, totalSz) <= 0) {
  343. XFREE(mac, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  344. return ASN_PARSE_E;
  345. }
  346. mac->digestSz = size;
  347. mac->digest = (byte*)XMALLOC(mac->digestSz, pkcs12->heap,
  348. DYNAMIC_TYPE_DIGEST);
  349. if (mac->digest == NULL || mac->digestSz + curIdx > totalSz) {
  350. ERROR_OUT(MEMORY_E, exit_gsd);
  351. }
  352. XMEMCPY(mac->digest, mem + curIdx, mac->digestSz);
  353. #ifdef WOLFSSL_DEBUG_PKCS12
  354. {
  355. byte* p;
  356. for (printf("\t\tDigest = "), p = (byte*)mem+curIdx;
  357. p < (byte*)mem + curIdx + mac->digestSz;
  358. printf("%02X", *p), p++);
  359. printf(" : size = %d\n", mac->digestSz);
  360. }
  361. #endif
  362. curIdx += mac->digestSz;
  363. /* get salt, should be octet string */
  364. if (GetASNTag(mem, &curIdx, &tag, totalSz) < 0) {
  365. ERROR_OUT(ASN_PARSE_E, exit_gsd);
  366. }
  367. if (tag != ASN_OCTET_STRING) {
  368. WOLFSSL_MSG("Failed to get salt");
  369. ERROR_OUT(ASN_PARSE_E, exit_gsd);
  370. }
  371. if ((ret = GetLength(mem, &curIdx, &size, totalSz)) < 0) {
  372. goto exit_gsd;
  373. }
  374. mac->saltSz = size;
  375. mac->salt = (byte*)XMALLOC(mac->saltSz, pkcs12->heap, DYNAMIC_TYPE_SALT);
  376. if (mac->salt == NULL || mac->saltSz + curIdx > totalSz) {
  377. ERROR_OUT(MEMORY_E, exit_gsd);
  378. }
  379. XMEMCPY(mac->salt, mem + curIdx, mac->saltSz);
  380. #ifdef WOLFSSL_DEBUG_PKCS12
  381. {
  382. byte* p;
  383. for (printf("\t\tSalt = "), p = (byte*)mem + curIdx;
  384. p < (byte*)mem + curIdx + mac->saltSz;
  385. printf("%02X", *p), p++);
  386. printf(" : size = %d\n", mac->saltSz);
  387. }
  388. #endif
  389. curIdx += mac->saltSz;
  390. /* check for MAC iterations, default to 1 */
  391. mac->itt = WC_PKCS12_MAC_DEFAULT;
  392. if (curIdx < totalSz) {
  393. int number = 0;
  394. if (GetShortInt(mem, &curIdx, &number, totalSz) >= 0) {
  395. /* found a iteration value */
  396. mac->itt = number;
  397. }
  398. }
  399. #ifdef WOLFSSL_DEBUG_PKCS12
  400. printf("\t\tITERATIONS : %d\n", mac->itt);
  401. #endif
  402. *idx = curIdx;
  403. pkcs12->signData = mac;
  404. ret = 0; /* success */
  405. exit_gsd:
  406. /* failure cleanup */
  407. if (ret != 0) {
  408. if (mac) {
  409. if (mac->digest)
  410. XFREE(mac->digest, pkcs12->heap, DYNAMIC_TYPE_DIGEST);
  411. XFREE(mac, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  412. }
  413. }
  414. return ret;
  415. }
  416. /* expects PKCS12 signData to be set up with OID
  417. *
  418. * returns the size of mac created on success. A negative value will be returned
  419. * in the case that an error happened.
  420. */
  421. static int wc_PKCS12_create_mac(WC_PKCS12* pkcs12, byte* data, word32 dataSz,
  422. const byte* psw, word32 pswSz, byte* out, word32 outSz)
  423. {
  424. Hmac hmac;
  425. MacData* mac;
  426. int ret, kLen;
  427. enum wc_HashType hashT;
  428. int idx = 0;
  429. int id = 3; /* value from RFC 7292 indicating key is used for MAC */
  430. word32 i;
  431. byte unicodePasswd[MAX_UNICODE_SZ];
  432. byte key[PKCS_MAX_KEY_SIZE];
  433. if (pkcs12 == NULL || pkcs12->signData == NULL || data == NULL ||
  434. out == NULL) {
  435. return BAD_FUNC_ARG;
  436. }
  437. mac = pkcs12->signData;
  438. /* unicode set up from asn.c */
  439. if ((pswSz * 2 + 2) > (int)sizeof(unicodePasswd)) {
  440. WOLFSSL_MSG("PKCS12 max unicode size too small");
  441. return UNICODE_SIZE_E;
  442. }
  443. for (i = 0; i < pswSz; i++) {
  444. unicodePasswd[idx++] = 0x00;
  445. unicodePasswd[idx++] = (byte)psw[i];
  446. }
  447. /* add trailing NULL */
  448. unicodePasswd[idx++] = 0x00;
  449. unicodePasswd[idx++] = 0x00;
  450. /* get hash type used and resulting size of HMAC key */
  451. hashT = wc_OidGetHash(mac->oid);
  452. if (hashT == WC_HASH_TYPE_NONE) {
  453. ForceZero(unicodePasswd, MAX_UNICODE_SZ);
  454. WOLFSSL_MSG("Unsupported hash used");
  455. return BAD_FUNC_ARG;
  456. }
  457. kLen = wc_HashGetDigestSize(hashT);
  458. /* check out buffer is large enough */
  459. if (kLen < 0 || outSz < (word32)kLen) {
  460. ForceZero(unicodePasswd, MAX_UNICODE_SZ);
  461. return BAD_FUNC_ARG;
  462. }
  463. /* idx contains size of unicodePasswd */
  464. ret = wc_PKCS12_PBKDF_ex(key, unicodePasswd, idx, mac->salt, mac->saltSz,
  465. mac->itt, kLen, (int)hashT, id, pkcs12->heap);
  466. ForceZero(unicodePasswd, MAX_UNICODE_SZ);
  467. if (ret < 0) {
  468. return ret;
  469. }
  470. /* now that key has been created use it to get HMAC hash on data */
  471. if ((ret = wc_HmacInit(&hmac, pkcs12->heap, INVALID_DEVID)) != 0) {
  472. return ret;
  473. }
  474. ret = wc_HmacSetKey(&hmac, (int)hashT, key, kLen);
  475. if (ret == 0)
  476. ret = wc_HmacUpdate(&hmac, data, dataSz);
  477. if (ret == 0)
  478. ret = wc_HmacFinal(&hmac, out);
  479. wc_HmacFree(&hmac);
  480. if (ret != 0)
  481. return ret;
  482. return kLen; /* same as digest size */
  483. }
  484. /* check mac on pkcs12, pkcs12->mac has been sanity checked before entering *
  485. * returns the result of comparison, success is 0 */
  486. static int wc_PKCS12_verify(WC_PKCS12* pkcs12, byte* data, word32 dataSz,
  487. const byte* psw, word32 pswSz)
  488. {
  489. MacData* mac;
  490. int ret;
  491. byte digest[WC_MAX_DIGEST_SIZE];
  492. if (pkcs12 == NULL || pkcs12->signData == NULL || data == NULL) {
  493. return BAD_FUNC_ARG;
  494. }
  495. mac = pkcs12->signData;
  496. #ifdef WOLFSSL_DEBUG_PKCS12
  497. printf("Verifying MAC with OID = %d\n", mac->oid);
  498. #endif
  499. /* check if this builds digest size is too small */
  500. if (mac->digestSz > WC_MAX_DIGEST_SIZE) {
  501. WOLFSSL_MSG("PKCS12 max digest size too small");
  502. return BAD_FUNC_ARG;
  503. }
  504. if ((ret = wc_PKCS12_create_mac(pkcs12, data, dataSz, psw, pswSz,
  505. digest, WC_MAX_DIGEST_SIZE)) < 0) {
  506. return ret;
  507. }
  508. #ifdef WOLFSSL_DEBUG_PKCS12
  509. {
  510. byte* p;
  511. for (printf("\t\tHash = "), p = (byte*)digest;
  512. p < (byte*)digest + mac->digestSz;
  513. printf("%02X", *p), p++);
  514. printf(" : size = %d\n", mac->digestSz);
  515. }
  516. #endif
  517. return XMEMCMP(digest, mac->digest, mac->digestSz);
  518. }
  519. int wc_PKCS12_verify_ex(WC_PKCS12* pkcs12, const byte* psw, word32 pswSz)
  520. {
  521. if (pkcs12 == NULL || pkcs12->safe == NULL) {
  522. return BAD_FUNC_ARG;
  523. }
  524. return wc_PKCS12_verify(pkcs12, pkcs12->safe->data, pkcs12->safe->dataSz,
  525. psw, pswSz);
  526. }
  527. /* Convert DER format stored in der buffer to WC_PKCS12 struct
  528. * Puts the raw contents of Content Info into structure without completely
  529. * parsing or decoding.
  530. * der : pointer to der buffer holding PKCS12
  531. * derSz : size of der buffer
  532. * pkcs12 : non-null pkcs12 pointer
  533. * return 0 on success and negative on failure.
  534. */
  535. int wc_d2i_PKCS12(const byte* der, word32 derSz, WC_PKCS12* pkcs12)
  536. {
  537. word32 idx = 0;
  538. word32 totalSz = 0;
  539. int ret;
  540. int size = 0;
  541. int version = 0;
  542. WOLFSSL_ENTER("wolfSSL_d2i_PKCS12");
  543. if (der == NULL || pkcs12 == NULL) {
  544. return BAD_FUNC_ARG;
  545. }
  546. totalSz = derSz;
  547. if (GetSequence(der, &idx, &size, totalSz) <= 0) {
  548. WOLFSSL_MSG("Failed to get PKCS12 sequence");
  549. return ASN_PARSE_E;
  550. }
  551. /* get version */
  552. if ((ret = GetMyVersion(der, &idx, &version, totalSz)) < 0) {
  553. return ret;
  554. }
  555. #ifdef WOLFSSL_DEBUG_PKCS12
  556. printf("\nBEGIN: PKCS12 size = %d\n", totalSz);
  557. printf("version = %d\n", version);
  558. #endif
  559. if (version != WC_PKCS12_VERSION_DEFAULT) {
  560. WOLFSSL_MSG("PKCS12 unsupported version!");
  561. WOLFSSL_ERROR_VERBOSE(ASN_VERSION_E);
  562. return ASN_VERSION_E;
  563. }
  564. if ((ret = GetSequence(der, &idx, &size, totalSz)) < 0) {
  565. return ret;
  566. }
  567. #ifdef WOLFSSL_DEBUG_PKCS12
  568. printf("\tSEQUENCE: AuthenticatedSafe size = %d\n", size);
  569. #endif
  570. if ((ret = GetSafeContent(pkcs12, der, &idx, size + idx)) < 0) {
  571. WOLFSSL_MSG("GetSafeContent error");
  572. return ret;
  573. }
  574. /* if more buffer left check for MAC data */
  575. if (idx < totalSz) {
  576. if ((ret = GetSequence(der, &idx, &size, totalSz)) < 0) {
  577. WOLFSSL_MSG("Ignoring unknown data at end of PKCS12 DER buffer");
  578. }
  579. else {
  580. #ifdef WOLFSSL_DEBUG_PKCS12
  581. printf("\tSEQUENCE: Signature size = %d\n", size);
  582. #endif
  583. if ((ret = GetSignData(pkcs12, der, &idx, totalSz)) < 0) {
  584. return ASN_PARSE_E;
  585. }
  586. }
  587. }
  588. #ifdef WOLFSSL_DEBUG_PKCS12
  589. printf("END: PKCS12\n");
  590. #endif
  591. return ret;
  592. }
  593. #ifndef NO_FILESYSTEM
  594. /* Parse the DER-encoded PKCS #12 object in the provided file. Populate the
  595. * WC_PKCS12 object pointed to by the passed in pointer, allocating the object
  596. * if necessary.
  597. *
  598. * file : path to PKCS #12 file.
  599. * pkcs12: pointer to a pointer to a WC_PKCS12 object to populate. If *pkcs12 is
  600. * NULL, this function will allocate a new WC_PKCS12.
  601. * return 0 on success and negative on failure.
  602. */
  603. int wc_d2i_PKCS12_fp(const char* file, WC_PKCS12** pkcs12)
  604. {
  605. int ret = 0;
  606. byte* buf = NULL;
  607. size_t bufSz = 0;
  608. WC_PKCS12* tmpPkcs12 = NULL;
  609. int callerAlloc = 1;
  610. WOLFSSL_ENTER("wc_d2i_PKCS12_fp");
  611. if (pkcs12 == NULL) {
  612. WOLFSSL_MSG("pkcs12 parameter NULL.");
  613. ret = BAD_FUNC_ARG;
  614. }
  615. if (ret == 0)
  616. ret = wc_FileLoad(file, &buf, &bufSz, NULL);
  617. if (ret == 0) {
  618. if (*pkcs12 == NULL) {
  619. tmpPkcs12 = wc_PKCS12_new();
  620. if (tmpPkcs12 == NULL) {
  621. WOLFSSL_MSG("Failed to allocate PKCS12 object.");
  622. ret = MEMORY_E;
  623. }
  624. else {
  625. *pkcs12 = tmpPkcs12;
  626. callerAlloc = 0;
  627. }
  628. }
  629. }
  630. if (ret == 0) {
  631. ret = wc_d2i_PKCS12(buf, (word32)bufSz, *pkcs12);
  632. if (ret != 0) {
  633. WOLFSSL_MSG("wc_d2i_PKCS12 failed.");
  634. }
  635. }
  636. if (ret != 0 && callerAlloc == 0 && *pkcs12 != NULL) {
  637. wc_PKCS12_free(*pkcs12);
  638. *pkcs12 = NULL;
  639. }
  640. if (buf != NULL) {
  641. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  642. }
  643. WOLFSSL_LEAVE("wc_d2i_PKCS12_fp", ret);
  644. return ret;
  645. }
  646. #endif /* NO_FILESYSTEM */
  647. /* Convert WC_PKCS12 struct to allocated DER buffer.
  648. * pkcs12 : non-null pkcs12 pointer
  649. * der : pointer-pointer to der buffer. If NULL space will be
  650. * allocated for der, which must be freed by application.
  651. * derSz : size of buffer passed in when der is not NULL. NULL arg disables
  652. * sanity checks on buffer read/writes. Max size gets set to derSz when
  653. * the "der" buffer passed in is NULL and LENGTH_ONLY_E is returned.
  654. * return size of DER on success and negative on failure.
  655. */
  656. int wc_i2d_PKCS12(WC_PKCS12* pkcs12, byte** der, int* derSz)
  657. {
  658. int ret = 0;
  659. word32 seqSz = 0, verSz = 0, totalSz = 0, idx = 0, sdBufSz = 0;
  660. byte *buf = NULL;
  661. byte ver[MAX_VERSION_SZ];
  662. byte seq[MAX_SEQ_SZ];
  663. byte *sdBuf = NULL;
  664. if ((pkcs12 == NULL) || (pkcs12->safe == NULL) ||
  665. (der == NULL && derSz == NULL)) {
  666. return BAD_FUNC_ARG;
  667. }
  668. /* Create the MAC portion */
  669. if (pkcs12->signData != NULL) {
  670. MacData *mac = (MacData*)pkcs12->signData;
  671. word32 innerSz = 0;
  672. word32 outerSz = 0;
  673. /* get exact size */
  674. {
  675. byte ASNLENGTH[MAX_LENGTH_SZ];
  676. byte ASNSHORT[MAX_SHORT_SZ];
  677. byte ASNALGO[MAX_ALGO_SZ];
  678. word32 tmpIdx = 0;
  679. /* algo id */
  680. innerSz += SetAlgoID(mac->oid, ASNALGO, oidHashType, 0);
  681. /* Octet string holding digest */
  682. innerSz += ASN_TAG_SZ;
  683. innerSz += SetLength(mac->digestSz, ASNLENGTH);
  684. innerSz += mac->digestSz;
  685. /* salt */
  686. outerSz += ASN_TAG_SZ;
  687. outerSz += SetLength(mac->saltSz, ASNLENGTH);
  688. outerSz += mac->saltSz;
  689. /* MAC iterations */
  690. ret = SetShortInt(ASNSHORT, &tmpIdx, mac->itt, MAX_SHORT_SZ);
  691. if (ret >= 0) {
  692. outerSz += ret;
  693. ret = 0;
  694. }
  695. else {
  696. return ret;
  697. }
  698. /* sequence of inner data */
  699. outerSz += SetSequence(innerSz, seq);
  700. outerSz += innerSz;
  701. }
  702. sdBufSz = outerSz + SetSequence(outerSz, seq);
  703. sdBuf = (byte*)XMALLOC(sdBufSz, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  704. if (sdBuf == NULL) {
  705. ret = MEMORY_E;
  706. }
  707. if (ret == 0) {
  708. idx += SetSequence(outerSz, sdBuf);
  709. idx += SetSequence(innerSz, &sdBuf[idx]);
  710. /* Set Algorithm Identifier */
  711. {
  712. word32 algoIdSz;
  713. algoIdSz = SetAlgoID(mac->oid, &sdBuf[idx], oidHashType, 0);
  714. if (algoIdSz == 0) {
  715. ret = ALGO_ID_E;
  716. }
  717. else {
  718. idx += algoIdSz;
  719. }
  720. }
  721. }
  722. if (ret == 0) {
  723. /* Octet string holding digest */
  724. idx += SetOctetString(mac->digestSz, &sdBuf[idx]);
  725. XMEMCPY(&sdBuf[idx], mac->digest, mac->digestSz);
  726. idx += mac->digestSz;
  727. /* Set salt */
  728. idx += SetOctetString(mac->saltSz, &sdBuf[idx]);
  729. XMEMCPY(&sdBuf[idx], mac->salt, mac->saltSz);
  730. idx += mac->saltSz;
  731. /* MAC iterations */
  732. {
  733. int tmpSz;
  734. word32 tmpIdx = 0;
  735. byte ar[MAX_SHORT_SZ];
  736. tmpSz = SetShortInt(ar, &tmpIdx, mac->itt, MAX_SHORT_SZ);
  737. if (tmpSz < 0) {
  738. ret = tmpSz;
  739. }
  740. else {
  741. XMEMCPY(&sdBuf[idx], ar, tmpSz);
  742. }
  743. }
  744. totalSz += sdBufSz;
  745. }
  746. }
  747. /* Calculate size of der */
  748. if (ret == 0) {
  749. totalSz += pkcs12->safe->dataSz;
  750. totalSz += 4; /* Octet string */
  751. totalSz += 4; /* Element */
  752. totalSz += 2 + sizeof(WC_PKCS12_DATA_OID);
  753. totalSz += 4; /* Seq */
  754. ret = SetMyVersion(WC_PKCS12_VERSION_DEFAULT, ver, FALSE);
  755. if (ret > 0) {
  756. verSz = (word32)ret;
  757. ret = 0; /* value larger than 0 is success */
  758. totalSz += verSz;
  759. seqSz = SetSequence(totalSz, seq);
  760. totalSz += seqSz;
  761. /* check if getting length only */
  762. if (der == NULL && derSz != NULL) {
  763. *derSz = totalSz;
  764. XFREE(sdBuf, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  765. return LENGTH_ONLY_E;
  766. }
  767. if (*der == NULL) {
  768. /* Allocate if requested */
  769. buf = (byte*)XMALLOC(totalSz, NULL, DYNAMIC_TYPE_PKCS);
  770. }
  771. else {
  772. buf = *der;
  773. /* sanity check on buffer size if passed in */
  774. if (derSz != NULL) {
  775. if (*derSz < (int)totalSz) {
  776. WOLFSSL_MSG("Buffer passed in is too small");
  777. ret = BUFFER_E;
  778. }
  779. }
  780. }
  781. }
  782. }
  783. if (buf == NULL) {
  784. ret = MEMORY_E;
  785. }
  786. if (ret == 0) {
  787. idx = 0;
  788. /* Copy parts to buf */
  789. XMEMCPY(&buf[idx], seq, seqSz);
  790. idx += seqSz;
  791. XMEMCPY(&buf[idx], ver, verSz);
  792. idx += verSz;
  793. seqSz = SetSequence(totalSz - sdBufSz - idx - 4, seq);
  794. XMEMCPY(&buf[idx], seq, seqSz);
  795. idx += seqSz;
  796. /* OID */
  797. idx += SetObjectId(sizeof(WC_PKCS12_DATA_OID), &buf[idx]);
  798. XMEMCPY(&buf[idx], WC_PKCS12_DATA_OID, sizeof(WC_PKCS12_DATA_OID));
  799. idx += sizeof(WC_PKCS12_DATA_OID);
  800. /* Element */
  801. buf[idx++] = ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC;
  802. idx += SetLength(totalSz - sdBufSz - idx - 3, &buf[idx]);
  803. /* Octet string */
  804. idx += SetOctetString(totalSz - sdBufSz - idx - 4, &buf[idx]);
  805. XMEMCPY(&buf[idx], pkcs12->safe->data, pkcs12->safe->dataSz);
  806. idx += pkcs12->safe->dataSz;
  807. if (pkcs12->signData != NULL) {
  808. XMEMCPY(&buf[idx], sdBuf, sdBufSz);
  809. }
  810. if (*der == NULL) {
  811. /* Point to start of data allocated for DER */
  812. *der = buf;
  813. }
  814. else {
  815. /* Increment pointer to byte past DER */
  816. *der = &buf[totalSz];
  817. }
  818. /* Return size of der */
  819. ret = totalSz;
  820. }
  821. XFREE(sdBuf, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  822. /* Allocation of buf was the last time ret could be a failure,
  823. * so no need to free here */
  824. return ret;
  825. }
  826. /* helper function to free WC_DerCertList */
  827. void wc_FreeCertList(WC_DerCertList* list, void* heap)
  828. {
  829. WC_DerCertList* current = list;
  830. WC_DerCertList* next;
  831. if (list == NULL) {
  832. return;
  833. }
  834. while (current != NULL) {
  835. next = current->next;
  836. if (current->buffer != NULL) {
  837. XFREE(current->buffer, heap, DYNAMIC_TYPE_PKCS);
  838. }
  839. XFREE(current, heap, DYNAMIC_TYPE_PKCS);
  840. current = next;
  841. }
  842. (void)heap;
  843. }
  844. static WARN_UNUSED_RESULT int freeDecCertList(WC_DerCertList** list,
  845. byte** pkey, word32* pkeySz, byte** cert, word32* certSz, void* heap)
  846. {
  847. WC_DerCertList* current = *list;
  848. WC_DerCertList* previous = NULL;
  849. #ifdef WOLFSSL_SMALL_STACK
  850. DecodedCert *DeCert = (DecodedCert *)XMALLOC(
  851. sizeof(*DeCert), heap, DYNAMIC_TYPE_PKCS);
  852. if (DeCert == NULL)
  853. return MEMORY_E;
  854. #else
  855. DecodedCert DeCert[1];
  856. #endif
  857. while (current != NULL) {
  858. InitDecodedCert(DeCert, current->buffer, current->bufferSz, heap);
  859. if (ParseCertRelative(DeCert, CERT_TYPE, NO_VERIFY, NULL) == 0) {
  860. if (wc_CheckPrivateKeyCert(*pkey, *pkeySz, DeCert) == 1) {
  861. WOLFSSL_MSG("Key Pair found");
  862. *cert = current->buffer;
  863. *certSz = current->bufferSz;
  864. if (previous == NULL) {
  865. *list = current->next;
  866. }
  867. else {
  868. previous->next = current->next;
  869. }
  870. FreeDecodedCert(DeCert);
  871. XFREE(current, heap, DYNAMIC_TYPE_PKCS);
  872. break;
  873. }
  874. }
  875. FreeDecodedCert(DeCert);
  876. previous = current;
  877. current = current->next;
  878. }
  879. #ifdef WOLFSSL_SMALL_STACK
  880. XFREE(DeCert, heap, DYNAMIC_TYPE_PKCS);
  881. #endif
  882. return 0;
  883. }
  884. /* return 0 on success and negative on failure.
  885. * By side effect returns private key, cert, and optionally ca.
  886. * Parses and decodes the parts of PKCS12
  887. *
  888. * NOTE: can parse with USER RSA enabled but may return cert that is not the
  889. * pair for the key when using RSA key pairs.
  890. *
  891. * pkcs12 : non-null WC_PKCS12 struct
  892. * psw : password to use for PKCS12 decode
  893. * pkey : Private key returned
  894. * cert : x509 cert returned
  895. * ca : optional ca returned
  896. */
  897. int wc_PKCS12_parse(WC_PKCS12* pkcs12, const char* psw,
  898. byte** pkey, word32* pkeySz, byte** cert, word32* certSz,
  899. WC_DerCertList** ca)
  900. {
  901. ContentInfo* ci = NULL;
  902. WC_DerCertList* certList = NULL;
  903. WC_DerCertList* tailList = NULL;
  904. byte* buf = NULL;
  905. word32 i, oid;
  906. int ret, pswSz;
  907. word32 algId;
  908. WOLFSSL_ENTER("wc_PKCS12_parse");
  909. if (pkcs12 == NULL || psw == NULL || cert == NULL || certSz == NULL ||
  910. pkey == NULL || pkeySz == NULL) {
  911. return BAD_FUNC_ARG;
  912. }
  913. pswSz = (int)XSTRLEN(psw);
  914. *cert = NULL;
  915. *pkey = NULL;
  916. if (ca != NULL)
  917. *ca = NULL;
  918. /* if there is sign data then verify the MAC */
  919. if (pkcs12->signData != NULL ) {
  920. if ((ret = wc_PKCS12_verify(pkcs12, pkcs12->safe->data,
  921. pkcs12->safe->dataSz, (byte*)psw, pswSz)) != 0) {
  922. WOLFSSL_MSG("PKCS12 Bad MAC on verify");
  923. WOLFSSL_LEAVE("wc_PKCS12_parse verify ", ret);
  924. (void)ret;
  925. return MAC_CMP_FAILED_E;
  926. }
  927. }
  928. if (pkcs12->safe == NULL) {
  929. WOLFSSL_MSG("No PKCS12 safes to parse");
  930. return BAD_FUNC_ARG;
  931. }
  932. /* Decode content infos */
  933. ci = pkcs12->safe->CI;
  934. for (i = 0; i < pkcs12->safe->numCI; i++) {
  935. byte* data;
  936. word32 idx = 0;
  937. int size, totalSz;
  938. byte tag;
  939. if (ci->type == WC_PKCS12_ENCRYPTED_DATA) {
  940. int number;
  941. WOLFSSL_MSG("Decrypting PKCS12 Content Info Container");
  942. data = ci->data;
  943. if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
  944. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  945. }
  946. if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) {
  947. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  948. }
  949. if ((ret = GetLength(data, &idx, &size, ci->dataSz)) < 0) {
  950. goto exit_pk12par;
  951. }
  952. if ((ret = GetSequence(data, &idx, &size, ci->dataSz)) < 0) {
  953. goto exit_pk12par;
  954. }
  955. if ((ret = GetShortInt(data, &idx, &number, ci->dataSz)) < 0) {
  956. goto exit_pk12par;
  957. }
  958. if (number != 0) {
  959. WOLFSSL_MSG("Expecting 0 for Integer with Encrypted PKCS12");
  960. }
  961. if ((ret = GetSequence(data, &idx, &size, ci->dataSz)) < 0) {
  962. goto exit_pk12par;
  963. }
  964. ret = GetObjectId(data, &idx, &oid, oidIgnoreType, ci->dataSz);
  965. if (ret < 0 || oid != WC_PKCS12_DATA) {
  966. WOLFSSL_MSG("Not PKCS12 DATA object or get object parse error");
  967. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  968. }
  969. /* decrypted content overwrites input buffer */
  970. size = ci->dataSz - idx;
  971. buf = (byte*)XMALLOC(size, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  972. if (buf == NULL) {
  973. ERROR_OUT(MEMORY_E, exit_pk12par);
  974. }
  975. XMEMCPY(buf, data + idx, size);
  976. if ((ret = DecryptContent(buf, size, psw, pswSz)) < 0) {
  977. WOLFSSL_MSG("Decryption failed, algorithm not compiled in?");
  978. goto exit_pk12par;
  979. }
  980. data = buf;
  981. idx = 0;
  982. #ifdef WOLFSSL_DEBUG_PKCS12
  983. {
  984. byte* p;
  985. for (printf("\tData = "), p = (byte*)buf;
  986. p < (byte*)buf + size;
  987. printf("%02X", *p), p++);
  988. printf("\n");
  989. }
  990. #endif
  991. }
  992. else { /* type DATA */
  993. WOLFSSL_MSG("Parsing PKCS12 DATA Content Info Container");
  994. data = ci->data;
  995. if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
  996. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  997. }
  998. if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) {
  999. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1000. }
  1001. if (GetLength(data, &idx, &size, ci->dataSz) <= 0) {
  1002. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1003. }
  1004. if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
  1005. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1006. }
  1007. if (tag != ASN_OCTET_STRING) {
  1008. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1009. }
  1010. if ((ret = GetLength(data, &idx, &size, ci->dataSz)) < 0) {
  1011. goto exit_pk12par;
  1012. }
  1013. }
  1014. /* parse through bags in ContentInfo */
  1015. if ((ret = GetSequence(data, &idx, &totalSz, ci->dataSz)) < 0) {
  1016. goto exit_pk12par;
  1017. }
  1018. totalSz += idx;
  1019. while ((int)idx < totalSz) {
  1020. int bagSz;
  1021. if ((ret = GetSequence(data, &idx, &bagSz, ci->dataSz)) < 0) {
  1022. goto exit_pk12par;
  1023. }
  1024. bagSz += idx;
  1025. if ((ret = GetObjectId(data, &idx, &oid, oidIgnoreType,
  1026. ci->dataSz)) < 0) {
  1027. goto exit_pk12par;
  1028. }
  1029. switch (oid) {
  1030. case WC_PKCS12_KeyBag: /* 667 */
  1031. WOLFSSL_MSG("PKCS12 Key Bag found");
  1032. if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
  1033. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1034. }
  1035. if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) {
  1036. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1037. }
  1038. if ((ret = GetLength(data, &idx, &size, ci->dataSz)) <= 0) {
  1039. if (ret == 0)
  1040. ret = ASN_PARSE_E;
  1041. goto exit_pk12par;
  1042. }
  1043. if (*pkey == NULL) {
  1044. *pkey = (byte*)XMALLOC(size, pkcs12->heap,
  1045. DYNAMIC_TYPE_PUBLIC_KEY);
  1046. if (*pkey == NULL) {
  1047. ERROR_OUT(MEMORY_E, exit_pk12par);
  1048. }
  1049. XMEMCPY(*pkey, data + idx, size);
  1050. *pkeySz = ToTraditional_ex(*pkey, size, &algId);
  1051. }
  1052. #ifdef WOLFSSL_DEBUG_PKCS12
  1053. {
  1054. byte* p;
  1055. for (printf("\tKey = "), p = (byte*)*pkey;
  1056. p < (byte*)*pkey + size;
  1057. printf("%02X", *p), p++);
  1058. printf("\n");
  1059. }
  1060. #endif
  1061. idx += size;
  1062. break;
  1063. case WC_PKCS12_ShroudedKeyBag: /* 668 */
  1064. {
  1065. byte* k;
  1066. WOLFSSL_MSG("PKCS12 Shrouded Key Bag found");
  1067. if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
  1068. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1069. }
  1070. if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) {
  1071. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1072. }
  1073. if ((ret = GetLength(data, &idx, &size,
  1074. ci->dataSz)) < 0) {
  1075. goto exit_pk12par;
  1076. }
  1077. k = (byte*)XMALLOC(size, pkcs12->heap,
  1078. DYNAMIC_TYPE_PUBLIC_KEY);
  1079. if (k == NULL) {
  1080. ERROR_OUT(MEMORY_E, exit_pk12par);
  1081. }
  1082. XMEMCPY(k, data + idx, size);
  1083. /* overwrites input, be warned */
  1084. if ((ret = ToTraditionalEnc(k, size, psw, pswSz,
  1085. &algId)) < 0) {
  1086. XFREE(k, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  1087. goto exit_pk12par;
  1088. }
  1089. if (ret < size) {
  1090. /* shrink key buffer */
  1091. byte* tmp = (byte*)XMALLOC(ret, pkcs12->heap,
  1092. DYNAMIC_TYPE_PUBLIC_KEY);
  1093. if (tmp == NULL) {
  1094. XFREE(k, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  1095. ERROR_OUT(MEMORY_E, exit_pk12par);
  1096. }
  1097. XMEMCPY(tmp, k, ret);
  1098. XFREE(k, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  1099. k = tmp;
  1100. }
  1101. size = ret;
  1102. if (*pkey == NULL) {
  1103. *pkey = k;
  1104. *pkeySz = size;
  1105. }
  1106. else { /* only expecting one key */
  1107. XFREE(k, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  1108. }
  1109. idx += size;
  1110. #ifdef WOLFSSL_DEBUG_PKCS12
  1111. {
  1112. byte* p;
  1113. for (printf("\tKey = "), p = (byte*)k;
  1114. p < (byte*)k + ret;
  1115. printf("%02X", *p), p++);
  1116. printf("\n");
  1117. }
  1118. #endif
  1119. }
  1120. break;
  1121. case WC_PKCS12_CertBag: /* 669 */
  1122. {
  1123. WC_DerCertList* node;
  1124. WOLFSSL_MSG("PKCS12 Cert Bag found");
  1125. if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
  1126. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1127. }
  1128. if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) {
  1129. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1130. }
  1131. if ((ret = GetLength(data, &idx, &size, ci->dataSz)) < 0) {
  1132. goto exit_pk12par;
  1133. }
  1134. /* get cert bag type */
  1135. if ((ret = GetSequence(data, &idx, &size, ci->dataSz)) <0) {
  1136. goto exit_pk12par;
  1137. }
  1138. if ((ret = GetObjectId(data, &idx, &oid, oidIgnoreType,
  1139. ci->dataSz)) < 0) {
  1140. goto exit_pk12par;
  1141. }
  1142. switch (oid) {
  1143. case WC_PKCS12_CertBag_Type1: /* 675 */
  1144. /* type 1 */
  1145. WOLFSSL_MSG("PKCS12 cert bag type 1");
  1146. if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
  1147. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1148. }
  1149. if (tag != (ASN_CONSTRUCTED |
  1150. ASN_CONTEXT_SPECIFIC)) {
  1151. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1152. }
  1153. if ((ret = GetLength(data, &idx, &size, ci->dataSz))
  1154. <= 0) {
  1155. if (ret == 0)
  1156. ret = ASN_PARSE_E;
  1157. goto exit_pk12par;
  1158. }
  1159. if (GetASNTag(data, &idx, &tag, ci->dataSz) < 0) {
  1160. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1161. }
  1162. if (tag != ASN_OCTET_STRING) {
  1163. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1164. }
  1165. if ((ret = GetLength(data, &idx, &size, ci->dataSz))
  1166. < 0) {
  1167. goto exit_pk12par;
  1168. }
  1169. break;
  1170. default:
  1171. WOLFSSL_MSG("Unknown PKCS12 cert bag type");
  1172. }
  1173. if (size + idx > (word32)bagSz) {
  1174. ERROR_OUT(ASN_PARSE_E, exit_pk12par);
  1175. }
  1176. /* list to hold all certs found */
  1177. node = (WC_DerCertList*)XMALLOC(sizeof(WC_DerCertList),
  1178. pkcs12->heap, DYNAMIC_TYPE_PKCS);
  1179. if (node == NULL) {
  1180. ERROR_OUT(MEMORY_E, exit_pk12par);
  1181. }
  1182. XMEMSET(node, 0, sizeof(WC_DerCertList));
  1183. node->buffer = (byte*)XMALLOC(size, pkcs12->heap,
  1184. DYNAMIC_TYPE_PKCS);
  1185. if (node->buffer == NULL) {
  1186. XFREE(node, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  1187. ERROR_OUT(MEMORY_E, exit_pk12par);
  1188. }
  1189. XMEMCPY(node->buffer, data + idx, size);
  1190. node->bufferSz = size;
  1191. /* put the new node into the list */
  1192. if (certList != NULL) {
  1193. WOLFSSL_MSG("Pushing new cert onto queue");
  1194. tailList->next = node;
  1195. tailList = node;
  1196. }
  1197. else {
  1198. certList = node;
  1199. tailList = node;
  1200. }
  1201. /* on to next */
  1202. idx += size;
  1203. }
  1204. break;
  1205. case WC_PKCS12_CrlBag: /* 670 */
  1206. WOLFSSL_MSG("PKCS12 CRL BAG not yet supported");
  1207. break;
  1208. case WC_PKCS12_SecretBag: /* 671 */
  1209. WOLFSSL_MSG("PKCS12 Secret BAG not yet supported");
  1210. break;
  1211. case WC_PKCS12_SafeContentsBag: /* 672 */
  1212. WOLFSSL_MSG("PKCS12 Safe Contents BAG not yet supported");
  1213. break;
  1214. default:
  1215. WOLFSSL_MSG("Unknown PKCS12 BAG type found");
  1216. }
  1217. /* Attribute, unknown bag or unsupported */
  1218. if ((int)idx < bagSz) {
  1219. idx = bagSz; /* skip for now */
  1220. }
  1221. }
  1222. /* free temporary buffer */
  1223. if (buf != NULL) {
  1224. XFREE(buf, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  1225. buf = NULL;
  1226. }
  1227. ci = ci->next;
  1228. WOLFSSL_MSG("Done Parsing PKCS12 Content Info Container");
  1229. }
  1230. /* check if key pair, remove from list */
  1231. if (*pkey != NULL) {
  1232. ret = freeDecCertList(&certList, pkey, pkeySz, cert, certSz,
  1233. pkcs12->heap);
  1234. if (ret < 0)
  1235. goto exit_pk12par;
  1236. }
  1237. /* if ca arg provided return certList, otherwise free it */
  1238. if (ca != NULL) {
  1239. *ca = certList;
  1240. }
  1241. else {
  1242. /* free list, not wanted */
  1243. wc_FreeCertList(certList, pkcs12->heap);
  1244. }
  1245. (void)tailList; /* not used */
  1246. ret = 0; /* success */
  1247. exit_pk12par:
  1248. if (ret != 0) {
  1249. /* failure cleanup */
  1250. if (*pkey) {
  1251. XFREE(*pkey, pkcs12->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  1252. *pkey = NULL;
  1253. }
  1254. if (buf) {
  1255. XFREE(buf, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  1256. buf = NULL;
  1257. }
  1258. wc_FreeCertList(certList, pkcs12->heap);
  1259. }
  1260. return ret;
  1261. }
  1262. /* Helper function to shroud keys.
  1263. *
  1264. * pkcs12 structure to use with shrouding key
  1265. * rng random number generator used
  1266. * out buffer to hold results
  1267. * outSz size of out buffer
  1268. * key key that is going to be shrouded
  1269. * keySz size of key buffer
  1270. * vAlgo algorithm version
  1271. * pass password to use
  1272. * passSz size of pass buffer
  1273. * itt number of iterations
  1274. *
  1275. * returns the size of the shrouded key on success
  1276. */
  1277. static int wc_PKCS12_shroud_key(WC_PKCS12* pkcs12, WC_RNG* rng,
  1278. byte* out, word32* outSz, byte* key, word32 keySz, int vAlgo,
  1279. const char* pass, int passSz, int itt)
  1280. {
  1281. void* heap;
  1282. word32 tmpIdx = 0;
  1283. int vPKCS = 1; /* PKCS#12 default set to 1 */
  1284. word32 sz;
  1285. word32 totalSz = 0;
  1286. int ret;
  1287. byte* pkcs8Key = NULL;
  1288. if (outSz == NULL || pkcs12 == NULL || rng == NULL || key == NULL ||
  1289. pass == NULL) {
  1290. return BAD_FUNC_ARG;
  1291. }
  1292. heap = wc_PKCS12_GetHeap(pkcs12);
  1293. /* check if trying to get size */
  1294. if (out != NULL) {
  1295. tmpIdx += MAX_LENGTH_SZ + 1; /* save room for length and tag (+1) */
  1296. sz = *outSz - tmpIdx;
  1297. pkcs8Key = out + tmpIdx;
  1298. }
  1299. /* case of no encryption */
  1300. if (vAlgo < 0) {
  1301. const byte* curveOID = NULL;
  1302. word32 oidSz = 0;
  1303. int algoID;
  1304. WOLFSSL_MSG("creating PKCS12 Key Bag");
  1305. /* check key type and get OID if ECC */
  1306. if ((ret = wc_GetKeyOID(key, keySz, &curveOID, &oidSz, &algoID, heap))
  1307. < 0) {
  1308. return ret;
  1309. }
  1310. /* PKCS#8 wrapping around key */
  1311. ret = wc_CreatePKCS8Key(pkcs8Key, &sz, key, keySz, algoID, curveOID,
  1312. oidSz);
  1313. }
  1314. else {
  1315. WOLFSSL_MSG("creating PKCS12 Shrouded Key Bag");
  1316. if (vAlgo == PBE_SHA1_DES) {
  1317. vPKCS = PKCS5;
  1318. vAlgo = 10;
  1319. }
  1320. ret = UnTraditionalEnc(key, keySz, pkcs8Key, &sz, pass, passSz,
  1321. vPKCS, vAlgo, NULL, 0, itt, rng, heap);
  1322. }
  1323. if (ret == LENGTH_ONLY_E) {
  1324. *outSz = sz + MAX_LENGTH_SZ + 1;
  1325. return LENGTH_ONLY_E;
  1326. }
  1327. if (ret < 0) {
  1328. return ret;
  1329. }
  1330. totalSz += ret;
  1331. /* out should not be null at this point but check before writing */
  1332. if (out == NULL) {
  1333. return BAD_FUNC_ARG;
  1334. }
  1335. /* rewind index and set tag and length */
  1336. tmpIdx -= MAX_LENGTH_SZ + 1;
  1337. sz = SetExplicit(0, ret, out + tmpIdx);
  1338. tmpIdx += sz; totalSz += sz;
  1339. XMEMMOVE(out + tmpIdx, out + MAX_LENGTH_SZ + 1, ret);
  1340. return totalSz;
  1341. }
  1342. /* Helper function to create key bag.
  1343. *
  1344. * pkcs12 structure to use with key bag
  1345. * rng random number generator used
  1346. * out buffer to hold results
  1347. * outSz size of out buffer
  1348. * key key that is going into key bag
  1349. * keySz size of key buffer
  1350. * algo algorithm version
  1351. * iter number of iterations
  1352. * pass password to use
  1353. * passSz size of pass buffer
  1354. *
  1355. * returns the size of the key bag on success
  1356. */
  1357. static int wc_PKCS12_create_key_bag(WC_PKCS12* pkcs12, WC_RNG* rng,
  1358. byte* out, word32* outSz, byte* key, word32 keySz, int algo, int iter,
  1359. char* pass, int passSz)
  1360. {
  1361. void* heap;
  1362. byte* tmp;
  1363. word32 length = 0;
  1364. word32 idx = 0;
  1365. word32 totalSz = 0;
  1366. word32 sz;
  1367. word32 i;
  1368. word32 tmpSz;
  1369. int ret;
  1370. /* get max size for shrouded key */
  1371. ret = wc_PKCS12_shroud_key(pkcs12, rng, NULL, &length, key, keySz,
  1372. algo, pass, passSz, iter);
  1373. if (ret != LENGTH_ONLY_E && ret < 0) {
  1374. return ret;
  1375. }
  1376. if (out == NULL) {
  1377. *outSz = MAX_SEQ_SZ + WC_PKCS12_DATA_OBJ_SZ + 1 + MAX_LENGTH_SZ +
  1378. length;
  1379. return LENGTH_ONLY_E;
  1380. }
  1381. heap = wc_PKCS12_GetHeap(pkcs12);
  1382. /* leave room for sequence */
  1383. idx += MAX_SEQ_SZ;
  1384. if (algo < 0) { /* not encrypted */
  1385. out[idx++] = ASN_OBJECT_ID; totalSz++;
  1386. sz = SetLength(sizeof(WC_PKCS12_KeyBag_OID), out + idx);
  1387. idx += sz; totalSz += sz;
  1388. for (i = 0; i < sizeof(WC_PKCS12_KeyBag_OID); i++) {
  1389. out[idx++] = WC_PKCS12_KeyBag_OID[i]; totalSz++;
  1390. }
  1391. }
  1392. else { /* encrypted */
  1393. out[idx++] = ASN_OBJECT_ID; totalSz++;
  1394. sz = SetLength(sizeof(WC_PKCS12_ShroudedKeyBag_OID), out + idx);
  1395. idx += sz; totalSz += sz;
  1396. for (i = 0; i < sizeof(WC_PKCS12_ShroudedKeyBag_OID); i++) {
  1397. out[idx++] = WC_PKCS12_ShroudedKeyBag_OID[i]; totalSz++;
  1398. }
  1399. }
  1400. /* shroud key */
  1401. tmp = (byte*)XMALLOC(length, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1402. if (tmp == NULL) {
  1403. return MEMORY_E;
  1404. }
  1405. ret = wc_PKCS12_shroud_key(pkcs12, rng, tmp, &length, key, keySz,
  1406. algo, pass, passSz, iter);
  1407. if (ret < 0) {
  1408. XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1409. return ret;
  1410. }
  1411. length = ret;
  1412. XMEMCPY(out + idx, tmp, length);
  1413. XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1414. totalSz += length;
  1415. /* set beginning sequence */
  1416. tmpSz = SetSequence(totalSz, out);
  1417. XMEMMOVE(out + tmpSz, out + MAX_SEQ_SZ, totalSz);
  1418. (void)heap;
  1419. return totalSz + tmpSz;
  1420. }
  1421. /* Helper function to create cert bag.
  1422. *
  1423. * pkcs12 structure to use with cert bag
  1424. * out buffer to hold results
  1425. * outSz size of out buffer
  1426. * cert cert that is going into cert bag
  1427. * certSz size of cert buffer
  1428. *
  1429. * returns the size of the cert bag on success
  1430. */
  1431. static int wc_PKCS12_create_cert_bag(WC_PKCS12* pkcs12,
  1432. byte* out, word32* outSz, byte* cert, word32 certSz)
  1433. {
  1434. word32 length = 0;
  1435. word32 idx = 0;
  1436. word32 totalSz = 0;
  1437. word32 sz;
  1438. int WC_CERTBAG_OBJECT_ID = 13;
  1439. int WC_CERTBAG1_OBJECT_ID = 12;
  1440. word32 i;
  1441. word32 tmpSz;
  1442. if (out == NULL) {
  1443. *outSz = MAX_SEQ_SZ + WC_CERTBAG_OBJECT_ID + 1 + MAX_LENGTH_SZ +
  1444. MAX_SEQ_SZ + WC_CERTBAG1_OBJECT_ID + 1 + MAX_LENGTH_SZ + 1 +
  1445. MAX_LENGTH_SZ + certSz;
  1446. return LENGTH_ONLY_E;
  1447. }
  1448. /* check buffer size able to handle max size */
  1449. if (*outSz < (MAX_SEQ_SZ + WC_CERTBAG_OBJECT_ID + 1 + MAX_LENGTH_SZ +
  1450. MAX_SEQ_SZ + WC_CERTBAG1_OBJECT_ID + 1 + MAX_LENGTH_SZ + 1 +
  1451. MAX_LENGTH_SZ + certSz)) {
  1452. return BUFFER_E;
  1453. }
  1454. /* save room for sequence */
  1455. idx += MAX_SEQ_SZ;
  1456. /* objectId WC_PKCS12_CertBag */
  1457. out[idx++] = ASN_OBJECT_ID; totalSz++;
  1458. sz = SetLength(sizeof(WC_PKCS12_CertBag_OID), out + idx);
  1459. idx += sz; totalSz += sz;
  1460. for (i = 0; i < sizeof(WC_PKCS12_CertBag_OID); i++) {
  1461. out[idx++] = WC_PKCS12_CertBag_OID[i]; totalSz++;
  1462. }
  1463. /**** Cert Bag type 1 ****/
  1464. out[idx++] = (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC); totalSz++;
  1465. /* save room for length and sequence */
  1466. idx += MAX_LENGTH_SZ;
  1467. idx += MAX_SEQ_SZ;
  1468. /* object id WC_PKCS12_CertBag_Type1 */
  1469. out[idx++] = ASN_OBJECT_ID; length++;
  1470. sz = SetLength(sizeof(WC_PKCS12_CertBag_Type1_OID), out + idx);
  1471. idx += sz; length += sz;
  1472. for (i = 0; i < sizeof(WC_PKCS12_CertBag_Type1_OID); i++) {
  1473. out[idx++] = WC_PKCS12_CertBag_Type1_OID[i]; length++;
  1474. }
  1475. out[idx++] = (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC); length++;
  1476. sz = 0;
  1477. idx += MAX_LENGTH_SZ; /* save room for length */
  1478. /* place the cert in the buffer */
  1479. out[idx++] = ASN_OCTET_STRING; sz++;
  1480. tmpSz = SetLength(certSz, out + idx);
  1481. idx += tmpSz; sz += tmpSz;
  1482. XMEMCPY(out + idx, cert, certSz);
  1483. idx += certSz; sz += certSz;
  1484. /* rewind idx and place length */
  1485. idx -= (sz + MAX_LENGTH_SZ);
  1486. tmpSz = SetLength(sz, out + idx);
  1487. XMEMMOVE(out + idx + tmpSz, out + idx + MAX_LENGTH_SZ, sz);
  1488. idx += tmpSz + sz; length += tmpSz + sz;
  1489. /* rewind idx and set sequence */
  1490. idx -= (length + MAX_SEQ_SZ);
  1491. tmpSz = SetSequence(length, out + idx);
  1492. XMEMMOVE(out + idx + tmpSz, out + idx + MAX_SEQ_SZ, length);
  1493. length += tmpSz;
  1494. /* place final length */
  1495. idx -= MAX_LENGTH_SZ;
  1496. tmpSz = SetLength(length, out + idx);
  1497. XMEMMOVE(out + idx + tmpSz, out + idx + MAX_LENGTH_SZ, length);
  1498. length += tmpSz;
  1499. /* place final sequence */
  1500. totalSz += length;
  1501. tmpSz = SetSequence(totalSz, out);
  1502. XMEMMOVE(out + tmpSz, out + MAX_SEQ_SZ, totalSz);
  1503. (void)pkcs12;
  1504. return totalSz + tmpSz;
  1505. }
  1506. /* Helper function to encrypt content.
  1507. *
  1508. * pkcs12 structure to use with key bag
  1509. * rng random number generator used
  1510. * out buffer to hold results
  1511. * outSz size of out buffer
  1512. * content content to encrypt
  1513. * contentSz size of content buffer
  1514. * vAlgo algorithm version
  1515. * pass password to use
  1516. * passSz size of pass buffer
  1517. * iter number of iterations
  1518. * type content type i.e WC_PKCS12_ENCRYPTED_DATA or WC_PKCS12_DATA
  1519. *
  1520. * returns the size of result on success
  1521. */
  1522. static int wc_PKCS12_encrypt_content(WC_PKCS12* pkcs12, WC_RNG* rng,
  1523. byte* out, word32* outSz, byte* content, word32 contentSz, int vAlgo,
  1524. const char* pass, int passSz, int iter, int type)
  1525. {
  1526. void* heap;
  1527. int vPKCS = 1; /* PKCS#12 is always set to 1 */
  1528. int ret;
  1529. byte* tmp;
  1530. word32 idx = 0;
  1531. word32 totalSz = 0;
  1532. word32 length = 0;
  1533. word32 tmpSz;
  1534. word32 encSz;
  1535. byte seq[MAX_SEQ_SZ];
  1536. WOLFSSL_MSG("encrypting PKCS12 content");
  1537. heap = wc_PKCS12_GetHeap(pkcs12);
  1538. /* ENCRYPTED DATA
  1539. * ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC
  1540. * length
  1541. * sequence
  1542. * short int
  1543. * sequence
  1544. * get object id */
  1545. if (type == WC_PKCS12_ENCRYPTED_DATA) {
  1546. word32 outerSz = 0;
  1547. encSz = contentSz;
  1548. if ((ret = EncryptContent(NULL, contentSz, NULL, &encSz,
  1549. pass, passSz, vPKCS, vAlgo, NULL, 0, iter, rng, heap)) < 0) {
  1550. if (ret != LENGTH_ONLY_E) {
  1551. return ret;
  1552. }
  1553. }
  1554. /* calculate size */
  1555. totalSz = SetObjectId(sizeof(WC_PKCS12_ENCRYPTED_OID), seq);
  1556. totalSz += sizeof(WC_PKCS12_ENCRYPTED_OID);
  1557. totalSz += ASN_TAG_SZ;
  1558. length = SetMyVersion(0, seq, 0);
  1559. tmpSz = SetObjectId(sizeof(WC_PKCS12_DATA_OID), seq);
  1560. tmpSz += sizeof(WC_PKCS12_DATA_OID);
  1561. tmpSz += encSz;
  1562. length += SetSequence(tmpSz, seq) + tmpSz;
  1563. outerSz = SetSequence(length, seq) + length;
  1564. totalSz += SetLength(outerSz, seq) + outerSz;
  1565. if (out == NULL) {
  1566. *outSz = totalSz + SetSequence(totalSz, seq);
  1567. return LENGTH_ONLY_E;
  1568. }
  1569. if (*outSz < totalSz + SetSequence(totalSz, seq)) {
  1570. return BUFFER_E;
  1571. }
  1572. idx = 0;
  1573. idx += SetSequence(totalSz, out + idx);
  1574. idx += SetObjectId(sizeof(WC_PKCS12_ENCRYPTED_OID), out + idx);
  1575. if (idx + sizeof(WC_PKCS12_ENCRYPTED_OID) > *outSz){
  1576. return BUFFER_E;
  1577. }
  1578. XMEMCPY(out + idx, WC_PKCS12_ENCRYPTED_OID,
  1579. sizeof(WC_PKCS12_ENCRYPTED_OID));
  1580. idx += sizeof(WC_PKCS12_ENCRYPTED_OID);
  1581. if (idx + 1 > *outSz){
  1582. return BUFFER_E;
  1583. }
  1584. out[idx++] = (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC);
  1585. idx += SetLength(outerSz, out + idx);
  1586. idx += SetSequence(length, out + idx);
  1587. idx += SetMyVersion(0, out + idx, 0);
  1588. tmp = (byte*)XMALLOC(encSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1589. if (tmp == NULL) {
  1590. return MEMORY_E;
  1591. }
  1592. if ((ret = EncryptContent(content, contentSz, tmp, &encSz,
  1593. pass, passSz, vPKCS, vAlgo, NULL, 0, iter, rng, heap)) < 0) {
  1594. XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1595. return ret;
  1596. }
  1597. encSz = ret;
  1598. #ifdef WOLFSSL_DEBUG_PKCS12
  1599. {
  1600. byte* p;
  1601. for (printf("(size %u) Encrypted Content = ", encSz),
  1602. p = (byte*)tmp;
  1603. p < (byte*)tmp + encSz;
  1604. printf("%02X", *p), p++);
  1605. printf("\n");
  1606. }
  1607. #endif
  1608. idx += SetSequence(WC_PKCS12_DATA_OBJ_SZ + encSz, out + idx);
  1609. idx += SetObjectId(sizeof(WC_PKCS12_DATA_OID), out + idx);
  1610. if (idx + sizeof(WC_PKCS12_DATA_OID) > *outSz){
  1611. WOLFSSL_MSG("Buffer not large enough for DATA OID");
  1612. XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1613. return BUFFER_E;
  1614. }
  1615. XMEMCPY(out + idx, WC_PKCS12_DATA_OID, sizeof(WC_PKCS12_DATA_OID));
  1616. idx += sizeof(WC_PKCS12_DATA_OID);
  1617. /* copy over encrypted data */
  1618. if (idx + encSz > *outSz){
  1619. XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1620. return BUFFER_E;
  1621. }
  1622. XMEMCPY(out + idx, tmp, encSz);
  1623. XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1624. idx += encSz;
  1625. return idx;
  1626. }
  1627. /* DATA
  1628. * ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC
  1629. * length
  1630. * ASN_OCTET_STRING
  1631. * length
  1632. * sequence containing all bags */
  1633. if (type == WC_PKCS12_DATA) {
  1634. /* calculate size */
  1635. totalSz = SetObjectId(sizeof(WC_PKCS12_DATA_OID), seq);
  1636. totalSz += sizeof(WC_PKCS12_DATA_OID);
  1637. totalSz += ASN_TAG_SZ;
  1638. length = SetOctetString(contentSz, seq);
  1639. length += contentSz;
  1640. totalSz += SetLength(length, seq);
  1641. totalSz += length;
  1642. if (out == NULL) {
  1643. *outSz = totalSz + SetSequence(totalSz, seq);
  1644. return LENGTH_ONLY_E;
  1645. }
  1646. if (*outSz < (totalSz + SetSequence(totalSz, seq))) {
  1647. return BUFFER_E;
  1648. }
  1649. /* place data in output buffer */
  1650. idx = 0;
  1651. idx += SetSequence(totalSz, out);
  1652. idx += SetObjectId(sizeof(WC_PKCS12_DATA_OID), out + idx);
  1653. if (idx + sizeof(WC_PKCS12_DATA_OID) > *outSz){
  1654. WOLFSSL_MSG("Buffer not large enough for DATA OID");
  1655. return BUFFER_E;
  1656. }
  1657. XMEMCPY(out + idx, WC_PKCS12_DATA_OID, sizeof(WC_PKCS12_DATA_OID));
  1658. idx += sizeof(WC_PKCS12_DATA_OID);
  1659. if (idx + 1 > *outSz){
  1660. return BUFFER_E;
  1661. }
  1662. out[idx++] = (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC);
  1663. idx += SetLength(length, out + idx);
  1664. idx += SetOctetString(contentSz, out + idx);
  1665. if (idx + contentSz > *outSz){
  1666. return BUFFER_E;
  1667. }
  1668. XMEMCPY(out + idx, content, contentSz);
  1669. idx += contentSz;
  1670. return idx;
  1671. }
  1672. WOLFSSL_MSG("Unknown/Unsupported content type");
  1673. return BAD_FUNC_ARG;
  1674. }
  1675. /* helper function to create the PKCS12 key content
  1676. * keyCiSz is output buffer size
  1677. * returns a pointer to be free'd by caller on success and NULL on failure */
  1678. static byte* PKCS12_create_key_content(WC_PKCS12* pkcs12, int nidKey,
  1679. word32* keyCiSz, WC_RNG* rng, char* pass, word32 passSz,
  1680. byte* key, word32 keySz, int iter)
  1681. {
  1682. byte* keyBuf;
  1683. word32 keyBufSz = 0;
  1684. byte* keyCi = NULL;
  1685. word32 tmpSz;
  1686. int ret;
  1687. int algo;
  1688. void* heap;
  1689. heap = wc_PKCS12_GetHeap(pkcs12);
  1690. *keyCiSz = 0;
  1691. switch (nidKey) {
  1692. case PBE_SHA1_RC4_128:
  1693. algo = 1;
  1694. break;
  1695. case PBE_SHA1_DES:
  1696. algo = 2;
  1697. break;
  1698. case PBE_SHA1_DES3:
  1699. algo = 3;
  1700. break;
  1701. /* no encryption */
  1702. case -1:
  1703. algo = -1;
  1704. break;
  1705. default:
  1706. WOLFSSL_MSG("Unknown/Unsupported key encryption");
  1707. return NULL;
  1708. }
  1709. /* get max size for key bag */
  1710. ret = wc_PKCS12_create_key_bag(pkcs12, rng, NULL, &keyBufSz, key, keySz,
  1711. algo, iter, pass, passSz);
  1712. if (ret != LENGTH_ONLY_E && ret < 0) {
  1713. WOLFSSL_MSG("Error getting key bag size");
  1714. return NULL;
  1715. }
  1716. /* account for sequence around bag */
  1717. keyBufSz += MAX_SEQ_SZ;
  1718. keyBuf = (byte*)XMALLOC(keyBufSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1719. if (keyBuf == NULL) {
  1720. WOLFSSL_MSG("Memory error creating keyBuf buffer");
  1721. return NULL;
  1722. }
  1723. ret = wc_PKCS12_create_key_bag(pkcs12, rng, keyBuf + MAX_SEQ_SZ, &keyBufSz,
  1724. key, keySz, algo, iter, pass, passSz);
  1725. if (ret < 0) {
  1726. XFREE(keyBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1727. WOLFSSL_MSG("Error creating key bag");
  1728. return NULL;
  1729. }
  1730. keyBufSz = ret;
  1731. tmpSz = SetSequence(keyBufSz, keyBuf);
  1732. XMEMMOVE(keyBuf + tmpSz, keyBuf + MAX_SEQ_SZ, keyBufSz);
  1733. keyBufSz += tmpSz;
  1734. #ifdef WOLFSSL_DEBUG_PKCS12
  1735. {
  1736. word32 i;
  1737. printf("(size %u) Key Bag = ", keyBufSz);
  1738. for (i = 0; i < keyBufSz; i++)
  1739. printf("%02X", keyBuf[i]);
  1740. printf("\n");
  1741. }
  1742. #endif
  1743. ret = wc_PKCS12_encrypt_content(pkcs12, rng, NULL, keyCiSz,
  1744. NULL, keyBufSz, algo, pass, passSz, iter, WC_PKCS12_DATA);
  1745. if (ret != LENGTH_ONLY_E) {
  1746. XFREE(keyBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1747. WOLFSSL_MSG("Error getting key encrypt content size");
  1748. return NULL;
  1749. }
  1750. keyCi = (byte*)XMALLOC(*keyCiSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1751. if (keyCi == NULL) {
  1752. XFREE(keyBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1753. return NULL;
  1754. }
  1755. ret = wc_PKCS12_encrypt_content(pkcs12, rng, keyCi, keyCiSz,
  1756. keyBuf, keyBufSz, algo, pass, passSz, iter, WC_PKCS12_DATA);
  1757. XFREE(keyBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1758. if (ret < 0 ) {
  1759. XFREE(keyCi, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1760. WOLFSSL_MSG("Error creating key encrypt content");
  1761. return NULL;
  1762. }
  1763. *keyCiSz = ret;
  1764. #ifdef WOLFSSL_DEBUG_PKCS12
  1765. {
  1766. word32 i;
  1767. printf("(size %u) Key Content Info = ", *keyCiSz);
  1768. for (i = 0; i < *keyCiSz; i++)
  1769. printf("%02X", keyCi[i]);
  1770. printf("\n");
  1771. }
  1772. #endif
  1773. (void)heap;
  1774. return keyCi;
  1775. }
  1776. /* helper function to create the PKCS12 certificate content
  1777. * certCiSz is output buffer size
  1778. * returns a pointer to be free'd by caller on success and NULL on failure */
  1779. static byte* PKCS12_create_cert_content(WC_PKCS12* pkcs12, int nidCert,
  1780. WC_DerCertList* ca, byte* cert, word32 certSz, word32* certCiSz,
  1781. WC_RNG* rng, char* pass, word32 passSz, int iter)
  1782. {
  1783. int algo;
  1784. int ret;
  1785. int type;
  1786. byte* certBuf = NULL;
  1787. word32 certBufSz;
  1788. word32 idx;
  1789. word32 sz;
  1790. word32 tmpSz;
  1791. byte* certCi;
  1792. void* heap;
  1793. heap = wc_PKCS12_GetHeap(pkcs12);
  1794. switch (nidCert) {
  1795. case PBE_SHA1_RC4_128:
  1796. type = WC_PKCS12_ENCRYPTED_DATA;
  1797. algo = 1;
  1798. break;
  1799. case PBE_SHA1_DES:
  1800. type = WC_PKCS12_ENCRYPTED_DATA;
  1801. algo = 2;
  1802. break;
  1803. case PBE_SHA1_DES3:
  1804. type = WC_PKCS12_ENCRYPTED_DATA;
  1805. algo = 3;
  1806. break;
  1807. case -1:
  1808. type = WC_PKCS12_DATA;
  1809. algo = -1;
  1810. break;
  1811. default:
  1812. WOLFSSL_MSG("Unknown/Unsupported certificate encryption");
  1813. return NULL;
  1814. }
  1815. /* get max size of buffer needed */
  1816. ret = wc_PKCS12_create_cert_bag(pkcs12, NULL, &certBufSz, cert, certSz);
  1817. if (ret != LENGTH_ONLY_E) {
  1818. return NULL;
  1819. }
  1820. if (ca != NULL) {
  1821. WC_DerCertList* current = ca;
  1822. word32 curBufSz = 0;
  1823. /* get max buffer size */
  1824. while (current != NULL) {
  1825. ret = wc_PKCS12_create_cert_bag(pkcs12, NULL, &curBufSz,
  1826. current->buffer, current->bufferSz);
  1827. if (ret != LENGTH_ONLY_E) {
  1828. return NULL;
  1829. }
  1830. certBufSz += curBufSz;
  1831. current = current->next;
  1832. }
  1833. }
  1834. /* account for Sequence that holds all certificate bags */
  1835. certBufSz += MAX_SEQ_SZ;
  1836. /* completed getting max size, now create buffer and start adding bags */
  1837. certBuf = (byte*)XMALLOC(certBufSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1838. if (certBuf == NULL) {
  1839. WOLFSSL_MSG("Memory error creating certificate bags");
  1840. return NULL;
  1841. }
  1842. idx = 0;
  1843. idx += MAX_SEQ_SZ;
  1844. sz = certBufSz - idx;
  1845. if ((ret = wc_PKCS12_create_cert_bag(pkcs12, certBuf + idx, &sz,
  1846. cert, certSz)) < 0) {
  1847. XFREE(certBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1848. return NULL;
  1849. }
  1850. idx += ret;
  1851. if (ca != NULL) {
  1852. WC_DerCertList* current = ca;
  1853. while (current != NULL) {
  1854. sz = certBufSz - idx;
  1855. if ((ret = wc_PKCS12_create_cert_bag(pkcs12, certBuf + idx, &sz,
  1856. current->buffer, current->bufferSz)) < 0) {
  1857. XFREE(certBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1858. return NULL;
  1859. }
  1860. idx += ret;
  1861. current = current->next;
  1862. }
  1863. }
  1864. /* set sequence and create encrypted content with all certificate bags */
  1865. tmpSz = SetSequence(idx - MAX_SEQ_SZ, certBuf);
  1866. XMEMMOVE(certBuf + tmpSz, certBuf + MAX_SEQ_SZ, idx - MAX_SEQ_SZ);
  1867. certBufSz = tmpSz + (idx - MAX_SEQ_SZ);
  1868. /* get buffer size needed for content info */
  1869. ret = wc_PKCS12_encrypt_content(pkcs12, rng, NULL, certCiSz,
  1870. NULL, certBufSz, algo, pass, passSz, iter, type);
  1871. if (ret != LENGTH_ONLY_E) {
  1872. XFREE(certBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1873. WOLFSSL_LEAVE("wc_PKCS12_create()", ret);
  1874. return NULL;
  1875. }
  1876. certCi = (byte*)XMALLOC(*certCiSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1877. if (certCi == NULL) {
  1878. XFREE(certBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1879. return NULL;
  1880. }
  1881. ret = wc_PKCS12_encrypt_content(pkcs12, rng, certCi, certCiSz,
  1882. certBuf, certBufSz, algo, pass, passSz, iter, type);
  1883. XFREE(certBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1884. if (ret < 0) {
  1885. WOLFSSL_LEAVE("wc_PKCS12_create()", ret);
  1886. XFREE(certCi, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1887. return NULL;
  1888. }
  1889. *certCiSz = ret;
  1890. #ifdef WOLFSSL_DEBUG_PKCS12
  1891. {
  1892. word32 i;
  1893. printf("(size %u) Encrypted Certificate Content Info = ", *certCiSz);
  1894. for (i = 0; i < *certCiSz; i++)
  1895. printf("%02X", certCi[i]);
  1896. printf("\n");
  1897. }
  1898. #endif
  1899. (void)heap;
  1900. return certCi;
  1901. }
  1902. /* helper function to create the PKCS12 safe
  1903. * returns 0 on success */
  1904. static int PKCS12_create_safe(WC_PKCS12* pkcs12, byte* certCi, word32 certCiSz,
  1905. byte* keyCi, word32 keyCiSz, WC_RNG* rng, char* pass, word32 passSz,
  1906. int iter)
  1907. {
  1908. int length;
  1909. int ret;
  1910. byte seq[MAX_SEQ_SZ];
  1911. word32 safeDataSz;
  1912. word32 innerDataSz;
  1913. byte *innerData = NULL;
  1914. byte *safeData = NULL;
  1915. word32 idx;
  1916. innerDataSz = certCiSz + keyCiSz+SetSequence(certCiSz + keyCiSz, seq);
  1917. /* add Content Info structs to safe, key first then cert */
  1918. ret = wc_PKCS12_encrypt_content(pkcs12, rng, NULL, &safeDataSz,
  1919. NULL, innerDataSz, 0, NULL, 0, 0, WC_PKCS12_DATA);
  1920. if (ret != LENGTH_ONLY_E) {
  1921. return ret;
  1922. }
  1923. safeData = (byte*)XMALLOC(safeDataSz, pkcs12->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1924. if (safeData == NULL) {
  1925. WOLFSSL_MSG("Error malloc'ing safe data buffer");
  1926. return MEMORY_E;
  1927. }
  1928. /* create sequence of inner data */
  1929. innerData = (byte*)XMALLOC(innerDataSz, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  1930. if (innerData == NULL) {
  1931. WOLFSSL_MSG("Error malloc'ing inner data buffer");
  1932. XFREE(safeData, pkcs12->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1933. return MEMORY_E;
  1934. }
  1935. idx = 0;
  1936. idx += SetSequence(certCiSz + keyCiSz, innerData);
  1937. XMEMCPY(innerData + idx, certCi, certCiSz);
  1938. XMEMCPY(innerData + idx + certCiSz, keyCi, keyCiSz);
  1939. ret = wc_PKCS12_encrypt_content(pkcs12, rng, safeData, &safeDataSz,
  1940. innerData, innerDataSz, 0, pass, passSz, iter, WC_PKCS12_DATA);
  1941. XFREE(innerData, pkcs12->heap, DYNAMIC_TYPE_PKCS);
  1942. if (ret < 0 ) {
  1943. WOLFSSL_MSG("Error setting data type for safe contents");
  1944. XFREE(safeData, pkcs12->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1945. return ret;
  1946. }
  1947. idx = 0;
  1948. ret = GetSequence(safeData, &idx, &length, safeDataSz);
  1949. if (ret < 0) {
  1950. WOLFSSL_MSG("Error getting first sequence of safe");
  1951. XFREE(safeData, pkcs12->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1952. return ret;
  1953. }
  1954. ret = GetSafeContent(pkcs12, safeData, &idx, safeDataSz);
  1955. XFREE(safeData, pkcs12->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1956. if (ret < 0) {
  1957. WOLFSSL_MSG("Unable to create safe contents");
  1958. return ret;
  1959. }
  1960. return 0;
  1961. }
  1962. /*
  1963. * pass : password to use with encryption
  1964. * passSz : size of the password buffer
  1965. * name : friendlyName to use
  1966. * key : DER format of key
  1967. * keySz : size of key buffer
  1968. * cert : DER format of certificate
  1969. * certSz : size of the certificate buffer
  1970. * ca : a list of extra certificates
  1971. * nidKey : type of encryption to use on the key (-1 means no encryption)
  1972. * nidCert : type of encryption to use on the certificate
  1973. * (-1 means no encryption)
  1974. * iter : number of iterations with encryption
  1975. * macIter : number of iterations when creating MAC
  1976. * keyType : flag for signature and/or encryption key
  1977. * heap : pointer to allocate from memory
  1978. *
  1979. * returns a pointer to a new WC_PKCS12 structure on success and NULL if failed
  1980. */
  1981. WC_PKCS12* wc_PKCS12_create(char* pass, word32 passSz, char* name,
  1982. byte* key, word32 keySz, byte* cert, word32 certSz, WC_DerCertList* ca,
  1983. int nidKey, int nidCert, int iter, int macIter, int keyType, void* heap)
  1984. {
  1985. WC_PKCS12* pkcs12;
  1986. WC_RNG rng;
  1987. int ret;
  1988. byte* certCi = NULL;
  1989. byte* keyCi = NULL;
  1990. word32 certCiSz;
  1991. word32 keyCiSz;
  1992. WOLFSSL_ENTER("wc_PKCS12_create()");
  1993. if (wc_InitRng_ex(&rng, heap, INVALID_DEVID) != 0) {
  1994. return NULL;
  1995. }
  1996. if ((pkcs12 = wc_PKCS12_new()) == NULL) {
  1997. wc_FreeRng(&rng);
  1998. WOLFSSL_LEAVE("wc_PKCS12_create", MEMORY_E);
  1999. return NULL;
  2000. }
  2001. if ((ret = wc_PKCS12_SetHeap(pkcs12, heap)) != 0) {
  2002. wc_PKCS12_free(pkcs12);
  2003. wc_FreeRng(&rng);
  2004. WOLFSSL_LEAVE("wc_PKCS12_create", ret);
  2005. (void)ret;
  2006. return NULL;
  2007. }
  2008. if (iter <= 0) {
  2009. iter = WC_PKCS12_ITT_DEFAULT;
  2010. }
  2011. /**** add private key bag ****/
  2012. keyCi = PKCS12_create_key_content(pkcs12, nidKey, &keyCiSz, &rng,
  2013. pass, passSz, key, keySz, iter);
  2014. if (keyCi == NULL) {
  2015. wc_PKCS12_free(pkcs12);
  2016. wc_FreeRng(&rng);
  2017. return NULL;
  2018. }
  2019. /**** add main certificate bag and extras ****/
  2020. certCi = PKCS12_create_cert_content(pkcs12, nidCert, ca, cert, certSz,
  2021. &certCiSz, &rng, pass, passSz, iter);
  2022. if (certCi == NULL) {
  2023. XFREE(keyCi, heap, DYNAMIC_TYPE_TMP_BUFFER);
  2024. wc_PKCS12_free(pkcs12);
  2025. wc_FreeRng(&rng);
  2026. return NULL;
  2027. }
  2028. /**** create safe and Content Info ****/
  2029. ret = PKCS12_create_safe(pkcs12, certCi, certCiSz, keyCi, keyCiSz, &rng,
  2030. pass, passSz, iter);
  2031. XFREE(keyCi, heap, DYNAMIC_TYPE_TMP_BUFFER);
  2032. XFREE(certCi, heap, DYNAMIC_TYPE_TMP_BUFFER);
  2033. if (ret != 0) {
  2034. WOLFSSL_MSG("Unable to create PKCS12 safe");
  2035. wc_PKCS12_free(pkcs12);
  2036. wc_FreeRng(&rng);
  2037. return NULL;
  2038. }
  2039. /* create MAC */
  2040. if (macIter > 0) {
  2041. MacData* mac;
  2042. byte digest[WC_MAX_DIGEST_SIZE]; /* for MAC */
  2043. mac = (MacData*)XMALLOC(sizeof(MacData), heap, DYNAMIC_TYPE_PKCS);
  2044. if (mac == NULL) {
  2045. wc_PKCS12_free(pkcs12);
  2046. wc_FreeRng(&rng);
  2047. WOLFSSL_MSG("Error malloc'ing mac data buffer");
  2048. return NULL;
  2049. }
  2050. XMEMSET(mac, 0, sizeof(MacData));
  2051. pkcs12->signData = mac; /* now wc_PKCS12_free will free all mac too */
  2052. #ifndef NO_SHA256
  2053. mac->oid = SHA256h;
  2054. #elif !defined(NO_SHA)
  2055. mac->oid = SHA;
  2056. #elif defined(WOLFSSL_SHA384)
  2057. mac->oid = SHA384;
  2058. #elif defined(WOLFSSL_SHA512)
  2059. mac->oid = SHA512;
  2060. #else
  2061. WOLFSSL_MSG("No supported hash algorithm compiled in!");
  2062. wc_PKCS12_free(pkcs12);
  2063. wc_FreeRng(&rng);
  2064. return NULL;
  2065. #endif
  2066. /* store number of iterations */
  2067. mac->itt = macIter;
  2068. /* set mac salt */
  2069. mac->saltSz = WC_PKCS12_MAC_SALT_SZ;
  2070. mac->salt = (byte*)XMALLOC(WC_PKCS12_MAC_SALT_SZ, heap,
  2071. DYNAMIC_TYPE_PKCS);
  2072. if (mac->salt == NULL) {
  2073. wc_PKCS12_free(pkcs12);
  2074. wc_FreeRng(&rng);
  2075. WOLFSSL_MSG("Error malloc'ing salt data buffer");
  2076. return NULL;
  2077. }
  2078. if (wc_RNG_GenerateBlock(&rng, mac->salt, mac->saltSz) != 0) {
  2079. WOLFSSL_MSG("Error generating random salt");
  2080. wc_PKCS12_free(pkcs12);
  2081. wc_FreeRng(&rng);
  2082. return NULL;
  2083. }
  2084. ret = wc_PKCS12_create_mac(pkcs12, pkcs12->safe->data,
  2085. pkcs12->safe->dataSz, (const byte*)pass, passSz, digest,
  2086. WC_MAX_DIGEST_SIZE);
  2087. if (ret < 0) {
  2088. wc_PKCS12_free(pkcs12);
  2089. wc_FreeRng(&rng);
  2090. WOLFSSL_MSG("Error creating mac");
  2091. WOLFSSL_LEAVE("wc_PKCS12_create", ret);
  2092. return NULL;
  2093. }
  2094. mac->digestSz = ret;
  2095. mac->digest = (byte*)XMALLOC(ret, heap, DYNAMIC_TYPE_PKCS);
  2096. if (mac->digest == NULL) {
  2097. WOLFSSL_MSG("Error malloc'ing mac digest buffer");
  2098. wc_PKCS12_free(pkcs12);
  2099. wc_FreeRng(&rng);
  2100. return NULL;
  2101. }
  2102. XMEMCPY(mac->digest, digest, mac->digestSz);
  2103. }
  2104. else {
  2105. pkcs12->signData = NULL;
  2106. }
  2107. wc_FreeRng(&rng);
  2108. (void)name;
  2109. (void)keyType;
  2110. return pkcs12;
  2111. }
  2112. /* if using a specific memory heap */
  2113. int wc_PKCS12_SetHeap(WC_PKCS12* pkcs12, void* heap)
  2114. {
  2115. if (pkcs12 == NULL) {
  2116. return BAD_FUNC_ARG;
  2117. }
  2118. pkcs12->heap = heap;
  2119. return 0;
  2120. }
  2121. /* getter for heap */
  2122. void* wc_PKCS12_GetHeap(WC_PKCS12* pkcs12)
  2123. {
  2124. if (pkcs12 == NULL) {
  2125. return NULL;
  2126. }
  2127. return pkcs12->heap;
  2128. }
  2129. #undef ERROR_OUT
  2130. #endif /* HAVE_PKCS12 && !NO_ASN && !NO_PWDBASED && !NO_HMAC */