Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Web / InternalApis / NDP_Common / inc / StrongNameHelpers.cs
1 using System;
2 using System.Security;
3 using System.Text;
4 using System.Runtime.CompilerServices;
5 using System.Runtime.InteropServices;
6 using System.Diagnostics.CodeAnalysis;
7
8 namespace Microsoft.Runtime.Hosting {
9
10     /// <summary>
11     /// The methods here are designed to aid in transition from the v2 StrongName APIs on mscoree.dll to the
12     /// v4 metahost APIs (which are in-proc SxS aware).
13     /// </summary>
14     internal static class StrongNameHelpers {
15
16         [ThreadStatic]
17         private static int ts_LastStrongNameHR;
18
19         [System.Security.SecurityCritical]
20         [ThreadStatic]
21         private static IClrStrongName s_StrongName;
22
23         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
24         private static IClrStrongName StrongName {
25             [System.Security.SecurityCritical]
26             get {
27                 if (s_StrongName == null) {
28                     s_StrongName = (IClrStrongName)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
29                         new Guid("B79B0ACD-F5CD-409b-B5A5-A16244610B92"), 
30                         new Guid("9FD93CCF-3280-4391-B3A9-96E1CDE77C8D"));
31                 }
32                 return s_StrongName;
33             }
34         }
35
36         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
37         private static IClrStrongNameUsingIntPtr StrongNameUsingIntPtr {
38             [System.Security.SecurityCritical]
39             get {
40                 return (IClrStrongNameUsingIntPtr)StrongName;
41             }
42         }
43
44         [System.Security.SecurityCritical]
45         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
46         public static int StrongNameErrorInfo() {
47             return ts_LastStrongNameHR;
48         }
49
50         [System.Security.SecurityCritical]
51         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
52         [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "Microsoft.Runtime.Hosting.IClrStrongNameUsingIntPtr.StrongNameFreeBuffer(System.IntPtr)", Justification = "StrongNameFreeBuffer returns void but the new runtime wrappers return an HRESULT.")]
53         public static void StrongNameFreeBuffer(IntPtr pbMemory) {
54             StrongNameUsingIntPtr.StrongNameFreeBuffer(pbMemory);
55         }
56
57         [System.Security.SecurityCritical]
58         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
59         public static bool StrongNameGetPublicKey(string pwzKeyContainer, IntPtr pbKeyBlob, int cbKeyBlob, out IntPtr ppbPublicKeyBlob, out int pcbPublicKeyBlob) {
60             int hr = StrongNameUsingIntPtr.StrongNameGetPublicKey(pwzKeyContainer, pbKeyBlob, cbKeyBlob, out ppbPublicKeyBlob, out pcbPublicKeyBlob);
61             if( hr < 0 )
62             {
63                 ts_LastStrongNameHR = hr;
64                 ppbPublicKeyBlob = IntPtr.Zero;
65                 pcbPublicKeyBlob = 0;
66                 return false;
67             }
68             return true;
69         }
70
71
72         [System.Security.SecurityCritical]
73         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
74         public static bool StrongNameKeyDelete(string pwzKeyContainer) {
75             int hr = StrongName.StrongNameKeyDelete(pwzKeyContainer);
76             if( hr < 0 )
77             {
78                 ts_LastStrongNameHR = hr;
79                 return false;
80             }
81             return true;
82         }
83
84         [System.Security.SecurityCritical]
85         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
86         public static bool StrongNameKeyGen(string pwzKeyContainer, int dwFlags, out IntPtr ppbKeyBlob, out int pcbKeyBlob) {
87             int hr = StrongName.StrongNameKeyGen(pwzKeyContainer, dwFlags, out ppbKeyBlob, out pcbKeyBlob);
88             if( hr < 0 )
89             {
90                 ts_LastStrongNameHR = hr;
91                 ppbKeyBlob = IntPtr.Zero;
92                 pcbKeyBlob = 0;
93                 return false;
94             }
95             return true;
96         }
97
98         [System.Security.SecurityCritical]
99         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
100         public static bool StrongNameKeyInstall(string pwzKeyContainer, IntPtr pbKeyBlob, int cbKeyBlob) {
101             int hr = StrongNameUsingIntPtr.StrongNameKeyInstall(pwzKeyContainer, pbKeyBlob, cbKeyBlob);
102             if( hr < 0 )
103             {
104                 ts_LastStrongNameHR = hr;
105                 return false;
106             }
107             return true;
108         }
109
110         [System.Security.SecurityCritical]
111         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
112         public static bool StrongNameSignatureGeneration(string pwzFilePath, string pwzKeyContainer, IntPtr pbKeyBlob, int cbKeyBlob) {
113             IntPtr ppbSignatureBlob = IntPtr.Zero;
114             int cbSignatureBlob = 0;
115             return StrongNameSignatureGeneration(pwzFilePath, pwzKeyContainer, pbKeyBlob, cbKeyBlob, ref ppbSignatureBlob, out cbSignatureBlob);
116         }
117
118
119         [System.Security.SecurityCritical]
120         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
121         public static bool StrongNameSignatureGeneration(string pwzFilePath, string pwzKeyContainer, IntPtr pbKeyBlob, int cbKeyBlob, ref IntPtr ppbSignatureBlob, out int pcbSignatureBlob) {
122             int hr = StrongNameUsingIntPtr.StrongNameSignatureGeneration(pwzFilePath, pwzKeyContainer, pbKeyBlob, cbKeyBlob, ppbSignatureBlob, out pcbSignatureBlob);
123             if( hr < 0 )
124             {
125                 ts_LastStrongNameHR = hr;
126                 pcbSignatureBlob = 0;
127                 return false;
128             }
129             return true;
130         }
131
132         [System.Security.SecurityCritical]
133         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
134         public static bool StrongNameSignatureSize(IntPtr pbPublicKeyBlob, int cbPublicKeyBlob, out int pcbSize) {
135             int hr = StrongNameUsingIntPtr.StrongNameSignatureSize(pbPublicKeyBlob, cbPublicKeyBlob, out pcbSize);
136             if( hr < 0 )
137             {
138                 ts_LastStrongNameHR = hr;
139                 pcbSize = 0;
140                 return false;
141             }
142             return true;
143         }
144
145         [System.Security.SecurityCritical]
146         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
147         public static bool StrongNameSignatureVerification(string pwzFilePath, int dwInFlags, out int pdwOutFlags) {
148             int hr = StrongName.StrongNameSignatureVerification(pwzFilePath, dwInFlags, out pdwOutFlags);
149             if( hr < 0 )
150             {
151                 ts_LastStrongNameHR = hr;
152                 pdwOutFlags = 0;
153                 return false;
154             }
155             return true; 
156         }
157
158         [System.Security.SecurityCritical]
159         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
160         public static bool StrongNameSignatureVerificationEx(string pwzFilePath, bool fForceVerification, out bool pfWasVerified) {
161             int hr = StrongName.StrongNameSignatureVerificationEx(pwzFilePath, fForceVerification, out pfWasVerified);
162             if( hr < 0 )
163             {
164                 ts_LastStrongNameHR = hr;
165                 pfWasVerified = false;
166                 return false;
167             }
168             return true;
169         }
170
171         [System.Security.SecurityCritical]
172         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
173         public static bool StrongNameTokenFromPublicKey(IntPtr pbPublicKeyBlob, int cbPublicKeyBlob, out IntPtr ppbStrongNameToken, out int pcbStrongNameToken) {
174             int hr = StrongNameUsingIntPtr.StrongNameTokenFromPublicKey(pbPublicKeyBlob, cbPublicKeyBlob, out ppbStrongNameToken, out pcbStrongNameToken);
175             if( hr < 0 )
176             {
177                 ts_LastStrongNameHR = hr;
178                 ppbStrongNameToken = IntPtr.Zero;
179                 pcbStrongNameToken = 0;
180                 return false;
181             }
182             return true;
183         }
184
185         [System.Security.SecurityCritical]
186         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
187         public static bool StrongNameSignatureSize(byte[] bPublicKeyBlob, int cbPublicKeyBlob, out int pcbSize) {
188             int hr = StrongName.StrongNameSignatureSize(bPublicKeyBlob, cbPublicKeyBlob, out pcbSize);
189             if( hr < 0 )
190             {
191                 ts_LastStrongNameHR = hr;
192                 pcbSize = 0;
193                 return false;
194             }
195             return true;
196         }
197         [System.Security.SecurityCritical]
198         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
199         public static bool StrongNameTokenFromPublicKey(byte[] bPublicKeyBlob, int cbPublicKeyBlob, out IntPtr ppbStrongNameToken, out int pcbStrongNameToken) {
200             int hr = StrongName.StrongNameTokenFromPublicKey(bPublicKeyBlob, cbPublicKeyBlob, out ppbStrongNameToken, out pcbStrongNameToken);
201             if( hr < 0 )
202             {
203                 ts_LastStrongNameHR = hr;
204                 ppbStrongNameToken = IntPtr.Zero;
205                 pcbStrongNameToken = 0;
206                 return false;
207             }
208             return true;
209         }
210
211         [System.Security.SecurityCritical]
212         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
213         public static bool StrongNameGetPublicKey(string pwzKeyContainer, byte[] bKeyBlob, int cbKeyBlob, out IntPtr ppbPublicKeyBlob, out int pcbPublicKeyBlob) {
214             int hr = StrongName.StrongNameGetPublicKey(pwzKeyContainer, bKeyBlob, cbKeyBlob, out ppbPublicKeyBlob, out pcbPublicKeyBlob);
215             if( hr < 0 )
216             {
217                 ts_LastStrongNameHR = hr;
218                 ppbPublicKeyBlob = IntPtr.Zero;
219                 pcbPublicKeyBlob = 0;
220                 return false;
221             }
222             return true;
223         }
224
225         [System.Security.SecurityCritical]
226         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
227         public static bool StrongNameKeyInstall(string pwzKeyContainer, byte[] bKeyBlob, int cbKeyBlob) {
228             int hr = StrongName.StrongNameKeyInstall(pwzKeyContainer, bKeyBlob, cbKeyBlob);
229             if( hr < 0 )
230             {
231                 ts_LastStrongNameHR = hr;
232                 return false;
233             }
234             return true;
235         }
236
237         [System.Security.SecurityCritical]
238         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
239         public static bool StrongNameSignatureGeneration(string pwzFilePath, string pwzKeyContainer, byte[] bKeyBlob, int cbKeyBlob) {
240             IntPtr ppbSignatureBlob = IntPtr.Zero;
241             int cbSignatureBlob = 0;
242             return StrongNameSignatureGeneration(pwzFilePath, pwzKeyContainer, bKeyBlob, cbKeyBlob, ref ppbSignatureBlob, out cbSignatureBlob);
243         }
244
245         [System.Security.SecurityCritical]
246         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Microsoft: This file is included in a lot of projects some of which only use a subset of the functions.")]
247         public static bool StrongNameSignatureGeneration(string pwzFilePath, string pwzKeyContainer, byte[] bKeyBlob, int cbKeyBlob, ref IntPtr ppbSignatureBlob, out int pcbSignatureBlob) {
248             int hr = StrongName.StrongNameSignatureGeneration(pwzFilePath, pwzKeyContainer, bKeyBlob, cbKeyBlob, ppbSignatureBlob, out pcbSignatureBlob);
249             if( hr < 0 )
250             {
251                 ts_LastStrongNameHR = hr;
252                 pcbSignatureBlob = 0;
253                 return false;
254             }
255             return true;
256         }
257
258     }
259
260     /// <summary>
261     /// This is a managed wrapper for the IClrStrongName interface defined in metahost.idl
262     /// This uses IntPtrs in some places where you'd normally expect a byte[] in order to
263     /// be compatible with callers who wrote their PInvoke signatures that way.
264     /// Ideally we'd probably just simplify all such callers to using byte[] and remove this
265     /// version of the interface.
266     /// </summary>
267     [System.Security.SecurityCritical]
268     [ComImport, ComConversionLoss, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("9FD93CCF-3280-4391-B3A9-96E1CDE77C8D")]
269     internal interface IClrStrongNameUsingIntPtr {
270         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
271         [PreserveSig]
272         int GetHashFromAssemblyFile(
273             [In, MarshalAs(UnmanagedType.LPStr)] string pszFilePath,
274             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
275             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
276             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
277             [MarshalAs(UnmanagedType.U4)] out int pchHash);
278
279         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
280         [PreserveSig]
281         int GetHashFromAssemblyFileW(
282             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
283             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
284             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
285             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
286             [MarshalAs(UnmanagedType.U4)] out int pchHash);
287
288         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
289         [PreserveSig]
290         int GetHashFromBlob(
291             [In] IntPtr pbBlob,
292             [In, MarshalAs(UnmanagedType.U4)] int cchBlob,
293             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
294             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)] byte[] pbHash,
295             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
296             [MarshalAs(UnmanagedType.U4)] out int pchHash);
297
298         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
299         [PreserveSig]
300         int GetHashFromFile(
301             [In, MarshalAs(UnmanagedType.LPStr)] string pszFilePath,
302             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
303             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
304             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
305             [MarshalAs(UnmanagedType.U4)] out int pchHash);
306
307         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
308         [PreserveSig]
309         int GetHashFromFileW(
310             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
311             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
312             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
313             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
314             [MarshalAs(UnmanagedType.U4)] out int pchHash);
315
316         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
317         [PreserveSig]
318         int GetHashFromHandle(
319             [In] IntPtr hFile,
320             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
321             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
322             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
323             [MarshalAs(UnmanagedType.U4)] out int pchHash);
324
325         [return: MarshalAs(UnmanagedType.U4)]
326         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
327         [PreserveSig]
328         int StrongNameCompareAssemblies(
329             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzAssembly1,
330             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzAssembly2,
331             [MarshalAs(UnmanagedType.U4)] out int dwResult);
332
333         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
334         [PreserveSig]
335         int StrongNameFreeBuffer(
336             [In] IntPtr pbMemory);
337
338         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
339         [PreserveSig]
340         int StrongNameGetBlob(
341             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
342             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pbBlob,
343             [In, Out, MarshalAs(UnmanagedType.U4)] ref int pcbBlob);
344
345         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
346         [PreserveSig]
347         int StrongNameGetBlobFromImage(
348             [In] IntPtr pbBase,
349             [In, MarshalAs(UnmanagedType.U4)] int dwLength,
350             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbBlob,
351             [In, Out, MarshalAs(UnmanagedType.U4)] ref int pcbBlob);
352
353         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
354         [PreserveSig]
355         int StrongNameGetPublicKey(
356             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
357             [In] IntPtr pbKeyBlob,
358             [In, MarshalAs(UnmanagedType.U4)] int cbKeyBlob,
359             out IntPtr ppbPublicKeyBlob,
360             [MarshalAs(UnmanagedType.U4)] out int pcbPublicKeyBlob);
361
362         [return: MarshalAs(UnmanagedType.U4)]
363         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
364         [PreserveSig]
365         int StrongNameHashSize(
366             [In, MarshalAs(UnmanagedType.U4)] int ulHashAlg,
367             [MarshalAs(UnmanagedType.U4)] out int cbSize);
368
369         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
370         [PreserveSig]
371         int StrongNameKeyDelete(
372             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer);
373
374         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
375         [PreserveSig]
376         int StrongNameKeyGen(
377             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
378             [In, MarshalAs(UnmanagedType.U4)] int dwFlags,
379             out IntPtr ppbKeyBlob,
380             [MarshalAs(UnmanagedType.U4)] out int pcbKeyBlob);
381
382         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
383         [PreserveSig]
384         int StrongNameKeyGenEx(
385             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
386             [In, MarshalAs(UnmanagedType.U4)] int dwFlags,
387             [In, MarshalAs(UnmanagedType.U4)] int dwKeySize,
388             out IntPtr ppbKeyBlob,
389             [MarshalAs(UnmanagedType.U4)] out int pcbKeyBlob);
390
391         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
392         [PreserveSig]
393         int StrongNameKeyInstall(
394             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
395             [In] IntPtr pbKeyBlob,
396             [In, MarshalAs(UnmanagedType.U4)] int cbKeyBlob);
397
398         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
399         [PreserveSig]
400         int StrongNameSignatureGeneration(
401             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
402             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
403             [In] IntPtr pbKeyBlob,
404             [In, MarshalAs(UnmanagedType.U4)] int cbKeyBlob,
405             [In, Out] IntPtr ppbSignatureBlob,
406             [MarshalAs(UnmanagedType.U4)] out int pcbSignatureBlob);
407
408         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
409         [PreserveSig]
410         int StrongNameSignatureGenerationEx(
411             [In, MarshalAs(UnmanagedType.LPWStr)] string wszFilePath,
412             [In, MarshalAs(UnmanagedType.LPWStr)] string wszKeyContainer,
413             [In] IntPtr pbKeyBlob,
414             [In, MarshalAs(UnmanagedType.U4)] int cbKeyBlob,
415             [In, Out] IntPtr ppbSignatureBlob,
416             [MarshalAs(UnmanagedType.U4)] out int pcbSignatureBlob,
417             [In, MarshalAs(UnmanagedType.U4)] int dwFlags);
418
419         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
420         [PreserveSig]
421         int StrongNameSignatureSize(
422             [In] IntPtr pbPublicKeyBlob,
423             [In, MarshalAs(UnmanagedType.U4)] int cbPublicKeyBlob,
424             [MarshalAs(UnmanagedType.U4)] out int pcbSize);
425
426         [return: MarshalAs(UnmanagedType.U4)]
427         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
428         [PreserveSig]
429         int StrongNameSignatureVerification(
430             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
431             [In, MarshalAs(UnmanagedType.U4)] int dwInFlags,
432             [MarshalAs(UnmanagedType.U4)] out int dwOutFlags);
433
434         [return: MarshalAs(UnmanagedType.U4)]
435         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
436         [PreserveSig]
437         int StrongNameSignatureVerificationEx(
438             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
439             [In, MarshalAs(UnmanagedType.I1)] bool fForceVerification,
440             [MarshalAs(UnmanagedType.I1)] out bool fWasVerified);
441
442         [return: MarshalAs(UnmanagedType.U4)]
443         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
444         [PreserveSig]
445         int StrongNameSignatureVerificationFromImage(
446             [In] IntPtr pbBase,
447             [In, MarshalAs(UnmanagedType.U4)] int dwLength,
448             [In, MarshalAs(UnmanagedType.U4)] int dwInFlags,
449             [MarshalAs(UnmanagedType.U4)] out int dwOutFlags);
450
451         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
452         [PreserveSig]
453         int StrongNameTokenFromAssembly(
454             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
455             out IntPtr ppbStrongNameToken,
456             [MarshalAs(UnmanagedType.U4)] out int pcbStrongNameToken);
457
458         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
459         [PreserveSig]
460         int StrongNameTokenFromAssemblyEx(
461             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
462             out IntPtr ppbStrongNameToken,
463             [MarshalAs(UnmanagedType.U4)] out int pcbStrongNameToken,
464             out IntPtr ppbPublicKeyBlob,
465             [MarshalAs(UnmanagedType.U4)] out int pcbPublicKeyBlob);
466
467         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
468         [PreserveSig]
469         int StrongNameTokenFromPublicKey(
470             [In] IntPtr pbPublicKeyBlob,
471             [In, MarshalAs(UnmanagedType.U4)] int cbPublicKeyBlob,
472             out IntPtr ppbStrongNameToken,
473             [MarshalAs(UnmanagedType.U4)] out int pcbStrongNameToken);
474     }
475
476     /// <summary>
477     /// This is a managed wrapper for the IClrStrongName interface defined in metahost.idl
478     /// This is very similar to the standard RCWs provided in 
479     /// ndp/fx/src/hosting/interop/microsoft/runtime/hosting/interop, but we don't want to
480     /// reference that assembly (part of the SDK only, not .NET redist).  Also, our version
481     /// is designed specifically for easy migration from the old mscoree APIs, for example
482     /// all APIs return HResults rather than throw exceptions.
483     /// </summary> 
484     [System.Security.SecurityCritical]
485     [ComImport, ComConversionLoss, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("9FD93CCF-3280-4391-B3A9-96E1CDE77C8D")]
486     internal interface IClrStrongName {
487         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
488         [PreserveSig]
489         int GetHashFromAssemblyFile(
490             [In, MarshalAs(UnmanagedType.LPStr)] string pszFilePath,
491             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
492             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
493             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
494             [MarshalAs(UnmanagedType.U4)] out int pchHash);
495
496         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
497         [PreserveSig]
498         int GetHashFromAssemblyFileW(
499             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
500             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
501             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
502             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
503             [MarshalAs(UnmanagedType.U4)] out int pchHash);
504
505         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
506         [PreserveSig]
507         int GetHashFromBlob(
508             [In] IntPtr pbBlob,
509             [In, MarshalAs(UnmanagedType.U4)] int cchBlob,
510             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
511             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)] byte[] pbHash,
512             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
513             [MarshalAs(UnmanagedType.U4)] out int pchHash);
514
515         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
516         [PreserveSig]
517         int GetHashFromFile(
518             [In, MarshalAs(UnmanagedType.LPStr)] string pszFilePath,
519             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
520             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
521             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
522             [MarshalAs(UnmanagedType.U4)] out int pchHash);
523
524         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
525         [PreserveSig]
526         int GetHashFromFileW(
527             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
528             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
529             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
530             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
531             [MarshalAs(UnmanagedType.U4)] out int pchHash);
532
533         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
534         [PreserveSig]
535         int GetHashFromHandle(
536             [In] IntPtr hFile,
537             [In, Out, MarshalAs(UnmanagedType.U4)] ref int piHashAlg,
538             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbHash,
539             [In, MarshalAs(UnmanagedType.U4)] int cchHash,
540             [MarshalAs(UnmanagedType.U4)] out int pchHash);
541
542         [return: MarshalAs(UnmanagedType.U4)]
543         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
544         [PreserveSig]
545         int StrongNameCompareAssemblies(
546             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzAssembly1,
547             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzAssembly2,
548             [MarshalAs(UnmanagedType.U4)] out int dwResult);
549
550         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
551         [PreserveSig]
552         int StrongNameFreeBuffer(
553             [In] IntPtr pbMemory);
554
555         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
556         [PreserveSig]
557         int StrongNameGetBlob(
558             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
559             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pbBlob,
560             [In, Out, MarshalAs(UnmanagedType.U4)] ref int pcbBlob);
561
562         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
563         [PreserveSig]
564         int StrongNameGetBlobFromImage(
565             [In] IntPtr pbBase,
566             [In, MarshalAs(UnmanagedType.U4)] int dwLength,
567             [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbBlob,
568             [In, Out, MarshalAs(UnmanagedType.U4)] ref int pcbBlob);
569
570         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
571         [PreserveSig]
572         int StrongNameGetPublicKey(
573             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
574             [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pbKeyBlob,
575             [In, MarshalAs(UnmanagedType.U4)] int cbKeyBlob,
576             out IntPtr ppbPublicKeyBlob,
577             [MarshalAs(UnmanagedType.U4)] out int pcbPublicKeyBlob);
578
579         [return: MarshalAs(UnmanagedType.U4)]
580         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
581         [PreserveSig]
582         int StrongNameHashSize(
583             [In, MarshalAs(UnmanagedType.U4)] int ulHashAlg,
584             [MarshalAs(UnmanagedType.U4)] out int cbSize);
585
586         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
587         [PreserveSig]
588         int StrongNameKeyDelete(
589             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer);
590
591         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
592         [PreserveSig]
593         int StrongNameKeyGen(
594             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
595             [In, MarshalAs(UnmanagedType.U4)] int dwFlags,
596             out IntPtr ppbKeyBlob,
597             [MarshalAs(UnmanagedType.U4)] out int pcbKeyBlob);
598
599         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
600         [PreserveSig]
601         int StrongNameKeyGenEx(
602             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
603             [In, MarshalAs(UnmanagedType.U4)] int dwFlags,
604             [In, MarshalAs(UnmanagedType.U4)] int dwKeySize,
605             out IntPtr ppbKeyBlob,
606             [MarshalAs(UnmanagedType.U4)] out int pcbKeyBlob);
607
608         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
609         [PreserveSig]
610         int StrongNameKeyInstall(
611             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
612             [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pbKeyBlob,
613             [In, MarshalAs(UnmanagedType.U4)] int cbKeyBlob);
614
615         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
616         [PreserveSig]
617         int StrongNameSignatureGeneration(
618             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
619             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzKeyContainer,
620             [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbKeyBlob,
621             [In, MarshalAs(UnmanagedType.U4)] int cbKeyBlob,
622             [In, Out] IntPtr ppbSignatureBlob,
623             [MarshalAs(UnmanagedType.U4)] out int pcbSignatureBlob);
624
625         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
626         [PreserveSig]
627         int StrongNameSignatureGenerationEx(
628             [In, MarshalAs(UnmanagedType.LPWStr)] string wszFilePath,
629             [In, MarshalAs(UnmanagedType.LPWStr)] string wszKeyContainer,
630             [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbKeyBlob,
631             [In, MarshalAs(UnmanagedType.U4)] int cbKeyBlob,
632             [In, Out] IntPtr ppbSignatureBlob,
633             [MarshalAs(UnmanagedType.U4)] out int pcbSignatureBlob,
634             [In, MarshalAs(UnmanagedType.U4)] int dwFlags);
635
636         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
637         [PreserveSig]
638         int StrongNameSignatureSize(
639             [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] pbPublicKeyBlob,
640             [In, MarshalAs(UnmanagedType.U4)] int cbPublicKeyBlob,
641             [MarshalAs(UnmanagedType.U4)] out int pcbSize);
642
643         [return: MarshalAs(UnmanagedType.U4)]
644         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
645         [PreserveSig]
646         int StrongNameSignatureVerification(
647             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
648             [In, MarshalAs(UnmanagedType.U4)] int dwInFlags,
649             [MarshalAs(UnmanagedType.U4)] out int dwOutFlags);
650
651         [return: MarshalAs(UnmanagedType.U4)]
652         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
653         [PreserveSig]
654         int StrongNameSignatureVerificationEx(
655             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
656             [In, MarshalAs(UnmanagedType.I1)] bool fForceVerification,
657             [MarshalAs(UnmanagedType.I1)] out bool fWasVerified);
658
659         [return: MarshalAs(UnmanagedType.U4)]
660         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
661         [PreserveSig]
662         int StrongNameSignatureVerificationFromImage(
663             [In] IntPtr pbBase,
664             [In, MarshalAs(UnmanagedType.U4)] int dwLength,
665             [In, MarshalAs(UnmanagedType.U4)] int dwInFlags,
666             [MarshalAs(UnmanagedType.U4)] out int dwOutFlags);
667
668         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
669         [PreserveSig]
670         int StrongNameTokenFromAssembly(
671             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
672             out IntPtr ppbStrongNameToken,
673             [MarshalAs(UnmanagedType.U4)] out int pcbStrongNameToken);
674
675         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
676         [PreserveSig]
677         int StrongNameTokenFromAssemblyEx(
678             [In, MarshalAs(UnmanagedType.LPWStr)] string pwzFilePath,
679             out IntPtr ppbStrongNameToken,
680             [MarshalAs(UnmanagedType.U4)] out int pcbStrongNameToken,
681             out IntPtr ppbPublicKeyBlob,
682             [MarshalAs(UnmanagedType.U4)] out int pcbPublicKeyBlob);
683
684         [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
685         [PreserveSig]
686         int StrongNameTokenFromPublicKey(
687             [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] pbPublicKeyBlob,
688             [In, MarshalAs(UnmanagedType.U4)] int cbPublicKeyBlob,
689             out IntPtr ppbStrongNameToken,
690             [MarshalAs(UnmanagedType.U4)] out int pcbStrongNameToken);
691     }
692 }