forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FindPal.cs
286 lines (253 loc) · 11.7 KB
/
FindPal.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace Internal.Cryptography.Pal
{
internal partial class FindPal
{
private const int NamedKeyUsageFlagsCount = 9;
private static readonly Dictionary<string, X509KeyUsageFlags> s_keyUsages =
new Dictionary<string, X509KeyUsageFlags>(NamedKeyUsageFlagsCount, StringComparer.OrdinalIgnoreCase)
{
{ "DigitalSignature", X509KeyUsageFlags.DigitalSignature },
{ "NonRepudiation", X509KeyUsageFlags.NonRepudiation },
{ "KeyEncipherment", X509KeyUsageFlags.KeyEncipherment },
{ "DataEncipherment", X509KeyUsageFlags.DataEncipherment },
{ "KeyAgreement", X509KeyUsageFlags.KeyAgreement },
{ "KeyCertSign", X509KeyUsageFlags.KeyCertSign },
{ "CrlSign", X509KeyUsageFlags.CrlSign },
{ "EncipherOnly", X509KeyUsageFlags.EncipherOnly },
{ "DecipherOnly", X509KeyUsageFlags.DecipherOnly },
};
#if DEBUG
static FindPal()
{
Debug.Assert(s_keyUsages.Count == NamedKeyUsageFlagsCount);
}
#endif
public static X509Certificate2Collection FindFromCollection(
X509Certificate2Collection coll,
X509FindType findType,
object findValue,
bool validOnly)
{
X509Certificate2Collection results = new X509Certificate2Collection();
using (IFindPal findPal = OpenPal(coll, results, validOnly))
{
switch (findType)
{
case X509FindType.FindByThumbprint:
{
byte[] thumbPrint = ConfirmedCast<string>(findValue).DecodeHexString();
findPal.FindByThumbprint(thumbPrint);
break;
}
case X509FindType.FindBySubjectName:
{
string subjectName = ConfirmedCast<string>(findValue);
findPal.FindBySubjectName(subjectName);
break;
}
case X509FindType.FindBySubjectDistinguishedName:
{
string subjectDistinguishedName = ConfirmedCast<string>(findValue);
findPal.FindBySubjectDistinguishedName(subjectDistinguishedName);
break;
}
case X509FindType.FindByIssuerName:
{
string issuerName = ConfirmedCast<string>(findValue);
findPal.FindByIssuerName(issuerName);
break;
}
case X509FindType.FindByIssuerDistinguishedName:
{
string issuerDistinguishedName = ConfirmedCast<string>(findValue);
findPal.FindByIssuerDistinguishedName(issuerDistinguishedName);
break;
}
case X509FindType.FindBySerialNumber:
{
string decimalOrHexString = ConfirmedCast<string>(findValue);
// FindBySerialNumber allows the input format to be either in
// hex or decimal. Since we can't know which one was intended,
// it compares against both interpretations and treats a match
// of either as a successful find.
// string is big-endian, BigInteger constructor requires little-endian.
byte[] hexBytes = decimalOrHexString.DecodeHexString();
Array.Reverse(hexBytes);
BigInteger hexValue = PositiveBigIntegerFromByteArray(hexBytes);
BigInteger decimalValue = LaxParseDecimalBigInteger(decimalOrHexString);
findPal.FindBySerialNumber(hexValue, decimalValue);
break;
}
case X509FindType.FindByTimeValid:
{
DateTime dateTime = ConfirmedCast<DateTime>(findValue);
findPal.FindByTimeValid(dateTime);
break;
}
case X509FindType.FindByTimeNotYetValid:
{
DateTime dateTime = ConfirmedCast<DateTime>(findValue);
findPal.FindByTimeNotYetValid(dateTime);
break;
}
case X509FindType.FindByTimeExpired:
{
DateTime dateTime = ConfirmedCast<DateTime>(findValue);
findPal.FindByTimeExpired(dateTime);
break;
}
case X509FindType.FindByTemplateName:
{
string expected = ConfirmedCast<string>(findValue);
findPal.FindByTemplateName(expected);
break;
}
case X509FindType.FindByApplicationPolicy:
{
string oidValue = ConfirmedOidValue(findPal, findValue, OidGroup.Policy);
findPal.FindByApplicationPolicy(oidValue);
break;
}
case X509FindType.FindByCertificatePolicy:
{
string oidValue = ConfirmedOidValue(findPal, findValue, OidGroup.Policy);
findPal.FindByCertificatePolicy(oidValue);
break;
}
case X509FindType.FindByExtension:
{
string oidValue = ConfirmedOidValue(findPal, findValue, OidGroup.ExtensionOrAttribute);
findPal.FindByExtension(oidValue);
break;
}
case X509FindType.FindByKeyUsage:
{
X509KeyUsageFlags keyUsage = ConfirmedX509KeyUsage(findValue);
findPal.FindByKeyUsage(keyUsage);
break;
}
case X509FindType.FindBySubjectKeyIdentifier:
{
byte[] keyIdentifier = ConfirmedCast<string>(findValue).DecodeHexString();
findPal.FindBySubjectKeyIdentifier(keyIdentifier);
break;
}
default:
throw new CryptographicException(SR.Cryptography_X509_InvalidFindType);
}
}
return results;
}
private static T ConfirmedCast<T>(object findValue)
{
Debug.Assert(findValue != null);
if (findValue.GetType() != typeof(T))
throw new CryptographicException(SR.Cryptography_X509_InvalidFindValue);
return (T)findValue;
}
private static string ConfirmedOidValue(IFindPal findPal, object findValue, OidGroup oidGroup)
{
string maybeOid = ConfirmedCast<string>(findValue);
if (maybeOid.Length == 0)
{
throw new ArgumentException(SR.Argument_InvalidOidValue);
}
return findPal.NormalizeOid(maybeOid, oidGroup);
}
private static X509KeyUsageFlags ConfirmedX509KeyUsage(object findValue)
{
if (findValue is X509KeyUsageFlags)
return (X509KeyUsageFlags)findValue;
if (findValue is int)
return (X509KeyUsageFlags)(int)findValue;
if (findValue is uint)
return (X509KeyUsageFlags)(uint)findValue;
string findValueString = findValue as string;
if (findValueString != null)
{
X509KeyUsageFlags usageFlags;
if (s_keyUsages.TryGetValue(findValueString, out usageFlags))
{
return usageFlags;
}
}
throw new CryptographicException(SR.Cryptography_X509_InvalidFindValue);
}
//
// verify the passed keyValue is valid as per X.208
//
// The first number must be 0, 1 or 2.
// Enforce all characters are digits and dots.
// Enforce that no dot starts or ends the Oid, and disallow double dots.
// Enforce there is at least one dot separator.
//
internal static void ValidateOidValue(string keyValue)
{
if (keyValue == null)
throw new ArgumentNullException("keyValue");
int len = keyValue.Length;
if (len < 2)
throw new ArgumentException(SR.Argument_InvalidOidValue);
// should not start with a dot. The first digit must be 0, 1 or 2.
char c = keyValue[0];
if (c != '0' && c != '1' && c != '2')
throw new ArgumentException(SR.Argument_InvalidOidValue);
if (keyValue[1] != '.' || keyValue[len - 1] == '.') // should not end in a dot
throw new ArgumentException(SR.Argument_InvalidOidValue);
// While characters 0 and 1 were both validated, start at character 1 to validate
// that there aren't two dots in a row.
for (int i = 1; i < len; i++)
{
// ensure every character is either a digit or a dot
if (char.IsDigit(keyValue[i]))
continue;
if (keyValue[i] != '.' || keyValue[i + 1] == '.') // disallow double dots
throw new ArgumentException(SR.Argument_InvalidOidValue);
}
}
internal static BigInteger PositiveBigIntegerFromByteArray(byte[] bytes)
{
// To prevent the big integer from misinterpreted as a negative number,
// add a "leading 0" to the byte array if it would considered negative.
//
// Since BigInteger(bytes[]) requires a little-endian byte array,
// the "leading 0" actually goes at the end of the array.
// An empty array is 0 (non-negative), so no over-allocation is required.
//
// If the last indexed value doesn't have the sign bit set (0x00-0x7F) then
// the number would be positive anyways, so no over-allocation is required.
if (bytes.Length == 0 || bytes[bytes.Length - 1] < 0x80)
{
return new BigInteger(bytes);
}
// Since the sign bit is set, put a new 0x00 on the end to move that bit from
// the sign bit to a data bit.
byte[] newBytes = new byte[bytes.Length + 1];
Array.Copy(bytes, 0, newBytes, 0, bytes.Length);
return new BigInteger(newBytes);
}
private static BigInteger LaxParseDecimalBigInteger(string decimalString)
{
BigInteger ten = new BigInteger(10);
BigInteger accum = BigInteger.Zero;
foreach (char c in decimalString)
{
if (c >= '0' && c <= '9')
{
accum = BigInteger.Multiply(accum, ten);
accum = BigInteger.Add(accum, c - '0');
}
}
return accum;
}
}
}