| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Security.Cryptography.X509Certificates; |
| | | 8 | | using CoreWCF.Security; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 11 | | { |
| | | 12 | | public class X509SecurityToken : SecurityToken, IDisposable |
| | | 13 | | { |
| | | 14 | | private readonly string _id; |
| | | 15 | | private readonly X509Certificate2 _certificate; |
| | | 16 | | private ReadOnlyCollection<SecurityKey> _securityKeys; |
| | 15 | 17 | | private DateTime _effectiveTime = SecurityUtils.MaxUtcDateTime; |
| | 15 | 18 | | private DateTime _expirationTime = SecurityUtils.MinUtcDateTime; |
| | | 19 | | private bool _disposed = false; |
| | | 20 | | private readonly bool _disposable; |
| | | 21 | | |
| | | 22 | | public X509SecurityToken(X509Certificate2 certificate) |
| | 5 | 23 | | : this(certificate, SecurityUniqueId.Create().Value) |
| | | 24 | | { |
| | 5 | 25 | | } |
| | | 26 | | |
| | | 27 | | public X509SecurityToken(X509Certificate2 certificate, string id) |
| | 5 | 28 | | : this(certificate, id, true) |
| | | 29 | | { |
| | 5 | 30 | | } |
| | | 31 | | |
| | | 32 | | public X509SecurityToken(X509Certificate2 certificate, bool clone) |
| | 2 | 33 | | : this(certificate, SecurityUniqueId.Create().Value, clone) |
| | | 34 | | { |
| | 2 | 35 | | } |
| | | 36 | | |
| | | 37 | | internal X509SecurityToken(X509Certificate2 certificate, bool clone, bool disposable) |
| | 5 | 38 | | : this(certificate, SecurityUniqueId.Create().Value, clone, disposable) |
| | | 39 | | { |
| | 5 | 40 | | } |
| | | 41 | | |
| | | 42 | | internal X509SecurityToken(X509Certificate2 certificate, string id, bool clone) |
| | 10 | 43 | | : this(certificate, id, clone, true) |
| | | 44 | | { |
| | 10 | 45 | | } |
| | | 46 | | |
| | 15 | 47 | | internal X509SecurityToken(X509Certificate2 certificate, string id, bool clone, bool disposable) |
| | | 48 | | { |
| | 15 | 49 | | if (certificate == null) |
| | | 50 | | { |
| | 0 | 51 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 52 | | } |
| | | 53 | | |
| | 15 | 54 | | _id = id ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id)); |
| | 15 | 55 | | _certificate = clone ? new X509Certificate2(certificate) : certificate; |
| | | 56 | | // if the cert needs to be cloned then the token owns the clone and should dispose it |
| | 15 | 57 | | _disposable = clone || disposable; |
| | 15 | 58 | | } |
| | | 59 | | |
| | | 60 | | public override string Id |
| | | 61 | | { |
| | 6 | 62 | | get { return _id; } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public override ReadOnlyCollection<SecurityKey> SecurityKeys |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | 8 | 69 | | ThrowIfDisposed(); |
| | 8 | 70 | | if (_securityKeys == null) |
| | | 71 | | { |
| | 4 | 72 | | List<SecurityKey> temp = new List<SecurityKey>(1) |
| | 4 | 73 | | { |
| | 4 | 74 | | new X509AsymmetricSecurityKey(_certificate) |
| | 4 | 75 | | }; |
| | 4 | 76 | | _securityKeys = temp.AsReadOnly(); |
| | | 77 | | } |
| | 8 | 78 | | return _securityKeys; |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public override DateTime ValidFrom |
| | | 83 | | { |
| | | 84 | | get |
| | | 85 | | { |
| | 0 | 86 | | ThrowIfDisposed(); |
| | 0 | 87 | | if (_effectiveTime == SecurityUtils.MaxUtcDateTime) |
| | | 88 | | { |
| | 0 | 89 | | _effectiveTime = _certificate.NotBefore.ToUniversalTime(); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | return _effectiveTime; |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public override DateTime ValidTo |
| | | 97 | | { |
| | | 98 | | get |
| | | 99 | | { |
| | 5 | 100 | | ThrowIfDisposed(); |
| | 5 | 101 | | if (_expirationTime == SecurityUtils.MinUtcDateTime) |
| | | 102 | | { |
| | 5 | 103 | | _expirationTime = _certificate.NotAfter.ToUniversalTime(); |
| | | 104 | | } |
| | | 105 | | |
| | 5 | 106 | | return _expirationTime; |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public X509Certificate2 Certificate |
| | | 111 | | { |
| | | 112 | | get |
| | | 113 | | { |
| | 138 | 114 | | ThrowIfDisposed(); |
| | 138 | 115 | | return _certificate; |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | public override bool CanCreateKeyIdentifierClause<T>() |
| | | 120 | | { |
| | 0 | 121 | | ThrowIfDisposed(); |
| | 0 | 122 | | if (typeof(T) == typeof(X509SubjectKeyIdentifierClause)) |
| | 0 | 123 | | return X509SubjectKeyIdentifierClause.CanCreateFrom(_certificate); |
| | | 124 | | |
| | 0 | 125 | | return typeof(T) == typeof(X509ThumbprintKeyIdentifierClause) || |
| | 0 | 126 | | typeof(T) == typeof(X509IssuerSerialKeyIdentifierClause) || |
| | 0 | 127 | | typeof(T) == typeof(X509RawDataKeyIdentifierClause) || |
| | 0 | 128 | | base.CanCreateKeyIdentifierClause<T>(); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public override T CreateKeyIdentifierClause<T>() |
| | | 132 | | { |
| | 0 | 133 | | ThrowIfDisposed(); |
| | 0 | 134 | | if (typeof(T) == typeof(X509SubjectKeyIdentifierClause)) |
| | | 135 | | { |
| | | 136 | | X509SubjectKeyIdentifierClause x509KeyIdentifierClause; |
| | 0 | 137 | | if (X509SubjectKeyIdentifierClause.TryCreateFrom(_certificate, out x509KeyIdentifierClause)) |
| | 0 | 138 | | return x509KeyIdentifierClause as T; |
| | | 139 | | } |
| | 0 | 140 | | else if (typeof(T) == typeof(X509ThumbprintKeyIdentifierClause)) |
| | | 141 | | { |
| | 0 | 142 | | return new X509ThumbprintKeyIdentifierClause(_certificate) as T; |
| | | 143 | | } |
| | 0 | 144 | | else if (typeof(T) == typeof(X509IssuerSerialKeyIdentifierClause)) |
| | | 145 | | { |
| | 0 | 146 | | return new X509IssuerSerialKeyIdentifierClause(_certificate) as T; |
| | | 147 | | } |
| | 0 | 148 | | else if (typeof(T) == typeof(X509RawDataKeyIdentifierClause)) |
| | | 149 | | { |
| | 0 | 150 | | return new X509RawDataKeyIdentifierClause(_certificate) as T; |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | return base.CreateKeyIdentifierClause<T>(); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | public override bool MatchesKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 157 | | { |
| | 56 | 158 | | ThrowIfDisposed(); |
| | 56 | 159 | | X509SubjectKeyIdentifierClause subjectKeyIdentifierClause = keyIdentifierClause as X509SubjectKeyIdentifierC |
| | 56 | 160 | | if (subjectKeyIdentifierClause != null) |
| | 3 | 161 | | return subjectKeyIdentifierClause.Matches(_certificate); |
| | | 162 | | |
| | 53 | 163 | | X509ThumbprintKeyIdentifierClause thumbprintKeyIdentifierClause = keyIdentifierClause as X509ThumbprintKeyId |
| | 53 | 164 | | if (thumbprintKeyIdentifierClause != null) |
| | 0 | 165 | | return thumbprintKeyIdentifierClause.Matches(_certificate); |
| | | 166 | | |
| | 53 | 167 | | X509IssuerSerialKeyIdentifierClause issuerKeyIdentifierClause = keyIdentifierClause as X509IssuerSerialKeyId |
| | 53 | 168 | | if (issuerKeyIdentifierClause != null) |
| | 0 | 169 | | return issuerKeyIdentifierClause.Matches(_certificate); |
| | | 170 | | |
| | 53 | 171 | | X509RawDataKeyIdentifierClause rawCertKeyIdentifierClause = keyIdentifierClause as X509RawDataKeyIdentifierC |
| | 53 | 172 | | if (rawCertKeyIdentifierClause != null) |
| | 50 | 173 | | return rawCertKeyIdentifierClause.Matches(_certificate); |
| | | 174 | | |
| | 3 | 175 | | return base.MatchesKeyIdentifierClause(keyIdentifierClause); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | public virtual void Dispose() |
| | | 179 | | { |
| | 0 | 180 | | if (_disposable && !_disposed) |
| | | 181 | | { |
| | 0 | 182 | | _disposed = true; |
| | 0 | 183 | | _certificate.Reset(); |
| | | 184 | | } |
| | 0 | 185 | | } |
| | | 186 | | |
| | | 187 | | protected void ThrowIfDisposed() |
| | | 188 | | { |
| | 207 | 189 | | if (_disposed) |
| | | 190 | | { |
| | 0 | 191 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().FullName |
| | | 192 | | } |
| | 207 | 193 | | } |
| | | 194 | | } |
| | | 195 | | } |