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.

388 lines
11 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. // PkiAsn1ReverseBytes
  25. //
  26. // PkiAsn1EncodeInfo
  27. // PkiAsn1DecodeAndAllocInfo
  28. // PkiAsn1FreeInfo
  29. // PkiAsn1EncodeInfoEx
  30. // PkiAsn1DecodeAndAllocInfo
  31. // PkiAsn1AllocStructInfoEx
  32. // PkiAsn1DecodeAndAllocInfoEx
  33. //
  34. // PkiAsn1ToObjectIdentifier
  35. // PkiAsn1FromObjectIdentifier
  36. //
  37. // History: 23-Oct-98 philh created
  38. //--------------------------------------------------------------------------
  39. #ifndef __PKIASN1_H__
  40. #define __PKIASN1_H__
  41. #include <msber.h>
  42. #include <msasn1.h>
  43. #include <winerror.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. //+-------------------------------------------------------------------------
  48. // Convert Asn1 error to a HRESULT.
  49. //--------------------------------------------------------------------------
  50. __inline
  51. HRESULT
  52. WINAPI
  53. PkiAsn1ErrToHr(ASN1error_e Asn1Err) {
  54. if (0 > Asn1Err)
  55. return CRYPT_E_ASN1_ERROR + (-Asn1Err -1000);
  56. else
  57. return CRYPT_E_ASN1_ERROR + 0x100 + (Asn1Err -1000);
  58. }
  59. //+-------------------------------------------------------------------------
  60. // Asn1 Encode function. The encoded output is allocated and must be freed
  61. // by calling PkiAsn1FreeEncoded().
  62. //--------------------------------------------------------------------------
  63. ASN1error_e
  64. WINAPI
  65. PkiAsn1Encode(
  66. IN ASN1encoding_t pEnc,
  67. IN void *pvAsn1Info,
  68. IN ASN1uint32_t id,
  69. OUT BYTE **ppbEncoded,
  70. OUT DWORD *pcbEncoded
  71. );
  72. //+-------------------------------------------------------------------------
  73. // Free encoded output returned by PkiAsn1Encode().
  74. //--------------------------------------------------------------------------
  75. __inline
  76. void
  77. WINAPI
  78. PkiAsn1FreeEncoded(
  79. IN ASN1encoding_t pEnc,
  80. IN void *pvEncoded
  81. )
  82. {
  83. if (pvEncoded)
  84. ASN1_FreeEncoded(pEnc, pvEncoded);
  85. }
  86. //+-------------------------------------------------------------------------
  87. // Asn1 Encode function. The encoded output isn't allocated.
  88. //
  89. // If pbEncoded is NULL, does a length only calculation.
  90. //--------------------------------------------------------------------------
  91. ASN1error_e
  92. WINAPI
  93. PkiAsn1Encode2(
  94. IN ASN1encoding_t pEnc,
  95. IN void *pvAsn1Info,
  96. IN ASN1uint32_t id,
  97. OUT OPTIONAL BYTE *pbEncoded,
  98. IN OUT DWORD *pcbEncoded
  99. );
  100. //+-------------------------------------------------------------------------
  101. // Asn1 Decode function. The allocated, decoded structure, **pvAsn1Info, must
  102. // be freed by calling PkiAsn1FreeDecoded().
  103. //--------------------------------------------------------------------------
  104. ASN1error_e
  105. WINAPI
  106. PkiAsn1Decode(
  107. IN ASN1decoding_t pDec,
  108. OUT void **ppvAsn1Info,
  109. IN ASN1uint32_t id,
  110. IN const BYTE *pbEncoded,
  111. IN DWORD cbEncoded
  112. );
  113. //+-------------------------------------------------------------------------
  114. // Asn1 Decode function. The allocated, decoded structure, **pvAsn1Info, must
  115. // be freed by calling PkiAsn1FreeDecoded().
  116. //
  117. // For a successful decode, *ppbEncoded is advanced
  118. // past the decoded bytes and *pcbDecoded is decremented by the number
  119. // of decoded bytes.
  120. //--------------------------------------------------------------------------
  121. ASN1error_e
  122. WINAPI
  123. PkiAsn1Decode2(
  124. IN ASN1decoding_t pDec,
  125. OUT void **ppvAsn1Info,
  126. IN ASN1uint32_t id,
  127. IN OUT BYTE **ppbEncoded,
  128. IN OUT DWORD *pcbEncoded
  129. );
  130. //+-------------------------------------------------------------------------
  131. // Free decoded structure returned by PkiAsn1Decode() or PkiAsn1Decode2().
  132. //--------------------------------------------------------------------------
  133. __inline
  134. void
  135. WINAPI
  136. PkiAsn1FreeDecoded(
  137. IN ASN1decoding_t pDec,
  138. IN void *pvAsn1Info,
  139. IN ASN1uint32_t id
  140. )
  141. {
  142. if (pvAsn1Info)
  143. ASN1_FreeDecoded(pDec, pvAsn1Info, id);
  144. }
  145. //+-------------------------------------------------------------------------
  146. // Asn1 Set/Get encoding rule functions
  147. //--------------------------------------------------------------------------
  148. ASN1error_e
  149. WINAPI
  150. PkiAsn1SetEncodingRule(
  151. IN ASN1encoding_t pEnc,
  152. IN ASN1encodingrule_e eRule
  153. );
  154. ASN1encodingrule_e
  155. WINAPI
  156. PkiAsn1GetEncodingRule(
  157. IN ASN1encoding_t pEnc
  158. );
  159. //+-------------------------------------------------------------------------
  160. // Asn1 EncodedOid To/From DotVal functions
  161. //--------------------------------------------------------------------------
  162. __inline
  163. LPSTR
  164. WINAPI
  165. PkiAsn1EncodedOidToDotVal(
  166. IN ASN1decoding_t pDec,
  167. IN ASN1encodedOID_t *pEncodedOid
  168. )
  169. {
  170. LPSTR pszDotVal = NULL;
  171. if (ASN1BEREoid2DotVal(pDec, pEncodedOid, &pszDotVal))
  172. return pszDotVal;
  173. else
  174. return NULL;
  175. }
  176. __inline
  177. void
  178. WINAPI
  179. PkiAsn1FreeDotVal(
  180. IN ASN1decoding_t pDec,
  181. IN LPSTR pszDotVal
  182. )
  183. {
  184. if (pszDotVal)
  185. ASN1Free(pszDotVal);
  186. }
  187. // Returns nonzero for success
  188. __inline
  189. int
  190. WINAPI
  191. PkiAsn1DotValToEncodedOid(
  192. IN ASN1encoding_t pEnc,
  193. IN LPSTR pszDotVal,
  194. OUT ASN1encodedOID_t *pEncodedOid
  195. )
  196. {
  197. return ASN1BERDotVal2Eoid(pEnc, pszDotVal, pEncodedOid);
  198. }
  199. __inline
  200. void
  201. WINAPI
  202. PkiAsn1FreeEncodedOid(
  203. IN ASN1encoding_t pEnc,
  204. IN ASN1encodedOID_t *pEncodedOid
  205. )
  206. {
  207. if (pEncodedOid->value)
  208. ASN1_FreeEncoded(pEnc, pEncodedOid->value);
  209. }
  210. //+-------------------------------------------------------------------------
  211. // Reverses a buffer of bytes in place
  212. //--------------------------------------------------------------------------
  213. void
  214. WINAPI
  215. PkiAsn1ReverseBytes(
  216. IN OUT PBYTE pbIn,
  217. IN DWORD cbIn
  218. );
  219. //+-------------------------------------------------------------------------
  220. // Encode an ASN1 formatted info structure
  221. //--------------------------------------------------------------------------
  222. BOOL
  223. WINAPI
  224. PkiAsn1EncodeInfo(
  225. IN ASN1encoding_t pEnc,
  226. IN ASN1uint32_t id,
  227. IN void *pvAsn1Info,
  228. OUT OPTIONAL BYTE *pbEncoded,
  229. IN OUT DWORD *pcbEncoded
  230. );
  231. //+-------------------------------------------------------------------------
  232. // Decode into an allocated, ASN1 formatted info structure
  233. //--------------------------------------------------------------------------
  234. BOOL
  235. WINAPI
  236. PkiAsn1DecodeAndAllocInfo(
  237. IN ASN1decoding_t pDec,
  238. IN ASN1uint32_t id,
  239. IN const BYTE *pbEncoded,
  240. IN DWORD cbEncoded,
  241. OUT void **ppvAsn1Info
  242. );
  243. //+-------------------------------------------------------------------------
  244. // Free an allocated, ASN1 formatted info structure
  245. //--------------------------------------------------------------------------
  246. __inline
  247. void
  248. WINAPI
  249. PkiAsn1FreeInfo(
  250. IN ASN1decoding_t pDec,
  251. IN ASN1uint32_t id,
  252. IN void *pvAsn1Info
  253. )
  254. {
  255. if (pvAsn1Info)
  256. ASN1_FreeDecoded(pDec, pvAsn1Info, id);
  257. }
  258. //+-------------------------------------------------------------------------
  259. // Encode an ASN1 formatted info structure.
  260. //
  261. // If CRYPT_ENCODE_ALLOC_FLAG is set, allocate memory for pbEncoded and
  262. // return *((BYTE **) pvEncoded) = pbAllocEncoded. Otherwise,
  263. // pvEncoded points to byte array to be updated.
  264. //--------------------------------------------------------------------------
  265. BOOL
  266. WINAPI
  267. PkiAsn1EncodeInfoEx(
  268. IN ASN1encoding_t pEnc,
  269. IN ASN1uint32_t id,
  270. IN void *pvAsn1Info,
  271. IN DWORD dwFlags,
  272. IN OPTIONAL PCRYPT_ENCODE_PARA pEncodePara,
  273. OUT OPTIONAL void *pvEncoded,
  274. IN OUT DWORD *pcbEncoded
  275. );
  276. typedef BOOL (WINAPI *PFN_PKI_ASN1_DECODE_EX_CALLBACK)(
  277. IN void *pvAsn1Info,
  278. IN DWORD dwFlags,
  279. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  280. OUT OPTIONAL void *pvStructInfo,
  281. IN OUT LONG *plRemainExtra
  282. );
  283. //+-------------------------------------------------------------------------
  284. // Call the callback to convert the ASN1 structure into the 'C' structure.
  285. // If CRYPT_DECODE_ALLOC_FLAG is set allocate memory for the 'C'
  286. // structure and call the callback initially to get the length and then
  287. // a second time to update the allocated 'C' structure.
  288. //
  289. // Allocated structure is returned:
  290. // *((void **) pvStructInfo) = pvAllocStructInfo
  291. //--------------------------------------------------------------------------
  292. BOOL
  293. WINAPI
  294. PkiAsn1AllocStructInfoEx(
  295. IN void *pvAsn1Info,
  296. IN DWORD dwFlags,
  297. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  298. IN PFN_PKI_ASN1_DECODE_EX_CALLBACK pfnDecodeExCallback,
  299. OUT OPTIONAL void *pvStructInfo,
  300. IN OUT DWORD *pcbStructInfo
  301. );
  302. //+-------------------------------------------------------------------------
  303. // Decode the ASN1 formatted info structure and call the callback
  304. // function to convert the ASN1 structure to the 'C' structure.
  305. //
  306. // If CRYPT_DECODE_ALLOC_FLAG is set allocate memory for the 'C'
  307. // structure and call the callback initially to get the length and then
  308. // a second time to update the allocated 'C' structure.
  309. //
  310. // Allocated structure is returned:
  311. // *((void **) pvStructInfo) = pvAllocStructInfo
  312. //--------------------------------------------------------------------------
  313. BOOL
  314. WINAPI
  315. PkiAsn1DecodeAndAllocInfoEx(
  316. IN ASN1decoding_t pDec,
  317. IN ASN1uint32_t id,
  318. IN const BYTE *pbEncoded,
  319. IN DWORD cbEncoded,
  320. IN DWORD dwFlags,
  321. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  322. IN PFN_PKI_ASN1_DECODE_EX_CALLBACK pfnDecodeExCallback,
  323. OUT OPTIONAL void *pvStructInfo,
  324. IN OUT DWORD *pcbStructInfo
  325. );
  326. //+-------------------------------------------------------------------------
  327. // Convert the ascii string ("1.2.9999") to ASN1's Object Identifier
  328. // represented as an array of unsigned longs.
  329. //
  330. // Returns TRUE for a successful conversion.
  331. //--------------------------------------------------------------------------
  332. BOOL
  333. WINAPI
  334. PkiAsn1ToObjectIdentifier(
  335. IN LPCSTR pszObjId,
  336. IN OUT ASN1uint16_t *pCount,
  337. OUT ASN1uint32_t rgulValue[]
  338. );
  339. //+-------------------------------------------------------------------------
  340. // Convert from OSS's Object Identifier represented as an array of
  341. // unsigned longs to an ascii string ("1.2.9999").
  342. //
  343. // Returns TRUE for a successful conversion
  344. //--------------------------------------------------------------------------
  345. BOOL
  346. WINAPI
  347. PkiAsn1FromObjectIdentifier(
  348. IN ASN1uint16_t Count,
  349. IN ASN1uint32_t rgulValue[],
  350. OUT LPSTR pszObjId,
  351. IN OUT DWORD *pcbObjId
  352. );
  353. #ifdef __cplusplus
  354. } // Balance extern "C" above
  355. #endif
  356. #endif