home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / strongname.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  11.8 KB  |  239 lines

  1. #ifndef __STRONG_NAME_H
  2. #define __STRONG_NAME_H
  3.  
  4. // ===========================================================================
  5. // File: StrongName.h
  6. // 
  7. // Copyright (c) 1999 Microsoft Corporation.  All Rights Reserved.
  8. // Microsoft Confidential.
  9. //
  10. // Wrappers for signing and hashing functions needed to implement strong names
  11. // ===========================================================================
  12.  
  13.  
  14. #include <windows.h>
  15. #include <wincrypt.h>
  16. #include <ole2.h>
  17. #include <urterror.h>
  18.  
  19.  
  20. #ifdef __cplusplus
  21. extern "C"{
  22. #endif 
  23.  
  24.  
  25. // Public key blob binary format.
  26. typedef struct {
  27.     unsigned int SigAlgID;       // (ALG_ID) signature algorithm used to create the signature
  28.     unsigned int HashAlgID;      // (ALG_ID) hash algorithm used to create the signature
  29.     ULONG        cbPublicKey;    // length of the key in bytes
  30.     BYTE         PublicKey[1];   // variable length byte array containing the key value in format output by CryptoAPI
  31. } PublicKeyBlob;
  32.  
  33.  
  34. // Location in the registry (under HKLM) that strong name configuration info is
  35. // stored.
  36. #define SN_CONFIG_KEY               "Software\\Microsoft\\StrongName"
  37. #define SN_CONFIG_CSP               "CSP"                   // REG_SZ
  38. #define SN_CONFIG_MACHINE_KEYSET    "MachineKeyset"         // REG_DWORD
  39. #define SN_CONFIG_HASH_ALG          "HashAlgorithm"         // REG_DWORD
  40. #define SN_CONFIG_SIGN_ALG          "SignAlgorithm"         // REG_DWORD
  41. #define SN_CONFIG_VERIFICATION      "Verification"          // Registry subkey
  42. #define SN_CONFIG_DEV_KEY           "PublicKey"             // REG_BINARY
  43. #define SN_CONFIG_USERLIST          "UserList"              // REG_MULTI_SZ
  44.  
  45.  
  46. #ifdef SNAPI_INTERNAL
  47. #define SNAPI __declspec(dllexport) BOOLEAN __stdcall
  48. #define SNAPI_(_type) __declspec(dllexport) _type __stdcall
  49. #else
  50. #define SNAPI __declspec(dllimport) BOOLEAN __stdcall
  51. #define SNAPI_(_type) __declspec(dllimport) _type __stdcall
  52. #endif
  53.  
  54.  
  55. // Return last error.
  56. SNAPI_(DWORD) StrongNameErrorInfo(VOID);
  57.  
  58.  
  59. // Free buffer allocated by routines below.
  60. SNAPI_(VOID) StrongNameFreeBuffer(BYTE *pbMemory);  // [in] address of memory to free
  61.  
  62.  
  63. // Generate a new key pair for strong name use.
  64. SNAPI StrongNameKeyGen(LPCWSTR  wszKeyContainer,    // [in] desired key container name
  65.                        DWORD    dwFlags,            // [in] flags (see below)
  66.                        BYTE   **ppbKeyBlob,         // [out] public/private key blob
  67.                        ULONG   *pcbKeyBlob);
  68.  
  69. // Flags for StrongNameKeyGen.
  70. #define SN_LEAVE_KEY    0x00000001                  // Leave key pair registered with CSP
  71.  
  72.  
  73. // Import key pair into a key container.
  74. SNAPI StrongNameKeyInstall(LPCWSTR  wszKeyContainer,// [in] desired key container name, must be a non-empty string
  75.                            BYTE    *pbKeyBlob,      // [in] public/private key pair blob
  76.                            ULONG    cbKeyBlob);
  77.  
  78.  
  79. // Delete a key pair.
  80. SNAPI StrongNameKeyDelete(LPCWSTR wszKeyContainer); // [in] desired key container name
  81.  
  82.  
  83. // Retrieve the public portion of a key pair.
  84. SNAPI StrongNameGetPublicKey (LPCWSTR   wszKeyContainer,    // [in] desired key container name
  85.                               BYTE     *pbKeyBlob,          // [in] public/private key blob (optional)
  86.                               ULONG     cbKeyBlob,
  87.                               BYTE    **ppbPublicKeyBlob,   // [out] public key blob
  88.                               ULONG    *pcbPublicKeyBlob);
  89.  
  90.  
  91. // Hash and sign a manifest.
  92. SNAPI StrongNameSignatureGeneration(LPCWSTR     wszFilePath,        // [in] valid path to the PE file for the assembly
  93.                                     LPCWSTR     wszKeyContainer,    // [in] desired key container name
  94.                                     BYTE       *pbKeyBlob,          // [in] public/private key blob (optional)
  95.                                     ULONG       cbKeyBlob,
  96.                                     BYTE      **ppbSignatureBlob,   // [out] signature blob
  97.                                     ULONG      *pcbSignatureBlob);
  98.  
  99.  
  100. // Create a strong name token from an assembly file.
  101. SNAPI StrongNameTokenFromAssembly(LPCWSTR   wszFilePath,            // [in] valid path to the PE file for the assembly
  102.                                   BYTE    **ppbStrongNameToken,     // [out] strong name token 
  103.                                   ULONG    *pcbStrongNameToken);
  104.  
  105. // Create a strong name token from an assembly file and additionally return the full public key.
  106. SNAPI StrongNameTokenFromAssemblyEx(LPCWSTR   wszFilePath,            // [in] valid path to the PE file for the assembly
  107.                                     BYTE    **ppbStrongNameToken,     // [out] strong name token 
  108.                                     ULONG    *pcbStrongNameToken,
  109.                                     BYTE    **ppbPublicKeyBlob,       // [out] public key blob
  110.                                     ULONG    *pcbPublicKeyBlob);
  111.  
  112. // Create a strong name token from a public key blob.
  113. SNAPI StrongNameTokenFromPublicKey(BYTE    *pbPublicKeyBlob,        // [in] public key blob
  114.                                    ULONG    cbPublicKeyBlob,
  115.                                    BYTE   **ppbStrongNameToken,     // [out] strong name token 
  116.                                    ULONG   *pcbStrongNameToken);
  117.  
  118.  
  119. // Verify a strong name/manifest against a public key blob.
  120. SNAPI StrongNameSignatureVerification(LPCWSTR wszFilePath);         // [in] valid path to the PE file for the assembly
  121.  
  122.  
  123. // Verify a strong name/manifest against a public key blob.
  124. SNAPI StrongNameSignatureVerificationEx(LPCWSTR     wszFilePath,        // [in] valid path to the PE file for the assembly
  125.                                         BOOLEAN     fForceVerification, // [in] verify even if settings in the registry disable it
  126.                                         BOOLEAN    *pfWasVerified);     // [out] set to false if verify succeeded due to registry settings
  127.  
  128.  
  129. // Verify that two assemblies differ only by signature blob.
  130. SNAPI StrongNameCompareAssemblies(LPCWSTR   wszAssembly1,           // [in] file name of first assembly
  131.                                   LPCWSTR   wszAssembly2,           // [in] file name of second assembly
  132.                                   DWORD    *pdwResult);             // [out] result of comparison (see codes below)
  133.  
  134. #define SN_CMP_DIFFERENT    0   // Assemblies contain different data
  135. #define SN_CMP_IDENTICAL    1   // Assemblies are exactly the same, even signatures
  136. #define SN_CMP_SIGONLY      2   // Assemblies differ only by signature (and checksum etc.)
  137.  
  138.  
  139. // Compute the size of buffer needed to hold a hash for a given hash algorithm.
  140. SNAPI StrongNameHashSize(ULONG  ulHashAlg,  // [in] hash algorithm
  141.                          DWORD *pcbSize);   // [out] size of the hash in bytes
  142.  
  143.  
  144. // Compute the size that needs to be allocated for a signature in an assembly.
  145. SNAPI StrongNameSignatureSize(BYTE    *pbPublicKeyBlob,    // [in] public key blob
  146.                               ULONG    cbPublicKeyBlob,
  147.                               DWORD   *pcbSize);           // [out] size of the signature in bytes
  148.  
  149.  
  150. // COM version of the strong name interface.
  151.  
  152. #ifndef DECLSPEC_SELECT_ANY
  153. #define DECLSPEC_SELECT_ANY __declspec(selectany)
  154. #endif
  155.  
  156. #undef INTERFACE
  157. #define INTERFACE IStrongName
  158.  
  159. // IID_IStrongName
  160. // {a09c68f1-d6ab-4da3-a5ce-991f1e0d0109}
  161. extern const GUID DECLSPEC_SELECT_ANY IID_IStrongName =
  162. { 0xa09c68f1, 0xd6ab, 0x4da3, { 0xa5, 0xce, 0x99, 0x1f, 0x1e, 0x0d, 0x01, 0x09 } };
  163.  
  164. DECLARE_INTERFACE_(IStrongName, IUnknown)
  165. {
  166.     STDMETHOD_(VOID, FreeBuffer)(BYTE *pbMemory) PURE;
  167.     STDMETHOD(GenerateKey)(LPCWSTR  wszKeyContainer,
  168.                            DWORD    dwFlags,
  169.                            BYTE   **ppbKeyBlob,
  170.                            ULONG   *pcbKeyBlob) PURE;
  171.     STDMETHOD(InstallKey)(LPCWSTR  wszKeyContainer,
  172.                           BYTE    *pbKeyBlob,
  173.                           ULONG    cbKeyBlob) PURE;
  174.     STDMETHOD(DeleteKey)(LPCWSTR wszKeyContainer) PURE;
  175.     STDMETHOD(GetPublicKey)(LPCWSTR   wszKeyContainer,
  176.                             BYTE     *pbKeyBlob,
  177.                             ULONG     cbKeyBlob,
  178.                             BYTE    **ppbPublicKeyBlob,
  179.                             ULONG    *pcbPublicKeyBlob) PURE;
  180.     STDMETHOD(GenerateSignature)(LPCWSTR     wszFilePath,
  181.                                  LPCWSTR     wszKeyContainer,
  182.                                  BYTE       *pbKeyBlob,
  183.                                  ULONG       cbKeyBlob,
  184.                                  BYTE      **ppbSignatureBlob,
  185.                                  ULONG      *pcbSignatureBlob) PURE;
  186.     STDMETHOD(GetTokenFromAssembly)(LPCWSTR   wszFilePath,
  187.                                     BYTE    **ppbStrongNameToken,
  188.                                     ULONG    *pcbStrongNameToken,
  189.                                     BYTE    **ppbPublicKeyBlob,
  190.                                     ULONG    *pcbPublicKeyBlob) PURE;
  191.     STDMETHOD(GetTokenFromPublicKey)(BYTE    *pbPublicKeyBlob,
  192.                                      ULONG    cbPublicKeyBlob,
  193.                                      BYTE   **ppbStrongNameToken,
  194.                                      ULONG   *pcbStrongNameToken) PURE;
  195.     STDMETHOD(VerifySignature)(LPCWSTR  wszFilePath,
  196.                                BOOLEAN  fForceVerification,
  197.                                BOOLEAN *pfWasVerified) PURE;
  198.     STDMETHOD(CompareAssemblies)(LPCWSTR   wszAssembly1,
  199.                                  LPCWSTR   wszAssembly2,
  200.                                  DWORD    *pdwResult) PURE;
  201.     STDMETHOD(GetHashSize)(ULONG  ulHashAlg,
  202.                            DWORD *pcbSize) PURE;
  203.     STDMETHOD(GetSignatureSize)(BYTE  *pbPublicKeyBlob,
  204.                                 ULONG  cbPublicKeyBlob,
  205.                                 DWORD *pcbSize) PURE;
  206. };
  207.  
  208.  
  209. SNAPI_(DWORD) GetHashFromAssemblyFile(LPCSTR szFilePath, // [IN] location of file to be hashed
  210.                                       unsigned int *piHashAlg, // [IN/OUT] constant specifying the hash algorithm (set to 0 if you want the default)
  211.                                       BYTE   *pbHash,    // [OUT] hash buffer
  212.                                       DWORD  cchHash,    // [IN]  max size of buffer
  213.                                       DWORD  *pchHash);  // [OUT] length of hash byte array
  214.     
  215. SNAPI_(DWORD) GetHashFromFile(LPCSTR szFilePath, // [IN] location of file to be hashed
  216.                               unsigned int *piHashAlg,   // [IN/OUT] constant specifying the hash algorithm (set to 0 if you want the default)
  217.                               BYTE   *pbHash,    // [OUT] hash buffer
  218.                               DWORD  cchHash,    // [IN]  max size of buffer
  219.                               DWORD  *pchHash);  // [OUT] length of hash byte array
  220.     
  221. SNAPI_(DWORD) GetHashFromHandle(HANDLE hFile,      // [IN] handle of file to be hashed
  222.                                 unsigned int *piHashAlg,   // [IN/OUT] constant specifying the hash algorithm (set to 0 if you want the default)
  223.                                 BYTE   *pbHash,    // [OUT] hash buffer
  224.                                 DWORD  cchHash,    // [IN]  max size of buffer
  225.                                 DWORD  *pchHash);  // [OUT] length of hash byte array
  226.  
  227. SNAPI_(DWORD) GetHashFromBlob(BYTE   *pbBlob,       // [IN] pointer to memory block to hash
  228.                               DWORD  cchBlob,       // [IN] length of blob
  229.                               unsigned int *piHashAlg,  // [IN/OUT] constant specifying the hash algorithm (set to 0 if you want the default)
  230.                               BYTE   *pbHash,       // [OUT] hash buffer
  231.                               DWORD  cchHash,       // [IN]  max size of buffer
  232.                               DWORD  *pchHash);     // [OUT] length of hash byte array
  233.  
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237.  
  238. #endif
  239.