PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/ctaocrypt/src/asn.c

https://github.com/andersmalm/cyassl
C | 5110 lines | 3782 code | 1041 blank | 287 comment | 881 complexity | 7ce215fd4d2dff86b6113ab1abcbab72 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* asn.c
  2. *
  3. * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef NO_ASN
  25. #ifdef THREADX
  26. #include "os.h" /* dc_rtc_api needs */
  27. #include "dc_rtc_api.h" /* to get current time */
  28. #endif
  29. #include <cyassl/ctaocrypt/integer.h>
  30. #include <cyassl/ctaocrypt/asn.h>
  31. #include <cyassl/ctaocrypt/coding.h>
  32. #include <cyassl/ctaocrypt/sha.h>
  33. #include <cyassl/ctaocrypt/md5.h>
  34. #include <cyassl/ctaocrypt/md2.h>
  35. #include <cyassl/ctaocrypt/error.h>
  36. #include <cyassl/ctaocrypt/pwdbased.h>
  37. #include <cyassl/ctaocrypt/des3.h>
  38. #include <cyassl/ctaocrypt/sha256.h>
  39. #include <cyassl/ctaocrypt/sha512.h>
  40. #include <cyassl/ctaocrypt/logging.h>
  41. #include <cyassl/ctaocrypt/random.h>
  42. #ifndef NO_RC4
  43. #include <cyassl/ctaocrypt/arc4.h>
  44. #endif
  45. #ifdef HAVE_NTRU
  46. #include "crypto_ntru.h"
  47. #endif
  48. #ifdef HAVE_ECC
  49. #include <cyassl/ctaocrypt/ecc.h>
  50. #endif
  51. #ifdef CYASSL_DEBUG_ENCODING
  52. #ifdef FREESCALE_MQX
  53. #include <fio.h>
  54. #else
  55. #include <stdio.h>
  56. #endif
  57. #endif
  58. #ifdef _MSC_VER
  59. /* 4996 warning to use MS extensions e.g., strcpy_s instead of XSTRNCPY */
  60. #pragma warning(disable: 4996)
  61. #endif
  62. #ifndef TRUE
  63. enum {
  64. FALSE = 0,
  65. TRUE = 1
  66. };
  67. #endif
  68. #ifdef THREADX
  69. /* uses parital <time.h> structures */
  70. #define XTIME(tl) (0)
  71. #define XGMTIME(c) my_gmtime((c))
  72. #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
  73. #elif defined(MICRIUM)
  74. #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
  75. #define XVALIDATE_DATE(d,f,t) NetSecure_ValidateDateHandler((d),(f),(t))
  76. #else
  77. #define XVALIDATE_DATE(d, f, t) (0)
  78. #endif
  79. #define NO_TIME_H
  80. /* since Micrium not defining XTIME or XGMTIME, CERT_GEN not available */
  81. #elif defined(USER_TIME)
  82. /* no <time.h> structures used */
  83. #define NO_TIME_H
  84. /* user time, and gmtime compatible functions, there is a gmtime
  85. implementation here that WINCE uses, so really just need some ticks
  86. since the EPOCH
  87. */
  88. #else
  89. /* default */
  90. /* uses complete <time.h> facility */
  91. #include <time.h>
  92. #define XTIME(tl) time((tl))
  93. #define XGMTIME(c) gmtime((c))
  94. #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
  95. #endif
  96. #ifdef _WIN32_WCE
  97. /* no time() or gmtime() even though in time.h header?? */
  98. #include <windows.h>
  99. time_t time(time_t* timer)
  100. {
  101. SYSTEMTIME sysTime;
  102. FILETIME fTime;
  103. ULARGE_INTEGER intTime;
  104. time_t localTime;
  105. if (timer == NULL)
  106. timer = &localTime;
  107. GetSystemTime(&sysTime);
  108. SystemTimeToFileTime(&sysTime, &fTime);
  109. XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
  110. /* subtract EPOCH */
  111. intTime.QuadPart -= 0x19db1ded53e8000;
  112. /* to secs */
  113. intTime.QuadPart /= 10000000;
  114. *timer = (time_t)intTime.QuadPart;
  115. return *timer;
  116. }
  117. struct tm* gmtime(const time_t* timer)
  118. {
  119. #define YEAR0 1900
  120. #define EPOCH_YEAR 1970
  121. #define SECS_DAY (24L * 60L * 60L)
  122. #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
  123. #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
  124. static const int _ytab[2][12] =
  125. {
  126. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  127. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  128. };
  129. static struct tm st_time;
  130. struct tm* ret = &st_time;
  131. time_t time = *timer;
  132. unsigned long dayclock, dayno;
  133. int year = EPOCH_YEAR;
  134. dayclock = (unsigned long)time % SECS_DAY;
  135. dayno = (unsigned long)time / SECS_DAY;
  136. ret->tm_sec = dayclock % 60;
  137. ret->tm_min = (dayclock % 3600) / 60;
  138. ret->tm_hour = dayclock / 3600;
  139. ret->tm_wday = (dayno + 4) % 7; /* day 0 a Thursday */
  140. while(dayno >= (unsigned long)YEARSIZE(year)) {
  141. dayno -= YEARSIZE(year);
  142. year++;
  143. }
  144. ret->tm_year = year - YEAR0;
  145. ret->tm_yday = dayno;
  146. ret->tm_mon = 0;
  147. while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
  148. dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
  149. ret->tm_mon++;
  150. }
  151. ret->tm_mday = ++dayno;
  152. ret->tm_isdst = 0;
  153. return ret;
  154. }
  155. #endif /* _WIN32_WCE */
  156. #ifdef THREADX
  157. #define YEAR0 1900
  158. struct tm* my_gmtime(const time_t* timer) /* has a gmtime() but hangs */
  159. {
  160. static struct tm st_time;
  161. struct tm* ret = &st_time;
  162. DC_RTC_CALENDAR cal;
  163. dc_rtc_time_get(&cal, TRUE);
  164. ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */
  165. ret->tm_mon = cal.month - 1; /* gm starts at 0 */
  166. ret->tm_mday = cal.day;
  167. ret->tm_hour = cal.hour;
  168. ret->tm_min = cal.minute;
  169. ret->tm_sec = cal.second;
  170. return ret;
  171. }
  172. #endif /* THREADX */
  173. static INLINE word32 btoi(byte b)
  174. {
  175. return b - 0x30;
  176. }
  177. /* two byte date/time, add to value */
  178. static INLINE void GetTime(int* value, const byte* date, int* idx)
  179. {
  180. int i = *idx;
  181. *value += btoi(date[i++]) * 10;
  182. *value += btoi(date[i++]);
  183. *idx = i;
  184. }
  185. #if defined(MICRIUM)
  186. CPU_INT32S NetSecure_ValidateDateHandler(CPU_INT08U *date, CPU_INT08U format,
  187. CPU_INT08U dateType)
  188. {
  189. CPU_BOOLEAN rtn_code;
  190. CPU_INT32S i;
  191. CPU_INT32S val;
  192. CPU_INT16U year;
  193. CPU_INT08U month;
  194. CPU_INT16U day;
  195. CPU_INT08U hour;
  196. CPU_INT08U min;
  197. CPU_INT08U sec;
  198. i = 0;
  199. year = 0u;
  200. if (format == ASN_UTC_TIME) {
  201. if (btoi(date[0]) >= 5)
  202. year = 1900;
  203. else
  204. year = 2000;
  205. }
  206. else { /* format == GENERALIZED_TIME */
  207. year += btoi(date[i++]) * 1000;
  208. year += btoi(date[i++]) * 100;
  209. }
  210. val = year;
  211. GetTime(&val, date, &i);
  212. year = (CPU_INT16U)val;
  213. val = 0;
  214. GetTime(&val, date, &i);
  215. month = (CPU_INT08U)val;
  216. val = 0;
  217. GetTime(&val, date, &i);
  218. day = (CPU_INT16U)val;
  219. val = 0;
  220. GetTime(&val, date, &i);
  221. hour = (CPU_INT08U)val;
  222. val = 0;
  223. GetTime(&val, date, &i);
  224. min = (CPU_INT08U)val;
  225. val = 0;
  226. GetTime(&val, date, &i);
  227. sec = (CPU_INT08U)val;
  228. return NetSecure_ValidateDate(year, month, day, hour, min, sec, dateType);
  229. }
  230. #endif /* MICRIUM */
  231. static int GetLength(const byte* input, word32* inOutIdx, int* len,
  232. word32 maxIdx)
  233. {
  234. int length = 0;
  235. word32 i = *inOutIdx;
  236. byte b;
  237. if ( (i+1) > maxIdx) { /* for first read */
  238. CYASSL_MSG("GetLength bad index on input");
  239. return BUFFER_E;
  240. }
  241. b = input[i++];
  242. if (b >= ASN_LONG_LENGTH) {
  243. word32 bytes = b & 0x7F;
  244. if ( (i+bytes) > maxIdx) { /* for reading bytes */
  245. CYASSL_MSG("GetLength bad long length");
  246. return BUFFER_E;
  247. }
  248. while (bytes--) {
  249. b = input[i++];
  250. length = (length << 8) | b;
  251. }
  252. }
  253. else
  254. length = b;
  255. if ( (i+length) > maxIdx) { /* for user of length */
  256. CYASSL_MSG("GetLength value exceeds buffer length");
  257. return BUFFER_E;
  258. }
  259. *inOutIdx = i;
  260. *len = length;
  261. return length;
  262. }
  263. static int GetSequence(const byte* input, word32* inOutIdx, int* len,
  264. word32 maxIdx)
  265. {
  266. int length = -1;
  267. word32 idx = *inOutIdx;
  268. if (input[idx++] != (ASN_SEQUENCE | ASN_CONSTRUCTED) ||
  269. GetLength(input, &idx, &length, maxIdx) < 0)
  270. return ASN_PARSE_E;
  271. *len = length;
  272. *inOutIdx = idx;
  273. return length;
  274. }
  275. static int GetSet(const byte* input, word32* inOutIdx, int* len, word32 maxIdx)
  276. {
  277. int length = -1;
  278. word32 idx = *inOutIdx;
  279. if (input[idx++] != (ASN_SET | ASN_CONSTRUCTED) ||
  280. GetLength(input, &idx, &length, maxIdx) < 0)
  281. return ASN_PARSE_E;
  282. *len = length;
  283. *inOutIdx = idx;
  284. return length;
  285. }
  286. /* winodws header clash for WinCE using GetVersion */
  287. static int GetMyVersion(const byte* input, word32* inOutIdx, int* version)
  288. {
  289. word32 idx = *inOutIdx;
  290. CYASSL_ENTER("GetMyVersion");
  291. if (input[idx++] != ASN_INTEGER)
  292. return ASN_PARSE_E;
  293. if (input[idx++] != 0x01)
  294. return ASN_VERSION_E;
  295. *version = input[idx++];
  296. *inOutIdx = idx;
  297. return *version;
  298. }
  299. /* Get small count integer, 32 bits or less */
  300. static int GetShortInt(const byte* input, word32* inOutIdx, int* number)
  301. {
  302. word32 idx = *inOutIdx;
  303. word32 len;
  304. *number = 0;
  305. if (input[idx++] != ASN_INTEGER)
  306. return ASN_PARSE_E;
  307. len = input[idx++];
  308. if (len > 4)
  309. return ASN_PARSE_E;
  310. while (len--) {
  311. *number = *number << 8 | input[idx++];
  312. }
  313. *inOutIdx = idx;
  314. return *number;
  315. }
  316. /* May not have one, not an error */
  317. static int GetExplicitVersion(const byte* input, word32* inOutIdx, int* version)
  318. {
  319. word32 idx = *inOutIdx;
  320. CYASSL_ENTER("GetExplicitVersion");
  321. if (input[idx++] == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) {
  322. *inOutIdx = ++idx; /* eat header */
  323. return GetMyVersion(input, inOutIdx, version);
  324. }
  325. /* go back as is */
  326. *version = 0;
  327. return 0;
  328. }
  329. static int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx,
  330. word32 maxIdx)
  331. {
  332. word32 i = *inOutIdx;
  333. byte b = input[i++];
  334. int length;
  335. if (b != ASN_INTEGER)
  336. return ASN_PARSE_E;
  337. if (GetLength(input, &i, &length, maxIdx) < 0)
  338. return ASN_PARSE_E;
  339. if ( (b = input[i++]) == 0x00)
  340. length--;
  341. else
  342. i--;
  343. if (mp_init(mpi) != MP_OKAY)
  344. return MP_INIT_E;
  345. if (mp_read_unsigned_bin(mpi, (byte*)input + i, length) != 0) {
  346. mp_clear(mpi);
  347. return ASN_GETINT_E;
  348. }
  349. *inOutIdx = i + length;
  350. return 0;
  351. }
  352. static int GetObjectId(const byte* input, word32* inOutIdx, word32* oid,
  353. word32 maxIdx)
  354. {
  355. int length;
  356. word32 i = *inOutIdx;
  357. byte b;
  358. *oid = 0;
  359. b = input[i++];
  360. if (b != ASN_OBJECT_ID)
  361. return ASN_OBJECT_ID_E;
  362. if (GetLength(input, &i, &length, maxIdx) < 0)
  363. return ASN_PARSE_E;
  364. while(length--)
  365. *oid += input[i++];
  366. /* just sum it up for now */
  367. *inOutIdx = i;
  368. return 0;
  369. }
  370. static int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
  371. word32 maxIdx)
  372. {
  373. int length;
  374. word32 i = *inOutIdx;
  375. byte b;
  376. *oid = 0;
  377. CYASSL_ENTER("GetAlgoId");
  378. if (GetSequence(input, &i, &length, maxIdx) < 0)
  379. return ASN_PARSE_E;
  380. b = input[i++];
  381. if (b != ASN_OBJECT_ID)
  382. return ASN_OBJECT_ID_E;
  383. if (GetLength(input, &i, &length, maxIdx) < 0)
  384. return ASN_PARSE_E;
  385. while(length--) {
  386. /* odd HC08 compiler behavior here when input[i++] */
  387. *oid += input[i];
  388. i++;
  389. }
  390. /* just sum it up for now */
  391. /* could have NULL tag and 0 terminator, but may not */
  392. b = input[i++];
  393. if (b == ASN_TAG_NULL) {
  394. b = input[i++];
  395. if (b != 0)
  396. return ASN_EXPECT_0_E;
  397. }
  398. else
  399. /* go back, didn't have it */
  400. i--;
  401. *inOutIdx = i;
  402. return 0;
  403. }
  404. #ifndef NO_RSA
  405. #ifdef HAVE_CAVIUM
  406. static int GetCaviumInt(byte** buff, word16* buffSz, const byte* input,
  407. word32* inOutIdx, word32 maxIdx, void* heap)
  408. {
  409. word32 i = *inOutIdx;
  410. byte b = input[i++];
  411. int length;
  412. if (b != ASN_INTEGER)
  413. return ASN_PARSE_E;
  414. if (GetLength(input, &i, &length, maxIdx) < 0)
  415. return ASN_PARSE_E;
  416. if ( (b = input[i++]) == 0x00)
  417. length--;
  418. else
  419. i--;
  420. *buffSz = (word16)length;
  421. *buff = XMALLOC(*buffSz, heap, DYNAMIC_TYPE_CAVIUM_RSA);
  422. if (*buff == NULL)
  423. return MEMORY_E;
  424. XMEMCPY(*buff, input + i, *buffSz);
  425. *inOutIdx = i + length;
  426. return 0;
  427. }
  428. static int CaviumRsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
  429. RsaKey* key, word32 inSz)
  430. {
  431. int version, length;
  432. void* h = key->heap;
  433. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  434. return ASN_PARSE_E;
  435. if (GetMyVersion(input, inOutIdx, &version) < 0)
  436. return ASN_PARSE_E;
  437. key->type = RSA_PRIVATE;
  438. if (GetCaviumInt(&key->c_n, &key->c_nSz, input, inOutIdx, inSz, h) < 0 ||
  439. GetCaviumInt(&key->c_e, &key->c_eSz, input, inOutIdx, inSz, h) < 0 ||
  440. GetCaviumInt(&key->c_d, &key->c_dSz, input, inOutIdx, inSz, h) < 0 ||
  441. GetCaviumInt(&key->c_p, &key->c_pSz, input, inOutIdx, inSz, h) < 0 ||
  442. GetCaviumInt(&key->c_q, &key->c_qSz, input, inOutIdx, inSz, h) < 0 ||
  443. GetCaviumInt(&key->c_dP, &key->c_dP_Sz, input, inOutIdx, inSz, h) < 0 ||
  444. GetCaviumInt(&key->c_dQ, &key->c_dQ_Sz, input, inOutIdx, inSz, h) < 0 ||
  445. GetCaviumInt(&key->c_u, &key->c_uSz, input, inOutIdx, inSz, h) < 0 )
  446. return ASN_RSA_KEY_E;
  447. return 0;
  448. }
  449. #endif /* HAVE_CAVIUM */
  450. int RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key,
  451. word32 inSz)
  452. {
  453. int version, length;
  454. #ifdef HAVE_CAVIUM
  455. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC)
  456. return CaviumRsaPrivateKeyDecode(input, inOutIdx, key, inSz);
  457. #endif
  458. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  459. return ASN_PARSE_E;
  460. if (GetMyVersion(input, inOutIdx, &version) < 0)
  461. return ASN_PARSE_E;
  462. key->type = RSA_PRIVATE;
  463. if (GetInt(&key->n, input, inOutIdx, inSz) < 0 ||
  464. GetInt(&key->e, input, inOutIdx, inSz) < 0 ||
  465. GetInt(&key->d, input, inOutIdx, inSz) < 0 ||
  466. GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
  467. GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
  468. GetInt(&key->dP, input, inOutIdx, inSz) < 0 ||
  469. GetInt(&key->dQ, input, inOutIdx, inSz) < 0 ||
  470. GetInt(&key->u, input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E;
  471. return 0;
  472. }
  473. #endif /* NO_RSA */
  474. /* Remove PKCS8 header, move beginning of traditional to beginning of input */
  475. int ToTraditional(byte* input, word32 sz)
  476. {
  477. word32 inOutIdx = 0, oid;
  478. int version, length;
  479. if (GetSequence(input, &inOutIdx, &length, sz) < 0)
  480. return ASN_PARSE_E;
  481. if (GetMyVersion(input, &inOutIdx, &version) < 0)
  482. return ASN_PARSE_E;
  483. if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0)
  484. return ASN_PARSE_E;
  485. if (input[inOutIdx] == ASN_OBJECT_ID) {
  486. /* pkcs8 ecc uses slightly different format */
  487. inOutIdx++; /* past id */
  488. if (GetLength(input, &inOutIdx, &length, sz) < 0)
  489. return ASN_PARSE_E;
  490. inOutIdx += length; /* over sub id, key input will verify */
  491. }
  492. if (input[inOutIdx++] != ASN_OCTET_STRING)
  493. return ASN_PARSE_E;
  494. if (GetLength(input, &inOutIdx, &length, sz) < 0)
  495. return ASN_PARSE_E;
  496. XMEMMOVE(input, input + inOutIdx, length);
  497. return 0;
  498. }
  499. #ifndef NO_PWDBASED
  500. /* Check To see if PKCS version algo is supported, set id if it is return 0
  501. < 0 on error */
  502. static int CheckAlgo(int first, int second, int* id, int* version)
  503. {
  504. *id = ALGO_ID_E;
  505. *version = PKCS5; /* default */
  506. if (first == 1) {
  507. switch (second) {
  508. case 1:
  509. *id = PBE_SHA1_RC4_128;
  510. *version = PKCS12;
  511. return 0;
  512. case 3:
  513. *id = PBE_SHA1_DES3;
  514. *version = PKCS12;
  515. return 0;
  516. default:
  517. return ALGO_ID_E;
  518. }
  519. }
  520. if (first != PKCS5)
  521. return ASN_INPUT_E; /* VERSION ERROR */
  522. if (second == PBES2) {
  523. *version = PKCS5v2;
  524. return 0;
  525. }
  526. switch (second) {
  527. case 3: /* see RFC 2898 for ids */
  528. *id = PBE_MD5_DES;
  529. return 0;
  530. case 10:
  531. *id = PBE_SHA1_DES;
  532. return 0;
  533. default:
  534. return ALGO_ID_E;
  535. }
  536. }
  537. /* Check To see if PKCS v2 algo is supported, set id if it is return 0
  538. < 0 on error */
  539. static int CheckAlgoV2(int oid, int* id)
  540. {
  541. switch (oid) {
  542. case 69:
  543. *id = PBE_SHA1_DES;
  544. return 0;
  545. case 652:
  546. *id = PBE_SHA1_DES3;
  547. return 0;
  548. default:
  549. return ALGO_ID_E;
  550. }
  551. }
  552. /* Decrypt intput in place from parameters based on id */
  553. static int DecryptKey(const char* password, int passwordSz, byte* salt,
  554. int saltSz, int iterations, int id, byte* input,
  555. int length, int version, byte* cbcIv)
  556. {
  557. byte key[MAX_KEY_SIZE];
  558. int typeH;
  559. int derivedLen;
  560. int decryptionType;
  561. int ret = 0;
  562. switch (id) {
  563. case PBE_MD5_DES:
  564. typeH = MD5;
  565. derivedLen = 16; /* may need iv for v1.5 */
  566. decryptionType = DES_TYPE;
  567. break;
  568. case PBE_SHA1_DES:
  569. typeH = SHA;
  570. derivedLen = 16; /* may need iv for v1.5 */
  571. decryptionType = DES_TYPE;
  572. break;
  573. case PBE_SHA1_DES3:
  574. typeH = SHA;
  575. derivedLen = 32; /* may need iv for v1.5 */
  576. decryptionType = DES3_TYPE;
  577. break;
  578. case PBE_SHA1_RC4_128:
  579. typeH = SHA;
  580. derivedLen = 16;
  581. decryptionType = RC4_TYPE;
  582. break;
  583. default:
  584. return ALGO_ID_E;
  585. }
  586. if (version == PKCS5v2)
  587. ret = PBKDF2(key, (byte*)password, passwordSz, salt, saltSz, iterations,
  588. derivedLen, typeH);
  589. else if (version == PKCS5)
  590. ret = PBKDF1(key, (byte*)password, passwordSz, salt, saltSz, iterations,
  591. derivedLen, typeH);
  592. else if (version == PKCS12) {
  593. int i, idx = 0;
  594. byte unicodePasswd[MAX_UNICODE_SZ];
  595. if ( (passwordSz * 2 + 2) > (int)sizeof(unicodePasswd))
  596. return UNICODE_SIZE_E;
  597. for (i = 0; i < passwordSz; i++) {
  598. unicodePasswd[idx++] = 0x00;
  599. unicodePasswd[idx++] = (byte)password[i];
  600. }
  601. /* add trailing NULL */
  602. unicodePasswd[idx++] = 0x00;
  603. unicodePasswd[idx++] = 0x00;
  604. ret = PKCS12_PBKDF(key, unicodePasswd, idx, salt, saltSz,
  605. iterations, derivedLen, typeH, 1);
  606. if (decryptionType != RC4_TYPE)
  607. ret += PKCS12_PBKDF(cbcIv, unicodePasswd, idx, salt, saltSz,
  608. iterations, 8, typeH, 2);
  609. }
  610. if (ret != 0)
  611. return ret;
  612. switch (decryptionType) {
  613. #ifndef NO_DES3
  614. case DES_TYPE:
  615. {
  616. Des dec;
  617. byte* desIv = key + 8;
  618. if (version == PKCS5v2 || version == PKCS12)
  619. desIv = cbcIv;
  620. Des_SetKey(&dec, key, desIv, DES_DECRYPTION);
  621. Des_CbcDecrypt(&dec, input, input, length);
  622. break;
  623. }
  624. case DES3_TYPE:
  625. {
  626. Des3 dec;
  627. byte* desIv = key + 24;
  628. if (version == PKCS5v2 || version == PKCS12)
  629. desIv = cbcIv;
  630. Des3_SetKey(&dec, key, desIv, DES_DECRYPTION);
  631. Des3_CbcDecrypt(&dec, input, input, length);
  632. break;
  633. }
  634. #endif
  635. #ifndef NO_RC4
  636. case RC4_TYPE:
  637. {
  638. Arc4 dec;
  639. Arc4SetKey(&dec, key, derivedLen);
  640. Arc4Process(&dec, input, input, length);
  641. break;
  642. }
  643. #endif
  644. default:
  645. return ALGO_ID_E;
  646. }
  647. return 0;
  648. }
  649. /* Remove Encrypted PKCS8 header, move beginning of traditional to beginning
  650. of input */
  651. int ToTraditionalEnc(byte* input, word32 sz,const char* password,int passwordSz)
  652. {
  653. word32 inOutIdx = 0, oid;
  654. int first, second, length, version, saltSz, id;
  655. int iterations = 0;
  656. byte salt[MAX_SALT_SIZE];
  657. byte cbcIv[MAX_IV_SIZE];
  658. if (GetSequence(input, &inOutIdx, &length, sz) < 0)
  659. return ASN_PARSE_E;
  660. if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0)
  661. return ASN_PARSE_E;
  662. first = input[inOutIdx - 2]; /* PKCS version alwyas 2nd to last byte */
  663. second = input[inOutIdx - 1]; /* version.algo, algo id last byte */
  664. if (CheckAlgo(first, second, &id, &version) < 0)
  665. return ASN_INPUT_E; /* Algo ID error */
  666. if (version == PKCS5v2) {
  667. if (GetSequence(input, &inOutIdx, &length, sz) < 0)
  668. return ASN_PARSE_E;
  669. if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0)
  670. return ASN_PARSE_E;
  671. if (oid != PBKDF2_OID)
  672. return ASN_PARSE_E;
  673. }
  674. if (GetSequence(input, &inOutIdx, &length, sz) < 0)
  675. return ASN_PARSE_E;
  676. if (input[inOutIdx++] != ASN_OCTET_STRING)
  677. return ASN_PARSE_E;
  678. if (GetLength(input, &inOutIdx, &saltSz, sz) < 0)
  679. return ASN_PARSE_E;
  680. if (saltSz > MAX_SALT_SIZE)
  681. return ASN_PARSE_E;
  682. XMEMCPY(salt, &input[inOutIdx], saltSz);
  683. inOutIdx += saltSz;
  684. if (GetShortInt(input, &inOutIdx, &iterations) < 0)
  685. return ASN_PARSE_E;
  686. if (version == PKCS5v2) {
  687. /* get encryption algo */
  688. if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0)
  689. return ASN_PARSE_E;
  690. if (CheckAlgoV2(oid, &id) < 0)
  691. return ASN_PARSE_E; /* PKCS v2 algo id error */
  692. if (input[inOutIdx++] != ASN_OCTET_STRING)
  693. return ASN_PARSE_E;
  694. if (GetLength(input, &inOutIdx, &length, sz) < 0)
  695. return ASN_PARSE_E;
  696. XMEMCPY(cbcIv, &input[inOutIdx], length);
  697. inOutIdx += length;
  698. }
  699. if (input[inOutIdx++] != ASN_OCTET_STRING)
  700. return ASN_PARSE_E;
  701. if (GetLength(input, &inOutIdx, &length, sz) < 0)
  702. return ASN_PARSE_E;
  703. if (DecryptKey(password, passwordSz, salt, saltSz, iterations, id,
  704. input + inOutIdx, length, version, cbcIv) < 0)
  705. return ASN_INPUT_E; /* decrypt failure */
  706. XMEMMOVE(input, input + inOutIdx, length);
  707. return ToTraditional(input, length);
  708. }
  709. #endif /* NO_PWDBASED */
  710. #ifndef NO_RSA
  711. int RsaPublicKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key,
  712. word32 inSz)
  713. {
  714. int length;
  715. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  716. return ASN_PARSE_E;
  717. key->type = RSA_PUBLIC;
  718. #ifdef OPENSSL_EXTRA
  719. {
  720. byte b = input[*inOutIdx];
  721. if (b != ASN_INTEGER) {
  722. /* not from decoded cert, will have algo id, skip past */
  723. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  724. return ASN_PARSE_E;
  725. b = input[(*inOutIdx)++];
  726. if (b != ASN_OBJECT_ID)
  727. return ASN_OBJECT_ID_E;
  728. if (GetLength(input, inOutIdx, &length, inSz) < 0)
  729. return ASN_PARSE_E;
  730. *inOutIdx += length; /* skip past */
  731. /* could have NULL tag and 0 terminator, but may not */
  732. b = input[(*inOutIdx)++];
  733. if (b == ASN_TAG_NULL) {
  734. b = input[(*inOutIdx)++];
  735. if (b != 0)
  736. return ASN_EXPECT_0_E;
  737. }
  738. else
  739. /* go back, didn't have it */
  740. (*inOutIdx)--;
  741. /* should have bit tag length and seq next */
  742. b = input[(*inOutIdx)++];
  743. if (b != ASN_BIT_STRING)
  744. return ASN_BITSTR_E;
  745. if (GetLength(input, inOutIdx, &length, inSz) < 0)
  746. return ASN_PARSE_E;
  747. /* could have 0 */
  748. b = input[(*inOutIdx)++];
  749. if (b != 0)
  750. (*inOutIdx)--;
  751. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  752. return ASN_PARSE_E;
  753. } /* end if */
  754. } /* openssl var block */
  755. #endif /* OPENSSL_EXTRA */
  756. if (GetInt(&key->n, input, inOutIdx, inSz) < 0 ||
  757. GetInt(&key->e, input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E;
  758. return 0;
  759. }
  760. #endif
  761. #ifndef NO_DH
  762. int DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz)
  763. {
  764. int length;
  765. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  766. return ASN_PARSE_E;
  767. if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
  768. GetInt(&key->g, input, inOutIdx, inSz) < 0 ) return ASN_DH_KEY_E;
  769. return 0;
  770. }
  771. int DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g, word32 gSz)
  772. {
  773. /* may have leading 0 */
  774. if (p[0] == 0) {
  775. pSz--; p++;
  776. }
  777. if (g[0] == 0) {
  778. gSz--; g++;
  779. }
  780. if (mp_init(&key->p) != MP_OKAY)
  781. return MP_INIT_E;
  782. if (mp_read_unsigned_bin(&key->p, p, pSz) != 0) {
  783. mp_clear(&key->p);
  784. return ASN_DH_KEY_E;
  785. }
  786. if (mp_init(&key->g) != MP_OKAY) {
  787. mp_clear(&key->p);
  788. return MP_INIT_E;
  789. }
  790. if (mp_read_unsigned_bin(&key->g, g, gSz) != 0) {
  791. mp_clear(&key->g);
  792. mp_clear(&key->p);
  793. return ASN_DH_KEY_E;
  794. }
  795. return 0;
  796. }
  797. #ifdef OPENSSL_EXTRA
  798. int DhParamsLoad(const byte* input, word32 inSz, byte* p, word32* pInOutSz,
  799. byte* g, word32* gInOutSz)
  800. {
  801. word32 i = 0;
  802. byte b;
  803. int length;
  804. if (GetSequence(input, &i, &length, inSz) < 0)
  805. return ASN_PARSE_E;
  806. b = input[i++];
  807. if (b != ASN_INTEGER)
  808. return ASN_PARSE_E;
  809. if (GetLength(input, &i, &length, inSz) < 0)
  810. return ASN_PARSE_E;
  811. if ( (b = input[i++]) == 0x00)
  812. length--;
  813. else
  814. i--;
  815. if (length <= (int)*pInOutSz) {
  816. XMEMCPY(p, &input[i], length);
  817. *pInOutSz = length;
  818. }
  819. else
  820. return BUFFER_E;
  821. i += length;
  822. b = input[i++];
  823. if (b != ASN_INTEGER)
  824. return ASN_PARSE_E;
  825. if (GetLength(input, &i, &length, inSz) < 0)
  826. return ASN_PARSE_E;
  827. if (length <= (int)*gInOutSz) {
  828. XMEMCPY(g, &input[i], length);
  829. *gInOutSz = length;
  830. }
  831. else
  832. return BUFFER_E;
  833. return 0;
  834. }
  835. #endif /* OPENSSL_EXTRA */
  836. #endif /* NO_DH */
  837. #ifndef NO_DSA
  838. int DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
  839. word32 inSz)
  840. {
  841. int length;
  842. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  843. return ASN_PARSE_E;
  844. if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
  845. GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
  846. GetInt(&key->g, input, inOutIdx, inSz) < 0 ||
  847. GetInt(&key->y, input, inOutIdx, inSz) < 0 ) return ASN_DH_KEY_E;
  848. key->type = DSA_PUBLIC;
  849. return 0;
  850. }
  851. int DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
  852. word32 inSz)
  853. {
  854. int length, version;
  855. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  856. return ASN_PARSE_E;
  857. if (GetMyVersion(input, inOutIdx, &version) < 0)
  858. return ASN_PARSE_E;
  859. if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
  860. GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
  861. GetInt(&key->g, input, inOutIdx, inSz) < 0 ||
  862. GetInt(&key->y, input, inOutIdx, inSz) < 0 ||
  863. GetInt(&key->x, input, inOutIdx, inSz) < 0 ) return ASN_DH_KEY_E;
  864. key->type = DSA_PRIVATE;
  865. return 0;
  866. }
  867. #endif /* NO_DSA */
  868. void InitDecodedCert(DecodedCert* cert, byte* source, word32 inSz, void* heap)
  869. {
  870. cert->publicKey = 0;
  871. cert->pubKeyStored = 0;
  872. cert->signature = 0;
  873. cert->subjectCN = 0;
  874. cert->subjectCNLen = 0;
  875. cert->subjectCNStored = 0;
  876. cert->altNames = NULL;
  877. cert->issuer[0] = '\0';
  878. cert->subject[0] = '\0';
  879. cert->source = source; /* don't own */
  880. cert->srcIdx = 0;
  881. cert->maxIdx = inSz; /* can't go over this index */
  882. cert->heap = heap;
  883. XMEMSET(cert->serial, 0, EXTERNAL_SERIAL_SIZE);
  884. cert->serialSz = 0;
  885. cert->extensions = 0;
  886. cert->extensionsSz = 0;
  887. cert->extensionsIdx = 0;
  888. cert->extAuthInfo = NULL;
  889. cert->extAuthInfoSz = 0;
  890. cert->extCrlInfo = NULL;
  891. cert->extCrlInfoSz = 0;
  892. cert->isCA = 0;
  893. #ifdef CYASSL_CERT_GEN
  894. cert->subjectSN = 0;
  895. cert->subjectSNLen = 0;
  896. cert->subjectC = 0;
  897. cert->subjectCLen = 0;
  898. cert->subjectL = 0;
  899. cert->subjectLLen = 0;
  900. cert->subjectST = 0;
  901. cert->subjectSTLen = 0;
  902. cert->subjectO = 0;
  903. cert->subjectOLen = 0;
  904. cert->subjectOU = 0;
  905. cert->subjectOULen = 0;
  906. cert->subjectEmail = 0;
  907. cert->subjectEmailLen = 0;
  908. cert->beforeDate = 0;
  909. cert->beforeDateLen = 0;
  910. cert->afterDate = 0;
  911. cert->afterDateLen = 0;
  912. #endif /* CYASSL_CERT_GEN */
  913. }
  914. void FreeAltNames(DNS_entry* altNames, void* heap)
  915. {
  916. (void)heap;
  917. while (altNames) {
  918. DNS_entry* tmp = altNames->next;
  919. XFREE(altNames->name, heap, DYNAMIC_TYPE_ALTNAME);
  920. XFREE(altNames, heap, DYNAMIC_TYPE_ALTNAME);
  921. altNames = tmp;
  922. }
  923. }
  924. void FreeDecodedCert(DecodedCert* cert)
  925. {
  926. if (cert->subjectCNStored == 1)
  927. XFREE(cert->subjectCN, cert->heap, DYNAMIC_TYPE_SUBJECT_CN);
  928. if (cert->pubKeyStored == 1)
  929. XFREE(cert->publicKey, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  930. if (cert->altNames)
  931. FreeAltNames(cert->altNames, cert->heap);
  932. }
  933. static int GetCertHeader(DecodedCert* cert)
  934. {
  935. int ret = 0, version, len;
  936. byte serialTmp[EXTERNAL_SERIAL_SIZE];
  937. mp_int mpi;
  938. if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0)
  939. return ASN_PARSE_E;
  940. cert->certBegin = cert->srcIdx;
  941. if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0)
  942. return ASN_PARSE_E;
  943. cert->sigIndex = len + cert->srcIdx;
  944. if (GetExplicitVersion(cert->source, &cert->srcIdx, &version) < 0)
  945. return ASN_PARSE_E;
  946. if (GetInt(&mpi, cert->source, &cert->srcIdx, cert->maxIdx) < 0)
  947. return ASN_PARSE_E;
  948. len = mp_unsigned_bin_size(&mpi);
  949. if (len < (int)sizeof(serialTmp)) {
  950. if (mp_to_unsigned_bin(&mpi, serialTmp) == MP_OKAY) {
  951. if (len > EXTERNAL_SERIAL_SIZE)
  952. len = EXTERNAL_SERIAL_SIZE;
  953. XMEMCPY(cert->serial, serialTmp, len);
  954. cert->serialSz = len;
  955. }
  956. }
  957. mp_clear(&mpi);
  958. return ret;
  959. }
  960. #if !defined(NO_RSA)
  961. /* Store Rsa Key, may save later, Dsa could use in future */
  962. static int StoreRsaKey(DecodedCert* cert)
  963. {
  964. int length;
  965. word32 read = cert->srcIdx;
  966. if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
  967. return ASN_PARSE_E;
  968. read = cert->srcIdx - read;
  969. length += read;
  970. while (read--)
  971. cert->srcIdx--;
  972. cert->pubKeySize = length;
  973. cert->publicKey = cert->source + cert->srcIdx;
  974. cert->srcIdx += length;
  975. return 0;
  976. }
  977. #endif
  978. #ifdef HAVE_ECC
  979. /* return 0 on sucess if the ECC curve oid sum is supported */
  980. static int CheckCurve(word32 oid)
  981. {
  982. if (oid != ECC_256R1 && oid != ECC_384R1 && oid != ECC_521R1 && oid !=
  983. ECC_160R1 && oid != ECC_192R1 && oid != ECC_224R1)
  984. return ALGO_ID_E;
  985. return 0;
  986. }
  987. #endif /* HAVE_ECC */
  988. static int GetKey(DecodedCert* cert)
  989. {
  990. int length;
  991. #ifdef HAVE_NTRU
  992. int tmpIdx = cert->srcIdx;
  993. #endif
  994. if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
  995. return ASN_PARSE_E;
  996. if (GetAlgoId(cert->source, &cert->srcIdx, &cert->keyOID, cert->maxIdx) < 0)
  997. return ASN_PARSE_E;
  998. switch (cert->keyOID) {
  999. case DSAk:
  1000. /* do nothing */
  1001. break;
  1002. #ifndef NO_RSA
  1003. case RSAk:
  1004. {
  1005. byte b = cert->source[cert->srcIdx++];
  1006. if (b != ASN_BIT_STRING)
  1007. return ASN_BITSTR_E;
  1008. if (GetLength(cert->source,&cert->srcIdx,&length,cert->maxIdx) < 0)
  1009. return ASN_PARSE_E;
  1010. b = cert->source[cert->srcIdx++];
  1011. if (b != 0x00)
  1012. return ASN_EXPECT_0_E;
  1013. return StoreRsaKey(cert);
  1014. }
  1015. break;
  1016. #endif /* NO_RSA */
  1017. #ifdef HAVE_NTRU
  1018. case NTRUk:
  1019. {
  1020. const byte* key = &cert->source[tmpIdx];
  1021. byte* next = (byte*)key;
  1022. word16 keyLen;
  1023. byte keyBlob[MAX_NTRU_KEY_SZ];
  1024. word32 rc = crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key,
  1025. &keyLen, NULL, &next);
  1026. if (rc != NTRU_OK)
  1027. return ASN_NTRU_KEY_E;
  1028. if (keyLen > sizeof(keyBlob))
  1029. return ASN_NTRU_KEY_E;
  1030. rc = crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key,&keyLen,
  1031. keyBlob, &next);
  1032. if (rc != NTRU_OK)
  1033. return ASN_NTRU_KEY_E;
  1034. if ( (next - key) < 0)
  1035. return ASN_NTRU_KEY_E;
  1036. cert->srcIdx = tmpIdx + (int)(next - key);
  1037. cert->publicKey = (byte*) XMALLOC(keyLen, cert->heap,
  1038. DYNAMIC_TYPE_PUBLIC_KEY);
  1039. if (cert->publicKey == NULL)
  1040. return MEMORY_E;
  1041. XMEMCPY(cert->publicKey, keyBlob, keyLen);
  1042. cert->pubKeyStored = 1;
  1043. cert->pubKeySize = keyLen;
  1044. }
  1045. break;
  1046. #endif /* HAVE_NTRU */
  1047. #ifdef HAVE_ECC
  1048. case ECDSAk:
  1049. {
  1050. word32 oid = 0;
  1051. int oidSz = 0;
  1052. byte b = cert->source[cert->srcIdx++];
  1053. if (b != ASN_OBJECT_ID)
  1054. return ASN_OBJECT_ID_E;
  1055. if (GetLength(cert->source,&cert->srcIdx,&oidSz,cert->maxIdx) < 0)
  1056. return ASN_PARSE_E;
  1057. while(oidSz--)
  1058. oid += cert->source[cert->srcIdx++];
  1059. if (CheckCurve(oid) < 0)
  1060. return ECC_CURVE_OID_E;
  1061. /* key header */
  1062. b = cert->source[cert->srcIdx++];
  1063. if (b != ASN_BIT_STRING)
  1064. return ASN_BITSTR_E;
  1065. if (GetLength(cert->source,&cert->srcIdx,&length,cert->maxIdx) < 0)
  1066. return ASN_PARSE_E;
  1067. b = cert->source[cert->srcIdx++];
  1068. if (b != 0x00)
  1069. return ASN_EXPECT_0_E;
  1070. /* actual key, use length - 1 since ate preceding 0 */
  1071. length -= 1;
  1072. cert->publicKey = (byte*) XMALLOC(length, cert->heap,
  1073. DYNAMIC_TYPE_PUBLIC_KEY);
  1074. if (cert->publicKey == NULL)
  1075. return MEMORY_E;
  1076. XMEMCPY(cert->publicKey, &cert->source[cert->srcIdx], length);
  1077. cert->pubKeyStored = 1;
  1078. cert->pubKeySize = length;
  1079. cert->srcIdx += length;
  1080. }
  1081. break;
  1082. #endif /* HAVE_ECC */
  1083. default:
  1084. return ASN_UNKNOWN_OID_E;
  1085. }
  1086. return 0;
  1087. }
  1088. /* process NAME, either issuer or subject */
  1089. static int GetName(DecodedCert* cert, int nameType)
  1090. {
  1091. Sha sha;
  1092. int length; /* length of all distinguished names */
  1093. int dummy;
  1094. char* full = (nameType == ISSUER) ? cert->issuer : cert->subject;
  1095. word32 idx;
  1096. CYASSL_MSG("Getting Cert Name");
  1097. if (cert->source[cert->srcIdx] == ASN_OBJECT_ID) {
  1098. CYASSL_MSG("Trying optional prefix...");
  1099. if (GetLength(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
  1100. return ASN_PARSE_E;
  1101. cert->srcIdx += length;
  1102. CYASSL_MSG("Got optional prefix");
  1103. }
  1104. /* For OCSP, RFC2560 section 4.1.1 states the issuer hash should be
  1105. * calculated over the entire DER encoding of the Name field, including
  1106. * the tag and length. */
  1107. idx = cert->srcIdx;
  1108. if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
  1109. return ASN_PARSE_E;
  1110. InitSha(&sha);
  1111. ShaUpdate(&sha, &cert->source[idx], length + cert->srcIdx - idx);
  1112. if (nameType == ISSUER)
  1113. ShaFinal(&sha, cert->issuerHash);
  1114. else
  1115. ShaFinal(&sha, cert->subjectHash);
  1116. length += cert->srcIdx;
  1117. idx = 0;
  1118. while (cert->srcIdx < (word32)length) {
  1119. byte b;
  1120. byte joint[2];
  1121. byte tooBig = FALSE;
  1122. int oidSz;
  1123. if (GetSet(cert->source, &cert->srcIdx, &dummy, cert->maxIdx) < 0) {
  1124. CYASSL_MSG("Cert name lacks set header, trying sequence");
  1125. }
  1126. if (GetSequence(cert->source, &cert->srcIdx, &dummy, cert->maxIdx) < 0)
  1127. return ASN_PARSE_E;
  1128. b = cert->source[cert->srcIdx++];
  1129. if (b != ASN_OBJECT_ID)
  1130. return ASN_OBJECT_ID_E;
  1131. if (GetLength(cert->source, &cert->srcIdx, &oidSz, cert->maxIdx) < 0)
  1132. return ASN_PARSE_E;
  1133. XMEMCPY(joint, &cert->source[cert->srcIdx], sizeof(joint));
  1134. /* v1 name types */
  1135. if (joint[0] == 0x55 && joint[1] == 0x04) {
  1136. byte id;
  1137. byte copy = FALSE;
  1138. int strLen;
  1139. cert->srcIdx += 2;
  1140. id = cert->source[cert->srcIdx++];
  1141. b = cert->source[cert->srcIdx++]; /* strType */
  1142. (void)b; /* may want to validate? */
  1143. if (GetLength(cert->source, &cert->srcIdx, &strLen,
  1144. cert->maxIdx) < 0)
  1145. return ASN_PARSE_E;
  1146. if ( (strLen + 4) > (int)(ASN_NAME_MAX - idx)) {
  1147. /* include biggest pre fix header too 4 = "/CN=" */
  1148. CYASSL_MSG("ASN Name too big, skipping");
  1149. tooBig = TRUE;
  1150. }
  1151. if (id == ASN_COMMON_NAME) {
  1152. if (nameType == SUBJECT) {
  1153. cert->subjectCN = (char *)&cert->source[cert->srcIdx];
  1154. cert->subjectCNLen = strLen;
  1155. }
  1156. if (!tooBig) {
  1157. XMEMCPY(&full[idx], "/CN=", 4);
  1158. idx += 4;
  1159. copy = TRUE;
  1160. }
  1161. }
  1162. else if (id == ASN_SUR_NAME) {
  1163. if (!tooBig) {
  1164. XMEMCPY(&full[idx], "/SN=", 4);
  1165. idx += 4;
  1166. copy = TRUE;
  1167. }
  1168. #ifdef CYASSL_CERT_GEN
  1169. if (nameType == SUBJECT) {
  1170. cert->subjectSN = (char*)&cert->source[cert->srcIdx];
  1171. cert->subjectSNLen = strLen;
  1172. }
  1173. #endif /* CYASSL_CERT_GEN */
  1174. }
  1175. else if (id == ASN_COUNTRY_NAME) {
  1176. if (!tooBig) {
  1177. XMEMCPY(&full[idx], "/C=", 3);
  1178. idx += 3;
  1179. copy = TRUE;
  1180. }
  1181. #ifdef CYASSL_CERT_GEN
  1182. if (nameType == SUBJECT) {
  1183. cert->subjectC = (char*)&cert->source[cert->srcIdx];
  1184. cert->subjectCLen = strLen;
  1185. }
  1186. #endif /* CYASSL_CERT_GEN */
  1187. }
  1188. else if (id == ASN_LOCALITY_NAME) {
  1189. if (!tooBig) {
  1190. XMEMCPY(&full[idx], "/L=", 3);
  1191. idx += 3;
  1192. copy = TRUE;
  1193. }
  1194. #ifdef CYASSL_CERT_GEN
  1195. if (nameType == SUBJECT) {
  1196. cert->subjectL = (char*)&cert->source[cert->srcIdx];
  1197. cert->subjectLLen = strLen;
  1198. }
  1199. #endif /* CYASSL_CERT_GEN */
  1200. }
  1201. else if (id == ASN_STATE_NAME) {
  1202. if (!tooBig) {
  1203. XMEMCPY(&full[idx], "/ST=", 4);
  1204. idx += 4;
  1205. copy = TRUE;
  1206. }
  1207. #ifdef CYASSL_CERT_GEN
  1208. if (nameType == SUBJECT) {
  1209. cert->subjectST = (char*)&cert->source[cert->srcIdx];
  1210. cert->subjectSTLen = strLen;
  1211. }
  1212. #endif /* CYASSL_CERT_GEN */
  1213. }
  1214. else if (id == ASN_ORG_NAME) {
  1215. if (!tooBig) {
  1216. XMEMCPY(&full[idx], "/O=", 3);
  1217. idx += 3;
  1218. copy = TRUE;
  1219. }
  1220. #ifdef CYASSL_CERT_GEN
  1221. if (nameType == SUBJECT) {
  1222. cert->subjectO = (char*)&cert->source[cert->srcIdx];
  1223. cert->subjectOLen = strLen;
  1224. }
  1225. #endif /* CYASSL_CERT_GEN */
  1226. }
  1227. else if (id == ASN_ORGUNIT_NAME) {
  1228. if (!tooBig) {
  1229. XMEMCPY(&full[idx], "/OU=", 4);
  1230. idx += 4;
  1231. copy = TRUE;
  1232. }
  1233. #ifdef CYASSL_CERT_GEN
  1234. if (nameType == SUBJECT) {
  1235. cert->subjectOU = (char*)&cert->source[cert->srcIdx];
  1236. cert->subjectOULen = strLen;
  1237. }
  1238. #endif /* CYASSL_CERT_GEN */
  1239. }
  1240. if (copy && !tooBig) {
  1241. XMEMCPY(&full[idx], &cert->source[cert->srcIdx], strLen);
  1242. idx += strLen;
  1243. }
  1244. cert->srcIdx += strLen;
  1245. }
  1246. else {
  1247. /* skip */
  1248. byte email = FALSE;
  1249. byte uid = FALSE;
  1250. int adv;
  1251. if (joint[0] == 0x2a && joint[1] == 0x86) /* email id hdr */
  1252. email = TRUE;
  1253. if (joint[0] == 0x9 && joint[1] == 0x92) /* uid id hdr */
  1254. uid = TRUE;
  1255. cert->srcIdx += oidSz + 1;
  1256. if (GetLength(cert->source, &cert->srcIdx, &adv, cert->maxIdx) < 0)
  1257. return ASN_PARSE_E;
  1258. if (adv > (int)(ASN_NAME_MAX - idx)) {
  1259. CYASSL_MSG("ASN name too big, skipping");
  1260. tooBig = TRUE;
  1261. }
  1262. if (email) {
  1263. if (14 > (ASN_NAME_MAX - idx)) {
  1264. CYASSL_MSG("ASN name too big, skipping");
  1265. tooBig = TRUE;
  1266. }
  1267. if (!tooBig) {
  1268. XMEMCPY(&full[idx], "/emailAddress=", 14);
  1269. idx += 14;
  1270. }
  1271. #ifdef CYASSL_CERT_GEN
  1272. if (nameType == SUBJECT) {
  1273. cert->subjectEmail = (char*)&cert->source[cert->srcIdx];
  1274. cert->subjectEmailLen = adv;
  1275. }
  1276. #endif /* CYASSL_CERT_GEN */
  1277. if (!tooBig) {
  1278. XMEMCPY(&full[idx], &cert->source[cert->srcIdx], adv);
  1279. idx += adv;
  1280. }
  1281. }
  1282. if (uid) {
  1283. if (5 > (ASN_NAME_MAX - idx)) {
  1284. CYASSL_MSG("ASN name too big, skipping");
  1285. tooBig = TRUE;
  1286. }
  1287. if (!tooBig) {
  1288. XMEMCPY(&full[idx], "/UID=", 5);
  1289. idx += 5;
  1290. XMEMCPY(&full[idx], &cert->source[cert->srcIdx], adv);
  1291. idx += adv;
  1292. }
  1293. }
  1294. cert->srcIdx += adv;
  1295. }
  1296. }
  1297. full[idx++] = 0;
  1298. return 0;
  1299. }
  1300. #ifndef NO_TIME_H
  1301. /* to the second */
  1302. static int DateGreaterThan(const struct tm* a, const struct tm* b)
  1303. {
  1304. if (a->tm_year > b->tm_year)
  1305. return 1;
  1306. if (a->tm_year == b->tm_year && a->tm_mon > b->tm_mon)
  1307. return 1;
  1308. if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
  1309. a->tm_mday > b->tm_mday)
  1310. return 1;
  1311. if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
  1312. a->tm_mday == b->tm_mday && a->tm_hour > b->tm_hour)
  1313. return 1;
  1314. if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
  1315. a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour &&
  1316. a->tm_min > b->tm_min)
  1317. return 1;
  1318. if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
  1319. a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour &&
  1320. a->tm_min == b->tm_min && a->tm_sec > b->tm_sec)
  1321. return 1;
  1322. return 0; /* false */
  1323. }
  1324. static INLINE int DateLessThan(const struct tm* a, const struct tm* b)
  1325. {
  1326. return !DateGreaterThan(a,b);
  1327. }
  1328. /* like atoi but only use first byte */
  1329. /* Make sure before and after dates are valid */
  1330. int ValidateDate(const byte* date, byte format, int dateType)
  1331. {
  1332. time_t ltime;
  1333. struct tm certTime;
  1334. struct tm* localTime;
  1335. int i = 0;
  1336. ltime = XTIME(0);
  1337. XMEMSET(&certTime, 0, sizeof(certTime));
  1338. if (format == ASN_UTC_TIME) {
  1339. if (btoi(date[0]) >= 5)
  1340. certTime.tm_year = 1900;
  1341. else
  1342. certTime.tm_year = 2000;
  1343. }
  1344. else { /* format == GENERALIZED_TIME */
  1345. certTime.tm_year += btoi(date[i++]) * 1000;
  1346. certTime.tm_year += btoi(date[i++]) * 100;
  1347. }
  1348. GetTime(&certTime.tm_year, date, &i); certTime.tm_year -= 1900; /* adjust */
  1349. GetTime(&certTime.tm_mon, date, &i); certTime.tm_mon -= 1; /* adjust */
  1350. GetTime(&certTime.tm_mday, date, &i);
  1351. GetTime(&certTime.tm_hour, date, &i);
  1352. GetTime(&certTime.tm_min, date, &i);
  1353. GetTime(&certTime.tm_sec, date, &i);
  1354. if (date[i] != 'Z') { /* only Zulu supported for this profile */
  1355. CYASSL_MSG("Only Zulu time supported for this profile");
  1356. return 0;
  1357. }
  1358. localTime = XGMTIME(&ltime);
  1359. if (dateType == BEFORE) {
  1360. if (DateLessThan(localTime, &certTime))
  1361. return 0;
  1362. }
  1363. else
  1364. if (DateGreaterThan(localTime, &certTime))
  1365. return 0;
  1366. return 1;
  1367. }
  1368. #endif /* NO_TIME_H */
  1369. static int GetDate(DecodedCert* cert, int dateType)
  1370. {
  1371. int length;
  1372. byte date[MAX_DATE_SIZE];
  1373. byte b;
  1374. #ifdef CYASSL_CERT_GEN
  1375. word32 startIdx = 0;
  1376. if (dateType == BEFORE)
  1377. cert->beforeDate = &cert->source[cert->srcIdx];
  1378. else
  1379. cert->afterDate = &cert->source[cert->srcIdx];
  1380. startIdx = cert->srcIdx;
  1381. #endif
  1382. b = cert->source[cert->srcIdx++];
  1383. if (b != ASN_UTC_TIME && b != ASN_GENERALIZED_TIME)
  1384. return ASN_TIME_E;
  1385. if (GetLength(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
  1386. return ASN_PARSE_E;
  1387. if (length > MAX_DATE_SIZE || length < MIN_DATE_SIZE)
  1388. return ASN_DATE_SZ_E;
  1389. XMEMCPY(date, &cert->source[cert->srcIdx], length);
  1390. cert->srcIdx += length;
  1391. #ifdef CYASSL_CERT_GEN
  1392. if (dateType == BEFORE)
  1393. cert->beforeDateLen = cert->srcIdx - startIdx;
  1394. else
  1395. cert->afterDateLen = cert->srcIdx - startIdx;
  1396. #endif
  1397. if (!XVALIDATE_DATE(date, b, dateType)) {
  1398. if (dateType == BEFORE)
  1399. return ASN_BEFORE_DATE_E;
  1400. else
  1401. return ASN_AFTER_DATE_E;
  1402. }
  1403. return 0;
  1404. }
  1405. static int GetValidity(DecodedCert* cert, int verify)
  1406. {
  1407. int length;
  1408. int badDate = 0;
  1409. if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
  1410. return ASN_PARSE_E;
  1411. if (GetDate(cert, BEFORE) < 0 && verify)
  1412. badDate = ASN_BEFORE_DATE_E; /* continue parsing */
  1413. if (GetDate(cert, AFTER) < 0 && verify)
  1414. return ASN_AFTER_DATE_E;
  1415. if (badDate != 0)
  1416. return badDate;
  1417. return 0;
  1418. }
  1419. int DecodeToKey(DecodedCert* cert, int verify)
  1420. {
  1421. int badDate = 0;
  1422. int ret;
  1423. if ( (ret = GetCertHeader(cert)) < 0)
  1424. return ret;
  1425. if ( (ret = GetAlgoId(cert->source, &cert->srcIdx, &cert->signatureOID,
  1426. cert->maxIdx)) < 0)
  1427. return ret;
  1428. if ( (ret = GetName(cert, ISSUER)) < 0)
  1429. return ret;
  1430. if ( (ret = GetValidity(cert, verify)) < 0)
  1431. badDate = ret;
  1432. if ( (ret = GetName(cert, SUBJECT)) < 0)
  1433. return ret;
  1434. if ( (ret = GetKey(cert)) < 0)
  1435. return ret;
  1436. if (badDate != 0)
  1437. return badDate;
  1438. return ret;
  1439. }
  1440. static int GetSignature(DecodedCert* cert)
  1441. {
  1442. int length;
  1443. byte b = cert->source[cert->srcIdx++];
  1444. if (b != ASN_BIT_STRING)
  1445. return ASN_BITSTR_E;
  1446. if (GetLength(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
  1447. return ASN_PARSE_E;
  1448. cert->sigLength = length;
  1449. b = cert->source[cert->srcIdx++];
  1450. if (b != 0x00)
  1451. return ASN_EXPECT_0_E;
  1452. cert->sigLength--;
  1453. cert->signature = &cert->source[cert->srcIdx];
  1454. cert->srcIdx += cert->sigLength;
  1455. return 0;
  1456. }
  1457. static word32 SetDigest(const byte* digest, word32 digSz, byte* output)
  1458. {
  1459. output[0] = ASN_OCTET_STRING;
  1460. output[1] = (byte)digSz;
  1461. XMEMCPY(&output[2], digest, digSz);
  1462. return digSz + 2;
  1463. }
  1464. static word32 BytePrecision(word32 value)
  1465. {
  1466. word32 i;
  1467. for (i = sizeof(value); i; --i)
  1468. if (value >> ((i - 1) * BIT_SIZE))
  1469. break;
  1470. return i;
  1471. }
  1472. static word32 SetLength(word32 length, byte* output)
  1473. {
  1474. word32 i = 0, j;
  1475. if (length < ASN_LONG_LENGTH)
  1476. output[i++] = (byte)length;
  1477. else {
  1478. output[i++] = (byte)(BytePrecision(length) | ASN_LONG_LENGTH);
  1479. for (j = BytePrecision(length); j; --j) {
  1480. output[i] = (byte)(length >> ((j - 1) * BIT_SIZE));
  1481. i++;
  1482. }
  1483. }
  1484. return i;
  1485. }
  1486. static word32 SetSequence(word32 len, byte* output)
  1487. {
  1488. output[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
  1489. return SetLength(len, output + 1) + 1;
  1490. }
  1491. static word32 SetAlgoID(int algoOID, byte* output, int type)
  1492. {
  1493. /* adding TAG_NULL and 0 to end */
  1494. /* hashTypes */
  1495. static const byte shaAlgoID[] = { 0x2b, 0x0e, 0x03, 0x02, 0x1a,
  1496. 0x05, 0x00 };
  1497. static const byte sha256AlgoID[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  1498. 0x04, 0x02, 0x01, 0x05, 0x00 };
  1499. static const byte sha384AlgoID[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  1500. 0x04, 0x02, 0x02,

Large files files are truncated, but you can click here to view the full file