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.

1310 lines
37 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. #include "cintern.h"
  5. #ifdef TEST_CODER
  6. typedef struct ASN1testcoder_s
  7. {
  8. struct ASN1INTERNencoding_s e;
  9. struct ASN1INTERNdecoding_s d;
  10. } *ASN1testcoder_t;
  11. #define ASN1_TEST_CODER_SIZE (sizeof(struct ASN1testcoder_s))
  12. int TestEnc_InitCoder(ASN1INTERNencoding_t e, ASN1module_t mod);
  13. int TestDec_InitCoder(ASN1INTERNdecoding_t d, ASN1module_t mod);
  14. int TestEnc_Compare(ASN1INTERNencoding_t e, ASN1uint32_t id, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize);
  15. int TestDec_Compare(ASN1INTERNdecoding_t d, ASN1uint32_t id, void *valref, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize);
  16. #else
  17. #define ASN1_TEST_CODER_SIZE 0
  18. #endif
  19. /* init an ASN1encoding_t */
  20. ASN1error_e ASN1_CreateEncoder
  21. (
  22. ASN1module_t mod,
  23. ASN1encoding_t *enc,
  24. ASN1octet_t *pbBuf,
  25. ASN1uint32_t cbBufSize,
  26. ASN1encoding_t pParent
  27. )
  28. {
  29. if (NULL != mod && NULL != enc)
  30. {
  31. ASN1INTERNencoding_t e;
  32. *enc = NULL;
  33. /* construct ASN1encoding_t */
  34. e = (ASN1INTERNencoding_t)MemAlloc(sizeof(*e) + ASN1_TEST_CODER_SIZE, mod->nModuleName);
  35. if (NULL != e)
  36. {
  37. ZeroMemory(e, sizeof(*e) + ASN1_TEST_CODER_SIZE);
  38. e->info.magic = MAGIC_ENCODER;
  39. e->info.err = ASN1_SUCCESS;
  40. // e->info.pos = e->info.buf = NULL;
  41. // e->info.size = e->info.len = e->info.bit = 0;
  42. e->info.dwFlags = mod->dwFlags;
  43. e->info.module = mod;
  44. // e->child = NULL;
  45. /* set buffer if given */
  46. if (NULL != pbBuf && cbBufSize)
  47. {
  48. e->info.dwFlags |= ASN1ENCODE_SETBUFFER;
  49. e->info.pos = e->info.buf = pbBuf;
  50. e->info.size = cbBufSize;
  51. }
  52. /* set parent if parented, otherwise, initialized to itself */
  53. if (NULL != pParent)
  54. {
  55. e->parent = (ASN1INTERNencoding_t) pParent;
  56. e->info.eRule = pParent->eRule;
  57. }
  58. else
  59. {
  60. e->parent = e;
  61. e->info.eRule = mod->eRule;
  62. }
  63. // e->mem = NULL;
  64. // e->memlength = 0;
  65. // e->memsize = 0;
  66. // e->epi = NULL;
  67. // e->epilength = 0;
  68. // e->episize = 0;
  69. // e->csi = NULL;
  70. // e->csilength = 0;
  71. // e->csisize = 0;
  72. if (! (e->info.dwFlags & ASN1ENCODE_SETBUFFER) && (NULL != pParent))
  73. {
  74. // lonchanc: make sure we have a minimum 256 bytes available.
  75. BOOL fRet = FALSE;
  76. if (ASN1_PER_RULE & e->info.eRule)
  77. {
  78. // this is required for h245.
  79. fRet = ASN1PEREncCheck((ASN1encoding_t) e, 1);
  80. }
  81. #ifdef ENABLE_BER
  82. else
  83. if (ASN1_BER_RULE & e->info.eRule)
  84. {
  85. // this is required for h245.
  86. fRet = ASN1BEREncCheck((ASN1encoding_t) e, 1);
  87. }
  88. #endif // ENABLE_BER
  89. else
  90. {
  91. EncAssert((ASN1encoding_t) e, 0);
  92. MemFree(e);
  93. return ASN1_ERR_RULE;
  94. }
  95. if (fRet)
  96. {
  97. // lonchanc: make sure the first byte is zeroed out, which
  98. // is required for h245.
  99. e->info.buf[0] = '\0';
  100. }
  101. else
  102. {
  103. MemFree(e);
  104. return ASN1_ERR_MEMORY;
  105. }
  106. }
  107. #if defined(TEST_CODER) && defined(_DEBUG)
  108. TestEnc_InitCoder(e, mod);
  109. #endif // defined(TEST_CODER) && defined(_DEBUG)
  110. if (NULL != pParent)
  111. {
  112. EncAssert((ASN1encoding_t) e, NULL == ((ASN1INTERNencoding_t) pParent)->child);
  113. ((ASN1INTERNencoding_t) pParent)->child = e;
  114. }
  115. *enc = (ASN1encoding_t) e;
  116. return ASN1_SUCCESS;
  117. }
  118. else
  119. {
  120. return ASN1_ERR_MEMORY;
  121. }
  122. }
  123. return ASN1_ERR_BADARGS;
  124. }
  125. /* encode a value */
  126. ASN1error_e ASN1_Encode
  127. (
  128. ASN1encoding_t enc,
  129. void *value,
  130. ASN1uint32_t id,
  131. ASN1uint32_t flags,
  132. ASN1octet_t *pbBuf,
  133. ASN1uint32_t cbBufSize
  134. )
  135. {
  136. if (NULL != enc)
  137. {
  138. ASN1INTERNencoding_t e = (ASN1INTERNencoding_t)enc;
  139. /* check magic numbers */
  140. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  141. /* clear error */
  142. ASN1EncSetError(enc, ASN1_SUCCESS);
  143. /* new buffer given? */
  144. if (flags & ASN1ENCODE_SETBUFFER)
  145. {
  146. e->info.dwFlags |= ASN1ENCODE_SETBUFFER;
  147. enc->pos = enc->buf = pbBuf;
  148. enc->size = cbBufSize;
  149. enc->len = enc->bit = 0;
  150. }
  151. /* use a new buffer? */
  152. else if ((e->info.dwFlags | flags) & ASN1ENCODE_ALLOCATEBUFFER)
  153. {
  154. e->info.dwFlags &= ~ASN1ENCODE_SETBUFFER;
  155. enc->pos = enc->buf = NULL;
  156. enc->size = enc->len = enc->bit = 0;
  157. }
  158. /* reuse buffer? */
  159. else if ((flags & ASN1ENCODE_REUSEBUFFER) || !((e->info.dwFlags | flags) & ASN1ENCODE_APPEND))
  160. {
  161. EncAssert(enc, NULL != enc->buf);
  162. enc->pos = enc->buf;
  163. enc->bit = enc->len = 0;
  164. }
  165. /* otherwise append to buffer */
  166. /* check id number */
  167. if (id < enc->module->cPDUs)
  168. {
  169. if (ASN1_PER_RULE & enc->eRule)
  170. {
  171. /* encode value */
  172. ASN1PerEncFun_t pfnPER;
  173. if (NULL != (pfnPER = enc->module->PER.apfnEncoder[id]))
  174. {
  175. if ((*pfnPER)(enc, value))
  176. {
  177. ASN1PEREncFlush(enc);
  178. }
  179. else
  180. {
  181. // the error code must be an error
  182. if (ASN1_SUCCEEDED(e->parent->info.err))
  183. {
  184. // cannot return here immediately because we need to do cleanup
  185. ASN1EncSetError(enc, ASN1_ERR_CORRUPT);
  186. }
  187. }
  188. }
  189. else
  190. {
  191. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  192. }
  193. }
  194. #ifdef ENABLE_BER
  195. else
  196. if (ASN1_BER_RULE & enc->eRule)
  197. {
  198. /* encode value */
  199. ASN1BerEncFun_t pfnBER;
  200. if (NULL != (pfnBER = enc->module->BER.apfnEncoder[id]))
  201. {
  202. if ((*pfnBER)(enc, 0, value)) // lonchanc: tag is 0 to make it compiled
  203. {
  204. ASN1BEREncFlush(enc);
  205. }
  206. else
  207. {
  208. // the error code must be an error
  209. if (ASN1_SUCCEEDED(e->parent->info.err))
  210. {
  211. // cannot return here immediately because we need to do cleanup
  212. ASN1EncSetError(enc, ASN1_ERR_CORRUPT);
  213. }
  214. }
  215. }
  216. else
  217. {
  218. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  219. }
  220. }
  221. #endif // ENABLE_BER
  222. else
  223. {
  224. return ASN1EncSetError(enc, ASN1_ERR_RULE);
  225. }
  226. /* call abort/done function for non-parented encoding stream */
  227. if (ASN1_SUCCEEDED(e->parent->info.err))
  228. {
  229. // not parented
  230. if (e == e->parent)
  231. {
  232. #if defined(TEST_CODER) && defined(_DEBUG)
  233. if (ASN1_PER_RULE & enc->eRule)
  234. {
  235. if (! TestEnc_Compare(e, id, enc->buf + enc->cbExtraHeader, enc->len - enc->cbExtraHeader))
  236. {
  237. MyDebugBreak();
  238. }
  239. }
  240. #ifdef ENABLE_BER
  241. else
  242. if (ASN1_BER_RULE & enc->eRule)
  243. {
  244. if (! TestEnc_Compare(e, id, enc->buf + enc->cbExtraHeader, enc->len - enc->cbExtraHeader))
  245. {
  246. MyDebugBreak();
  247. }
  248. }
  249. #endif // ENABLE_BER
  250. #endif
  251. ASN1EncDone(enc);
  252. }
  253. }
  254. else
  255. {
  256. ASN1INTERNencoding_t child, child2;
  257. // not parented
  258. if (e == e->parent)
  259. {
  260. ASN1EncAbort(enc);
  261. }
  262. // clean up...
  263. if ((e->info.dwFlags | flags) & ASN1ENCODE_ALLOCATEBUFFER)
  264. {
  265. ASN1_FreeEncoded(enc, enc->buf);
  266. enc->pos = enc->buf = NULL;
  267. enc->size = enc->len = enc->bit = 0;
  268. }
  269. for (child = e->child; child; child = child2)
  270. {
  271. child2 = child->child;
  272. // make sure it does not touch its parent which may already be freed
  273. child->parent = child;
  274. ASN1_CloseEncoder2((ASN1encoding_t) child);
  275. }
  276. e->child = NULL;
  277. }
  278. /* return error code */
  279. return e->parent->info.err;
  280. }
  281. else
  282. {
  283. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  284. }
  285. }
  286. return ASN1_ERR_BADARGS;
  287. }
  288. /* control function for encoding */
  289. ASN1error_e ASN1_SetEncoderOption
  290. (
  291. ASN1encoding_t enc,
  292. ASN1optionparam_t *pOptParam
  293. )
  294. {
  295. if (NULL != enc && NULL != pOptParam)
  296. {
  297. ASN1INTERNencoding_t e = (ASN1INTERNencoding_t)enc;
  298. ASN1error_e rc = ASN1_SUCCESS;
  299. /* check magic number */
  300. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  301. switch (pOptParam->eOption)
  302. {
  303. case ASN1OPT_CHANGE_RULE:
  304. enc->eRule = pOptParam->eRule;
  305. break;
  306. case ASN1OPT_NOT_REUSE_BUFFER:
  307. e->info.dwFlags &= ~ASN1ENCODE_SETBUFFER;
  308. enc->buf = enc->pos = NULL;
  309. enc->size = enc->bit = enc->len = 0;
  310. break;
  311. case ASN1OPT_REWIND_BUFFER:
  312. enc->pos = enc->buf;
  313. enc->bit = enc->len = 0;
  314. break;
  315. default:
  316. rc = ASN1_ERR_BADARGS;
  317. break;
  318. }
  319. return ASN1EncSetError(enc, rc);
  320. }
  321. return ASN1_ERR_BADARGS;
  322. }
  323. ASN1error_e ASN1_GetEncoderOption
  324. (
  325. ASN1encoding_t enc,
  326. ASN1optionparam_t *pOptParam
  327. )
  328. {
  329. if (NULL != enc && NULL != pOptParam)
  330. {
  331. ASN1INTERNencoding_t e = (ASN1INTERNencoding_t)enc;
  332. ASN1error_e rc = ASN1_SUCCESS;
  333. /* check magic number */
  334. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  335. switch (pOptParam->eOption)
  336. {
  337. case ASN1OPT_GET_RULE:
  338. pOptParam->eRule = enc->eRule;
  339. break;
  340. default:
  341. rc = ASN1_ERR_BADARGS;
  342. break;
  343. }
  344. return ASN1EncSetError(enc, rc);
  345. }
  346. return ASN1_ERR_BADARGS;
  347. }
  348. /* destroy encoding stream */
  349. void ASN1_CloseEncoder
  350. (
  351. ASN1encoding_t enc
  352. )
  353. {
  354. if (NULL != enc)
  355. {
  356. ASN1INTERNencoding_t e = (ASN1INTERNencoding_t)enc;
  357. /* check magic number */
  358. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  359. if (e != e->parent)
  360. {
  361. EncAssert(enc, e == e->parent->child);
  362. e->parent->child = NULL;
  363. }
  364. /* free encoding stream */
  365. MemFree(e);
  366. }
  367. }
  368. /* destroy encoding stream */
  369. void ASN1_CloseEncoder2
  370. (
  371. ASN1encoding_t enc
  372. )
  373. {
  374. if (NULL != enc)
  375. {
  376. /* check magic number */
  377. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  378. EncMemFree(enc, enc->buf);
  379. ASN1_CloseEncoder(enc);
  380. }
  381. }
  382. /* init an ASN1decoding_t */
  383. ASN1error_e ASN1_CreateDecoder
  384. (
  385. ASN1module_t mod,
  386. ASN1decoding_t *dec,
  387. ASN1octet_t *pbBuf,
  388. ASN1uint32_t cbBufSize,
  389. ASN1decoding_t pParent
  390. )
  391. {
  392. return ASN1_CreateDecoderEx(mod, dec, pbBuf, cbBufSize, pParent, ASN1FLAGS_NONE);
  393. }
  394. ASN1error_e ASN1_CreateDecoderEx
  395. (
  396. ASN1module_t mod,
  397. ASN1decoding_t *dec,
  398. ASN1octet_t *pbBuf,
  399. ASN1uint32_t cbBufSize,
  400. ASN1decoding_t pParent,
  401. ASN1uint32_t dwFlags
  402. )
  403. {
  404. if (NULL != mod && NULL != dec)
  405. {
  406. ASN1INTERNdecoding_t d;
  407. *dec = NULL;
  408. /* construct ASN1decoding_t */
  409. d = (ASN1INTERNdecoding_t)MemAlloc(sizeof(*d) + ASN1_TEST_CODER_SIZE, mod->nModuleName);
  410. if (NULL != d)
  411. {
  412. ZeroMemory(d, sizeof(*d) + ASN1_TEST_CODER_SIZE);
  413. d->info.magic = MAGIC_DECODER;
  414. d->info.err = ASN1_SUCCESS;
  415. d->info.dwFlags = mod->dwFlags;
  416. d->info.module = mod;
  417. // d->child = NULL;
  418. /* set parent if parented */
  419. if (NULL != pParent)
  420. {
  421. DecAssert((ASN1decoding_t) d, NULL == ((ASN1INTERNdecoding_t) pParent)->child);
  422. ((ASN1INTERNdecoding_t) pParent)->child = d;
  423. d->parent = (ASN1INTERNdecoding_t) pParent;
  424. d->info.eRule = pParent->eRule;
  425. }
  426. else
  427. /* initialize otherwise */
  428. {
  429. d->parent = d;
  430. d->info.eRule = mod->eRule;
  431. }
  432. /* set buffer if given */
  433. // lonchanc: it is ok to have a zero buffer size here
  434. if (NULL != pbBuf)
  435. {
  436. d->info.dwFlags |= ASN1DECODE_SETBUFFER;
  437. d->info.buf = d->info.pos = pbBuf;
  438. d->info.size = cbBufSize;
  439. // d->info.len = d->info.bit = 0;
  440. if ((dwFlags & ASN1DECODE_AUTOFREEBUFFER)
  441. && !d->parent->fExtBuf)
  442. {
  443. // dbarlow: It's possible the buffer isn't really
  444. // allocated, but instead came from the
  445. // parent's Extension buffer.
  446. d->info.dwFlags |= ASN1DECODE_AUTOFREEBUFFER;
  447. }
  448. }
  449. // else
  450. // {
  451. // d->info.buf = d->info.pos = NULL;
  452. // d->info.size = d->info.len = d->info.bit = 0;
  453. // }
  454. // d->mem = NULL;
  455. // d->memlength = 0;
  456. // d->memsize = 0;
  457. // d->epi = NULL;
  458. // d->epilength = 0;
  459. // d->episize = 0;
  460. // d->csi = NULL;
  461. // d->csilength = 0;
  462. // d->csisize = 0;
  463. #if defined(TEST_CODER) && defined(_DEBUG)
  464. TestDec_InitCoder(d, mod);
  465. #endif // defined(TEST_CODER) && defined(_DEBUG)
  466. *dec = (ASN1decoding_t) d;
  467. return ASN1_SUCCESS;
  468. }
  469. else
  470. {
  471. if (dwFlags & ASN1DECODE_AUTOFREEBUFFER)
  472. {
  473. // dbarlow: It's possible the buffer isn't really
  474. // allocated, but instead came from the
  475. // parent's Extension buffer.
  476. d = (ASN1INTERNdecoding_t)pParent;
  477. if ((NULL == d) || !d->fExtBuf)
  478. {
  479. MemFree(pbBuf);
  480. }
  481. }
  482. return ASN1_ERR_MEMORY;
  483. }
  484. }
  485. return ASN1_ERR_BADARGS;
  486. }
  487. /* decode a value */
  488. ASN1error_e ASN1_Decode
  489. (
  490. ASN1decoding_t dec,
  491. void **valref,
  492. ASN1uint32_t id,
  493. ASN1uint32_t flags,
  494. ASN1octet_t *pbBuf,
  495. ASN1uint32_t cbBufSize
  496. )
  497. {
  498. ASN1error_e ReturnCode = ASN1_SUCCESS;
  499. if (NULL != dec && NULL != valref)
  500. {
  501. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  502. /* check magic numbers */
  503. DecAssert(dec, MAGIC_DECODER == dec->magic);
  504. /* clear error */
  505. ASN1DecSetError(dec, ASN1_SUCCESS);
  506. /* new buffer given? */
  507. *valref = NULL;
  508. if (flags & ASN1DECODE_SETBUFFER)
  509. {
  510. if (NULL != pbBuf && 0 != cbBufSize)
  511. {
  512. dec->pos = dec->buf = pbBuf;
  513. dec->size = cbBufSize;
  514. dec->bit = dec->len = 0;
  515. }
  516. else
  517. {
  518. ReturnCode = ASN1DecSetError(dec, ASN1_ERR_BADARGS);
  519. goto ErrorExit;
  520. }
  521. }
  522. /* rewind buffer? */
  523. else if ((flags & ASN1DECODE_REWINDBUFFER) ||
  524. !((d->info.dwFlags | flags ) & ASN1DECODE_APPENDED))
  525. {
  526. dec->pos = dec->buf;
  527. dec->bit = dec->len = 0;
  528. }
  529. /* otherwise continue reading from last buffer */
  530. /* check id number */
  531. if (id < dec->module->cPDUs)
  532. {
  533. ASN1uint32_t cbTopLevelStruct;
  534. /* clear length of linear buffer required */
  535. d->cbLinearBufSize = 0;
  536. /* double check for the availability of destination buffer */
  537. if (d->lpOrigExtBuf == NULL || d->cbOrigExtBufSize == 0)
  538. {
  539. d->fExtBuf = FALSE;
  540. }
  541. cbTopLevelStruct = dec->module->acbStructSize[id];
  542. if (NULL != (*valref = DecMemAlloc(dec, cbTopLevelStruct)))
  543. {
  544. if (ASN1_PER_RULE & dec->eRule)
  545. {
  546. ASN1PerDecFun_t pfnPER;
  547. /* decode value */
  548. if (NULL != (pfnPER = dec->module->PER.apfnDecoder[id]))
  549. {
  550. if ((*pfnPER)(dec, *valref))
  551. {
  552. ASN1PERDecFlush(dec);
  553. }
  554. else
  555. {
  556. // the error code must be an error
  557. if (ASN1_SUCCEEDED(d->parent->info.err))
  558. {
  559. // cannot return here immediately because we need to do cleanup
  560. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  561. }
  562. }
  563. }
  564. else
  565. {
  566. ReturnCode = ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  567. goto ErrorExit;
  568. }
  569. }
  570. #ifdef ENABLE_BER
  571. else
  572. if (ASN1_BER_RULE & dec->eRule)
  573. {
  574. ASN1BerDecFun_t pfnBER;
  575. /* decode value */
  576. if (NULL != (pfnBER = dec->module->BER.apfnDecoder[id]))
  577. {
  578. if ((*pfnBER)(dec, 0, *valref)) // lonchanc: tag is 0 to make it compiled
  579. {
  580. ASN1BERDecFlush(dec);
  581. }
  582. else
  583. {
  584. // the error code must be an error
  585. if (ASN1_SUCCEEDED(d->parent->info.err))
  586. {
  587. // cannot return here immediately because we need to do cleanup
  588. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  589. }
  590. }
  591. }
  592. else
  593. {
  594. ReturnCode = ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  595. goto ErrorExit;
  596. }
  597. }
  598. #endif // ENABLE_BER
  599. else
  600. {
  601. ReturnCode = ASN1DecSetError(dec, ASN1_ERR_RULE);
  602. goto ErrorExit;
  603. }
  604. /* call abort/done function for non-parented decoding stream */
  605. if (ASN1_SUCCEEDED(d->parent->info.err))
  606. {
  607. // not parented
  608. if (d == d->parent)
  609. {
  610. #if defined(TEST_CODER) && defined(_DEBUG)
  611. if (ASN1_PER_RULE & dec->eRule)
  612. {
  613. if (! TestDec_Compare(d, id, *valref, dec->buf, dec->len))
  614. {
  615. MyDebugBreak();
  616. }
  617. }
  618. #ifdef ENABLE_BER
  619. else
  620. if (ASN1_BER_RULE & dec->eRule)
  621. {
  622. if (! TestDec_Compare(d, id, *valref, dec->buf, dec->len))
  623. {
  624. MyDebugBreak();
  625. }
  626. }
  627. #endif // ENABLE_BER
  628. #endif
  629. ASN1DecDone(dec);
  630. }
  631. }
  632. else
  633. {
  634. ASN1INTERNdecoding_t child, child2;
  635. // not parented
  636. if (d == d->parent)
  637. {
  638. ASN1DecAbort(dec);
  639. }
  640. // clean up...
  641. for (child = d->child; child; child = child2)
  642. {
  643. child2 = child->child;
  644. // make sure it does not touch its parent which may already be freed
  645. child->parent = child;
  646. ASN1_CloseDecoder((ASN1decoding_t) child);
  647. }
  648. d->child = NULL;
  649. }
  650. /* return error code */
  651. ReturnCode = d->parent->info.err;
  652. goto ErrorExit;
  653. }
  654. else
  655. {
  656. ReturnCode = ASN1_ERR_MEMORY;
  657. goto ErrorExit;
  658. }
  659. }
  660. else
  661. {
  662. ReturnCode = ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  663. goto ErrorExit;
  664. }
  665. }
  666. ReturnCode = ASN1_ERR_BADARGS;
  667. ErrorExit:
  668. if (ASN1_FAILED(ReturnCode))
  669. {
  670. if (NULL != valref)
  671. {
  672. ASN1_FreeDecoded(dec ,*valref, id);
  673. *valref = NULL;
  674. }
  675. }
  676. return ReturnCode;
  677. }
  678. /* control function for decoding */
  679. ASN1error_e ASN1_SetDecoderOption
  680. (
  681. ASN1decoding_t dec,
  682. ASN1optionparam_t *pOptParam
  683. )
  684. {
  685. if (NULL != dec)
  686. {
  687. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  688. ASN1error_e rc = ASN1_SUCCESS;
  689. /* check magic number */
  690. DecAssert(dec, MAGIC_DECODER == dec->magic);
  691. switch (pOptParam->eOption)
  692. {
  693. case ASN1OPT_CHANGE_RULE:
  694. dec->eRule = pOptParam->eRule;
  695. break;
  696. case ASN1OPT_SET_DECODED_BUFFER:
  697. if (NULL != pOptParam->Buffer.pbBuf && 0 != pOptParam->Buffer.cbBufSize)
  698. {
  699. d->fExtBuf = TRUE;
  700. d->lpOrigExtBuf = pOptParam->Buffer.pbBuf;
  701. d->cbOrigExtBufSize = pOptParam->Buffer.cbBufSize;
  702. d->lpRemExtBuf = d->lpOrigExtBuf;
  703. d->cbRemExtBufSize = d->cbOrigExtBufSize;
  704. }
  705. else
  706. {
  707. rc = ASN1_ERR_BADARGS;
  708. }
  709. break;
  710. case ASN1OPT_DEL_DECODED_BUFFER:
  711. d->fExtBuf = FALSE;
  712. d->lpOrigExtBuf = NULL;
  713. d->cbOrigExtBufSize = 0;
  714. d->lpRemExtBuf = NULL;
  715. d->cbRemExtBufSize = 0;
  716. break;
  717. default:
  718. rc = ASN1_ERR_BADARGS;
  719. break;
  720. }
  721. return ASN1DecSetError(dec, rc);
  722. }
  723. return ASN1_ERR_BADARGS;
  724. }
  725. /* control function for decoding */
  726. ASN1error_e ASN1_GetDecoderOption
  727. (
  728. ASN1decoding_t dec,
  729. ASN1optionparam_t *pOptParam
  730. )
  731. {
  732. if (NULL != dec)
  733. {
  734. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  735. ASN1error_e rc = ASN1_SUCCESS;
  736. /* check magic number */
  737. DecAssert(dec, MAGIC_DECODER == dec->magic);
  738. switch (pOptParam->eOption)
  739. {
  740. case ASN1OPT_GET_RULE:
  741. pOptParam->eRule = dec->eRule;
  742. break;
  743. case ASN1OPT_GET_DECODED_BUFFER_SIZE:
  744. pOptParam->cbRequiredDecodedBufSize = d->cbLinearBufSize;
  745. break;
  746. default:
  747. rc = ASN1_ERR_BADARGS;
  748. break;
  749. }
  750. return ASN1DecSetError(dec, rc);
  751. }
  752. return ASN1_ERR_BADARGS;
  753. }
  754. /* destroy decoding stream */
  755. void ASN1_CloseDecoder
  756. (
  757. ASN1decoding_t dec
  758. )
  759. {
  760. if (NULL != dec)
  761. {
  762. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  763. /* check magic number */
  764. DecAssert(dec, MAGIC_DECODER == dec->magic);
  765. if (d != d->parent)
  766. {
  767. DecAssert(dec, d == d->parent->child);
  768. d->parent->child = NULL;
  769. }
  770. if ((NULL != d->info.buf)
  771. && (d->info.dwFlags & ASN1DECODE_AUTOFREEBUFFER))
  772. MemFree(d->info.buf);
  773. /* free decoding stream */
  774. MemFree(d);
  775. }
  776. }
  777. /* free an encoded value */
  778. void ASN1_FreeEncoded
  779. (
  780. ASN1encoding_t enc,
  781. void *val
  782. )
  783. {
  784. if (NULL != enc)
  785. {
  786. /* check magic number */
  787. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  788. EncMemFree(enc, val);
  789. }
  790. }
  791. /* free a unencoded value */
  792. void ASN1_FreeDecoded
  793. (
  794. ASN1decoding_t dec,
  795. void *val,
  796. ASN1uint32_t id
  797. )
  798. {
  799. if (NULL != dec)
  800. {
  801. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  802. /* check magic number */
  803. DecAssert(dec, MAGIC_DECODER == dec->magic);
  804. // same behavior of LocalFree
  805. if (val != NULL)
  806. {
  807. if (id != ASN1DECFREE_NON_PDU_ID)
  808. {
  809. ASN1FreeFun_t pfnFreeMemory;
  810. /* free value */
  811. if (id < dec->module->cPDUs)
  812. {
  813. if (NULL != (pfnFreeMemory = dec->module->apfnFreeMemory[id]))
  814. {
  815. (*pfnFreeMemory)(val);
  816. }
  817. }
  818. else
  819. {
  820. return;
  821. }
  822. }
  823. // free the top-level structure
  824. DecMemFree(dec, val);
  825. }
  826. }
  827. }
  828. ASN1module_t ASN1_CreateModule
  829. (
  830. ASN1uint32_t version,
  831. ASN1encodingrule_e eEncodingRule,
  832. ASN1uint32_t dwFlags,
  833. ASN1uint32_t cPDUs,
  834. const ASN1GenericFun_t apfnEncoder[],
  835. const ASN1GenericFun_t apfnDecoder[],
  836. const ASN1FreeFun_t apfnFreeMemory[],
  837. const ASN1uint32_t acbStructSize[],
  838. ASN1magic_t nModuleName
  839. )
  840. {
  841. ASN1module_t module = NULL;
  842. /* compiler output and library version match together? */
  843. if (
  844. // version <= ASN1_THIS_VERSION &&
  845. NULL != apfnEncoder &&
  846. NULL != apfnDecoder &&
  847. NULL != apfnFreeMemory &&
  848. NULL != acbStructSize)
  849. {
  850. if (NULL != (module = (ASN1module_t)MemAlloc(sizeof(*module), nModuleName)))
  851. {
  852. module->nModuleName = nModuleName;
  853. module->eRule = eEncodingRule;
  854. module->dwFlags = dwFlags;
  855. module->cPDUs = cPDUs;
  856. module->apfnFreeMemory = apfnFreeMemory;
  857. module->acbStructSize = acbStructSize;
  858. if (ASN1_PER_RULE & eEncodingRule)
  859. {
  860. module->PER.apfnEncoder = (const ASN1PerEncFun_t *) apfnEncoder;
  861. module->PER.apfnDecoder = (const ASN1PerDecFun_t *) apfnDecoder;
  862. }
  863. #ifdef ENABLE_BER
  864. else
  865. if (ASN1_BER_RULE & eEncodingRule)
  866. {
  867. module->BER.apfnEncoder = (const ASN1BerEncFun_t *) apfnEncoder;
  868. module->BER.apfnDecoder = (const ASN1BerDecFun_t *) apfnDecoder;
  869. }
  870. #endif // ENABLE_BER
  871. }
  872. }
  873. return module;
  874. }
  875. void ASN1_CloseModule(ASN1module_t pModule)
  876. {
  877. MemFree(pModule);
  878. }
  879. #ifdef TEST_CODER
  880. static int MyMemCmp(ASN1octet_t *p1, ASN1octet_t *p2, ASN1uint32_t c)
  881. {
  882. BYTE diff;
  883. while (c--)
  884. {
  885. if ((diff = *p1++ - *p2++) != 0)
  886. return (int) diff;
  887. }
  888. return 0;
  889. }
  890. __inline ASN1INTERNencoding_t TestEnc_GetEnc(ASN1INTERNencoding_t e)
  891. { return &(((ASN1testcoder_t) (e+1))->e); }
  892. __inline ASN1INTERNdecoding_t TestEnc_GetDec(ASN1INTERNencoding_t e)
  893. { return &(((ASN1testcoder_t) (e+1))->d); }
  894. __inline ASN1INTERNencoding_t TestDec_GetEnc(ASN1INTERNdecoding_t d)
  895. { return &(((ASN1testcoder_t) (d+1))->e); }
  896. __inline ASN1INTERNdecoding_t TestDec_GetDec(ASN1INTERNdecoding_t d)
  897. { return &(((ASN1testcoder_t) (d+1))->d); }
  898. static void Test_InitEnc(ASN1INTERNencoding_t e, ASN1module_t mod, ASN1encodingrule_e eRule)
  899. {
  900. ZeroMemory(e, sizeof(*e));
  901. e->info.magic = MAGIC_ENCODER;
  902. e->info.err = ASN1_SUCCESS;
  903. e->info.module = mod;
  904. e->info.eRule = eRule;
  905. e->parent = e;
  906. e->child = NULL;
  907. }
  908. static void Test_InitDec(ASN1INTERNdecoding_t d, ASN1module_t mod, ASN1encodingrule_e eRule)
  909. {
  910. ZeroMemory(d, sizeof(*d));
  911. d->info.magic = MAGIC_DECODER;
  912. d->info.err = ASN1_SUCCESS;
  913. d->info.module = mod;
  914. d->info.eRule = eRule;
  915. d->parent = d;
  916. d->child = NULL;
  917. }
  918. static int TestEnc_InitCoder(ASN1INTERNencoding_t e, ASN1module_t mod)
  919. {
  920. ASN1INTERNencoding_t ee = TestEnc_GetEnc(e);
  921. ASN1INTERNdecoding_t ed = TestEnc_GetDec(e);
  922. Test_InitEnc(ee, mod, e->info.eRule);
  923. Test_InitDec(ed, mod, e->info.eRule);
  924. return 1;
  925. }
  926. static int TestDec_InitCoder(ASN1INTERNdecoding_t d, ASN1module_t mod)
  927. {
  928. ASN1INTERNencoding_t de = TestDec_GetEnc(d);
  929. ASN1INTERNdecoding_t dd = TestDec_GetDec(d);
  930. Test_InitEnc(de, mod, d->info.eRule);
  931. Test_InitDec(dd, mod, d->info.eRule);
  932. return 1;
  933. }
  934. static int Test_Encode(ASN1INTERNencoding_t e, void *value, ASN1uint32_t id)
  935. {
  936. ASN1encoding_t enc = (ASN1encoding_t) e;
  937. /* clear error */
  938. ASN1EncSetError(enc, ASN1_SUCCESS);
  939. // clean buffer
  940. enc->pos = enc->buf;
  941. enc->bit = enc->len = 0;
  942. if (ASN1_PER_RULE & enc->eRule)
  943. {
  944. /* encode value */
  945. ASN1PerEncFun_t pfnPER;
  946. if (NULL != (pfnPER = enc->module->PER.apfnEncoder[id]))
  947. {
  948. if ((*pfnPER)(enc, value))
  949. {
  950. ASN1PEREncFlush(enc);
  951. }
  952. }
  953. else
  954. {
  955. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  956. }
  957. }
  958. #ifdef ENABLE_BER
  959. else
  960. if (ASN1_BER_RULE & enc->eRule)
  961. {
  962. /* encode value */
  963. ASN1BerEncFun_t pfnBER;
  964. if (NULL != (pfnBER = enc->module->BER.apfnEncoder[id]))
  965. {
  966. if ((*pfnBER)(enc, 0, value)) // lonchanc: tag is 0 to make it compiled
  967. {
  968. ASN1BEREncFlush(enc);
  969. }
  970. }
  971. else
  972. {
  973. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  974. }
  975. }
  976. #endif // ENABLE_BER
  977. else
  978. {
  979. return ASN1EncSetError(enc, ASN1_ERR_RULE);
  980. }
  981. /* call abort/done function for non-parented encoding stream */
  982. if (e->parent->info.err >= 0)
  983. {
  984. if (e == e->parent)
  985. {
  986. ASN1EncDone(enc);
  987. }
  988. }
  989. else
  990. {
  991. ASN1INTERNencoding_t child, child2;
  992. if (e == e->parent)
  993. {
  994. ASN1EncAbort(enc);
  995. }
  996. // clean up...
  997. ASN1_FreeEncoded(enc, enc->buf);
  998. enc->pos = enc->buf = NULL;
  999. enc->size = enc->len = enc->bit = 0;
  1000. for (child = e->child; child; child = child2)
  1001. {
  1002. child2 = child->child;
  1003. // make sure it does not touch its parent which may already be freed
  1004. child->parent = child;
  1005. ASN1_CloseEncoder((ASN1encoding_t) child);
  1006. }
  1007. e->child = NULL;
  1008. }
  1009. /* return error code */
  1010. return e->parent->info.err;
  1011. }
  1012. static int Test_Decode(ASN1INTERNdecoding_t d, void ** valref, ASN1uint32_t id, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize)
  1013. {
  1014. ASN1decoding_t dec = (ASN1decoding_t) d;
  1015. ASN1uint32_t cbTopLevelStruct;
  1016. /* clear error */
  1017. ASN1DecSetError(dec, ASN1_SUCCESS);
  1018. // set up buffer containing encoded data
  1019. dec->pos = dec->buf = pbBuf;
  1020. dec->size = cbBufSize;
  1021. dec->bit = dec->len = 0;
  1022. /* clear length of linear buffer required */
  1023. d->cbLinearBufSize = 0;
  1024. d->fExtBuf = FALSE;
  1025. cbTopLevelStruct = dec->module->acbStructSize[id];
  1026. if (NULL != (*valref = DecMemAlloc(dec, cbTopLevelStruct)))
  1027. {
  1028. if (ASN1_PER_RULE & dec->eRule)
  1029. {
  1030. ASN1PerDecFun_t pfnPER;
  1031. /* decode value */
  1032. if (NULL != (pfnPER = dec->module->PER.apfnDecoder[id]))
  1033. {
  1034. if ((*pfnPER)(dec, *valref))
  1035. {
  1036. ASN1PERDecFlush(dec);
  1037. }
  1038. }
  1039. else
  1040. {
  1041. return ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  1042. }
  1043. }
  1044. #ifdef ENABLE_BER
  1045. else
  1046. if (ASN1_BER_RULE & dec->eRule)
  1047. {
  1048. ASN1BerDecFun_t pfnBER;
  1049. /* decode value */
  1050. if (NULL != (pfnBER = dec->module->BER.apfnDecoder[id]))
  1051. {
  1052. if ((*pfnBER)(dec, 0, *valref))
  1053. {
  1054. ASN1BERDecFlush(dec);
  1055. }
  1056. }
  1057. else
  1058. {
  1059. return ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  1060. }
  1061. }
  1062. #endif // ENABLE_BER
  1063. else
  1064. {
  1065. return ASN1DecSetError(dec, ASN1_ERR_RULE);
  1066. }
  1067. /* call abort/done function for non-parented decoding stream */
  1068. if (d->parent->info.err >= 0)
  1069. {
  1070. // not parented
  1071. if (d == d->parent)
  1072. {
  1073. ASN1DecDone(dec);
  1074. }
  1075. }
  1076. else
  1077. {
  1078. ASN1INTERNdecoding_t child, child2;
  1079. // not parented
  1080. if (d == d->parent)
  1081. {
  1082. ASN1DecAbort(dec);
  1083. }
  1084. // clean up...
  1085. ASN1_FreeDecoded(dec ,*valref, id);
  1086. *valref = NULL;
  1087. for (child = d->child; child; child = child2)
  1088. {
  1089. child2 = child->child;
  1090. // make sure it does not touch its parent which may already be freed
  1091. child->parent = child;
  1092. ASN1_CloseDecoder((ASN1decoding_t) child);
  1093. }
  1094. d->child = NULL;
  1095. }
  1096. }
  1097. else
  1098. {
  1099. return ASN1_ERR_MEMORY;
  1100. }
  1101. /* return error code */
  1102. return d->parent->info.err;
  1103. }
  1104. static void Test_CleanEnc(ASN1INTERNencoding_t e)
  1105. {
  1106. if (e->info.buf)
  1107. {
  1108. EncMemFree((ASN1encoding_t) e, e->info.buf);
  1109. }
  1110. Test_InitEnc(e, e->info.module, e->info.eRule);
  1111. }
  1112. static void Test_CleanDec(ASN1INTERNdecoding_t d)
  1113. {
  1114. Test_InitDec(d, d->info.module, d->info.eRule);
  1115. }
  1116. static int TestEnc_Compare(ASN1INTERNencoding_t e, ASN1uint32_t id, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize)
  1117. {
  1118. ASN1INTERNencoding_t ee = TestEnc_GetEnc(e);
  1119. ASN1INTERNdecoding_t ed = TestEnc_GetDec(e);
  1120. int ret;
  1121. void *val = NULL;
  1122. int fRet = 0;
  1123. ee->info.eRule = e->info.eRule;
  1124. ed->info.eRule = e->info.eRule;
  1125. ret = Test_Decode(ed, &val, id, pbBuf, cbBufSize);
  1126. if (ret == ASN1_SUCCESS)
  1127. {
  1128. ret = Test_Encode(ee, val, id);
  1129. if (ret == ASN1_SUCCESS)
  1130. {
  1131. if (ee->info.len == cbBufSize)
  1132. {
  1133. fRet = (MyMemCmp(pbBuf, ee->info.buf, cbBufSize) == 0);
  1134. }
  1135. }
  1136. }
  1137. if (val)
  1138. {
  1139. ASN1_FreeDecoded((ASN1decoding_t) ed, val, id);
  1140. }
  1141. Test_CleanEnc(ee);
  1142. Test_CleanDec(ed);
  1143. return fRet;
  1144. }
  1145. static int TestDec_Compare(ASN1INTERNdecoding_t d, ASN1uint32_t id, void *val, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize)
  1146. {
  1147. ASN1INTERNencoding_t de = TestDec_GetEnc(d);
  1148. int ret;
  1149. int fRet = 0;
  1150. de->info.eRule = d->info.eRule;
  1151. ret = Test_Encode(de, val, id);
  1152. if (ret == ASN1_SUCCESS)
  1153. {
  1154. if (de->info.len == cbBufSize)
  1155. {
  1156. fRet = (MyMemCmp(pbBuf, de->info.buf, cbBufSize) == 0);
  1157. }
  1158. }
  1159. Test_CleanEnc(de);
  1160. return fRet;
  1161. }
  1162. #endif