| | | 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.Xml; |
| | | 6 | | using Microsoft.IdentityModel.Logging; |
| | | 7 | | using Microsoft.IdentityModel.Xml; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel.Tokens.Saml |
| | | 10 | | { |
| | | 11 | | internal class DSigSerializerExtended : DSigSerializer |
| | | 12 | | { |
| | | 13 | | protected override bool TryReadKeyInfoType(XmlReader reader, ref Microsoft.IdentityModel.Xml.KeyInfo keyInfo) |
| | | 14 | | { |
| | 4 | 15 | | if (keyInfo is KeyInfo keyInfoType) |
| | | 16 | | { |
| | | 17 | | |
| | 4 | 18 | | if (TryReadSecurityTokenReference(reader, out SecurityTokenReference securityTokenReference)) |
| | | 19 | | { |
| | 3 | 20 | | keyInfoType.SecurityTokenReference = securityTokenReference; |
| | 3 | 21 | | return true; |
| | | 22 | | } |
| | 1 | 23 | | else if (TryReadBinarySecret(reader, out BinarySecret secret)) |
| | | 24 | | { |
| | 1 | 25 | | keyInfoType.BinarySecret = secret; |
| | 1 | 26 | | return true; |
| | | 27 | | } |
| | 0 | 28 | | else if (reader.IsStartElement(XmlEncryptionStrings.EncryptedKey)) |
| | | 29 | | { |
| | 0 | 30 | | if (TryReadEncryptedKey(reader, out var encryptedKey)) |
| | | 31 | | { |
| | 0 | 32 | | keyInfoType.EncryptedKey = encryptedKey; |
| | 0 | 33 | | return true; |
| | | 34 | | } |
| | | 35 | | else |
| | 0 | 36 | | return false; |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | return base.TryReadKeyInfoType(reader, ref keyInfo); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | private static bool TryReadBinarySecret(XmlReader reader, out BinarySecret secret) |
| | | 44 | | { |
| | 1 | 45 | | secret = null; |
| | 1 | 46 | | if (!reader.IsStartElement("BinarySecret", "http://docs.oasis-open.org/ws-sx/ws-trust/200512")) |
| | 0 | 47 | | return false; |
| | | 48 | | |
| | 1 | 49 | | secret = new BinarySecret(reader.ReadElementContentAsString()); |
| | | 50 | | |
| | 1 | 51 | | return true; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | protected override Microsoft.IdentityModel.Xml.KeyInfo CreateKeyInfo(XmlReader reader) |
| | | 55 | | { |
| | 4 | 56 | | Microsoft.IdentityModel.Xml.XmlUtil.CheckReaderOnEntry(reader, XmlSignatureConstants.Elements.KeyInfo, XmlSi |
| | | 57 | | |
| | 4 | 58 | | return new KeyInfo |
| | 4 | 59 | | { |
| | 4 | 60 | | Prefix = reader.Prefix |
| | 4 | 61 | | }; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Reads the "SecurityTokenReference" element |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="reader">A <see cref="XmlReader"/> positioned on a <see cref="CoreWcfXmlSignatureConstants.Eleme |
| | | 68 | | /// <param name="securityTokenReference"></param> |
| | | 69 | | private static bool TryReadSecurityTokenReference(XmlReader reader, out SecurityTokenReference securityTokenRefe |
| | | 70 | | { |
| | 4 | 71 | | securityTokenReference = null; |
| | 4 | 72 | | if (!reader.IsStartElement(CoreWCF.XD.SecurityJan2004Dictionary.SecurityTokenReference.Value, CoreWCF.XD.Sec |
| | 1 | 73 | | return false; |
| | | 74 | | |
| | 3 | 75 | | reader.ReadStartElement(CoreWCF.XD.SecurityJan2004Dictionary.SecurityTokenReference.Value, CoreWCF.XD.Securi |
| | | 76 | | |
| | 3 | 77 | | if (!reader.IsStartElement(CoreWCF.XD.SecurityJan2004Dictionary.KeyIdentifier.Value, CoreWCF.XD.SecurityJan2 |
| | 0 | 78 | | return false; |
| | | 79 | | |
| | 3 | 80 | | string valueType = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.ValueType.Value, null); |
| | 3 | 81 | | string encodingType = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.EncodingType.Value, null); |
| | 3 | 82 | | string value = reader.ReadElementContentAsString(CoreWCF.XD.SecurityJan2004Dictionary.KeyIdentifier.Value, C |
| | | 83 | | |
| | 3 | 84 | | reader.ReadEndElement(); |
| | | 85 | | |
| | 3 | 86 | | securityTokenReference = new SecurityTokenReference(new SecurityKeyIdentifier(valueType, encodingType, value |
| | | 87 | | |
| | 3 | 88 | | return true; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Attempts to read the <see cref="CoreWcfXmlSignatureConstants.Elements.EncryptedKey"/> |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="reader">A <see cref="XmlReader"/> positioned on a <see cref="CoreWcfXmlSignatureConstants.Eleme |
| | | 95 | | /// <param name="encryptedKey">The parsed <see cref="CoreWcfXmlSignatureConstants.Elements.EncryptedKey"/> eleme |
| | | 96 | | protected virtual bool TryReadEncryptedKey(XmlReader reader, out EncryptedKey encryptedKey) |
| | | 97 | | { |
| | 0 | 98 | | if (reader == null) |
| | 0 | 99 | | throw new ArgumentNullException(LogHelper.FormatInvariant("IDX10000: The parameter '{0}' cannot be a 'nu |
| | | 100 | | |
| | 0 | 101 | | encryptedKey = null; |
| | | 102 | | |
| | 0 | 103 | | if (!reader.IsStartElement(XmlEncryptionStrings.EncryptedKey, XmlSignatureStrings.Namespace)) |
| | 0 | 104 | | return false; |
| | | 105 | | |
| | 0 | 106 | | reader.ReadStartElement(XmlEncryptionStrings.EncryptedKey, XmlSignatureStrings.Namespace); |
| | | 107 | | |
| | 0 | 108 | | if (!reader.IsStartElement(XmlEncryptionStrings.EncryptionMethod, XmlSignatureStrings.Namespace)) |
| | 0 | 109 | | throw Microsoft.IdentityModel.Xml.XmlUtil.LogReadException( |
| | 0 | 110 | | "IDX30011: Unable to read XML. Expecting XmlReader to be at ns.element: '{0}.{1}', found: '{2}.{3}'. |
| | 0 | 111 | | XmlSignatureStrings.Namespace, |
| | 0 | 112 | | XmlEncryptionStrings.EncryptionMethod, |
| | 0 | 113 | | reader.NamespaceURI, |
| | 0 | 114 | | reader.LocalName); |
| | | 115 | | |
| | 0 | 116 | | string algorithm = reader.GetAttribute(XmlEncryptionStrings.AlgorithmAttribute); |
| | 0 | 117 | | Microsoft.IdentityModel.Xml.KeyInfo keyInfo = ReadKeyInfo(reader); |
| | | 118 | | |
| | 0 | 119 | | if (!reader.IsStartElement(XmlEncryptionStrings.CipherData, XmlSignatureStrings.Namespace)) |
| | 0 | 120 | | throw Microsoft.IdentityModel.Xml.XmlUtil.LogReadException( |
| | 0 | 121 | | "IDX30011: Unable to read XML. Expecting XmlReader to be at ns.element: '{0}.{1}', found: '{2}.{3}'. |
| | 0 | 122 | | XmlSignatureStrings.Namespace, |
| | 0 | 123 | | XmlEncryptionStrings.CipherData, |
| | 0 | 124 | | reader.NamespaceURI, |
| | 0 | 125 | | reader.LocalName); |
| | | 126 | | |
| | 0 | 127 | | reader.ReadStartElement(XmlEncryptionStrings.CipherData, XmlSignatureStrings.Namespace); |
| | | 128 | | |
| | 0 | 129 | | if (!reader.IsStartElement(XmlEncryptionStrings.CipherValue, XmlSignatureStrings.Namespace)) |
| | 0 | 130 | | throw Microsoft.IdentityModel.Xml.XmlUtil.LogReadException( |
| | 0 | 131 | | "IDX30011: Unable to read XML. Expecting XmlReader to be at ns.element: '{0}.{1}', found: '{2}.{3}'. |
| | 0 | 132 | | XmlSignatureStrings.Namespace, |
| | 0 | 133 | | XmlEncryptionStrings.CipherValue, |
| | 0 | 134 | | reader.NamespaceURI, |
| | 0 | 135 | | reader.LocalName); |
| | | 136 | | |
| | 0 | 137 | | string cipherValue = reader.ReadElementContentAsString(XmlEncryptionStrings.CipherValue, XmlSignatureStrings |
| | 0 | 138 | | reader.ReadEndElement(); |
| | 0 | 139 | | encryptedKey = new EncryptedKey(algorithm, keyInfo, cipherValue); |
| | | 140 | | |
| | 0 | 141 | | return true; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |