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.

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