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.

264 lines
9.4 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: asn1util.h
  7. //
  8. // Contents: ASN.1 utility functions.
  9. //
  10. // APIs:
  11. // Asn1UtilDecodeLength
  12. // Asn1UtilExtractContent
  13. // Asn1UtilIsPKCS7WithoutContentType
  14. // Asn1UtilAdjustEncodedLength
  15. // Asn1UtilExtractValues
  16. // Asn1UtilExtractPKCS7SignedDataContent
  17. // Asn1UtilExtractCertificateToBeSignedContent
  18. // Asn1UtilExtractCertificatePublicKeyInfo
  19. // Asn1UtilExtractKeyIdFromCertInfo
  20. //
  21. // History: 06-Dec-96 philh created from kevinr's wincrmsg version
  22. //--------------------------------------------------------------------------
  23. #ifndef __ASN1UTIL_H__
  24. #define __ASN1UTIL_H__
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #define ASN1UTIL_INSUFFICIENT_DATA -2
  29. //+-------------------------------------------------------------------------
  30. // ASN.1 Tag Defines
  31. //--------------------------------------------------------------------------
  32. #define ASN1UTIL_TAG_NULL 0x00
  33. #define ASN1UTIL_TAG_BOOLEAN 0x01
  34. #define ASN1UTIL_TAG_INTEGER 0x02
  35. #define ASN1UTIL_TAG_BITSTRING 0x03
  36. #define ASN1UTIL_TAG_OCTETSTRING 0x04
  37. #define ASN1UTIL_TAG_OID 0x06
  38. #define ASN1UTIL_TAG_UTC_TIME 0x17
  39. #define ASN1UTIL_TAG_GENERALIZED_TIME 0x18
  40. #define ASN1UTIL_TAG_CONSTRUCTED 0x20
  41. #define ASN1UTIL_TAG_SEQ 0x30
  42. #define ASN1UTIL_TAG_SET 0x31
  43. #define ASN1UTIL_TAG_CONTEXT_0 0x80
  44. #define ASN1UTIL_TAG_CONTEXT_1 0x81
  45. #define ASN1UTIL_TAG_CONSTRUCTED_CONTEXT_0 \
  46. (ASN1UTIL_TAG_CONSTRUCTED | ASN1UTIL_TAG_CONTEXT_0)
  47. #define ASN1UTIL_TAG_CONSTRUCTED_CONTEXT_1 \
  48. (ASN1UTIL_TAG_CONSTRUCTED | ASN1UTIL_TAG_CONTEXT_1)
  49. //+-------------------------------------------------------------------------
  50. // ASN.1 Length Defines for indefinite length encooding
  51. //--------------------------------------------------------------------------
  52. #define ASN1UTIL_LENGTH_INDEFINITE 0x80
  53. #define ASN1UTIL_LENGTH_NULL 0x00
  54. //+-------------------------------------------------------------------------
  55. // Get the number of contents octets in a definite-length BER-encoding.
  56. //
  57. // Parameters:
  58. // pcbContent - receives the number of contents octets
  59. // pbLength - points to the first length octet
  60. // cbDER - number of bytes remaining in the DER encoding
  61. //
  62. // Returns:
  63. // success - the number of bytes in the length field, >=0
  64. // failure - <0
  65. //--------------------------------------------------------------------------
  66. LONG
  67. WINAPI
  68. Asn1UtilDecodeLength(
  69. OUT DWORD *pcbContent,
  70. IN const BYTE *pbLength,
  71. IN DWORD cbDER);
  72. //+-------------------------------------------------------------------------
  73. // Point to the content octets in a DER-encoded blob.
  74. //
  75. // Returns:
  76. // success - the number of bytes skipped, >=0
  77. // failure - <0
  78. //
  79. // Assumption: pbData points to a definite-length BER-encoded blob.
  80. //--------------------------------------------------------------------------
  81. LONG
  82. WINAPI
  83. Asn1UtilExtractContent(
  84. IN const BYTE *pbDER,
  85. IN DWORD cbDER,
  86. OUT DWORD *pcbContent,
  87. OUT const BYTE **ppbContent);
  88. //+-------------------------------------------------------------------------
  89. // Returns TRUE if we believe this is a Bob special that has ommitted the
  90. // PKCS #7 ContentType.
  91. //
  92. // For PKCS #7: an Object Identifier tag (0x06) immediately follows the
  93. // identifier and length octets. For a Bob special: an integer tag (0x02)
  94. // follows the identifier and length octets.
  95. //--------------------------------------------------------------------------
  96. BOOL
  97. WINAPI
  98. Asn1UtilIsPKCS7WithoutContentType(
  99. IN const BYTE *pbDER,
  100. IN DWORD cbDER);
  101. //+-------------------------------------------------------------------------
  102. // Decode the Asn1 length bytes to possibly downward adjust the length.
  103. //
  104. // The returned length is always <= cbDER.
  105. //--------------------------------------------------------------------------
  106. DWORD
  107. WINAPI
  108. Asn1UtilAdjustEncodedLength(
  109. IN const BYTE *pbDER,
  110. IN DWORD cbDER
  111. );
  112. typedef struct _ASN1UTIL_EXTRACT_VALUE_PARA {
  113. // See below for list of operations and optional return blobs.
  114. DWORD dwFlags;
  115. // The following 0 terminated array of tags is optional. If ommited, the
  116. // value may contain any tag. Note, for OPTIONAL_STEP_OVER, not optional.
  117. const BYTE *rgbTag;
  118. } ASN1UTIL_EXTRACT_VALUE_PARA, *PASN1UTIL_EXTRACT_VALUE_PARA;
  119. // The lower 8 bits of dwFlags is set to one of the following operations
  120. #define ASN1UTIL_MASK_VALUE_OP 0xFF
  121. #define ASN1UTIL_STEP_INTO_VALUE_OP 1
  122. #define ASN1UTIL_STEP_OVER_VALUE_OP 2
  123. #define ASN1UTIL_OPTIONAL_STEP_OVER_VALUE_OP 3
  124. #define ASN1UTIL_RETURN_VALUE_BLOB_FLAG 0x80000000
  125. #define ASN1UTIL_RETURN_CONTENT_BLOB_FLAG 0x40000000
  126. //+-------------------------------------------------------------------------
  127. // Extract one or more tagged values from the ASN.1 encoded byte array.
  128. //
  129. // Either steps into the value's content octets (ASN1UTIL_STEP_INTO_VALUE_OP)
  130. // or steps over the value's tag, length and content octets
  131. // (ASN1UTIL_STEP_OVER_VALUE_OP or ASN1UTIL_OPTIONAL_STEP_OVER_VALUE_OP).
  132. //
  133. // For tag matching, only supports single byte tags. STEP_OVER values
  134. // must be definite-length encoded.
  135. //
  136. // *pcValue is updated with the number of values successfully extracted.
  137. //
  138. // Returns:
  139. // success - >= 0 => length of all values successfully extracted. For
  140. // STEP_INTO, only the tag and length octets.
  141. // failure - < 0 => negative (offset + 1) of first bad tagged value
  142. // LastError is updated with the error.
  143. //
  144. // A non-NULL rgValueBlob[] is updated with the pointer to and length of the
  145. // tagged value or its content octets. For OPTIONAL_STEP_OVER, if tag isn't
  146. // found, pbData and cbData are set to 0. If a STEP_INTO value is
  147. // indefinite-length encoded, cbData is set to CMSG_INDEFINITE_LENGTH.
  148. // If ASN1UTIL_DEFINITE_LENGTH_FLAG is set, then, all returned lengths
  149. // are definite-length, ie, CMSG_INDEFINITE_LENGTH is never returned.
  150. //
  151. // If ASN1UTIL_RETURN_VALUE_BLOB_FLAG is set, pbData points to
  152. // the tag. cbData includes the tag, length and content octets.
  153. //
  154. // If ASN1UTIL_RETURN_CONTENT_BLOB_FLAG is set, pbData points to the content
  155. // octets. cbData includes only the content octets.
  156. //
  157. // If neither BLOB_FLAG is set, rgValueBlob[] isn't updated.
  158. //--------------------------------------------------------------------------
  159. LONG
  160. WINAPI
  161. Asn1UtilExtractValues(
  162. IN const BYTE *pbEncoded,
  163. IN DWORD cbEncoded,
  164. IN DWORD dwFlags,
  165. IN OUT DWORD *pcValue,
  166. IN const ASN1UTIL_EXTRACT_VALUE_PARA *rgValuePara,
  167. OUT OPTIONAL PCRYPT_DER_BLOB rgValueBlob
  168. );
  169. #define ASN1UTIL_DEFINITE_LENGTH_FLAG 0x1
  170. //+-------------------------------------------------------------------------
  171. // Skips past PKCS7 ASN.1 encoded values to get to the SignedData content.
  172. //
  173. // Checks that the outer ContentType has the SignedData OID and optionally
  174. // checks the inner SignedData content's ContentType.
  175. //
  176. // Returns:
  177. // success - the number of bytes skipped, >=0
  178. // failure - <0
  179. //
  180. // If the SignedData content is indefinite-length encoded,
  181. // *pcbContent is set to CMSG_INDEFINITE_LENGTH
  182. //--------------------------------------------------------------------------
  183. LONG
  184. WINAPI
  185. Asn1UtilExtractPKCS7SignedDataContent(
  186. IN const BYTE *pbEncoded,
  187. IN DWORD cbEncoded,
  188. IN OPTIONAL const CRYPT_DER_BLOB *pEncodedInnerOID,
  189. OUT DWORD *pcbContent,
  190. OUT const BYTE **ppbContent
  191. );
  192. //+-------------------------------------------------------------------------
  193. // Verifies this is a certificate ASN.1 encoded signed content.
  194. // Returns the pointer to and length of the ToBeSigned content.
  195. //
  196. // Returns an error if the ToBeSigned content isn't definite length
  197. // encoded.
  198. //--------------------------------------------------------------------------
  199. BOOL
  200. WINAPI
  201. Asn1UtilExtractCertificateToBeSignedContent(
  202. IN const BYTE *pbEncoded,
  203. IN DWORD cbEncoded,
  204. OUT DWORD *pcbContent,
  205. OUT const BYTE **ppbContent
  206. );
  207. //+-------------------------------------------------------------------------
  208. // Returns the pointer to and length of the SubjectPublicKeyInfo value in
  209. // a signed and encoded X.509 certificate.
  210. //
  211. // Returns an error if the value isn't definite length encoded.
  212. //--------------------------------------------------------------------------
  213. BOOL
  214. WINAPI
  215. Asn1UtilExtractCertificatePublicKeyInfo(
  216. IN const BYTE *pbEncoded,
  217. IN DWORD cbEncoded,
  218. OUT DWORD *pcbPublicKeyInfo,
  219. OUT const BYTE **ppbPublicKeyInfo
  220. );
  221. //+-------------------------------------------------------------------------
  222. // If the Issuer and SerialNumber in the CERT_INFO contains a special
  223. // KeyID RDN attribute returns TRUE with pKeyId's cbData and pbData updated
  224. // with the RDN attribute's OCTET_STRING value. Otherwise, returns FALSE.
  225. //--------------------------------------------------------------------------
  226. BOOL
  227. WINAPI
  228. Asn1UtilExtractKeyIdFromCertInfo(
  229. IN PCERT_INFO pCertInfo,
  230. OUT PCRYPT_HASH_BLOB pKeyId
  231. );
  232. #ifdef __cplusplus
  233. } // Balance extern "C" above
  234. #endif
  235. #endif