1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Security.Tokens
{
using System.Collections.ObjectModel;
using System.IdentityModel;
using System.IdentityModel.Tokens;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.ServiceModel.Security;
using System.Xml;
[TypeForwardedFrom("System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class WrappedKeySecurityToken : SecurityToken
{
string id;
DateTime effectiveTime;
EncryptedKey encryptedKey;
ReadOnlyCollection<SecurityKey> securityKey;
byte[] wrappedKey;
string wrappingAlgorithm;
ISspiNegotiation wrappingSspiContext;
SecurityToken wrappingToken;
SecurityKey wrappingSecurityKey;
SecurityKeyIdentifier wrappingTokenReference;
bool serializeCarriedKeyName;
byte[] wrappedKeyHash;
XmlDictionaryString wrappingAlgorithmDictionaryString;
// sender use
internal WrappedKeySecurityToken(string id, byte[] keyToWrap, ISspiNegotiation wrappingSspiContext)
: this(id, keyToWrap, (wrappingSspiContext != null) ? (wrappingSspiContext.KeyEncryptionAlgorithm) : null, wrappingSspiContext, null)
{
}
// sender use
public WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, SecurityToken wrappingToken, SecurityKeyIdentifier wrappingTokenReference)
: this(id, keyToWrap, wrappingAlgorithm, null, wrappingToken, wrappingTokenReference)
{
}
internal WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, XmlDictionaryString wrappingAlgorithmDictionaryString, SecurityToken wrappingToken, SecurityKeyIdentifier wrappingTokenReference)
: this(id, keyToWrap, wrappingAlgorithm, wrappingAlgorithmDictionaryString, wrappingToken, wrappingTokenReference, null, null)
{
}
// direct receiver use, chained sender use
internal WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, ISspiNegotiation wrappingSspiContext, byte[] wrappedKey)
: this(id, keyToWrap, wrappingAlgorithm, null)
{
if (wrappingSspiContext == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("wrappingSspiContext");
}
this.wrappingSspiContext = wrappingSspiContext;
if (wrappedKey == null)
{
this.wrappedKey = wrappingSspiContext.Encrypt(keyToWrap);
}
else
{
this.wrappedKey = wrappedKey;
}
this.serializeCarriedKeyName = false;
}
// receiver use
internal WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, SecurityToken wrappingToken, SecurityKeyIdentifier wrappingTokenReference, byte[] wrappedKey, SecurityKey wrappingSecurityKey)
: this(id, keyToWrap, wrappingAlgorithm, null, wrappingToken, wrappingTokenReference, wrappedKey, wrappingSecurityKey)
{
}
WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, XmlDictionaryString wrappingAlgorithmDictionaryString, SecurityToken wrappingToken, SecurityKeyIdentifier wrappingTokenReference, byte[] wrappedKey, SecurityKey wrappingSecurityKey)
: this(id, keyToWrap, wrappingAlgorithm, wrappingAlgorithmDictionaryString)
{
if (wrappingToken == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("wrappingToken");
}
this.wrappingToken = wrappingToken;
this.wrappingTokenReference = wrappingTokenReference;
if (wrappedKey == null)
{
this.wrappedKey = SecurityUtils.EncryptKey(wrappingToken, wrappingAlgorithm, keyToWrap);
}
else
{
this.wrappedKey = wrappedKey;
}
this.wrappingSecurityKey = wrappingSecurityKey;
this.serializeCarriedKeyName = true;
}
WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, XmlDictionaryString wrappingAlgorithmDictionaryString)
{
if (id == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("id");
if (wrappingAlgorithm == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("wrappingAlgorithm");
if (keyToWrap == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("securityKeyToWrap");
this.id = id;
this.effectiveTime = DateTime.UtcNow;
this.securityKey = SecurityUtils.CreateSymmetricSecurityKeys(keyToWrap);
this.wrappingAlgorithm = wrappingAlgorithm;
this.wrappingAlgorithmDictionaryString = wrappingAlgorithmDictionaryString;
}
public override string Id
{
get { return this.id; }
}
public override DateTime ValidFrom
{
get { return this.effectiveTime; }
}
public override DateTime ValidTo
{
// Never expire
get { return DateTime.MaxValue; }
}
internal EncryptedKey EncryptedKey
{
get { return this.encryptedKey; }
set { this.encryptedKey = value; }
}
internal ReferenceList ReferenceList
{
get
{
return this.encryptedKey == null ? null : this.encryptedKey.ReferenceList;
}
}
public string WrappingAlgorithm
{
get { return this.wrappingAlgorithm; }
}
internal SecurityKey WrappingSecurityKey
{
get { return this.wrappingSecurityKey; }
}
public SecurityToken WrappingToken
{
get { return this.wrappingToken; }
}
public SecurityKeyIdentifier WrappingTokenReference
{
get { return this.wrappingTokenReference; }
}
internal string CarriedKeyName
{
get { return null; }
}
public override ReadOnlyCollection<SecurityKey> SecurityKeys
{
get { return this.securityKey; }
}
internal byte[] GetHash()
{
if (this.wrappedKeyHash == null)
{
EnsureEncryptedKeySetUp();
using (HashAlgorithm hash = CryptoHelper.NewSha1HashAlgorithm())
{
this.wrappedKeyHash = hash.ComputeHash(this.encryptedKey.GetWrappedKey());
}
}
return wrappedKeyHash;
}
public byte[] GetWrappedKey()
{
return SecurityUtils.CloneBuffer(this.wrappedKey);
}
internal void EnsureEncryptedKeySetUp()
{
if (this.encryptedKey == null)
{
EncryptedKey ek = new EncryptedKey();
ek.Id = this.Id;
if (this.serializeCarriedKeyName)
{
ek.CarriedKeyName = this.CarriedKeyName;
}
else
{
ek.CarriedKeyName = null;
}
ek.EncryptionMethod = this.WrappingAlgorithm;
ek.EncryptionMethodDictionaryString = this.wrappingAlgorithmDictionaryString;
ek.SetUpKeyWrap(this.wrappedKey);
if (this.WrappingTokenReference != null)
{
ek.KeyIdentifier = this.WrappingTokenReference;
}
this.encryptedKey = ek;
}
}
public override bool CanCreateKeyIdentifierClause<T>()
{
if (typeof(T) == typeof(EncryptedKeyHashIdentifierClause))
return true;
return base.CanCreateKeyIdentifierClause<T>();
}
public override T CreateKeyIdentifierClause<T>()
{
if (typeof(T) == typeof(EncryptedKeyHashIdentifierClause))
return new EncryptedKeyHashIdentifierClause(GetHash()) as T;
return base.CreateKeyIdentifierClause<T>();
}
public override bool MatchesKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause)
{
EncryptedKeyHashIdentifierClause encKeyIdentifierClause = keyIdentifierClause as EncryptedKeyHashIdentifierClause;
if (encKeyIdentifierClause != null)
return encKeyIdentifierClause.Matches(GetHash());
return base.MatchesKeyIdentifierClause(keyIdentifierClause);
}
}
}
|