| | | 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.Security.Cryptography.X509Certificates; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.IdentityModel.Claims; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF |
| | | 10 | | { |
| | | 11 | | public class X509CertificateEndpointIdentity : EndpointIdentity |
| | | 12 | | { |
| | 0 | 13 | | public X509CertificateEndpointIdentity(X509Certificate2 certificate) |
| | | 14 | | { |
| | 0 | 15 | | if (certificate == null) |
| | | 16 | | { |
| | 0 | 17 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 18 | | } |
| | | 19 | | |
| | 0 | 20 | | Initialize(new Claim(ClaimTypes.Thumbprint, certificate.GetCertHash(), Rights.PossessProperty)); |
| | | 21 | | |
| | 0 | 22 | | Certificates.Add(certificate); |
| | 0 | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | public X509CertificateEndpointIdentity(X509Certificate2 primaryCertificate, X509Certificate2Collection supportin |
| | | 26 | | { |
| | 0 | 27 | | if (primaryCertificate == null) |
| | | 28 | | { |
| | 0 | 29 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(primaryCertificate)); |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | if (supportingCertificates == null) |
| | | 33 | | { |
| | 0 | 34 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(supportingCertificates)); |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | Initialize(new Claim(ClaimTypes.Thumbprint, primaryCertificate.GetCertHash(), Rights.PossessProperty)); |
| | | 38 | | |
| | 0 | 39 | | Certificates.Add(primaryCertificate); |
| | | 40 | | |
| | 0 | 41 | | for (int i = 0; i < supportingCertificates.Count; ++i) |
| | | 42 | | { |
| | 0 | 43 | | Certificates.Add(supportingCertificates[i]); |
| | | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | internal X509CertificateEndpointIdentity(XmlDictionaryReader reader) |
| | | 48 | | { |
| | 0 | 49 | | if (reader == null) |
| | | 50 | | { |
| | 0 | 51 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | reader.MoveToContent(); |
| | 0 | 55 | | if (reader.IsEmptyElement) |
| | | 56 | | { |
| | 0 | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnexpectedEmptyE |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | reader.ReadStartElement(XD.XmlSignatureDictionary.X509Data, XD.XmlSignatureDictionary.Namespace); |
| | 0 | 61 | | while (reader.IsStartElement(XD.XmlSignatureDictionary.X509Certificate, XD.XmlSignatureDictionary.Namespace) |
| | | 62 | | { |
| | 0 | 63 | | X509Certificate2 certificate = new X509Certificate2(Convert.FromBase64String(reader.ReadElementString()) |
| | 0 | 64 | | if (Certificates.Count == 0) |
| | | 65 | | { |
| | | 66 | | // This is the first certificate. We assume this as the primary |
| | | 67 | | // certificate and initialize the base class. |
| | 0 | 68 | | Initialize(new Claim(ClaimTypes.Thumbprint, certificate.GetCertHash(), Rights.PossessProperty)); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | Certificates.Add(certificate); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | reader.ReadEndElement(); |
| | | 75 | | |
| | 0 | 76 | | if (Certificates.Count == 0) |
| | | 77 | | { |
| | 0 | 78 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnexpectedEmptyE |
| | | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | public X509Certificate2Collection Certificates { get; } = new X509Certificate2Collection(); |
| | | 83 | | |
| | | 84 | | internal override void WriteContentsTo(XmlDictionaryWriter writer) |
| | | 85 | | { |
| | 0 | 86 | | if (writer == null) |
| | | 87 | | { |
| | 0 | 88 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | writer.WriteStartElement(XD.XmlSignatureDictionary.Prefix.Value, XD.XmlSignatureDictionary.KeyInfo, XD.XmlSi |
| | 0 | 92 | | writer.WriteStartElement(XD.XmlSignatureDictionary.Prefix.Value, XD.XmlSignatureDictionary.X509Data, XD.XmlS |
| | 0 | 93 | | for (int i = 0; i < Certificates.Count; ++i) |
| | | 94 | | { |
| | 0 | 95 | | writer.WriteElementString(XD.XmlSignatureDictionary.X509Certificate, XD.XmlSignatureDictionary.Namespace |
| | | 96 | | } |
| | 0 | 97 | | writer.WriteEndElement(); |
| | 0 | 98 | | writer.WriteEndElement(); |
| | 0 | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |