[threads] Don't ignore abort requests in abort protected blocks
[mono.git] / mcs / class / referencesource / System.Core / System / Security / Cryptography / CngKeyBlobFormat.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6
7 using System;
8 using System.Diagnostics.Contracts;
9
10 namespace System.Security.Cryptography {
11     /// <summary>
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
16     ///     formats to use.
17     /// </summary>
18     [Serializable]
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;
27
28         private string m_format;
29
30         public CngKeyBlobFormat(string format) {
31             Contract.Ensures(!String.IsNullOrEmpty(m_format));
32
33             if (format == null) {
34                 throw new ArgumentNullException("format");
35             }
36             if (format.Length == 0) {
37                 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidKeyBlobFormat, format), "format");
38             }
39
40             m_format = format;
41         }
42
43         /// <summary>
44         ///     Name of the blob format
45         /// </summary>
46         public string Format {
47             get {
48                 Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()));
49                 return m_format;
50             }
51         }
52
53         public static bool operator ==(CngKeyBlobFormat left, CngKeyBlobFormat right) {
54             if (Object.ReferenceEquals(left, null)) {
55                 return Object.ReferenceEquals(right, null);
56             }
57
58             return left.Equals(right);
59         }
60
61         [Pure]
62         public static bool operator !=(CngKeyBlobFormat left, CngKeyBlobFormat right) {
63             if (Object.ReferenceEquals(left, null)) {
64                 return !Object.ReferenceEquals(right, null);
65             }
66
67             return !left.Equals(right);
68         }
69
70         public override bool Equals(object obj) {
71             Contract.Assert(m_format != null);
72
73             return Equals(obj as CngKeyBlobFormat);
74         }
75
76         public bool Equals(CngKeyBlobFormat other) {
77             if (Object.ReferenceEquals(other, null)) {
78                 return false;
79             }
80
81             return m_format.Equals(other.Format);
82         }
83
84         public override int GetHashCode() {
85             Contract.Assert(m_format != null);
86             return m_format.GetHashCode();
87         }
88
89         public override string ToString() {
90             Contract.Assert(m_format != null);
91             return m_format;
92         }
93
94         //
95         // Well known key blob formats
96         //
97
98         public static CngKeyBlobFormat EccPrivateBlob {
99             get {
100                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
101
102                 if (s_eccPrivate == null) {
103                     s_eccPrivate = new CngKeyBlobFormat("ECCPRIVATEBLOB");      // BCRYPT_ECCPRIVATE_BLOB
104                 }
105
106                 return s_eccPrivate;
107             }
108         }
109
110         public static CngKeyBlobFormat EccPublicBlob {
111             get {
112                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
113
114                 if (s_eccPublic == null) {
115                     s_eccPublic = new CngKeyBlobFormat("ECCPUBLICBLOB");        // BCRYPT_ECCPUBLIC_BLOB
116                 }
117
118                 return s_eccPublic;
119             }
120         }
121
122 #if NETSTANDARD
123         public static CngKeyBlobFormat EccFullPrivateBlob {
124             get {
125                 throw new NotImplementedException ();
126             }
127         }
128
129         public static CngKeyBlobFormat EccFullPublicBlob {
130             get {
131                 throw new NotImplementedException ();
132             }
133         }
134 #endif
135
136         public static CngKeyBlobFormat GenericPrivateBlob {
137             get {
138                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
139
140                 if (s_genericPrivate == null) {
141                     s_genericPrivate = new CngKeyBlobFormat("PRIVATEBLOB");     // BCRYPT_PRIVATE_KEY_BLOB
142                 }
143
144                 return s_genericPrivate;
145             }
146         }
147
148         public static CngKeyBlobFormat GenericPublicBlob {
149             get {
150                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
151
152                 if (s_genericPublic == null) {
153                     s_genericPublic = new CngKeyBlobFormat("PUBLICBLOB");       // BCRYPT_PUBLIC_KEY_BLOB
154                 }
155
156                 return s_genericPublic;
157             }
158         }
159
160         public static CngKeyBlobFormat OpaqueTransportBlob {
161             get {
162                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
163
164                 if (s_opaqueTransport == null) {
165                     s_opaqueTransport = new CngKeyBlobFormat("OpaqueTransport");    // NCRYPT_OPAQUETRANSPORT_BLOB
166                 }
167
168                 return s_opaqueTransport;
169             }
170         }
171
172         public static CngKeyBlobFormat Pkcs8PrivateBlob {
173             get {
174                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
175
176                 if (s_pkcs8Private == null) {
177                     s_pkcs8Private = new CngKeyBlobFormat("PKCS8_PRIVATEKEY");      // NCRYPT_PKCS8_PRIVATE_KEY_BLOB
178                 }
179
180                 return s_pkcs8Private;
181             }
182         }
183     }
184 }