-
Notifications
You must be signed in to change notification settings - Fork 411
/
X509SecurityKey.cs
224 lines (202 loc) · 8.18 KB
/
X509SecurityKey.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
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//------------------------------------------------------------------------------
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Microsoft.IdentityModel.Logging;
namespace Microsoft.IdentityModel.Tokens
{
/// <summary>
/// An <see cref="AsymmetricSecurityKey"/> that is backed by a <see cref="X509Certificate2"/>
/// </summary>
public class X509SecurityKey : AsymmetricSecurityKey
{
AsymmetricAlgorithm _privateKey;
bool _privateKeyAvailabilityDetermined;
AsymmetricAlgorithm _publicKey;
object _thisLock = new Object();
internal X509SecurityKey(JsonWebKey webKey)
: base(webKey)
{
Certificate = new X509Certificate2(Convert.FromBase64String(webKey.X5c[0]));
X5t = Base64UrlEncoder.Encode(Certificate.GetCertHash());
webKey.ConvertedSecurityKey = this;
}
/// <summary>
/// Instantiates a <see cref="X509SecurityKey"/> using a <see cref="X509Certificate2"/>
/// </summary>
/// <param name="certificate">The <see cref="X509Certificate2"/> to use.</param>
/// <exception cref="ArgumentNullException">if <paramref name="certificate"/> is null.</exception>
public X509SecurityKey(X509Certificate2 certificate)
{
Certificate = certificate ?? throw LogHelper.LogArgumentNullException(nameof(certificate));
KeyId = certificate.Thumbprint;
X5t = Base64UrlEncoder.Encode(certificate.GetCertHash());
}
/// <summary>
/// Instantiates a <see cref="X509SecurityKey"/> using a <see cref="X509Certificate2"/>.
/// </summary>
/// <param name="certificate">The <see cref="X509Certificate2"/> to use.</param>
/// <param name="keyId">The value to set for the KeyId</param>
/// <exception cref="ArgumentNullException">if <paramref name="certificate"/> is null.</exception>
/// <exception cref="ArgumentNullException">if <paramref name="keyId"/> is null or empty.</exception>
public X509SecurityKey(X509Certificate2 certificate, string keyId)
{
Certificate = certificate ?? throw LogHelper.LogArgumentNullException(nameof(certificate));
KeyId = string.IsNullOrEmpty(keyId) ? throw LogHelper.LogArgumentNullException(nameof(keyId)) : keyId;
X5t = Base64UrlEncoder.Encode(certificate.GetCertHash());
}
/// <summary>
/// Gets the key size.
/// </summary>
public override int KeySize
{
get => PublicKey.KeySize;
}
/// <summary>
/// Gets the X5t of this <see cref="X509SecurityKey"/>.
/// </summary>
public string X5t { get; }
/// <summary>
/// Returns the private key from the <see cref="X509SecurityKey"/>.
/// </summary>
public AsymmetricAlgorithm PrivateKey
{
get
{
if (!_privateKeyAvailabilityDetermined)
{
lock (ThisLock)
{
if (!_privateKeyAvailabilityDetermined)
{
#if NET461 || NETSTANDARD2_0
_privateKey = RSACertificateExtensions.GetRSAPrivateKey(Certificate);
#else
_privateKey = Certificate.PrivateKey;
#endif
_privateKeyAvailabilityDetermined = true;
}
}
}
return _privateKey;
}
}
/// <summary>
/// Gets the public key from the <see cref="X509SecurityKey"/>.
/// </summary>
public AsymmetricAlgorithm PublicKey
{
get
{
if (_publicKey == null)
{
lock (ThisLock)
{
if (_publicKey == null)
{
#if NET461 || NETSTANDARD2_0
_publicKey = RSACertificateExtensions.GetRSAPublicKey(Certificate);
#else
_publicKey = Certificate.PublicKey.Key;
#endif
}
}
}
return _publicKey;
}
}
object ThisLock
{
get { return _thisLock; }
}
/// <summary>
/// Gets a bool indicating if a private key exists.
/// </summary>
/// <return>true if it has a private key; otherwise, false.</return>
[System.Obsolete("HasPrivateKey method is deprecated, please use PrivateKeyStatus.")]
public override bool HasPrivateKey
{
get { return (PrivateKey != null); }
}
/// <summary>
/// Gets an enum indicating if a private key exists.
/// </summary>
/// <return>'Exists' if private key exists for sure; 'DoesNotExist' if private key doesn't exist for sure; 'Unknown' if we cannot determine.</return>
public override PrivateKeyStatus PrivateKeyStatus
{
get
{
return PrivateKey == null ? PrivateKeyStatus.DoesNotExist : PrivateKeyStatus.Exists;
}
}
/// <summary>
/// Gets the <see cref="X509Certificate2"/>.
/// </summary>
public X509Certificate2 Certificate
{
get; private set;
}
internal override string InternalId => X5t;
/// <summary>
/// Determines whether the <see cref="X509SecurityKey"/> can compute a JWK thumbprint.
/// </summary>
/// <returns><c>true</c> if JWK thumbprint can be computed; otherwise, <c>false</c>.</returns>
/// <remarks>https://tools.ietf.org/html/rfc7638</remarks>
public override bool CanComputeJwkThumbprint()
{
return (PublicKey as RSA) != null ? true : false;
}
/// <summary>
/// Computes a sha256 hash over the <see cref="X509SecurityKey"/>.
/// </summary>
/// <returns>A JWK thumbprint.</returns>
/// <remarks>https://tools.ietf.org/html/rfc7638</remarks>
public override byte[] ComputeJwkThumbprint()
{
return new RsaSecurityKey(PublicKey as RSA).ComputeJwkThumbprint();
}
/// <summary>
/// Returns a bool indicating if this key is equivalent to another key.
/// </summary>
/// <return>true if the keys are equal; otherwise, false.</return>
public override bool Equals(object obj)
{
if (!(obj is X509SecurityKey other))
return false;
return other.Certificate.Thumbprint.ToString() == Certificate.Thumbprint.ToString();
}
/// <summary>
/// Returns an int hash code.
/// </summary>
/// <return>An int hash code</return>
public override int GetHashCode()
{
return Certificate.GetHashCode();
}
}
}