/
CngKeyBlobFormat.cs
165 lines (140 loc) · 4.72 KB
/
CngKeyBlobFormat.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.Security.Cryptography
{
/// <summary>
/// Utility class to strongly type the format of key blobs used with CNG. Since all CNG APIs which
/// require or return a key blob format take the name as a string, we use this string wrapper class to
/// specifically mark which parameters and return values are expected to be key blob formats. We also
/// provide a list of well known blob formats, which helps Intellisense users find a set of good blob
/// formats to use.
/// </summary>
public sealed class CngKeyBlobFormat : IEquatable<CngKeyBlobFormat>
{
public CngKeyBlobFormat(string format)
{
ArgumentException.ThrowIfNullOrEmpty(format);
_format = format;
}
/// <summary>
/// Name of the blob format
/// </summary>
public string Format
{
get
{
return _format;
}
}
public static bool operator ==(CngKeyBlobFormat? left, CngKeyBlobFormat? right)
{
if (left is null)
{
return right is null;
}
return left.Equals(right);
}
public static bool operator !=(CngKeyBlobFormat? left, CngKeyBlobFormat? right)
{
if (left is null)
{
return right is not null;
}
return !left.Equals(right);
}
public override bool Equals([NotNullWhen(true)] object? obj)
{
Debug.Assert(_format != null);
return Equals(obj as CngKeyBlobFormat);
}
public bool Equals([NotNullWhen(true)] CngKeyBlobFormat? other)
{
if (other is null)
{
return false;
}
return _format.Equals(other.Format);
}
public override int GetHashCode()
{
Debug.Assert(_format != null);
return _format.GetHashCode();
}
public override string ToString()
{
Debug.Assert(_format != null);
return _format;
}
//
// Well known key blob formats
//
public static CngKeyBlobFormat EccPrivateBlob
{
get
{
return s_eccPrivate ??= new CngKeyBlobFormat("ECCPRIVATEBLOB"); // BCRYPT_ECCPRIVATE_BLOB
}
}
public static CngKeyBlobFormat EccPublicBlob
{
get
{
return s_eccPublic ??= new CngKeyBlobFormat("ECCPUBLICBLOB"); // BCRYPT_ECCPUBLIC_BLOB
}
}
public static CngKeyBlobFormat EccFullPrivateBlob
{
get
{
return s_eccFullPrivate ??= new CngKeyBlobFormat("ECCFULLPRIVATEBLOB"); // BCRYPT_ECCFULLPRIVATE_BLOB
}
}
public static CngKeyBlobFormat EccFullPublicBlob
{
get
{
return s_eccFullPublic ??= new CngKeyBlobFormat("ECCFULLPUBLICBLOB"); // BCRYPT_ECCFULLPUBLIC_BLOB
}
}
public static CngKeyBlobFormat GenericPrivateBlob
{
get
{
return s_genericPrivate ??= new CngKeyBlobFormat("PRIVATEBLOB"); // BCRYPT_PRIVATE_KEY_BLOB
}
}
public static CngKeyBlobFormat GenericPublicBlob
{
get
{
return s_genericPublic ??= new CngKeyBlobFormat("PUBLICBLOB"); // BCRYPT_PUBLIC_KEY_BLOB
}
}
public static CngKeyBlobFormat OpaqueTransportBlob
{
get
{
return s_opaqueTransport ??= new CngKeyBlobFormat("OpaqueTransport"); // NCRYPT_OPAQUETRANSPORT_BLOB
}
}
public static CngKeyBlobFormat Pkcs8PrivateBlob
{
get
{
return s_pkcs8Private ??= new CngKeyBlobFormat("PKCS8_PRIVATEKEY"); // NCRYPT_PKCS8_PRIVATE_KEY_BLOB
}
}
private static CngKeyBlobFormat? s_eccPrivate;
private static CngKeyBlobFormat? s_eccPublic;
private static CngKeyBlobFormat? s_eccFullPrivate;
private static CngKeyBlobFormat? s_eccFullPublic;
private static CngKeyBlobFormat? s_genericPrivate;
private static CngKeyBlobFormat? s_genericPublic;
private static CngKeyBlobFormat? s_opaqueTransport;
private static CngKeyBlobFormat? s_pkcs8Private;
private readonly string _format;
}
}