| | | 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.ObjectModel; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Security.Tokens |
| | | 10 | | { |
| | | 11 | | public class SupportingTokenParameters |
| | | 12 | | { |
| | 2784 | 13 | | private SupportingTokenParameters(SupportingTokenParameters other) |
| | | 14 | | { |
| | 2784 | 15 | | if (other == null) |
| | | 16 | | { |
| | 0 | 17 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(other)); |
| | | 18 | | } |
| | | 19 | | |
| | 5704 | 20 | | foreach (SecurityTokenParameters p in other.Signed) |
| | | 21 | | { |
| | 68 | 22 | | Signed.Add((SecurityTokenParameters)p.Clone()); |
| | | 23 | | } |
| | | 24 | | |
| | 6506 | 25 | | foreach (SecurityTokenParameters p in other.SignedEncrypted) |
| | | 26 | | { |
| | 469 | 27 | | SignedEncrypted.Add((SecurityTokenParameters)p.Clone()); |
| | | 28 | | } |
| | | 29 | | |
| | 7190 | 30 | | foreach (SecurityTokenParameters p in other.Endorsing) |
| | | 31 | | { |
| | 811 | 32 | | Endorsing.Add((SecurityTokenParameters)p.Clone()); |
| | | 33 | | } |
| | | 34 | | |
| | 5568 | 35 | | foreach (SecurityTokenParameters p in other.SignedEndorsing) |
| | | 36 | | { |
| | 0 | 37 | | SignedEndorsing.Add((SecurityTokenParameters)p.Clone()); |
| | | 38 | | } |
| | 2784 | 39 | | } |
| | | 40 | | |
| | 766 | 41 | | public SupportingTokenParameters() |
| | | 42 | | { |
| | | 43 | | // empty |
| | 766 | 44 | | } |
| | | 45 | | |
| | 8258 | 46 | | public Collection<SecurityTokenParameters> Endorsing { get; } = new Collection<SecurityTokenParameters>(); |
| | | 47 | | |
| | 7004 | 48 | | public Collection<SecurityTokenParameters> SignedEndorsing { get; } = new Collection<SecurityTokenParameters>(); |
| | | 49 | | |
| | 6783 | 50 | | public Collection<SecurityTokenParameters> Signed { get; } = new Collection<SecurityTokenParameters>(); |
| | | 51 | | |
| | 7565 | 52 | | public Collection<SecurityTokenParameters> SignedEncrypted { get; } = new Collection<SecurityTokenParameters>(); |
| | | 53 | | |
| | | 54 | | public void SetKeyDerivation(bool requireDerivedKeys) |
| | | 55 | | { |
| | 286 | 56 | | foreach (SecurityTokenParameters t in Endorsing) |
| | | 57 | | { |
| | 1 | 58 | | if (t.HasAsymmetricKey) |
| | | 59 | | { |
| | 0 | 60 | | t.RequireDerivedKeys = false; |
| | | 61 | | } |
| | | 62 | | else |
| | | 63 | | { |
| | 1 | 64 | | t.RequireDerivedKeys = requireDerivedKeys; |
| | | 65 | | } |
| | | 66 | | } |
| | 284 | 67 | | foreach (SecurityTokenParameters t in SignedEndorsing) |
| | | 68 | | { |
| | 0 | 69 | | if (t.HasAsymmetricKey) |
| | | 70 | | { |
| | 0 | 71 | | t.RequireDerivedKeys = false; |
| | | 72 | | } |
| | | 73 | | else |
| | | 74 | | { |
| | 0 | 75 | | t.RequireDerivedKeys = requireDerivedKeys; |
| | | 76 | | } |
| | | 77 | | } |
| | 142 | 78 | | } |
| | | 79 | | |
| | | 80 | | internal bool IsSetKeyDerivation(bool requireDerivedKeys) |
| | | 81 | | { |
| | 0 | 82 | | foreach (SecurityTokenParameters t in Endorsing) |
| | | 83 | | { |
| | 0 | 84 | | if (t.RequireDerivedKeys != requireDerivedKeys) |
| | | 85 | | { |
| | 0 | 86 | | return false; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | foreach (SecurityTokenParameters t in SignedEndorsing) |
| | | 91 | | { |
| | 0 | 92 | | if (t.RequireDerivedKeys != requireDerivedKeys) |
| | | 93 | | { |
| | 0 | 94 | | return false; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | return true; |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | public override string ToString() |
| | | 102 | | { |
| | 0 | 103 | | StringBuilder sb = new StringBuilder(); |
| | | 104 | | int k; |
| | | 105 | | |
| | 0 | 106 | | if (Endorsing.Count == 0) |
| | | 107 | | { |
| | 0 | 108 | | sb.AppendLine("No endorsing tokens."); |
| | | 109 | | } |
| | | 110 | | else |
| | | 111 | | { |
| | 0 | 112 | | for (k = 0; k < Endorsing.Count; k++) |
| | | 113 | | { |
| | 0 | 114 | | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "Endorsing[{0}]", k.ToString(CultureInfo.I |
| | 0 | 115 | | sb.AppendLine(" " + Endorsing[k].ToString().Trim().Replace("\n", "\n ")); |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | if (Signed.Count == 0) |
| | | 120 | | { |
| | 0 | 121 | | sb.AppendLine("No signed tokens."); |
| | | 122 | | } |
| | | 123 | | else |
| | | 124 | | { |
| | 0 | 125 | | for (k = 0; k < Signed.Count; k++) |
| | | 126 | | { |
| | 0 | 127 | | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "Signed[{0}]", k.ToString(CultureInfo.Inva |
| | 0 | 128 | | sb.AppendLine(" " + Signed[k].ToString().Trim().Replace("\n", "\n ")); |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | if (SignedEncrypted.Count == 0) |
| | | 133 | | { |
| | 0 | 134 | | sb.AppendLine("No signed encrypted tokens."); |
| | | 135 | | } |
| | | 136 | | else |
| | | 137 | | { |
| | 0 | 138 | | for (k = 0; k < SignedEncrypted.Count; k++) |
| | | 139 | | { |
| | 0 | 140 | | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "SignedEncrypted[{0}]", k.ToString(Culture |
| | 0 | 141 | | sb.AppendLine(" " + SignedEncrypted[k].ToString().Trim().Replace("\n", "\n ")); |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | if (SignedEndorsing.Count == 0) |
| | | 146 | | { |
| | 0 | 147 | | sb.AppendLine("No signed endorsing tokens."); |
| | | 148 | | } |
| | | 149 | | else |
| | | 150 | | { |
| | 0 | 151 | | for (k = 0; k < SignedEndorsing.Count; k++) |
| | | 152 | | { |
| | 0 | 153 | | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "SignedEndorsing[{0}]", k.ToString(Culture |
| | 0 | 154 | | sb.AppendLine(" " + SignedEndorsing[k].ToString().Trim().Replace("\n", "\n ")); |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | return sb.ToString().Trim(); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | public SupportingTokenParameters Clone() |
| | | 162 | | { |
| | 2784 | 163 | | SupportingTokenParameters parameters = CloneCore(); |
| | | 164 | | /* if (parameters == null || parameters.GetType() != this.GetType()) |
| | | 165 | | { |
| | | 166 | | TraceUtility.TraceEvent( |
| | | 167 | | TraceEventType.Error, |
| | | 168 | | TraceCode.Security, |
| | | 169 | | SR.GetString(SR.CloneNotImplementedCorrectly, new object[] { this.GetType(), (parameters != null) ? |
| | | 170 | | }*/ |
| | | 171 | | |
| | 2784 | 172 | | return parameters; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | protected virtual SupportingTokenParameters CloneCore() |
| | | 176 | | { |
| | 2784 | 177 | | return new SupportingTokenParameters(this); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | internal bool IsEmpty() |
| | | 181 | | { |
| | 0 | 182 | | return Signed.Count == 0 && SignedEncrypted.Count == 0 && Endorsing.Count == 0 && SignedEndorsing.Count == 0 |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | } |