| | | 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.Globalization; |
| | | 6 | | using System.Security.Cryptography; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Security; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 11 | | { |
| | | 12 | | public class RsaKeyIdentifierClause : SecurityKeyIdentifierClause |
| | | 13 | | { |
| | 0 | 14 | | private static readonly string s_clauseType = XmlSignatureStrings.Namespace + XmlSignatureStrings.RsaKeyValue; |
| | | 15 | | private readonly RSAParameters _rsaParameters; |
| | | 16 | | private RsaSecurityKey _rsaSecurityKey; |
| | | 17 | | |
| | | 18 | | public RsaKeyIdentifierClause(RSA rsa) |
| | 0 | 19 | | : base(s_clauseType) |
| | | 20 | | { |
| | 0 | 21 | | Rsa = rsa ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rsa)); |
| | 0 | 22 | | _rsaParameters = rsa.ExportParameters(false); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | public override bool CanCreateKey |
| | | 26 | | { |
| | 0 | 27 | | get { return true; } |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | public RSA Rsa { get; } |
| | | 31 | | |
| | | 32 | | public override SecurityKey CreateKey() |
| | | 33 | | { |
| | 0 | 34 | | if (_rsaSecurityKey == null) |
| | | 35 | | { |
| | 0 | 36 | | _rsaSecurityKey = new RsaSecurityKey(Rsa); |
| | | 37 | | } |
| | 0 | 38 | | return _rsaSecurityKey; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public byte[] GetExponent() |
| | | 42 | | { |
| | 0 | 43 | | return SecurityUtils.CloneBuffer(_rsaParameters.Exponent); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public byte[] GetModulus() |
| | | 47 | | { |
| | 0 | 48 | | return SecurityUtils.CloneBuffer(_rsaParameters.Modulus); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public override bool Matches(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 52 | | { |
| | 0 | 53 | | RsaKeyIdentifierClause that = keyIdentifierClause as RsaKeyIdentifierClause; |
| | 0 | 54 | | return ReferenceEquals(this, that) || (that != null && that.Matches(Rsa)); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public bool Matches(RSA rsa) |
| | | 58 | | { |
| | 0 | 59 | | if (rsa == null) |
| | | 60 | | { |
| | 0 | 61 | | return false; |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | RSAParameters rsaParameters = rsa.ExportParameters(false); |
| | 0 | 65 | | return SecurityUtils.MatchesBuffer(_rsaParameters.Modulus, rsaParameters.Modulus) && |
| | 0 | 66 | | SecurityUtils.MatchesBuffer(_rsaParameters.Exponent, rsaParameters.Exponent); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public override string ToString() |
| | | 70 | | { |
| | 0 | 71 | | return string.Format(CultureInfo.InvariantCulture, "RsaKeyIdentifierClause(Modulus = {0}, Exponent = {1})", |
| | 0 | 72 | | Convert.ToBase64String(_rsaParameters.Modulus), |
| | 0 | 73 | | Convert.ToBase64String(_rsaParameters.Exponent)); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public void WriteExponentAsBase64(XmlWriter writer) |
| | | 77 | | { |
| | 0 | 78 | | if (writer == null) |
| | | 79 | | { |
| | 0 | 80 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 81 | | } |
| | 0 | 82 | | writer.WriteBase64(_rsaParameters.Exponent, 0, _rsaParameters.Exponent.Length); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public void WriteModulusAsBase64(XmlWriter writer) |
| | | 86 | | { |
| | 0 | 87 | | if (writer == null) |
| | | 88 | | { |
| | 0 | 89 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 90 | | } |
| | 0 | 91 | | writer.WriteBase64(_rsaParameters.Modulus, 0, _rsaParameters.Modulus.Length); |
| | 0 | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |