3 // Copyright (c) Microsoft Corporation. All rights reserved.
8 using System.Diagnostics.Contracts;
10 namespace System.Security.Cryptography {
12 /// Utility class to strongly type the format of key blobs used with CNG. Since all CNG APIs which
13 /// require or return a key blob format take the name as a string, we use this string wrapper class to
14 /// specifically mark which parameters and return values are expected to be key blob formats. We also
15 /// provide a list of well known blob formats, which helps Intellisense users find a set of good blob
19 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
20 public sealed class CngKeyBlobFormat : IEquatable<CngKeyBlobFormat> {
21 private static volatile CngKeyBlobFormat s_eccPrivate;
22 private static volatile CngKeyBlobFormat s_eccPublic;
23 private static volatile CngKeyBlobFormat s_genericPrivate;
24 private static volatile CngKeyBlobFormat s_genericPublic;
25 private static volatile CngKeyBlobFormat s_opaqueTransport;
26 private static volatile CngKeyBlobFormat s_pkcs8Private;
28 private string m_format;
30 public CngKeyBlobFormat(string format) {
31 Contract.Ensures(!String.IsNullOrEmpty(m_format));
34 throw new ArgumentNullException("format");
36 if (format.Length == 0) {
37 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidKeyBlobFormat, format), "format");
44 /// Name of the blob format
46 public string Format {
48 Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()));
53 public static bool operator ==(CngKeyBlobFormat left, CngKeyBlobFormat right) {
54 if (Object.ReferenceEquals(left, null)) {
55 return Object.ReferenceEquals(right, null);
58 return left.Equals(right);
62 public static bool operator !=(CngKeyBlobFormat left, CngKeyBlobFormat right) {
63 if (Object.ReferenceEquals(left, null)) {
64 return !Object.ReferenceEquals(right, null);
67 return !left.Equals(right);
70 public override bool Equals(object obj) {
71 Contract.Assert(m_format != null);
73 return Equals(obj as CngKeyBlobFormat);
76 public bool Equals(CngKeyBlobFormat other) {
77 if (Object.ReferenceEquals(other, null)) {
81 return m_format.Equals(other.Format);
84 public override int GetHashCode() {
85 Contract.Assert(m_format != null);
86 return m_format.GetHashCode();
89 public override string ToString() {
90 Contract.Assert(m_format != null);
95 // Well known key blob formats
98 public static CngKeyBlobFormat EccPrivateBlob {
100 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
102 if (s_eccPrivate == null) {
103 s_eccPrivate = new CngKeyBlobFormat("ECCPRIVATEBLOB"); // BCRYPT_ECCPRIVATE_BLOB
110 public static CngKeyBlobFormat EccPublicBlob {
112 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
114 if (s_eccPublic == null) {
115 s_eccPublic = new CngKeyBlobFormat("ECCPUBLICBLOB"); // BCRYPT_ECCPUBLIC_BLOB
122 public static CngKeyBlobFormat EccFullPrivateBlob {
124 throw new NotImplementedException ();
128 public static CngKeyBlobFormat EccFullPublicBlob {
130 throw new NotImplementedException ();
134 public static CngKeyBlobFormat GenericPrivateBlob {
136 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
138 if (s_genericPrivate == null) {
139 s_genericPrivate = new CngKeyBlobFormat("PRIVATEBLOB"); // BCRYPT_PRIVATE_KEY_BLOB
142 return s_genericPrivate;
146 public static CngKeyBlobFormat GenericPublicBlob {
148 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
150 if (s_genericPublic == null) {
151 s_genericPublic = new CngKeyBlobFormat("PUBLICBLOB"); // BCRYPT_PUBLIC_KEY_BLOB
154 return s_genericPublic;
158 public static CngKeyBlobFormat OpaqueTransportBlob {
160 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
162 if (s_opaqueTransport == null) {
163 s_opaqueTransport = new CngKeyBlobFormat("OpaqueTransport"); // NCRYPT_OPAQUETRANSPORT_BLOB
166 return s_opaqueTransport;
170 public static CngKeyBlobFormat Pkcs8PrivateBlob {
172 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
174 if (s_pkcs8Private == null) {
175 s_pkcs8Private = new CngKeyBlobFormat("PKCS8_PRIVATEKEY"); // NCRYPT_PKCS8_PRIVATE_KEY_BLOB
178 return s_pkcs8Private;