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.

390 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. UNREFERENCED_PARAMETER(pDec);
  185. if (pszDotVal)
  186. ASN1Free(pszDotVal);
  187. }
  188. // Returns nonzero for success
  189. __inline
  190. int
  191. WINAPI
  192. PkiAsn1DotValToEncodedOid(
  193. IN ASN1encoding_t pEnc,
  194. IN LPSTR pszDotVal,
  195. OUT ASN1encodedOID_t *pEncodedOid
  196. )
  197. {
  198. return ASN1BERDotVal2Eoid(pEnc, pszDotVal, pEncodedOid);
  199. }
  200. __inline
  201. void
  202. WINAPI
  203. PkiAsn1FreeEncodedOid(
  204. IN ASN1encoding_t pEnc,
  205. IN ASN1encodedOID_t *pEncodedOid
  206. )
  207. {
  208. if (pEncodedOid->value)
  209. ASN1_FreeEncoded(pEnc, pEncodedOid->value);
  210. }
  211. //+-------------------------------------------------------------------------
  212. // Reverses a buffer of bytes in place
  213. //--------------------------------------------------------------------------
  214. void
  215. WINAPI
  216. PkiAsn1ReverseBytes(
  217. IN OUT PBYTE pbIn,
  218. IN DWORD cbIn
  219. );
  220. //+-------------------------------------------------------------------------
  221. // Encode an ASN1 formatted info structure
  222. //--------------------------------------------------------------------------
  223. BOOL
  224. WINAPI
  225. PkiAsn1EncodeInfo(
  226. IN ASN1encoding_t pEnc,
  227. IN ASN1uint32_t id,
  228. IN void *pvAsn1Info,
  229. OUT OPTIONAL BYTE *pbEncoded,
  230. IN OUT DWORD *pcbEncoded
  231. );
  232. //+-------------------------------------------------------------------------
  233. // Decode into an allocated, ASN1 formatted info structure
  234. //--------------------------------------------------------------------------
  235. BOOL
  236. WINAPI
  237. PkiAsn1DecodeAndAllocInfo(
  238. IN ASN1decoding_t pDec,
  239. IN ASN1uint32_t id,
  240. IN const BYTE *pbEncoded,
  241. IN DWORD cbEncoded,
  242. OUT void **ppvAsn1Info
  243. );
  244. //+-------------------------------------------------------------------------
  245. // Free an allocated, ASN1 formatted info structure
  246. //--------------------------------------------------------------------------
  247. __inline
  248. void
  249. WINAPI
  250. PkiAsn1FreeInfo(
  251. IN ASN1decoding_t pDec,
  252. IN ASN1uint32_t id,
  253. IN void *pvAsn1Info
  254. )
  255. {
  256. if (pvAsn1Info)
  257. ASN1_FreeDecoded(pDec, pvAsn1Info, id);
  258. }
  259. //+-------------------------------------------------------------------------
  260. // Encode an ASN1 formatted info structure.
  261. //
  262. // If CRYPT_ENCODE_ALLOC_FLAG is set, allocate memory for pbEncoded and
  263. // return *((BYTE **) pvEncoded) = pbAllocEncoded. Otherwise,
  264. // pvEncoded points to byte array to be updated.
  265. //--------------------------------------------------------------------------
  266. BOOL
  267. WINAPI
  268. PkiAsn1EncodeInfoEx(
  269. IN ASN1encoding_t pEnc,
  270. IN ASN1uint32_t id,
  271. IN void *pvAsn1Info,
  272. IN DWORD dwFlags,
  273. IN OPTIONAL PCRYPT_ENCODE_PARA pEncodePara,
  274. OUT OPTIONAL void *pvEncoded,
  275. IN OUT DWORD *pcbEncoded
  276. );
  277. typedef BOOL (WINAPI *PFN_PKI_ASN1_DECODE_EX_CALLBACK)(
  278. IN void *pvAsn1Info,
  279. IN DWORD dwFlags,
  280. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  281. OUT OPTIONAL void *pvStructInfo,
  282. IN OUT LONG *plRemainExtra
  283. );
  284. //+-------------------------------------------------------------------------
  285. // Call the callback to convert the ASN1 structure into the 'C' structure.
  286. // If CRYPT_DECODE_ALLOC_FLAG is set allocate memory for the 'C'
  287. // structure and call the callback initially to get the length and then
  288. // a second time to update the allocated 'C' structure.
  289. //
  290. // Allocated structure is returned:
  291. // *((void **) pvStructInfo) = pvAllocStructInfo
  292. //--------------------------------------------------------------------------
  293. BOOL
  294. WINAPI
  295. PkiAsn1AllocStructInfoEx(
  296. IN void *pvAsn1Info,
  297. IN DWORD dwFlags,
  298. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  299. IN PFN_PKI_ASN1_DECODE_EX_CALLBACK pfnDecodeExCallback,
  300. OUT OPTIONAL void *pvStructInfo,
  301. IN OUT DWORD *pcbStructInfo
  302. );
  303. //+-------------------------------------------------------------------------
  304. // Decode the ASN1 formatted info structure and call the callback
  305. // function to convert the ASN1 structure to the 'C' structure.
  306. //
  307. // If CRYPT_DECODE_ALLOC_FLAG is set allocate memory for the 'C'
  308. // structure and call the callback initially to get the length and then
  309. // a second time to update the allocated 'C' structure.
  310. //
  311. // Allocated structure is returned:
  312. // *((void **) pvStructInfo) = pvAllocStructInfo
  313. //--------------------------------------------------------------------------
  314. BOOL
  315. WINAPI
  316. PkiAsn1DecodeAndAllocInfoEx(
  317. IN ASN1decoding_t pDec,
  318. IN ASN1uint32_t id,
  319. IN const BYTE *pbEncoded,
  320. IN DWORD cbEncoded,
  321. IN DWORD dwFlags,
  322. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara,
  323. IN PFN_PKI_ASN1_DECODE_EX_CALLBACK pfnDecodeExCallback,
  324. OUT OPTIONAL void *pvStructInfo,
  325. IN OUT DWORD *pcbStructInfo
  326. );
  327. //+-------------------------------------------------------------------------
  328. // Convert the ascii string ("1.2.9999") to ASN1's Object Identifier
  329. // represented as an array of unsigned longs.
  330. //
  331. // Returns TRUE for a successful conversion.
  332. //--------------------------------------------------------------------------
  333. BOOL
  334. WINAPI
  335. PkiAsn1ToObjectIdentifier(
  336. IN LPCSTR pszObjId,
  337. IN OUT ASN1uint16_t *pCount,
  338. OUT ASN1uint32_t rgulValue[]
  339. );
  340. //+-------------------------------------------------------------------------
  341. // Convert from OSS's Object Identifier represented as an array of
  342. // unsigned longs to an ascii string ("1.2.9999").
  343. //
  344. // Returns TRUE for a successful conversion
  345. //--------------------------------------------------------------------------
  346. BOOL
  347. WINAPI
  348. PkiAsn1FromObjectIdentifier(
  349. IN ASN1uint16_t Count,
  350. IN ASN1uint32_t rgulValue[],
  351. OUT LPSTR pszObjId,
  352. IN OUT DWORD *pcbObjId
  353. );
  354. #ifdef __cplusplus
  355. } // Balance extern "C" above
  356. #endif
  357. #endif