(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Security.Win32 / Mono.Security.Cryptography / CapiContext.cs
blob32f0a13ceb1a51378bd4068b68484a5d60a13740
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 //
11 using System;
12 using System.Security.Cryptography;
14 namespace Mono.Security.Cryptography {
16 // we deal with unmanaged resources - they MUST be released after use!
17 public class CapiContext : IDisposable {
19 // handles to CryptoAPI - they are
20 private IntPtr providerHandle;
22 private CspParameters cspParams;
24 // has the last call succeded ?
25 private bool lastResult;
27 // Create an instance using the default CSP
28 public CapiContext () : this (null)
32 // Create an instance using the specified CSP
33 public CapiContext (CspParameters csp)
35 providerHandle = IntPtr.Zero;
36 if (csp == null) {
37 // default parameters
38 cspParams = new CspParameters ();
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;
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;
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);
63 ~CapiContext ()
65 Dispose ();
68 public int Error {
69 get { return CryptoAPI.GetLastError (); }
72 public IntPtr Handle {
73 get { return providerHandle; }
76 public bool Result {
77 get { return lastResult; }
80 internal bool InternalResult {
81 set { lastResult = value; }
84 // release unmanaged resources
85 public void Dispose ()
87 if (providerHandle != IntPtr.Zero) {
88 lastResult = CryptoAPI.CryptReleaseContext (providerHandle, 0);
89 GC.KeepAlive (this);
90 providerHandle = IntPtr.Zero;
91 GC.SuppressFinalize (this);