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.

1241 lines
35 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. // THE FOLLOWING IS FROM BERENCOD.C
  5. /* get the expected length based on the table */
  6. ASN1uint32_t _BERGetLength(ASN1uint32_t val, const ASN1uint32_t Tbl[], ASN1uint32_t cItems)
  7. {
  8. ASN1uint32_t i;
  9. for (i = 0; i < cItems; i++)
  10. {
  11. if (val < Tbl[i])
  12. return i+1;
  13. }
  14. return cItems+1;
  15. }
  16. static const ASN1uint32_t c_TagTable[] = { 31, 0x80, 0x4000, 0x200000, 0x10000000 };
  17. /* encode a tag */
  18. int ASN1BEREncTag(ASN1encoding_t enc, ASN1uint32_t tag)
  19. {
  20. ASN1uint32_t tagclass, tagvalue, cbTagLength;
  21. tagclass = (tag >> 24) & 0xe0;
  22. tagvalue = tag & 0x1fffffff;
  23. cbTagLength = _BERGetLength(tagvalue, c_TagTable, ARRAY_SIZE(c_TagTable));
  24. if (ASN1BEREncCheck(enc, cbTagLength))
  25. {
  26. if (cbTagLength == 1)
  27. {
  28. *enc->pos++ = (ASN1octet_t)(tagclass | tagvalue);
  29. }
  30. else
  31. {
  32. *enc->pos++ = (ASN1octet_t)(tagclass | 0x1f);
  33. switch (cbTagLength)
  34. {
  35. case 6:
  36. *enc->pos++ = (ASN1octet_t)((tagvalue >> 28) | 0x80);
  37. // lonchanc: intentionally fall through
  38. case 5:
  39. *enc->pos++ = (ASN1octet_t)((tagvalue >> 21) | 0x80);
  40. // lonchanc: intentionally fall through
  41. case 4:
  42. *enc->pos++ = (ASN1octet_t)((tagvalue >> 14) | 0x80);
  43. // lonchanc: intentionally fall through
  44. case 3:
  45. *enc->pos++ = (ASN1octet_t)((tagvalue >> 7) | 0x80);
  46. // lonchanc: intentionally fall through
  47. case 2:
  48. *enc->pos++ = (ASN1octet_t)(tagvalue & 0x7f);
  49. break;
  50. }
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. /* put the length value */
  57. void _BERPutLength(ASN1encoding_t enc, ASN1uint32_t len, ASN1uint32_t cbLength)
  58. {
  59. if (cbLength > 1)
  60. {
  61. *enc->pos++ = (ASN1octet_t) (0x7f + cbLength); // 0x80 + cbLength - 1;
  62. }
  63. switch (cbLength)
  64. {
  65. case 5:
  66. *enc->pos++ = (ASN1octet_t)(len >> 24);
  67. // lonchanc: intentionally fall through
  68. case 4:
  69. *enc->pos++ = (ASN1octet_t)(len >> 16);
  70. // lonchanc: intentionally fall through
  71. case 3:
  72. *enc->pos++ = (ASN1octet_t)(len >> 8);
  73. // lonchanc: intentionally fall through
  74. default: // case 2: case 1:
  75. *enc->pos++ = (ASN1octet_t)len;
  76. break;
  77. }
  78. }
  79. static const ASN1uint32_t c_LengthTable[] = { 0x80, 0x100, 0x10000, 0x1000000 };
  80. /* encode length */
  81. int ASN1BEREncLength(ASN1encoding_t enc, ASN1uint32_t len)
  82. {
  83. ASN1uint32_t cbLength = _BERGetLength(len, c_LengthTable, ARRAY_SIZE(c_LengthTable));
  84. if (ASN1BEREncCheck(enc, cbLength + len))
  85. {
  86. _BERPutLength(enc, len, cbLength);
  87. return 1;
  88. }
  89. return 0;
  90. }
  91. /* encode an octet string value */
  92. int ASN1BEREncOctetString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1octet_t *val)
  93. {
  94. /* encode tag */
  95. if (ASN1BEREncTag(enc, tag))
  96. {
  97. /* encode length */
  98. if (ASN1BEREncLength(enc, len))
  99. {
  100. /* copy value */
  101. CopyMemory(enc->pos, val, len);
  102. enc->pos += len;
  103. return 1;
  104. }
  105. }
  106. return 0;
  107. }
  108. /* encode a boolean value */
  109. int ASN1BEREncBool(ASN1encoding_t enc, ASN1uint32_t tag, ASN1bool_t val)
  110. {
  111. if (ASN1BEREncTag(enc, tag))
  112. {
  113. if (ASN1BEREncLength(enc, 1))
  114. {
  115. *enc->pos++ = val ? 0xFF : 0;
  116. return 1;
  117. }
  118. }
  119. return 0;
  120. }
  121. static const c_U32LengthTable[] = { 0x80, 0x8000, 0x800000, 0x80000000 };
  122. /* encode a unsigned integer value */
  123. int ASN1BEREncU32(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t val)
  124. {
  125. EncAssert(enc, tag != 0x01);
  126. if (ASN1BEREncTag(enc, tag))
  127. {
  128. ASN1uint32_t cbLength;
  129. cbLength = _BERGetLength(val, c_U32LengthTable, ARRAY_SIZE(c_U32LengthTable));
  130. if (ASN1BEREncLength(enc, cbLength))
  131. {
  132. switch (cbLength)
  133. {
  134. case 5:
  135. *enc->pos++ = 0;
  136. // lonchanc: intentionally fall through
  137. case 4:
  138. *enc->pos++ = (ASN1octet_t)(val >> 24);
  139. // lonchanc: intentionally fall through
  140. case 3:
  141. *enc->pos++ = (ASN1octet_t)(val >> 16);
  142. // lonchanc: intentionally fall through
  143. case 2:
  144. *enc->pos++ = (ASN1octet_t)(val >> 8);
  145. // lonchanc: intentionally fall through
  146. case 1:
  147. *enc->pos++ = (ASN1octet_t)(val);
  148. break;
  149. }
  150. return 1;
  151. }
  152. }
  153. return 0;
  154. }
  155. // THE FOLLOWING IS FROM BERDECOD.C
  156. /* check if len octets are left in decoding stream */
  157. int ASN1BERDecCheck(ASN1decoding_t dec, ASN1uint32_t len)
  158. {
  159. // We need to ensure:
  160. // 1) that dec->pos + len doesn't cause an arithmetic overflow
  161. // 2) dec->pos + len doesn't exceed the size of our buffer
  162. if (dec->pos + len >= dec->pos &&
  163. dec->pos + len <= dec->buf + dec->size)
  164. {
  165. return 1;
  166. }
  167. ASN1DecSetError(dec, ASN1_ERR_EOD);
  168. return 0;
  169. }
  170. int _BERDecPeekCheck(ASN1decoding_t dec, ASN1uint32_t len)
  171. {
  172. return ((dec->pos + len <= dec->buf + dec->size) ? 1 : 0);
  173. }
  174. /* start decoding of a constructed value */
  175. int _BERDecConstructed(ASN1decoding_t dec, ASN1uint32_t len, ASN1uint32_t infinite, ASN1decoding_t *dd, ASN1octet_t **ppBufEnd)
  176. {
  177. // safety net
  178. DecAssert(dec, NULL != dd);
  179. *dd = dec;
  180. #if 0 // NO_NESTED_DECODING
  181. // lonchanc: this does not work because open type can be the last component and
  182. // the open type decoder needs to peek a tag. as a result, we may peek the tag
  183. // outside the buffer boundary.
  184. if (ppBufEnd && (! infinite))
  185. {
  186. *ppBufEnd = dec->pos + len;
  187. return 1;
  188. }
  189. #endif
  190. /* initialize a new decoding stream as child of running decoding stream */
  191. if (ASN1_CreateDecoder(dec->module, dd,
  192. dec->pos, infinite ? dec->size - (ASN1uint32_t) (dec->pos - dec->buf) : len, dec) >= 0)
  193. {
  194. /* set pointer to end of decoding stream if definite length case selected */
  195. *ppBufEnd = infinite ? NULL : (*dd)->buf + (*dd)->size;
  196. return 1;
  197. }
  198. return 0;
  199. }
  200. /* decode a tag value; return constructed bit if desired */
  201. int ASN1BERDecTag(ASN1decoding_t dec, ASN1uint32_t tag, ASN1uint32_t *constructed)
  202. {
  203. ASN1uint32_t tagvalue, tagclass, c;
  204. /* get tag class and value */
  205. if (ASN1BERDecCheck(dec, 1))
  206. {
  207. tagclass = *dec->pos & 0xe0;
  208. tagvalue = *dec->pos++ & 0x1f;
  209. if (tagvalue == 0x1f)
  210. {
  211. tagvalue = 0;
  212. do {
  213. if (ASN1BERDecCheck(dec, 1))
  214. {
  215. c = *dec->pos++;
  216. if (! (tagvalue & 0xe0000000))
  217. {
  218. tagvalue = (tagvalue << 7) | (c & 0x7f);
  219. }
  220. else
  221. {
  222. ASN1DecSetError(dec, ASN1_ERR_BADTAG);
  223. return 0;
  224. }
  225. }
  226. else
  227. {
  228. return 0;
  229. }
  230. } while (c & 0x80);
  231. }
  232. /* extract constructed bit if wanted */
  233. if (constructed)
  234. {
  235. *constructed = tagclass & 0x20;
  236. tagclass &= ~0x20;
  237. }
  238. /* check if tag equals desires */
  239. if (tag == ((tagclass << 24) | tagvalue))
  240. {
  241. return 1;
  242. }
  243. ASN1DecSetError(dec, ASN1_ERR_BADTAG);
  244. }
  245. return 0;
  246. }
  247. /* decode length */
  248. int ASN1BERDecLength(ASN1decoding_t dec, ASN1uint32_t *len, ASN1uint32_t *infinite)
  249. {
  250. // default is definite length
  251. if (infinite)
  252. {
  253. *infinite = 0;
  254. }
  255. /* get length and infinite flag */
  256. if (ASN1BERDecCheck(dec, 1))
  257. {
  258. ASN1uint32_t l = *dec->pos++;
  259. if (l < 0x80)
  260. {
  261. *len = l;
  262. }
  263. else
  264. if (l == 0x80)
  265. {
  266. *len = 0;
  267. if (infinite)
  268. {
  269. *infinite = 1;
  270. }
  271. else
  272. {
  273. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  274. return 0;
  275. }
  276. }
  277. else
  278. if (l <= 0x84)
  279. {
  280. ASN1uint32_t i = l - 0x80;
  281. if (ASN1BERDecCheck(dec, i))
  282. {
  283. l = 0;
  284. switch (i)
  285. {
  286. case 4:
  287. l = *dec->pos++ << 24;
  288. /*FALLTHROUGH*/
  289. case 3:
  290. l |= *dec->pos++ << 16;
  291. /*FALLTHROUGH*/
  292. case 2:
  293. l |= *dec->pos++ << 8;
  294. /*FALLTHROUGH*/
  295. case 1:
  296. l |= *dec->pos++;
  297. break;
  298. }
  299. *len = l;
  300. }
  301. else
  302. {
  303. return 0;
  304. }
  305. }
  306. else
  307. {
  308. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  309. return 0;
  310. }
  311. /* check if enough octets left if length is known */
  312. if (!infinite || !*infinite)
  313. {
  314. return ASN1BERDecCheck(dec, *len);
  315. }
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. /* decode an explicit tag */
  321. int ASN1BERDecExplicitTag(ASN1decoding_t dec, ASN1uint32_t tag, ASN1decoding_t *dd, ASN1octet_t **ppBufEnd)
  322. {
  323. ASN1uint32_t len, infinite, constructed;
  324. // safety net
  325. if (dd)
  326. {
  327. *dd = dec;
  328. }
  329. /* skip the constructed tag */
  330. if (ASN1BERDecTag(dec, tag | 0x20000000, NULL))
  331. {
  332. /* get the length */
  333. if (ASN1BERDecLength(dec, &len, &infinite))
  334. {
  335. /* start decoding of constructed value */
  336. if (! dd)
  337. {
  338. *ppBufEnd = infinite ? NULL : dec->pos + len;
  339. return 1;
  340. }
  341. return _BERDecConstructed(dec, len, infinite, dd, ppBufEnd);
  342. }
  343. }
  344. return 0;
  345. }
  346. /* decode octet string value (helper function) */
  347. int _BERDecOctetStringWorker(ASN1decoding_t dec, ASN1uint32_t tag, ASN1octetstring_t *val, ASN1uint32_t fNoCopy, ASN1uint32_t nMaxRecursionDepth)
  348. {
  349. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  350. ASN1uint32_t constructed, len, infinite;
  351. ASN1decoding_t dd;
  352. ASN1octet_t *di;
  353. /* BUG 750698: Limit the recursion depth for octet strings */
  354. if (nMaxRecursionDepth-- == 0)
  355. {
  356. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  357. return -1; /* max recursion depth exceeded */
  358. }
  359. if (ASN1BERDecTag(dec, tag, &constructed))
  360. {
  361. if (ASN1BERDecLength(dec, &len, &infinite))
  362. {
  363. if (! constructed)
  364. {
  365. val->length = len;
  366. if (fNoCopy)
  367. {
  368. val->value = dec->pos;
  369. dec->pos += len;
  370. return 1;
  371. }
  372. else
  373. {
  374. if (len)
  375. {
  376. val->value = (ASN1octet_t *)DecMemAlloc(dec, len);
  377. if (val->value)
  378. {
  379. CopyMemory(val->value, dec->pos, len);
  380. dec->pos += len;
  381. return 1;
  382. }
  383. }
  384. else
  385. {
  386. val->value = NULL;
  387. return 1;
  388. }
  389. }
  390. }
  391. else
  392. {
  393. ASN1octetstring_t o;
  394. val->length = 0;
  395. val->value = NULL;
  396. if (_BERDecConstructed(dec, len, infinite, &dd, &di))
  397. {
  398. while (ASN1BERDecNotEndOfContents(dd, di))
  399. {
  400. int nRet;
  401. o.length = 0;
  402. o.value = NULL;
  403. nRet = _BERDecOctetStringWorker(dd, 0x4, &o, fNoCopy, nMaxRecursionDepth);
  404. if (-1 == nRet) /* max recursion depth exceeded */
  405. {
  406. /* propagate the error */
  407. return nRet;
  408. }
  409. else if (nRet)
  410. {
  411. if (o.length)
  412. {
  413. if (fNoCopy)
  414. {
  415. *val = o;
  416. break; // break out the loop because nocopy cannot have multiple constructed streams
  417. }
  418. /* resize value */
  419. val->value = (ASN1octet_t *)DecMemReAlloc(dd, val->value,
  420. val->length + o.length);
  421. if (val->value)
  422. {
  423. /* concat octet strings */
  424. CopyMemory(val->value + val->length, o.value, o.length);
  425. val->length += o.length;
  426. /* free unused octet string */
  427. DecMemFree(dec, o.value);
  428. }
  429. else
  430. {
  431. return 0;
  432. }
  433. }
  434. }
  435. }
  436. return ASN1BERDecEndOfContents(dec, dd, di);
  437. }
  438. }
  439. }
  440. }
  441. return 0;
  442. }
  443. /* define the maximum allowed recursion depth for octet strings */
  444. #define MAX_OCTET_STRING_DEPTH 10
  445. /* decode octet string value */
  446. int _BERDecOctetString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1octetstring_t *val, ASN1uint32_t fNoCopy)
  447. {
  448. int nRet = _BERDecOctetStringWorker(dec, tag, val, fNoCopy, MAX_OCTET_STRING_DEPTH);
  449. if (-1 == nRet)
  450. {
  451. nRet = 0;
  452. }
  453. return nRet;
  454. }
  455. /* decode octet string value, making copy */
  456. int ASN1BERDecOctetString(ASN1decoding_t dec, ASN1uint32_t tag, ASN1octetstring_t *val)
  457. {
  458. return _BERDecOctetString(dec, tag, val, FALSE);
  459. }
  460. /* decode octet string value, no copy */
  461. int ASN1BERDecOctetString2(ASN1decoding_t dec, ASN1uint32_t tag, ASN1octetstring_t *val)
  462. {
  463. return _BERDecOctetString(dec, tag, val, TRUE);
  464. }
  465. /* peek the following tag without advancing the read position */
  466. int ASN1BERDecPeekTag(ASN1decoding_t dec, ASN1uint32_t *tag)
  467. {
  468. ASN1uint32_t tagvalue, tagclass, c;
  469. ASN1octet_t *p;
  470. *tag = 0; // invalid tag
  471. if (_BERDecPeekCheck(dec, 1))
  472. {
  473. p = dec->pos;
  474. /* get tagclass without primitive/constructed bit */
  475. tagclass = *dec->pos & 0xc0;
  476. /* get tag value */
  477. tagvalue = *dec->pos++ & 0x1f;
  478. if (tagvalue == 0x1f)
  479. {
  480. tagvalue = 0;
  481. do {
  482. if (_BERDecPeekCheck(dec, 1))
  483. {
  484. c = *dec->pos++;
  485. if (! (tagvalue & 0xe0000000))
  486. {
  487. tagvalue = (tagvalue << 7) | (c & 0x7f);
  488. }
  489. else
  490. {
  491. ASN1DecSetError(dec, ASN1_ERR_BADTAG);
  492. return 0;
  493. }
  494. }
  495. else
  496. {
  497. return 0;
  498. }
  499. } while (c & 0x80);
  500. }
  501. /* return tag */
  502. *tag = ((tagclass << 24) | tagvalue);
  503. /* reset decoding position */
  504. dec->pos = p;
  505. return 1;
  506. }
  507. return 0;
  508. }
  509. /* decode boolean into ASN1boot_t */
  510. int ASN1BERDecBool(ASN1decoding_t dec, ASN1uint32_t tag, ASN1bool_t *val)
  511. {
  512. /* skip tag */
  513. if (ASN1BERDecTag(dec, tag, NULL))
  514. {
  515. /* get length */
  516. ASN1uint32_t len;
  517. if (ASN1BERDecLength(dec, &len, NULL))
  518. {
  519. if (len >= 1)
  520. {
  521. DecAssert(dec, len == 1);
  522. *val = *dec->pos ? 1 : 0;
  523. dec->pos += len; // self defensive
  524. return 1;
  525. }
  526. }
  527. }
  528. return 0;
  529. }
  530. /* decode integer into unsigned 32 bit value */
  531. int ASN1BERDecU32Val(ASN1decoding_t dec, ASN1uint32_t tag, ASN1uint32_t *val)
  532. {
  533. ASN1uint32_t len;
  534. /* skip tag */
  535. if (ASN1BERDecTag(dec, tag, NULL))
  536. {
  537. /* get length */
  538. if (ASN1BERDecLength(dec, &len, NULL))
  539. {
  540. /* get value */
  541. if (len >= 1)
  542. {
  543. switch (len)
  544. {
  545. case 1:
  546. *val = *dec->pos++;
  547. break;
  548. case 2:
  549. *val = (*dec->pos << 8) | dec->pos[1];
  550. dec->pos += 2;
  551. break;
  552. case 3:
  553. *val = (*dec->pos << 16) | (dec->pos[1] << 8) | dec->pos[2];
  554. dec->pos += 3;
  555. break;
  556. case 4:
  557. *val = (*dec->pos << 24) | (dec->pos[1] << 16) |
  558. (dec->pos[2] << 8) | dec->pos[3];
  559. dec->pos += 4;
  560. break;
  561. case 5:
  562. if (! *dec->pos)
  563. {
  564. *val = (dec->pos[1] << 24) | (dec->pos[2] << 16) |
  565. (dec->pos[3] << 8) | dec->pos[4];
  566. dec->pos += 5;
  567. break;
  568. }
  569. // intentionally fall through
  570. default:
  571. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  572. return 0;
  573. }
  574. return 1;
  575. }
  576. else
  577. {
  578. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  579. return 0;
  580. }
  581. }
  582. }
  583. return 0;
  584. }
  585. int ASN1BERDecEndOfContents(ASN1decoding_t dec, ASN1decoding_t dd, ASN1octet_t *pBufEnd)
  586. {
  587. ASN1error_e err = ASN1_ERR_CORRUPT;
  588. if (! dd)
  589. {
  590. dd = dec;
  591. }
  592. DecAssert(dec, NULL != dd);
  593. if (pBufEnd)
  594. {
  595. /* end of definite length case: */
  596. /* check if decoded up to end of contents */
  597. if (dd->pos == pBufEnd)
  598. {
  599. dec->pos = pBufEnd;
  600. err = ASN1_SUCCESS;
  601. }
  602. }
  603. else
  604. {
  605. /* end of infinite length case: */
  606. /* expect end-of-contents octets */
  607. if (ASN1BERDecCheck(dd, 2))
  608. {
  609. if (0 == dd->pos[0] && 0 == dd->pos[1])
  610. {
  611. dd->pos += 2;
  612. if (dd != dec)
  613. {
  614. /* finit child decoding stream and update parent decoding stream */
  615. dec->pos = dd->pos;
  616. }
  617. err = ASN1_SUCCESS;
  618. }
  619. }
  620. else
  621. {
  622. err = ASN1_ERR_EOD;
  623. }
  624. }
  625. if (dd && dd != dec)
  626. {
  627. ASN1_CloseDecoder(dd);
  628. }
  629. if (ASN1_SUCCESS == err)
  630. {
  631. return 1;
  632. }
  633. ASN1DecSetError(dec, err);
  634. return 0;
  635. }
  636. /* check if end of contents (of a constructed value) has been reached */
  637. int ASN1BERDecNotEndOfContents(ASN1decoding_t dec, ASN1octet_t *pBufEnd)
  638. {
  639. return (pBufEnd ?
  640. (dec->pos < pBufEnd) :
  641. (ASN1BERDecCheck(dec, 2) && (dec->pos[0] || dec->pos[1])));
  642. }
  643. #ifdef ENABLE_BER
  644. typedef struct
  645. {
  646. ASN1octet_t *pBuf;
  647. ASN1uint32_t cbBufSize;
  648. }
  649. CER_BLK_BUF;
  650. typedef struct
  651. {
  652. ASN1blocktype_e eBlkType;
  653. ASN1encoding_t encPrimary;
  654. ASN1encoding_t encSecondary;
  655. ASN1uint32_t nMaxBlkSize;
  656. ASN1uint32_t nCurrBlkSize;
  657. CER_BLK_BUF *aBlkBuf;
  658. }
  659. CER_BLOCK;
  660. #define MAX_INIT_BLK_SIZE 16
  661. int ASN1CEREncBeginBlk(ASN1encoding_t enc, ASN1blocktype_e eBlkType, void **ppBlk_)
  662. {
  663. CER_BLOCK *pBlk = (CER_BLOCK *) EncMemAlloc(enc, sizeof(CER_BLOCK));
  664. if (NULL != pBlk)
  665. {
  666. EncAssert(enc, ASN1_DER_SET_OF_BLOCK == eBlkType);
  667. pBlk->eBlkType = eBlkType;
  668. pBlk->encPrimary = enc;
  669. pBlk->encSecondary = NULL;
  670. pBlk->nMaxBlkSize = MAX_INIT_BLK_SIZE;
  671. pBlk->nCurrBlkSize = 0;
  672. pBlk->aBlkBuf = (CER_BLK_BUF *)EncMemAlloc(enc, MAX_INIT_BLK_SIZE * sizeof(CER_BLK_BUF));
  673. if (NULL != pBlk->aBlkBuf)
  674. {
  675. *ppBlk_ = (void *) pBlk;
  676. return 1;
  677. }
  678. EncMemFree(enc, pBlk);
  679. }
  680. return 0;
  681. }
  682. int ASN1CEREncNewBlkElement(void *pBlk_, ASN1encoding_t *enc2)
  683. {
  684. CER_BLOCK *pBlk = (CER_BLOCK *) pBlk_;
  685. if (NULL == pBlk->encSecondary)
  686. {
  687. if (ASN1_SUCCESS == ASN1_CreateEncoder(pBlk->encPrimary->module,
  688. &(pBlk->encSecondary),
  689. NULL, 0, pBlk->encPrimary))
  690. {
  691. pBlk->encSecondary->eRule = pBlk->encPrimary->eRule;
  692. *enc2 = pBlk->encSecondary;
  693. return 1;
  694. }
  695. }
  696. else
  697. {
  698. ASN1INTERNencoding_t e = (ASN1INTERNencoding_t) (*enc2 = pBlk->encSecondary);
  699. ZeroMemory(e, sizeof(*e));
  700. e->info.magic = MAGIC_ENCODER;
  701. // e->info.err = ASN1_SUCCESS;
  702. // e->info.pos = e->info.buf = NULL;
  703. // e->info.size = e->info.len = e->info.bit = 0;
  704. // e->info.dwFlags = 0;
  705. e->info.module = pBlk->encPrimary->module;
  706. e->info.eRule = pBlk->encPrimary->eRule;
  707. ((ASN1INTERNencoding_t) pBlk->encPrimary)->child = e;
  708. e->parent = (ASN1INTERNencoding_t) pBlk->encPrimary;
  709. // e->child = NULL;
  710. // e->mem = NULL;
  711. // e->memlength = 0;
  712. // e->memsize = 0;
  713. // e->epi = NULL;
  714. // e->epilength = 0;
  715. // e->episize = 0;
  716. // e->csi = NULL;
  717. // e->csilength = 0;
  718. // e->csisize = 0;
  719. if (ASN1BEREncCheck((ASN1encoding_t) e, 1))
  720. {
  721. // lonchanc: make sure the first byte is zeroed out, which
  722. // is required for h245.
  723. e->info.buf[0] = '\0';
  724. return 1;
  725. }
  726. }
  727. *enc2 = NULL;
  728. return 0;
  729. }
  730. int ASN1CEREncFlushBlkElement(void *pBlk_)
  731. {
  732. CER_BLOCK *pBlk = (CER_BLOCK *) pBlk_;
  733. ASN1encoding_t enc = pBlk->encSecondary;
  734. ASN1uint32_t i;
  735. if (ASN1BEREncFlush(enc))
  736. {
  737. // make sure we have enough space...
  738. if (pBlk->nCurrBlkSize >= pBlk->nMaxBlkSize)
  739. {
  740. CER_BLK_BUF *aBlkBuf = (CER_BLK_BUF *)EncMemAlloc(pBlk->encPrimary, (pBlk->nMaxBlkSize << 1) * sizeof(CER_BLK_BUF));
  741. if (NULL != aBlkBuf)
  742. {
  743. CopyMemory(aBlkBuf, pBlk->aBlkBuf, pBlk->nMaxBlkSize * sizeof(CER_BLK_BUF));
  744. EncMemFree(pBlk->encPrimary, pBlk->aBlkBuf);
  745. pBlk->aBlkBuf = aBlkBuf;
  746. pBlk->nMaxBlkSize <<= 1;
  747. }
  748. else
  749. {
  750. return 0;
  751. }
  752. }
  753. if (pBlk->encPrimary->eRule & (ASN1_BER_RULE_DER | ASN1_BER_RULE_CER))
  754. {
  755. // we need to sort these octet strings
  756. for (i = 0; i < pBlk->nCurrBlkSize; i++)
  757. {
  758. if (0 >= My_memcmp(enc->buf, enc->len, pBlk->aBlkBuf[i].pBuf, pBlk->aBlkBuf[i].cbBufSize))
  759. {
  760. ASN1uint32_t cnt = pBlk->nCurrBlkSize - i;
  761. ASN1uint32_t j;
  762. for (j = pBlk->nCurrBlkSize; cnt--; j--)
  763. {
  764. pBlk->aBlkBuf[j] = pBlk->aBlkBuf[j-1];
  765. }
  766. // i is the place to hold the new one
  767. break;
  768. }
  769. }
  770. }
  771. else
  772. {
  773. EncAssert(enc, ASN1_BER_RULE_BER == pBlk->encPrimary->eRule);
  774. i = pBlk->nCurrBlkSize;
  775. }
  776. // remeber the new one.
  777. pBlk->aBlkBuf[i].pBuf = enc->buf;
  778. pBlk->aBlkBuf[i].cbBufSize = enc->len;
  779. pBlk->nCurrBlkSize++;
  780. // clean up the encoder structure
  781. enc->buf = enc->pos = NULL;
  782. enc->size = enc->len = 0;
  783. return 1;
  784. }
  785. return 0;
  786. }
  787. int ASN1CEREncEndBlk(void *pBlk_)
  788. {
  789. CER_BLOCK *pBlk = (CER_BLOCK *) pBlk_;
  790. ASN1encoding_t enc = pBlk->encPrimary;
  791. ASN1uint32_t cbTotalSize = 0;
  792. ASN1uint32_t i;
  793. int fRet = 0;
  794. // calculate the total size for all the buffers.
  795. for (i = 0; i < pBlk->nCurrBlkSize; i++)
  796. {
  797. cbTotalSize += pBlk->aBlkBuf[i].cbBufSize;
  798. }
  799. if (ASN1BEREncCheck(enc, cbTotalSize))
  800. {
  801. for (i = 0; i < pBlk->nCurrBlkSize; i++)
  802. {
  803. ASN1uint32_t cbBufSize = pBlk->aBlkBuf[i].cbBufSize;
  804. CopyMemory(enc->pos, pBlk->aBlkBuf[i].pBuf, cbBufSize);
  805. enc->pos += cbBufSize;
  806. }
  807. fRet = 1;
  808. }
  809. // free these block buffers.
  810. for (i = 0; i < pBlk->nCurrBlkSize; i++)
  811. {
  812. EncMemFree(enc, pBlk->aBlkBuf[i].pBuf);
  813. }
  814. // free the block buffer array
  815. EncMemFree(enc, pBlk->aBlkBuf);
  816. // free the secondary encoder structure
  817. ASN1_CloseEncoder(pBlk->encSecondary);
  818. // free the block structure itself.
  819. EncMemFree(enc, pBlk);
  820. return fRet;
  821. }
  822. #endif // ENABLE_BER
  823. /* encode explicit tag */
  824. int ASN1BEREncExplicitTag(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t *pnLenOff)
  825. {
  826. /* encode tag */
  827. if (ASN1BEREncTag(enc, tag | 0x20000000))
  828. {
  829. /* encode infinite length */
  830. if (ASN1BEREncCheck(enc, 1))
  831. {
  832. if (ASN1_BER_RULE_CER != enc->eRule)
  833. {
  834. // BER and DER always use definite length form.
  835. /* return the place to hold the length */
  836. *pnLenOff = (ASN1uint32_t) (enc->pos++ - enc->buf);
  837. }
  838. else
  839. {
  840. // CER sub-rule always use indefinite length form.
  841. *enc->pos++ = 0x80;
  842. *pnLenOff = 0;
  843. }
  844. return 1;
  845. }
  846. }
  847. return 0;
  848. }
  849. /* encode definite length */
  850. int ASN1BEREncEndOfContents(ASN1encoding_t enc, ASN1uint32_t nLenOff)
  851. {
  852. if (ASN1_BER_RULE_CER != enc->eRule)
  853. {
  854. ASN1octet_t *pbLen = enc->buf + nLenOff;
  855. ASN1uint32_t len = (ASN1uint32_t) (enc->pos - pbLen - 1);
  856. ASN1uint32_t cbLength = _BERGetLength(len, c_LengthTable, ARRAY_SIZE(c_LengthTable));
  857. ASN1uint32_t i;
  858. if (cbLength == 1)
  859. {
  860. *pbLen = (ASN1octet_t) len;
  861. return 1;
  862. }
  863. // we have to move the octets upward by cbLength-1
  864. // --cbLength;
  865. if (ASN1BEREncCheck(enc, cbLength-1))
  866. {
  867. // update pbLen because enc->buf may change due to realloc.
  868. pbLen = enc->buf + nLenOff;
  869. // move memory
  870. MoveMemory(pbLen + cbLength, pbLen + 1, len);
  871. // put the length
  872. enc->pos = pbLen;
  873. _BERPutLength(enc, len, cbLength);
  874. EncAssert(enc, enc->pos == pbLen + cbLength);
  875. // set up new position pointer.
  876. // now enc->pos is at the beginning of contents.
  877. enc->pos += len;
  878. return 1;
  879. }
  880. }
  881. else
  882. {
  883. EncAssert(enc, 0 == nLenOff);
  884. if (ASN1BEREncCheck(enc, 2))
  885. {
  886. *enc->pos++ = 0;
  887. *enc->pos++ = 0;
  888. return 1;
  889. }
  890. }
  891. return 0;
  892. }
  893. // The following is for CryptoAPI
  894. #ifdef ENABLE_BER
  895. #include <stdlib.h>
  896. // max num of octets, ceiling of 64 / 7, is 10
  897. #define MAX_BYTES_PER_NODE 10
  898. ASN1uint32_t _BEREncOidNode64(ASN1encoding_t enc, unsigned __int64 n64, ASN1octet_t *pOut)
  899. {
  900. ASN1uint32_t Low32, i, cb;
  901. ASN1octet_t aLittleEndian[MAX_BYTES_PER_NODE];
  902. ZeroMemory(aLittleEndian, sizeof(aLittleEndian));
  903. for (i = 0; n64 != 0; i++)
  904. {
  905. Low32 = *(ASN1uint32_t *) &n64;
  906. aLittleEndian[i] = (ASN1octet_t) (Low32 & 0x7f);
  907. n64 = Int64ShrlMod32(n64, 7);
  908. }
  909. cb = i ? i : 1; // at least one byte for zero value
  910. EncAssert(enc, cb <= MAX_BYTES_PER_NODE);
  911. for (i = 0; i < cb; i++)
  912. {
  913. EncAssert(enc, 0 == (0x80 & aLittleEndian[cb - i - 1]));
  914. *pOut++ = (ASN1octet_t) (0x80 | aLittleEndian[cb - i - 1]);
  915. }
  916. *(pOut-1) &= 0x7f;
  917. return cb;
  918. }
  919. int ASN1BERDotVal2Eoid(ASN1encoding_t enc, char *pszDotVal, ASN1encodedOID_t *pOut)
  920. {
  921. ASN1uint32_t cNodes, cbMaxSize, cb1, cb2;
  922. char *psz;
  923. // calculate how many nodes, at least 1.
  924. for (cNodes = 0, psz = pszDotVal; NULL != psz; cNodes++)
  925. {
  926. psz = strchr(psz, '.');
  927. if (psz)
  928. {
  929. psz++;
  930. }
  931. }
  932. // calculate how many bytes should be allocated
  933. cb1 = My_lstrlenA(pszDotVal);
  934. cb2 = cNodes * MAX_BYTES_PER_NODE;
  935. cbMaxSize = (cb1 > cb2) ? cb1 : cb2;
  936. // allocate buffer
  937. pOut->length = 0; // safety net
  938. pOut->value = (ASN1octet_t *) EncMemAlloc(enc, cbMaxSize);
  939. if (pOut->value)
  940. {
  941. ASN1octet_t *p = pOut->value;
  942. ASN1uint32_t i;
  943. unsigned __int64 n64;
  944. psz = pszDotVal;
  945. for (i = 0; i < cNodes; i++)
  946. {
  947. EncAssert(enc, NULL != psz);
  948. n64 = (unsigned __int64) _atoi64(psz);
  949. psz = strchr(psz, '.') + 1;
  950. if (0 == i && cNodes > 1)
  951. {
  952. i++;
  953. n64 = n64 * 40 + (unsigned __int64) _atoi64(psz);
  954. psz = strchr(psz, '.') + 1;
  955. }
  956. p += _BEREncOidNode64(enc, n64, p);
  957. }
  958. pOut->length = (ASN1uint16_t) (p - pOut->value);
  959. EncAssert(enc, pOut->length <= cbMaxSize);
  960. return 1;
  961. }
  962. pOut->length = 0;
  963. return 0;
  964. }
  965. ASN1uint32_t _BERDecOidNode64(unsigned __int64 *pn64, ASN1octet_t *pIn)
  966. {
  967. ASN1uint32_t c;
  968. *pn64 = 0;
  969. for (c = 1; TRUE; c++)
  970. {
  971. *pn64 = Int64ShllMod32(*pn64, 7) + (unsigned __int64) (*pIn & 0x7f);
  972. if (!(*pIn++ & 0x80))
  973. {
  974. return c;
  975. }
  976. }
  977. return 0;
  978. }
  979. int ASN1BEREoid2DotVal(ASN1decoding_t dec, ASN1encodedOID_t *pIn, char **ppszDotVal)
  980. {
  981. ASN1octet_t *p;
  982. ASN1uint32_t i, cNodes, cb, n32, nLen, nChars;
  983. unsigned __int64 n64;
  984. char *psz;
  985. char szBuf[256+64]; // should be large enough
  986. // null out return value
  987. *ppszDotVal = NULL;
  988. if (NULL == dec)
  989. {
  990. return 0;
  991. }
  992. // calculate how many nodes
  993. cNodes = 0;
  994. for (p = pIn->value, i = 0; i < pIn->length; p++, i++)
  995. {
  996. if (!(*p & 0x80))
  997. {
  998. cNodes++;
  999. }
  1000. }
  1001. if (cNodes++) // first encoded node consists of two nodes
  1002. {
  1003. // decode nodes one by one
  1004. psz = &szBuf[0];
  1005. // BUG 773871 ESCROW: ETA 2/6: Security issue with Msasn1.dll
  1006. // keep a count of how many characters we write to the psz buffer.
  1007. // if this count exceeds 256, then we have a buffer overrun.
  1008. // note that we've padded our buffer with an extra 64 bytes, which
  1009. // allows us to detect the overrun safely. The below code can't
  1010. // overrun our buffer by more than ~22 bytes.
  1011. nChars = 0;
  1012. p = pIn->value;
  1013. for (i = 0; i < cNodes; i++)
  1014. {
  1015. p += _BERDecOidNode64(&n64, p);
  1016. if (!i)
  1017. { // first node
  1018. n32 = (ASN1uint32_t) (n64 / 40);
  1019. if (n32 > 2)
  1020. {
  1021. n32 = 2;
  1022. }
  1023. n64 -= (unsigned __int64) (40 * n32);
  1024. i++; // first encoded node actually consists of two nodes
  1025. _ultoa(n32, psz, 10);
  1026. nLen = lstrlenA(psz);
  1027. psz += nLen;
  1028. *psz++ = '.';
  1029. nChars += nLen+1;
  1030. }
  1031. _ui64toa(n64, psz, 10);
  1032. nLen = lstrlenA(psz);
  1033. psz += nLen;
  1034. *psz++ = '.';
  1035. nChars += nLen+1;
  1036. if (nChars > 256)
  1037. {
  1038. // This oid is too long for our buffer. Bail out now.
  1039. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  1040. return 0;
  1041. }
  1042. }
  1043. DecAssert(dec, psz > &szBuf[0]);
  1044. *(psz-1) = '\0';
  1045. // duplicate the string
  1046. cb = (ASN1uint32_t) (psz - &szBuf[0]);
  1047. *ppszDotVal = (char *) DecMemAlloc(dec, cb);
  1048. if (*ppszDotVal)
  1049. {
  1050. CopyMemory(*ppszDotVal, &szBuf[0], cb);
  1051. return 1;
  1052. }
  1053. }
  1054. return 0;
  1055. }
  1056. /* encode an object identifier value */
  1057. int ASN1BEREncEoid(ASN1encoding_t enc, ASN1uint32_t tag, ASN1encodedOID_t *val)
  1058. {
  1059. /* encode tag */
  1060. if (ASN1BEREncTag(enc, tag))
  1061. {
  1062. /* encode length */
  1063. int rc = ASN1BEREncLength(enc, val->length);
  1064. if (rc)
  1065. {
  1066. /* copy value */
  1067. CopyMemory(enc->pos, val->value, val->length);
  1068. enc->pos += val->length;
  1069. }
  1070. return rc;
  1071. }
  1072. return 0;
  1073. }
  1074. /* decode object identifier value */
  1075. int ASN1BERDecEoid(ASN1decoding_t dec, ASN1uint32_t tag, ASN1encodedOID_t *val)
  1076. {
  1077. val->length = 0; // safety net
  1078. if (ASN1BERDecTag(dec, tag, NULL))
  1079. {
  1080. ASN1uint32_t len;
  1081. if (ASN1BERDecLength(dec, &len, NULL))
  1082. {
  1083. val->length = (ASN1uint16_t) len;
  1084. if (len)
  1085. {
  1086. val->value = (ASN1octet_t *) DecMemAlloc(dec, len);
  1087. if (val->value)
  1088. {
  1089. CopyMemory(val->value, dec->pos, len);
  1090. dec->pos += len;
  1091. return 1;
  1092. }
  1093. }
  1094. else
  1095. {
  1096. val->value = NULL;
  1097. return 1;
  1098. }
  1099. }
  1100. }
  1101. return 0;
  1102. }
  1103. void ASN1BEREoid_free(ASN1encodedOID_t *val)
  1104. {
  1105. if (val)
  1106. {
  1107. MemFree(val->value);
  1108. }
  1109. }
  1110. #endif // ENABLE_BER