| | | 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 CoreWCF.IdentityModel.Selectors; |
| | | 6 | | using CoreWCF.IdentityModel.Tokens; |
| | | 7 | | using System.Xml; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// This class implements a deserialization for: EncryptedType as defined in section 3.5.1 of http://www.w3.org/TR/2 |
| | | 13 | | /// </summary> |
| | | 14 | | internal class EncryptedKeyElement : EncryptedTypeElement |
| | | 15 | | { |
| | | 16 | | private string _recipient; |
| | | 17 | | private List<string> _keyReferences; |
| | | 18 | | private List<string> _dataReferences; |
| | | 19 | | |
| | | 20 | | public EncryptedKeyElement( SecurityTokenSerializer keyInfoSerializer ) |
| | 0 | 21 | | : base( keyInfoSerializer ) |
| | | 22 | | { |
| | 0 | 23 | | _keyReferences = new List<string>(); |
| | 0 | 24 | | _dataReferences = new List<string>(); |
| | 0 | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | public string CarriedName { get; private set; } |
| | | 28 | | |
| | | 29 | | public IList<string> DataReferences |
| | | 30 | | { |
| | 0 | 31 | | get { return _dataReferences; } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public IList<string> KeyReferences |
| | | 35 | | { |
| | 0 | 36 | | get { return _keyReferences; } |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public override void ReadExtensions( XmlDictionaryReader reader ) |
| | | 40 | | { |
| | 0 | 41 | | reader.MoveToContent(); |
| | 0 | 42 | | if ( reader.IsStartElement( XmlEncryptionConstants.Elements.ReferenceList, XmlEncryptionConstants.Namespace |
| | | 43 | | { |
| | 0 | 44 | | reader.ReadStartElement(); |
| | | 45 | | |
| | | 46 | | // could have data or key references. these are the only two possible elements sec 3.6 xml enc. |
| | | 47 | | // 3.6 The ReferenceList Element specifies there is a choice. Once one is chosen, it is fixed. |
| | 0 | 48 | | if ( reader.IsStartElement( XmlEncryptionConstants.Elements.DataReference, XmlEncryptionConstants.Namesp |
| | | 49 | | { |
| | 0 | 50 | | while ( reader.IsStartElement() ) |
| | | 51 | | { |
| | 0 | 52 | | if ( reader.IsStartElement( XmlEncryptionConstants.Elements.DataReference, XmlEncryptionConstant |
| | | 53 | | { |
| | 0 | 54 | | string dataRef = reader.GetAttribute( XmlEncryptionConstants.Attributes.Uri ); |
| | 0 | 55 | | if ( !string.IsNullOrEmpty( dataRef ) ) |
| | | 56 | | { |
| | 0 | 57 | | _dataReferences.Add( dataRef ); |
| | | 58 | | } |
| | 0 | 59 | | reader.Skip(); |
| | | 60 | | } |
| | 0 | 61 | | else if ( reader.IsStartElement( XmlEncryptionConstants.Elements.KeyReference, XmlEncryptionCons |
| | | 62 | | { |
| | 0 | 63 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml( reader, SR.Format( SR.ID4189 ) ); |
| | | 64 | | } |
| | | 65 | | else |
| | | 66 | | { |
| | 0 | 67 | | string xml = reader.ReadOuterXml(); |
| | | 68 | | //if ( DiagnosticUtility.ShouldTraceWarning ) |
| | | 69 | | //{ |
| | | 70 | | // TraceUtility.TraceString( System.Diagnostics.TraceEventType.Warning, SR.Format( SR.ID8 |
| | | 71 | | //} |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | } |
| | 0 | 75 | | else if ( reader.IsStartElement( XmlEncryptionConstants.Elements.KeyReference, XmlEncryptionConstants.Na |
| | | 76 | | { |
| | 0 | 77 | | while ( reader.IsStartElement() ) |
| | | 78 | | { |
| | 0 | 79 | | if ( reader.IsStartElement( XmlEncryptionConstants.Elements.KeyReference, XmlEncryptionConstants |
| | | 80 | | { |
| | 0 | 81 | | string keyRef = reader.GetAttribute( XmlEncryptionConstants.Attributes.Uri ); |
| | 0 | 82 | | if ( !string.IsNullOrEmpty( keyRef ) ) |
| | | 83 | | { |
| | 0 | 84 | | _keyReferences.Add( keyRef ); |
| | | 85 | | } |
| | 0 | 86 | | reader.Skip(); |
| | | 87 | | } |
| | 0 | 88 | | else if ( reader.IsStartElement( XmlEncryptionConstants.Elements.DataReference, XmlEncryptionCon |
| | | 89 | | { |
| | 0 | 90 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml( reader, SR.Format( SR.ID4190 ) ); |
| | | 91 | | } |
| | | 92 | | else |
| | | 93 | | { |
| | 0 | 94 | | string xml = reader.ReadOuterXml(); |
| | | 95 | | //if ( DiagnosticUtility.ShouldTraceWarning ) |
| | | 96 | | //{ |
| | | 97 | | // TraceUtility.TraceString( System.Diagnostics.TraceEventType.Warning, SR.Format( SR.ID8 |
| | | 98 | | //} |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | else |
| | | 103 | | { |
| | | 104 | | // there must be at least one reference. |
| | 0 | 105 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml( reader, SR.Format( SR.ID4191 ) ); |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | reader.MoveToContent(); |
| | 0 | 109 | | if ( reader.IsStartElement( XmlEncryptionConstants.Elements.CarriedKeyName, XmlEncryptionConstants.Names |
| | | 110 | | { |
| | 0 | 111 | | reader.ReadStartElement(); |
| | 0 | 112 | | CarriedName = reader.ReadString(); |
| | 0 | 113 | | reader.ReadEndElement(); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | // </ReferenceList> |
| | 0 | 117 | | reader.ReadEndElement(); |
| | | 118 | | } |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | public override void ReadXml( XmlDictionaryReader reader ) |
| | | 122 | | { |
| | 0 | 123 | | if ( reader == null ) |
| | | 124 | | { |
| | 0 | 125 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | reader.MoveToContent(); |
| | 0 | 129 | | if ( !reader.IsStartElement( XmlEncryptionConstants.Elements.EncryptedKey, XmlEncryptionConstants.Namespace |
| | | 130 | | { |
| | 0 | 131 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml( reader, SR.Format( SR.ID4187 ) ); |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | _recipient = reader.GetAttribute( XmlEncryptionConstants.Attributes.Recipient, null ); |
| | | 135 | | |
| | | 136 | | //<EncryptedKey> extends <EncryptedType> |
| | | 137 | | // base will read the start element and end elements |
| | 0 | 138 | | base.ReadXml( reader ); |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | public EncryptedKeyIdentifierClause GetClause() |
| | | 142 | | { |
| | 0 | 143 | | return new EncryptedKeyIdentifierClause( CipherData.CipherValue, Algorithm, KeyIdentifier ); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | } |
| | | 147 | | } |