Merge pull request #4433 from kumpera/android-fixes
[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         public static CngKeyBlobFormat EccFullPrivateBlob {
123             get {
124                 throw new NotImplementedException ();
125             }
126         }
127
128         public static CngKeyBlobFormat EccFullPublicBlob {
129             get {
130                 throw new NotImplementedException ();
131             }
132         }
133
134         public static CngKeyBlobFormat GenericPrivateBlob {
135             get {
136                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
137
138                 if (s_genericPrivate == null) {
139                     s_genericPrivate = new CngKeyBlobFormat("PRIVATEBLOB");     // BCRYPT_PRIVATE_KEY_BLOB
140                 }
141
142                 return s_genericPrivate;
143             }
144         }
145
146         public static CngKeyBlobFormat GenericPublicBlob {
147             get {
148                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
149
150                 if (s_genericPublic == null) {
151                     s_genericPublic = new CngKeyBlobFormat("PUBLICBLOB");       // BCRYPT_PUBLIC_KEY_BLOB
152                 }
153
154                 return s_genericPublic;
155             }
156         }
157
158         public static CngKeyBlobFormat OpaqueTransportBlob {
159             get {
160                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
161
162                 if (s_opaqueTransport == null) {
163                     s_opaqueTransport = new CngKeyBlobFormat("OpaqueTransport");    // NCRYPT_OPAQUETRANSPORT_BLOB
164                 }
165
166                 return s_opaqueTransport;
167             }
168         }
169
170         public static CngKeyBlobFormat Pkcs8PrivateBlob {
171             get {
172                 Contract.Ensures(Contract.Result<CngKeyBlobFormat>() != null);
173
174                 if (s_pkcs8Private == null) {
175                     s_pkcs8Private = new CngKeyBlobFormat("PKCS8_PRIVATEKEY");      // NCRYPT_PKCS8_PRIVATE_KEY_BLOB
176                 }
177
178                 return s_pkcs8Private;
179             }
180         }
181     }
182 }