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.

1215 lines
35 KiB

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