Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1960 lines
61 KiB

  1. /* Copyright (C) Boris Nikolaus, Germany, 1996-1997. All rights reserved. */
  2. /* Copyright (C) Microsoft Corporation, 1997-1998. All rights reserved. */
  3. #include "precomp.h"
  4. #ifdef ENABLE_BER
  5. #include <math.h>
  6. static const char bitmsk2[] =
  7. {
  8. (const char) 0x00,
  9. (const char) 0x80,
  10. (const char) 0xc0,
  11. (const char) 0xe0,
  12. (const char) 0xf0,
  13. (const char) 0xf8,
  14. (const char) 0xfc,
  15. (const char) 0xfe
  16. };
  17. /* decode bit string value */
  18. int _BERDecBitString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1bitstring_t *val, ASN1uint32_t fNoCopy)
  19. {
  20. ASN1uint32_t constructed, len, infinite;
  21. ASN1bitstring_t b;
  22. ASN1decoding_t dd;
  23. ASN1octet_t *di;
  24. /* skip tag */
  25. if (ASN1BERDecTag(dec, tag, &constructed))
  26. {
  27. /* get length */
  28. if (ASN1BERDecLength(dec, &len, &infinite))
  29. {
  30. if (constructed)
  31. {
  32. /* constructed? then start decoding of constructed value */
  33. val->length = 0;
  34. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  35. {
  36. while (ASN1BERDecNotEndOfContents(dd, di))
  37. {
  38. if (_BERDecBitString(dd, 0x3, &b, fNoCopy))
  39. {
  40. if (b.length)
  41. {
  42. if (fNoCopy)
  43. {
  44. *val = b;
  45. break; // break out the loop because nocopy cannot have multiple constructed streams
  46. }
  47. /* resize value */
  48. val->value = (ASN1octet_t *)DecMemReAlloc(dd, val->value,
  49. (val->length + b.length + 7) / 8);
  50. if (val->value)
  51. {
  52. /* concat bit strings */
  53. ASN1bitcpy(val->value, val->length, b.value, 0, b.length);
  54. val->length += b.length;
  55. if (val->length & 7)
  56. val->value[val->length / 8] &= bitmsk2[val->length & 7];
  57. /* free unused bit string */
  58. DecMemFree(dec, b.value);
  59. }
  60. else
  61. {
  62. return 0;
  63. }
  64. }
  65. }
  66. } // while
  67. return ASN1BERDecEndOfContents(dec, dd, di);
  68. }
  69. }
  70. else
  71. {
  72. /* primitive? then copy value */
  73. if (!len)
  74. {
  75. val->length = 0;
  76. val->value = NULL;
  77. return 1;
  78. }
  79. else
  80. {
  81. if (*dec->pos < 8)
  82. {
  83. len--; // skip over the initial octet; len is now the actual length of octets
  84. val->length = len * 8 - *dec->pos++;
  85. if (fNoCopy)
  86. {
  87. val->value = dec->pos;
  88. dec->pos += len;
  89. return 1;
  90. }
  91. else
  92. {
  93. if (val->length)
  94. {
  95. val->value = (ASN1octet_t *)DecMemAlloc(dec, (val->length + 7) / 8);
  96. if (val->value)
  97. {
  98. CopyMemory(val->value, dec->pos, len);
  99. if (val->length & 7)
  100. val->value[len - 1] &= bitmsk2[val->length & 7];
  101. dec->pos += len;
  102. return 1;
  103. }
  104. }
  105. else
  106. {
  107. val->value = NULL;
  108. return 1;
  109. }
  110. }
  111. }
  112. else
  113. {
  114. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. return 0;
  121. }
  122. /* decode bit string value, making copy */
  123. int ASN1BERDecBitString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1bitstring_t *val)
  124. {
  125. return _BERDecBitString(dec, tag, val, FALSE);
  126. }
  127. /* decode bit string value, no copy */
  128. int ASN1BERDecBitString2(ASN1decoding_t dec, ASN1uint32_t tag, ASN1bitstring_t *val)
  129. {
  130. return _BERDecBitString(dec, tag, val, TRUE);
  131. }
  132. /* decode string value */
  133. int ASN1BERDecCharString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1charstring_t *val)
  134. {
  135. ASN1uint32_t constructed, len, infinite;
  136. ASN1charstring_t c;
  137. ASN1decoding_t dd;
  138. ASN1octet_t *di;
  139. /* skip tag */
  140. if (ASN1BERDecTag(dec, tag, &constructed))
  141. {
  142. /* get length */
  143. if (ASN1BERDecLength(dec, &len, &infinite))
  144. {
  145. if (constructed)
  146. {
  147. /* constructed? then start decoding of constructed value */
  148. val->length = 0;
  149. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  150. {
  151. while (ASN1BERDecNotEndOfContents(dd, di))
  152. {
  153. if (ASN1BERDecCharString(dd, 0x4, &c))
  154. {
  155. if (c.length)
  156. {
  157. /* resize value */
  158. val->value = (char *)DecMemReAlloc(dd, val->value,
  159. val->length + c.length);
  160. if (val->value)
  161. {
  162. /* concat strings */
  163. CopyMemory(val->value + val->length, c.value, c.length);
  164. val->length += c.length;
  165. /* free unused string */
  166. DecMemFree(dec, c.value);
  167. }
  168. else
  169. {
  170. return 0;
  171. }
  172. }
  173. }
  174. else
  175. {
  176. return 0;
  177. }
  178. } // while
  179. return ASN1BERDecEndOfContents(dec, dd, di);
  180. }
  181. }
  182. else
  183. {
  184. /* primitive? then copy value */
  185. val->length = len;
  186. if (len)
  187. {
  188. val->value = (char *)DecMemAlloc(dec, len+1);
  189. if (val->value)
  190. {
  191. CopyMemory(val->value, dec->pos, len);
  192. dec->pos += len;
  193. val->value[len] = 0;
  194. return 1;
  195. }
  196. }
  197. else
  198. {
  199. val->value = NULL;
  200. return 1;
  201. }
  202. }
  203. }
  204. }
  205. return 0;
  206. }
  207. /* decode 16 bit string value */
  208. int ASN1BERDecChar16String(ASN1decoding_t dec, ASN1uint32_t tag, ASN1char16string_t *val)
  209. {
  210. ASN1uint32_t constructed, len, infinite;
  211. ASN1char16string_t c;
  212. ASN1decoding_t dd;
  213. ASN1octet_t *di;
  214. ASN1uint32_t i;
  215. /* skip tag */
  216. if (ASN1BERDecTag(dec, tag, &constructed))
  217. {
  218. /* get length */
  219. if (ASN1BERDecLength(dec, &len, &infinite))
  220. {
  221. if (constructed)
  222. {
  223. /* constructed? then start decoding of constructed value */
  224. val->length = 0;
  225. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  226. {
  227. while (ASN1BERDecNotEndOfContents(dd, di))
  228. {
  229. if (ASN1BERDecChar16String(dd, 0x4, &c))
  230. {
  231. if (c.length)
  232. {
  233. /* resize value */
  234. val->value = (ASN1char16_t *)DecMemReAlloc(dd, val->value,
  235. (val->length + c.length) * sizeof(ASN1char16_t));
  236. if (val->value)
  237. {
  238. /* concat strings */
  239. CopyMemory(val->value + val->length, c.value,
  240. c.length * sizeof(ASN1char16_t));
  241. val->length += c.length;
  242. /* free unused string */
  243. DecMemFree(dec, c.value);
  244. }
  245. else
  246. {
  247. return 0;
  248. }
  249. }
  250. }
  251. else
  252. {
  253. return 0;
  254. }
  255. }
  256. return ASN1BERDecEndOfContents(dec, dd, di);
  257. }
  258. }
  259. else
  260. {
  261. /* primitive? then copy value */
  262. DecAssert(dec, 2 * sizeof(ASN1octet_t) == sizeof(ASN1char16_t));
  263. len = len >> 1; // divided by 2
  264. val->length = len;
  265. if (len)
  266. {
  267. val->value = (ASN1char16_t *)DecMemAlloc(dec, (len+1) * sizeof(ASN1char16_t));
  268. if (val->value)
  269. {
  270. for (i = 0; i < len; i++)
  271. {
  272. val->value[i] = (*dec->pos << 8) | dec->pos[1];
  273. dec->pos += 2;
  274. }
  275. val->value[len] = 0;
  276. return 1;
  277. }
  278. }
  279. else
  280. {
  281. val->value = NULL;
  282. return 1;
  283. }
  284. }
  285. }
  286. }
  287. return 0;
  288. }
  289. /* decode 32 bit string value */
  290. int ASN1BERDecChar32String(ASN1decoding_t dec, ASN1uint32_t tag, ASN1char32string_t *val)
  291. {
  292. ASN1uint32_t constructed, len, infinite;
  293. ASN1char32string_t c;
  294. ASN1decoding_t dd;
  295. ASN1octet_t *di;
  296. ASN1uint32_t i;
  297. /* skip tag */
  298. if (ASN1BERDecTag(dec, tag, &constructed))
  299. {
  300. /* get length */
  301. if (ASN1BERDecLength(dec, &len, &infinite))
  302. {
  303. if (constructed)
  304. {
  305. /* constructed? then start decoding of constructed value */
  306. val->length = 0;
  307. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  308. {
  309. while (ASN1BERDecNotEndOfContents(dd, di))
  310. {
  311. if (ASN1BERDecChar32String(dd, 0x4, &c))
  312. {
  313. if (c.length)
  314. {
  315. /* resize value */
  316. val->value = (ASN1char32_t *)DecMemReAlloc(dd, val->value,
  317. (val->length + c.length) * sizeof(ASN1char32_t));
  318. if (val->value)
  319. {
  320. /* concat strings */
  321. CopyMemory(val->value + val->length, c.value,
  322. c.length * sizeof(ASN1char32_t));
  323. val->length += c.length;
  324. /* free unused string */
  325. DecMemFree(dec, c.value);
  326. }
  327. else
  328. {
  329. return 0;
  330. }
  331. }
  332. }
  333. else
  334. {
  335. return 0;
  336. }
  337. }
  338. return ASN1BERDecEndOfContents(dec, dd, di);
  339. }
  340. }
  341. else
  342. {
  343. /* primitive? then copy value */
  344. DecAssert(dec, 4 * sizeof(ASN1octet_t) == sizeof(ASN1char32_t));
  345. len = len >> 2; // divided by 4
  346. val->length = len;
  347. if (len)
  348. {
  349. val->value = (ASN1char32_t *)DecMemAlloc(dec, (len+1) * sizeof(ASN1char32_t));
  350. if (val->value)
  351. {
  352. for (i = 0; i < len; i++)
  353. {
  354. val->value[i] = (*dec->pos << 24) | (dec->pos[1] << 16) |
  355. (dec->pos[2] << 8) | dec->pos[3];;
  356. dec->pos += 4;
  357. }
  358. val->value[len] = 0;
  359. return 1;
  360. }
  361. }
  362. else
  363. {
  364. val->value = NULL;
  365. return 1;
  366. }
  367. }
  368. }
  369. }
  370. return 0;
  371. }
  372. #ifdef ENABLE_GENERALIZED_CHAR_STR
  373. /* decode character string value */
  374. int ASN1BERDecCharacterString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1characterstring_t *val)
  375. {
  376. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t) dec;
  377. ASN1uint32_t constructed, len, infinite;
  378. ASN1uint32_t index;
  379. ASN1characterstring_identification_t *identification;
  380. ASN1decoding_t dd, dd2, dd3;
  381. ASN1octet_t *di, *di2, *di3;
  382. /* skip tag */
  383. if (!ASN1BERDecTag(dec, tag, &constructed))
  384. return 0;
  385. if (constructed)
  386. {
  387. /* constructed? CS-A encoded: */
  388. /* get length */
  389. if (!ASN1BERDecLength(dec, &len, &infinite))
  390. return 0;
  391. /* start decoding of constructed value */
  392. if (! _BERDecConstructed(dec, len, infinite, &dd, &di))
  393. return 0;
  394. if (!ASN1BERDecU32Val(dd, 0x80000000, &index))
  395. return 0;
  396. if (index != d->parent->csilength)
  397. {
  398. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  399. return 0;
  400. }
  401. if (!ASN1BERDecExplicitTag(dd, 0x80000001, &dd2, &di2))
  402. return 0;
  403. if (!ASN1BERDecPeekTag(dd2, &tag))
  404. return 0;
  405. switch (tag)
  406. {
  407. case 0x80000000:
  408. val->identification.o =
  409. ASN1characterstring_identification_syntaxes_o;
  410. if (!ASN1BERDecExplicitTag(dd2, 0x80000000, &dd3, &di3))
  411. return 0;
  412. if (!ASN1BERDecObjectIdentifier(dd3, 0x80000000,
  413. &val->identification.u.syntaxes.abstract))
  414. return 0;
  415. if (!ASN1BERDecObjectIdentifier(dd3, 0x80000001,
  416. &val->identification.u.syntaxes.transfer))
  417. return 0;
  418. if (!ASN1BERDecEndOfContents(dd2, dd3, di3))
  419. return 0;
  420. break;
  421. case 0x80000001:
  422. val->identification.o = ASN1characterstring_identification_syntax_o;
  423. if (!ASN1BERDecObjectIdentifier(dd2, 0x80000001,
  424. &val->identification.u.syntax))
  425. return 0;
  426. break;
  427. case 0x80000002:
  428. val->identification.o =
  429. ASN1characterstring_identification_presentation_context_id_o;
  430. if (!ASN1BERDecU32Val(dd2, 0x80000002,
  431. &val->identification.u.presentation_context_id))
  432. return 0;
  433. break;
  434. case 0x80000003:
  435. val->identification.o =
  436. ASN1characterstring_identification_context_negotiation_o;
  437. if (!ASN1BERDecExplicitTag(dd2, 0x80000003, &dd3, &di3))
  438. return 0;
  439. if (!ASN1BERDecU32Val(dd3, 0x80000000, &val->
  440. identification.u.context_negotiation.presentation_context_id))
  441. return 0;
  442. if (!ASN1BERDecObjectIdentifier(dd3, 0x80000001,
  443. &val->identification.u.context_negotiation.transfer_syntax))
  444. return 0;
  445. if (!ASN1BERDecEndOfContents(dd2, dd3, di3))
  446. return 0;
  447. break;
  448. case 0x80000004:
  449. val->identification.o =
  450. ASN1characterstring_identification_transfer_syntax_o;
  451. if (!ASN1BERDecObjectIdentifier(dd2, 0x80000004,
  452. &val->identification.u.transfer_syntax))
  453. return 0;
  454. break;
  455. case 0x80000005:
  456. val->identification.o = ASN1characterstring_identification_fixed_o;
  457. if (!ASN1BERDecNull(dd2, 0x80000005))
  458. return 0;
  459. break;
  460. default:
  461. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  462. return 0;
  463. }
  464. if (!ASN1BERDecEndOfContents(dd, dd2, di2))
  465. return 0;
  466. if (!ASN1DecAddCharacterStringIdentification(d->parent,
  467. &val->identification))
  468. return 0;
  469. val->data_value.o = ASN1characterstring_data_value_encoded_o;
  470. if (!ASN1BERDecOctetString(dd, 0x80000003,
  471. &val->data_value.u.encoded))
  472. return 0;
  473. if (!ASN1BERDecEndOfContents(dec, dd, di))
  474. return 0;
  475. }
  476. else
  477. {
  478. /* primitive? CS-B encoded */
  479. /* get length */
  480. if (!ASN1BERDecLength(dec, &len, NULL))
  481. return 0;
  482. /* then copy value */
  483. if (!len)
  484. {
  485. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  486. return 0;
  487. }
  488. val->data_value.o = ASN1characterstring_data_value_encoded_o;
  489. val->data_value.u.encoded.length = len - 1;
  490. val->data_value.u.encoded.value = (ASN1octet_t *)DecMemAlloc(dec, len - 1);
  491. if (!val->data_value.u.encoded.value)
  492. {
  493. return 0;
  494. }
  495. index = *dec->pos++;
  496. CopyMemory(val->data_value.u.encoded.value, dec->pos, len - 1);
  497. identification = ASN1DecGetCharacterStringIdentification(d->parent,
  498. index);
  499. if (!identification)
  500. return 0;
  501. val->identification.o = identification->o;
  502. switch (identification->o)
  503. {
  504. case ASN1characterstring_identification_syntaxes_o:
  505. if (!ASN1DecDupObjectIdentifier(dec,
  506. &val->identification.u.syntaxes.abstract,
  507. &identification->u.syntaxes.abstract))
  508. return 0;
  509. if (!ASN1DecDupObjectIdentifier(dec,
  510. &val->identification.u.syntaxes.transfer,
  511. &identification->u.syntaxes.transfer))
  512. return 0;
  513. break;
  514. case ASN1characterstring_identification_syntax_o:
  515. if (!ASN1DecDupObjectIdentifier(dec,
  516. &val->identification.u.syntax,
  517. &identification->u.syntax))
  518. return 0;
  519. break;
  520. case ASN1characterstring_identification_presentation_context_id_o:
  521. val->identification.u.presentation_context_id =
  522. identification->u.presentation_context_id;
  523. break;
  524. case ASN1characterstring_identification_context_negotiation_o:
  525. val->identification.u.context_negotiation.presentation_context_id =
  526. identification->u.context_negotiation.presentation_context_id;
  527. if (!ASN1DecDupObjectIdentifier(dec,
  528. &val->identification.u.context_negotiation.transfer_syntax,
  529. &identification->u.context_negotiation.transfer_syntax))
  530. return 0;
  531. break;
  532. case ASN1characterstring_identification_transfer_syntax_o:
  533. if (!ASN1DecDupObjectIdentifier(dec,
  534. &val->identification.u.transfer_syntax,
  535. &identification->u.transfer_syntax))
  536. return 0;
  537. break;
  538. case ASN1characterstring_identification_fixed_o:
  539. break;
  540. }
  541. }
  542. return 1;
  543. }
  544. #endif // ENABLE_GENERALIZED_CHAR_STR
  545. #ifdef ENABLE_DOUBLE
  546. /* decode real value */
  547. int ASN1BERDecDouble(ASN1decoding_t dec, ASN1uint32_t tag, double *val)
  548. {
  549. ASN1uint32_t head;
  550. ASN1int32_t exponent;
  551. ASN1uint32_t baselog2;
  552. ASN1uint32_t len;
  553. ASN1uint32_t i;
  554. ASN1octet_t *p, *q;
  555. double v;
  556. char buf[256], *b;
  557. /* skip tag */
  558. if (!ASN1BERDecTag(dec, tag, NULL))
  559. return 0;
  560. /* get length */
  561. if (!ASN1BERDecLength(dec, &len, NULL))
  562. return 0;
  563. /* null length is 0.0 */
  564. if (!len)
  565. {
  566. *val = 0.0;
  567. }
  568. else
  569. {
  570. p = q = dec->pos;
  571. dec->pos += len;
  572. head = *p++;
  573. /* binary encoding? */
  574. if (head & 0x80)
  575. {
  576. /* get base */
  577. switch (head & 0x30)
  578. {
  579. case 0:
  580. /* base 2 */
  581. baselog2 = 1;
  582. break;
  583. case 0x10:
  584. /* base 8 */
  585. baselog2 = 3;
  586. break;
  587. case 0x20:
  588. /* base 16 */
  589. baselog2 = 4;
  590. break;
  591. default:
  592. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  593. return 0;
  594. }
  595. /* get exponent */
  596. switch (head & 0x03)
  597. {
  598. case 0:
  599. /* 8 bit exponent */
  600. exponent = (ASN1int8_t)*p++;
  601. break;
  602. case 1:
  603. /* 16 bit exponent */
  604. exponent = (ASN1int16_t)((*p << 8) | p[1]);
  605. p += 2;
  606. break;
  607. case 2:
  608. /* 24 bit exponent */
  609. exponent = ((*p << 16) | (p[1] << 8) | p[2]);
  610. if (exponent & 0x800000)
  611. exponent -= 0x1000000;
  612. break;
  613. default:
  614. /* variable length exponent */
  615. exponent = (p[1] & 0x80) ? -1 : 0;
  616. for (i = 1; i <= *p; i++)
  617. exponent = (exponent << 8) | p[i];
  618. p += *p + 1;
  619. break;
  620. }
  621. /* calculate remaining length */
  622. len -= (ASN1uint32_t) (p - q);
  623. /* get mantissa */
  624. v = 0.0;
  625. for (i = 0; i < len; i++)
  626. v = v * 256.0 + *p++;
  627. /* scale mantissa */
  628. switch (head & 0x0c)
  629. {
  630. case 0x04:
  631. /* scaling factor 1 */
  632. v *= 2.0;
  633. break;
  634. case 0x08:
  635. /* scaling factor 2 */
  636. v *= 4.0;
  637. break;
  638. case 0x0c:
  639. /* scaling factor 3 */
  640. v *= 8.0;
  641. break;
  642. }
  643. /* check sign */
  644. if (head & 0x40)
  645. v = -v;
  646. /* calculate value */
  647. *val = ldexp(v, exponent * baselog2);
  648. }
  649. else
  650. /* special real values? */
  651. if (head & 0x40)
  652. {
  653. switch (head)
  654. {
  655. case 0x40:
  656. /* PLUS-INFINITY */
  657. *val = ASN1double_pinf();
  658. break;
  659. case 0x41:
  660. /* MINUS-INFINITY */
  661. *val = ASN1double_minf();
  662. break;
  663. default:
  664. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  665. return 0;
  666. }
  667. }
  668. /* decimal encoding */
  669. else
  670. {
  671. CopyMemory(buf, p, len - 1);
  672. buf[len - 1] = 0;
  673. b = strchr(buf, ',');
  674. if (b)
  675. *b = '.';
  676. *val = strtod((char *)buf, &b);
  677. if (*b)
  678. {
  679. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  680. return 0;
  681. }
  682. }
  683. }
  684. return 1;
  685. }
  686. #endif // ENABLE_DOUBLE
  687. #ifdef ENABLE_REAL
  688. int ASN1BERDecReal(ASN1decoding_t dec, ASN1uint32_t tag, ASN1real_t *val)
  689. {
  690. ASN1uint32_t head;
  691. ASN1int32_t ex;
  692. // ASN1intx_t exponent;
  693. ASN1uint32_t baselog2;
  694. ASN1uint32_t len;
  695. ASN1uint32_t i;
  696. ASN1octet_t *p, *q;
  697. double v;
  698. ASN1intx_t help;
  699. if (!ASN1BERDecTag(dec, tag, NULL))
  700. return 0;
  701. if (!ASN1BERDecLength(dec, &len, NULL))
  702. return 0;
  703. // *val = 0.0;
  704. DecAssert(dec, 0 == (int) eReal_Normal);
  705. ZeroMemory(val, sizeof(*val));
  706. if (len)
  707. {
  708. p = q = dec->pos;
  709. dec->pos += len;
  710. head = *p++;
  711. /* binary encoding? */
  712. if (head & 0x80)
  713. {
  714. val->type = eReal_Normal;
  715. /* get base */
  716. switch (head & 0x30)
  717. {
  718. case 0:
  719. /* base 2 */
  720. baselog2 = 1;
  721. break;
  722. case 0x10:
  723. /* base 8 */
  724. baselog2 = 3;
  725. break;
  726. case 0x20:
  727. /* base 16 */
  728. baselog2 = 4;
  729. break;
  730. default:
  731. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  732. return 0;
  733. }
  734. /* get exponent */
  735. switch (head & 0x03)
  736. {
  737. case 0:
  738. /* 8 bit exponent */
  739. ex = (ASN1int8_t)*p++;
  740. ASN1intx_setint32(&val->exponent, ex);
  741. break;
  742. case 1:
  743. /* 16 bit exponent */
  744. ex = (ASN1int16_t)((*p << 8) | p[1]);
  745. p += 2;
  746. // ASN1intx_setint32_t(&exponent, ex);
  747. ASN1intx_setint32(&val->exponent, ex);
  748. break;
  749. case 2:
  750. /* 24 bit exponent */
  751. ex = ((*p << 16) | (p[1] << 8) | p[2]);
  752. if (ex & 0x800000)
  753. ex -= 0x1000000;
  754. // ASN1intx_setint32_t(&exponent, ex);
  755. ASN1intx_setint32(&val->exponent, ex);
  756. break;
  757. default:
  758. /* variable length exponent */
  759. val->exponent.length = *p;
  760. val->exponent.value = (ASN1octet_t *)DecMemAlloc(dec, *p);
  761. if (!val->exponent.value)
  762. {
  763. return 0;
  764. }
  765. CopyMemory(val->exponent.value, p + 1, *p);
  766. p += *p + 1;
  767. break;
  768. }
  769. /* calculate remaining length */
  770. len -= (p - q);
  771. if (!len)
  772. {
  773. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  774. return 0;
  775. }
  776. /* get mantissa */
  777. val->mantissa.length = (*p & 0x80) ? len + 1 : len;
  778. val->mantissa.value = (ASN1octet_t *)DecMemAlloc(dec, val->mantissa.length);
  779. if (!val->mantissa.value)
  780. {
  781. return 0;
  782. }
  783. val->mantissa.value[0] = 0;
  784. CopyMemory(val->mantissa.value + val->mantissa.length - len, p, len);
  785. /* scale mantissa */
  786. switch (head & 0x0c)
  787. {
  788. case 0x04:
  789. /* scaling factor 1 */
  790. ASN1intx_muloctet(&help, &val->mantissa, 2);
  791. ASN1intx_free(&val->mantissa);
  792. val->mantissa = help;
  793. break;
  794. case 0x08:
  795. /* scaling factor 2 */
  796. ASN1intx_muloctet(&help, &val->mantissa, 4);
  797. ASN1intx_free(&val->mantissa);
  798. val->mantissa = help;
  799. break;
  800. case 0x0c:
  801. /* scaling factor 3 */
  802. ASN1intx_muloctet(&help, &val->mantissa, 8);
  803. ASN1intx_free(&val->mantissa);
  804. val->mantissa = help;
  805. break;
  806. }
  807. /* check sign */
  808. if (head & 0x40)
  809. {
  810. ASN1intx_neg(&help, &val->mantissa);
  811. ASN1intx_free(&val->mantissa);
  812. val->mantissa = help;
  813. }
  814. }
  815. else
  816. /* special real values? */
  817. if (head & 0x40)
  818. {
  819. switch (head)
  820. {
  821. case 0x40:
  822. /* PLUS-INFINITY */
  823. val->type = eReal_PlusInfinity;
  824. break;
  825. case 0x41:
  826. /* MINUS-INFINITY */
  827. val->type = eReal_MinusInfinity;
  828. break;
  829. default:
  830. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  831. return 0;
  832. }
  833. }
  834. /* decimal encoding */
  835. else
  836. {
  837. char *b;
  838. char buf[256];
  839. DecAssert(dec, (head & 0xc0) == 0xc0);
  840. CopyMemory(buf, p, len - 1);
  841. buf[len - 1] = 0;
  842. ex = 0;
  843. b = strchr(buf, ',');
  844. if (b)
  845. {
  846. // move the decimal point to the right
  847. ex -= lstrlenA(b+1);
  848. lstrcpyA(b, b+1);
  849. }
  850. // skip leading zeros
  851. for (b = &buf[0]; '0' == *b; b++)
  852. ;
  853. val->type = eReal_Normal;
  854. val->base = 10;
  855. ASN1intx_setint32(&val->exponent, ex);
  856. /*XXX*/
  857. // missing code here!!!
  858. // need to set val->mantissa through the decimal digits string
  859. DecAssert(dec, 0);
  860. return 0;
  861. }
  862. }
  863. return 1;
  864. }
  865. #endif // ENABLE_REAL
  866. #ifdef ENABLE_EMBEDDED_PDV
  867. /* decode embedded pdv value */
  868. int ASN1BERDecEmbeddedPdv(ASN1decoding_t dec, ASN1uint32_t tag, ASN1embeddedpdv_t *val)
  869. {
  870. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t) dec;
  871. ASN1uint32_t constructed, len, infinite;
  872. ASN1uint32_t index;
  873. ASN1embeddedpdv_identification_t *identification;
  874. ASN1decoding_t dd, dd2, dd3;
  875. ASN1octet_t *di, *di2, *di3;
  876. /* skip tag */
  877. if (!ASN1BERDecTag(dec, tag, &constructed))
  878. return 0;
  879. if (constructed)
  880. {
  881. /* constructed? EP-A encoded: */
  882. /* get length */
  883. if (!ASN1BERDecLength(dec, &len, &infinite))
  884. return 0;
  885. /* then start decoding of constructed value */
  886. if (! _BERDecConstructed(dec, len, infinite, &dd, &di))
  887. return 0;
  888. if (!ASN1BERDecU32Val(dd, 0x80000000, &index))
  889. return 0;
  890. if (index != d->parent->epilength)
  891. {
  892. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  893. return 0;
  894. }
  895. if (!ASN1BERDecExplicitTag(dd, 0x80000001, &dd2, &di2))
  896. return 0;
  897. if (!ASN1BERDecPeekTag(dd2, &tag))
  898. return 0;
  899. switch (tag)
  900. {
  901. case 0x80000000:
  902. val->identification.o = ASN1embeddedpdv_identification_syntaxes_o;
  903. if (!ASN1BERDecExplicitTag(dd2, 0x80000000, &dd3, &di3))
  904. return 0;
  905. if (!ASN1BERDecObjectIdentifier(dd3, 0x80000000,
  906. &val->identification.u.syntaxes.abstract))
  907. return 0;
  908. if (!ASN1BERDecObjectIdentifier(dd3, 0x80000001,
  909. &val->identification.u.syntaxes.transfer))
  910. return 0;
  911. if (!ASN1BERDecEndOfContents(dd2, dd3, di3))
  912. return 0;
  913. break;
  914. case 0x80000001:
  915. val->identification.o = ASN1embeddedpdv_identification_syntax_o;
  916. if (!ASN1BERDecObjectIdentifier(dd2, 0x80000001,
  917. &val->identification.u.syntax))
  918. return 0;
  919. break;
  920. case 0x80000002:
  921. val->identification.o =
  922. ASN1embeddedpdv_identification_presentation_context_id_o;
  923. if (!ASN1BERDecU32Val(dd2, 0x80000002,
  924. &val->identification.u.presentation_context_id))
  925. return 0;
  926. break;
  927. case 0x80000003:
  928. val->identification.o =
  929. ASN1embeddedpdv_identification_context_negotiation_o;
  930. if (!ASN1BERDecExplicitTag(dd2, 0x80000003, &dd3, &di3))
  931. return 0;
  932. if (!ASN1BERDecU32Val(dd3, 0x80000000, &val->
  933. identification.u.context_negotiation.presentation_context_id))
  934. return 0;
  935. if (!ASN1BERDecObjectIdentifier(dd3, 0x80000001,
  936. &val->identification.u.context_negotiation.transfer_syntax))
  937. return 0;
  938. if (!ASN1BERDecEndOfContents(dd2, dd3, di3))
  939. return 0;
  940. break;
  941. case 0x80000004:
  942. val->identification.o =
  943. ASN1embeddedpdv_identification_transfer_syntax_o;
  944. if (!ASN1BERDecObjectIdentifier(dd2, 0x80000004,
  945. &val->identification.u.transfer_syntax))
  946. return 0;
  947. break;
  948. case 0x80000005:
  949. val->identification.o = ASN1embeddedpdv_identification_fixed_o;
  950. if (!ASN1BERDecNull(dd2, 0x80000005))
  951. return 0;
  952. break;
  953. default:
  954. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  955. return 0;
  956. }
  957. if (!ASN1BERDecEndOfContents(dd, dd2, di2))
  958. return 0;
  959. if (!ASN1DecAddEmbeddedPdvIdentification(d->parent,
  960. &val->identification))
  961. return 0;
  962. val->data_value.o = ASN1embeddedpdv_data_value_encoded_o;
  963. if (!ASN1BERDecBitString(dd, 0x80000003,
  964. &val->data_value.u.encoded))
  965. return 0;
  966. if (!ASN1BERDecEndOfContents(dec, dd, di))
  967. return 0;
  968. }
  969. else
  970. {
  971. /* primitive? EP-B encoded: */
  972. if (!ASN1BERDecLength(dec, &len, NULL))
  973. return 0;
  974. /* then copy value */
  975. if (!len)
  976. {
  977. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  978. return 0;
  979. }
  980. val->data_value.o = ASN1embeddedpdv_data_value_encoded_o;
  981. val->data_value.u.encoded.length = 8 * (len - 1);
  982. val->data_value.u.encoded.value = (ASN1octet_t *)DecMemAlloc(dec, len - 1);
  983. if (!val->data_value.u.encoded.value)
  984. {
  985. return 0;
  986. }
  987. index = *dec->pos++;
  988. CopyMemory(val->data_value.u.encoded.value, dec->pos, len - 1);
  989. identification = ASN1DecGetEmbeddedPdvIdentification(d->parent, index);
  990. if (!identification)
  991. return 0;
  992. val->identification.o = identification->o;
  993. switch (identification->o)
  994. {
  995. case ASN1embeddedpdv_identification_syntaxes_o:
  996. if (!ASN1DecDupObjectIdentifier(dec,
  997. &val->identification.u.syntaxes.abstract,
  998. &identification->u.syntaxes.abstract))
  999. return 0;
  1000. if (!ASN1DecDupObjectIdentifier(dec,
  1001. &val->identification.u.syntaxes.transfer,
  1002. &identification->u.syntaxes.transfer))
  1003. return 0;
  1004. break;
  1005. case ASN1embeddedpdv_identification_syntax_o:
  1006. if (!ASN1DecDupObjectIdentifier(dec,
  1007. &val->identification.u.syntax,
  1008. &identification->u.syntax))
  1009. return 0;
  1010. break;
  1011. case ASN1embeddedpdv_identification_presentation_context_id_o:
  1012. val->identification.u.presentation_context_id =
  1013. identification->u.presentation_context_id;
  1014. break;
  1015. case ASN1embeddedpdv_identification_context_negotiation_o:
  1016. val->identification.u.context_negotiation.presentation_context_id =
  1017. identification->u.context_negotiation.presentation_context_id;
  1018. if (!ASN1DecDupObjectIdentifier(dec,
  1019. &val->identification.u.context_negotiation.transfer_syntax,
  1020. &identification->u.context_negotiation.transfer_syntax))
  1021. return 0;
  1022. break;
  1023. case ASN1embeddedpdv_identification_transfer_syntax_o:
  1024. if (!ASN1DecDupObjectIdentifier(dec,
  1025. &val->identification.u.transfer_syntax,
  1026. &identification->u.transfer_syntax))
  1027. return 0;
  1028. break;
  1029. case ASN1embeddedpdv_identification_fixed_o:
  1030. break;
  1031. }
  1032. }
  1033. return 1;
  1034. }
  1035. #endif // ENABLE_EMBEDDED_PDV
  1036. #ifdef ENABLE_EXTERNAL
  1037. /* decode external value */
  1038. int ASN1BERDecExternal(ASN1decoding_t dec, ASN1uint32_t tag, ASN1external_t *val)
  1039. {
  1040. ASN1decoding_t dd;
  1041. ASN1octet_t *di;
  1042. ASN1objectidentifier_t id;
  1043. ASN1octetstring_t os;
  1044. /* decode explicit tag */
  1045. if (!ASN1BERDecExplicitTag(dec, tag | 0x20000000, &dd, &di))
  1046. return 0;
  1047. /* peek tag of choice alternative */
  1048. if (!ASN1BERDecPeekTag(dd, &tag))
  1049. return 0;
  1050. /* decode alternative */
  1051. if (tag == 0x6)
  1052. {
  1053. if (!ASN1BERDecObjectIdentifier(dd, 0x6, &id))
  1054. return 0;
  1055. if (!ASN1BERDecPeekTag(dd, &tag))
  1056. return 0;
  1057. if (tag == 0x2)
  1058. {
  1059. val->identification.o =
  1060. ASN1external_identification_context_negotiation_o;
  1061. val->identification.u.context_negotiation.transfer_syntax = id;
  1062. if (!ASN1BERDecU32Val(dd, 0x2, &val->
  1063. identification.u.context_negotiation.presentation_context_id))
  1064. return 0;
  1065. if (!ASN1BERDecPeekTag(dd, &tag))
  1066. return 0;
  1067. }
  1068. else
  1069. {
  1070. val->identification.o = ASN1external_identification_syntax_o;
  1071. val->identification.u.syntax = id;
  1072. }
  1073. }
  1074. else
  1075. if (tag == 0x2)
  1076. {
  1077. val->identification.o =
  1078. ASN1external_identification_presentation_context_id_o;
  1079. if (!ASN1BERDecU32Val(dd, 0x2,
  1080. &val->identification.u.presentation_context_id))
  1081. return 0;
  1082. if (!ASN1BERDecPeekTag(dd, &tag))
  1083. return 0;
  1084. }
  1085. else
  1086. {
  1087. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1088. return 0;
  1089. }
  1090. /* decode optional data value descriptor if present */
  1091. if (tag == 0x7)
  1092. {
  1093. if (!ASN1BERDecZeroCharString(dd, 0x7, &val->data_value_descriptor))
  1094. return 0;
  1095. if (!ASN1BERDecPeekTag(dd, &tag))
  1096. return 0;
  1097. }
  1098. else
  1099. {
  1100. val->data_value_descriptor = NULL;
  1101. }
  1102. /* decode data value alternative */
  1103. switch (tag)
  1104. {
  1105. case 0:
  1106. val->data_value.o = ASN1external_data_value_notation_o;
  1107. if (!ASN1BERDecOpenType(dd, &val->data_value.u.notation))
  1108. return 0;
  1109. break;
  1110. case 1:
  1111. val->data_value.o = ASN1external_data_value_encoded_o;
  1112. if (!ASN1BERDecOctetString(dd, 0x4, &os))
  1113. return 0;
  1114. val->data_value.u.encoded.value = os.value;
  1115. val->data_value.u.encoded.length = os.length * 8;
  1116. break;
  1117. case 2:
  1118. val->data_value.o = ASN1external_data_value_encoded_o;
  1119. if (!ASN1BERDecBitString(dd, 0x3, &val->data_value.u.encoded))
  1120. return 0;
  1121. break;
  1122. default:
  1123. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1124. return 0;
  1125. }
  1126. /* end of constructed (explicit tagged) value */
  1127. if (!ASN1BERDecEndOfContents(dec, dd, di))
  1128. return 0;
  1129. return 1;
  1130. }
  1131. #endif // ENABLE_EXTERNAL
  1132. /* decode generalized time value */
  1133. int ASN1BERDecGeneralizedTime(ASN1decoding_t dec, ASN1uint32_t tag, ASN1generalizedtime_t *val)
  1134. {
  1135. ASN1ztcharstring_t time;
  1136. if (ASN1BERDecZeroCharString(dec, tag, &time))
  1137. {
  1138. int rc = ASN1string2generalizedtime(val, time);
  1139. DecMemFree(dec, time);
  1140. if (rc)
  1141. {
  1142. return 1;
  1143. }
  1144. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1145. }
  1146. return 0;
  1147. }
  1148. /* decode multibyte string value */
  1149. int ASN1BERDecZeroMultibyteString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1ztcharstring_t *val)
  1150. {
  1151. return ASN1BERDecZeroCharString(dec, tag, val);
  1152. }
  1153. int ASN1BERDecMultibyteString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1charstring_t *val)
  1154. {
  1155. return ASN1BERDecCharString(dec, tag, val);
  1156. }
  1157. /* decode null value */
  1158. int ASN1BERDecNull(ASN1decoding_t dec, ASN1uint32_t tag)
  1159. {
  1160. ASN1uint32_t len;
  1161. if (ASN1BERDecTag(dec, tag, NULL))
  1162. {
  1163. if (ASN1BERDecLength(dec, &len, NULL))
  1164. {
  1165. if (! len)
  1166. {
  1167. return 1;
  1168. }
  1169. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1170. }
  1171. }
  1172. return 0;
  1173. }
  1174. /* decode object identifier value */
  1175. int ASN1BERDecObjectIdentifier(ASN1decoding_t dec, ASN1uint32_t tag, ASN1objectidentifier_t *val)
  1176. {
  1177. if (ASN1BERDecTag(dec, tag, NULL))
  1178. {
  1179. ASN1uint32_t len, i, v;
  1180. ASN1octet_t *data, *p;
  1181. ASN1uint32_t nelem;
  1182. ASN1objectidentifier_t q;
  1183. if (ASN1BERDecLength(dec, &len, NULL))
  1184. {
  1185. data = dec->pos;
  1186. dec->pos += len;
  1187. nelem = 1;
  1188. for (i = 0, p = data; i < len; i++, p++)
  1189. {
  1190. if (!(*p & 0x80))
  1191. nelem++;
  1192. }
  1193. *val = q = DecAllocObjectIdentifier(dec, nelem);
  1194. if (q)
  1195. {
  1196. v = 0;
  1197. for (i = 0, p = data; i < len; i++, p++)
  1198. {
  1199. v = (v << 7) | (*p & 0x7f);
  1200. if (!(*p & 0x80))
  1201. {
  1202. if (q == *val)
  1203. { // first id
  1204. q->value = v / 40;
  1205. if (q->value > 2)
  1206. q->value = 2;
  1207. q->next->value = v - 40 * q->value;
  1208. q = q->next->next;
  1209. }
  1210. else
  1211. {
  1212. q->value = v;
  1213. q = q->next;
  1214. }
  1215. v = 0;
  1216. }
  1217. }
  1218. return 1;
  1219. }
  1220. }
  1221. }
  1222. return 0;
  1223. }
  1224. /* decode object identifier value */
  1225. int ASN1BERDecObjectIdentifier2(ASN1decoding_t dec, ASN1uint32_t tag, ASN1objectidentifier2_t *val)
  1226. {
  1227. if (ASN1BERDecTag(dec, tag, NULL))
  1228. {
  1229. ASN1uint32_t len, i, v;
  1230. ASN1octet_t *data, *p;
  1231. ASN1objectidentifier_t q;
  1232. if (ASN1BERDecLength(dec, &len, NULL))
  1233. {
  1234. if (len <= 16) // lonchanc: hard-coded value 16 to be consistent with ASN1objectidentifier2_t
  1235. {
  1236. data = dec->pos;
  1237. dec->pos += len;
  1238. val->count = 0;
  1239. v = 0;
  1240. for (i = 0, p = data; i < len; i++, p++)
  1241. {
  1242. v = (v << 7) | (*p & 0x7f);
  1243. if (!(*p & 0x80))
  1244. {
  1245. if (! val->count)
  1246. { // first id
  1247. val->value[0] = v / 40;
  1248. if (val->value[0] > 2)
  1249. val->value[0] = 2;
  1250. val->value[1] = v - 40 * val->value[0];
  1251. val->count = 2;
  1252. }
  1253. else
  1254. {
  1255. val->value[val->count++] = v;
  1256. }
  1257. v = 0;
  1258. }
  1259. }
  1260. return 1;
  1261. }
  1262. else
  1263. {
  1264. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  1265. }
  1266. }
  1267. }
  1268. return 0;
  1269. }
  1270. /* decode integer into signed 8 bit value */
  1271. int ASN1BERDecS8Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1int8_t *val)
  1272. {
  1273. /* skip tag */
  1274. if (ASN1BERDecTag(dec, tag, NULL))
  1275. {
  1276. ASN1uint32_t len;
  1277. /* get length */
  1278. if (ASN1BERDecLength(dec, &len, NULL))
  1279. {
  1280. /* get value */
  1281. if (1 == len)
  1282. {
  1283. *val = *dec->pos++;
  1284. return 1;
  1285. }
  1286. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1287. }
  1288. }
  1289. return 0;
  1290. }
  1291. /* decode integer into signed 16 bit value */
  1292. int ASN1BERDecS16Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1int16_t *val)
  1293. {
  1294. /* skip tag */
  1295. if (ASN1BERDecTag(dec, tag, NULL))
  1296. {
  1297. ASN1uint32_t len;
  1298. /* get length */
  1299. if (ASN1BERDecLength(dec, &len, NULL))
  1300. {
  1301. /* get value */
  1302. switch (len)
  1303. {
  1304. case 1:
  1305. *val = *dec->pos++;
  1306. break;
  1307. case 2:
  1308. *val = (*dec->pos << 8) | dec->pos[1];
  1309. dec->pos += 2;
  1310. break;
  1311. default:
  1312. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1313. return 0;
  1314. }
  1315. return 1;
  1316. }
  1317. }
  1318. return 0;
  1319. }
  1320. const ASN1int32_t c_nSignMask[] = { 0xFFFFFF00, 0xFFFF0000, 0xFF000000, 0 };
  1321. /* decode integer into signed 32 bit value */
  1322. int ASN1BERDecS32Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1int32_t *val)
  1323. {
  1324. /* skip tag */
  1325. if (ASN1BERDecTag(dec, tag, NULL))
  1326. {
  1327. ASN1uint32_t len;
  1328. /* get length */
  1329. if (ASN1BERDecLength(dec, &len, NULL))
  1330. {
  1331. int fSigned = 0x80 & *dec->pos;
  1332. /* get value */
  1333. switch (len)
  1334. {
  1335. case 1:
  1336. *val = *dec->pos++;
  1337. break;
  1338. case 2:
  1339. *val = (*dec->pos << 8) | dec->pos[1];
  1340. dec->pos += 2;
  1341. break;
  1342. case 3:
  1343. *val = (*dec->pos << 16) | (dec->pos[1] << 8) | dec->pos[2];
  1344. dec->pos += 3;
  1345. break;
  1346. case 4:
  1347. *val = (*dec->pos << 24) | (dec->pos[1] << 16) |
  1348. (dec->pos[2] << 8) | dec->pos[3];
  1349. dec->pos += 4;
  1350. break;
  1351. default:
  1352. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1353. return 0;
  1354. }
  1355. if (fSigned)
  1356. {
  1357. *val |= c_nSignMask[len-1];
  1358. }
  1359. return 1;
  1360. }
  1361. }
  1362. return 0;
  1363. }
  1364. /* decode integer into intx value */
  1365. int ASN1BERDecSXVal(ASN1decoding_t dec, ASN1uint32_t tag, ASN1intx_t *val)
  1366. {
  1367. /* skip tag */
  1368. if (ASN1BERDecTag(dec, tag, NULL))
  1369. {
  1370. ASN1uint32_t len;
  1371. /* get length */
  1372. if (ASN1BERDecLength(dec, &len, NULL))
  1373. {
  1374. /* get value */
  1375. if (len >= 1)
  1376. {
  1377. val->length = len;
  1378. val->value = (ASN1octet_t *)DecMemAlloc(dec, len);
  1379. if (val->value)
  1380. {
  1381. CopyMemory(val->value, dec->pos, len);
  1382. dec->pos += len;
  1383. return 1;
  1384. }
  1385. }
  1386. else
  1387. {
  1388. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1389. }
  1390. }
  1391. }
  1392. return 0;
  1393. }
  1394. /* decode integer into unsigned 8 bit value */
  1395. int ASN1BERDecU8Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1uint8_t *val)
  1396. {
  1397. /* skip tag */
  1398. if (ASN1BERDecTag(dec, tag, NULL))
  1399. {
  1400. ASN1uint32_t len;
  1401. /* get length */
  1402. if (ASN1BERDecLength(dec, &len, NULL))
  1403. {
  1404. /* get value */
  1405. switch (len)
  1406. {
  1407. case 1:
  1408. *val = *dec->pos++;
  1409. return 1;
  1410. case 2:
  1411. if (0 == *dec->pos)
  1412. {
  1413. *val = dec->pos[1];
  1414. dec->pos += 2;
  1415. return 1;
  1416. }
  1417. // intentionally fall through
  1418. default:
  1419. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1420. break;
  1421. }
  1422. }
  1423. }
  1424. return 0;
  1425. }
  1426. /* decode integer into unsigned 16 bit value */
  1427. int ASN1BERDecU16Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1uint16_t *val)
  1428. {
  1429. /* skip tag */
  1430. if (ASN1BERDecTag(dec, tag, NULL))
  1431. {
  1432. ASN1uint32_t len;
  1433. /* get length */
  1434. if (ASN1BERDecLength(dec, &len, NULL))
  1435. {
  1436. /* get value */
  1437. switch (len)
  1438. {
  1439. case 1:
  1440. *val = *dec->pos++;
  1441. return 1;
  1442. case 2:
  1443. *val = (*dec->pos << 8) | dec->pos[1];
  1444. dec->pos += 2;
  1445. return 1;
  1446. case 3:
  1447. if (0 == *dec->pos)
  1448. {
  1449. *val = (dec->pos[1] << 8) | dec->pos[2];
  1450. dec->pos += 3;
  1451. return 1;
  1452. }
  1453. // intentionally fall through
  1454. default:
  1455. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1456. break;
  1457. }
  1458. }
  1459. }
  1460. return 0;
  1461. }
  1462. /* decode utc time value */
  1463. int ASN1BERDecUTCTime(ASN1decoding_t dec, ASN1uint32_t tag, ASN1utctime_t *val)
  1464. {
  1465. ASN1ztcharstring_t time;
  1466. if (ASN1BERDecZeroCharString(dec, tag, &time))
  1467. {
  1468. int rc = ASN1string2utctime(val, time);
  1469. DecMemFree(dec, time);
  1470. if (rc)
  1471. {
  1472. return 1;
  1473. }
  1474. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1475. }
  1476. return 0;
  1477. }
  1478. /* decode zero terminated string value */
  1479. int ASN1BERDecZeroCharString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1ztcharstring_t *val)
  1480. {
  1481. ASN1uint32_t constructed, len, infinite;
  1482. ASN1ztcharstring_t c;
  1483. ASN1decoding_t dd;
  1484. ASN1octet_t *di;
  1485. ASN1uint32_t lv, lc;
  1486. /* skip tag */
  1487. if (ASN1BERDecTag(dec, tag, &constructed))
  1488. {
  1489. /* get length */
  1490. if (ASN1BERDecLength(dec, &len, &infinite))
  1491. {
  1492. if (constructed)
  1493. {
  1494. /* constructed? then start decoding of constructed value */
  1495. *val = NULL;
  1496. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1497. {
  1498. while (ASN1BERDecNotEndOfContents(dd, di))
  1499. {
  1500. if (ASN1BERDecZeroCharString(dd, 0x4, &c))
  1501. {
  1502. lv = My_lstrlenA(*val);
  1503. lc = My_lstrlenA(c);
  1504. if (lc)
  1505. {
  1506. /* resize value */
  1507. *val = (char *)DecMemReAlloc(dd, *val, lv + lc + 1);
  1508. if (*val)
  1509. {
  1510. /* concat strings */
  1511. CopyMemory(*val + lv, c, lc + 1);
  1512. /* free unused string */
  1513. DecMemFree(dec, c);
  1514. }
  1515. else
  1516. {
  1517. return 0;
  1518. }
  1519. }
  1520. }
  1521. else
  1522. {
  1523. return 0;
  1524. }
  1525. } // while
  1526. return ASN1BERDecEndOfContents(dec, dd, di);
  1527. }
  1528. }
  1529. else
  1530. {
  1531. /* primitive? then copy value */
  1532. *val = (char *)DecMemAlloc(dec, len + 1);
  1533. if (*val)
  1534. {
  1535. CopyMemory(*val, dec->pos, len);
  1536. (*val)[len] = 0;
  1537. dec->pos += len;
  1538. return 1;
  1539. }
  1540. }
  1541. }
  1542. }
  1543. return 0;
  1544. }
  1545. /* decode zero terminated 16 bit string value */
  1546. int ASN1BERDecZeroChar16String(ASN1decoding_t dec, ASN1uint32_t tag, ASN1ztchar16string_t *val)
  1547. {
  1548. ASN1uint32_t constructed, len, infinite;
  1549. ASN1ztchar16string_t c;
  1550. ASN1decoding_t dd;
  1551. ASN1octet_t *di;
  1552. ASN1uint32_t i;
  1553. ASN1uint32_t lv, lc;
  1554. /* skip tag */
  1555. if (ASN1BERDecTag(dec, tag, &constructed))
  1556. {
  1557. /* get length */
  1558. if (ASN1BERDecLength(dec, &len, &infinite))
  1559. {
  1560. if (constructed)
  1561. {
  1562. /* constructed? then start decoding of constructed value */
  1563. *val = NULL;
  1564. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1565. {
  1566. while (ASN1BERDecNotEndOfContents(dd, di))
  1567. {
  1568. if (ASN1BERDecZeroChar16String(dd, 0x4, &c))
  1569. {
  1570. lv = ASN1str16len(*val);
  1571. lc = ASN1str16len(c);
  1572. if (lc)
  1573. {
  1574. /* resize value */
  1575. *val = (ASN1char16_t *)DecMemReAlloc(dd, *val, (lv + lc + 1) * sizeof(ASN1char16_t));
  1576. if (*val)
  1577. {
  1578. /* concat strings */
  1579. CopyMemory(*val + lv, c, (lc + 1) * sizeof(ASN1char16_t));
  1580. /* free unused string */
  1581. DecMemFree(dec, c);
  1582. }
  1583. else
  1584. {
  1585. return 0;
  1586. }
  1587. }
  1588. }
  1589. else
  1590. {
  1591. return 0;
  1592. }
  1593. } // while
  1594. return ASN1BERDecEndOfContents(dec, dd, di);
  1595. }
  1596. }
  1597. else
  1598. {
  1599. /* primitive? then copy value */
  1600. *val = (ASN1char16_t *)DecMemAlloc(dec, (len + 1) * sizeof(ASN1char16_t));
  1601. if (*val)
  1602. {
  1603. for (i = 0; i < len; i++)
  1604. {
  1605. (*val)[i] = (*dec->pos << 8) | dec->pos[1];
  1606. dec->pos += 2;
  1607. }
  1608. (*val)[len] = 0;
  1609. return 1;
  1610. }
  1611. }
  1612. }
  1613. }
  1614. return 0;
  1615. }
  1616. /* decode zero terminated 32 bit string value */
  1617. int ASN1BERDecZeroChar32String(ASN1decoding_t dec, ASN1uint32_t tag, ASN1ztchar32string_t *val)
  1618. {
  1619. ASN1uint32_t constructed, len, infinite;
  1620. ASN1ztchar32string_t c;
  1621. ASN1decoding_t dd;
  1622. ASN1octet_t *di;
  1623. ASN1uint32_t i;
  1624. ASN1uint32_t lv, lc;
  1625. /* skip tag */
  1626. if (ASN1BERDecTag(dec, tag, &constructed))
  1627. {
  1628. /* get length */
  1629. if (ASN1BERDecLength(dec, &len, &infinite))
  1630. {
  1631. if (constructed)
  1632. {
  1633. /* constructed? then start decoding of constructed value */
  1634. *val = (ASN1char32_t *)DecMemAlloc(dec, sizeof(ASN1char32_t));
  1635. if (*val)
  1636. {
  1637. **val = 0;
  1638. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1639. {
  1640. while (ASN1BERDecNotEndOfContents(dd, di))
  1641. {
  1642. if (ASN1BERDecZeroChar32String(dd, 0x4, &c))
  1643. {
  1644. lv = ASN1str32len(*val);
  1645. lc = ASN1str32len(c);
  1646. if (lc)
  1647. {
  1648. /* resize value */
  1649. *val = (ASN1char32_t *)DecMemReAlloc(dd, *val, (lv + lc + 1) * sizeof(ASN1char32_t));
  1650. if (*val)
  1651. {
  1652. /* concat strings */
  1653. CopyMemory(*val + lv, c, (lc + 1) * sizeof(ASN1char32_t));
  1654. /* free unused string */
  1655. DecMemFree(dec, c);
  1656. }
  1657. }
  1658. }
  1659. else
  1660. {
  1661. return 0;
  1662. }
  1663. }
  1664. return ASN1BERDecEndOfContents(dec, dd, di);
  1665. }
  1666. }
  1667. }
  1668. else
  1669. {
  1670. /* primitive? then copy value */
  1671. *val = (ASN1char32_t *)DecMemAlloc(dec, (len + 1) * sizeof(ASN1char32_t));
  1672. if (*val)
  1673. {
  1674. for (i = 0; i < len; i++)
  1675. {
  1676. (*val)[i] = (*dec->pos << 24) | (dec->pos[1] << 16) |
  1677. (dec->pos[2] << 8) | dec->pos[3];;
  1678. dec->pos += 4;
  1679. }
  1680. (*val)[len] = 0;
  1681. return 1;
  1682. }
  1683. }
  1684. }
  1685. }
  1686. return 0;
  1687. }
  1688. /* skip a value */
  1689. int ASN1BERDecSkip(ASN1decoding_t dec)
  1690. {
  1691. ASN1uint32_t tag;
  1692. ASN1uint32_t constructed, len, infinite;
  1693. ASN1decoding_t dd;
  1694. ASN1octet_t *di;
  1695. /* set warning flag */
  1696. ASN1DecSetError(dec, ASN1_WRN_EXTENDED);
  1697. /* read tag */
  1698. if (ASN1BERDecPeekTag(dec, &tag))
  1699. {
  1700. if (ASN1BERDecTag(dec, tag, &constructed))
  1701. {
  1702. if (constructed)
  1703. {
  1704. /* constructed? then get length */
  1705. if (ASN1BERDecLength(dec, &len, &infinite))
  1706. {
  1707. if (!infinite)
  1708. {
  1709. /* skip value */
  1710. dec->pos += len;
  1711. // remove the above warning set previously
  1712. ASN1DecSetError(dec, ASN1_SUCCESS);
  1713. return 1;
  1714. }
  1715. /* start skipping of constructed value */
  1716. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1717. {
  1718. while (ASN1BERDecNotEndOfContents(dd, di))
  1719. {
  1720. if (ASN1BERDecSkip(dd))
  1721. {
  1722. continue;
  1723. }
  1724. return 0;
  1725. }
  1726. if (ASN1BERDecEndOfContents(dec, dd, di))
  1727. {
  1728. // remove the above warning set previously
  1729. ASN1DecSetError(dec, ASN1_SUCCESS);
  1730. return 1;
  1731. }
  1732. return 0;
  1733. }
  1734. }
  1735. }
  1736. else
  1737. {
  1738. /* primitive? then get length */
  1739. if (ASN1BERDecLength(dec, &len, NULL))
  1740. {
  1741. /* skip value */
  1742. dec->pos += len;
  1743. // remove the above warning set previously
  1744. ASN1DecSetError(dec, ASN1_SUCCESS);
  1745. return 1;
  1746. }
  1747. }
  1748. }
  1749. }
  1750. return 0;
  1751. }
  1752. /* decode an open type value */
  1753. int _BERDecOpenType(ASN1decoding_t dec, ASN1open_t *val, ASN1uint32_t fNoCopy)
  1754. {
  1755. ASN1uint32_t tag;
  1756. ASN1uint32_t constructed, len, infinite;
  1757. ASN1decoding_t dd;
  1758. ASN1octet_t *di;
  1759. ASN1octet_t *p;
  1760. p = dec->pos;
  1761. /* skip tag */
  1762. if (ASN1BERDecPeekTag(dec, &tag))
  1763. {
  1764. if (ASN1BERDecTag(dec, tag, &constructed))
  1765. {
  1766. if (constructed)
  1767. {
  1768. /* constructed? then get length */
  1769. if (ASN1BERDecLength(dec, &len, &infinite))
  1770. {
  1771. if (!infinite)
  1772. {
  1773. /* skip value */
  1774. dec->pos += len;
  1775. goto MakeCopy;
  1776. }
  1777. /* start decoding of constructed value */
  1778. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1779. {
  1780. while (ASN1BERDecNotEndOfContents(dd, di))
  1781. {
  1782. if (ASN1BERDecSkip(dd))
  1783. {
  1784. continue;
  1785. }
  1786. return 0;
  1787. }
  1788. if (ASN1BERDecEndOfContents(dec, dd, di))
  1789. {
  1790. goto MakeCopy;
  1791. }
  1792. }
  1793. }
  1794. return 0;
  1795. }
  1796. else
  1797. {
  1798. /* primitive? then get length */
  1799. if (ASN1BERDecLength(dec, &len, NULL))
  1800. {
  1801. /* skip value */
  1802. dec->pos += len;
  1803. }
  1804. else
  1805. {
  1806. return 0;
  1807. }
  1808. }
  1809. MakeCopy:
  1810. // clean up unused fields
  1811. // val->decoded = NULL;
  1812. // val->userdata = NULL;
  1813. /* copy skipped value */
  1814. val->length = (ASN1uint32_t) (dec->pos - p);
  1815. if (fNoCopy)
  1816. {
  1817. val->encoded = p;
  1818. return 1;
  1819. }
  1820. else
  1821. {
  1822. val->encoded = (ASN1octet_t *)DecMemAlloc(dec, val->length);
  1823. if (val->encoded)
  1824. {
  1825. CopyMemory(val->encoded, p, val->length);
  1826. return 1;
  1827. }
  1828. }
  1829. }
  1830. }
  1831. return 0;
  1832. }
  1833. /* decode an open type value, making a copy */
  1834. int ASN1BERDecOpenType(ASN1decoding_t dec, ASN1open_t *val)
  1835. {
  1836. return _BERDecOpenType(dec, val, FALSE);
  1837. }
  1838. /* decode an open type value, no copy */
  1839. int ASN1BERDecOpenType2(ASN1decoding_t dec, ASN1open_t *val)
  1840. {
  1841. return _BERDecOpenType(dec, val, TRUE);
  1842. }
  1843. /* finish decoding */
  1844. int ASN1BERDecFlush(ASN1decoding_t dec)
  1845. {
  1846. /* calculate length */
  1847. dec->len = (ASN1uint32_t) (dec->pos - dec->buf);
  1848. /* set WRN_NOEOD if data left */
  1849. if (dec->len >= dec->size)
  1850. {
  1851. DecAssert(dec, dec->len == dec->size);
  1852. return 1;
  1853. }
  1854. ASN1DecSetError(dec, ASN1_WRN_NOEOD);
  1855. return 1;
  1856. }
  1857. #endif // ENABLE_BER