| | | 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.Collections.Generic; |
| | | 5 | | using System.Security.Cryptography.X509Certificates; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.IdentityModel.Selectors; |
| | | 8 | | using CoreWCF.Security; |
| | | 9 | | using KeyIdentifierEntry = CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer.KeyIdentifierEntry; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 12 | | { |
| | | 13 | | internal class XmlDsigSep2000 : SecurityTokenSerializer.SerializerEntries |
| | | 14 | | { |
| | | 15 | | private readonly KeyInfoSerializer _securityTokenSerializer; |
| | | 16 | | |
| | 134 | 17 | | public XmlDsigSep2000(KeyInfoSerializer securityTokenSerializer) |
| | | 18 | | { |
| | 134 | 19 | | _securityTokenSerializer = securityTokenSerializer; |
| | 134 | 20 | | } |
| | | 21 | | |
| | | 22 | | public override void PopulateKeyIdentifierEntries(IList<KeyIdentifierEntry> keyIdentifierEntries) |
| | | 23 | | { |
| | 134 | 24 | | keyIdentifierEntries.Add(new KeyInfoEntry(_securityTokenSerializer)); |
| | 134 | 25 | | } |
| | | 26 | | |
| | | 27 | | public override void PopulateKeyIdentifierClauseEntries(IList<SecurityTokenSerializer.KeyIdentifierClauseEntry> |
| | | 28 | | { |
| | 134 | 29 | | keyIdentifierClauseEntries.Add(new X509CertificateClauseEntry()); |
| | 134 | 30 | | } |
| | | 31 | | |
| | | 32 | | internal class KeyInfoEntry : KeyIdentifierEntry |
| | | 33 | | { |
| | | 34 | | private readonly KeyInfoSerializer _securityTokenSerializer; |
| | | 35 | | |
| | 134 | 36 | | public KeyInfoEntry(KeyInfoSerializer securityTokenSerializer) |
| | | 37 | | { |
| | 134 | 38 | | _securityTokenSerializer = securityTokenSerializer; |
| | 134 | 39 | | } |
| | | 40 | | |
| | | 41 | | protected override XmlDictionaryString LocalName |
| | | 42 | | { |
| | | 43 | | get |
| | | 44 | | { |
| | 0 | 45 | | return CoreWCF.XD.XmlSignatureDictionary.KeyInfo; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | protected override XmlDictionaryString NamespaceUri |
| | | 50 | | { |
| | | 51 | | get |
| | | 52 | | { |
| | 0 | 53 | | return CoreWCF.XD.XmlSignatureDictionary.Namespace; |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlDictionaryReader reader) |
| | | 58 | | { |
| | 0 | 59 | | reader.ReadStartElement(LocalName, NamespaceUri); |
| | 0 | 60 | | SecurityKeyIdentifier keyIdentifier = new SecurityKeyIdentifier(); |
| | 0 | 61 | | while (reader.IsStartElement()) |
| | | 62 | | { |
| | 0 | 63 | | SecurityKeyIdentifierClause clause = _securityTokenSerializer.ReadKeyIdentifierClause(reader); |
| | 0 | 64 | | if (clause == null) |
| | | 65 | | { |
| | 0 | 66 | | reader.Skip(); |
| | | 67 | | } |
| | | 68 | | else |
| | | 69 | | { |
| | 0 | 70 | | keyIdentifier.Add(clause); |
| | | 71 | | } |
| | | 72 | | } |
| | 0 | 73 | | if (keyIdentifier.Count == 0) |
| | | 74 | | { |
| | 0 | 75 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.ErrorDeserializingKeyI |
| | | 76 | | } |
| | 0 | 77 | | reader.ReadEndElement(); |
| | 0 | 78 | | return keyIdentifier; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public override bool SupportsCore(SecurityKeyIdentifier keyIdentifier) |
| | | 82 | | { |
| | 0 | 83 | | return true; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public override void WriteKeyIdentifierCore(XmlDictionaryWriter writer, SecurityKeyIdentifier keyIdentifier) |
| | | 87 | | { |
| | 0 | 88 | | writer.WriteStartElement(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, LocalName, NamespaceUri); |
| | 0 | 89 | | bool clauseWritten = false; |
| | 0 | 90 | | foreach (SecurityKeyIdentifierClause clause in keyIdentifier) |
| | | 91 | | { |
| | 0 | 92 | | _securityTokenSerializer.InnerSecurityTokenSerializer.WriteKeyIdentifierClause(writer, clause); |
| | 0 | 93 | | clauseWritten = true; |
| | | 94 | | } |
| | 0 | 95 | | writer.WriteEndElement(); // KeyInfo |
| | 0 | 96 | | if (!clauseWritten) |
| | | 97 | | { |
| | 0 | 98 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityMessageSerializationException( |
| | | 99 | | } |
| | 0 | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | // so far, we only support two types of X509Data directly under KeyInfo - X509Certificate and X509SKI |
| | | 104 | | // <ds:X509Data> |
| | | 105 | | // <ds:X509Certificate>...</ds:X509Certificate> |
| | | 106 | | // or |
| | | 107 | | // <X509SKI>... </X509SKI> |
| | | 108 | | // </ds:X509Data> |
| | | 109 | | // only support 1 certificate right now |
| | | 110 | | internal class X509CertificateClauseEntry : SecurityTokenSerializer.KeyIdentifierClauseEntry |
| | | 111 | | { |
| | | 112 | | protected override XmlDictionaryString LocalName |
| | | 113 | | { |
| | | 114 | | get |
| | | 115 | | { |
| | 45 | 116 | | return CoreWCF.XD.XmlSignatureDictionary.X509Data; |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | protected override XmlDictionaryString NamespaceUri |
| | | 121 | | { |
| | | 122 | | get |
| | | 123 | | { |
| | 45 | 124 | | return CoreWCF.XD.XmlSignatureDictionary.Namespace; |
| | | 125 | | } |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | public override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlDictionaryReader reader) |
| | | 129 | | { |
| | 0 | 130 | | SecurityKeyIdentifierClause ski = null; |
| | 0 | 131 | | reader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.X509Data, NamespaceUri); |
| | 0 | 132 | | while (reader.IsStartElement()) |
| | | 133 | | { |
| | 0 | 134 | | if (ski == null && reader.IsStartElement(CoreWCF.XD.XmlSignatureDictionary.X509Certificate, Namespac |
| | | 135 | | { |
| | 0 | 136 | | if (!SecurityUtils.TryCreateX509CertificateFromRawData(reader.ReadElementContentAsBase64(), out |
| | | 137 | | { |
| | 0 | 138 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityMessageSerializationEx |
| | | 139 | | } |
| | 0 | 140 | | ski = new X509RawDataKeyIdentifierClause(certificate); |
| | | 141 | | } |
| | 0 | 142 | | else if (ski == null && reader.IsStartElement(XmlSignatureStrings.X509Ski, NamespaceUri.ToString())) |
| | | 143 | | { |
| | 0 | 144 | | ski = new X509SubjectKeyIdentifierClause(reader.ReadElementContentAsBase64()); |
| | | 145 | | } |
| | 0 | 146 | | else if ((ski == null) && reader.IsStartElement(CoreWCF.XD.XmlSignatureDictionary.X509IssuerSerial, |
| | | 147 | | { |
| | 0 | 148 | | reader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.X509IssuerSerial, CoreWCF.XD.XmlSignat |
| | 0 | 149 | | reader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.X509IssuerName, CoreWCF.XD.XmlSignatur |
| | 0 | 150 | | string issuerName = reader.ReadContentAsString(); |
| | 0 | 151 | | reader.ReadEndElement(); |
| | 0 | 152 | | reader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.X509SerialNumber, CoreWCF.XD.XmlSignat |
| | 0 | 153 | | string serialNumber = reader.ReadContentAsString(); |
| | 0 | 154 | | reader.ReadEndElement(); |
| | 0 | 155 | | reader.ReadEndElement(); |
| | | 156 | | |
| | 0 | 157 | | ski = new X509IssuerSerialKeyIdentifierClause(issuerName, serialNumber); |
| | | 158 | | } |
| | | 159 | | else |
| | | 160 | | { |
| | 0 | 161 | | reader.Skip(); |
| | | 162 | | } |
| | | 163 | | } |
| | 0 | 164 | | reader.ReadEndElement(); |
| | 0 | 165 | | return ski; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | public override bool SupportsCore(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 169 | | { |
| | 20 | 170 | | return (keyIdentifierClause is X509RawDataKeyIdentifierClause); |
| | | 171 | | // This method should not write X509IssuerSerialKeyIdentifierClause or X509SubjectKeyIdentifierClause as |
| | | 172 | | // The XmlDsig entries are written by the X509SecurityTokenHandler. |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause ke |
| | | 176 | | { |
| | 0 | 177 | | if (keyIdentifierClause is X509RawDataKeyIdentifierClause x509Clause) |
| | | 178 | | { |
| | 0 | 179 | | writer.WriteStartElement(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDict |
| | | 180 | | |
| | 0 | 181 | | writer.WriteStartElement(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDict |
| | 0 | 182 | | byte[] certBytes = x509Clause.GetX509RawData(); |
| | 0 | 183 | | writer.WriteBase64(certBytes, 0, certBytes.Length); |
| | 0 | 184 | | writer.WriteEndElement(); |
| | | 185 | | |
| | 0 | 186 | | writer.WriteEndElement(); |
| | | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | if (keyIdentifierClause is X509IssuerSerialKeyIdentifierClause issuerSerialClause) |
| | | 190 | | { |
| | 0 | 191 | | writer.WriteStartElement(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDict |
| | 0 | 192 | | writer.WriteStartElement(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDict |
| | 0 | 193 | | writer.WriteElementString(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDic |
| | 0 | 194 | | writer.WriteElementString(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDic |
| | 0 | 195 | | writer.WriteEndElement(); |
| | 0 | 196 | | writer.WriteEndElement(); |
| | 0 | 197 | | return; |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | if (keyIdentifierClause is X509SubjectKeyIdentifierClause skiClause) |
| | | 201 | | { |
| | 0 | 202 | | writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Data, XmlS |
| | 0 | 203 | | writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509SKI, XmlSi |
| | 0 | 204 | | byte[] ski = skiClause.GetX509SubjectKeyIdentifier(); |
| | 0 | 205 | | writer.WriteBase64(ski, 0, ski.Length); |
| | 0 | 206 | | writer.WriteEndElement(); |
| | 0 | 207 | | writer.WriteEndElement(); |
| | 0 | 208 | | return; |
| | | 209 | | } |
| | 0 | 210 | | } |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | } |