| | | 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.Xml; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Implementation of SecurityKeyIdentifierClauseSerializer that handles X.509 Certificate |
| | | 12 | | /// reference types. |
| | | 13 | | /// </summary> |
| | | 14 | | public class X509DataSecurityKeyIdentifierClauseSerializer : SecurityKeyIdentifierClauseSerializer |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Checks if the given reader is referring to a <ds:X509Data> element. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name=nameof(reader)>XmlReader positioned at the SecurityKeyIdentifierClause. </param> |
| | | 20 | | /// <returns>True if the XmlReader is referring to a <ds:X509Data> element.</returns> |
| | | 21 | | /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception> |
| | | 22 | | public override bool CanReadKeyIdentifierClause(XmlReader reader) |
| | | 23 | | { |
| | 0 | 24 | | if (reader == null) |
| | | 25 | | { |
| | 0 | 26 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | return reader.IsStartElement(XmlSignatureConstants.Elements.X509Data, XmlSignatureConstants.Namespace); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Checks if the given SecurityKeyIdentifierClause can be serialized. The |
| | | 34 | | /// supported SecurityKeyIdentifierClause are, |
| | | 35 | | /// 1. <see cref="System.IdentityModel.Tokens.X509IssuerSerialKeyIdentifierClause"/> |
| | | 36 | | /// 2. <see cref="System.IdentityModel.Tokens.X509RawDataKeyIdentifierClause"/> |
| | | 37 | | /// 3. <see cref="System.IdentityModel.Tokens.X509SubjectKeyIdentifierClause"/> |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="securityKeyIdentifierClause">SecurityKeyIdentifierClause to be serialized.</param> |
| | | 40 | | /// <returns>True if the 'securityKeyIdentifierClause' is supported.</returns> |
| | | 41 | | /// <exception cref="ArgumentNullException">The parameter 'securityKeyIdentifierClause' is null.</exception> |
| | | 42 | | public override bool CanWriteKeyIdentifierClause(SecurityKeyIdentifierClause securityKeyIdentifierClause) |
| | | 43 | | { |
| | 0 | 44 | | if (securityKeyIdentifierClause == null) |
| | | 45 | | { |
| | 0 | 46 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityKeyIdentifierClause)); |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | return securityKeyIdentifierClause is X509IssuerSerialKeyIdentifierClause || |
| | 0 | 50 | | securityKeyIdentifierClause is X509RawDataKeyIdentifierClause || |
| | 0 | 51 | | securityKeyIdentifierClause is X509SubjectKeyIdentifierClause; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Deserializes a SecurityKeyIdentifierClause from a given XmlReader. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="reader">XmlReader that references a SecurityKeyIdentifierClause.</param> |
| | | 58 | | /// <returns>Instance of SecurityKeyIdentifierClause</returns> |
| | | 59 | | /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception> |
| | | 60 | | /// <exception cref="InvalidOperationException">The XmlReader is not positioned at a valid X.509 SecurityTokenRe |
| | | 61 | | public override SecurityKeyIdentifierClause ReadKeyIdentifierClause(XmlReader reader) |
| | | 62 | | { |
| | 0 | 63 | | if (!this.CanReadKeyIdentifierClause(reader)) |
| | | 64 | | { |
| | 0 | 65 | | throw new InvalidOperationException( |
| | 0 | 66 | | SR.Format(SR.ID3032, reader.LocalName, reader.NamespaceURI, XmlSignatureConstants.Elements.X509Data, |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | XmlDictionaryReader dictionaryReader = XmlDictionaryReader.CreateDictionaryReader(reader); |
| | | 70 | | |
| | | 71 | | // Read the starting <X509Data> element. |
| | 0 | 72 | | dictionaryReader.ReadStartElement(XmlSignatureConstants.Elements.X509Data, XmlSignatureConstants.Namespace); |
| | | 73 | | |
| | 0 | 74 | | List<SecurityKeyIdentifierClause> clauses = new List<SecurityKeyIdentifierClause>(); |
| | 0 | 75 | | while (dictionaryReader.IsStartElement()) |
| | | 76 | | { |
| | 0 | 77 | | if (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509IssuerSerial, XmlSignatureConstan |
| | | 78 | | { |
| | 0 | 79 | | clauses.Add(CreateIssuerSerialKeyIdentifierClause(dictionaryReader)); |
| | | 80 | | } |
| | 0 | 81 | | else if (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509SKI, XmlSignatureConstants.N |
| | | 82 | | { |
| | 0 | 83 | | clauses.Add(CreateSubjectKeyIdentifierClause(dictionaryReader)); |
| | | 84 | | } |
| | 0 | 85 | | else if (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509Certificate, XmlSignatureCon |
| | | 86 | | { |
| | 0 | 87 | | clauses.Add(CreateRawDataKeyIdentifierClause(dictionaryReader)); |
| | | 88 | | } |
| | | 89 | | else |
| | | 90 | | { |
| | | 91 | | // Skip the element since it is not one of <X509IssuerSerial>, <X509SKI> and <X509Certificate> |
| | 0 | 92 | | dictionaryReader.Skip(); |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // Read the ending </X509Data> element. |
| | 0 | 97 | | dictionaryReader.ReadEndElement(); |
| | | 98 | | |
| | | 99 | | // Return the first identified clause or null if none is identified |
| | 0 | 100 | | return clauses.Count > 0 ? clauses[0] : null; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Serialize a SecurityKeyIdentifierClause to the given XmlWriter. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="writer">XmlWriter to which the SecurityKeyIdentifierClause is serialized.</param> |
| | | 107 | | /// <param name="securityKeyIdentifierClause">SecurityKeyIdentifierClause to serialize.</param> |
| | | 108 | | /// <exception cref="ArgumentNullException">The input parameter 'reader' or 'securityKeyIdentifierClause' is nul |
| | | 109 | | /// <exception cref="ArgumentException">The parameter 'securityKeyIdentifierClause' is not a supported clause ty |
| | | 110 | | public override void WriteKeyIdentifierClause(XmlWriter writer, SecurityKeyIdentifierClause securityKeyIdentifie |
| | | 111 | | { |
| | 0 | 112 | | if (writer == null) |
| | | 113 | | { |
| | 0 | 114 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | if (securityKeyIdentifierClause == null) |
| | | 118 | | { |
| | 0 | 119 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityKeyIdentifierClause)); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | X509IssuerSerialKeyIdentifierClause issuerSerialClause = securityKeyIdentifierClause as X509IssuerSerialKeyI |
| | 0 | 123 | | if (issuerSerialClause != null) |
| | | 124 | | { |
| | 0 | 125 | | writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Data, XmlSigna |
| | 0 | 126 | | writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509IssuerSerial, |
| | 0 | 127 | | writer.WriteElementString(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509IssuerName, X |
| | 0 | 128 | | writer.WriteElementString(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509SerialNumber, |
| | 0 | 129 | | writer.WriteEndElement(); |
| | 0 | 130 | | writer.WriteEndElement(); |
| | 0 | 131 | | return; |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | X509SubjectKeyIdentifierClause skiClause = securityKeyIdentifierClause as X509SubjectKeyIdentifierClause; |
| | 0 | 135 | | if (skiClause != null) |
| | | 136 | | { |
| | 0 | 137 | | writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Data, XmlSigna |
| | 0 | 138 | | writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509SKI, XmlSignat |
| | 0 | 139 | | byte[] ski = skiClause.GetX509SubjectKeyIdentifier(); |
| | 0 | 140 | | writer.WriteBase64(ski, 0, ski.Length); |
| | 0 | 141 | | writer.WriteEndElement(); |
| | 0 | 142 | | writer.WriteEndElement(); |
| | 0 | 143 | | return; |
| | | 144 | | } |
| | | 145 | | #if INCLUDE_CERT_CHAIN |
| | | 146 | | X509ChainRawDataKeyIdentifierClause x509ChainDataClause = securityKeyIdentifierClause as X509ChainRawDataKey |
| | | 147 | | if ( x509ChainDataClause != null ) |
| | | 148 | | { |
| | | 149 | | writer.WriteStartElement( XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Data, XmlSign |
| | | 150 | | for( int i = 0; i < x509ChainDataClause.CertificateCount; i++ ) |
| | | 151 | | { |
| | | 152 | | writer.WriteStartElement( XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Certifica |
| | | 153 | | byte[] rawData = x509ChainDataClause.GetX509RawData( i ); |
| | | 154 | | writer.WriteBase64( rawData, 0, rawData.Length ); |
| | | 155 | | writer.WriteEndElement(); |
| | | 156 | | } |
| | | 157 | | writer.WriteEndElement(); |
| | | 158 | | return; |
| | | 159 | | } |
| | | 160 | | #endif |
| | | 161 | | |
| | 0 | 162 | | X509RawDataKeyIdentifierClause rawDataClause = securityKeyIdentifierClause as X509RawDataKeyIdentifierClause |
| | 0 | 163 | | if (rawDataClause != null) |
| | | 164 | | { |
| | 0 | 165 | | writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Data, XmlSigna |
| | 0 | 166 | | writer.WriteStartElement(XmlSignatureConstants.Prefix, XmlSignatureConstants.Elements.X509Certificate, X |
| | 0 | 167 | | byte[] rawData = rawDataClause.GetX509RawData(); |
| | 0 | 168 | | writer.WriteBase64(rawData, 0, rawData.Length); |
| | 0 | 169 | | writer.WriteEndElement(); |
| | 0 | 170 | | writer.WriteEndElement(); |
| | 0 | 171 | | return; |
| | | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(securityKeyIdentifierClause), SR.Format( |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Parses the "X509IssuerSerial" element and generates a corresponding <see cref="X509IssuerSerialKeyIdentifier |
| | | 179 | | /// </summary> |
| | | 180 | | /// <param name="dictionaryReader">The <see cref="XmlDictionaryReader"/> currently positioning on the "X509Issue |
| | | 181 | | /// <returns>An instance of <see cref="X509IssuerSerialKeyIdentifierClause"/> created from the "X509IssuerSerial |
| | | 182 | | private static SecurityKeyIdentifierClause CreateIssuerSerialKeyIdentifierClause(XmlDictionaryReader dictionaryR |
| | | 183 | | { |
| | 0 | 184 | | dictionaryReader.ReadStartElement(XmlSignatureConstants.Elements.X509IssuerSerial, XmlSignatureConstants.Nam |
| | | 185 | | |
| | 0 | 186 | | if (!dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509IssuerName, XmlSignatureConstants.Na |
| | | 187 | | { |
| | 0 | 188 | | throw new InvalidOperationException(SR.Format(SR.ID3032, dictionaryReader.LocalName, dictionaryReader.Na |
| | | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | string issuerName = dictionaryReader.ReadElementContentAsString(XmlSignatureConstants.Elements.X509IssuerNam |
| | | 192 | | |
| | 0 | 193 | | if (!dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509SerialNumber, XmlSignatureConstants. |
| | | 194 | | { |
| | 0 | 195 | | throw new InvalidOperationException(SR.Format(SR.ID3032, dictionaryReader.LocalName, dictionaryReader.Na |
| | | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | string serialNumber = dictionaryReader.ReadElementContentAsString(XmlSignatureConstants.Elements.X509SerialN |
| | | 199 | | |
| | 0 | 200 | | dictionaryReader.ReadEndElement(); // Reade the ending </X509IssuerSerial> element. |
| | | 201 | | |
| | 0 | 202 | | return new X509IssuerSerialKeyIdentifierClause(issuerName, serialNumber); |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Parses the "X509SKI" element and generates a corresponding <see cref="SecurityKeyIdentifierClause"/> instanc |
| | | 207 | | /// </summary> |
| | | 208 | | /// <param name="dictionaryReader">The <see cref="XmlDictionaryReader"/> currently positioning on the "X509SKI" |
| | | 209 | | /// <returns>An instance of <see cref="X509SubjectKeyIdentifierClause"/> created from the "X509SKI" element.</re |
| | | 210 | | private static SecurityKeyIdentifierClause CreateSubjectKeyIdentifierClause(XmlDictionaryReader dictionaryReader |
| | | 211 | | { |
| | 0 | 212 | | byte[] ski = dictionaryReader.ReadElementContentAsBase64(); |
| | 0 | 213 | | if ((ski == null) || (ski.Length == 0)) |
| | | 214 | | { |
| | 0 | 215 | | throw new InvalidOperationException(SR.Format(SR.ID4258, XmlSignatureConstants.Elements.X509SKI, XmlSign |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | return new X509SubjectKeyIdentifierClause(ski); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Parses the "X509Certificate" element and generates a corresponding <see cref="X509RawDataKeyIdentifierClause |
| | | 223 | | /// </summary> |
| | | 224 | | /// <param name="dictionaryReader">The <see cref="XmlDictionaryReader"/> currently positioning on the "X509Certi |
| | | 225 | | /// <returns>An instance of <see cref="X509RawDataKeyIdentifierClause"/> created from the "X509Certificate" elem |
| | | 226 | | private static SecurityKeyIdentifierClause CreateRawDataKeyIdentifierClause(XmlDictionaryReader dictionaryReader |
| | | 227 | | { |
| | | 228 | | #if INCLUDE_CERT_CHAIN |
| | | 229 | | List<byte[]> rawDatas = new List<byte[]>(); |
| | | 230 | | while (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509Certificate, XmlSignatureConst |
| | | 231 | | { |
| | | 232 | | byte[] rawBuffer = dictionaryReader.ReadElementContentAsBase64(); |
| | | 233 | | if (rawBuffer == null || rawBuffer.Length == 0) |
| | | 234 | | { |
| | | 235 | | throw new InvalidOperationException(SR.Format(SR.ID4258, XmlSignatureConstants.Elements.X509Cert |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | rawDatas.Add(rawBuffer); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | if (rawDatas.Count > 1) |
| | | 242 | | { |
| | | 243 | | return new X509ChainRawDataKeyIdentifierClause(rawDatas); |
| | | 244 | | } |
| | | 245 | | else |
| | | 246 | | { |
| | | 247 | | return new X509RawDataKeyIdentifierClause(rawDatas[0]); |
| | | 248 | | } |
| | | 249 | | #else |
| | 0 | 250 | | byte[] rawData = null; |
| | 0 | 251 | | while (dictionaryReader.IsStartElement(XmlSignatureConstants.Elements.X509Certificate, XmlSignatureConstants |
| | | 252 | | { |
| | 0 | 253 | | if (rawData == null) |
| | | 254 | | { |
| | 0 | 255 | | rawData = dictionaryReader.ReadElementContentAsBase64(); |
| | 0 | 256 | | if ((rawData == null) || (rawData.Length == 0)) |
| | | 257 | | { |
| | 0 | 258 | | throw new InvalidOperationException(SR.Format(SR.ID4258, XmlSignatureConstants.Elements.X509Cert |
| | | 259 | | } |
| | | 260 | | } |
| | | 261 | | else |
| | | 262 | | { |
| | | 263 | | // We do not support reading intermediary certs. |
| | 0 | 264 | | dictionaryReader.Skip(); |
| | | 265 | | } |
| | | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | return new X509RawDataKeyIdentifierClause(rawData); |
| | | 269 | | #endif |
| | | 270 | | } |
| | | 271 | | } |
| | | 272 | | } |