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.

1429 lines
41 KiB

  1. /* Copyright (C) Boris Nikolaus, Germany, 1996-1997. All rights reserved. */
  2. /* Copyright (C) Microsoft Corporation, 1997-1998. All rights reserved. */
  3. #include "precomp.h"
  4. #ifdef ENABLE_BER
  5. #include <float.h>
  6. #include <math.h>
  7. #if HAS_IEEEFP_H
  8. // #include <ieeefp.h>
  9. #elif HAS_FLOAT_H
  10. // #include <float.h>
  11. #endif
  12. static const char bitmsk2[] =
  13. {
  14. (const char) 0x00,
  15. (const char) 0x80,
  16. (const char) 0xc0,
  17. (const char) 0xe0,
  18. (const char) 0xf0,
  19. (const char) 0xf8,
  20. (const char) 0xfc,
  21. (const char) 0xfe
  22. };
  23. /* encode a string value */
  24. int ASN1BEREncCharString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1char_t *val)
  25. {
  26. /* encode tag */
  27. if (ASN1BEREncTag(enc, tag))
  28. {
  29. /* encode length */
  30. if (ASN1BEREncLength(enc, len))
  31. {
  32. CopyMemory(enc->pos, val, len);
  33. enc->pos += len;
  34. return 1;
  35. }
  36. }
  37. return 0;
  38. }
  39. /* encode a string value (CER) */
  40. int ASN1CEREncCharString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1char_t *val)
  41. {
  42. ASN1uint32_t n;
  43. if (len <= 1000)
  44. {
  45. /* encode tag */
  46. if (ASN1BEREncTag(enc, tag))
  47. {
  48. /* encode length */
  49. if (ASN1BEREncLength(enc, len))
  50. {
  51. CopyMemory(enc->pos, val, len);
  52. enc->pos += len;
  53. return 1;
  54. }
  55. }
  56. }
  57. else
  58. {
  59. ASN1uint32_t nLenOff;
  60. /* encode value as constructed, using segments of 1000 octets */
  61. if (ASN1BEREncExplicitTag(enc, tag, &nLenOff))
  62. {
  63. while (len)
  64. {
  65. n = len > 1000 ? 1000 : len;
  66. if (ASN1BEREncTag(enc, 0x4))
  67. {
  68. if (ASN1BEREncLength(enc, n))
  69. {
  70. CopyMemory(enc->pos, val, n);
  71. enc->pos += n;
  72. val += n;
  73. len -= n;
  74. }
  75. else
  76. {
  77. return 0;
  78. }
  79. }
  80. else
  81. {
  82. return 0;
  83. }
  84. }
  85. return ASN1BEREncEndOfContents(enc, nLenOff);
  86. }
  87. }
  88. return 0;
  89. }
  90. /* encode a 16 bit string value */
  91. int ASN1BEREncChar16String(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1char16_t *val)
  92. {
  93. /* encode tag */
  94. if (ASN1BEREncTag(enc, tag))
  95. {
  96. /* encode length */
  97. if (ASN1BEREncLength(enc, len * sizeof(ASN1char16_t)))
  98. {
  99. while (len--)
  100. {
  101. *enc->pos++ = (ASN1octet_t)(*val >> 8);
  102. *enc->pos++ = (ASN1octet_t)(*val);
  103. val++;
  104. }
  105. }
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. /* encode a 16 bit string value (CER) */
  111. int ASN1CEREncChar16String(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1char16_t *val)
  112. {
  113. ASN1uint32_t n;
  114. if (len <= 1000 / sizeof(ASN1char16_t))
  115. {
  116. /* encode tag */
  117. if (ASN1BEREncTag(enc, tag))
  118. {
  119. /* encode length */
  120. if (ASN1BEREncLength(enc, len * sizeof(ASN1char16_t)))
  121. {
  122. while (len--)
  123. {
  124. *enc->pos++ = (ASN1octet_t)(*val >> 8);
  125. *enc->pos++ = (ASN1octet_t)(*val);
  126. val++;
  127. }
  128. return 1;
  129. }
  130. }
  131. }
  132. else
  133. {
  134. ASN1uint32_t nLenOff;
  135. /* encode value as constructed, using segments of 1000 octets */
  136. if (ASN1BEREncExplicitTag(enc, tag, &nLenOff))
  137. {
  138. while (len)
  139. {
  140. n = len > 1000 / sizeof(ASN1char16_t) ?
  141. 1000 / sizeof(ASN1char16_t) : len;
  142. if (ASN1BEREncTag(enc, 0x4))
  143. {
  144. if (ASN1BEREncLength(enc, n))
  145. {
  146. while (len--)
  147. {
  148. *enc->pos++ = (ASN1octet_t)(*val >> 8);
  149. *enc->pos++ = (ASN1octet_t)(*val);
  150. val++;
  151. }
  152. len -= n;
  153. }
  154. else
  155. {
  156. return 0;
  157. }
  158. }
  159. else
  160. {
  161. return 0;
  162. }
  163. }
  164. return ASN1BEREncEndOfContents(enc, nLenOff);
  165. }
  166. }
  167. return 0;
  168. }
  169. /* encode a 32 bit string value */
  170. int ASN1BEREncChar32String(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1char32_t *val)
  171. {
  172. /* encode tag */
  173. if (ASN1BEREncTag(enc, tag))
  174. {
  175. /* encode length */
  176. if (ASN1BEREncLength(enc, len * sizeof(ASN1char32_t)))
  177. {
  178. while (len--)
  179. {
  180. *enc->pos++ = (ASN1octet_t)(*val >> 24);
  181. *enc->pos++ = (ASN1octet_t)(*val >> 16);
  182. *enc->pos++ = (ASN1octet_t)(*val >> 8);
  183. *enc->pos++ = (ASN1octet_t)(*val);
  184. val++;
  185. }
  186. return 1;
  187. }
  188. }
  189. return 0;
  190. }
  191. /* encode a 32 bit string value (CER) */
  192. int ASN1CEREncChar32String(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1char32_t *val)
  193. {
  194. ASN1uint32_t n;
  195. if (len <= 1000 / sizeof(ASN1char32_t))
  196. {
  197. /* encode tag */
  198. if (ASN1BEREncTag(enc, tag))
  199. {
  200. /* encode length */
  201. if (ASN1BEREncLength(enc, len * sizeof(ASN1char32_t)))
  202. {
  203. while (len--)
  204. {
  205. *enc->pos++ = (ASN1octet_t)(*val >> 24);
  206. *enc->pos++ = (ASN1octet_t)(*val >> 16);
  207. *enc->pos++ = (ASN1octet_t)(*val >> 8);
  208. *enc->pos++ = (ASN1octet_t)(*val);
  209. val++;
  210. }
  211. return 1;
  212. }
  213. }
  214. }
  215. else
  216. {
  217. ASN1uint32_t nLenOff;
  218. /* encode value as constructed, using segments of 1000 octets */
  219. if (ASN1BEREncExplicitTag(enc, tag, &nLenOff))
  220. {
  221. while (len)
  222. {
  223. n = len > 1000 / sizeof(ASN1char32_t) ?
  224. 1000 / sizeof(ASN1char32_t) : len;
  225. if (ASN1BEREncTag(enc, 0x4))
  226. {
  227. if (ASN1BEREncLength(enc, n))
  228. {
  229. while (len--)
  230. {
  231. *enc->pos++ = (ASN1octet_t)(*val >> 24);
  232. *enc->pos++ = (ASN1octet_t)(*val >> 16);
  233. *enc->pos++ = (ASN1octet_t)(*val >> 8);
  234. *enc->pos++ = (ASN1octet_t)(*val);
  235. val++;
  236. }
  237. len -= n;
  238. }
  239. else
  240. {
  241. return 0;
  242. }
  243. }
  244. else
  245. {
  246. return 0;
  247. }
  248. } // while
  249. return ASN1BEREncEndOfContents(enc, nLenOff);
  250. }
  251. }
  252. return 0;
  253. }
  254. /* encode a bit string value */
  255. int ASN1BEREncBitString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1octet_t *val)
  256. {
  257. ASN1uint32_t noctets = (len + 7) / 8;
  258. /* encode tag */
  259. if (ASN1BEREncTag(enc, tag))
  260. {
  261. /* encode length */
  262. if (ASN1BEREncLength(enc, noctets + 1))
  263. {
  264. ASN1uint32_t cUnusedBits = (7 - ((len + 7) & 7));
  265. *enc->pos++ = (ASN1octet_t) cUnusedBits;
  266. CopyMemory(enc->pos, val, noctets);
  267. enc->pos += noctets;
  268. EncAssert(enc, noctets >= 1);
  269. if (cUnusedBits)
  270. {
  271. EncAssert(enc, 8 >= cUnusedBits);
  272. *(enc->pos - 1) &= bitmsk2[8 - cUnusedBits];
  273. }
  274. return 1;
  275. }
  276. }
  277. return 0;
  278. }
  279. /* encode a bit string value (CER) */
  280. int ASN1CEREncBitString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1octet_t *val)
  281. {
  282. ASN1uint32_t noctets;
  283. ASN1uint32_t n;
  284. noctets = (len + 7) / 8;
  285. if (noctets + 1 <= 1000)
  286. {
  287. /* encode tag */
  288. if (ASN1BEREncTag(enc, tag))
  289. {
  290. /* encode length */
  291. if (ASN1BEREncLength(enc, noctets + 1))
  292. {
  293. *enc->pos++ = (ASN1octet_t) (7 - ((len + 7) & 7));
  294. CopyMemory(enc->pos, val, noctets);
  295. enc->pos += noctets;
  296. return 1;
  297. }
  298. }
  299. }
  300. else
  301. {
  302. ASN1uint32_t nLenOff;
  303. /* encode value as constructed, using segments of 1000 octets */
  304. if (ASN1BEREncExplicitTag(enc, tag, &nLenOff))
  305. {
  306. while (noctets)
  307. {
  308. n = len > 999 ? 999 : len;
  309. if (ASN1BEREncTag(enc, 0x3))
  310. {
  311. if (ASN1BEREncLength(enc, n + 1))
  312. {
  313. *enc->pos++ = (ASN1octet_t) (n < len ? 0 : 7 - ((len + 7) & 7));
  314. CopyMemory(enc->pos, val, n);
  315. enc->pos += n;
  316. val += n;
  317. noctets -= n;
  318. }
  319. else
  320. {
  321. return 0;
  322. }
  323. }
  324. else
  325. {
  326. return 0;
  327. }
  328. } // while
  329. return ASN1BEREncEndOfContents(enc, nLenOff);
  330. }
  331. }
  332. return 0;
  333. }
  334. #ifdef ENABLE_GENERALIZED_CHAR_STR
  335. /* encode a character string value */
  336. int ASN1BEREncCharacterString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1characterstring_t *val)
  337. {
  338. ASN1uint32_t index;
  339. ASN1uint32_t flag;
  340. /* search identification */
  341. if (!ASN1EncSearchCharacterStringIdentification(((ASN1INTERNencoding_t) enc)->parent,
  342. &val->identification, &index, &flag))
  343. return 0;
  344. if (index > 255)
  345. flag = 1;
  346. if (flag)
  347. {
  348. ASN1uint32_t nLenOff_, nLenOff0, nLenOff1;
  349. /* CS-A encoding: */
  350. /* encode as constructed value */
  351. if (!ASN1BEREncExplicitTag(enc, tag, &nLenOff_))
  352. return 0;
  353. /* encode index */
  354. if (!ASN1BEREncU32(enc, 0x80000000, index))
  355. return 0;
  356. /* encode tag of identification */
  357. if (!ASN1BEREncExplicitTag(enc, 0x80000001, &nLenOff0))
  358. return 0;
  359. /* encode identification */
  360. switch (val->identification.o)
  361. {
  362. case ASN1characterstring_identification_syntaxes_o:
  363. if (!ASN1BEREncExplicitTag(enc, 0x80000000, &nLenOff1))
  364. return 0;
  365. if (!ASN1BEREncObjectIdentifier(enc, 0x80000000,
  366. &val->identification.u.syntaxes.abstract))
  367. return 0;
  368. if (!ASN1BEREncObjectIdentifier(enc, 0x80000001,
  369. &val->identification.u.syntaxes.transfer))
  370. return 0;
  371. if (!ASN1BEREncEndOfContents(enc, nLenOff1))
  372. return 0;
  373. break;
  374. case ASN1characterstring_identification_syntax_o:
  375. if (!ASN1BEREncObjectIdentifier(enc, 0x80000001,
  376. &val->identification.u.syntax))
  377. return 0;
  378. break;
  379. case ASN1characterstring_identification_presentation_context_id_o:
  380. if (!ASN1BEREncU32(enc, 0x80000002,
  381. val->identification.u.presentation_context_id))
  382. return 0;
  383. break;
  384. case ASN1characterstring_identification_context_negotiation_o:
  385. if (!ASN1BEREncExplicitTag(enc, 0x80000003, &nLenOff1))
  386. return 0;
  387. if (!ASN1BEREncU32(enc, 0x80000000, val->
  388. identification.u.context_negotiation.presentation_context_id))
  389. return 0;
  390. if (!ASN1BEREncObjectIdentifier(enc, 0x80000001,
  391. &val->identification.u.context_negotiation.transfer_syntax))
  392. return 0;
  393. if (!ASN1BEREncEndOfContents(enc, nLenOff1))
  394. return 0;
  395. break;
  396. case ASN1characterstring_identification_transfer_syntax_o:
  397. if (!ASN1BEREncObjectIdentifier(enc, 0x80000004,
  398. &val->identification.u.transfer_syntax))
  399. return 0;
  400. break;
  401. case ASN1characterstring_identification_fixed_o:
  402. if (!ASN1BEREncNull(enc, 0x80000005))
  403. return 0;
  404. break;
  405. default:
  406. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  407. return 0;
  408. }
  409. /* end of identification */
  410. if (!ASN1BEREncEndOfContents(enc, nLenOff0))
  411. return 0;
  412. /* encode data value */
  413. switch (val->data_value.o)
  414. {
  415. case ASN1characterstring_data_value_notation_o:
  416. if (!ASN1BEREncOctetString(enc, 0x80000002,
  417. val->data_value.u.notation.length,
  418. val->data_value.u.notation.encoded))
  419. return 0;
  420. break;
  421. case ASN1characterstring_data_value_encoded_o:
  422. if (!ASN1BEREncOctetString(enc, 0x80000002,
  423. val->data_value.u.encoded.length,
  424. val->data_value.u.encoded.value))
  425. return 0;
  426. break;
  427. default:
  428. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  429. return 0;
  430. }
  431. /* end of character string */
  432. if (!ASN1BEREncEndOfContents(enc, nLenOff_))
  433. return 0;
  434. }
  435. else
  436. {
  437. /* CS-B encoding: */
  438. /* encode tag */
  439. if (!ASN1BEREncTag(enc, tag))
  440. return 0;
  441. /* encode data value */
  442. switch (val->data_value.o)
  443. {
  444. case ASN1characterstring_data_value_notation_o:
  445. if (!ASN1BEREncLength(enc,
  446. val->data_value.u.notation.length + 1))
  447. return 0;
  448. *enc->pos++ = (ASN1octet_t) index;
  449. CopyMemory(enc->pos, val->data_value.u.notation.encoded,
  450. val->data_value.u.notation.length);
  451. enc->pos += val->data_value.u.notation.length;
  452. break;
  453. case ASN1characterstring_data_value_encoded_o:
  454. if (!ASN1BEREncLength(enc,
  455. val->data_value.u.encoded.length + 1))
  456. return 0;
  457. *enc->pos++ = (ASN1octet_t) index;
  458. CopyMemory(enc->pos, val->data_value.u.encoded.value,
  459. val->data_value.u.encoded.length);
  460. enc->pos += val->data_value.u.encoded.length;
  461. break;
  462. default:
  463. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  464. return 0;
  465. }
  466. }
  467. return 1;
  468. }
  469. #endif // ENABLE_GENERALIZED_CHAR_STR
  470. #ifdef ENABLE_DOUBLE
  471. /* encode a real value */
  472. int ASN1BEREncDouble(ASN1encoding_t enc, ASN1uint32_t tag, double d)
  473. {
  474. double mantissa;
  475. int exponent;
  476. ASN1uint32_t nmoctets;
  477. ASN1uint32_t neoctets;
  478. ASN1octet_t head;
  479. ASN1uint32_t sign;
  480. ASN1uint32_t len;
  481. ASN1octet_t mASN1octets[16]; /* should be enough */
  482. ASN1octet_t eASN1octets[16]; /* should be enough */
  483. /* encode tag */
  484. if (ASN1BEREncTag(enc, tag))
  485. {
  486. /* check for PLUS_INFINITY */
  487. if (ASN1double_ispinf(d))
  488. {
  489. /* encode length */
  490. if (ASN1BEREncLength(enc, 1))
  491. {
  492. /* encode value */
  493. *enc->pos++ = 0x40;
  494. return 1;
  495. }
  496. }
  497. else
  498. /* check for MINUS_INFINITY */
  499. if (ASN1double_isminf(d))
  500. {
  501. /* encode length */
  502. if (ASN1BEREncLength(enc, 1))
  503. {
  504. /* encode value */
  505. *enc->pos++ = 0x41;
  506. return 1;
  507. }
  508. }
  509. else
  510. /* check for bad real value */
  511. if (finite(d))
  512. {
  513. /* encode normal real value */
  514. /* split into mantissa and exponent */
  515. mantissa = frexp(d, &exponent);
  516. /* check for zero value */
  517. if (mantissa == 0.0 && exponent == 0)
  518. {
  519. /* encode zero length */
  520. return ASN1BEREncLength(enc, 0);
  521. }
  522. /* get sign bit */
  523. if (mantissa < 0.0)
  524. {
  525. sign = 1;
  526. mantissa = -mantissa;
  527. }
  528. else
  529. {
  530. sign = 0;
  531. }
  532. /* encode mantissa */
  533. nmoctets = 0;
  534. while (mantissa != 0.0 && nmoctets < sizeof(mASN1octets))
  535. {
  536. mantissa *= 256.0;
  537. exponent -= 8;
  538. mASN1octets[nmoctets++] = (int)mantissa;
  539. mantissa -= (double)(int)mantissa;
  540. }
  541. /* encode exponent and create head octet of encoded value */
  542. head = (ASN1octet_t) (0x80 | (sign << 6));
  543. if (exponent <= 0x7f && exponent >= -0x80)
  544. {
  545. eASN1octets[0] = (ASN1octet_t)(exponent);
  546. neoctets = 1;
  547. }
  548. else
  549. if (exponent <= 0x7fff && exponent >= -0x8000)
  550. {
  551. eASN1octets[0] = (ASN1octet_t)(exponent >> 8);
  552. eASN1octets[1] = (ASN1octet_t)(exponent);
  553. neoctets = 2;
  554. head |= 0x01;
  555. }
  556. else
  557. if (exponent <= 0x7fffff && exponent >= -0x800000)
  558. {
  559. eASN1octets[0] = (ASN1octet_t)(exponent >> 16);
  560. eASN1octets[1] = (ASN1octet_t)(exponent >> 8);
  561. eASN1octets[2] = (ASN1octet_t)(exponent);
  562. neoctets = 3;
  563. head |= 0x02;
  564. }
  565. else
  566. {
  567. eASN1octets[0] = 4; /* XXX does not work if ASN1int32_t != int */
  568. eASN1octets[1] = (ASN1octet_t)(exponent >> 24);
  569. eASN1octets[2] = (ASN1octet_t)(exponent >> 16);
  570. eASN1octets[3] = (ASN1octet_t)(exponent >> 8);
  571. eASN1octets[4] = (ASN1octet_t)(exponent);
  572. neoctets = 5;
  573. head |= 0x03;
  574. }
  575. /* encode length into first octet */
  576. len = 1 + neoctets + nmoctets;
  577. if (ASN1BEREncLength(enc, len))
  578. {
  579. /* put head octet, mantissa and exponent */
  580. *enc->pos++ = head;
  581. CopyMemory(enc->pos, eASN1octets, neoctets);
  582. enc->pos += neoctets;
  583. CopyMemory(enc->pos, mASN1octets, nmoctets);
  584. enc->pos += nmoctets;
  585. return 1;
  586. }
  587. }
  588. else
  589. {
  590. ASN1EncSetError(enc, ASN1_ERR_BADREAL);
  591. }
  592. }
  593. /* finished */
  594. return 0;
  595. }
  596. #endif // ENABLE_DOUBLE
  597. #ifdef ENABLE_REAL
  598. /* encode a real value */
  599. int ASN1BEREncReal(ASN1encoding_t enc, ASN1uint32_t tag, ASN1real_t *val)
  600. {
  601. ASN1intx_t mantissa;
  602. ASN1intx_t exponent;
  603. ASN1intx_t help;
  604. ASN1uint32_t nmoctets;
  605. ASN1uint32_t neoctets;
  606. ASN1octet_t head;
  607. ASN1uint32_t sign;
  608. ASN1uint32_t len;
  609. ASN1octet_t mASN1octets[256]; /* should be enough */
  610. ASN1octet_t eASN1octets[256]; /* should be enough */
  611. /* encode tag */
  612. if (ASN1BEREncTag(enc, tag))
  613. {
  614. /* check for PLUS_INFINITY */
  615. if (val->type == eReal_PlusInfinity)
  616. {
  617. /* encode length */
  618. if (ASN1BEREncLength(enc, 1))
  619. {
  620. /* encode value */
  621. *enc->pos++ = 0x40;
  622. return 1;
  623. }
  624. }
  625. else
  626. /* check for MINUS_INFINITY */
  627. if (val->type == eReal_MinusInfinity)
  628. {
  629. /* encode length */
  630. if (ASN1BEREncLength(enc, 1))
  631. {
  632. /* encode value */
  633. *enc->pos++ = 0x41;
  634. return 1;
  635. }
  636. }
  637. /* encode normal real value */
  638. else
  639. {
  640. /* check for zero value */
  641. if (!ASN1intx_cmp(&val->mantissa, &ASN1intx_0))
  642. {
  643. /* encode zero length */
  644. return ASN1BEREncLength(enc, 0);
  645. }
  646. /* get sign bit */
  647. if (val->mantissa.value[0] > 0x7f)
  648. {
  649. sign = 1;
  650. ASN1intx_neg(&mantissa, &val->mantissa);
  651. }
  652. else
  653. {
  654. sign = 0;
  655. if (! ASN1intx_dup(&mantissa, &val->mantissa))
  656. {
  657. return 0;
  658. }
  659. }
  660. if (! ASN1intx_dup(&exponent, &val->exponent))
  661. {
  662. return 0;
  663. }
  664. /* encode mantissa */
  665. nmoctets = ASN1intx_uoctets(&mantissa);
  666. if (nmoctets < 256)
  667. {
  668. CopyMemory(mASN1octets,
  669. mantissa.value + mantissa.length - nmoctets,
  670. nmoctets);
  671. ASN1intx_setuint32(&help, 8 * nmoctets);
  672. ASN1intx_sub(&exponent, &exponent, &help);
  673. ASN1intx_free(&mantissa);
  674. ASN1intx_free(&help);
  675. /* encode exponent and create head octet of encoded value */
  676. neoctets = ASN1intx_octets(&exponent);
  677. if (neoctets < 256)
  678. {
  679. CopyMemory(mASN1octets,
  680. val->exponent.value + val->exponent.length - neoctets,
  681. neoctets);
  682. ASN1intx_free(&exponent);
  683. head = (ASN1octet_t) (0x80 | (sign << 6) | (neoctets - 1));
  684. /* encode length into first octet */
  685. len = 1 + neoctets + nmoctets;
  686. if (ASN1BEREncLength(enc, len))
  687. {
  688. /* put head octet, mantissa and exponent */
  689. *enc->pos++ = head;
  690. CopyMemory(enc->pos, eASN1octets, neoctets);
  691. enc->pos += neoctets;
  692. CopyMemory(enc->pos, mASN1octets, nmoctets);
  693. enc->pos += nmoctets;
  694. return 1;
  695. }
  696. }
  697. else
  698. {
  699. ASN1intx_free(&exponent);
  700. ASN1EncSetError(enc, ASN1_ERR_LARGE);
  701. }
  702. }
  703. else
  704. {
  705. ASN1intx_free(&mantissa);
  706. ASN1intx_free(&help);
  707. ASN1EncSetError(enc, ASN1_ERR_LARGE);
  708. }
  709. }
  710. }
  711. /* finished */
  712. return 0;
  713. }
  714. #endif // ENABLE_REAL
  715. #ifdef ENABLE_EMBEDDED_PDV
  716. /* encode an embedded pdv value */
  717. int ASN1BEREncEmbeddedPdv(ASN1encoding_t enc, ASN1uint32_t tag, ASN1embeddedpdv_t *val)
  718. {
  719. ASN1uint32_t index;
  720. ASN1uint32_t flag;
  721. /* search identification */
  722. if (!ASN1EncSearchEmbeddedPdvIdentification(((ASN1INTERNencoding_t) enc)->parent,
  723. &val->identification, &index, &flag))
  724. return 0;
  725. if (index > 255 ||
  726. (val->data_value.o == ASN1embeddedpdv_data_value_encoded_o &&
  727. (val->data_value.u.encoded.length & 7))) {
  728. flag = 1;
  729. }
  730. if (flag)
  731. {
  732. ASN1uint32_t nLenOff_, nLenOff0, nLenOff1;
  733. /* EP-A encoding: */
  734. /* encode as construct value */
  735. if (!ASN1BEREncExplicitTag(enc, tag, &nLenOff_))
  736. return 0;
  737. /* encode index */
  738. if (!ASN1BEREncU32(enc, 0x80000000, index))
  739. return 0;
  740. /* encode tag of identification */
  741. if (!ASN1BEREncExplicitTag(enc, 0x80000001, &nLenOff0))
  742. return 0;
  743. /* encode identification */
  744. switch (val->identification.o)
  745. {
  746. case ASN1embeddedpdv_identification_syntaxes_o:
  747. if (!ASN1BEREncExplicitTag(enc, 0x80000000, &nLenOff1))
  748. return 0;
  749. if (!ASN1BEREncObjectIdentifier(enc, 0x80000000,
  750. &val->identification.u.syntaxes.abstract))
  751. return 0;
  752. if (!ASN1BEREncObjectIdentifier(enc, 0x80000001,
  753. &val->identification.u.syntaxes.transfer))
  754. return 0;
  755. if (!ASN1BEREncEndOfContents(enc, nLenOff1))
  756. return 0;
  757. break;
  758. case ASN1embeddedpdv_identification_syntax_o:
  759. if (!ASN1BEREncObjectIdentifier(enc, 0x80000001,
  760. &val->identification.u.syntax))
  761. return 0;
  762. break;
  763. case ASN1embeddedpdv_identification_presentation_context_id_o:
  764. if (!ASN1BEREncU32(enc, 0x80000002,
  765. val->identification.u.presentation_context_id))
  766. return 0;
  767. break;
  768. case ASN1embeddedpdv_identification_context_negotiation_o:
  769. if (!ASN1BEREncExplicitTag(enc, 0x80000003, &nLenOff1))
  770. return 0;
  771. if (!ASN1BEREncU32(enc, 0x80000000, val->
  772. identification.u.context_negotiation.presentation_context_id))
  773. return 0;
  774. if (!ASN1BEREncObjectIdentifier(enc, 0x80000001,
  775. &val->identification.u.context_negotiation.transfer_syntax))
  776. return 0;
  777. if (!ASN1BEREncEndOfContents(enc, nLenOff1))
  778. return 0;
  779. break;
  780. case ASN1embeddedpdv_identification_transfer_syntax_o:
  781. if (!ASN1BEREncObjectIdentifier(enc, 0x80000004,
  782. &val->identification.u.transfer_syntax))
  783. return 0;
  784. break;
  785. case ASN1embeddedpdv_identification_fixed_o:
  786. if (!ASN1BEREncNull(enc, 0x80000005))
  787. return 0;
  788. break;
  789. default:
  790. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  791. return 0;
  792. }
  793. /* end of identification */
  794. if (!ASN1BEREncEndOfContents(enc, nLenOff0))
  795. return 0;
  796. /* encode data value */
  797. switch (val->data_value.o)
  798. {
  799. case ASN1embeddedpdv_data_value_notation_o:
  800. if (!ASN1BEREncBitString(enc, 0x80000002,
  801. val->data_value.u.notation.length * 8,
  802. val->data_value.u.notation.encoded))
  803. return 0;
  804. break;
  805. case ASN1embeddedpdv_data_value_encoded_o:
  806. if (!ASN1BEREncBitString(enc, 0x80000002,
  807. val->data_value.u.encoded.length,
  808. val->data_value.u.encoded.value))
  809. return 0;
  810. break;
  811. default:
  812. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  813. return 0;
  814. }
  815. /* end of embedded pdv */
  816. if (!ASN1BEREncEndOfContents(enc, nLenOff_))
  817. return 0;
  818. }
  819. else
  820. {
  821. /* EP-B encoding: */
  822. /* encode tag */
  823. if (!ASN1BEREncTag(enc, tag))
  824. return 0;
  825. /* encode data value */
  826. switch (val->data_value.o)
  827. {
  828. case ASN1embeddedpdv_data_value_notation_o:
  829. if (!ASN1BEREncLength(enc,
  830. val->data_value.u.notation.length + 1))
  831. return 0;
  832. *enc->pos++ = (ASN1octet_t) index;
  833. CopyMemory(enc->pos, val->data_value.u.notation.encoded,
  834. val->data_value.u.notation.length);
  835. enc->pos += val->data_value.u.notation.length;
  836. break;
  837. case ASN1embeddedpdv_data_value_encoded_o:
  838. if (!ASN1BEREncLength(enc,
  839. val->data_value.u.encoded.length / 8 + 1))
  840. return 0;
  841. *enc->pos++ = (ASN1octet_t) index;
  842. CopyMemory(enc->pos, val->data_value.u.encoded.value,
  843. val->data_value.u.encoded.length / 8);
  844. enc->pos += val->data_value.u.encoded.length / 8;
  845. break;
  846. default:
  847. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  848. return 0;
  849. }
  850. }
  851. return 1;
  852. }
  853. #endif // ENABLE_EMBEDDED_PDV
  854. #ifdef ENABLE_EXTERNAL
  855. /* encode an external value */
  856. int ASN1BEREncExternal(ASN1encoding_t enc, ASN1uint32_t tag, ASN1external_t *val)
  857. {
  858. ASN1uint32_t t;
  859. ASN1uint32_t nLenOff_, nLenOff0;
  860. if (!val->data_value_descriptor)
  861. val->o[0] &= ~0x80;
  862. /* encode tag */
  863. if (!ASN1BEREncExplicitTag(enc, tag, &nLenOff_))
  864. return 0;
  865. /* encode identification */
  866. switch (val->identification.o) {
  867. case ASN1external_identification_syntax_o:
  868. if (!ASN1BEREncObjectIdentifier(enc, 0x6,
  869. &val->identification.u.syntax))
  870. return 0;
  871. break;
  872. case ASN1external_identification_presentation_context_id_o:
  873. if (!ASN1BEREncU32(enc, 0x2,
  874. val->identification.u.presentation_context_id))
  875. return 0;
  876. break;
  877. case ASN1external_identification_context_negotiation_o:
  878. if (!ASN1BEREncObjectIdentifier(enc, 0x6,
  879. &val->identification.u.context_negotiation.transfer_syntax))
  880. return 0;
  881. if (!ASN1BEREncU32(enc, 0x2,
  882. val->identification.u.context_negotiation.presentation_context_id))
  883. return 0;
  884. break;
  885. default:
  886. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  887. return 0;
  888. }
  889. /* encode data value descriptor if present */
  890. if (val->o[0] & 0x80) {
  891. t = My_lstrlenA(val->data_value_descriptor);
  892. if (!ASN1BEREncCharString(enc, 0x7, t, val->data_value_descriptor))
  893. return 0;
  894. }
  895. /* encode data value */
  896. switch (val->data_value.o)
  897. {
  898. case ASN1external_data_value_notation_o:
  899. if (!ASN1BEREncExplicitTag(enc, 0, &nLenOff0))
  900. return 0;
  901. if (!ASN1BEREncOpenType(enc, &val->data_value.u.notation))
  902. return 0;
  903. if (!ASN1BEREncEndOfContents(enc, nLenOff0))
  904. return 0;
  905. break;
  906. case ASN1external_data_value_encoded_o:
  907. if (!(val->data_value.u.encoded.length & 7))
  908. {
  909. if (!ASN1BEREncExplicitTag(enc, 1, &nLenOff0))
  910. return 0;
  911. if (!ASN1BEREncOctetString(enc, 0x4,
  912. val->data_value.u.encoded.length / 8,
  913. val->data_value.u.encoded.value))
  914. return 0;
  915. if (!ASN1BEREncEndOfContents(enc, nLenOff0))
  916. return 0;
  917. }
  918. else
  919. {
  920. if (!ASN1BEREncExplicitTag(enc, 2, &nLenOff0))
  921. return 0;
  922. if (!ASN1BEREncBitString(enc, 0x3,
  923. val->data_value.u.encoded.length,
  924. val->data_value.u.encoded.value))
  925. return 0;
  926. if (!ASN1BEREncEndOfContents(enc, nLenOff0))
  927. return 0;
  928. }
  929. break;
  930. default:
  931. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  932. return 0;
  933. }
  934. /* end of external value */
  935. return ASN1BEREncEndOfContents(enc, nLenOff_);
  936. }
  937. #endif // ENABLE_EXTERNAL
  938. /* encode a generalized time value */
  939. int ASN1BEREncGeneralizedTime(ASN1encoding_t enc, ASN1uint32_t tag, ASN1generalizedtime_t *val)
  940. {
  941. char time[32];
  942. ASN1generalizedtime2string(time, val);
  943. return ASN1BEREncCharString(enc, tag, My_lstrlenA(time), time);
  944. }
  945. /* encode a generalized time value (CER) */
  946. int ASN1CEREncGeneralizedTime(ASN1encoding_t enc, ASN1uint32_t tag, ASN1generalizedtime_t *val)
  947. {
  948. char time[32];
  949. ASN1generalizedtime2string(time, val);
  950. return ASN1CEREncCharString(enc, tag, My_lstrlenA(time), time);
  951. }
  952. /* encode a signed integer value */
  953. int ASN1BEREncS32(ASN1encoding_t enc, ASN1uint32_t tag, ASN1int32_t val)
  954. {
  955. if (ASN1BEREncTag(enc, tag))
  956. {
  957. if (val >= -0x8000 && val < 0x8000)
  958. {
  959. if (val >= -0x80 && val < 0x80)
  960. {
  961. if (ASN1BEREncLength(enc, 1))
  962. {
  963. *enc->pos++ = (ASN1octet_t)(val);
  964. return 1;
  965. }
  966. }
  967. else
  968. {
  969. if (ASN1BEREncLength(enc, 2))
  970. {
  971. *enc->pos++ = (ASN1octet_t)(val >> 8);
  972. *enc->pos++ = (ASN1octet_t)(val);
  973. return 1;
  974. }
  975. }
  976. }
  977. else
  978. {
  979. if (val >= -0x800000 && val < 0x800000)
  980. {
  981. if (ASN1BEREncLength(enc, 3))
  982. {
  983. *enc->pos++ = (ASN1octet_t)(val >> 16);
  984. *enc->pos++ = (ASN1octet_t)(val >> 8);
  985. *enc->pos++ = (ASN1octet_t)(val);
  986. return 1;
  987. }
  988. }
  989. else
  990. {
  991. if (ASN1BEREncLength(enc, 4))
  992. {
  993. *enc->pos++ = (ASN1octet_t)(val >> 24);
  994. *enc->pos++ = (ASN1octet_t)(val >> 16);
  995. *enc->pos++ = (ASN1octet_t)(val >> 8);
  996. *enc->pos++ = (ASN1octet_t)(val);
  997. return 1;
  998. }
  999. }
  1000. }
  1001. }
  1002. return 0;
  1003. }
  1004. /* encode a intx_t integer value */
  1005. int ASN1BEREncSX(ASN1encoding_t enc, ASN1uint32_t tag, ASN1intx_t *val)
  1006. {
  1007. ASN1uint32_t cb;
  1008. ASN1octet_t *p;
  1009. /* encode tag */
  1010. if (ASN1BEREncTag(enc, tag))
  1011. {
  1012. // strip out leading 0 and ff.
  1013. for (cb = val->length, p = val->value; cb > 1; cb--, p++)
  1014. {
  1015. // break if not 00 nor FF
  1016. if (*p && *p != 0xFF)
  1017. {
  1018. break;
  1019. }
  1020. // break if 00 FF
  1021. if ((! *p) && (*(p+1) & 0x80))
  1022. {
  1023. break;
  1024. }
  1025. // break if FF 7F
  1026. if (*p == 0xFF && (!(*(p+1) & 0x80)))
  1027. {
  1028. break;
  1029. }
  1030. }
  1031. /* encode length */
  1032. if (ASN1BEREncLength(enc, cb))
  1033. {
  1034. /* copy value */
  1035. CopyMemory(enc->pos, p, cb);
  1036. enc->pos += cb;
  1037. return 1;
  1038. }
  1039. }
  1040. return 0;
  1041. }
  1042. /* encode a multibyte string value */
  1043. int ASN1BEREncZeroMultibyteString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1ztcharstring_t val)
  1044. {
  1045. return ASN1BEREncCharString(enc, tag, My_lstrlenA(val), val);
  1046. }
  1047. int ASN1BEREncMultibyteString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1charstring_t *val)
  1048. {
  1049. return ASN1BEREncCharString(enc, tag, val->length, val->value);
  1050. }
  1051. /* encode a multibyte string value (CER) */
  1052. int ASN1CEREncZeroMultibyteString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1ztcharstring_t val)
  1053. {
  1054. return ASN1CEREncCharString(enc, tag, My_lstrlenA(val), val);
  1055. }
  1056. int ASN1CEREncMultibyteString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1charstring_t *val)
  1057. {
  1058. return ASN1CEREncCharString(enc, tag, val->length, val->value);
  1059. }
  1060. /* encode a null value */
  1061. int ASN1BEREncNull(ASN1encoding_t enc, ASN1uint32_t tag)
  1062. {
  1063. /* encode tag */
  1064. if (ASN1BEREncTag(enc, tag))
  1065. {
  1066. /* encode zero length */
  1067. return ASN1BEREncLength(enc, 0);
  1068. }
  1069. return 0;
  1070. }
  1071. // encode an oid node s to buffer pointed by p
  1072. ASN1octet_t *_BEREncOidNode(ASN1octet_t *p, ASN1uint32_t s)
  1073. {
  1074. if (s < 0x80)
  1075. {
  1076. *p++ = (ASN1octet_t)(s);
  1077. }
  1078. else
  1079. if (s < 0x4000)
  1080. {
  1081. *p++ = (ASN1octet_t)((s >> 7) | 0x80);
  1082. *p++ = (ASN1octet_t)(s & 0x7f);
  1083. }
  1084. else
  1085. if (s < 0x200000)
  1086. {
  1087. *p++ = (ASN1octet_t)((s >> 14) | 0x80);
  1088. *p++ = (ASN1octet_t)((s >> 7) | 0x80);
  1089. *p++ = (ASN1octet_t)(s & 0x7f);
  1090. }
  1091. else
  1092. if (s < 0x10000000)
  1093. {
  1094. *p++ = (ASN1octet_t)((s >> 21) | 0x80);
  1095. *p++ = (ASN1octet_t)((s >> 14) | 0x80);
  1096. *p++ = (ASN1octet_t)((s >> 7) | 0x80);
  1097. *p++ = (ASN1octet_t)(s & 0x7f);
  1098. }
  1099. else
  1100. {
  1101. *p++ = (ASN1octet_t)((s >> 28) | 0x80);
  1102. *p++ = (ASN1octet_t)((s >> 21) | 0x80);
  1103. *p++ = (ASN1octet_t)((s >> 14) | 0x80);
  1104. *p++ = (ASN1octet_t)((s >> 7) | 0x80);
  1105. *p++ = (ASN1octet_t)(s & 0x7f);
  1106. }
  1107. return p;
  1108. }
  1109. /* encode an object identifier value */
  1110. int ASN1BEREncObjectIdentifier(ASN1encoding_t enc, ASN1uint32_t tag, ASN1objectidentifier_t *val)
  1111. {
  1112. /* encode tag */
  1113. if (ASN1BEREncTag(enc, tag))
  1114. {
  1115. ASN1objectidentifier_t obj = *val;
  1116. ASN1uint32_t i, s, l, *v;
  1117. ASN1octet_t *data, *p;
  1118. l = GetObjectIdentifierCount(obj);
  1119. if (l)
  1120. {
  1121. /* convert object identifier to octets */
  1122. p = data = (ASN1octet_t *)MemAlloc(l * 5, _ModName(enc)); /* max. 5 octets/subelement */
  1123. if (p)
  1124. {
  1125. int rc;
  1126. for (i = 0; i < l; i++)
  1127. {
  1128. s = obj->value;
  1129. obj = obj->next;
  1130. if (!i && l > 1)
  1131. {
  1132. s = s * 40 + obj->value;
  1133. obj = obj->next;
  1134. i++;
  1135. }
  1136. p = _BEREncOidNode(p, s);
  1137. } // for
  1138. /* encode length */
  1139. rc = ASN1BEREncLength(enc, (ASN1uint32_t) (p - data));
  1140. if (rc)
  1141. {
  1142. /* copy value */
  1143. CopyMemory(enc->pos, data, p - data);
  1144. enc->pos += p - data;
  1145. }
  1146. MemFree(data);
  1147. return rc;
  1148. }
  1149. ASN1EncSetError(enc, ASN1_ERR_MEMORY);
  1150. return 0;
  1151. } // if (l)
  1152. /* encode zero length */
  1153. return ASN1BEREncLength(enc, 0);
  1154. }
  1155. return 0;
  1156. }
  1157. /* encode an object identifier value */
  1158. int ASN1BEREncObjectIdentifier2(ASN1encoding_t enc, ASN1uint32_t tag, ASN1objectidentifier2_t *val)
  1159. {
  1160. /* encode tag */
  1161. if (ASN1BEREncTag(enc, tag))
  1162. {
  1163. ASN1uint32_t i, s;
  1164. ASN1octet_t *data, *p;
  1165. if (val->count)
  1166. {
  1167. /* convert object identifier to octets */
  1168. p = data = (ASN1octet_t *)MemAlloc(val->count * 5, _ModName(enc)); /* max. 5 octets/subelement */
  1169. if (p)
  1170. {
  1171. int rc;
  1172. for (i = 0; i < val->count; i++)
  1173. {
  1174. s = val->value[i];
  1175. if (!i && val->count > 1)
  1176. {
  1177. i++;
  1178. s = s * 40 + val->value[i];
  1179. }
  1180. p = _BEREncOidNode(p, s);
  1181. } // for
  1182. /* encode length */
  1183. rc = ASN1BEREncLength(enc, (ASN1uint32_t) (p - data));
  1184. if (rc)
  1185. {
  1186. /* copy value */
  1187. CopyMemory(enc->pos, data, p - data);
  1188. enc->pos += p - data;
  1189. }
  1190. MemFree(data);
  1191. return rc;
  1192. }
  1193. ASN1EncSetError(enc, ASN1_ERR_MEMORY);
  1194. return 0;
  1195. } // if (l)
  1196. /* encode zero length */
  1197. return ASN1BEREncLength(enc, 0);
  1198. }
  1199. return 0;
  1200. }
  1201. /* encode an octet string value (CER) */
  1202. int ASN1CEREncOctetString(ASN1encoding_t enc, ASN1uint32_t tag, ASN1uint32_t len, ASN1octet_t *val)
  1203. {
  1204. ASN1uint32_t n;
  1205. if (len <= 1000)
  1206. {
  1207. /* encode tag */
  1208. if (ASN1BEREncTag(enc, tag))
  1209. {
  1210. /* encode length */
  1211. if (ASN1BEREncLength(enc, len))
  1212. {
  1213. /* copy value */
  1214. CopyMemory(enc->pos, val, len);
  1215. enc->pos += len;
  1216. return 1;
  1217. }
  1218. }
  1219. }
  1220. else
  1221. {
  1222. ASN1uint32_t nLenOff;
  1223. /* encode value as constructed, using segments of 1000 octets */
  1224. if (ASN1BEREncExplicitTag(enc, tag, &nLenOff))
  1225. {
  1226. while (len)
  1227. {
  1228. n = len > 1000 ? 1000 : len;
  1229. if (ASN1BEREncTag(enc, 0x4))
  1230. {
  1231. if (ASN1BEREncLength(enc, n))
  1232. {
  1233. CopyMemory(enc->pos, val, n);
  1234. enc->pos += n;
  1235. val += n;
  1236. len -= n;
  1237. }
  1238. else
  1239. {
  1240. return 0;
  1241. }
  1242. }
  1243. else
  1244. {
  1245. return 0;
  1246. }
  1247. }
  1248. return ASN1BEREncEndOfContents(enc, nLenOff);
  1249. }
  1250. }
  1251. return 0;
  1252. }
  1253. /* encode an open type value */
  1254. int ASN1BEREncOpenType(ASN1encoding_t enc, ASN1open_t *val)
  1255. {
  1256. if (ASN1BEREncCheck(enc, val->length))
  1257. {
  1258. /* copy value */
  1259. CopyMemory(enc->pos, val->encoded, val->length);
  1260. enc->pos += val->length;
  1261. return 1;
  1262. }
  1263. return 0;
  1264. }
  1265. /* remove trailing zero bits from bit string */
  1266. int ASN1BEREncRemoveZeroBits(ASN1uint32_t *nbits, ASN1octet_t *val)
  1267. {
  1268. ASN1uint32_t n;
  1269. int i;
  1270. /* get value */
  1271. n = *nbits;
  1272. /* let val point to last ASN1octet used */
  1273. val += (n - 1) / 8;
  1274. /* check if broken ASN1octet consist out of zero bits */
  1275. if ((n & 7) && !(*val & bitmsk2[n & 7]))
  1276. {
  1277. n &= ~7;
  1278. val--;
  1279. }
  1280. /* scan complete ASN1octets (memcchr missing ...) */
  1281. if (!(n & 7))
  1282. {
  1283. while (n && !*val)
  1284. {
  1285. n -= 8;
  1286. val--;
  1287. }
  1288. }
  1289. /* scan current ASN1octet bit after bit */
  1290. if (n)
  1291. {
  1292. for (i = (n - 1) & 7; i >= 0; i--)
  1293. {
  1294. if (*val & (0x80 >> i))
  1295. break;
  1296. n--;
  1297. }
  1298. }
  1299. /* return real bitstring len */
  1300. *nbits = n;
  1301. return 1;
  1302. }
  1303. /* encode an utc time value */
  1304. int ASN1BEREncUTCTime(ASN1encoding_t enc, ASN1uint32_t tag, ASN1utctime_t *val)
  1305. {
  1306. char time[32];
  1307. ASN1utctime2string(time, val);
  1308. return ASN1BEREncCharString(enc, tag, My_lstrlenA(time), time);
  1309. }
  1310. /* encode an utc time value (CER) */
  1311. int ASN1CEREncUTCTime(ASN1encoding_t enc, ASN1uint32_t tag, ASN1utctime_t *val)
  1312. {
  1313. char time[32];
  1314. ASN1utctime2string(time, val);
  1315. return ASN1CEREncCharString(enc, tag, My_lstrlenA(time), time);
  1316. }
  1317. /* end of encoding */
  1318. int ASN1BEREncFlush(ASN1encoding_t enc)
  1319. {
  1320. /* allocate at least one octet */
  1321. if (enc->buf)
  1322. {
  1323. /* fill in zero-octet if encoding is empty bitstring */
  1324. if (enc->buf == enc->pos)
  1325. *enc->pos++ = 0;
  1326. /* calculate length */
  1327. enc->len = (ASN1uint32_t) (enc->pos - enc->buf);
  1328. return 1;
  1329. }
  1330. return ASN1BEREncCheck(enc, 1);
  1331. }
  1332. #endif // ENABLE_BER