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.

343 lines
14 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: spc.h
  8. //
  9. // Contents: Software Publishing Certificate (SPC) Prototypes and Definitions
  10. //
  11. // Defines a set of Win32 APIs specific to software publishing
  12. // for encoding and decoding X.509 v3 certificate extensions and
  13. // PKCS #7 signed message content and authenticated attributes.
  14. // Defines a PKCS #10 attribute containing X509 v3 extensions.
  15. //
  16. // Defines a set of Win32 APIs for signing and verifying files
  17. // used in software publishing. The APIs have file processing
  18. // callbacks to accommodate any type of file. Direct support is
  19. // provided for: Portable Executable (PE) image, Java class,
  20. // structured storage and raw files.
  21. //
  22. // APIs:
  23. // SpcGetSignedDataIndirect
  24. // SpcWriteSpcFile
  25. // SpcReadSpcFile
  26. // SpcWriteSpcToMemory
  27. // SpcReadSpcFromMemory
  28. // SpcSignPeImageFile
  29. // SpcVerifyPeImageFile
  30. // SpcSignJavaClassFile
  31. // SpcVerifyJavaClassFile
  32. // SpcSignStructuredStorageFile
  33. // SpcVerifyStructuredStorageFile
  34. // SpcSignRawFile
  35. // SpcVerifyRawFile
  36. // SpcSignCabFile
  37. // SpcVerifyCabFile
  38. // SpcSignFile
  39. // SpcVerifyFile
  40. //
  41. // History: 15-Apr-96 philh created
  42. //--------------------------------------------------------------------------
  43. #ifndef __SPC_H__
  44. #define __SPC_H__
  45. #include "wincrypt.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. //+-------------------------------------------------------------------------
  50. // SPC_SP_AGENCY_INFO_OBJID
  51. //
  52. // All the fields in the Image and Info structures are optional. When
  53. // omitted, a pointer is NULL or a blob's cbData is 0.
  54. //--------------------------------------------------------------------------
  55. //+-------------------------------------------------------------------------
  56. // SPC_MINIMAL_CRITERIA_OBJID
  57. //
  58. // Type of BOOL. Its set to TRUE if publisher meets minimal criteria.
  59. //--------------------------------------------------------------------------
  60. //+-------------------------------------------------------------------------
  61. // SPC_FINANCIAL_CRITERIA_OBJID
  62. //--------------------------------------------------------------------------
  63. //+=========================================================================
  64. //
  65. // SPC PKCS #7 Signed Message Content
  66. //
  67. //-=========================================================================
  68. //+-------------------------------------------------------------------------
  69. // SPC PKCS #7 IndirectData ContentType Object Identifier
  70. //--------------------------------------------------------------------------
  71. //+=========================================================================
  72. //
  73. // SPC Sign and Verify File APIs and Type Definitions
  74. //
  75. // Following file types are directly supported:
  76. // Portable Executable (PE) Image
  77. // Java Class
  78. // Structured Storage
  79. // Raw (signed data is stored outside of the file)
  80. //
  81. //-=========================================================================
  82. //+-------------------------------------------------------------------------
  83. // Callback to get and verify the software publisher's certificate.
  84. //
  85. // Passed the CertId of the signer (its Issuer and SerialNumber), a
  86. // handle to a cert store containing certs and CRLs copied from
  87. // the signed message, the indirect data content attribute extracted from
  88. // the signed data's indirect content,
  89. // flag indicating if computed digest of the file matched the digest in the
  90. // signed data's indirect content and the signer's authenticated attributes.
  91. //
  92. // If the file's signed data doesn't contain any content or signers, then,
  93. // called with pSignerId, pIndirectDataContentAttr and rgAuthnAttr == NULL.
  94. //
  95. // For a valid signer certificate, returns SPC_VERIFY_SUCCESS and a pointer
  96. // to a read only CERT_CONTEXT. The returned CERT_CONTEXT is either obtained
  97. // from a cert store or was created via CertStoreCreateCert. For either case,
  98. // its freed via CertStoreFreeCert.
  99. //
  100. // If this is the wrong signer or if a certificate wasn't found for the
  101. // signer, returns either
  102. // SPC_VERIFY_CONTINUE to continue on to the next signer or SPC_VERIFY_FAILED
  103. // to terminate the verification process.
  104. //
  105. // The NULL implementation tries to get the Signer certificate from the
  106. // signed data's cert store. It doesn't verify the certificate.
  107. //--------------------------------------------------------------------------
  108. typedef int (WINAPI *PFN_SPC_VERIFY_SIGNER_POLICY)(
  109. IN void *pvVerifyArg,
  110. IN DWORD dwCertEncodingType,
  111. IN OPTIONAL PCERT_INFO pSignerId, // Only the Issuer and
  112. // SerialNumber fields have
  113. // been updated
  114. IN HCERTSTORE hMsgCertStore,
  115. IN OPTIONAL PCRYPT_ATTRIBUTE_TYPE_VALUE pIndirectDataContentAttr,
  116. IN BOOL fDigestResult,
  117. IN DWORD cAuthnAttr,
  118. IN OPTIONAL PCRYPT_ATTRIBUTE rgAuthnAttr,
  119. IN DWORD cUnauthAttr,
  120. IN OPTIONAL PCRYPT_ATTRIBUTE rgUnauthAttr,
  121. IN DWORD cDigest,
  122. IN OPTIONAL PBYTE rgDigest,
  123. OUT PCCERT_CONTEXT *ppSignerCert
  124. );
  125. #define SPC_VERIFY_SUCCESS 0
  126. #define SPC_VERIFY_FAILED -1
  127. #define SPC_VERIFY_CONTINUE 1
  128. //+-------------------------------------------------------------------------
  129. // The SPC_SIGN_PARA are used for signing files used in software publishing.
  130. //
  131. // Either the CERT_KEY_PROV_HANDLE_PROP_ID or CERT_KEY_PROV_INFO_PROP_ID must
  132. // be set for pSigningCert. Either one specifies the private
  133. // signature key to use.
  134. //
  135. // If any certificates and/or CRLs are to be included in the file's signed
  136. // data, then, the MsgCert and MsgCrl fields need to be updated. If the
  137. // rgpSigningCerts are to be included, then, they must also be in the
  138. // rgpMsgCert array.
  139. //
  140. // If any authenticated attributes are to be included, then, the AuthnAttr
  141. // fields must be updated.
  142. //--------------------------------------------------------------------------
  143. typedef struct _SPC_SIGN_PARA {
  144. DWORD dwVersion;
  145. DWORD dwMsgAndCertEncodingType;
  146. PCCERT_CONTEXT pSigningCert;
  147. CRYPT_ALGORITHM_IDENTIFIER DigestAlgorithm;
  148. DWORD cMsgCert;
  149. PCCERT_CONTEXT *rgpMsgCert;
  150. DWORD cMsgCrl;
  151. PCCRL_CONTEXT *rgpMsgCrl;
  152. DWORD cAuthnAttr;
  153. PCRYPT_ATTRIBUTE rgAuthnAttr;
  154. DWORD cUnauthnAttr;
  155. PCRYPT_ATTRIBUTE rgUnauthnAttr;
  156. } SPC_SIGN_PARA, *PSPC_SIGN_PARA;
  157. //+-------------------------------------------------------------------------
  158. // The SCA_VERIFY_PARA are used to verify files signed for software
  159. // publishing.
  160. //
  161. // hCryptProv is used to do digesting and signature verification.
  162. //
  163. // hMsgCertStore is the store to copy certificates and CRLs from the message
  164. // to. If hMsgCertStore is NULL, then, a temporary store is created before
  165. // calling the VerifySignerPolicy callback.
  166. //
  167. // The dwMsgAndCertEncodingType specifies the encoding type of the certificates
  168. // and/or CRLs in the message.
  169. //
  170. // pfnVerifySignerPolicy is called to verify the message signer's certificate.
  171. //--------------------------------------------------------------------------
  172. typedef struct _SPC_VERIFY_PARA {
  173. DWORD dwVersion;
  174. DWORD dwMsgAndCertEncodingType;
  175. HCRYPTPROV hCryptProv;
  176. HCERTSTORE hMsgCertStore; // OPTIONAL
  177. PFN_SPC_VERIFY_SIGNER_POLICY pfnVerifySignerPolicy;
  178. void *pvVerifyArg;
  179. } SPC_VERIFY_PARA, *PSPC_VERIFY_PARA;
  180. //+-------------------------------------------------------------------------
  181. // Sign / Verify Flags
  182. //--------------------------------------------------------------------------
  183. #define SPC_LENGTH_ONLY_FLAG 0x00000001
  184. #define SPC_DISABLE_DIGEST_FILE_FLAG 0x00000002
  185. #define SPC_DISABLE_VERIFY_SIGNATURE_FLAG 0x00000004
  186. #define SPC_ADD_SIGNER_FLAG 0x00000100
  187. #define SPC_GET_SIGNATURE 0x00000200
  188. //+-------------------------------------------------------------------------
  189. // Put any certs/crl's into the store, and verify the SignedData's signature
  190. //--------------------------------------------------------------------------
  191. BOOL
  192. WINAPI
  193. SpcVerifySignedData(
  194. IN PSPC_VERIFY_PARA pVerifyPara,
  195. IN PBYTE pbSignedData,
  196. IN DWORD cbSignedData
  197. );
  198. //+-------------------------------------------------------------------------
  199. // Table of functions called to support the signing and verifying of files
  200. // used in software publishing. The functions read the portions of the
  201. // file to be digested, store the signed data or retrieve the signed data.
  202. //
  203. // pfnOpenSignFile is called with the pvSignFileArg passed to either
  204. // SpcSignFile() or SpcVerifyFile(). It returns a handle to be passed to the
  205. // other functions. pfnCloseSignFile is called to close the hSignFile.
  206. //
  207. // pfnDigestSignFile reads the portions of the file to be digested and
  208. // calls pfnDigestData to do the actual digesting.
  209. //
  210. // pfnSetSignedData stores the PKCS #7 Signed Data in the appropriate place
  211. // in the file. pfnGetSignedData retrieves the PKCS #7 Signed Data from the
  212. // file. pfnGetSignedData returns a pointer to its copy of the signed
  213. // data. Its not freed until pfnCloseSignFile is called.
  214. //--------------------------------------------------------------------------
  215. typedef void *HSPCDIGESTDATA;
  216. typedef BOOL (WINAPI *PFN_SPC_DIGEST_DATA)(
  217. IN HSPCDIGESTDATA hDigestData,
  218. IN const BYTE *pbData,
  219. IN DWORD cbData
  220. );
  221. typedef void *HSPCSIGNFILE;
  222. typedef HSPCSIGNFILE (WINAPI *PFN_SPC_OPEN_SIGN_FILE)(
  223. IN void *pvSignFileArg
  224. );
  225. typedef BOOL (WINAPI *PFN_SPC_CLOSE_SIGN_FILE)(
  226. IN HSPCSIGNFILE hSignFile
  227. );
  228. typedef BOOL (WINAPI *PFN_SPC_DIGEST_SIGN_FILE)(
  229. IN HSPCSIGNFILE hSignFile,
  230. IN DWORD dwMsgAndCertEncodingType,
  231. IN PCRYPT_ATTRIBUTE_TYPE_VALUE pIndirectDataContentAttr,
  232. IN PFN_SPC_DIGEST_DATA pfnDigestData,
  233. IN HSPCDIGESTDATA hDigestData
  234. );
  235. typedef BOOL (WINAPI *PFN_SPC_GET_SIGNED_DATA)(
  236. IN HSPCSIGNFILE hSignFile,
  237. OUT const BYTE **ppbSignedData,
  238. OUT DWORD *pcbSignedData
  239. );
  240. typedef BOOL (WINAPI *PFN_SPC_SET_SIGNED_DATA)(
  241. IN HSPCSIGNFILE hSignFile,
  242. IN const BYTE *pbSignedData,
  243. IN DWORD cbSignedData
  244. );
  245. typedef struct _SPC_SIGN_FILE_FUNC_TABLE {
  246. PFN_SPC_OPEN_SIGN_FILE pfnOpenSignFile;
  247. PFN_SPC_CLOSE_SIGN_FILE pfnCloseSignFile;
  248. PFN_SPC_DIGEST_SIGN_FILE pfnDigestSignFile;
  249. PFN_SPC_GET_SIGNED_DATA pfnGetSignedData;
  250. PFN_SPC_SET_SIGNED_DATA pfnSetSignedData;
  251. } SPC_SIGN_FILE_FUNC_TABLE, *PSPC_SIGN_FILE_FUNC_TABLE;
  252. typedef const SPC_SIGN_FILE_FUNC_TABLE *PCSPC_SIGN_FILE_FUNC_TABLE;
  253. //+-------------------------------------------------------------------------
  254. // Sign any type of file used for software publishing.
  255. //
  256. // The IndirectDataContentAttr indicates the type of file being digested
  257. // and signed. It may have an optional value, such as, a link to the file.
  258. // Its stored with the file's digest algorithm and digest in the
  259. // indirect data content of the signed data.
  260. //
  261. // The SPC_DISABLE_DIGEST_FLAG inhibits the digesting of the file.
  262. // The SPC_LENGTH_ONLY_FLAG implicitly sets the SPC_DISABLE_DIGEST_FLAG_FLAG
  263. // and only calculates a length for the signed data.
  264. //--------------------------------------------------------------------------
  265. BOOL
  266. WINAPI
  267. SpcSignFile(IN PSPC_SIGN_PARA pSignPara,
  268. IN PCSPC_SIGN_FILE_FUNC_TABLE pSignFileFuncTable,
  269. IN void *pvSignFileArg,
  270. IN PCRYPT_ATTRIBUTE_TYPE_VALUE pIndirectDataContentAttr,
  271. IN DWORD dwFlags,
  272. OUT PBYTE* pbEncoding,
  273. OUT DWORD* cbEncoding);
  274. //+-------------------------------------------------------------------------
  275. // Verify any type of file signed for software publishing.
  276. //
  277. // pVerifyPara's pfnVerifySignerPolicy is called to verify the signer's
  278. // certificate.
  279. //
  280. // For a verified signer and file, *ppSignerCert is updated
  281. // with the CertContext of the signer. It must be freed by calling
  282. // CertStoreFreeCert. Otherwise, *ppSignerCert is set to NULL.
  283. // For *pbcbDecoded == 0 on input, *ppSignerCert is always set to
  284. // NULL.
  285. //
  286. // ppSignerCert can be NULL, indicating the caller isn't interested
  287. // in getting the CertContext of the signer.
  288. //
  289. // If specified, the attribute type of the indirect data content in the
  290. // file's signed data is compared with pszDataAttrObjId.
  291. //
  292. // The SPC_DISABLE_DIGEST_FLAG inhibits the digesting of the file.
  293. // The SPC_DISABLE_VERIFY_SIGNATURE_FLAG inhibits the verification of the
  294. // the signed data in the file. The SPC_LENGTH_ONLY_FLAG isn't allowed and
  295. // returns an error.
  296. //--------------------------------------------------------------------------
  297. BOOL
  298. WINAPI
  299. SpcVerifyFile(
  300. IN PSPC_VERIFY_PARA pVerifyPara,
  301. IN PCSPC_SIGN_FILE_FUNC_TABLE pSignFileFuncTable,
  302. IN void *pvSignFileArg,
  303. IN OPTIONAL LPSTR pszDataAttrObjId,
  304. IN DWORD dwFlags,
  305. OUT OPTIONAL PCCERT_CONTEXT *ppSignerCert
  306. );
  307. //+-------------------------------------------------------------------------
  308. // SPC error codes
  309. //--------------------------------------------------------------------------
  310. #include "sgnerror.h"
  311. #ifdef __cplusplus
  312. } // Balance extern "C" above
  313. #endif
  314. #endif