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.

1239 lines
36 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1996 - 1998
  5. //
  6. // File: pkiasn1.h
  7. //
  8. // Contents: PKI ASN.1 support functions.
  9. //
  10. // APIs: PkiAsn1ErrToHr
  11. // PkiAsn1Encode
  12. // PkiAsn1FreeEncoded
  13. // PkiAsn1Encode2
  14. // PkiAsn1Decode
  15. // PkiAsn1Decode2
  16. // PkiAsn1FreeDecoded
  17. // PkiAsn1SetEncodingRule
  18. // PkiAsn1GetEncodingRule
  19. // PkiAsn1EncodedOidToDotVal
  20. // PkiAsn1FreeDotVal
  21. // PkiAsn1DotValToEncodedOid
  22. // PkiAsn1FreeEncodedOid
  23. //
  24. // PkiAsn1Alloc
  25. // PkiAsn1Free
  26. // PkiAsn1ReverseBytes
  27. // PkiAsn1AllocAndReverseBytes
  28. // PkiAsn1GetOctetString
  29. // PkiAsn1SetHugeInteger
  30. // PkiAsn1FreeHugeInteger
  31. // PkiAsn1GetHugeInteger
  32. // PkiAsn1SetHugeUINT
  33. // PkiAsn1FreeHugeUINT
  34. // PkiAsn1GetHugeUINT
  35. // PkiAsn1SetBitString
  36. // PkiAsn1GetBitString
  37. // PkiAsn1GetIA5String
  38. // PkiAsn1SetUnicodeConvertedToIA5String
  39. // PkiAsn1FreeUnicodeConvertedToIA5String
  40. // PkiAsn1GetIA5StringConvertedToUnicode
  41. // PkiAsn1GetBMPString
  42. // PkiAsn1SetAny
  43. // PkiAsn1GetAny
  44. // PkiAsn1EncodeInfo
  45. // PkiAsn1DecodeAndAllocInfo
  46. // PkiAsn1FreeInfo
  47. // PkiAsn1EncodeInfoEx
  48. // PkiAsn1DecodeAndAllocInfo
  49. // PkiAsn1AllocStructInfoEx
  50. // PkiAsn1DecodeAndAllocInfoEx
  51. //
  52. // PkiAsn1ToObjectIdentifier
  53. // PkiAsn1FromObjectIdentifier
  54. // PkiAsn1ToUTCTime
  55. // PkiAsn1FromUTCTime
  56. // PkiAsn1ToGeneralizedTime
  57. // PkiAsn1FromGeneralizedTime
  58. // PkiAsn1ToChoiceOfTime
  59. // PkiAsn1FromChoiceOfTime
  60. //
  61. // Notes: According to the <draft-ietf-pkix-ipki-part1-03.txt> :
  62. // For UTCTime. Where YY is greater than 50, the year shall
  63. // be interpreted as 19YY. Where YY is less than or equal to
  64. // 50, the year shall be interpreted as 20YY.
  65. //
  66. // History: 23-Oct-98 philh created
  67. //--------------------------------------------------------------------------
  68. #ifndef __PKIASN1_H__
  69. #define __PKIASN1_H__
  70. #include <msber.h>
  71. #include <msasn1.h>
  72. #include <winerror.h>
  73. #include <pkialloc.h>
  74. #ifdef OSS_CRYPT_ASN1
  75. #include "asn1hdr.h"
  76. #include "asn1code.h"
  77. #include "ossglobl.h"
  78. #include "pkioss.h"
  79. #include "ossutil.h"
  80. #include "ossconv.h"
  81. #endif // OSS_CRYPT_ASN1
  82. #ifndef OSS_CRYPT_ASN1
  83. //+-------------------------------------------------------------------------
  84. // Convert Asn1 error to a HRESULT.
  85. //--------------------------------------------------------------------------
  86. __inline
  87. HRESULT
  88. WINAPI
  89. PkiAsn1ErrToHr(ASN1error_e Asn1Err) {
  90. if (0 > Asn1Err)
  91. return CRYPT_E_OSS_ERROR + 0x100 + (-Asn1Err -1000);
  92. else
  93. return CRYPT_E_OSS_ERROR + 0x200 + (Asn1Err -1000);
  94. }
  95. //+-------------------------------------------------------------------------
  96. // Asn1 Encode function. The encoded output is allocated and must be freed
  97. // by calling PkiAsn1FreeEncoded().
  98. //--------------------------------------------------------------------------
  99. ASN1error_e
  100. WINAPI
  101. PkiAsn1Encode(
  102. IN ASN1encoding_t pEnc,
  103. IN void *pvAsn1Info,
  104. IN ASN1uint32_t id,
  105. OUT BYTE **ppbEncoded,
  106. OUT DWORD *pcbEncoded
  107. );
  108. //+-------------------------------------------------------------------------
  109. // Free encoded output returned by PkiAsn1Encode().
  110. //--------------------------------------------------------------------------
  111. __inline
  112. void
  113. WINAPI
  114. PkiAsn1FreeEncoded(
  115. IN ASN1encoding_t pEnc,
  116. IN void *pvEncoded
  117. )
  118. {
  119. if (pvEncoded)
  120. ASN1_FreeEncoded(pEnc, pvEncoded);
  121. }
  122. //+-------------------------------------------------------------------------
  123. // Asn1 Encode function. The encoded output isn't allocated.
  124. //
  125. // If pbEncoded is NULL, does a length only calculation.
  126. //--------------------------------------------------------------------------
  127. ASN1error_e
  128. WINAPI
  129. PkiAsn1Encode2(
  130. IN ASN1encoding_t pEnc,
  131. IN void *pvAsn1Info,
  132. IN ASN1uint32_t id,
  133. OUT OPTIONAL BYTE *pbEncoded,
  134. IN OUT DWORD *pcbEncoded
  135. );
  136. //+-------------------------------------------------------------------------
  137. // Asn1 Decode function. The allocated, decoded structure, **pvAsn1Info, must
  138. // be freed by calling PkiAsn1FreeDecoded().
  139. //--------------------------------------------------------------------------
  140. ASN1error_e
  141. WINAPI
  142. PkiAsn1Decode(
  143. IN ASN1decoding_t pDec,
  144. OUT void **ppvAsn1Info,
  145. IN ASN1uint32_t id,
  146. IN const BYTE *pbEncoded,
  147. IN DWORD cbEncoded
  148. );
  149. //+-------------------------------------------------------------------------
  150. // Asn1 Decode function. The allocated, decoded structure, **pvAsn1Info, must
  151. // be freed by calling PkiAsn1FreeDecoded().
  152. //
  153. // For a successful decode, *ppbEncoded is advanced
  154. // past the decoded bytes and *pcbDecoded is decremented by the number
  155. // of decoded bytes.
  156. //--------------------------------------------------------------------------
  157. ASN1error_e
  158. WINAPI
  159. PkiAsn1Decode2(
  160. IN ASN1decoding_t pDec,
  161. OUT void **ppvAsn1Info,
  162. IN ASN1uint32_t id,
  163. IN OUT BYTE **ppbEncoded,
  164. IN OUT DWORD *pcbEncoded
  165. );
  166. //+-------------------------------------------------------------------------
  167. // Free decoded structure returned by PkiAsn1Decode() or PkiAsn1Decode2().
  168. //--------------------------------------------------------------------------
  169. __inline
  170. void
  171. WINAPI
  172. PkiAsn1FreeDecoded(
  173. IN ASN1decoding_t pDec,
  174. IN void *pvAsn1Info,
  175. IN ASN1uint32_t id
  176. )
  177. {
  178. if (pvAsn1Info)
  179. ASN1_FreeDecoded(pDec, pvAsn1Info, id);
  180. }
  181. //+-------------------------------------------------------------------------
  182. // Asn1 Set/Get encoding rule functions
  183. //--------------------------------------------------------------------------
  184. ASN1error_e
  185. WINAPI
  186. PkiAsn1SetEncodingRule(
  187. IN ASN1encoding_t pEnc,
  188. IN ASN1encodingrule_e eRule
  189. );
  190. ASN1encodingrule_e
  191. WINAPI
  192. PkiAsn1GetEncodingRule(
  193. IN ASN1encoding_t pEnc
  194. );
  195. //+-------------------------------------------------------------------------
  196. // Asn1 EncodedOid To/From DotVal functions
  197. //--------------------------------------------------------------------------
  198. __inline
  199. LPSTR
  200. WINAPI
  201. PkiAsn1EncodedOidToDotVal(
  202. IN ASN1decoding_t pDec,
  203. IN ASN1encodedOID_t *pEncodedOid
  204. )
  205. {
  206. LPSTR pszDotVal = NULL;
  207. if (ASN1BEREoid2DotVal(pDec, pEncodedOid, &pszDotVal))
  208. return pszDotVal;
  209. else
  210. return NULL;
  211. }
  212. __inline
  213. void
  214. WINAPI
  215. PkiAsn1FreeDotVal(
  216. IN ASN1decoding_t pDec,
  217. IN LPSTR pszDotVal
  218. )
  219. {
  220. if (pszDotVal)
  221. ASN1Free(pszDotVal);
  222. }
  223. // Returns nonzero for success
  224. __inline
  225. int
  226. WINAPI
  227. PkiAsn1DotValToEncodedOid(
  228. IN ASN1encoding_t pEnc,
  229. IN LPSTR pszDotVal,
  230. OUT ASN1encodedOID_t *pEncodedOid
  231. )
  232. {
  233. return ASN1BERDotVal2Eoid(pEnc, pszDotVal, pEncodedOid);
  234. }
  235. __inline
  236. void
  237. WINAPI
  238. PkiAsn1FreeEncodedOid(
  239. IN ASN1encoding_t pEnc,
  240. IN ASN1encodedOID_t *pEncodedOid
  241. )
  242. {
  243. if (pEncodedOid->value)
  244. ASN1_FreeEncoded(pEnc, pEncodedOid->value);
  245. }
  246. //+-------------------------------------------------------------------------
  247. // PkiAsn1 allocation and free functions
  248. //--------------------------------------------------------------------------
  249. #define PkiAsn1Alloc PkiNonzeroAlloc
  250. #define PkiAsn1Free PkiFree
  251. //+-------------------------------------------------------------------------
  252. // Reverses a buffer of bytes in place
  253. //--------------------------------------------------------------------------
  254. void
  255. WINAPI
  256. PkiAsn1ReverseBytes(
  257. IN OUT PBYTE pbIn,
  258. IN DWORD cbIn
  259. );
  260. //+-------------------------------------------------------------------------
  261. // Reverses a buffer of bytes to a new buffer. PkiAsn1Free() must be
  262. // called to free allocated bytes.
  263. //--------------------------------------------------------------------------
  264. PBYTE
  265. WINAPI
  266. PkiAsn1AllocAndReverseBytes(
  267. IN PBYTE pbIn,
  268. IN DWORD cbIn
  269. );
  270. inline void WINAPI
  271. PkiAsn1SetOctetString(IN PCRYPT_DATA_BLOB pInfo,
  272. OUT ASN1octetstring_t * pAsn1)
  273. {
  274. memset(pAsn1, 0, sizeof(*pAsn1));
  275. pAsn1->length = pInfo->cbData;
  276. pAsn1->value = pInfo->pbData;
  277. }
  278. //+-------------------------------------------------------------------------
  279. // Get Octet String
  280. //--------------------------------------------------------------------------
  281. void
  282. WINAPI
  283. PkiAsn1GetOctetString(
  284. IN ASN1uint32_t Asn1Length,
  285. IN ASN1octet_t *pAsn1Value,
  286. IN DWORD dwFlags,
  287. OUT PCRYPT_DATA_BLOB pInfo,
  288. IN OUT BYTE **ppbExtra,
  289. IN OUT LONG *plRemainExtra
  290. );
  291. inline void WINAPI
  292. PkiAsn1GetOctetString(
  293. IN ASN1octetstring_t * pAsn1,
  294. IN DWORD dwFlags,
  295. OUT PCRYPT_DATA_BLOB pInfo,
  296. IN OUT BYTE **ppbExtra,
  297. IN OUT LONG *plRemainExtra
  298. )
  299. {
  300. PkiAsn1GetOctetString(pAsn1->length, pAsn1->value, dwFlags, pInfo, ppbExtra, plRemainExtra);
  301. }
  302. //+-------------------------------------------------------------------------
  303. // Set/Free/Get HugeInteger
  304. //
  305. // PkiAsn1FreeHugeInteger must be called to free the allocated OssValue.
  306. //--------------------------------------------------------------------------
  307. BOOL
  308. WINAPI
  309. PkiAsn1SetHugeInteger(
  310. IN PCRYPT_INTEGER_BLOB pInfo,
  311. OUT ASN1uint32_t *pAsn1Length,
  312. OUT ASN1octet_t **ppAsn1Value
  313. );
  314. inline BOOL WINAPI
  315. PkiAsn1SetHugeInteger(
  316. IN PCRYPT_INTEGER_BLOB pInfo,
  317. OUT ASN1intx_t * pAsn1)
  318. {
  319. return PkiAsn1SetHugeInteger(pInfo, &pAsn1->length, &pAsn1->value);
  320. }
  321. void
  322. WINAPI
  323. PkiAsn1FreeHugeInteger(
  324. IN ASN1octet_t *pAsn1Value
  325. );
  326. inline void WINAPI
  327. PkiAsn1FreeHugeInteger(ASN1intx_t asn1)
  328. {
  329. PkiAsn1FreeHugeInteger(asn1.value);
  330. }
  331. void
  332. WINAPI
  333. PkiAsn1GetHugeInteger(
  334. IN ASN1uint32_t Asn1Length,
  335. IN ASN1octet_t *pAsn1Value,
  336. IN DWORD dwFlags,
  337. OUT PCRYPT_INTEGER_BLOB pInfo,
  338. IN OUT BYTE **ppbExtra,
  339. IN OUT LONG *plRemainExtra
  340. );
  341. inline void
  342. WINAPI
  343. PkiAsn1GetHugeInteger(
  344. ASN1intx_t asn1,
  345. IN DWORD dwFlags,
  346. OUT PCRYPT_INTEGER_BLOB pInfo,
  347. IN OUT BYTE **ppbExtra,
  348. IN OUT LONG *plRemainExtra
  349. )
  350. {
  351. PkiAsn1GetHugeInteger(asn1.length, asn1.value, dwFlags, pInfo, ppbExtra,
  352. plRemainExtra);
  353. }
  354. //+-------------------------------------------------------------------------
  355. // Set/Free/Get Huge Unsigned Integer
  356. //
  357. // Set inserts a leading 0x00 before reversing.
  358. // Get removes a leading 0x00 if present, after reversing.
  359. //
  360. // PkiAsn1FreeHugeUINT must be called to free the allocated OssValue.
  361. //--------------------------------------------------------------------------
  362. BOOL
  363. WINAPI
  364. PkiAsn1SetHugeUINT(
  365. IN PCRYPT_UINT_BLOB pInfo,
  366. OUT ASN1intx_t * pAsn1);
  367. #define PkiAsn1FreeHugeUINT PkiAsn1FreeHugeInteger
  368. void
  369. WINAPI
  370. PkiAsn1GetHugeUINT(
  371. IN ASN1intx_t pAsn1,
  372. IN DWORD dwFlags,
  373. OUT PCRYPT_UINT_BLOB pInfo,
  374. IN OUT BYTE **ppbExtra,
  375. IN OUT LONG *plRemainExtra
  376. );
  377. //+-------------------------------------------------------------------------
  378. // Set/Get BitString
  379. //--------------------------------------------------------------------------
  380. void
  381. WINAPI
  382. PkiAsn1SetBitString(
  383. IN PCRYPT_BIT_BLOB pInfo,
  384. OUT ASN1uint32_t *pAsn1BitLength,
  385. OUT ASN1octet_t **ppAsn1Value
  386. );
  387. void
  388. WINAPI
  389. PkiAsn1GetBitString(
  390. IN ASN1uint32_t Asn1BitLength,
  391. IN ASN1octet_t *pAsn1Value,
  392. IN DWORD dwFlags,
  393. OUT PCRYPT_BIT_BLOB pInfo,
  394. IN OUT BYTE **ppbExtra,
  395. IN OUT LONG *plRemainExtra
  396. );
  397. //+-------------------------------------------------------------------------
  398. // Get IA5 String
  399. //--------------------------------------------------------------------------
  400. void
  401. WINAPI
  402. PkiAsn1GetIA5String(
  403. IN ASN1uint32_t Asn1Length,
  404. IN ASN1char_t *pAsn1Value,
  405. IN DWORD dwFlags,
  406. OUT LPSTR *ppsz,
  407. IN OUT BYTE **ppbExtra,
  408. IN OUT LONG *plRemainExtra
  409. );
  410. //+-------------------------------------------------------------------------
  411. // Set/Free/Get Unicode mapped to IA5 String
  412. //--------------------------------------------------------------------------
  413. BOOL
  414. WINAPI
  415. PkiAsn1SetUnicodeConvertedToIA5String(
  416. IN LPWSTR pwsz,
  417. OUT ASN1uint32_t *pAsn1Length,
  418. OUT ASN1char_t **ppAsn1Value
  419. );
  420. void
  421. WINAPI
  422. PkiAsn1FreeUnicodeConvertedToIA5String(
  423. IN ASN1char_t *pAsn1Value
  424. );
  425. void
  426. WINAPI
  427. PkiAsn1GetIA5StringConvertedToUnicode(
  428. IN ASN1uint32_t Asn1Length,
  429. IN ASN1char_t *pAsn1Value,
  430. IN DWORD dwFlags,
  431. OUT LPWSTR *ppwsz,
  432. IN OUT BYTE **ppbExtra,
  433. IN OUT LONG *plRemainExtra
  434. );
  435. //+-------------------------------------------------------------------------
  436. // Get BMP String
  437. //--------------------------------------------------------------------------
  438. void
  439. WINAPI
  440. PkiAsn1GetBMPString(
  441. IN ASN1uint32_t Asn1Length,
  442. IN ASN1char16_t *pAsn1Value,
  443. IN DWORD dwFlags,
  444. OUT LPWSTR *ppwsz,
  445. IN OUT BYTE **ppbExtra,
  446. IN OUT LONG *plRemainExtra
  447. );
  448. //+-------------------------------------------------------------------------
  449. // Set/Get "Any" DER BLOB
  450. //--------------------------------------------------------------------------
  451. void
  452. WINAPI
  453. PkiAsn1SetAny(
  454. IN PCRYPT_OBJID_BLOB pInfo,
  455. OUT ASN1open_t *pAsn1
  456. );
  457. void
  458. WINAPI
  459. PkiAsn1GetAny(
  460. IN ASN1open_t *pAsn1,
  461. IN DWORD dwFlags,
  462. OUT PCRYPT_OBJID_BLOB pInfo,
  463. IN OUT BYTE **ppbExtra,
  464. IN OUT LONG *plRemainExtra
  465. );
  466. //+-------------------------------------------------------------------------
  467. // Encode an ASN1 formatted info structure
  468. //--------------------------------------------------------------------------
  469. BOOL
  470. WINAPI
  471. PkiAsn1EncodeInfo(
  472. IN ASN1encoding_t pEnc,
  473. IN ASN1uint32_t id,
  474. IN void *pvAsn1Info,
  475. OUT OPTIONAL BYTE *pbEncoded,
  476. IN OUT DWORD *pcbEncoded
  477. );
  478. //+-------------------------------------------------------------------------
  479. // Decode into an allocated, ASN1 formatted info structure
  480. //--------------------------------------------------------------------------
  481. BOOL
  482. WINAPI
  483. PkiAsn1DecodeAndAllocInfo(
  484. IN ASN1decoding_t pDec,
  485. IN ASN1uint32_t id,
  486. IN const BYTE *pbEncoded,
  487. IN DWORD cbEncoded,
  488. OUT void **ppvAsn1Info
  489. );
  490. //+-------------------------------------------------------------------------
  491. // Free an allocated, ASN1 formatted info structure
  492. //--------------------------------------------------------------------------
  493. __inline
  494. void
  495. WINAPI
  496. PkiAsn1FreeInfo(
  497. IN ASN1decoding_t pDec,
  498. IN ASN1uint32_t id,
  499. IN void *pvAsn1Info
  500. )
  501. {
  502. if (pvAsn1Info)
  503. ASN1_FreeDecoded(pDec, pvAsn1Info, id);
  504. }
  505. //+-------------------------------------------------------------------------
  506. // Encode an ASN1 formatted info structure.
  507. //
  508. // If CRYPT_ENCODE_ALLOC_FLAG is set, allocate memory for pbEncoded and
  509. // return *((BYTE **) pvEncoded) = pbAllocEncoded. Otherwise,
  510. // pvEncoded points to byte array to be updated.
  511. //--------------------------------------------------------------------------
  512. BOOL
  513. WINAPI
  514. PkiAsn1EncodeInfoEx(
  515. IN ASN1encoding_t pEnc,
  516. IN ASN1uint32_t id,
  517. IN void *pvAsn1Info,
  518. IN DWORD dwFlags,
  519. IN OPTIONAL PCRYPT_ENCODE_PARA pEncodePara,
  520. OUT OPTIONAL void *pvEncoded,
  521. IN OUT DWORD *pcbEncoded
  522. );
  523. typedef BOOL (WINAPI *PFN_PKI_ASN1_DECODE_EX_CALLBACK)(
  524. IN void *pvAsn1Info,
  525. IN DWORD dwFlags,
  526. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  527. OUT OPTIONAL void *pvStructInfo,
  528. IN OUT LONG *plRemainExtra
  529. );
  530. //+-------------------------------------------------------------------------
  531. // Call the callback to convert the ASN1 structure into the 'C' structure.
  532. // If CRYPT_DECODE_ALLOC_FLAG is set allocate memory for the 'C'
  533. // structure and call the callback initially to get the length and then
  534. // a second time to update the allocated 'C' structure.
  535. //
  536. // Allocated structure is returned:
  537. // *((void **) pvStructInfo) = pvAllocStructInfo
  538. //--------------------------------------------------------------------------
  539. BOOL
  540. WINAPI
  541. PkiAsn1AllocStructInfoEx(
  542. IN void *pvAsn1Info,
  543. IN DWORD dwFlags,
  544. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  545. IN PFN_PKI_ASN1_DECODE_EX_CALLBACK pfnDecodeExCallback,
  546. OUT OPTIONAL void *pvStructInfo,
  547. IN OUT DWORD *pcbStructInfo
  548. );
  549. //+-------------------------------------------------------------------------
  550. // Decode the ASN1 formatted info structure and call the callback
  551. // function to convert the ASN1 structure to the 'C' structure.
  552. //
  553. // If CRYPT_DECODE_ALLOC_FLAG is set allocate memory for the 'C'
  554. // structure and call the callback initially to get the length and then
  555. // a second time to update the allocated 'C' structure.
  556. //
  557. // Allocated structure is returned:
  558. // *((void **) pvStructInfo) = pvAllocStructInfo
  559. //--------------------------------------------------------------------------
  560. BOOL
  561. WINAPI
  562. PkiAsn1DecodeAndAllocInfoEx(
  563. IN ASN1decoding_t pDec,
  564. IN ASN1uint32_t id,
  565. IN const BYTE *pbEncoded,
  566. IN DWORD cbEncoded,
  567. IN DWORD dwFlags,
  568. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  569. IN PFN_PKI_ASN1_DECODE_EX_CALLBACK pfnDecodeExCallback,
  570. OUT OPTIONAL void *pvStructInfo,
  571. IN OUT DWORD *pcbStructInfo
  572. );
  573. //+-------------------------------------------------------------------------
  574. // Convert the ascii string ("1.2.9999") to ASN1's Object Identifier
  575. // represented as an array of unsigned longs.
  576. //
  577. // Returns TRUE for a successful conversion.
  578. //--------------------------------------------------------------------------
  579. BOOL
  580. WINAPI
  581. PkiAsn1ToObjectIdentifier(
  582. IN LPCSTR pszObjId,
  583. IN OUT ASN1uint16_t *pCount,
  584. OUT ASN1uint32_t rgulValue[]
  585. );
  586. //+-------------------------------------------------------------------------
  587. // Convert from OSS's Object Identifier represented as an array of
  588. // unsigned longs to an ascii string ("1.2.9999").
  589. //
  590. // Returns TRUE for a successful conversion
  591. //--------------------------------------------------------------------------
  592. BOOL
  593. WINAPI
  594. PkiAsn1FromObjectIdentifier(
  595. IN ASN1uint16_t Count,
  596. IN ASN1uint32_t rgulValue[],
  597. OUT LPSTR * ppszObjId,
  598. IN OUT BYTE **ppbExtra,
  599. IN OUT LONG *plRemainExtra
  600. );
  601. //+-------------------------------------------------------------------------
  602. // Convert FILETIME to ASN1's UTCTime.
  603. //
  604. // Returns TRUE for a successful conversion
  605. //--------------------------------------------------------------------------
  606. BOOL
  607. WINAPI
  608. PkiAsn1ToUTCTime(
  609. IN LPFILETIME pFileTime,
  610. OUT ASN1utctime_t *pAsn1Time
  611. );
  612. //+-------------------------------------------------------------------------
  613. // Convert from ASN1's UTCTime to FILETIME.
  614. //
  615. // Returns TRUE for a successful conversion
  616. //--------------------------------------------------------------------------
  617. BOOL
  618. WINAPI
  619. PkiAsn1FromUTCTime(
  620. IN ASN1utctime_t *pAsn1Time,
  621. OUT LPFILETIME pFileTime
  622. );
  623. //+-------------------------------------------------------------------------
  624. // Convert FILETIME to ASN1's GeneralizedTime.
  625. //
  626. // Returns TRUE for a successful conversion
  627. //--------------------------------------------------------------------------
  628. BOOL
  629. WINAPI
  630. PkiAsn1ToGeneralizedTime(
  631. IN LPFILETIME pFileTime,
  632. OUT ASN1generalizedtime_t *pAsn1Time
  633. );
  634. //+-------------------------------------------------------------------------
  635. // Convert from ASN1's GeneralizedTime to FILETIME.
  636. //
  637. // Returns TRUE for a successful conversion
  638. //--------------------------------------------------------------------------
  639. BOOL
  640. WINAPI
  641. PkiAsn1FromGeneralizedTime(
  642. IN ASN1generalizedtime_t *pAsn1Time,
  643. OUT LPFILETIME pFileTime
  644. );
  645. //+-------------------------------------------------------------------------
  646. // Convert FILETIME to ASN1's UTCTime or GeneralizedTime.
  647. //
  648. // If 1950 < FILETIME < 2005, then UTCTime is chosen. Otherwise,
  649. // GeneralizedTime is chosen. GeneralizedTime values shall not include
  650. // fractional seconds.
  651. //
  652. // Returns TRUE for a successful conversion
  653. //--------------------------------------------------------------------------
  654. BOOL
  655. WINAPI
  656. PkiAsn1ToChoiceOfTime(
  657. IN LPFILETIME pFileTime,
  658. OUT WORD *pwChoice,
  659. OUT ASN1generalizedtime_t *pGeneralTime,
  660. OUT ASN1utctime_t *pUtcTime
  661. );
  662. #define PKI_ASN1_UTC_TIME_CHOICE 1
  663. #define PKI_ASN1_GENERALIZED_TIME_CHOICE 2
  664. //+-------------------------------------------------------------------------
  665. // Convert from ASN1's UTCTime or GeneralizedTime to FILETIME.
  666. //
  667. // Returns TRUE for a successful conversion.
  668. //--------------------------------------------------------------------------
  669. BOOL
  670. WINAPI
  671. PkiAsn1FromChoiceOfTime(
  672. IN WORD wChoice,
  673. IN ASN1generalizedtime_t *pGeneralTime,
  674. IN ASN1utctime_t *pUtcTime,
  675. OUT LPFILETIME pFileTime
  676. );
  677. #else
  678. //+=========================================================================
  679. // The following map to the OSS ASN1 routines
  680. //==========================================================================
  681. //+-------------------------------------------------------------------------
  682. // Convert Asn1 error to a HRESULT.
  683. //--------------------------------------------------------------------------
  684. __inline
  685. HRESULT
  686. WINAPI
  687. PkiAsn1ErrToHr(ASN1error_e Asn1Err) {
  688. if (0 <= Asn1Err && 1000 > Asn1Err)
  689. return CRYPT_E_OSS_ERROR + Asn1Err;
  690. else if (0 > Asn1Err)
  691. return CRYPT_E_OSS_ERROR + 0x100 + (-Asn1Err -1000);
  692. else
  693. return CRYPT_E_OSS_ERROR + 0x200 + (Asn1Err -1000);
  694. }
  695. //+-------------------------------------------------------------------------
  696. // Asn1 Encode function. The encoded output is allocated and must be freed
  697. // by calling PkiAsn1FreeEncoded().
  698. //--------------------------------------------------------------------------
  699. __inline
  700. ASN1error_e
  701. WINAPI
  702. PkiAsn1Encode(
  703. IN ASN1encoding_t pEnc,
  704. IN void *pvAsn1Info,
  705. IN ASN1uint32_t id,
  706. OUT BYTE **ppbEncoded,
  707. OUT OPTIONAL DWORD *pcbEncoded = NULL
  708. )
  709. {
  710. return (ASN1error_e) PkiOssEncode(
  711. (OssGlobal *) pEnc,
  712. pvAsn1Info,
  713. (int) id,
  714. ppbEncoded,
  715. pcbEncoded
  716. );
  717. }
  718. //+-------------------------------------------------------------------------
  719. // Free encoded output returned by PkiAsn1Encode().
  720. //--------------------------------------------------------------------------
  721. __inline
  722. void
  723. WINAPI
  724. PkiAsn1FreeEncoded(
  725. IN ASN1encoding_t pEnc,
  726. IN void *pvEncoded
  727. )
  728. {
  729. if (pvEncoded)
  730. ossFreeBuf((OssGlobal *) pEnc, pvEncoded);
  731. }
  732. //+-------------------------------------------------------------------------
  733. // Asn1 Encode function. The encoded output isn't allocated.
  734. //
  735. // If pbEncoded is NULL, does a length only calculation.
  736. //--------------------------------------------------------------------------
  737. __inline
  738. ASN1error_e
  739. WINAPI
  740. PkiAsn1Encode2(
  741. IN ASN1encoding_t pEnc,
  742. IN void *pvAsn1Info,
  743. IN ASN1uint32_t id,
  744. OUT OPTIONAL BYTE *pbEncoded,
  745. IN OUT DWORD *pcbEncoded
  746. )
  747. {
  748. return (ASN1error_e) PkiOssEncode2(
  749. (OssGlobal *) pEnc,
  750. pvAsn1Info,
  751. (int) id,
  752. pbEncoded,
  753. pcbEncoded
  754. );
  755. }
  756. //+-------------------------------------------------------------------------
  757. // Asn1 Decode function. The allocated, decoded structure, **pvAsn1Info, must
  758. // be freed by calling PkiAsn1FreeDecoded().
  759. //--------------------------------------------------------------------------
  760. __inline
  761. ASN1error_e
  762. WINAPI
  763. PkiAsn1Decode(
  764. IN ASN1decoding_t pDec,
  765. OUT void **ppvAsn1Info,
  766. IN ASN1uint32_t id,
  767. IN const BYTE *pbEncoded,
  768. IN DWORD cbEncoded
  769. )
  770. {
  771. return (ASN1error_e) PkiOssDecode(
  772. (OssGlobal *) pDec,
  773. ppvAsn1Info,
  774. (int) id,
  775. pbEncoded,
  776. cbEncoded
  777. );
  778. }
  779. //+-------------------------------------------------------------------------
  780. // Asn1 Decode function. The allocated, decoded structure, **pvAsn1Info, must
  781. // be freed by calling PkiAsn1FreeDecoded().
  782. //
  783. // For a successful decode, *ppbEncoded is advanced
  784. // past the decoded bytes and *pcbDecoded is decremented by the number
  785. // of decoded bytes.
  786. //--------------------------------------------------------------------------
  787. __inline
  788. ASN1error_e
  789. WINAPI
  790. PkiAsn1Decode2(
  791. IN ASN1decoding_t pDec,
  792. OUT void **ppvAsn1Info,
  793. IN ASN1uint32_t id,
  794. IN OUT BYTE **ppbEncoded,
  795. IN OUT DWORD *pcbEncoded
  796. )
  797. {
  798. return (ASN1error_e) PkiOssDecode2(
  799. (OssGlobal *) pDec,
  800. ppvAsn1Info,
  801. (int) id,
  802. ppbEncoded,
  803. pcbEncoded
  804. );
  805. }
  806. //+-------------------------------------------------------------------------
  807. // Free decoded structure returned by PkiAsn1Decode() or PkiAsn1Decode2().
  808. //--------------------------------------------------------------------------
  809. __inline
  810. void
  811. WINAPI
  812. PkiAsn1FreeDecoded(
  813. IN ASN1decoding_t pDec,
  814. IN void *pvAsn1Info,
  815. IN ASN1uint32_t id
  816. )
  817. {
  818. if (pvAsn1Info)
  819. ossFreePDU((OssGlobal *) pDec, (int) id, pvAsn1Info);
  820. }
  821. //+-------------------------------------------------------------------------
  822. // Asn1 Set/Get encoding rules functions
  823. //--------------------------------------------------------------------------
  824. __inline
  825. ASN1error_e
  826. WINAPI
  827. PkiAsn1SetEncodingRule(
  828. IN ASN1encoding_t pEnc,
  829. IN ASN1encodingrule_e eRule
  830. )
  831. {
  832. ossEncodingRules ossRules;
  833. if (ASN1_BER_RULE_BER == eRule)
  834. ossRules = OSS_BER;
  835. else
  836. ossRules = OSS_DER;
  837. return (ASN1error_e) ossSetEncodingRules((OssGlobal *) pEnc, ossRules);
  838. }
  839. __inline
  840. ASN1encodingrule_e
  841. WINAPI
  842. PkiAsn1GetEncodingRule(
  843. IN ASN1encoding_t pEnc
  844. )
  845. {
  846. ossEncodingRules ossRules;
  847. ossRules = ossGetEncodingRules((OssGlobal *) pEnc);
  848. if (OSS_BER == ossRules)
  849. return ASN1_BER_RULE_BER;
  850. else
  851. return ASN1_BER_RULE_DER;
  852. }
  853. //+-------------------------------------------------------------------------
  854. // Asn1 EncodedOid To/From DotVal functions
  855. //--------------------------------------------------------------------------
  856. __inline
  857. LPSTR
  858. WINAPI
  859. PkiAsn1EncodedOidToDotVal(
  860. IN ASN1decoding_t pDec,
  861. IN ASN1encodedOID_t *pEncodedOid
  862. )
  863. {
  864. OssEncodedOID OssEncodedOid;
  865. OssBuf dotOid;
  866. memset(&dotOid, 0, sizeof(dotOid));
  867. OssEncodedOid.length = pEncodedOid->length;
  868. OssEncodedOid.value = pEncodedOid->value;
  869. if (0 == ossEncodedOidToDotVal((OssGlobal *) pDec, &OssEncodedOid,
  870. &dotOid))
  871. return (LPSTR) dotOid.value;
  872. else
  873. return NULL;
  874. }
  875. __inline
  876. void
  877. WINAPI
  878. PkiAsn1FreeDotVal(
  879. IN ASN1decoding_t pDec,
  880. IN LPSTR pszDotVal
  881. )
  882. {
  883. if (pszDotVal)
  884. ossFreeBuf((OssGlobal *) pDec, pszDotVal);
  885. }
  886. // Returns nonzero for success
  887. __inline
  888. int
  889. WINAPI
  890. PkiAsn1DotValToEncodedOid(
  891. IN ASN1encoding_t pEnc,
  892. IN LPSTR pszDotVal,
  893. OUT ASN1encodedOID_t *pEncodedOid
  894. )
  895. {
  896. OssEncodedOID eoid;
  897. memset(&eoid, 0, sizeof(eoid));
  898. if (0 == ossDotValToEncodedOid((OssGlobal *) pEnc, pszDotVal, &eoid)) {
  899. pEncodedOid->length = eoid.length;
  900. pEncodedOid->value = eoid.value;
  901. return 1;
  902. } else {
  903. pEncodedOid->length = 0;
  904. pEncodedOid->value = NULL;
  905. return 0;
  906. }
  907. }
  908. __inline
  909. void
  910. WINAPI
  911. PkiAsn1FreeEncodedOid(
  912. IN ASN1encoding_t pEnc,
  913. IN ASN1encodedOID_t *pEncodedOid
  914. )
  915. {
  916. if (pEncodedOid->value)
  917. ossFreeBuf((OssGlobal *) pEnc, pEncodedOid->value);
  918. }
  919. #define PkiAsn1Alloc OssUtilAlloc
  920. #define PkiAsn1Free OssUtilFree
  921. #define PkiAsn1ReverseBytes OssUtilReverseBytes
  922. #define PkiAsn1AllocAndReverseBytes OssUtilAllocAndReverseBytes
  923. #define PkiAsn1GetOctetString OssUtilGetOctetString
  924. #define PkiAsn1SetHugeInteger OssUtilSetHugeInteger
  925. #define PkiAsn1FreeHugeInteger OssUtilFreeHugeInteger
  926. #define PkiAsn1GetHugeInteger OssUtilGetHugeInteger
  927. #define PkiAsn1SetHugeUINT OssUtilSetHugeUINT
  928. #define PkiAsn1FreeHugeUINT OssUtilFreeHugeInteger
  929. #define PkiAsn1GetHugeUINT OssUtilGetHugeUINT
  930. #define PkiAsn1SetBitString OssUtilSetBitString
  931. #define PkiAsn1GetBitString OssUtilGetBitString
  932. #define PkiAsn1GetIA5String OssUtilGetIA5String
  933. #define PkiAsn1SetUnicodeConvertedToIA5String OssUtilSetUnicodeConvertedToIA5String
  934. #define PkiAsn1FreeUnicodeConvertedToIA5String OssUtilFreeUnicodeConvertedToIA5String
  935. #define PkiAsn1GetIA5StringConvertedToUnicode OssUtilGetIA5StringConvertedToUnicode
  936. #define PkiAsn1GetBMPString OssUtilGetBMPString
  937. #define PkiAsn1SetAny OssUtilSetAny
  938. #define PkiAsn1GetAny OssUtilGetAny
  939. //+-------------------------------------------------------------------------
  940. // Encode an ASN1 formatted info structure
  941. //--------------------------------------------------------------------------
  942. __inline
  943. BOOL
  944. WINAPI
  945. PkiAsn1EncodeInfo(
  946. IN ASN1encoding_t pEnc,
  947. IN ASN1uint32_t id,
  948. IN void *pvAsn1Info,
  949. OUT OPTIONAL BYTE *pbEncoded,
  950. IN OUT DWORD *pcbEncoded
  951. )
  952. {
  953. return OssUtilEncodeInfo(
  954. (OssGlobal *) pEnc,
  955. (int) id,
  956. pvAsn1Info,
  957. pbEncoded,
  958. pcbEncoded
  959. );
  960. }
  961. //+-------------------------------------------------------------------------
  962. // Decode into an allocated, OSS formatted info structure
  963. //--------------------------------------------------------------------------
  964. __inline
  965. BOOL
  966. WINAPI
  967. PkiAsn1DecodeAndAllocInfo(
  968. IN ASN1decoding_t pDec,
  969. IN ASN1uint32_t id,
  970. IN const BYTE *pbEncoded,
  971. IN DWORD cbEncoded,
  972. OUT void **ppvAsn1Info
  973. )
  974. {
  975. return OssUtilDecodeAndAllocInfo(
  976. (OssGlobal *) pDec,
  977. (int) id,
  978. pbEncoded,
  979. cbEncoded,
  980. ppvAsn1Info
  981. );
  982. }
  983. //+-------------------------------------------------------------------------
  984. // Free an allocated, OSS formatted info structure
  985. //--------------------------------------------------------------------------
  986. __inline
  987. void
  988. WINAPI
  989. PkiAsn1FreeInfo(
  990. IN ASN1decoding_t pDec,
  991. IN ASN1uint32_t id,
  992. IN void *pvAsn1Info
  993. )
  994. {
  995. OssUtilFreeInfo(
  996. (OssGlobal *) pDec,
  997. (int) id,
  998. pvAsn1Info
  999. );
  1000. }
  1001. //+-------------------------------------------------------------------------
  1002. // Encode an OSS formatted info structure.
  1003. //
  1004. // If CRYPT_ENCODE_ALLOC_FLAG is set, allocate memory for pbEncoded and
  1005. // return *((BYTE **) pvEncoded) = pbAllocEncoded. Otherwise,
  1006. // pvEncoded points to byte array to be updated.
  1007. //--------------------------------------------------------------------------
  1008. __inline
  1009. BOOL
  1010. WINAPI
  1011. PkiAsn1EncodeInfoEx(
  1012. IN ASN1encoding_t pEnc,
  1013. IN ASN1uint32_t id,
  1014. IN void *pvAsn1Info,
  1015. IN DWORD dwFlags,
  1016. IN OPTIONAL PCRYPT_ENCODE_PARA pEncodePara,
  1017. OUT OPTIONAL void *pvEncoded,
  1018. IN OUT DWORD *pcbEncoded
  1019. )
  1020. {
  1021. return OssUtilEncodeInfoEx(
  1022. (OssGlobal *) pEnc,
  1023. (int) id,
  1024. pvAsn1Info,
  1025. dwFlags,
  1026. pEncodePara,
  1027. pvEncoded,
  1028. pcbEncoded
  1029. );
  1030. }
  1031. typedef BOOL (WINAPI *PFN_PKI_ASN1_DECODE_EX_CALLBACK)(
  1032. IN void *pvAsn1Info,
  1033. IN DWORD dwFlags,
  1034. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  1035. OUT OPTIONAL void *pvStructInfo,
  1036. IN OUT LONG *plRemainExtra
  1037. );
  1038. //+-------------------------------------------------------------------------
  1039. // Call the callback to convert the ASN1 structure into the 'C' structure.
  1040. // If CRYPT_DECODE_ALLOC_FLAG is set allocate memory for the 'C'
  1041. // structure and call the callback initially to get the length and then
  1042. // a second time to update the allocated 'C' structure.
  1043. //
  1044. // Allocated structure is returned:
  1045. // *((void **) pvStructInfo) = pvAllocStructInfo
  1046. //--------------------------------------------------------------------------
  1047. __inline
  1048. BOOL
  1049. WINAPI
  1050. PkiAsn1AllocStructInfoEx(
  1051. IN void *pvAsn1Info,
  1052. IN DWORD dwFlags,
  1053. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  1054. IN PFN_PKI_ASN1_DECODE_EX_CALLBACK pfnDecodeExCallback,
  1055. OUT OPTIONAL void *pvStructInfo,
  1056. IN OUT DWORD *pcbStructInfo
  1057. )
  1058. {
  1059. return OssUtilAllocStructInfoEx(
  1060. pvAsn1Info,
  1061. dwFlags,
  1062. pDecodePara,
  1063. (PFN_OSS_UTIL_DECODE_EX_CALLBACK) pfnDecodeExCallback,
  1064. pvStructInfo,
  1065. pcbStructInfo
  1066. );
  1067. }
  1068. //+-------------------------------------------------------------------------
  1069. // Decode the ASN1 formatted info structure and call the callback
  1070. // function to convert the ASN1 structure to the 'C' structure.
  1071. //
  1072. // If CRYPT_DECODE_ALLOC_FLAG is set allocate memory for the 'C'
  1073. // structure and call the callback initially to get the length and then
  1074. // a second time to update the allocated 'C' structure.
  1075. //
  1076. // Allocated structure is returned:
  1077. // *((void **) pvStructInfo) = pvAllocStructInfo
  1078. //--------------------------------------------------------------------------
  1079. __inline
  1080. BOOL
  1081. WINAPI
  1082. PkiAsn1DecodeAndAllocInfoEx(
  1083. IN ASN1decoding_t pDec,
  1084. IN ASN1uint32_t id,
  1085. IN const BYTE *pbEncoded,
  1086. IN DWORD cbEncoded,
  1087. IN DWORD dwFlags,
  1088. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  1089. IN PFN_PKI_ASN1_DECODE_EX_CALLBACK pfnDecodeExCallback,
  1090. OUT OPTIONAL void *pvStructInfo,
  1091. IN OUT DWORD *pcbStructInfo
  1092. )
  1093. {
  1094. return OssUtilDecodeAndAllocInfoEx(
  1095. (OssGlobal *) pDec,
  1096. (int) id,
  1097. pbEncoded,
  1098. cbEncoded,
  1099. dwFlags,
  1100. pDecodePara,
  1101. (PFN_OSS_UTIL_DECODE_EX_CALLBACK) pfnDecodeExCallback,
  1102. pvStructInfo,
  1103. pcbStructInfo
  1104. );
  1105. }
  1106. #define PkiAsn1ToObjectIdentifier OssConvToObjectIdentifier
  1107. #define PkiAsn1FromObjectIdentifier OssConvFromObjectIdentifier
  1108. #define PkiAsn1ToUTCTime OssConvToUTCTime
  1109. #define PkiAsn1FromUTCTime OssConvFromUTCTime
  1110. #define PkiAsn1ToGeneralizedTime OssConvToGeneralizedTime
  1111. #define PkiAsn1FromGeneralizedTime OssConvFromGeneralizedTime
  1112. __inline
  1113. BOOL
  1114. WINAPI
  1115. PkiAsn1ToChoiceOfTime(
  1116. IN LPFILETIME pFileTime,
  1117. OUT WORD *pwChoice,
  1118. OUT GeneralizedTime *pGeneralTime,
  1119. OUT UTCTime *pUtcTime
  1120. )
  1121. {
  1122. return OssConvToChoiceOfTime(
  1123. pFileTime,
  1124. pwChoice,
  1125. pGeneralTime
  1126. );
  1127. }
  1128. #define PKI_ASN1_UTC_TIME_CHOICE OSS_UTC_TIME_CHOICE
  1129. #define PKI_ASN1_GENERALIZED_TIME_CHOICE OSS_GENERALIZED_TIME_CHOICE
  1130. __inline
  1131. BOOL
  1132. WINAPI
  1133. PkiAsn1FromChoiceOfTime(
  1134. IN WORD wChoice,
  1135. IN GeneralizedTime *pGeneralTime,
  1136. IN UTCTime *pUtcTime,
  1137. OUT LPFILETIME pFileTime
  1138. )
  1139. {
  1140. return OssConvFromChoiceOfTime(
  1141. wChoice,
  1142. pGeneralTime,
  1143. pFileTime
  1144. );
  1145. }
  1146. #endif // OSS_CRYPT_ASN1
  1147. #endif