Leaked source code of windows server 2003
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.

1979 lines
64 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. // Prevent a buffer overrun by ensuring that buf is large enough to hold the
  672. // data stored in p:
  673. if (len > sizeof(buf))
  674. {
  675. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  676. return 0;
  677. }
  678. CopyMemory(buf, p, len - 1);
  679. buf[len - 1] = 0;
  680. b = strchr(buf, ',');
  681. if (b)
  682. *b = '.';
  683. *val = strtod((char *)buf, &b);
  684. if (*b)
  685. {
  686. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  687. return 0;
  688. }
  689. }
  690. }
  691. return 1;
  692. }
  693. #endif // ENABLE_DOUBLE
  694. #ifdef ENABLE_REAL
  695. int ASN1BERDecReal(ASN1decoding_t dec, ASN1uint32_t tag, ASN1real_t *val)
  696. {
  697. ASN1uint32_t head;
  698. ASN1int32_t ex;
  699. // ASN1intx_t exponent;
  700. ASN1uint32_t baselog2;
  701. ASN1uint32_t len;
  702. ASN1uint32_t i;
  703. ASN1octet_t *p, *q;
  704. double v;
  705. ASN1intx_t help;
  706. if (!ASN1BERDecTag(dec, tag, NULL))
  707. return 0;
  708. if (!ASN1BERDecLength(dec, &len, NULL))
  709. return 0;
  710. // *val = 0.0;
  711. DecAssert(dec, 0 == (int) eReal_Normal);
  712. ZeroMemory(val, sizeof(*val));
  713. if (len)
  714. {
  715. p = q = dec->pos;
  716. dec->pos += len;
  717. head = *p++;
  718. /* binary encoding? */
  719. if (head & 0x80)
  720. {
  721. val->type = eReal_Normal;
  722. /* get base */
  723. switch (head & 0x30)
  724. {
  725. case 0:
  726. /* base 2 */
  727. baselog2 = 1;
  728. break;
  729. case 0x10:
  730. /* base 8 */
  731. baselog2 = 3;
  732. break;
  733. case 0x20:
  734. /* base 16 */
  735. baselog2 = 4;
  736. break;
  737. default:
  738. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  739. return 0;
  740. }
  741. /* get exponent */
  742. switch (head & 0x03)
  743. {
  744. case 0:
  745. /* 8 bit exponent */
  746. ex = (ASN1int8_t)*p++;
  747. ASN1intx_setint32(&val->exponent, ex);
  748. break;
  749. case 1:
  750. /* 16 bit exponent */
  751. ex = (ASN1int16_t)((*p << 8) | p[1]);
  752. p += 2;
  753. // ASN1intx_setint32_t(&exponent, ex);
  754. ASN1intx_setint32(&val->exponent, ex);
  755. break;
  756. case 2:
  757. /* 24 bit exponent */
  758. ex = ((*p << 16) | (p[1] << 8) | p[2]);
  759. if (ex & 0x800000)
  760. ex -= 0x1000000;
  761. // ASN1intx_setint32_t(&exponent, ex);
  762. ASN1intx_setint32(&val->exponent, ex);
  763. break;
  764. default:
  765. /* variable length exponent */
  766. val->exponent.length = *p;
  767. val->exponent.value = (ASN1octet_t *)DecMemAlloc(dec, *p);
  768. if (!val->exponent.value)
  769. {
  770. return 0;
  771. }
  772. CopyMemory(val->exponent.value, p + 1, *p);
  773. p += *p + 1;
  774. break;
  775. }
  776. /* calculate remaining length */
  777. len -= (p - q);
  778. if (!len)
  779. {
  780. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  781. return 0;
  782. }
  783. /* get mantissa */
  784. val->mantissa.length = (*p & 0x80) ? len + 1 : len;
  785. val->mantissa.value = (ASN1octet_t *)DecMemAlloc(dec, val->mantissa.length);
  786. if (!val->mantissa.value)
  787. {
  788. return 0;
  789. }
  790. val->mantissa.value[0] = 0;
  791. CopyMemory(val->mantissa.value + val->mantissa.length - len, p, len);
  792. /* scale mantissa */
  793. switch (head & 0x0c)
  794. {
  795. case 0x04:
  796. /* scaling factor 1 */
  797. ASN1intx_muloctet(&help, &val->mantissa, 2);
  798. ASN1intx_free(&val->mantissa);
  799. val->mantissa = help;
  800. break;
  801. case 0x08:
  802. /* scaling factor 2 */
  803. ASN1intx_muloctet(&help, &val->mantissa, 4);
  804. ASN1intx_free(&val->mantissa);
  805. val->mantissa = help;
  806. break;
  807. case 0x0c:
  808. /* scaling factor 3 */
  809. ASN1intx_muloctet(&help, &val->mantissa, 8);
  810. ASN1intx_free(&val->mantissa);
  811. val->mantissa = help;
  812. break;
  813. }
  814. /* check sign */
  815. if (head & 0x40)
  816. {
  817. ASN1intx_neg(&help, &val->mantissa);
  818. ASN1intx_free(&val->mantissa);
  819. val->mantissa = help;
  820. }
  821. }
  822. else
  823. /* special real values? */
  824. if (head & 0x40)
  825. {
  826. switch (head)
  827. {
  828. case 0x40:
  829. /* PLUS-INFINITY */
  830. val->type = eReal_PlusInfinity;
  831. break;
  832. case 0x41:
  833. /* MINUS-INFINITY */
  834. val->type = eReal_MinusInfinity;
  835. break;
  836. default:
  837. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  838. return 0;
  839. }
  840. }
  841. /* decimal encoding */
  842. else
  843. {
  844. char *b;
  845. char buf[256];
  846. DecAssert(dec, (head & 0xc0) == 0xc0);
  847. // Prevent a buffer overrun by ensuring that buf is large enough to hold the
  848. // data stored in p:
  849. if (len > sizeof(buf))
  850. {
  851. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  852. return 0;
  853. }
  854. CopyMemory(buf, p, len - 1);
  855. buf[len - 1] = 0;
  856. ex = 0;
  857. b = strchr(buf, ',');
  858. if (b)
  859. {
  860. // move the decimal point to the right
  861. ex -= lstrlenA(b+1);
  862. lstrcpyA(b, b+1);
  863. }
  864. // skip leading zeros
  865. for (b = &buf[0]; '0' == *b; b++)
  866. ;
  867. val->type = eReal_Normal;
  868. val->base = 10;
  869. ASN1intx_setint32(&val->exponent, ex);
  870. /*XXX*/
  871. // missing code here!!!
  872. // need to set val->mantissa through the decimal digits string
  873. DecAssert(dec, 0);
  874. return 0;
  875. }
  876. }
  877. return 1;
  878. }
  879. #endif // ENABLE_REAL
  880. #ifdef ENABLE_EMBEDDED_PDV
  881. /* decode embedded pdv value */
  882. int ASN1BERDecEmbeddedPdv(ASN1decoding_t dec, ASN1uint32_t tag, ASN1embeddedpdv_t *val)
  883. {
  884. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t) dec;
  885. ASN1uint32_t constructed, len, infinite;
  886. ASN1uint32_t index;
  887. ASN1embeddedpdv_identification_t *identification;
  888. ASN1decoding_t dd, dd2, dd3;
  889. ASN1octet_t *di, *di2, *di3;
  890. /* skip tag */
  891. if (!ASN1BERDecTag(dec, tag, &constructed))
  892. return 0;
  893. if (constructed)
  894. {
  895. /* constructed? EP-A encoded: */
  896. /* get length */
  897. if (!ASN1BERDecLength(dec, &len, &infinite))
  898. return 0;
  899. /* then start decoding of constructed value */
  900. if (! _BERDecConstructed(dec, len, infinite, &dd, &di))
  901. return 0;
  902. if (!ASN1BERDecU32Val(dd, 0x80000000, &index))
  903. return 0;
  904. if (index != d->parent->epilength)
  905. {
  906. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  907. return 0;
  908. }
  909. if (!ASN1BERDecExplicitTag(dd, 0x80000001, &dd2, &di2))
  910. return 0;
  911. if (!ASN1BERDecPeekTag(dd2, &tag))
  912. return 0;
  913. switch (tag)
  914. {
  915. case 0x80000000:
  916. val->identification.o = ASN1embeddedpdv_identification_syntaxes_o;
  917. if (!ASN1BERDecExplicitTag(dd2, 0x80000000, &dd3, &di3))
  918. return 0;
  919. if (!ASN1BERDecObjectIdentifier(dd3, 0x80000000,
  920. &val->identification.u.syntaxes.abstract))
  921. return 0;
  922. if (!ASN1BERDecObjectIdentifier(dd3, 0x80000001,
  923. &val->identification.u.syntaxes.transfer))
  924. return 0;
  925. if (!ASN1BERDecEndOfContents(dd2, dd3, di3))
  926. return 0;
  927. break;
  928. case 0x80000001:
  929. val->identification.o = ASN1embeddedpdv_identification_syntax_o;
  930. if (!ASN1BERDecObjectIdentifier(dd2, 0x80000001,
  931. &val->identification.u.syntax))
  932. return 0;
  933. break;
  934. case 0x80000002:
  935. val->identification.o =
  936. ASN1embeddedpdv_identification_presentation_context_id_o;
  937. if (!ASN1BERDecU32Val(dd2, 0x80000002,
  938. &val->identification.u.presentation_context_id))
  939. return 0;
  940. break;
  941. case 0x80000003:
  942. val->identification.o =
  943. ASN1embeddedpdv_identification_context_negotiation_o;
  944. if (!ASN1BERDecExplicitTag(dd2, 0x80000003, &dd3, &di3))
  945. return 0;
  946. if (!ASN1BERDecU32Val(dd3, 0x80000000, &val->
  947. identification.u.context_negotiation.presentation_context_id))
  948. return 0;
  949. if (!ASN1BERDecObjectIdentifier(dd3, 0x80000001,
  950. &val->identification.u.context_negotiation.transfer_syntax))
  951. return 0;
  952. if (!ASN1BERDecEndOfContents(dd2, dd3, di3))
  953. return 0;
  954. break;
  955. case 0x80000004:
  956. val->identification.o =
  957. ASN1embeddedpdv_identification_transfer_syntax_o;
  958. if (!ASN1BERDecObjectIdentifier(dd2, 0x80000004,
  959. &val->identification.u.transfer_syntax))
  960. return 0;
  961. break;
  962. case 0x80000005:
  963. val->identification.o = ASN1embeddedpdv_identification_fixed_o;
  964. if (!ASN1BERDecNull(dd2, 0x80000005))
  965. return 0;
  966. break;
  967. default:
  968. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  969. return 0;
  970. }
  971. if (!ASN1BERDecEndOfContents(dd, dd2, di2))
  972. return 0;
  973. if (!ASN1DecAddEmbeddedPdvIdentification(d->parent,
  974. &val->identification))
  975. return 0;
  976. val->data_value.o = ASN1embeddedpdv_data_value_encoded_o;
  977. if (!ASN1BERDecBitString(dd, 0x80000003,
  978. &val->data_value.u.encoded))
  979. return 0;
  980. if (!ASN1BERDecEndOfContents(dec, dd, di))
  981. return 0;
  982. }
  983. else
  984. {
  985. /* primitive? EP-B encoded: */
  986. if (!ASN1BERDecLength(dec, &len, NULL))
  987. return 0;
  988. /* then copy value */
  989. if (!len)
  990. {
  991. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  992. return 0;
  993. }
  994. val->data_value.o = ASN1embeddedpdv_data_value_encoded_o;
  995. val->data_value.u.encoded.length = 8 * (len - 1);
  996. val->data_value.u.encoded.value = (ASN1octet_t *)DecMemAlloc(dec, len - 1);
  997. if (!val->data_value.u.encoded.value)
  998. {
  999. return 0;
  1000. }
  1001. index = *dec->pos++;
  1002. CopyMemory(val->data_value.u.encoded.value, dec->pos, len - 1);
  1003. identification = ASN1DecGetEmbeddedPdvIdentification(d->parent, index);
  1004. if (!identification)
  1005. return 0;
  1006. val->identification.o = identification->o;
  1007. switch (identification->o)
  1008. {
  1009. case ASN1embeddedpdv_identification_syntaxes_o:
  1010. if (!ASN1DecDupObjectIdentifier(dec,
  1011. &val->identification.u.syntaxes.abstract,
  1012. &identification->u.syntaxes.abstract))
  1013. return 0;
  1014. if (!ASN1DecDupObjectIdentifier(dec,
  1015. &val->identification.u.syntaxes.transfer,
  1016. &identification->u.syntaxes.transfer))
  1017. return 0;
  1018. break;
  1019. case ASN1embeddedpdv_identification_syntax_o:
  1020. if (!ASN1DecDupObjectIdentifier(dec,
  1021. &val->identification.u.syntax,
  1022. &identification->u.syntax))
  1023. return 0;
  1024. break;
  1025. case ASN1embeddedpdv_identification_presentation_context_id_o:
  1026. val->identification.u.presentation_context_id =
  1027. identification->u.presentation_context_id;
  1028. break;
  1029. case ASN1embeddedpdv_identification_context_negotiation_o:
  1030. val->identification.u.context_negotiation.presentation_context_id =
  1031. identification->u.context_negotiation.presentation_context_id;
  1032. if (!ASN1DecDupObjectIdentifier(dec,
  1033. &val->identification.u.context_negotiation.transfer_syntax,
  1034. &identification->u.context_negotiation.transfer_syntax))
  1035. return 0;
  1036. break;
  1037. case ASN1embeddedpdv_identification_transfer_syntax_o:
  1038. if (!ASN1DecDupObjectIdentifier(dec,
  1039. &val->identification.u.transfer_syntax,
  1040. &identification->u.transfer_syntax))
  1041. return 0;
  1042. break;
  1043. case ASN1embeddedpdv_identification_fixed_o:
  1044. break;
  1045. }
  1046. }
  1047. return 1;
  1048. }
  1049. #endif // ENABLE_EMBEDDED_PDV
  1050. #ifdef ENABLE_EXTERNAL
  1051. /* decode external value */
  1052. int ASN1BERDecExternal(ASN1decoding_t dec, ASN1uint32_t tag, ASN1external_t *val)
  1053. {
  1054. ASN1decoding_t dd;
  1055. ASN1octet_t *di;
  1056. ASN1objectidentifier_t id;
  1057. ASN1octetstring_t os;
  1058. /* decode explicit tag */
  1059. if (!ASN1BERDecExplicitTag(dec, tag | 0x20000000, &dd, &di))
  1060. return 0;
  1061. /* peek tag of choice alternative */
  1062. if (!ASN1BERDecPeekTag(dd, &tag))
  1063. return 0;
  1064. /* decode alternative */
  1065. if (tag == 0x6)
  1066. {
  1067. if (!ASN1BERDecObjectIdentifier(dd, 0x6, &id))
  1068. return 0;
  1069. if (!ASN1BERDecPeekTag(dd, &tag))
  1070. return 0;
  1071. if (tag == 0x2)
  1072. {
  1073. val->identification.o =
  1074. ASN1external_identification_context_negotiation_o;
  1075. val->identification.u.context_negotiation.transfer_syntax = id;
  1076. if (!ASN1BERDecU32Val(dd, 0x2, &val->
  1077. identification.u.context_negotiation.presentation_context_id))
  1078. return 0;
  1079. if (!ASN1BERDecPeekTag(dd, &tag))
  1080. return 0;
  1081. }
  1082. else
  1083. {
  1084. val->identification.o = ASN1external_identification_syntax_o;
  1085. val->identification.u.syntax = id;
  1086. }
  1087. }
  1088. else
  1089. if (tag == 0x2)
  1090. {
  1091. val->identification.o =
  1092. ASN1external_identification_presentation_context_id_o;
  1093. if (!ASN1BERDecU32Val(dd, 0x2,
  1094. &val->identification.u.presentation_context_id))
  1095. return 0;
  1096. if (!ASN1BERDecPeekTag(dd, &tag))
  1097. return 0;
  1098. }
  1099. else
  1100. {
  1101. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1102. return 0;
  1103. }
  1104. /* decode optional data value descriptor if present */
  1105. if (tag == 0x7)
  1106. {
  1107. if (!ASN1BERDecZeroCharString(dd, 0x7, &val->data_value_descriptor))
  1108. return 0;
  1109. if (!ASN1BERDecPeekTag(dd, &tag))
  1110. return 0;
  1111. }
  1112. else
  1113. {
  1114. val->data_value_descriptor = NULL;
  1115. }
  1116. /* decode data value alternative */
  1117. switch (tag)
  1118. {
  1119. case 0:
  1120. val->data_value.o = ASN1external_data_value_notation_o;
  1121. if (!ASN1BERDecOpenType(dd, &val->data_value.u.notation))
  1122. return 0;
  1123. break;
  1124. case 1:
  1125. val->data_value.o = ASN1external_data_value_encoded_o;
  1126. if (!ASN1BERDecOctetString(dd, 0x4, &os))
  1127. return 0;
  1128. val->data_value.u.encoded.value = os.value;
  1129. val->data_value.u.encoded.length = os.length * 8;
  1130. break;
  1131. case 2:
  1132. val->data_value.o = ASN1external_data_value_encoded_o;
  1133. if (!ASN1BERDecBitString(dd, 0x3, &val->data_value.u.encoded))
  1134. return 0;
  1135. break;
  1136. default:
  1137. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1138. return 0;
  1139. }
  1140. /* end of constructed (explicit tagged) value */
  1141. if (!ASN1BERDecEndOfContents(dec, dd, di))
  1142. return 0;
  1143. return 1;
  1144. }
  1145. #endif // ENABLE_EXTERNAL
  1146. /* decode generalized time value */
  1147. int ASN1BERDecGeneralizedTime(ASN1decoding_t dec, ASN1uint32_t tag, ASN1generalizedtime_t *val)
  1148. {
  1149. ASN1ztcharstring_t time;
  1150. if (ASN1BERDecZeroCharString(dec, tag, &time))
  1151. {
  1152. int rc = ASN1string2generalizedtime(val, time);
  1153. DecMemFree(dec, time);
  1154. if (rc)
  1155. {
  1156. return 1;
  1157. }
  1158. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1159. }
  1160. return 0;
  1161. }
  1162. /* decode multibyte string value */
  1163. int ASN1BERDecZeroMultibyteString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1ztcharstring_t *val)
  1164. {
  1165. return ASN1BERDecZeroCharString(dec, tag, val);
  1166. }
  1167. int ASN1BERDecMultibyteString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1charstring_t *val)
  1168. {
  1169. return ASN1BERDecCharString(dec, tag, val);
  1170. }
  1171. /* decode null value */
  1172. int ASN1BERDecNull(ASN1decoding_t dec, ASN1uint32_t tag)
  1173. {
  1174. ASN1uint32_t len;
  1175. if (ASN1BERDecTag(dec, tag, NULL))
  1176. {
  1177. if (ASN1BERDecLength(dec, &len, NULL))
  1178. {
  1179. if (! len)
  1180. {
  1181. return 1;
  1182. }
  1183. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1184. }
  1185. }
  1186. return 0;
  1187. }
  1188. /* decode object identifier value */
  1189. int ASN1BERDecObjectIdentifier(ASN1decoding_t dec, ASN1uint32_t tag, ASN1objectidentifier_t *val)
  1190. {
  1191. if (ASN1BERDecTag(dec, tag, NULL))
  1192. {
  1193. ASN1uint32_t len, i, v;
  1194. ASN1octet_t *data, *p;
  1195. ASN1uint32_t nelem;
  1196. ASN1objectidentifier_t q;
  1197. if (ASN1BERDecLength(dec, &len, NULL))
  1198. {
  1199. data = dec->pos;
  1200. dec->pos += len;
  1201. nelem = 1;
  1202. for (i = 0, p = data; i < len; i++, p++)
  1203. {
  1204. if (!(*p & 0x80))
  1205. nelem++;
  1206. }
  1207. *val = q = DecAllocObjectIdentifier(dec, nelem);
  1208. if (q)
  1209. {
  1210. v = 0;
  1211. for (i = 0, p = data; i < len; i++, p++)
  1212. {
  1213. v = (v << 7) | (*p & 0x7f);
  1214. if (!(*p & 0x80))
  1215. {
  1216. if (q == *val)
  1217. { // first id
  1218. q->value = v / 40;
  1219. if (q->value > 2)
  1220. q->value = 2;
  1221. q->next->value = v - 40 * q->value;
  1222. q = q->next->next;
  1223. }
  1224. else
  1225. {
  1226. q->value = v;
  1227. q = q->next;
  1228. }
  1229. v = 0;
  1230. }
  1231. }
  1232. return 1;
  1233. }
  1234. }
  1235. }
  1236. return 0;
  1237. }
  1238. /* decode object identifier value */
  1239. int ASN1BERDecObjectIdentifier2(ASN1decoding_t dec, ASN1uint32_t tag, ASN1objectidentifier2_t *val)
  1240. {
  1241. if (ASN1BERDecTag(dec, tag, NULL))
  1242. {
  1243. ASN1uint32_t len, i, v;
  1244. ASN1octet_t *data, *p;
  1245. ASN1objectidentifier_t q;
  1246. if (ASN1BERDecLength(dec, &len, NULL))
  1247. {
  1248. if (len <= 16) // lonchanc: hard-coded value 16 to be consistent with ASN1objectidentifier2_t
  1249. {
  1250. data = dec->pos;
  1251. dec->pos += len;
  1252. val->count = 0;
  1253. v = 0;
  1254. for (i = 0, p = data; i < len; i++, p++)
  1255. {
  1256. v = (v << 7) | (*p & 0x7f);
  1257. if (!(*p & 0x80))
  1258. {
  1259. if (! val->count)
  1260. { // first id
  1261. val->value[0] = v / 40;
  1262. if (val->value[0] > 2)
  1263. val->value[0] = 2;
  1264. val->value[1] = v - 40 * val->value[0];
  1265. val->count = 2;
  1266. }
  1267. else
  1268. {
  1269. val->value[val->count++] = v;
  1270. }
  1271. v = 0;
  1272. }
  1273. }
  1274. return 1;
  1275. }
  1276. else
  1277. {
  1278. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  1279. }
  1280. }
  1281. }
  1282. return 0;
  1283. }
  1284. /* decode integer into signed 8 bit value */
  1285. int ASN1BERDecS8Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1int8_t *val)
  1286. {
  1287. /* skip tag */
  1288. if (ASN1BERDecTag(dec, tag, NULL))
  1289. {
  1290. ASN1uint32_t len;
  1291. /* get length */
  1292. if (ASN1BERDecLength(dec, &len, NULL))
  1293. {
  1294. /* get value */
  1295. if (1 == len)
  1296. {
  1297. *val = *dec->pos++;
  1298. return 1;
  1299. }
  1300. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1301. }
  1302. }
  1303. return 0;
  1304. }
  1305. /* decode integer into signed 16 bit value */
  1306. int ASN1BERDecS16Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1int16_t *val)
  1307. {
  1308. /* skip tag */
  1309. if (ASN1BERDecTag(dec, tag, NULL))
  1310. {
  1311. ASN1uint32_t len;
  1312. /* get length */
  1313. if (ASN1BERDecLength(dec, &len, NULL))
  1314. {
  1315. /* get value */
  1316. switch (len)
  1317. {
  1318. case 1:
  1319. *val = *dec->pos++;
  1320. break;
  1321. case 2:
  1322. *val = (*dec->pos << 8) | dec->pos[1];
  1323. dec->pos += 2;
  1324. break;
  1325. default:
  1326. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1327. return 0;
  1328. }
  1329. return 1;
  1330. }
  1331. }
  1332. return 0;
  1333. }
  1334. const ASN1int32_t c_nSignMask[] = { 0xFFFFFF00, 0xFFFF0000, 0xFF000000, 0 };
  1335. /* decode integer into signed 32 bit value */
  1336. int ASN1BERDecS32Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1int32_t *val)
  1337. {
  1338. /* skip tag */
  1339. if (ASN1BERDecTag(dec, tag, NULL))
  1340. {
  1341. ASN1uint32_t len;
  1342. /* get length */
  1343. if (ASN1BERDecLength(dec, &len, NULL))
  1344. {
  1345. int fSigned = 0x80 & *dec->pos;
  1346. /* get value */
  1347. switch (len)
  1348. {
  1349. case 1:
  1350. *val = *dec->pos++;
  1351. break;
  1352. case 2:
  1353. *val = (*dec->pos << 8) | dec->pos[1];
  1354. dec->pos += 2;
  1355. break;
  1356. case 3:
  1357. *val = (*dec->pos << 16) | (dec->pos[1] << 8) | dec->pos[2];
  1358. dec->pos += 3;
  1359. break;
  1360. case 4:
  1361. *val = (*dec->pos << 24) | (dec->pos[1] << 16) |
  1362. (dec->pos[2] << 8) | dec->pos[3];
  1363. dec->pos += 4;
  1364. break;
  1365. default:
  1366. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1367. return 0;
  1368. }
  1369. if (fSigned)
  1370. {
  1371. *val |= c_nSignMask[len-1];
  1372. }
  1373. return 1;
  1374. }
  1375. }
  1376. return 0;
  1377. }
  1378. /* decode integer into intx value */
  1379. int ASN1BERDecSXVal(ASN1decoding_t dec, ASN1uint32_t tag, ASN1intx_t *val)
  1380. {
  1381. /* skip tag */
  1382. if (ASN1BERDecTag(dec, tag, NULL))
  1383. {
  1384. ASN1uint32_t len;
  1385. /* get length */
  1386. if (ASN1BERDecLength(dec, &len, NULL))
  1387. {
  1388. /* get value */
  1389. if (len >= 1)
  1390. {
  1391. val->length = len;
  1392. val->value = (ASN1octet_t *)DecMemAlloc(dec, len);
  1393. if (val->value)
  1394. {
  1395. CopyMemory(val->value, dec->pos, len);
  1396. dec->pos += len;
  1397. return 1;
  1398. }
  1399. }
  1400. else
  1401. {
  1402. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1403. }
  1404. }
  1405. }
  1406. return 0;
  1407. }
  1408. /* decode integer into unsigned 8 bit value */
  1409. int ASN1BERDecU8Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1uint8_t *val)
  1410. {
  1411. /* skip tag */
  1412. if (ASN1BERDecTag(dec, tag, NULL))
  1413. {
  1414. ASN1uint32_t len;
  1415. /* get length */
  1416. if (ASN1BERDecLength(dec, &len, NULL))
  1417. {
  1418. /* get value */
  1419. switch (len)
  1420. {
  1421. case 1:
  1422. *val = *dec->pos++;
  1423. return 1;
  1424. case 2:
  1425. if (0 == *dec->pos)
  1426. {
  1427. *val = dec->pos[1];
  1428. dec->pos += 2;
  1429. return 1;
  1430. }
  1431. // intentionally fall through
  1432. default:
  1433. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1434. break;
  1435. }
  1436. }
  1437. }
  1438. return 0;
  1439. }
  1440. /* decode integer into unsigned 16 bit value */
  1441. int ASN1BERDecU16Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1uint16_t *val)
  1442. {
  1443. /* skip tag */
  1444. if (ASN1BERDecTag(dec, tag, NULL))
  1445. {
  1446. ASN1uint32_t len;
  1447. /* get length */
  1448. if (ASN1BERDecLength(dec, &len, NULL))
  1449. {
  1450. /* get value */
  1451. switch (len)
  1452. {
  1453. case 1:
  1454. *val = *dec->pos++;
  1455. return 1;
  1456. case 2:
  1457. *val = (*dec->pos << 8) | dec->pos[1];
  1458. dec->pos += 2;
  1459. return 1;
  1460. case 3:
  1461. if (0 == *dec->pos)
  1462. {
  1463. *val = (dec->pos[1] << 8) | dec->pos[2];
  1464. dec->pos += 3;
  1465. return 1;
  1466. }
  1467. // intentionally fall through
  1468. default:
  1469. ASN1DecSetError(dec, (len < 1) ? ASN1_ERR_CORRUPT : ASN1_ERR_LARGE);
  1470. break;
  1471. }
  1472. }
  1473. }
  1474. return 0;
  1475. }
  1476. /* decode utc time value */
  1477. int ASN1BERDecUTCTime(ASN1decoding_t dec, ASN1uint32_t tag, ASN1utctime_t *val)
  1478. {
  1479. ASN1ztcharstring_t time;
  1480. if (ASN1BERDecZeroCharString(dec, tag, &time))
  1481. {
  1482. int rc = ASN1string2utctime(val, time);
  1483. DecMemFree(dec, time);
  1484. if (rc)
  1485. {
  1486. return 1;
  1487. }
  1488. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1489. }
  1490. return 0;
  1491. }
  1492. /* decode zero terminated string value */
  1493. int ASN1BERDecZeroCharString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1ztcharstring_t *val)
  1494. {
  1495. ASN1uint32_t constructed, len, infinite;
  1496. ASN1ztcharstring_t c;
  1497. ASN1decoding_t dd;
  1498. ASN1octet_t *di;
  1499. ASN1uint32_t lv, lc;
  1500. /* skip tag */
  1501. if (ASN1BERDecTag(dec, tag, &constructed))
  1502. {
  1503. /* get length */
  1504. if (ASN1BERDecLength(dec, &len, &infinite))
  1505. {
  1506. if (constructed)
  1507. {
  1508. /* constructed? then start decoding of constructed value */
  1509. *val = NULL;
  1510. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1511. {
  1512. while (ASN1BERDecNotEndOfContents(dd, di))
  1513. {
  1514. if (ASN1BERDecZeroCharString(dd, 0x4, &c))
  1515. {
  1516. lv = My_lstrlenA(*val);
  1517. lc = My_lstrlenA(c);
  1518. if (lc)
  1519. {
  1520. /* resize value */
  1521. *val = (char *)DecMemReAlloc(dd, *val, lv + lc + 1);
  1522. if (*val)
  1523. {
  1524. /* concat strings */
  1525. CopyMemory(*val + lv, c, lc + 1);
  1526. /* free unused string */
  1527. DecMemFree(dec, c);
  1528. }
  1529. else
  1530. {
  1531. return 0;
  1532. }
  1533. }
  1534. }
  1535. else
  1536. {
  1537. return 0;
  1538. }
  1539. } // while
  1540. return ASN1BERDecEndOfContents(dec, dd, di);
  1541. }
  1542. }
  1543. else
  1544. {
  1545. /* primitive? then copy value */
  1546. *val = (char *)DecMemAlloc(dec, len + 1);
  1547. if (*val)
  1548. {
  1549. CopyMemory(*val, dec->pos, len);
  1550. (*val)[len] = 0;
  1551. dec->pos += len;
  1552. return 1;
  1553. }
  1554. }
  1555. }
  1556. }
  1557. return 0;
  1558. }
  1559. /* decode zero terminated 16 bit string value */
  1560. int ASN1BERDecZeroChar16String(ASN1decoding_t dec, ASN1uint32_t tag, ASN1ztchar16string_t *val)
  1561. {
  1562. ASN1uint32_t constructed, len, infinite;
  1563. ASN1ztchar16string_t c;
  1564. ASN1decoding_t dd;
  1565. ASN1octet_t *di;
  1566. ASN1uint32_t i;
  1567. ASN1uint32_t lv, lc;
  1568. /* skip tag */
  1569. if (ASN1BERDecTag(dec, tag, &constructed))
  1570. {
  1571. /* get length */
  1572. if (ASN1BERDecLength(dec, &len, &infinite))
  1573. {
  1574. if (constructed)
  1575. {
  1576. /* constructed? then start decoding of constructed value */
  1577. *val = NULL;
  1578. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1579. {
  1580. while (ASN1BERDecNotEndOfContents(dd, di))
  1581. {
  1582. if (ASN1BERDecZeroChar16String(dd, 0x4, &c))
  1583. {
  1584. lv = ASN1str16len(*val);
  1585. lc = ASN1str16len(c);
  1586. if (lc)
  1587. {
  1588. /* resize value */
  1589. *val = (ASN1char16_t *)DecMemReAlloc(dd, *val, (lv + lc + 1) * sizeof(ASN1char16_t));
  1590. if (*val)
  1591. {
  1592. /* concat strings */
  1593. CopyMemory(*val + lv, c, (lc + 1) * sizeof(ASN1char16_t));
  1594. /* free unused string */
  1595. DecMemFree(dec, c);
  1596. }
  1597. else
  1598. {
  1599. return 0;
  1600. }
  1601. }
  1602. }
  1603. else
  1604. {
  1605. return 0;
  1606. }
  1607. } // while
  1608. return ASN1BERDecEndOfContents(dec, dd, di);
  1609. }
  1610. }
  1611. else
  1612. {
  1613. /* primitive? then copy value */
  1614. *val = (ASN1char16_t *)DecMemAlloc(dec, (len + 1) * sizeof(ASN1char16_t));
  1615. if (*val)
  1616. {
  1617. for (i = 0; i < len; i++)
  1618. {
  1619. (*val)[i] = (*dec->pos << 8) | dec->pos[1];
  1620. dec->pos += 2;
  1621. }
  1622. (*val)[len] = 0;
  1623. return 1;
  1624. }
  1625. }
  1626. }
  1627. }
  1628. return 0;
  1629. }
  1630. /* decode zero terminated 32 bit string value */
  1631. int ASN1BERDecZeroChar32String(ASN1decoding_t dec, ASN1uint32_t tag, ASN1ztchar32string_t *val)
  1632. {
  1633. ASN1uint32_t constructed, len, infinite;
  1634. ASN1ztchar32string_t c;
  1635. ASN1decoding_t dd;
  1636. ASN1octet_t *di;
  1637. ASN1uint32_t i;
  1638. ASN1uint32_t lv, lc;
  1639. /* skip tag */
  1640. if (ASN1BERDecTag(dec, tag, &constructed))
  1641. {
  1642. /* get length */
  1643. if (ASN1BERDecLength(dec, &len, &infinite))
  1644. {
  1645. if (constructed)
  1646. {
  1647. /* constructed? then start decoding of constructed value */
  1648. *val = (ASN1char32_t *)DecMemAlloc(dec, sizeof(ASN1char32_t));
  1649. if (*val)
  1650. {
  1651. **val = 0;
  1652. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1653. {
  1654. while (ASN1BERDecNotEndOfContents(dd, di))
  1655. {
  1656. if (ASN1BERDecZeroChar32String(dd, 0x4, &c))
  1657. {
  1658. lv = ASN1str32len(*val);
  1659. lc = ASN1str32len(c);
  1660. if (lc)
  1661. {
  1662. /* resize value */
  1663. *val = (ASN1char32_t *)DecMemReAlloc(dd, *val, (lv + lc + 1) * sizeof(ASN1char32_t));
  1664. if (*val)
  1665. {
  1666. /* concat strings */
  1667. CopyMemory(*val + lv, c, (lc + 1) * sizeof(ASN1char32_t));
  1668. /* free unused string */
  1669. DecMemFree(dec, c);
  1670. }
  1671. else
  1672. {
  1673. return 0;
  1674. }
  1675. }
  1676. }
  1677. else
  1678. {
  1679. return 0;
  1680. }
  1681. }
  1682. return ASN1BERDecEndOfContents(dec, dd, di);
  1683. }
  1684. }
  1685. }
  1686. else
  1687. {
  1688. /* primitive? then copy value */
  1689. *val = (ASN1char32_t *)DecMemAlloc(dec, (len + 1) * sizeof(ASN1char32_t));
  1690. if (*val)
  1691. {
  1692. for (i = 0; i < len; i++)
  1693. {
  1694. (*val)[i] = (*dec->pos << 24) | (dec->pos[1] << 16) |
  1695. (dec->pos[2] << 8) | dec->pos[3];;
  1696. dec->pos += 4;
  1697. }
  1698. (*val)[len] = 0;
  1699. return 1;
  1700. }
  1701. }
  1702. }
  1703. }
  1704. return 0;
  1705. }
  1706. /* skip a value */
  1707. int ASN1BERDecSkip(ASN1decoding_t dec)
  1708. {
  1709. ASN1uint32_t tag;
  1710. ASN1uint32_t constructed, len, infinite;
  1711. ASN1decoding_t dd;
  1712. ASN1octet_t *di;
  1713. /* set warning flag */
  1714. ASN1DecSetError(dec, ASN1_WRN_EXTENDED);
  1715. /* read tag */
  1716. if (ASN1BERDecPeekTag(dec, &tag))
  1717. {
  1718. if (ASN1BERDecTag(dec, tag, &constructed))
  1719. {
  1720. if (constructed)
  1721. {
  1722. /* constructed? then get length */
  1723. if (ASN1BERDecLength(dec, &len, &infinite))
  1724. {
  1725. if (!infinite)
  1726. {
  1727. /* skip value */
  1728. dec->pos += len;
  1729. // remove the above warning set previously
  1730. ASN1DecSetError(dec, ASN1_SUCCESS);
  1731. return 1;
  1732. }
  1733. /* start skipping of constructed value */
  1734. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1735. {
  1736. while (ASN1BERDecNotEndOfContents(dd, di))
  1737. {
  1738. if (ASN1BERDecSkip(dd))
  1739. {
  1740. continue;
  1741. }
  1742. return 0;
  1743. }
  1744. if (ASN1BERDecEndOfContents(dec, dd, di))
  1745. {
  1746. // remove the above warning set previously
  1747. ASN1DecSetError(dec, ASN1_SUCCESS);
  1748. return 1;
  1749. }
  1750. return 0;
  1751. }
  1752. }
  1753. }
  1754. else
  1755. {
  1756. /* primitive? then get length */
  1757. if (ASN1BERDecLength(dec, &len, NULL))
  1758. {
  1759. /* skip value */
  1760. dec->pos += len;
  1761. // remove the above warning set previously
  1762. ASN1DecSetError(dec, ASN1_SUCCESS);
  1763. return 1;
  1764. }
  1765. }
  1766. }
  1767. }
  1768. return 0;
  1769. }
  1770. /* decode an open type value */
  1771. int _BERDecOpenType(ASN1decoding_t dec, ASN1open_t *val, ASN1uint32_t fNoCopy)
  1772. {
  1773. ASN1uint32_t tag;
  1774. ASN1uint32_t constructed, len, infinite;
  1775. ASN1decoding_t dd;
  1776. ASN1octet_t *di;
  1777. ASN1octet_t *p;
  1778. p = dec->pos;
  1779. /* skip tag */
  1780. if (ASN1BERDecPeekTag(dec, &tag))
  1781. {
  1782. if (ASN1BERDecTag(dec, tag, &constructed))
  1783. {
  1784. if (constructed)
  1785. {
  1786. /* constructed? then get length */
  1787. if (ASN1BERDecLength(dec, &len, &infinite))
  1788. {
  1789. if (!infinite)
  1790. {
  1791. /* skip value */
  1792. dec->pos += len;
  1793. goto MakeCopy;
  1794. }
  1795. /* start decoding of constructed value */
  1796. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  1797. {
  1798. while (ASN1BERDecNotEndOfContents(dd, di))
  1799. {
  1800. if (ASN1BERDecSkip(dd))
  1801. {
  1802. continue;
  1803. }
  1804. return 0;
  1805. }
  1806. if (ASN1BERDecEndOfContents(dec, dd, di))
  1807. {
  1808. goto MakeCopy;
  1809. }
  1810. }
  1811. }
  1812. return 0;
  1813. }
  1814. else
  1815. {
  1816. /* primitive? then get length */
  1817. if (ASN1BERDecLength(dec, &len, NULL))
  1818. {
  1819. /* skip value */
  1820. dec->pos += len;
  1821. }
  1822. else
  1823. {
  1824. return 0;
  1825. }
  1826. }
  1827. MakeCopy:
  1828. // clean up unused fields
  1829. // val->decoded = NULL;
  1830. // val->userdata = NULL;
  1831. /* copy skipped value */
  1832. val->length = (ASN1uint32_t) (dec->pos - p);
  1833. if (fNoCopy)
  1834. {
  1835. val->encoded = p;
  1836. return 1;
  1837. }
  1838. else
  1839. {
  1840. val->encoded = (ASN1octet_t *)DecMemAlloc(dec, val->length);
  1841. if (val->encoded)
  1842. {
  1843. CopyMemory(val->encoded, p, val->length);
  1844. return 1;
  1845. }
  1846. }
  1847. }
  1848. }
  1849. return 0;
  1850. }
  1851. /* decode an open type value, making a copy */
  1852. int ASN1BERDecOpenType(ASN1decoding_t dec, ASN1open_t *val)
  1853. {
  1854. return _BERDecOpenType(dec, val, FALSE);
  1855. }
  1856. /* decode an open type value, no copy */
  1857. int ASN1BERDecOpenType2(ASN1decoding_t dec, ASN1open_t *val)
  1858. {
  1859. return _BERDecOpenType(dec, val, TRUE);
  1860. }
  1861. /* finish decoding */
  1862. int ASN1BERDecFlush(ASN1decoding_t dec)
  1863. {
  1864. /* calculate length */
  1865. dec->len = (ASN1uint32_t) (dec->pos - dec->buf);
  1866. /* set WRN_NOEOD if data left */
  1867. if (dec->len >= dec->size)
  1868. {
  1869. DecAssert(dec, dec->len == dec->size);
  1870. return 1;
  1871. }
  1872. ASN1DecSetError(dec, ASN1_WRN_NOEOD);
  1873. return 1;
  1874. }
  1875. #endif // ENABLE_BER