File: CapiContext.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (96 lines) | stat: -rw-r--r-- 2,379 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Mono.Security.Cryptography.CapiContext
//
// Authors:
//	Sebastien Pouliot (sebastien@ximian.com)
//
// Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004 Novell (http://www.novell.com)
//

using System;
using System.Security.Cryptography;

namespace Mono.Security.Cryptography {

// we deal with unmanaged resources - they MUST be released after use!
public class CapiContext : IDisposable {

	// handles to CryptoAPI - they are 
	private IntPtr providerHandle;
        
	private CspParameters cspParams;

	// has the last call succeded ?
	private bool lastResult;

	// Create an instance using the default CSP
	public CapiContext () : this (null)
	{
	}

	// Create an instance using the specified CSP
	public CapiContext (CspParameters csp) 
	{
		providerHandle = IntPtr.Zero;
		if (csp == null) {
			// default parameters
			cspParams = new CspParameters ();
		}
		else {
			// keep of copy of the parameters
			cspParams = new CspParameters (csp.ProviderType, csp.ProviderName, csp.KeyContainerName);
			cspParams.KeyNumber = csp.KeyNumber;
			cspParams.Flags = csp.Flags;
		}
		
		// do not show user interface (CRYPT_SILENT) -  if UI is required then the function fails.
		uint flags = CryptoAPI.CRYPT_SILENT;
		if ((cspParams.Flags & CspProviderFlags.UseMachineKeyStore) == CspProviderFlags.UseMachineKeyStore) {
			flags |= CryptoAPI.CRYPT_MACHINE_KEYSET;
		}

		lastResult = CryptoAPI.CryptAcquireContextA (ref providerHandle, cspParams.KeyContainerName,
			cspParams.ProviderName, cspParams.ProviderType, flags);
		if (!lastResult) {
			// key container may not exist
			flags |= CryptoAPI.CRYPT_NEWKEYSET;
			lastResult = CryptoAPI.CryptAcquireContextA (ref providerHandle, cspParams.KeyContainerName,
				cspParams.ProviderName, cspParams.ProviderType, flags);
		}
	}

	~CapiContext () 
	{
		Dispose ();
	}

	public int Error {
		get { return CryptoAPI.GetLastError (); }
	}

	public IntPtr Handle {
		get { return providerHandle; }
	}

	public bool Result {
		get { return lastResult; }
	}

	internal bool InternalResult {
		set { lastResult = value; }
	}

	// release unmanaged resources
	public void Dispose () 
	{
		if (providerHandle != IntPtr.Zero) {
			lastResult = CryptoAPI.CryptReleaseContext (providerHandle, 0);
			GC.KeepAlive (this);
			providerHandle = IntPtr.Zero;
			GC.SuppressFinalize (this);
		}
	}
}

}