| | | 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.Collections.ObjectModel; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Runtime.Serialization; |
| | | 9 | | using System.Security.Cryptography; |
| | | 10 | | using System.Xml; |
| | | 11 | | using CoreWCF.Channels; |
| | | 12 | | using CoreWCF.Dispatcher; |
| | | 13 | | using CoreWCF.IdentityModel; |
| | | 14 | | using CoreWCF.IdentityModel.Policy; |
| | | 15 | | using CoreWCF.IdentityModel.Selectors; |
| | | 16 | | using CoreWCF.IdentityModel.Tokens; |
| | | 17 | | using CoreWCF.Runtime; |
| | | 18 | | using CoreWCF.Security.Tokens; |
| | | 19 | | using Psha1DerivedKeyGenerator = CoreWCF.IdentityModel.Psha1DerivedKeyGenerator; |
| | | 20 | | |
| | | 21 | | namespace CoreWCF.Security |
| | | 22 | | { |
| | | 23 | | internal class RequestSecurityTokenResponse : BodyWriter |
| | | 24 | | { |
| | 2 | 25 | | private static readonly int s_minSaneKeySizeInBits = 8 * 8; // 8 Bytes. |
| | 2 | 26 | | private static readonly int s_maxSaneKeySizeInBits = (16 * 1024) * 8; // 16 K |
| | | 27 | | private SecurityStandardsManager _standardsManager; |
| | | 28 | | private string _context; |
| | | 29 | | private int _keySize; |
| | | 30 | | private bool _computeKey; |
| | | 31 | | private string _tokenType; |
| | | 32 | | private SecurityKeyIdentifierClause _requestedAttachedReference; |
| | | 33 | | private SecurityKeyIdentifierClause _requestedUnattachedReference; |
| | | 34 | | private SecurityToken _issuedToken; |
| | | 35 | | private SecurityToken _proofToken; |
| | | 36 | | private SecurityToken _entropyToken; |
| | | 37 | | private BinaryNegotiation _negotiationData; |
| | | 38 | | private readonly XmlElement _rstrXml; |
| | | 39 | | private bool _isLifetimeSet; |
| | | 40 | | private byte[] _authenticator; |
| | | 41 | | private byte[] _cachedWriteBuffer; |
| | | 42 | | private int _cachedWriteBufferLength; |
| | | 43 | | private bool _isRequestedTokenClosed; |
| | | 44 | | private object _appliesTo; |
| | | 45 | | private XmlObjectSerializer _appliesToSerializer; |
| | | 46 | | private Type _appliesToType; |
| | | 47 | | |
| | | 48 | | public RequestSecurityTokenResponse() |
| | 0 | 49 | | : this(SecurityStandardsManager.DefaultInstance) |
| | | 50 | | { |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public RequestSecurityTokenResponse(MessageSecurityVersion messageSecurityVersion, SecurityTokenSerializer secur |
| | 0 | 54 | | : this(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer)) |
| | | 55 | | { |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public RequestSecurityTokenResponse(XmlElement requestSecurityTokenResponseXml, |
| | | 59 | | string context, |
| | | 60 | | string tokenType, |
| | | 61 | | int keySize, |
| | | 62 | | SecurityKeyIdentifierClause requestedAttachedReference, |
| | | 63 | | SecurityKeyIdentifierClause requestedUnattachedReference, |
| | | 64 | | bool computeKey, |
| | | 65 | | DateTime validFrom, |
| | | 66 | | DateTime validTo, |
| | | 67 | | bool isRequestedTokenClosed) |
| | 0 | 68 | | : this(SecurityStandardsManager.DefaultInstance, |
| | 0 | 69 | | requestSecurityTokenResponseXml, |
| | 0 | 70 | | context, |
| | 0 | 71 | | tokenType, |
| | 0 | 72 | | keySize, |
| | 0 | 73 | | requestedAttachedReference, |
| | 0 | 74 | | requestedUnattachedReference, |
| | 0 | 75 | | computeKey, |
| | 0 | 76 | | validFrom, |
| | 0 | 77 | | validTo, |
| | 0 | 78 | | isRequestedTokenClosed) |
| | | 79 | | { |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public RequestSecurityTokenResponse(MessageSecurityVersion messageSecurityVersion, |
| | | 83 | | SecurityTokenSerializer securityTokenSerializer, |
| | | 84 | | XmlElement requestSecurityTokenResponseXml, |
| | | 85 | | string context, |
| | | 86 | | string tokenType, |
| | | 87 | | int keySize, |
| | | 88 | | SecurityKeyIdentifierClause requestedAttachedReference, |
| | | 89 | | SecurityKeyIdentifierClause requestedUnattachedReference, |
| | | 90 | | bool computeKey, |
| | | 91 | | DateTime validFrom, |
| | | 92 | | DateTime validTo, |
| | | 93 | | bool isRequestedTokenClosed) |
| | 0 | 94 | | : this(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer), |
| | 0 | 95 | | requestSecurityTokenResponseXml, |
| | 0 | 96 | | context, |
| | 0 | 97 | | tokenType, |
| | 0 | 98 | | keySize, |
| | 0 | 99 | | requestedAttachedReference, |
| | 0 | 100 | | requestedUnattachedReference, |
| | 0 | 101 | | computeKey, |
| | 0 | 102 | | validFrom, |
| | 0 | 103 | | validTo, |
| | 0 | 104 | | isRequestedTokenClosed) |
| | | 105 | | { |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | internal RequestSecurityTokenResponse(SecurityStandardsManager standardsManager) |
| | 20 | 109 | | : base(true) |
| | | 110 | | { |
| | 20 | 111 | | _standardsManager = standardsManager ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Argume |
| | 20 | 112 | | ValidFrom = SecurityUtils.MinUtcDateTime; |
| | 20 | 113 | | ValidTo = SecurityUtils.MaxUtcDateTime; |
| | 20 | 114 | | _isRequestedTokenClosed = false; |
| | 20 | 115 | | _isLifetimeSet = false; |
| | 20 | 116 | | IsReceiver = false; |
| | 20 | 117 | | IsReadOnly = false; |
| | 20 | 118 | | } |
| | | 119 | | |
| | | 120 | | internal RequestSecurityTokenResponse(SecurityStandardsManager standardsManager, |
| | | 121 | | XmlElement rstrXml, |
| | | 122 | | string context, |
| | | 123 | | string tokenType, |
| | | 124 | | int keySize, |
| | | 125 | | SecurityKeyIdentifierClause requestedAttachedReference, |
| | | 126 | | SecurityKeyIdentifierClause requestedUnattachedReference, |
| | | 127 | | bool computeKey, |
| | | 128 | | DateTime validFrom, |
| | | 129 | | DateTime validTo, |
| | | 130 | | bool isRequestedTokenClosed) |
| | 0 | 131 | | : base(true) |
| | | 132 | | { |
| | 0 | 133 | | _standardsManager = standardsManager ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Argume |
| | 0 | 134 | | _rstrXml = rstrXml ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rstrXml)); |
| | 0 | 135 | | _context = context; |
| | 0 | 136 | | _tokenType = tokenType; |
| | 0 | 137 | | _keySize = keySize; |
| | 0 | 138 | | _requestedAttachedReference = requestedAttachedReference; |
| | 0 | 139 | | _requestedUnattachedReference = requestedUnattachedReference; |
| | 0 | 140 | | _computeKey = computeKey; |
| | 0 | 141 | | ValidFrom = validFrom.ToUniversalTime(); |
| | 0 | 142 | | ValidTo = validTo.ToUniversalTime(); |
| | 0 | 143 | | _isLifetimeSet = true; |
| | 0 | 144 | | _isRequestedTokenClosed = isRequestedTokenClosed; |
| | | 145 | | // this.issuedTokenBuffer = issuedTokenBuffer; |
| | 0 | 146 | | IsReceiver = true; |
| | 0 | 147 | | IsReadOnly = true; |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | public RequestSecurityTokenResponse(SecurityStandardsManager standardsManager, |
| | | 151 | | XmlElement rstrXml, |
| | | 152 | | string context, |
| | | 153 | | string tokenType, int keySize, SecurityKeyIdentifierClause requestedAttachedReference, |
| | | 154 | | SecurityKeyIdentifierClause requestedUnattachedReference, bool computeKey, DateTime validFrom, DateTime vali |
| | | 155 | | bool isRequestedTokenClosed, XmlBuffer issuedTokenBuffer) : |
| | 0 | 156 | | this(standardsManager, rstrXml, context, tokenType, keySize, requestedAttachedReference, requestedUnattached |
| | | 157 | | { |
| | 0 | 158 | | IssuedTokenBuffer = issuedTokenBuffer; |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | public string Context |
| | | 162 | | { |
| | | 163 | | get |
| | | 164 | | { |
| | 20 | 165 | | return _context; |
| | | 166 | | } |
| | | 167 | | set |
| | | 168 | | { |
| | 20 | 169 | | if (IsReadOnly) |
| | | 170 | | { |
| | 0 | 171 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 172 | | } |
| | | 173 | | |
| | 20 | 174 | | _context = value; |
| | 20 | 175 | | } |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | public string TokenType |
| | | 179 | | { |
| | | 180 | | get |
| | | 181 | | { |
| | 30 | 182 | | return _tokenType; |
| | | 183 | | } |
| | | 184 | | set |
| | | 185 | | { |
| | 10 | 186 | | if (IsReadOnly) |
| | | 187 | | { |
| | 0 | 188 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 189 | | } |
| | | 190 | | |
| | 10 | 191 | | _tokenType = value; |
| | 10 | 192 | | } |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | public SecurityKeyIdentifierClause RequestedAttachedReference |
| | | 196 | | { |
| | | 197 | | get |
| | | 198 | | { |
| | 30 | 199 | | return _requestedAttachedReference; |
| | | 200 | | } |
| | | 201 | | set |
| | | 202 | | { |
| | 10 | 203 | | if (IsReadOnly) |
| | | 204 | | { |
| | 0 | 205 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 206 | | } |
| | | 207 | | |
| | 10 | 208 | | _requestedAttachedReference = value; |
| | 10 | 209 | | } |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | public SecurityKeyIdentifierClause RequestedUnattachedReference |
| | | 213 | | { |
| | | 214 | | get |
| | | 215 | | { |
| | 30 | 216 | | return _requestedUnattachedReference; |
| | | 217 | | } |
| | | 218 | | set |
| | | 219 | | { |
| | 10 | 220 | | if (IsReadOnly) |
| | | 221 | | { |
| | 0 | 222 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 223 | | } |
| | | 224 | | |
| | 10 | 225 | | _requestedUnattachedReference = value; |
| | 10 | 226 | | } |
| | | 227 | | } |
| | | 228 | | |
| | 40 | 229 | | public DateTime ValidFrom { get; private set; } |
| | | 230 | | |
| | 40 | 231 | | public DateTime ValidTo { get; private set; } |
| | | 232 | | |
| | | 233 | | public bool ComputeKey |
| | | 234 | | { |
| | | 235 | | get |
| | | 236 | | { |
| | 30 | 237 | | return _computeKey; |
| | | 238 | | } |
| | | 239 | | set |
| | | 240 | | { |
| | 10 | 241 | | if (IsReadOnly) |
| | | 242 | | { |
| | 0 | 243 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 244 | | } |
| | | 245 | | |
| | 10 | 246 | | _computeKey = value; |
| | 10 | 247 | | } |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | public int KeySize |
| | | 251 | | { |
| | | 252 | | get |
| | | 253 | | { |
| | 30 | 254 | | return _keySize; |
| | | 255 | | } |
| | | 256 | | set |
| | | 257 | | { |
| | 10 | 258 | | if (IsReadOnly) |
| | | 259 | | { |
| | 0 | 260 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 261 | | } |
| | | 262 | | |
| | 10 | 263 | | if (value < 0) |
| | | 264 | | { |
| | 0 | 265 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 266 | | } |
| | | 267 | | |
| | 10 | 268 | | _keySize = value; |
| | 10 | 269 | | } |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | public bool IsRequestedTokenClosed |
| | | 273 | | { |
| | | 274 | | get |
| | | 275 | | { |
| | 20 | 276 | | return _isRequestedTokenClosed; |
| | | 277 | | } |
| | | 278 | | set |
| | | 279 | | { |
| | 10 | 280 | | if (IsReadOnly) |
| | | 281 | | { |
| | 0 | 282 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 283 | | } |
| | | 284 | | |
| | 10 | 285 | | _isRequestedTokenClosed = value; |
| | 10 | 286 | | } |
| | | 287 | | } |
| | | 288 | | |
| | 190 | 289 | | public bool IsReadOnly { get; private set; } |
| | | 290 | | |
| | 20 | 291 | | protected object ThisLock { get; } = new object(); |
| | | 292 | | |
| | 200 | 293 | | internal bool IsReceiver { get; } |
| | | 294 | | |
| | | 295 | | internal SecurityStandardsManager StandardsManager |
| | | 296 | | { |
| | | 297 | | get |
| | | 298 | | { |
| | 0 | 299 | | return _standardsManager; |
| | | 300 | | } |
| | | 301 | | set |
| | | 302 | | { |
| | 0 | 303 | | if (IsReadOnly) |
| | | 304 | | { |
| | 0 | 305 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 306 | | } |
| | | 307 | | |
| | 0 | 308 | | _standardsManager = (value ?? SecurityStandardsManager.DefaultInstance); |
| | 0 | 309 | | } |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | public SecurityToken EntropyToken |
| | | 313 | | { |
| | | 314 | | get |
| | | 315 | | { |
| | 0 | 316 | | if (IsReceiver) |
| | | 317 | | { |
| | 0 | 318 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 319 | | } |
| | 0 | 320 | | return _entropyToken; |
| | | 321 | | } |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | public SecurityToken RequestedSecurityToken |
| | | 325 | | { |
| | | 326 | | get |
| | | 327 | | { |
| | 40 | 328 | | if (IsReceiver) |
| | | 329 | | { |
| | 0 | 330 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 331 | | } |
| | 40 | 332 | | return _issuedToken; |
| | | 333 | | } |
| | | 334 | | set |
| | | 335 | | { |
| | 10 | 336 | | if (IsReadOnly) |
| | | 337 | | { |
| | 0 | 338 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 339 | | } |
| | | 340 | | |
| | 10 | 341 | | _issuedToken = value; |
| | 10 | 342 | | } |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | public SecurityToken RequestedProofToken |
| | | 346 | | { |
| | | 347 | | get |
| | | 348 | | { |
| | 10 | 349 | | if (IsReceiver) |
| | | 350 | | { |
| | 0 | 351 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 352 | | } |
| | 10 | 353 | | return _proofToken; |
| | | 354 | | } |
| | | 355 | | set |
| | | 356 | | { |
| | 0 | 357 | | if (IsReadOnly) |
| | | 358 | | { |
| | 0 | 359 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsR |
| | | 360 | | } |
| | | 361 | | |
| | 0 | 362 | | _proofToken = value; |
| | 0 | 363 | | } |
| | | 364 | | } |
| | | 365 | | |
| | | 366 | | public XmlElement RequestSecurityTokenResponseXml |
| | | 367 | | { |
| | | 368 | | get |
| | | 369 | | { |
| | 0 | 370 | | if (!IsReceiver) |
| | | 371 | | { |
| | 0 | 372 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 373 | | } |
| | 0 | 374 | | return _rstrXml; |
| | | 375 | | } |
| | | 376 | | } |
| | | 377 | | |
| | | 378 | | internal object AppliesTo |
| | | 379 | | { |
| | | 380 | | get |
| | | 381 | | { |
| | 20 | 382 | | if (IsReceiver) |
| | | 383 | | { |
| | 0 | 384 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 385 | | } |
| | 20 | 386 | | return _appliesTo; |
| | | 387 | | } |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | internal XmlObjectSerializer AppliesToSerializer |
| | | 391 | | { |
| | | 392 | | get |
| | | 393 | | { |
| | 0 | 394 | | if (IsReceiver) |
| | | 395 | | { |
| | 0 | 396 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 397 | | } |
| | 0 | 398 | | return _appliesToSerializer; |
| | | 399 | | } |
| | | 400 | | } |
| | | 401 | | |
| | | 402 | | internal Type AppliesToType |
| | | 403 | | { |
| | | 404 | | get |
| | | 405 | | { |
| | 0 | 406 | | if (IsReceiver) |
| | | 407 | | { |
| | 0 | 408 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 409 | | } |
| | 0 | 410 | | return _appliesToType; |
| | | 411 | | } |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | internal bool IsLifetimeSet |
| | | 415 | | { |
| | | 416 | | get |
| | | 417 | | { |
| | 30 | 418 | | if (IsReceiver) |
| | | 419 | | { |
| | 0 | 420 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 421 | | } |
| | 30 | 422 | | return _isLifetimeSet; |
| | | 423 | | } |
| | | 424 | | } |
| | | 425 | | |
| | 0 | 426 | | internal CoreWCF.XmlBuffer IssuedTokenBuffer { get; } |
| | | 427 | | |
| | | 428 | | public void SetIssuerEntropy(byte[] issuerEntropy) |
| | | 429 | | { |
| | 10 | 430 | | if (IsReadOnly) |
| | | 431 | | { |
| | 0 | 432 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 433 | | } |
| | | 434 | | |
| | 10 | 435 | | _entropyToken = (issuerEntropy != null) ? new NonceToken(issuerEntropy) : null; |
| | 10 | 436 | | } |
| | | 437 | | |
| | | 438 | | internal void SetIssuerEntropy(WrappedKeySecurityToken issuerEntropy) |
| | | 439 | | { |
| | 0 | 440 | | if (IsReadOnly) |
| | | 441 | | { |
| | 0 | 442 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 443 | | } |
| | | 444 | | |
| | 0 | 445 | | _entropyToken = issuerEntropy; |
| | 0 | 446 | | } |
| | | 447 | | |
| | | 448 | | public SecurityToken GetIssuerEntropy() |
| | | 449 | | { |
| | 20 | 450 | | return GetIssuerEntropy(null); |
| | | 451 | | } |
| | | 452 | | |
| | | 453 | | internal SecurityToken GetIssuerEntropy(SecurityTokenResolver resolver) |
| | | 454 | | { |
| | 20 | 455 | | if (IsReceiver) |
| | | 456 | | { |
| | 0 | 457 | | return _standardsManager.TrustDriver.GetEntropy(this, resolver); |
| | | 458 | | } |
| | | 459 | | else |
| | | 460 | | { |
| | 20 | 461 | | return _entropyToken; |
| | | 462 | | } |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | public void SetLifetime(DateTime validFrom, DateTime validTo) |
| | | 466 | | { |
| | 10 | 467 | | if (IsReadOnly) |
| | | 468 | | { |
| | 0 | 469 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 470 | | } |
| | | 471 | | |
| | 10 | 472 | | if (validFrom.ToUniversalTime() > validTo.ToUniversalTime()) |
| | | 473 | | { |
| | 0 | 474 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.EffectiveGreaterThanExpiration); |
| | | 475 | | } |
| | 10 | 476 | | ValidFrom = validFrom.ToUniversalTime(); |
| | 10 | 477 | | ValidTo = validTo.ToUniversalTime(); |
| | 10 | 478 | | _isLifetimeSet = true; |
| | 10 | 479 | | } |
| | | 480 | | |
| | | 481 | | public void SetAppliesTo<T>(T appliesTo, XmlObjectSerializer serializer) |
| | | 482 | | { |
| | 0 | 483 | | if (IsReadOnly) |
| | | 484 | | { |
| | 0 | 485 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 486 | | } |
| | | 487 | | |
| | 0 | 488 | | if (appliesTo != null && serializer == null) |
| | | 489 | | { |
| | 0 | 490 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer)); |
| | | 491 | | } |
| | 0 | 492 | | _appliesTo = appliesTo; |
| | 0 | 493 | | _appliesToSerializer = serializer; |
| | 0 | 494 | | _appliesToType = typeof(T); |
| | 0 | 495 | | } |
| | | 496 | | |
| | | 497 | | public void GetAppliesToQName(out string localName, out string namespaceUri) |
| | | 498 | | { |
| | 0 | 499 | | if (!IsReceiver) |
| | | 500 | | { |
| | 0 | 501 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Ite |
| | | 502 | | } |
| | | 503 | | |
| | 0 | 504 | | _standardsManager.TrustDriver.GetAppliesToQName(this, out localName, out namespaceUri); |
| | 0 | 505 | | } |
| | | 506 | | |
| | | 507 | | public T GetAppliesTo<T>() |
| | | 508 | | { |
| | 0 | 509 | | return GetAppliesTo<T>(DataContractSerializerDefaults.CreateSerializer(typeof(T), DataContractSerializerDefa |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | public T GetAppliesTo<T>(XmlObjectSerializer serializer) |
| | | 513 | | { |
| | 0 | 514 | | if (IsReceiver) |
| | | 515 | | { |
| | 0 | 516 | | if (serializer == null) |
| | | 517 | | { |
| | 0 | 518 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer)); |
| | | 519 | | } |
| | 0 | 520 | | return _standardsManager.TrustDriver.GetAppliesTo<T>(this, serializer); |
| | | 521 | | } |
| | | 522 | | else |
| | | 523 | | { |
| | 0 | 524 | | return (T)_appliesTo; |
| | | 525 | | } |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | internal void SetBinaryNegotiation(BinaryNegotiation negotiation) |
| | | 529 | | { |
| | 0 | 530 | | if (IsReadOnly) |
| | | 531 | | { |
| | 0 | 532 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 533 | | } |
| | | 534 | | |
| | 0 | 535 | | _negotiationData = negotiation ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ne |
| | 0 | 536 | | } |
| | | 537 | | |
| | | 538 | | internal BinaryNegotiation GetBinaryNegotiation() |
| | | 539 | | { |
| | 20 | 540 | | if (IsReceiver) |
| | | 541 | | { |
| | 0 | 542 | | return _standardsManager.TrustDriver.GetBinaryNegotiation(this); |
| | | 543 | | } |
| | | 544 | | else |
| | | 545 | | { |
| | 20 | 546 | | return _negotiationData; |
| | | 547 | | } |
| | | 548 | | } |
| | | 549 | | |
| | | 550 | | internal void SetAuthenticator(byte[] authenticator) |
| | | 551 | | { |
| | 0 | 552 | | if (authenticator == null) |
| | | 553 | | { |
| | 0 | 554 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(authenticator)); |
| | | 555 | | } |
| | | 556 | | |
| | 0 | 557 | | if (IsReadOnly) |
| | | 558 | | { |
| | 0 | 559 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 560 | | } |
| | | 561 | | |
| | 0 | 562 | | _authenticator = Fx.AllocateByteArray(authenticator.Length); |
| | 0 | 563 | | Buffer.BlockCopy(authenticator, 0, _authenticator, 0, authenticator.Length); |
| | 0 | 564 | | } |
| | | 565 | | |
| | | 566 | | internal byte[] GetAuthenticator() |
| | | 567 | | { |
| | 20 | 568 | | if (IsReceiver) |
| | | 569 | | { |
| | 0 | 570 | | return _standardsManager.TrustDriver.GetAuthenticator(this); |
| | | 571 | | } |
| | | 572 | | else |
| | | 573 | | { |
| | 20 | 574 | | if (_authenticator == null) |
| | | 575 | | { |
| | 20 | 576 | | return null; |
| | | 577 | | } |
| | | 578 | | else |
| | | 579 | | { |
| | 0 | 580 | | byte[] result = Fx.AllocateByteArray(_authenticator.Length); |
| | 0 | 581 | | Buffer.BlockCopy(_authenticator, 0, result, 0, _authenticator.Length); |
| | 0 | 582 | | return result; |
| | | 583 | | } |
| | | 584 | | } |
| | | 585 | | } |
| | | 586 | | |
| | | 587 | | private void OnWriteTo(XmlWriter w) |
| | | 588 | | { |
| | 20 | 589 | | if (IsReceiver) |
| | | 590 | | { |
| | 0 | 591 | | _rstrXml.WriteTo(w); |
| | | 592 | | } |
| | | 593 | | else |
| | | 594 | | { |
| | 20 | 595 | | _standardsManager.TrustDriver.WriteRequestSecurityTokenResponse(this, w); |
| | | 596 | | } |
| | 20 | 597 | | } |
| | | 598 | | |
| | | 599 | | public void WriteTo(XmlWriter writer) |
| | | 600 | | { |
| | 20 | 601 | | if (writer == null) |
| | | 602 | | { |
| | 0 | 603 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 604 | | } |
| | | 605 | | |
| | 20 | 606 | | if (IsReadOnly) |
| | | 607 | | { |
| | | 608 | | // cache the serialized bytes to ensure repeatability |
| | 20 | 609 | | if (_cachedWriteBuffer == null) |
| | | 610 | | { |
| | 20 | 611 | | MemoryStream stream = new MemoryStream(); |
| | 20 | 612 | | using (XmlDictionaryWriter binaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream, XD.Dictiona |
| | | 613 | | { |
| | 20 | 614 | | OnWriteTo(binaryWriter); |
| | 20 | 615 | | binaryWriter.Flush(); |
| | 20 | 616 | | stream.Flush(); |
| | 20 | 617 | | stream.Seek(0, SeekOrigin.Begin); |
| | 20 | 618 | | _cachedWriteBuffer = stream.GetBuffer(); |
| | 20 | 619 | | _cachedWriteBufferLength = (int)stream.Length; |
| | 20 | 620 | | } |
| | | 621 | | } |
| | 20 | 622 | | writer.WriteNode(XmlDictionaryReader.CreateBinaryReader(_cachedWriteBuffer, 0, _cachedWriteBufferLength, |
| | | 623 | | } |
| | | 624 | | else |
| | | 625 | | { |
| | 0 | 626 | | OnWriteTo(writer); |
| | | 627 | | } |
| | 0 | 628 | | } |
| | | 629 | | |
| | | 630 | | public static RequestSecurityTokenResponse CreateFrom(XmlReader reader) |
| | | 631 | | { |
| | 0 | 632 | | return CreateFrom(SecurityStandardsManager.DefaultInstance, reader); |
| | | 633 | | } |
| | | 634 | | |
| | | 635 | | public static RequestSecurityTokenResponse CreateFrom(XmlReader reader, MessageSecurityVersion messageSecurityVe |
| | | 636 | | { |
| | 0 | 637 | | return CreateFrom(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializ |
| | | 638 | | } |
| | | 639 | | |
| | | 640 | | internal static RequestSecurityTokenResponse CreateFrom(SecurityStandardsManager standardsManager, XmlReader rea |
| | | 641 | | { |
| | 0 | 642 | | return standardsManager.TrustDriver.CreateRequestSecurityTokenResponse(reader); |
| | | 643 | | } |
| | | 644 | | |
| | | 645 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 646 | | { |
| | 12 | 647 | | WriteTo(writer); |
| | 12 | 648 | | } |
| | | 649 | | |
| | | 650 | | public void MakeReadOnly() |
| | | 651 | | { |
| | 20 | 652 | | if (!IsReadOnly) |
| | | 653 | | { |
| | 20 | 654 | | IsReadOnly = true; |
| | 20 | 655 | | OnMakeReadOnly(); |
| | | 656 | | } |
| | 20 | 657 | | } |
| | | 658 | | |
| | | 659 | | public GenericXmlSecurityToken GetIssuedToken(SecurityTokenResolver resolver, IList<SecurityTokenAuthenticator> |
| | | 660 | | ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies) |
| | | 661 | | { |
| | 0 | 662 | | return GetIssuedToken(resolver, allowedAuthenticators, keyEntropyMode, requestorEntropy, expectedTokenType, |
| | | 663 | | } |
| | | 664 | | |
| | | 665 | | public virtual GenericXmlSecurityToken GetIssuedToken(SecurityTokenResolver resolver, IList<SecurityTokenAuthent |
| | | 666 | | ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies, int defaultKeySize, bool isBearerKeyType) |
| | | 667 | | { |
| | 0 | 668 | | if (!IsReceiver) |
| | | 669 | | { |
| | 0 | 670 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Ite |
| | | 671 | | } |
| | | 672 | | |
| | 0 | 673 | | return _standardsManager.TrustDriver.GetIssuedToken(this, resolver, allowedAuthenticators, keyEntropyMode, r |
| | | 674 | | } |
| | | 675 | | |
| | | 676 | | public virtual GenericXmlSecurityToken GetIssuedToken(string expectedTokenType, ReadOnlyCollection<IAuthorizatio |
| | | 677 | | { |
| | 0 | 678 | | if (!IsReceiver) |
| | | 679 | | { |
| | 0 | 680 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Ite |
| | | 681 | | } |
| | | 682 | | |
| | 0 | 683 | | return _standardsManager.TrustDriver.GetIssuedToken(this, expectedTokenType, authorizationPolicies, clientKe |
| | | 684 | | } |
| | | 685 | | |
| | | 686 | | protected internal virtual void OnWriteCustomAttributes(XmlWriter writer) |
| | 20 | 687 | | { } |
| | | 688 | | |
| | | 689 | | protected internal virtual void OnWriteCustomElements(XmlWriter writer) |
| | 20 | 690 | | { } |
| | | 691 | | |
| | 20 | 692 | | protected virtual void OnMakeReadOnly() { } |
| | | 693 | | |
| | | 694 | | public static byte[] ComputeCombinedKey(byte[] requestorEntropy, byte[] issuerEntropy, int keySizeInBits) |
| | | 695 | | { |
| | 10 | 696 | | if (requestorEntropy == null) |
| | | 697 | | { |
| | 0 | 698 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(requestorEntropy)); |
| | | 699 | | } |
| | | 700 | | |
| | 10 | 701 | | if (issuerEntropy == null) |
| | | 702 | | { |
| | 0 | 703 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuerEntropy)); |
| | | 704 | | } |
| | | 705 | | // Do a sanity check here. We don't want to allow invalid keys or keys that are too |
| | | 706 | | // large. |
| | 10 | 707 | | if ((keySizeInBits < s_minSaneKeySizeInBits) || (keySizeInBits > s_maxSaneKeySizeInBits)) |
| | | 708 | | { |
| | 0 | 709 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(SR.Format(SR. |
| | | 710 | | } |
| | | 711 | | |
| | 10 | 712 | | Psha1DerivedKeyGenerator generator = new Psha1DerivedKeyGenerator(requestorEntropy); |
| | 10 | 713 | | return generator.GenerateDerivedKey(Array.Empty<byte>(), issuerEntropy, keySizeInBits, 0); |
| | | 714 | | } |
| | | 715 | | } |
| | | 716 | | } |