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.

1729 lines
51 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 <math.h>
  5. #include "cintern.h"
  6. #if HAS_IEEEFP_H
  7. #include <ieeefp.h>
  8. #elif HAS_FLOAT_H
  9. #include <float.h>
  10. #endif
  11. #define ENCODE_BUFFER_INCREMENT 1024
  12. void PerEncAdvance(ASN1encoding_t enc, ASN1uint32_t nbits)
  13. {
  14. enc->pos += ((enc->bit + nbits) >> 3);
  15. enc->bit = (enc->bit + nbits) & 7;
  16. }
  17. static const ASN1uint8_t c_aBitMask2[] =
  18. {
  19. 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
  20. };
  21. /* compare two string table entries */
  22. /* return 0 if a1 == a2, 1 if a1 > a2 and -1 if a1 < a2 */
  23. static int __cdecl ASN1CmpStringTableEntries(const void *a1, const void *a2)
  24. {
  25. ASN1stringtableentry_t *c1 = (ASN1stringtableentry_t *)a1;
  26. ASN1stringtableentry_t *c2 = (ASN1stringtableentry_t *)a2;
  27. if (c1->upper < c2->lower)
  28. return -1;
  29. return ((c1->lower > c2->upper) ? 1 : 0);
  30. }
  31. /* check for space in buffer for PER and BER. */
  32. int ASN1EncCheck(ASN1encoding_t enc, ASN1uint32_t noctets)
  33. {
  34. // any additional space required?
  35. if (noctets)
  36. {
  37. // buffer exists?
  38. if (NULL != enc->buf)
  39. {
  40. // buffer large enough?
  41. if (enc->size - (enc->pos - enc->buf) - ((enc->bit != 0) ? 1 : 0) >= noctets)
  42. {
  43. return 1;
  44. }
  45. // static buffer overflow?
  46. if (enc->dwFlags & ASN1ENCODE_SETBUFFER)
  47. {
  48. ASN1EncSetError(enc, ASN1_ERR_OVERFLOW);
  49. return 0;
  50. }
  51. else
  52. {
  53. // round up to next 256 byte boundary and resize buffer
  54. ASN1octet_t *oldbuf = enc->buf;
  55. // enc->size = ((noctets + (enc->pos - oldbuf) + (enc->bit != 0) - 1) | 255) + 1;
  56. if (ASN1_PER_RULE & enc->eRule)
  57. {
  58. enc->size += max(noctets, ENCODE_BUFFER_INCREMENT);
  59. }
  60. else
  61. {
  62. enc->size += max(noctets, enc->size);
  63. }
  64. enc->buf = (ASN1octet_t *)EncMemReAlloc(enc, enc->buf, enc->size);
  65. if (NULL != enc->buf)
  66. {
  67. enc->pos = enc->buf + (enc->pos - oldbuf);
  68. }
  69. else
  70. {
  71. ASN1EncSetError(enc, ASN1_ERR_MEMORY);
  72. return 0;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. // no buffer exists, allocate new one.
  79. // round up to next 256 byte boundary and allocate buffer
  80. // enc->size = ((noctets - 1) | 255) + 1;
  81. enc->size = max(noctets + enc->cbExtraHeader, ENCODE_BUFFER_INCREMENT);
  82. enc->buf = EncMemAlloc(enc, enc->size);
  83. if (NULL != enc->buf)
  84. {
  85. enc->pos = (ASN1octet_t *) (enc->buf + enc->cbExtraHeader);
  86. }
  87. else
  88. {
  89. enc->pos = NULL;
  90. ASN1EncSetError(enc, ASN1_ERR_MEMORY);
  91. return 0;
  92. }
  93. }
  94. }
  95. return 1;
  96. }
  97. /* encode a zero octet string of length nbits */
  98. int ASN1PEREncZero(ASN1encoding_t enc, ASN1uint32_t nbits)
  99. {
  100. /* nothing to encode? */
  101. if (nbits)
  102. {
  103. /* get enough space in buffer */
  104. if (ASN1PEREncCheck(enc, (nbits + enc->bit + 7) / 8))
  105. {
  106. /* clear bits */
  107. ASN1bitclr(enc->pos, enc->bit, nbits);
  108. PerEncAdvance(enc, nbits);
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. return 1;
  114. }
  115. /* encode a bit */
  116. int ASN1PEREncBit(ASN1encoding_t enc, ASN1uint32_t val)
  117. {
  118. /* get enough space in buffer */
  119. if (ASN1PEREncCheck(enc, 1))
  120. {
  121. /* put one bit */
  122. if (val)
  123. {
  124. *enc->pos |= 0x80 >> enc->bit;
  125. }
  126. if (enc->bit < 7)
  127. {
  128. enc->bit++;
  129. }
  130. else
  131. {
  132. enc->bit = 0;
  133. enc->pos++;
  134. }
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. /* encode an integer value into a bit field of given size */
  140. int ASN1PEREncBitVal(ASN1encoding_t enc, ASN1uint32_t nbits, ASN1uint32_t val)
  141. {
  142. /* nothing to encode? */
  143. if (nbits)
  144. {
  145. /* get enough space in buffer */
  146. if (ASN1PEREncCheck(enc, (nbits + enc->bit + 7) / 8))
  147. {
  148. /* put bits */
  149. ASN1bitput(enc->pos, enc->bit, val, nbits);
  150. PerEncAdvance(enc, nbits);
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. return 1;
  156. }
  157. /* encode an integer value of intx type into a bit field of given size */
  158. int ASN1PEREncBitIntx(ASN1encoding_t enc, ASN1uint32_t nbits, ASN1intx_t *val)
  159. {
  160. /* nothing to encode? */
  161. if (nbits)
  162. {
  163. /* get enough space in buffer */
  164. if (ASN1PEREncCheck(enc, (nbits + enc->bit + 7) / 8))
  165. {
  166. /* stuff sign bits into if value encoding is too small */
  167. if (nbits > 8 * val->length)
  168. {
  169. if (val->value[0] > 0x7f)
  170. ASN1bitset(enc->pos, enc->bit, nbits - 8 * val->length);
  171. else
  172. ASN1bitclr(enc->pos, enc->bit, nbits - 8 * val->length);
  173. PerEncAdvance(enc, nbits - 8 * val->length);
  174. nbits = 8 * val->length;
  175. }
  176. /* copy bits of value */
  177. ASN1bitcpy(enc->pos, enc->bit, val->value, 8 * val->length - nbits, nbits);
  178. PerEncAdvance(enc, nbits);
  179. return 1;
  180. }
  181. return 0;
  182. }
  183. return 1;
  184. }
  185. /* encode a bit field of given size */
  186. int ASN1PEREncBits(ASN1encoding_t enc, ASN1uint32_t nbits, ASN1octet_t *val)
  187. {
  188. /* nothing to encode? */
  189. if (nbits)
  190. {
  191. /* get enough space in buffer */
  192. if (ASN1PEREncCheck(enc, (nbits + enc->bit + 7) / 8))
  193. {
  194. /* copy bits */
  195. ASN1bitcpy(enc->pos, enc->bit, val, 0, nbits);
  196. PerEncAdvance(enc, nbits);
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. /* encode a normally small integer value */
  204. int ASN1PEREncNormallySmall(ASN1encoding_t enc, ASN1uint32_t val)
  205. {
  206. ASN1uint32_t noctets;
  207. /* is normally small ASN1really small? */
  208. if (val < 64)
  209. {
  210. return ASN1PEREncBitVal(enc, 7, val);
  211. }
  212. /* large */
  213. if (ASN1PEREncBitVal(enc, 1, 1))
  214. {
  215. ASN1PEREncAlignment(enc);
  216. noctets = ASN1uint32_uoctets(val);
  217. if (ASN1PEREncCheck(enc, noctets + 1))
  218. {
  219. EncAssert(enc, noctets < 256);
  220. *enc->pos++ = (ASN1octet_t) noctets;
  221. ASN1octetput(enc->pos, val, noctets);
  222. enc->pos += noctets;
  223. return 1;
  224. }
  225. }
  226. return 0;
  227. }
  228. /* encode a bit field with a normally small length */
  229. int ASN1PEREncNormallySmallBits(ASN1encoding_t enc, ASN1uint32_t nbits, ASN1octet_t *val)
  230. {
  231. /* is normally small really small? */
  232. if (nbits <= 64)
  233. {
  234. if (ASN1PEREncBitVal(enc, 7, nbits - 1))
  235. {
  236. return ASN1PEREncBits(enc, nbits, val);
  237. }
  238. }
  239. /* large */
  240. else
  241. {
  242. if (ASN1PEREncBitVal(enc, 1, 1))
  243. {
  244. return ASN1PEREncFragmented(enc, nbits, val, 1);
  245. }
  246. }
  247. return 0;
  248. }
  249. /* encode an octet string of given length */
  250. #ifdef ENABLE_ALL
  251. int ASN1PEREncOctets(ASN1encoding_t enc, ASN1uint32_t noctets, ASN1octet_t *val)
  252. {
  253. /* nothing to encode? */
  254. if (noctets)
  255. {
  256. /* get enough space in buffer */
  257. if (ASN1PEREncCheck(enc, noctets + (enc->bit != 0)))
  258. {
  259. ASN1bitcpy(enc->pos, enc->bit, val, 0, noctets * 8);
  260. PerEncAdvance(enc, noctets * 8);
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. return 1;
  266. }
  267. #endif // ENABLE_ALL
  268. /* encode a string of given length and fixed character size */
  269. int ASN1PEREncCharString(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char_t *val, ASN1uint32_t nbits)
  270. {
  271. /* nothing to encode? */
  272. if (nchars)
  273. {
  274. /* get enough space in buffer */
  275. if (ASN1PEREncCheck(enc, (nbits * nchars + enc->bit + 7) / 8))
  276. {
  277. /* same src and dst charsize? then do it simple (and fast!) */
  278. if (nbits == 8)
  279. {
  280. ASN1bitcpy(enc->pos, enc->bit, (ASN1octet_t *)val, 0, nchars * 8);
  281. PerEncAdvance(enc, nchars * 8);
  282. return 1;
  283. }
  284. /* copy characters one by one */
  285. while (nchars--)
  286. {
  287. ASN1bitput(enc->pos, enc->bit, *val++, nbits);
  288. PerEncAdvance(enc, nbits);
  289. }
  290. return 1;
  291. }
  292. return 0;
  293. }
  294. return 1;
  295. }
  296. /* encode a 16 bit string of given length and fixed character size */
  297. int ASN1PEREncChar16String(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char16_t *val, ASN1uint32_t nbits)
  298. {
  299. /* nothing to encode? */
  300. if (nchars)
  301. {
  302. /* octet aligned and same src and dst charsize?
  303. then do it simple (and fast!) */
  304. if (!enc->bit && nbits == 16)
  305. {
  306. if (ASN1PEREncCheck(enc, nchars * 2))
  307. {
  308. while (nchars--)
  309. {
  310. *enc->pos++ = (ASN1octet_t)(*val >> 8);
  311. *enc->pos++ = (ASN1octet_t)(*val);
  312. val++;
  313. }
  314. return 1;
  315. }
  316. return 0;
  317. }
  318. /* get enough space in buffer */
  319. if (ASN1PEREncCheck(enc, (nbits * nchars + enc->bit + 7) / 8))
  320. {
  321. /* copy characters one by one */
  322. while (nchars--)
  323. {
  324. ASN1bitput(enc->pos, enc->bit, *val++, nbits);
  325. PerEncAdvance(enc, nbits);
  326. }
  327. return 1;
  328. }
  329. return 0;
  330. }
  331. return 1;
  332. }
  333. /* encode a 32 bit string of given length and fixed character size */
  334. #ifdef ENABLE_ALL
  335. int ASN1PEREncChar32String(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char32_t *val, ASN1uint32_t nbits)
  336. {
  337. /* nothing to encode? */
  338. if (nchars)
  339. {
  340. /* octet aligned and same src and dst charsize?
  341. then do it simple (and fast!) */
  342. if (!enc->bit && nbits == 32)
  343. {
  344. if (ASN1PEREncCheck(enc, nchars * 4))
  345. {
  346. while (nchars--)
  347. {
  348. *enc->pos++ = (ASN1octet_t)(*val >> 24);
  349. *enc->pos++ = (ASN1octet_t)(*val >> 16);
  350. *enc->pos++ = (ASN1octet_t)(*val >> 8);
  351. *enc->pos++ = (ASN1octet_t)(*val);
  352. val++;
  353. }
  354. return 1;
  355. }
  356. return 0;
  357. }
  358. /* get enough space in buffer */
  359. if (ASN1PEREncCheck(enc, (nbits * nchars + enc->bit + 7) / 8))
  360. {
  361. /* copy characters */
  362. while (nchars--)
  363. {
  364. ASN1bitput(enc->pos, enc->bit, *val++, nbits);
  365. PerEncAdvance(enc, nbits);
  366. }
  367. return 1;
  368. }
  369. return 0;
  370. }
  371. return 1;
  372. }
  373. #endif // ENABLE_ALL
  374. /* encode a table string of given length and fixed character size */
  375. int ASN1PEREncTableCharString(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char_t *val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  376. {
  377. /* nothing to encode? */
  378. if (nchars)
  379. {
  380. /* get enough space in buffer */
  381. if (ASN1PEREncCheck(enc, (nbits * nchars + enc->bit + 7) / 8))
  382. {
  383. /* copy characters one by one */
  384. while (nchars--)
  385. {
  386. ASN1stringtableentry_t chr, *entry;
  387. chr.lower = chr.upper = (unsigned char)*val++;
  388. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  389. table->length, sizeof(ASN1stringtableentry_t),
  390. ASN1CmpStringTableEntries);
  391. ASN1bitput(enc->pos, enc->bit,
  392. entry ? entry->value + (chr.lower - entry->lower) : 0, nbits);
  393. PerEncAdvance(enc, nbits);
  394. }
  395. return 1;
  396. }
  397. return 0;
  398. }
  399. return 1;
  400. }
  401. /* encode a 16 bit table string of given length and fixed character size */
  402. int ASN1PEREncTableChar16String(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char16_t *val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  403. {
  404. /* nothing to encode? */
  405. if (nchars)
  406. {
  407. /* get enough space in buffer */
  408. if (ASN1PEREncCheck(enc, (nbits * nchars + enc->bit + 7) / 8))
  409. {
  410. /* copy characters one by one */
  411. while (nchars--)
  412. {
  413. ASN1stringtableentry_t chr, *entry;
  414. chr.lower = chr.upper = *val++;
  415. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  416. table->length, sizeof(ASN1stringtableentry_t),
  417. ASN1CmpStringTableEntries);
  418. ASN1bitput(enc->pos, enc->bit,
  419. entry ? entry->value + (chr.lower - entry->lower) : 0, nbits);
  420. PerEncAdvance(enc, nbits);
  421. }
  422. return 1;
  423. }
  424. return 0;
  425. }
  426. return 1;
  427. }
  428. /* encode a 32 bit table string of given length and fixed character size */
  429. #ifdef ENABLE_ALL
  430. int ASN1PEREncTableChar32String(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char32_t *val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  431. {
  432. /* nothing to encode? */
  433. if (nchars)
  434. {
  435. /* get enough space in buffer */
  436. if (ASN1PEREncCheck(enc, (nbits * nchars + enc->bit + 7) / 8))
  437. {
  438. /* copy characters one by one */
  439. while (nchars--)
  440. {
  441. ASN1stringtableentry_t chr, *entry;
  442. chr.lower = chr.upper = *val++;
  443. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  444. table->length, sizeof(ASN1stringtableentry_t),
  445. ASN1CmpStringTableEntries);
  446. ASN1bitput(enc->pos, enc->bit,
  447. entry ? entry->value + (chr.lower - entry->lower) : 0, nbits);
  448. PerEncAdvance(enc, nbits);
  449. }
  450. return 1;
  451. }
  452. return 0;
  453. }
  454. return 1;
  455. }
  456. #endif // ENABLE_ALL
  457. /* encode a fragmented string of given length and fixed character size */
  458. int ASN1PEREncFragmentedCharString(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char_t *val, ASN1uint32_t nbits)
  459. {
  460. ASN1uint32_t n = 0x4000;
  461. /* encode fragments */
  462. while (nchars)
  463. {
  464. if (ASN1PEREncFragmentedLength(&n, enc, nchars))
  465. {
  466. if (ASN1PEREncCharString(enc, n, val, nbits))
  467. {
  468. nchars -= n;
  469. val += n;
  470. }
  471. else
  472. {
  473. return 0;
  474. }
  475. }
  476. else
  477. {
  478. return 0;
  479. }
  480. }
  481. /* add zero length octet if last fragment contained more than 16K chars */
  482. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  483. }
  484. /* encode a fragmented 16 bit string of given length and fixed character size */
  485. #ifdef ENABLE_ALL
  486. int ASN1PEREncFragmentedChar16String(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char16_t *val, ASN1uint32_t nbits)
  487. {
  488. ASN1uint32_t n = 0x4000;
  489. /* encode fragments */
  490. while (nchars)
  491. {
  492. if (ASN1PEREncFragmentedLength(&n, enc, nchars))
  493. {
  494. if (ASN1PEREncChar16String(enc, n, val, nbits))
  495. {
  496. nchars -= n;
  497. val += n;
  498. }
  499. else
  500. {
  501. return 0;
  502. }
  503. }
  504. else
  505. {
  506. return 0;
  507. }
  508. }
  509. /* add zero length octet if last fragment contained more than 16K chars */
  510. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  511. }
  512. #endif // ENABLE_ALL
  513. /* encode a fragmented 32 bit string of given length and fixed character size */
  514. #ifdef ENABLE_ALL
  515. int ASN1PEREncFragmentedChar32String(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char32_t *val, ASN1uint32_t nbits)
  516. {
  517. ASN1uint32_t n = 0x4000;
  518. /* encode fragments */
  519. while (nchars)
  520. {
  521. if (ASN1PEREncFragmentedLength(&n, enc, nchars))
  522. {
  523. if (ASN1PEREncChar32String(enc, n, val, nbits))
  524. {
  525. nchars -= n;
  526. val += n;
  527. }
  528. else
  529. {
  530. return 0;
  531. }
  532. }
  533. else
  534. {
  535. return 0;
  536. }
  537. }
  538. /* add zero length octet if last fragment contained more than 16K chars */
  539. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  540. }
  541. #endif // ENABLE_ALL
  542. /* encode a fragmented table string of given length and fixed character size */
  543. #ifdef ENABLE_ALL
  544. int ASN1PEREncFragmentedTableCharString(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char_t *val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  545. {
  546. ASN1uint32_t n = 0x4000;
  547. /* encode fragments */
  548. while (nchars)
  549. {
  550. if (ASN1PEREncFragmentedLength(&n, enc, nchars))
  551. {
  552. if (ASN1PEREncTableCharString(enc, n, val, nbits, table))
  553. {
  554. nchars -= n;
  555. val += n;
  556. }
  557. else
  558. {
  559. return 0;
  560. }
  561. }
  562. else
  563. {
  564. return 0;
  565. }
  566. }
  567. /* add zero length octet if last fragment contained more than 16K chars */
  568. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  569. }
  570. #endif // ENABLE_ALL
  571. /* encode a fragmented 16 bit table string of given length and fixed */
  572. /* character size */
  573. #ifdef ENABLE_ALL
  574. int ASN1PEREncFragmentedTableChar16String(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char16_t *val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  575. {
  576. ASN1uint32_t n = 0x4000;
  577. /* encode fragments */
  578. while (nchars)
  579. {
  580. if (ASN1PEREncFragmentedLength(&n, enc, nchars))
  581. {
  582. if (ASN1PEREncTableChar16String(enc, n, val, nbits, table))
  583. {
  584. nchars -= n;
  585. val += n;
  586. }
  587. else
  588. {
  589. return 0;
  590. }
  591. }
  592. else
  593. {
  594. return 0;
  595. }
  596. }
  597. /* add zero length octet if last fragment contained more than 16K chars */
  598. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  599. }
  600. #endif // ENABLE_ALL
  601. /* encode a fragmented 32 bit table string of given length and fixed */
  602. /* character size */
  603. #ifdef ENABLE_ALL
  604. int ASN1PEREncFragmentedTableChar32String(ASN1encoding_t enc, ASN1uint32_t nchars, ASN1char32_t *val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  605. {
  606. ASN1uint32_t n = 0x4000;
  607. /* encode fragments */
  608. while (nchars)
  609. {
  610. if (ASN1PEREncFragmentedLength(&n, enc, nchars))
  611. {
  612. if (ASN1PEREncTableChar32String(enc, n, val, nbits, table))
  613. {
  614. nchars -= n;
  615. val += n;
  616. }
  617. else
  618. {
  619. return 0;
  620. }
  621. }
  622. else
  623. {
  624. return 0;
  625. }
  626. }
  627. /* add zero length octet if last fragment contained more than 16K chars */
  628. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  629. }
  630. #endif // ENABLE_ALL
  631. #if 0
  632. #ifdef ENABLE_ALL
  633. int ASN1PEREncCheckTableCharString(ASN1uint32_t nchars, ASN1char_t *val, ASN1stringtable_t *table)
  634. {
  635. ASN1stringtableentry_t chr;
  636. /* check if every char is in given string table */
  637. while (nchars--) {
  638. chr.lower = chr.upper = (unsigned char)*val++;
  639. if (!ms_bSearch(&chr, table->values,
  640. table->length, sizeof(ASN1stringtableentry_t),
  641. ASN1CmpStringTableEntries))
  642. return 0;
  643. }
  644. return 1;
  645. }
  646. #endif // ENABLE_ALL
  647. #ifdef ENABLE_ALL
  648. int ASN1PEREncCheckTableChar16String(ASN1uint32_t nchars, ASN1char16_t *val, ASN1stringtable_t *table)
  649. {
  650. ASN1stringtableentry_t chr;
  651. /* check if every char is in given string table */
  652. while (nchars--) {
  653. chr.lower = chr.upper = *val++;
  654. if (!ms_bSearch(&chr, table->values,
  655. table->length, sizeof(ASN1stringtableentry_t),
  656. ASN1CmpStringTableEntries))
  657. return 0;
  658. }
  659. return 1;
  660. }
  661. #endif // ENABLE_ALL
  662. #ifdef ENABLE_ALL
  663. int ASN1PEREncCheckTableChar32String(ASN1uint32_t nchars, ASN1char32_t *val, ASN1stringtable_t *table)
  664. {
  665. ASN1stringtableentry_t chr;
  666. /* check if every char is in given string table */
  667. while (nchars--) {
  668. chr.lower = chr.upper = *val++;
  669. if (!ms_bSearch(&chr, table->values,
  670. table->length, sizeof(ASN1stringtableentry_t),
  671. ASN1CmpStringTableEntries))
  672. return 0;
  673. }
  674. return 1;
  675. }
  676. #endif // ENABLE_ALL
  677. #endif // 0
  678. /* remove trailing zero bits of a bit string */
  679. #ifdef ENABLE_ALL
  680. int ASN1PEREncRemoveZeroBits(ASN1uint32_t *nbits, ASN1octet_t *val, ASN1uint32_t minlen)
  681. {
  682. ASN1uint32_t n;
  683. int i;
  684. /* get value */
  685. n = *nbits;
  686. /* nothing to scan? */
  687. if (n > minlen)
  688. {
  689. /* let val point to last ASN1octet used */
  690. val += (n - 1) / 8;
  691. /* check if broken ASN1octet consist out of zero bits */
  692. if ((n & 7) && !(*val & c_aBitMask2[n & 7])) {
  693. n &= ~7;
  694. val--;
  695. }
  696. /* scan complete ASN1octets (memrchr missing ...) */
  697. if (!(n & 7)) {
  698. while (n > minlen && !*val) {
  699. n -= 8;
  700. val--;
  701. }
  702. }
  703. /* scan current octet bit after bit */
  704. if (n > minlen) {
  705. for (i = (n - 1) & 7; i >= 0; i--) {
  706. if (*val & (0x80 >> i))
  707. break;
  708. n--;
  709. }
  710. }
  711. /* return real bitstring len */
  712. *nbits = n < minlen ? minlen : n;
  713. }
  714. return 1;
  715. }
  716. #endif // ENABLE_ALL
  717. /* encode a fragmented integer of intx type */
  718. #ifdef ENABLE_ALL
  719. int ASN1PEREncFragmentedIntx(ASN1encoding_t enc, ASN1intx_t *val)
  720. {
  721. ASN1uint32_t noctets;
  722. ASN1uint32_t n = 0x4000;
  723. ASN1octet_t *v;
  724. ASN1uint32_t val1, val2;
  725. /* get length */
  726. noctets = val->length;
  727. /* get value */
  728. v = val->value;
  729. /* required size:
  730. noctets for data itself
  731. + noctets / 0x10000 for 64K-fragment size prefixes
  732. + ((noctets & 0xc000) > 0) for last 16K..48K-fragment size prefix
  733. + 1 for size prefix of remaining <128 octets
  734. + ((noctets & 0x3fff) >= 0x80) for additional octet if rem. data >= 128
  735. */
  736. val1 = ((noctets & 0xc000) > 0) ? 1 : 0;
  737. val2 = ((noctets & 0x3fff) >= 0x80) ? 1 : 0;
  738. if (ASN1PEREncCheck(enc, noctets + noctets / 0x10000 + val1 + 1 + val2))
  739. {
  740. /* encode fragments */
  741. while (noctets)
  742. {
  743. if (ASN1PEREncFragmentedLength(&n, enc, noctets))
  744. {
  745. CopyMemory(enc->pos, v, n);
  746. enc->pos += n;
  747. noctets -= n;
  748. v += n;
  749. }
  750. else
  751. {
  752. return 0;
  753. }
  754. }
  755. /* add zero length octet if last fragment contained more than 16K octets */
  756. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  757. }
  758. return 0;
  759. }
  760. #endif // ENABLE_ALL
  761. /* encode a fragmented unsigned integer of intx type */
  762. #ifdef ENABLE_ALL
  763. int ASN1PEREncFragmentedUIntx(ASN1encoding_t enc, ASN1intx_t *val)
  764. {
  765. ASN1uint32_t noctets;
  766. ASN1uint32_t n = 0x4000;
  767. ASN1octet_t *v;
  768. ASN1uint32_t val1, val2;
  769. /* get length */
  770. noctets = ASN1intx_uoctets(val);
  771. /* get value */
  772. v = val->value + val->length - noctets;
  773. /* required size:
  774. noctets for data itself
  775. + noctets / 0x10000 for 64K-fragment size prefixes
  776. + ((noctets & 0xc000) > 0) for last 16K..48K-fragment size prefix
  777. + 1 for size prefix of remaining <128 octets
  778. + ((noctets & 0x3fff) >= 0x80) for additional octet if rem. data >= 128
  779. */
  780. val1 = ((noctets & 0xc000) > 0) ? 1 : 0;
  781. val2 = ((noctets & 0x3fff) >= 0x80) ? 1 : 0;
  782. if (ASN1PEREncCheck(enc, noctets + noctets / 0x10000 + val1 + 1 + val2))
  783. {
  784. /* encode fragments */
  785. while (noctets)
  786. {
  787. if (ASN1PEREncFragmentedLength(&n, enc, noctets))
  788. {
  789. CopyMemory(enc->pos, v, n);
  790. enc->pos += n;
  791. noctets -= n;
  792. v += n;
  793. }
  794. else
  795. {
  796. return 0;
  797. }
  798. }
  799. /* add zero length octet if last fragment contained more than 16K octets */
  800. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  801. }
  802. return 0;
  803. }
  804. #endif // ENABLE_ALL
  805. /* encode a fragment length */
  806. int ASN1PEREncFragmentedLength(ASN1uint32_t *len, ASN1encoding_t enc, ASN1uint32_t nitems)
  807. {
  808. /* always ASN1octet aligned */
  809. ASN1PEREncAlignment(enc);
  810. /* fragmented encoding:
  811. *
  812. * - nitems < 0x80:
  813. * octet #1:
  814. * bit 8: 0,
  815. * bit 7..1: nitems
  816. * octet #2..:
  817. * nitems items
  818. * - 0x80 <= nitems < 0x4000:
  819. * octet #1:
  820. * bit 8..7: 10,
  821. * bit 6..1: bit 14..9 of nitems
  822. * octet #2:
  823. * bit 8..1: bit 8..1 of nitems
  824. * octet #3..:
  825. * nitems items
  826. * - 0x4000 <= nitems < 0x10000:
  827. * octet #1:
  828. * bit 8..7: 11,
  829. * bit 6..1: nitems / 0x4000
  830. * octet #2..:
  831. * (nitems & 0xc000) items
  832. * - 0x10000 <= nitems:
  833. * octet #1:
  834. * bit 8..1: 11000100
  835. * octet #2..:
  836. * 0x10000 items
  837. */
  838. if (nitems < 0x80)
  839. {
  840. if (ASN1PEREncCheck(enc, 1))
  841. {
  842. *enc->pos++ = (ASN1octet_t)nitems;
  843. *len = nitems;
  844. return 1;
  845. }
  846. }
  847. else
  848. if (nitems < 0x4000)
  849. {
  850. if (ASN1PEREncCheck(enc, 2))
  851. {
  852. *enc->pos++ = (ASN1octet_t)(0x80 | (nitems >> 8));
  853. *enc->pos++ = (ASN1octet_t)nitems;
  854. *len = nitems;
  855. return 1;
  856. }
  857. }
  858. else
  859. if (nitems < 0x10000)
  860. {
  861. if (ASN1PEREncCheck(enc, 1))
  862. {
  863. *enc->pos++ = (ASN1octet_t)(0xc0 | (nitems >> 14));
  864. *len = nitems & 0xc000;
  865. return 1;
  866. }
  867. }
  868. else
  869. {
  870. if (ASN1PEREncCheck(enc, 1))
  871. {
  872. *enc->pos++ = (ASN1octet_t)0xc4;
  873. *len = 0x10000;
  874. return 1;
  875. }
  876. }
  877. return 0;
  878. }
  879. /* encode a fragment bit string containing nitems of size itemsize */
  880. int ASN1PEREncFragmented(ASN1encoding_t enc, ASN1uint32_t nitems, ASN1octet_t *val, ASN1uint32_t itemsize)
  881. {
  882. ASN1uint32_t n = 0x4000;
  883. ASN1uint32_t noctets = (nitems * itemsize + 7) / 8;
  884. /* required size:
  885. + noctets for data itself
  886. + nitems / 0x10000 for 64K-fragment size prefixes
  887. + ((nitems & 0xc000) > 0) for last 16K..48K-fragment size prefix
  888. + 1 for size prefix of remaining <128 ASN1octets
  889. + ((nitems & 0x3fff) >= 0x80) for additional ASN1octet if rem. data >= 128
  890. */
  891. if (ASN1PEREncCheck(enc, noctets + nitems / 0x10000 + ((nitems & 0xc000) > 0) + 1 + ((nitems & 0x3fff) >= 0x80)))
  892. {
  893. /* encode fragments */
  894. while (nitems)
  895. {
  896. if (ASN1PEREncFragmentedLength(&n, enc, nitems))
  897. {
  898. ASN1bitcpy(enc->pos, 0, val, 0, n * itemsize);
  899. PerEncAdvance(enc, n * itemsize);
  900. nitems -= n;
  901. val += n * itemsize / 8;
  902. }
  903. else
  904. {
  905. return 0;
  906. }
  907. }
  908. /* add zero length octet if last fragment contained more than 16K items */
  909. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  910. }
  911. return 0;
  912. }
  913. int ASN1PEREncFlushFragmentedToParent(ASN1encoding_t enc)
  914. {
  915. // make sure it is parented
  916. EncAssert(enc, ((ASN1INTERNencoding_t)enc)->parent != (ASN1INTERNencoding_t)enc);
  917. if (ASN1PEREncFlush(enc))
  918. {
  919. if (ASN1PEREncFragmented((ASN1encoding_t) ((ASN1INTERNencoding_t)enc)->parent,
  920. enc->len, enc->buf, 8))
  921. {
  922. // reset the buffer, i.e. keep enc->buf and enc->size
  923. enc->pos = enc->buf;
  924. enc->len = enc->bit = 0;
  925. return 1;
  926. }
  927. }
  928. return 0;
  929. }
  930. ASN1octet_t * _PEREncOidNode(ASN1octet_t *p, ASN1uint32_t s)
  931. {
  932. if (s < 0x80)
  933. {
  934. *p++ = (ASN1octet_t)(s);
  935. }
  936. else
  937. if (s < 0x4000)
  938. {
  939. *p++ = (ASN1octet_t)((s >> 7) | 0x80);
  940. *p++ = (ASN1octet_t)(s & 0x7f);
  941. }
  942. else
  943. if (s < 0x200000)
  944. {
  945. *p++ = (ASN1octet_t)((s >> 14) | 0x80);
  946. *p++ = (ASN1octet_t)((s >> 7) | 0x80);
  947. *p++ = (ASN1octet_t)(s & 0x7f);
  948. }
  949. else
  950. if (s < 0x10000000)
  951. {
  952. *p++ = (ASN1octet_t)((s >> 21) | 0x80);
  953. *p++ = (ASN1octet_t)((s >> 14) | 0x80);
  954. *p++ = (ASN1octet_t)((s >> 7) | 0x80);
  955. *p++ = (ASN1octet_t)(s & 0x7f);
  956. }
  957. else
  958. {
  959. *p++ = (ASN1octet_t)((s >> 28) | 0x80);
  960. *p++ = (ASN1octet_t)((s >> 21) | 0x80);
  961. *p++ = (ASN1octet_t)((s >> 14) | 0x80);
  962. *p++ = (ASN1octet_t)((s >> 7) | 0x80);
  963. *p++ = (ASN1octet_t)(s & 0x7f);
  964. }
  965. return p;
  966. }
  967. /* encode an object identifier */
  968. int ASN1PEREncObjectIdentifier(ASN1encoding_t enc, ASN1objectidentifier_t *val)
  969. {
  970. ASN1objectidentifier_t obj = *val;
  971. ASN1uint32_t l = GetObjectIdentifierCount(obj);
  972. if (l)
  973. {
  974. ASN1uint32_t i, s;
  975. ASN1octet_t *data, *p;
  976. int rc;
  977. /* convert object identifier to octets */
  978. p = data = (ASN1octet_t *)MemAlloc(l * 5, _ModName(enc)); /* max. 5 octets/subelement */
  979. if (p)
  980. {
  981. for (i = 0; i < l; i++)
  982. {
  983. s = obj->value;
  984. obj = obj->next;
  985. if (!i && l > 1)
  986. {
  987. s = s * 40 + obj->value;
  988. obj = obj->next;
  989. i++;
  990. }
  991. p = _PEREncOidNode(p, s);
  992. }
  993. /* encode octet string as fragmented octet string */
  994. rc = ASN1PEREncFragmented(enc, (ASN1uint32_t) (p - data), data, 8);
  995. MemFree(data);
  996. return rc;
  997. }
  998. ASN1EncSetError(enc, ASN1_ERR_MEMORY);
  999. return 0;
  1000. }
  1001. /* encode zero length */
  1002. return ASN1PEREncFragmented(enc, 0, NULL, 8);
  1003. }
  1004. /* encode an object identifier */
  1005. int ASN1PEREncObjectIdentifier2(ASN1encoding_t enc, ASN1objectidentifier2_t *val)
  1006. {
  1007. if (val->count)
  1008. {
  1009. ASN1uint32_t i, s;
  1010. ASN1octet_t *data, *p;
  1011. int rc;
  1012. /* convert object identifier to octets */
  1013. p = data = (ASN1octet_t *)MemAlloc(val->count * 5, _ModName(enc)); /* max. 5 octets/subelement */
  1014. if (p)
  1015. {
  1016. for (i = 0; i < val->count; i++)
  1017. {
  1018. s = val->value[i];
  1019. if (!i && val->count > 1)
  1020. {
  1021. i++;
  1022. s = s * 40 + val->value[i];
  1023. }
  1024. p = _PEREncOidNode(p, s);
  1025. }
  1026. /* encode octet string as fragmented octet string */
  1027. rc = ASN1PEREncFragmented(enc, (ASN1uint32_t) (p - data), data, 8);
  1028. MemFree(data);
  1029. return rc;
  1030. }
  1031. ASN1EncSetError(enc, ASN1_ERR_MEMORY);
  1032. return 0;
  1033. }
  1034. /* encode zero length */
  1035. return ASN1PEREncFragmented(enc, 0, NULL, 8);
  1036. }
  1037. /* encode real value with double representation */
  1038. int ASN1PEREncDouble(ASN1encoding_t enc, double dbl)
  1039. {
  1040. double mantissa;
  1041. int exponent;
  1042. ASN1octet_t mASN1octets[16]; /* should be enough */
  1043. ASN1uint32_t nmASN1octets;
  1044. ASN1octet_t eASN1octets[16]; /* should be enough */
  1045. ASN1uint32_t neASN1octets;
  1046. ASN1octet_t head;
  1047. ASN1uint32_t sign;
  1048. ASN1uint32_t len;
  1049. ASN1uint32_t n;
  1050. /* always octet aligned */
  1051. ASN1PEREncAlignment(enc);
  1052. /* check for PLUS_INFINITY */
  1053. if (ASN1double_ispinf(dbl))
  1054. {
  1055. if (ASN1PEREncCheck(enc, 2))
  1056. {
  1057. *enc->pos++ = 1;
  1058. *enc->pos++ = 0x40;
  1059. return 1;
  1060. }
  1061. }
  1062. else
  1063. /* check for MINUS_INFINITY */
  1064. if (ASN1double_isminf(dbl))
  1065. {
  1066. if (ASN1PEREncCheck(enc, 2))
  1067. {
  1068. *enc->pos++ = 1;
  1069. *enc->pos++ = 0x41;
  1070. return 1;
  1071. }
  1072. }
  1073. else
  1074. /* check for bad real value */
  1075. if (finite(dbl))
  1076. {
  1077. /* encode normal real value */
  1078. /* split into mantissa and exponent */
  1079. mantissa = frexp(dbl, &exponent);
  1080. /* check for zero value */
  1081. if (mantissa == 0.0 && exponent == 0)
  1082. {
  1083. if (ASN1PEREncCheck(enc, 1))
  1084. {
  1085. *enc->pos++ = 0;
  1086. return 1;
  1087. }
  1088. }
  1089. else
  1090. {
  1091. /* get sign bit */
  1092. if (mantissa < 0.0)
  1093. {
  1094. sign = 1;
  1095. mantissa = -mantissa;
  1096. }
  1097. else
  1098. {
  1099. sign = 0;
  1100. }
  1101. /* encode mantissa */
  1102. nmASN1octets = 0;
  1103. while (mantissa != 0.0 && nmASN1octets < sizeof(mASN1octets))
  1104. {
  1105. mantissa *= 256.0;
  1106. exponent -= 8;
  1107. mASN1octets[nmASN1octets++] = (int)mantissa;
  1108. mantissa -= (double)(int)mantissa;
  1109. }
  1110. /* encode exponent and create head octet of encoded value */
  1111. head = (ASN1octet_t) (0x80 | (sign << 6));
  1112. if (exponent <= 0x7f && exponent >= -0x80)
  1113. {
  1114. eASN1octets[0] = (ASN1octet_t)(exponent);
  1115. neASN1octets = 1;
  1116. }
  1117. else
  1118. if (exponent <= 0x7fff && exponent >= -0x8000)
  1119. {
  1120. eASN1octets[0] = (ASN1octet_t)(exponent >> 8);
  1121. eASN1octets[1] = (ASN1octet_t)(exponent);
  1122. neASN1octets = 2;
  1123. head |= 0x01;
  1124. }
  1125. else
  1126. if (exponent <= 0x7fffff && exponent >= -0x800000)
  1127. {
  1128. eASN1octets[0] = (ASN1octet_t)(exponent >> 16);
  1129. eASN1octets[1] = (ASN1octet_t)(exponent >> 8);
  1130. eASN1octets[2] = (ASN1octet_t)(exponent);
  1131. neASN1octets = 3;
  1132. head |= 0x02;
  1133. }
  1134. else
  1135. {
  1136. eASN1octets[0] = 4; /* XXX does not work if int32_t != int */
  1137. eASN1octets[1] = (ASN1octet_t)(exponent >> 24);
  1138. eASN1octets[2] = (ASN1octet_t)(exponent >> 16);
  1139. eASN1octets[3] = (ASN1octet_t)(exponent >> 8);
  1140. eASN1octets[4] = (ASN1octet_t)(exponent);
  1141. neASN1octets = 5;
  1142. head |= 0x03;
  1143. }
  1144. /* encode length into first octet */
  1145. len = 1 + neASN1octets + nmASN1octets;
  1146. if (ASN1PEREncFragmentedLength(&n, enc, len))
  1147. {
  1148. /* check for space for head octet, mantissa and exponent */
  1149. if (ASN1PEREncCheck(enc, len))
  1150. {
  1151. /* put head octet, mantissa and exponent */
  1152. *enc->pos++ = head;
  1153. CopyMemory(enc->pos, eASN1octets, neASN1octets);
  1154. enc->pos += neASN1octets;
  1155. CopyMemory(enc->pos, mASN1octets, nmASN1octets);
  1156. enc->pos += nmASN1octets;
  1157. return 1;
  1158. }
  1159. }
  1160. }
  1161. }
  1162. else
  1163. {
  1164. ASN1EncSetError(enc, ASN1_ERR_BADREAL);
  1165. }
  1166. /* finished */
  1167. return 0;
  1168. }
  1169. /* encode an external value */
  1170. #ifdef ENABLE_EXTERNAL
  1171. int ASN1PEREncExternal(ASN1encoding_t enc, ASN1external_t *val)
  1172. {
  1173. ASN1uint32_t t, l;
  1174. if (!val->data_value_descriptor)
  1175. val->o[0] &= ~0x80;
  1176. /* encode identification */
  1177. switch (val->identification.o)
  1178. {
  1179. case ASN1external_identification_syntax_o:
  1180. if (!ASN1PEREncBitVal(enc, 3, 4 | !!val->data_value_descriptor))
  1181. return 0;
  1182. if (!ASN1PEREncObjectIdentifier(enc, &val->identification.u.syntax))
  1183. return 0;
  1184. break;
  1185. case ASN1external_identification_presentation_context_id_o:
  1186. if (!ASN1PEREncBitVal(enc, 3, 2 | !!val->data_value_descriptor))
  1187. return 0;
  1188. ASN1PEREncAlignment(enc);
  1189. l = ASN1uint32_uoctets(val->identification.u.presentation_context_id);
  1190. if (!ASN1PEREncBitVal(enc, 8, l))
  1191. return 0;
  1192. if (!ASN1PEREncBitVal(enc, l * 8,
  1193. val->identification.u.presentation_context_id))
  1194. return 0;
  1195. break;
  1196. case ASN1external_identification_context_negotiation_o:
  1197. if (!ASN1PEREncBitVal(enc, 3, 6 | !!val->data_value_descriptor))
  1198. return 0;
  1199. if (!ASN1PEREncObjectIdentifier(enc, &val->identification.u.context_negotiation.transfer_syntax))
  1200. return 0;
  1201. ASN1PEREncAlignment(enc);
  1202. l = ASN1uint32_uoctets(
  1203. val->identification.u.context_negotiation.presentation_context_id);
  1204. if (!ASN1PEREncBitVal(enc, 8, l))
  1205. return 0;
  1206. if (!ASN1PEREncBitVal(enc, l * 8,
  1207. val->identification.u.context_negotiation.presentation_context_id))
  1208. return 0;
  1209. break;
  1210. default:
  1211. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  1212. return 0;
  1213. }
  1214. /* encode value descriptor */
  1215. if (val->o[0] & 0x80)
  1216. {
  1217. t = My_lstrlenA(val->data_value_descriptor);
  1218. if (!ASN1PEREncFragmentedCharString(enc, t,
  1219. val->data_value_descriptor, 8))
  1220. return 0;
  1221. }
  1222. /* encode value */
  1223. switch (val->data_value.o)
  1224. {
  1225. case ASN1external_data_value_notation_o:
  1226. if (!ASN1PEREncBitVal(enc, 2, 0))
  1227. return 0;
  1228. if (!ASN1PEREncFragmented(enc,
  1229. val->data_value.u.notation.length,
  1230. val->data_value.u.notation.encoded, 8))
  1231. return 0;
  1232. break;
  1233. case ASN1external_data_value_encoded_o:
  1234. if (!(val->data_value.u.encoded.length & 7))
  1235. {
  1236. if (!ASN1PEREncBitVal(enc, 2, 1))
  1237. return 0;
  1238. if (!ASN1PEREncFragmented(enc, val->data_value.u.encoded.length / 8,
  1239. val->data_value.u.encoded.value, 8))
  1240. return 0;
  1241. }
  1242. else
  1243. {
  1244. if (!ASN1PEREncBitVal(enc, 2, 2))
  1245. return 0;
  1246. if (!ASN1PEREncFragmented(enc, val->data_value.u.encoded.length,
  1247. val->data_value.u.encoded.value, 1))
  1248. return 0;
  1249. }
  1250. break;
  1251. default:
  1252. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  1253. return 0;
  1254. }
  1255. return 1;
  1256. }
  1257. #endif // ENABLE_EXTERNAL
  1258. /* encode an embedded pdv value */
  1259. #ifdef ENABLE_EMBEDDED_PDV
  1260. int ASN1PEREncEmbeddedPdv(ASN1encoding_t enc, ASN1embeddedpdv_t *val)
  1261. {
  1262. ASN1uint32_t l;
  1263. ASN1uint32_t index;
  1264. ASN1uint32_t flag;
  1265. /* search identification */
  1266. if (!ASN1EncSearchEmbeddedPdvIdentification(((ASN1INTERNencoding_t) enc)->parent,
  1267. &val->identification, &index, &flag))
  1268. return 0;
  1269. /* encode EP-A/EP-B flag */
  1270. if (!ASN1PEREncBitVal(enc, 1, flag))
  1271. return 0;
  1272. /* encode index of identification */
  1273. if (!ASN1PEREncNormallySmall(enc, index))
  1274. return 0;
  1275. if (flag)
  1276. {
  1277. /* EP-A encoding: */
  1278. /* encode identification */
  1279. if (!ASN1PEREncBitVal(enc, 3, val->identification.o))
  1280. return 0;
  1281. switch (val->identification.o)
  1282. {
  1283. case ASN1embeddedpdv_identification_syntaxes_o:
  1284. if (!ASN1PEREncObjectIdentifier(enc,
  1285. &val->identification.u.syntaxes.abstract))
  1286. return 0;
  1287. if (!ASN1PEREncObjectIdentifier(enc,
  1288. &val->identification.u.syntaxes.transfer))
  1289. return 0;
  1290. break;
  1291. case ASN1embeddedpdv_identification_syntax_o:
  1292. if (!ASN1PEREncObjectIdentifier(enc, &val->identification.u.syntax))
  1293. return 0;
  1294. break;
  1295. case ASN1embeddedpdv_identification_presentation_context_id_o:
  1296. ASN1PEREncAlignment(enc);
  1297. l = ASN1uint32_uoctets(
  1298. val->identification.u.presentation_context_id);
  1299. if (!ASN1PEREncBitVal(enc, 8, l))
  1300. return 0;
  1301. if (!ASN1PEREncBitVal(enc, l * 8,
  1302. val->identification.u.presentation_context_id))
  1303. return 0;
  1304. break;
  1305. case ASN1embeddedpdv_identification_context_negotiation_o:
  1306. ASN1PEREncAlignment(enc);
  1307. l = ASN1uint32_uoctets(val->
  1308. identification.u.context_negotiation.presentation_context_id);
  1309. if (!ASN1PEREncBitVal(enc, 8, l))
  1310. return 0;
  1311. if (!ASN1PEREncBitVal(enc, l * 8, val->
  1312. identification.u.context_negotiation.presentation_context_id))
  1313. return 0;
  1314. if (!ASN1PEREncObjectIdentifier(enc,
  1315. &val->identification.u.context_negotiation.transfer_syntax))
  1316. return 0;
  1317. break;
  1318. case ASN1embeddedpdv_identification_transfer_syntax_o:
  1319. if (!ASN1PEREncObjectIdentifier(enc,
  1320. &val->identification.u.transfer_syntax))
  1321. return 0;
  1322. break;
  1323. case ASN1embeddedpdv_identification_fixed_o:
  1324. break;
  1325. default:
  1326. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  1327. return 0;
  1328. }
  1329. }
  1330. /* encode value */
  1331. ASN1PEREncAlignment(enc);
  1332. switch (val->data_value.o)
  1333. {
  1334. case ASN1embeddedpdv_data_value_notation_o:
  1335. if (!ASN1PEREncFragmented(enc,
  1336. val->data_value.u.notation.length,
  1337. val->data_value.u.notation.encoded, 1))
  1338. return 0;
  1339. break;
  1340. case ASN1embeddedpdv_data_value_encoded_o:
  1341. if (!ASN1PEREncFragmented(enc,
  1342. val->data_value.u.encoded.length,
  1343. val->data_value.u.encoded.value, 1))
  1344. return 0;
  1345. break;
  1346. default:
  1347. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  1348. return 0;
  1349. }
  1350. return 1;
  1351. }
  1352. #endif // ENABLE_EMBEDDED_PDV
  1353. /* encode an optimized embedded pdv value */
  1354. #ifdef ENABLE_EMBEDDED_PDV
  1355. int ASN1PEREncEmbeddedPdvOpt(ASN1encoding_t enc, ASN1embeddedpdv_t *val)
  1356. {
  1357. /* encode data value */
  1358. switch (val->data_value.o)
  1359. {
  1360. case ASN1embeddedpdv_data_value_notation_o:
  1361. if (!ASN1PEREncFragmented(enc,
  1362. val->data_value.u.notation.length,
  1363. val->data_value.u.notation.encoded, 1))
  1364. return 0;
  1365. break;
  1366. case ASN1embeddedpdv_data_value_encoded_o:
  1367. if (!ASN1PEREncFragmented(enc,
  1368. val->data_value.u.encoded.length,
  1369. val->data_value.u.encoded.value, 1))
  1370. return 0;
  1371. break;
  1372. default:
  1373. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  1374. return 0;
  1375. }
  1376. return 1;
  1377. }
  1378. #endif // ENABLE_EMBEDDED_PDV
  1379. /* encode a character string */
  1380. #ifdef ENABLE_GENERALIZED_CHAR_STR
  1381. int ASN1PEREncCharacterString(ASN1encoding_t enc, ASN1characterstring_t *val)
  1382. {
  1383. ASN1uint32_t l;
  1384. ASN1uint32_t index;
  1385. ASN1uint32_t flag;
  1386. /* search identification */
  1387. if (!ASN1EncSearchCharacterStringIdentification(((ASN1INTERNencoding_t) enc)->parent,
  1388. &val->identification, &index, &flag))
  1389. return 0;
  1390. /* encode CS-A/CS-B flag */
  1391. if (!ASN1PEREncBitVal(enc, 1, flag))
  1392. return 0;
  1393. /* encode index of identification */
  1394. if (!ASN1PEREncNormallySmall(enc, index))
  1395. return 0;
  1396. if (flag)
  1397. {
  1398. /* CS-A encoding: */
  1399. /* encode identification */
  1400. if (!ASN1PEREncBitVal(enc, 3, val->identification.o))
  1401. return 0;
  1402. switch (val->identification.o) {
  1403. case ASN1characterstring_identification_syntaxes_o:
  1404. if (!ASN1PEREncObjectIdentifier(enc,
  1405. &val->identification.u.syntaxes.abstract))
  1406. return 0;
  1407. if (!ASN1PEREncObjectIdentifier(enc,
  1408. &val->identification.u.syntaxes.transfer))
  1409. return 0;
  1410. break;
  1411. case ASN1characterstring_identification_syntax_o:
  1412. if (!ASN1PEREncObjectIdentifier(enc, &val->identification.u.syntax))
  1413. return 0;
  1414. break;
  1415. case ASN1characterstring_identification_presentation_context_id_o:
  1416. ASN1PEREncAlignment(enc);
  1417. l = ASN1uint32_uoctets(
  1418. val->identification.u.presentation_context_id);
  1419. if (!ASN1PEREncBitVal(enc, 8, l))
  1420. return 0;
  1421. if (!ASN1PEREncBitVal(enc, l * 8,
  1422. val->identification.u.presentation_context_id))
  1423. return 0;
  1424. break;
  1425. case ASN1characterstring_identification_context_negotiation_o:
  1426. ASN1PEREncAlignment(enc);
  1427. l = ASN1uint32_uoctets(val->
  1428. identification.u.context_negotiation.presentation_context_id);
  1429. if (!ASN1PEREncBitVal(enc, 8, l))
  1430. return 0;
  1431. if (!ASN1PEREncBitVal(enc, l * 8, val->
  1432. identification.u.context_negotiation.presentation_context_id))
  1433. return 0;
  1434. if (!ASN1PEREncObjectIdentifier(enc,
  1435. &val->identification.u.context_negotiation.transfer_syntax))
  1436. return 0;
  1437. break;
  1438. case ASN1characterstring_identification_transfer_syntax_o:
  1439. if (!ASN1PEREncObjectIdentifier(enc,
  1440. &val->identification.u.transfer_syntax))
  1441. return 0;
  1442. break;
  1443. case ASN1characterstring_identification_fixed_o:
  1444. break;
  1445. default:
  1446. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  1447. return 0;
  1448. }
  1449. }
  1450. /* encode value */
  1451. ASN1PEREncAlignment(enc);
  1452. switch (val->data_value.o)
  1453. {
  1454. case ASN1characterstring_data_value_notation_o:
  1455. if (!ASN1PEREncFragmented(enc,
  1456. val->data_value.u.notation.length,
  1457. val->data_value.u.notation.encoded, 8))
  1458. return 0;
  1459. break;
  1460. case ASN1characterstring_data_value_encoded_o:
  1461. if (!ASN1PEREncFragmented(enc,
  1462. val->data_value.u.encoded.length,
  1463. val->data_value.u.encoded.value, 8))
  1464. return 0;
  1465. break;
  1466. default:
  1467. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  1468. return 0;
  1469. }
  1470. return 1;
  1471. }
  1472. #endif // ENABLE_GENERALIZED_CHAR_STR
  1473. /* encode an optimized character string value */
  1474. #ifdef ENABLE_GENERALIZED_CHAR_STR
  1475. int ASN1PEREncCharacterStringOpt(ASN1encoding_t enc, ASN1characterstring_t *val)
  1476. {
  1477. switch (val->data_value.o)
  1478. {
  1479. case ASN1characterstring_data_value_notation_o:
  1480. return ASN1PEREncFragmented(enc,
  1481. val->data_value.u.notation.length,
  1482. val->data_value.u.notation.encoded, 8);
  1483. break;
  1484. case ASN1characterstring_data_value_encoded_o:
  1485. return ASN1PEREncFragmented(enc,
  1486. val->data_value.u.encoded.length,
  1487. val->data_value.u.encoded.value, 8);
  1488. break;
  1489. }
  1490. ASN1EncSetError(enc, ASN1_ERR_INTERNAL);
  1491. return 0;
  1492. }
  1493. #endif // ENABLE_GENERALIZED_CHAR_STR
  1494. /* encode a multibyte string */
  1495. #ifdef ENABLE_ALL
  1496. int ASN1PEREncMultibyteString(ASN1encoding_t enc, ASN1char_t *val)
  1497. {
  1498. return ASN1PEREncFragmented(enc, My_lstrlenA(val), (ASN1octet_t *)val, 8);
  1499. }
  1500. #endif // ENABLE_ALL
  1501. /* encode a generalized time */
  1502. int ASN1PEREncGeneralizedTime(ASN1encoding_t enc, ASN1generalizedtime_t *val, ASN1uint32_t nbits)
  1503. {
  1504. char time[32];
  1505. if (ASN1generalizedtime2string(time, val))
  1506. {
  1507. return ASN1PEREncFragmentedCharString(enc, My_lstrlenA(time), time, nbits);
  1508. }
  1509. return 0;
  1510. }
  1511. /* encode a utc time */
  1512. #ifdef ENABLE_ALL
  1513. int ASN1PEREncUTCTime(ASN1encoding_t enc, ASN1utctime_t *val, ASN1uint32_t nbits)
  1514. {
  1515. char time[32];
  1516. if (ASN1utctime2string(time, val))
  1517. {
  1518. return ASN1PEREncFragmentedCharString(enc, My_lstrlenA(time), time, nbits);
  1519. }
  1520. return 0;
  1521. }
  1522. #endif // ENABLE_ALL
  1523. /* end of encoding */
  1524. int ASN1PEREncFlush(ASN1encoding_t enc)
  1525. {
  1526. /* complete broken octet */
  1527. ASN1PEREncAlignment(enc);
  1528. /* allocate at least one octet */
  1529. if (enc->buf)
  1530. {
  1531. /* fill in zero-octet if encoding is empty bitstring */
  1532. if (enc->buf == enc->pos)
  1533. *enc->pos++ = 0;
  1534. /* calculate length */
  1535. enc->len = (ASN1uint32_t) (enc->pos - enc->buf);
  1536. return 1;
  1537. }
  1538. return ASN1PEREncCheck(enc, 1);
  1539. }
  1540. /* encode an octet alignment */
  1541. void ASN1PEREncAlignment(ASN1encoding_t enc)
  1542. {
  1543. /* complete broken octet */
  1544. if (enc->bit)
  1545. {
  1546. enc->pos++;
  1547. enc->bit = 0;
  1548. }
  1549. }
  1550. /* compare two encodings */
  1551. #ifdef ENABLE_ALL
  1552. int ASN1PEREncCmpEncodings(const void *p1, const void *p2)
  1553. {
  1554. ASN1INTERNencoding_t e1 = (ASN1INTERNencoding_t)p1;
  1555. ASN1INTERNencoding_t e2 = (ASN1INTERNencoding_t)p2;
  1556. ASN1uint32_t l1, l2;
  1557. int r;
  1558. l1 = (ASN1uint32_t) (e1->info.pos - e1->info.buf) + ((e1->info.bit > 0) ? 1 : 0);
  1559. l2 = (ASN1uint32_t) (e2->info.pos - e2->info.buf) + ((e2->info.bit > 0) ? 1 : 0);
  1560. r = memcmp(e1->info.buf, e2->info.buf, l1 < l2 ? l1 : l2);
  1561. if (!r)
  1562. r = l1 - l2;
  1563. return r;
  1564. }
  1565. #endif // ENABLE_ALL
  1566. /* check a bit field for present optionals */
  1567. int ASN1PEREncCheckExtensions(ASN1uint32_t nbits, ASN1octet_t *val)
  1568. {
  1569. while (nbits >= 8)
  1570. {
  1571. if (*val)
  1572. return 1;
  1573. val++;
  1574. nbits -= 8;
  1575. }
  1576. if (nbits)
  1577. {
  1578. return ((*val & c_aBitMask2[nbits]) ? 1 : 0);
  1579. }
  1580. return 0;
  1581. }
  1582. /* encode an open type value */
  1583. #ifdef ENABLE_ALL
  1584. int ASN1PEREncOpenType(ASN1encoding_t enc, ASN1open_t *val)
  1585. {
  1586. return ASN1PEREncFragmented(enc, val->length, (ASN1octet_t *)val->encoded, 8);
  1587. }
  1588. #endif // ENABLE_ALL