2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Mono.Security.Win32 / Mono.Security.Cryptography / CapiContext.cs
1 //
2 // Mono.Security.Cryptography.CapiContext
3 //
4 // Authors:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
10
11 using System;
12 using System.Security.Cryptography;
13
14 namespace Mono.Security.Cryptography {
15
16 // we deal with unmanaged resources - they MUST be released after use!
17 public class CapiContext : IDisposable {
18
19         // handles to CryptoAPI - they are 
20         private IntPtr providerHandle;
21         
22         private CspParameters cspParams;
23
24         // has the last call succeded ?
25         private bool lastResult;
26
27         // Create an instance using the default CSP
28         public CapiContext () : this (null)
29         {
30         }
31
32         // Create an instance using the specified CSP
33         public CapiContext (CspParameters csp) 
34         {
35                 providerHandle = IntPtr.Zero;
36                 if (csp == null) {
37                         // default parameters
38                         cspParams = new CspParameters ();
39                 }
40                 else {
41                         // keep of copy of the parameters
42                         cspParams = new CspParameters (csp.ProviderType, csp.ProviderName, csp.KeyContainerName);
43                         cspParams.KeyNumber = csp.KeyNumber;
44                         cspParams.Flags = csp.Flags;
45                 }
46                 
47                 // do not show user interface (CRYPT_SILENT) -  if UI is required then the function fails.
48                 uint flags = CryptoAPI.CRYPT_SILENT;
49                 if ((cspParams.Flags & CspProviderFlags.UseMachineKeyStore) == CspProviderFlags.UseMachineKeyStore) {
50                         flags |= CryptoAPI.CRYPT_MACHINE_KEYSET;
51                 }
52
53                 lastResult = CryptoAPI.CryptAcquireContextA (ref providerHandle, cspParams.KeyContainerName,
54                         cspParams.ProviderName, cspParams.ProviderType, flags);
55                 if (!lastResult) {
56                         // key container may not exist
57                         flags |= CryptoAPI.CRYPT_NEWKEYSET;
58                         lastResult = CryptoAPI.CryptAcquireContextA (ref providerHandle, cspParams.KeyContainerName,
59                                 cspParams.ProviderName, cspParams.ProviderType, flags);
60                 }
61         }
62
63         ~CapiContext () 
64         {
65                 Dispose ();
66         }
67
68         public int Error {
69                 get { return CryptoAPI.GetLastError (); }
70         }
71
72         public IntPtr Handle {
73                 get { return providerHandle; }
74         }
75
76         public bool Result {
77                 get { return lastResult; }
78         }
79
80         internal bool InternalResult {
81                 set { lastResult = value; }
82         }
83
84         // release unmanaged resources
85         public void Dispose () 
86         {
87                 if (providerHandle != IntPtr.Zero) {
88                         lastResult = CryptoAPI.CryptReleaseContext (providerHandle, 0);
89                         GC.KeepAlive (this);
90                         providerHandle = IntPtr.Zero;
91                         GC.SuppressFinalize (this);
92                 }
93         }
94 }
95
96 }