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.

605 lines
16 KiB

  1. /* Copyright (C) Boris Nikolaus, Germany, 1996-1997. All rights reserved. */
  2. /* Copyright (C) Microsoft Corporation, 1997-1998. All rights reserved. */
  3. //--------------------------------------------------------------------------
  4. //
  5. // Module Name: ms_per.c
  6. //
  7. // Brief Description:
  8. // This module contains the routines for the Microsoft
  9. // ASN.1 encoder and decoder.
  10. //
  11. // History:
  12. // 10/15/97 Lon-Chan Chu (lonchanc)
  13. // Created.
  14. //
  15. // Copyright (c) 1997 Microsoft Corporation
  16. //
  17. //--------------------------------------------------------------------------
  18. #include "precomp.h"
  19. #define MLZ_FILE_ZONE ZONE_MSPER
  20. int ASN1PEREncInteger(ASN1encoding_t enc, ASN1int32_t val)
  21. {
  22. ASN1uint32_t l = ASN1int32_octets(val);
  23. ASN1PEREncAlignment(enc);
  24. if (ASN1PEREncBitVal(enc, 8, l))
  25. {
  26. return ASN1PEREncBitVal(enc, l * 8, val);
  27. }
  28. return 0;
  29. }
  30. int ASN1PERDecInteger(ASN1decoding_t dec, ASN1int32_t *val)
  31. {
  32. ASN1uint32_t l;
  33. ASN1PERDecAlignment(dec);
  34. if (ASN1PERDecFragmentedLength(dec, &l))
  35. {
  36. return ASN1PERDecS32Val(dec, l * 8, val);
  37. }
  38. return 0;
  39. }
  40. int ASN1PEREncUnsignedInteger(ASN1encoding_t enc, ASN1uint32_t val)
  41. {
  42. ASN1uint32_t l = ASN1uint32_uoctets(val);
  43. ASN1PEREncAlignment(enc);
  44. if (ASN1PEREncBitVal(enc, 8, l))
  45. {
  46. return ASN1PEREncBitVal(enc, l * 8, val);
  47. }
  48. return 0;
  49. }
  50. int ASN1PERDecUnsignedInteger(ASN1decoding_t dec, ASN1uint32_t *val)
  51. {
  52. ASN1uint32_t l;
  53. ASN1PERDecAlignment(dec);
  54. if (ASN1PERDecFragmentedLength(dec, &l))
  55. {
  56. return ASN1PERDecU32Val(dec, l * 8, val);
  57. }
  58. return 0;
  59. }
  60. int ASN1PEREncUnsignedShort(ASN1encoding_t enc, ASN1uint32_t val)
  61. {
  62. ASN1PEREncAlignment(enc);
  63. return ASN1PEREncBitVal(enc, 16, val);
  64. }
  65. int ASN1PERDecUnsignedShort(ASN1decoding_t dec, ASN1uint16_t *val)
  66. {
  67. ASN1PERDecAlignment(dec);
  68. return ASN1PERDecU16Val(dec, 16, val);
  69. }
  70. int ASN1PEREncBoolean(ASN1encoding_t enc, ASN1bool_t val)
  71. {
  72. return ASN1PEREncBitVal(enc, 1, val ? 1 : 0);
  73. }
  74. int ASN1PERDecBoolean(ASN1decoding_t dec, ASN1bool_t *val)
  75. {
  76. DecAssert(dec, sizeof(ASN1bool_t) == sizeof(ASN1uint8_t));
  77. *val = 0; // in case we change the boolean type
  78. return ASN1PERDecU8Val(dec, 1, val);
  79. }
  80. __inline int _EncExtensionBitClear(ASN1encoding_t enc)
  81. {
  82. return ASN1PEREncBitVal(enc, 1, 0);
  83. }
  84. int ASN1PEREncExtensionBitClear(ASN1encoding_t enc)
  85. {
  86. return _EncExtensionBitClear(enc);
  87. }
  88. __inline int _EncExtensionBitSet(ASN1encoding_t enc)
  89. {
  90. return ASN1PEREncBitVal(enc, 1, 1);
  91. }
  92. int ASN1PEREncExtensionBitSet(ASN1encoding_t enc)
  93. {
  94. return _EncExtensionBitSet(enc);
  95. }
  96. int ASN1PERDecSkipNormallySmallExtensionFragmented(ASN1decoding_t dec)
  97. {
  98. ASN1uint32_t e, i;
  99. if (ASN1PERDecSkipNormallySmallExtension(dec, &e))
  100. {
  101. for (i = 0; i < e; i++)
  102. {
  103. if (ASN1PERDecSkipFragmented(dec, 8))
  104. {
  105. continue;
  106. }
  107. return 0;
  108. }
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. int ASN1PEREncSimpleChoice(ASN1encoding_t enc, ASN1choice_t ChoiceVal, ASN1int32_t cChoiceBits)
  114. {
  115. if (ChoiceVal >= ASN1_CHOICE_BASE)
  116. {
  117. ChoiceVal -= ASN1_CHOICE_BASE;
  118. return (cChoiceBits ? ASN1PEREncBitVal(enc, cChoiceBits, ChoiceVal) : 1);
  119. }
  120. EncAssert(enc, FALSE);
  121. return 0;
  122. }
  123. int ASN1PERDecSimpleChoice(ASN1decoding_t dec, ASN1choice_t *pChoiceVal, ASN1int32_t cChoiceBits)
  124. {
  125. DecAssert(dec, cChoiceBits <= sizeof(ASN1choice_t) * 8);
  126. *pChoiceVal = ASN1_CHOICE_BASE; // default choice
  127. if (cChoiceBits)
  128. {
  129. if (ASN1PERDecU16Val(dec, cChoiceBits, pChoiceVal))
  130. {
  131. *pChoiceVal += ASN1_CHOICE_BASE;
  132. }
  133. else
  134. {
  135. return 0;
  136. }
  137. }
  138. return 1;
  139. }
  140. int ASN1PEREncSimpleChoiceEx(ASN1encoding_t enc, ASN1choice_t ChoiceVal, ASN1int32_t cChoiceBits)
  141. {
  142. if (ChoiceVal >= ASN1_CHOICE_BASE)
  143. {
  144. ChoiceVal -= ASN1_CHOICE_BASE;
  145. if (_EncExtensionBitClear(enc))
  146. {
  147. return (cChoiceBits ? ASN1PEREncBitVal(enc, cChoiceBits, ChoiceVal) : 1);
  148. }
  149. }
  150. else
  151. {
  152. EncAssert(enc, 0);
  153. }
  154. return 0;
  155. }
  156. int ASN1PERDecSimpleChoiceEx(ASN1decoding_t dec, ASN1choice_t *pChoiceVal, ASN1int32_t cChoiceBits)
  157. {
  158. ASN1uint32_t x;
  159. DecAssert(dec, cChoiceBits <= sizeof(ASN1choice_t) * 8);
  160. if (ASN1PERDecExtensionBit(dec, &x))
  161. {
  162. if (!x)
  163. {
  164. *pChoiceVal = ASN1_CHOICE_BASE; // default choice
  165. if (cChoiceBits)
  166. {
  167. if (ASN1PERDecU16Val(dec, cChoiceBits, pChoiceVal))
  168. {
  169. *pChoiceVal += ASN1_CHOICE_BASE;
  170. return 1;
  171. }
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. *pChoiceVal = ASN1_CHOICE_EXTENSION; // extension choice
  177. return ASN1PERDecSkipNormallySmall(dec);
  178. }
  179. return 0;
  180. }
  181. int ASN1PEREncComplexChoice(ASN1encoding_t enc, ASN1choice_t ChoiceVal, ASN1int32_t cChoiceBits, ASN1choice_t ExtensionChoice)
  182. {
  183. if (ChoiceVal >= ASN1_CHOICE_BASE)
  184. {
  185. ChoiceVal -= ASN1_CHOICE_BASE;
  186. if (ChoiceVal < ExtensionChoice) // lonchanc: no equal sign
  187. {
  188. if (_EncExtensionBitClear(enc))
  189. {
  190. if (cChoiceBits)
  191. {
  192. return ASN1PEREncBitVal(enc, cChoiceBits, ChoiceVal);
  193. }
  194. return 1;
  195. }
  196. }
  197. else
  198. {
  199. if (_EncExtensionBitSet(enc))
  200. {
  201. return ASN1PEREncNormallySmall(enc, ChoiceVal - ExtensionChoice);
  202. }
  203. }
  204. }
  205. else
  206. {
  207. EncAssert(enc, 0);
  208. }
  209. return 0;
  210. }
  211. int ASN1PERDecComplexChoice(ASN1decoding_t dec, ASN1choice_t *pChoiceVal, ASN1int32_t cChoiceBits, ASN1choice_t ExtensionChoice)
  212. {
  213. ASN1uint32_t x;
  214. DecAssert(dec, cChoiceBits <= sizeof(ASN1choice_t) * 8);
  215. if (ASN1PERDecExtensionBit(dec, &x))
  216. {
  217. if (!x)
  218. {
  219. *pChoiceVal = ASN1_CHOICE_BASE; // default choice
  220. if (cChoiceBits)
  221. {
  222. if (ASN1PERDecU16Val(dec, cChoiceBits, pChoiceVal))
  223. {
  224. *pChoiceVal += ASN1_CHOICE_BASE;
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. return 1;
  230. }
  231. if (ASN1PERDecN16Val(dec, pChoiceVal))
  232. {
  233. *pChoiceVal += ExtensionChoice + ASN1_CHOICE_BASE;
  234. return 1;
  235. }
  236. }
  237. return 0;
  238. }
  239. int ASN1PEREncOctetString_NoSize(ASN1encoding_t enc, ASN1octetstring_t *pOctetStr)
  240. {
  241. return ASN1PEREncFragmented(enc, pOctetStr->length, pOctetStr->value, 8);
  242. }
  243. int ASN1PERDecOctetString_NoSize(ASN1decoding_t dec, ASN1octetstring_t *pOctetStr)
  244. {
  245. return ASN1PERDecFragmented(dec, &(pOctetStr->length), &(pOctetStr->value), 8);
  246. }
  247. int _PEREncOctetString2
  248. (
  249. ASN1encoding_t enc,
  250. ASN1uint32_t length,
  251. ASN1octet_t *value,
  252. ASN1uint32_t nSizeLowerBound,
  253. ASN1uint32_t nSizeUpperBound,
  254. ASN1uint32_t cSizeBits
  255. )
  256. {
  257. // fixed size array?
  258. if (nSizeLowerBound == nSizeUpperBound)
  259. {
  260. ASN1uint32_t nSizeLimit = nSizeLowerBound;
  261. EncAssert(enc, cSizeBits == 0);
  262. EncAssert(enc, nSizeLimit < 64 * 1024);
  263. if (length == nSizeLimit)
  264. {
  265. if (nSizeLimit > 2)
  266. {
  267. ASN1PEREncAlignment(enc);
  268. }
  269. return ASN1PEREncBits(enc, nSizeLimit * 8, value);
  270. }
  271. EncAssert(enc, 0);
  272. return 0;
  273. }
  274. // ranged size array
  275. EncAssert(enc, cSizeBits);
  276. EncAssert(enc, nSizeLowerBound < nSizeUpperBound);
  277. if (nSizeLowerBound <= length && length <= nSizeUpperBound)
  278. {
  279. if (nSizeUpperBound - nSizeLowerBound < 255) // lonchanc: inherited from TELES
  280. {
  281. if (ASN1PEREncBitVal(enc, cSizeBits, length - nSizeLowerBound))
  282. {
  283. ASN1PEREncAlignment(enc);
  284. }
  285. else
  286. {
  287. return 0;
  288. }
  289. }
  290. else
  291. {
  292. EncAssert(enc, cSizeBits % 8 == 0);
  293. ASN1PEREncAlignment(enc);
  294. if (!ASN1PEREncBitVal(enc, cSizeBits, length - nSizeLowerBound))
  295. return 0;
  296. }
  297. return ASN1PEREncBits(enc, length * 8, value);
  298. }
  299. EncAssert(enc, 0);
  300. return 0;
  301. }
  302. int ASN1PEREncOctetString_FixedSize(ASN1encoding_t enc, ASN1octetstring2_t *pOctetStr, ASN1uint32_t nSizeLimit)
  303. {
  304. return _PEREncOctetString2(enc, pOctetStr->length, &(pOctetStr->value[0]), nSizeLimit, nSizeLimit, 0);
  305. }
  306. int ASN1PEREncOctetString_FixedSizeEx(ASN1encoding_t enc, ASN1octetstring_t *pOctetStr, ASN1uint32_t nSizeLimit)
  307. {
  308. return _PEREncOctetString2(enc, pOctetStr->length, pOctetStr->value, nSizeLimit, nSizeLimit, 0);
  309. }
  310. int ASN1PEREncOctetString_VarSize(ASN1encoding_t enc, ASN1octetstring2_t *pOctetStr, ASN1uint32_t nSizeLowerBound, ASN1uint32_t nSizeUpperBound, ASN1uint32_t cSizeBits)
  311. {
  312. return _PEREncOctetString2(enc, pOctetStr->length, &(pOctetStr->value[0]), nSizeLowerBound, nSizeUpperBound, cSizeBits);
  313. }
  314. int ASN1PEREncOctetString_VarSizeEx(ASN1encoding_t enc, ASN1octetstring_t *pOctetStr, ASN1uint32_t nSizeLowerBound, ASN1uint32_t nSizeUpperBound, ASN1uint32_t cSizeBits)
  315. {
  316. return _PEREncOctetString2(enc, pOctetStr->length, pOctetStr->value, nSizeLowerBound, nSizeUpperBound, cSizeBits);
  317. }
  318. int _PERDecOctetString2
  319. (
  320. ASN1decoding_t dec,
  321. ASN1uint32_t *length,
  322. ASN1octet_t **value,
  323. ASN1uint32_t nSizeLowerBound,
  324. ASN1uint32_t nSizeUpperBound,
  325. ASN1uint32_t cSizeBits
  326. )
  327. {
  328. // fixed size array?
  329. if (nSizeLowerBound == nSizeUpperBound)
  330. {
  331. ASN1uint32_t nSizeLimit = nSizeLowerBound;
  332. DecAssert(dec, cSizeBits == 0);
  333. DecAssert(dec, nSizeLimit < 64 * 1024);
  334. *length = nSizeLimit;
  335. if (nSizeLimit > 2)
  336. {
  337. ASN1PERDecAlignment(dec);
  338. }
  339. if (NULL == *value)
  340. {
  341. // must be unbounded
  342. *value = (ASN1octet_t *) DecMemAlloc(dec, nSizeLimit + 1);
  343. if (NULL == *value)
  344. {
  345. return 0;
  346. }
  347. }
  348. return ASN1PERDecExtension(dec, nSizeLimit * 8, *value);
  349. }
  350. // ranged size array
  351. DecAssert(dec, cSizeBits);
  352. DecAssert(dec, nSizeLowerBound < nSizeUpperBound);
  353. if (nSizeUpperBound - nSizeLowerBound < 255) // lonchanc: inherited from TELES
  354. {
  355. if (ASN1PERDecU32Val(dec, cSizeBits, length))
  356. {
  357. *length += nSizeLowerBound;
  358. ASN1PERDecAlignment(dec);
  359. }
  360. else
  361. {
  362. return 0;
  363. }
  364. }
  365. else
  366. {
  367. DecAssert(dec, cSizeBits % 8 == 0);
  368. ASN1PERDecAlignment(dec);
  369. if (ASN1PERDecU32Val(dec, cSizeBits, length))
  370. {
  371. *length += nSizeLowerBound;
  372. }
  373. else
  374. {
  375. return 0;
  376. }
  377. }
  378. if (*length <= nSizeUpperBound)
  379. {
  380. if (NULL == *value)
  381. {
  382. *value = (ASN1octet_t *) DecMemAlloc(dec, *length + 1);
  383. if (NULL == *value)
  384. {
  385. return 0;
  386. }
  387. }
  388. return ASN1PERDecExtension(dec, *length * 8, *value);
  389. }
  390. DecAssert(dec, 0);
  391. return 0;
  392. }
  393. int ASN1PERDecOctetString_FixedSize(ASN1decoding_t dec, ASN1octetstring2_t *pOctetStr, ASN1uint32_t nSizeLimit)
  394. {
  395. ASN1octet_t *pData = &(pOctetStr->value[0]);
  396. return _PERDecOctetString2(dec, &(pOctetStr->length), &pData, nSizeLimit, nSizeLimit, 0);
  397. }
  398. int ASN1PERDecOctetString_FixedSizeEx(ASN1decoding_t dec, ASN1octetstring_t *pOctetStr, ASN1uint32_t nSizeLimit)
  399. {
  400. pOctetStr->value = NULL;
  401. return _PERDecOctetString2(dec, &(pOctetStr->length), &(pOctetStr->value), nSizeLimit, nSizeLimit, 0);
  402. }
  403. int ASN1PERDecOctetString_VarSize(ASN1decoding_t dec, ASN1octetstring2_t *pOctetStr, ASN1uint32_t nSizeLowerBound, ASN1uint32_t nSizeUpperBound, ASN1uint32_t cSizeBits)
  404. {
  405. ASN1octet_t *pData = &(pOctetStr->value[0]);
  406. return _PERDecOctetString2(dec, &(pOctetStr->length), &pData, nSizeLowerBound, nSizeUpperBound, cSizeBits);
  407. }
  408. int ASN1PERDecOctetString_VarSizeEx(ASN1decoding_t dec, ASN1octetstring_t *pOctetStr, ASN1uint32_t nSizeLowerBound, ASN1uint32_t nSizeUpperBound, ASN1uint32_t cSizeBits)
  409. {
  410. pOctetStr->value = NULL;
  411. return _PERDecOctetString2(dec, &(pOctetStr->length), &(pOctetStr->value), nSizeLowerBound, nSizeUpperBound, cSizeBits);
  412. }
  413. int ASN1PEREncSeqOf_NoSize(ASN1encoding_t enc, ASN1iterator_t **val, ASN1iterator_encfn pfnIterator)
  414. {
  415. ASN1uint32_t t;
  416. ASN1iterator_t *f;
  417. ASN1uint32_t i;
  418. ASN1uint32_t j, n = 0x4000;
  419. EncAssert(enc, NULL != pfnIterator);
  420. for (t = 0, f = *val; f; f = f->next)
  421. t++;
  422. f = *val;
  423. for (i = 0; i < t;)
  424. {
  425. if (ASN1PEREncFragmentedLength(&n, enc, t - i))
  426. {
  427. for (j = 0; j < n; i++, j++)
  428. {
  429. if (((*pfnIterator)(enc, f)))
  430. {
  431. f = f->next;
  432. continue;
  433. }
  434. return 0;
  435. }
  436. }
  437. else
  438. {
  439. return 0;
  440. }
  441. }
  442. return ((n < 0x4000) ? 1 : ASN1PEREncFragmentedLength(&n, enc, 0));
  443. }
  444. int ASN1PERDecSeqOf_NoSize(ASN1decoding_t dec, ASN1iterator_t **val, ASN1iterator_decfn pfnIterator, ASN1uint32_t cbElementSize)
  445. {
  446. ASN1iterator_t **f;
  447. ASN1uint32_t l;
  448. ASN1uint32_t i;
  449. ASN1uint32_t n;
  450. DecAssert(dec, NULL != pfnIterator);
  451. f = val;
  452. do {
  453. if (ASN1PERDecFragmentedLength(dec, &n))
  454. {
  455. for (i = 0; i < n; i++)
  456. {
  457. if (NULL != (*f = (ASN1iterator_t *)DecMemAlloc(dec, cbElementSize)))
  458. {
  459. if ((*pfnIterator)(dec, *f))
  460. {
  461. f = &(*f)->next;
  462. continue;
  463. }
  464. }
  465. return 0;
  466. }
  467. }
  468. else
  469. {
  470. return 0;
  471. }
  472. } while (n >= 0x4000);
  473. *f = NULL;
  474. return 1;
  475. }
  476. int ASN1PEREncSeqOf_VarSize(ASN1encoding_t enc, ASN1iterator_t **val, ASN1iterator_encfn pfnIterator,
  477. ASN1uint32_t nSizeLowerBound, ASN1uint32_t nSizeUpperBound, ASN1uint32_t cSizeBits)
  478. {
  479. ASN1uint32_t t;
  480. ASN1iterator_t *f;
  481. for (t = 0, f = *val; f; f = f->next)
  482. t++;
  483. if (nSizeLowerBound <= t && t <= nSizeUpperBound)
  484. {
  485. if (nSizeUpperBound - nSizeLowerBound + 1 >= 256)
  486. {
  487. ASN1PEREncAlignment(enc);
  488. }
  489. if (ASN1PEREncBitVal(enc, cSizeBits, t - nSizeLowerBound))
  490. {
  491. for (f = *val; f; f = f->next)
  492. {
  493. if (((*pfnIterator)(enc, f)))
  494. {
  495. continue;
  496. }
  497. return 0;
  498. }
  499. return 1;
  500. }
  501. }
  502. else
  503. {
  504. EncAssert(enc, 0);
  505. }
  506. return 0;
  507. }
  508. int ASN1PERDecSeqOf_VarSize(ASN1decoding_t dec, ASN1iterator_t **val, ASN1iterator_decfn pfnIterator, ASN1uint32_t cbElementSize,
  509. ASN1uint32_t nSizeLowerBound, ASN1uint32_t nSizeUpperBound, ASN1uint32_t cSizeBits)
  510. {
  511. ASN1iterator_t **f;
  512. ASN1uint32_t l, i;
  513. if (nSizeUpperBound - nSizeLowerBound + 1 >= 256)
  514. {
  515. ASN1PERDecAlignment(dec);
  516. }
  517. if (ASN1PERDecU32Val(dec, cSizeBits, &l))
  518. {
  519. l += nSizeLowerBound;
  520. DecAssert(dec, l <= nSizeUpperBound);
  521. f = val;
  522. for (i = 0; i < l; i++)
  523. {
  524. if (NULL != (*f = (ASN1iterator_t *)DecMemAlloc(dec, cbElementSize)))
  525. {
  526. if ((*pfnIterator)(dec, *f))
  527. {
  528. f = &(*f)->next;
  529. continue;
  530. }
  531. }
  532. return 0;
  533. }
  534. *f = NULL;
  535. return 1;
  536. }
  537. return 0;
  538. }
  539. void ASN1PERFreeSeqOf(ASN1iterator_t **val, ASN1iterator_freefn pfnIterator)
  540. {
  541. if (val)
  542. {
  543. ASN1iterator_t *f, *ff;
  544. for (f = *val; f; f = ff)
  545. {
  546. ff = f->next;
  547. if (pfnIterator)
  548. {
  549. (*pfnIterator)(f);
  550. }
  551. MemFree(f);
  552. }
  553. }
  554. }