MS.StrongNameNativeNativeMethods.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 namespace MS.StrongName.Native
5 {
6 /// <summary>
7 /// P/Invoke declarations for strong name APIs
8 /// </summary>
9 internal static class NativeMethods
10 {
11 /// <summary>
12 /// Return the last error
13 /// </summary>
14 /// <returns>error information for the last strong name call</returns>
15 [DllImport("mscoree.dll")]
16 public extern static int StrongNameErrorInfo();
17
18 /// <summary>
19 /// Free the buffer allocated by strong name functions
20 /// </summary>
21 /// <param name="pbMemory">address of memory to free</param>
22 [DllImport("mscoree.dll")]
23 public extern static void StrongNameFreeBuffer(IntPtr pbMemory);
24
25 /// <summary>
26 /// Retrieve the public portion of a key pair.
27 /// </summary>
28 /// <param name="wszKeyContainer">key container to extract from, null to create a temporary container</param>
29 /// <param name="pbKeyBlob">key blob to extract from, null to extract from a container</param>
30 /// <param name="cbKeyBlob">size in bytes of <paramref name="pbKeyBlob"/></param>
31 /// <param name="ppbPublicKeyBlob"> [out]public key blob</param>
32 /// <param name="pcbPublicKeyBlob"> [out]size of <paramref name="pcbPublicKeyBlob"/></param>
33 /// <returns>true on success, false on error</returns>
34 [DllImport("mscoree.dll")]
35 public extern static bool StrongNameGetPublicKey(
36 [MarshalAs(UnmanagedType.LPWStr)]string wszKeyContainer,
37 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]byte[] pbKeyBlob,
38 [MarshalAs(UnmanagedType.U4)]int cbKeyBlob,
39 [Out]out IntPtr ppbPublicKeyBlob,
40 [Out, MarshalAs(UnmanagedType.U4)]out int pcbPublicKeyBlob);
41
42 /// <summary>
43 /// Delete a key pair from a key container
44 /// </summary>
45 /// <param name="wszKeyContainer">key container name</param>
46 /// <returns>true on success, false on failure</returns>
47 [DllImport("mscoree.dll")]
48 public extern static bool StrongNameKeyDelete(
49 [MarshalAs(UnmanagedType.LPWStr)]string wszKeyContainer);
50
51 /// <summary>
52 /// Generate a new key pair with the specified key size for strong name use
53 /// </summary>
54 /// <param name="wszKeyContainer">desired key container name</param>
55 /// <param name="dwFlags">flags</param>
56 /// <param name="dwKeySize">desired key size</param>
57 /// <param name="ppbKeyBlob"> [out] generated public / private key blob</param>
58 /// <param name="pcbKeyBlob"> [out] size of the generated blob</param>
59 /// <returns>true if the key was generated, false if there was an error</returns>
60 [DllImport("mscoree.dll")]
61 public extern static bool StrongNameKeyGenEx(
62 [MarshalAs(UnmanagedType.LPWStr)]string wszKeyContainer,
63 StrongNameKeyGenFlags dwFlags,
64 int dwKeySize,
65 [Out]out IntPtr ppbKeyBlob,
66 [Out]out long pcbKeyBlob);
67
68 /// <summary>
69 /// Import a key pair into a key container
70 /// </summary>
71 /// <param name="wszKeyContainer">desired key container name</param>
72 /// <param name="pbKeyBlob">public/private key blob</param>
73 /// <param name="cbKeyBlob">number of bytes in <paramref name="pbKeyBlob"/></param>
74 /// <returns>true on success, false on error</returns>
75 [DllImport("mscoree.dll")]
76 public extern static bool StrongNameKeyInstall(
77 [MarshalAs(UnmanagedType.LPWStr)]string wszKeyContainer,
78 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]byte[]pbKeyBlob,
79 int cbKeyBlob);
80
81 /// <summary>
82 /// Verify a strong name/manifest against a public key blob
83 /// </summary>
84 /// <param name="wszFilePath">valid path to the PE file for the assembly</param>
85 /// <param name="fForceVerification">verify even if the settings in the registry disable it</param>
86 /// <param name="pfWasVerified"> [out] set to false if verify succeeded due to registry settings</param>
87 /// <returns>true if the assembly verified, false otherwise</returns>
88 [DllImport("mscoree.dll")]
89 public static extern bool StrongNameSignatureVerificationEx(
90 [MarshalAs(UnmanagedType.LPWStr)]string wszFilePath,
91 bool fForceVerification,
92 ref bool pfWasVerified);
93
94 /// <summary>
95 /// Create a strong name token from an assembly file, and addtionally
96 /// return the full public key blob
97 /// </summary>
98 /// <param name="wszFilePath">path to the PE file for the assembly</param>
99 /// <param name="ppbStrongNameToken"> [out]strong name token</param>
100 /// <param name="pcbStrongNameToken"> [out]length of <paramref name="ppbStrongNameToken"/></param>
101 /// <param name="ppbPublicKeyBlob"> [out]public key blob</param>
102 /// <param name="pcbPublicKeyBlob"> [out]length of <paramref name="ppbPublicKeyBlob"/></param>
103 /// <returns>true on success, false on error</returns>
104 [DllImport("mscoree.dll")]
105 public extern static bool StrongNameTokenFromAssemblyEx(
106 [MarshalAs(UnmanagedType.LPWStr)]string wszFilePath,
107 [Out]out IntPtr ppbStrongNameToken,
108 [Out, MarshalAs(UnmanagedType.U4)]out int pcbStrongNameToken,
109 [Out]out IntPtr ppbPublicKeyBlob,
110 [Out, MarshalAs(UnmanagedType.U4)]out int pcbPublicKeyBlob);
111
112 /// <summary>
113 /// Create a strong name token from a public key blob
114 /// </summary>
115 /// <param name="pbPublicKeyBlob">key blob to generate the token for</param>
116 /// <param name="cbPublicKeyBlob">number of bytes in <paramref name="pbPublicKeyBlob"/></param>
117 /// <param name="ppbStrongNameToken"> [out]public key token</param>
118 /// <param name="pcbStrongNameToken"> [out]number of bytes in the token</param>
119 /// <returns>true on success, false on error</returns>
120 [DllImport("mscoree.dll")]
121 public extern static bool StrongNameTokenFromPublicKey(
122 [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]byte[] pbPublicKeyBlob,
123 int cbPublicKeyBlob,
124 [Out]out IntPtr ppbStrongNameToken,
125 [Out, MarshalAs(UnmanagedType.U4)]out int pcbStrongNameToken);
126 }
127 }