| | | 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.Globalization; |
| | | 6 | | using System.Text; |
| | | 7 | | using CoreWCF.IdentityModel; |
| | | 8 | | using CoreWCF.IdentityModel.Selectors; |
| | | 9 | | using CoreWCF.IdentityModel.Tokens; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Security.Tokens |
| | | 12 | | { |
| | | 13 | | public class X509SecurityTokenParameters : SecurityTokenParameters |
| | | 14 | | { |
| | | 15 | | internal const X509KeyIdentifierClauseType defaultX509ReferenceStyle = X509KeyIdentifierClauseType.Any; |
| | | 16 | | private X509KeyIdentifierClauseType _x509ReferenceStyle; |
| | | 17 | | |
| | | 18 | | protected X509SecurityTokenParameters(X509SecurityTokenParameters other) |
| | 308 | 19 | | : base(other) |
| | | 20 | | { |
| | 308 | 21 | | _x509ReferenceStyle = other._x509ReferenceStyle; |
| | 308 | 22 | | } |
| | | 23 | | |
| | | 24 | | public X509SecurityTokenParameters() |
| | 0 | 25 | | : this(defaultX509ReferenceStyle, defaultInclusionMode) |
| | | 26 | | { |
| | | 27 | | // empty |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public X509SecurityTokenParameters(X509KeyIdentifierClauseType x509ReferenceStyle) |
| | 0 | 31 | | : this(x509ReferenceStyle, defaultInclusionMode) |
| | | 32 | | { |
| | | 33 | | // empty |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public X509SecurityTokenParameters(X509KeyIdentifierClauseType x509ReferenceStyle, SecurityTokenInclusionMode in |
| | 0 | 37 | | : this(x509ReferenceStyle, inclusionMode, defaultRequireDerivedKeys) |
| | | 38 | | { |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | internal X509SecurityTokenParameters(X509KeyIdentifierClauseType x509ReferenceStyle, SecurityTokenInclusionMode |
| | | 42 | | bool requireDerivedKeys) |
| | 33 | 43 | | : base() |
| | | 44 | | { |
| | 33 | 45 | | X509ReferenceStyle = x509ReferenceStyle; |
| | 33 | 46 | | InclusionMode = inclusionMode; |
| | 33 | 47 | | RequireDerivedKeys = requireDerivedKeys; |
| | 33 | 48 | | } |
| | | 49 | | |
| | 17 | 50 | | protected internal override bool HasAsymmetricKey { get { return true; } } |
| | | 51 | | |
| | | 52 | | public X509KeyIdentifierClauseType X509ReferenceStyle |
| | | 53 | | { |
| | | 54 | | get |
| | | 55 | | { |
| | 8 | 56 | | return _x509ReferenceStyle; |
| | | 57 | | } |
| | | 58 | | set |
| | | 59 | | { |
| | 33 | 60 | | X509SecurityTokenReferenceStyleHelper.Validate(value); |
| | 33 | 61 | | _x509ReferenceStyle = value; |
| | 33 | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | 81 | 65 | | protected internal override bool SupportsClientAuthentication { get { return true; } } |
| | 0 | 66 | | protected internal override bool SupportsServerAuthentication { get { return true; } } |
| | 81 | 67 | | protected internal override bool SupportsClientWindowsIdentity { get { return true; } } |
| | | 68 | | |
| | | 69 | | protected override SecurityTokenParameters CloneCore() |
| | | 70 | | { |
| | 308 | 71 | | return new X509SecurityTokenParameters(this); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | protected internal override SecurityKeyIdentifierClause CreateKeyIdentifierClause(SecurityToken token, SecurityT |
| | | 75 | | { |
| | 0 | 76 | | SecurityKeyIdentifierClause result = null; |
| | | 77 | | |
| | 0 | 78 | | switch (_x509ReferenceStyle) |
| | | 79 | | { |
| | | 80 | | default: |
| | | 81 | | case X509KeyIdentifierClauseType.Any: |
| | 0 | 82 | | if (referenceStyle == SecurityTokenReferenceStyle.External) |
| | | 83 | | { |
| | 0 | 84 | | if (token is X509SecurityToken x509Token) |
| | | 85 | | { |
| | 0 | 86 | | if (X509SubjectKeyIdentifierClause.TryCreateFrom(x509Token.Certificate, out X509SubjectKeyId |
| | | 87 | | { |
| | 0 | 88 | | result = x509KeyIdentifierClause; |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | else |
| | | 92 | | { |
| | 0 | 93 | | if (token is X509WindowsSecurityToken windowsX509Token) |
| | | 94 | | { |
| | 0 | 95 | | if (X509SubjectKeyIdentifierClause.TryCreateFrom(windowsX509Token.Certificate, out X509S |
| | | 96 | | { |
| | 0 | 97 | | result = x509KeyIdentifierClause; |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | if (result == null) |
| | | 103 | | { |
| | 0 | 104 | | result = token.CreateKeyIdentifierClause<X509IssuerSerialKeyIdentifierClause>(); |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | if (result == null) |
| | | 108 | | { |
| | 0 | 109 | | result = token.CreateKeyIdentifierClause<X509ThumbprintKeyIdentifierClause>(); |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | else |
| | | 113 | | { |
| | 0 | 114 | | result = token.CreateKeyIdentifierClause<LocalIdKeyIdentifierClause>(); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | break; |
| | | 118 | | case X509KeyIdentifierClauseType.Thumbprint: |
| | 0 | 119 | | result = CreateKeyIdentifierClause<X509ThumbprintKeyIdentifierClause, LocalIdKeyIdentifierClause>(to |
| | 0 | 120 | | break; |
| | | 121 | | case X509KeyIdentifierClauseType.SubjectKeyIdentifier: |
| | 0 | 122 | | result = CreateKeyIdentifierClause<X509SubjectKeyIdentifierClause, LocalIdKeyIdentifierClause>(token |
| | 0 | 123 | | break; |
| | | 124 | | case X509KeyIdentifierClauseType.IssuerSerial: |
| | 0 | 125 | | result = CreateKeyIdentifierClause<X509IssuerSerialKeyIdentifierClause, LocalIdKeyIdentifierClause>( |
| | 0 | 126 | | break; |
| | | 127 | | case X509KeyIdentifierClauseType.RawDataKeyIdentifier: |
| | 0 | 128 | | result = CreateKeyIdentifierClause<X509RawDataKeyIdentifierClause, LocalIdKeyIdentifierClause>(token |
| | | 129 | | break; |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | return result; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | protected internal override void InitializeSecurityTokenRequirement(SecurityTokenRequirement requirement) |
| | | 136 | | { |
| | 11 | 137 | | requirement.TokenType = SecurityTokenTypes.X509Certificate; |
| | 11 | 138 | | requirement.RequireCryptographicToken = true; |
| | 11 | 139 | | requirement.KeyType = SecurityKeyType.AsymmetricKey; |
| | 11 | 140 | | } |
| | | 141 | | |
| | | 142 | | public override string ToString() |
| | | 143 | | { |
| | 0 | 144 | | StringBuilder sb = new StringBuilder(); |
| | 0 | 145 | | sb.AppendLine(base.ToString()); |
| | | 146 | | |
| | 0 | 147 | | sb.Append(string.Format(CultureInfo.InvariantCulture, "X509ReferenceStyle: {0}", _x509ReferenceStyle.ToStrin |
| | | 148 | | |
| | 0 | 149 | | return sb.ToString(); |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |