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
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Security
{
using System.IdentityModel;
using System.Runtime.CompilerServices;
using System.Xml;
[TypeForwardedFrom("System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
sealed class EncryptedKey : EncryptedType
{
internal static readonly XmlDictionaryString CarriedKeyElementName = XD.XmlEncryptionDictionary.CarriedKeyName;
internal static readonly XmlDictionaryString ElementName = XD.XmlEncryptionDictionary.EncryptedKey;
internal static readonly XmlDictionaryString RecipientAttribute = XD.XmlEncryptionDictionary.Recipient;
string carriedKeyName;
string recipient;
ReferenceList referenceList;
byte[] wrappedKey;
public string CarriedKeyName
{
get { return this.carriedKeyName; }
set { this.carriedKeyName = value; }
}
public string Recipient
{
get { return this.recipient; }
set { this.recipient = value; }
}
public ReferenceList ReferenceList
{
get { return this.referenceList; }
set { this.referenceList = value; }
}
protected override XmlDictionaryString OpeningElementName
{
get { return ElementName; }
}
protected override void ForceEncryption()
{
// no work to be done here since, unlike bulk encryption, key wrapping is done eagerly
}
public byte[] GetWrappedKey()
{
if (this.State == EncryptionState.New)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.BadEncryptionState)));
}
return this.wrappedKey;
}
public void SetUpKeyWrap(byte[] wrappedKey)
{
if (this.State != EncryptionState.New)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.BadEncryptionState)));
}
if (wrappedKey == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("wrappedKey");
}
this.wrappedKey = wrappedKey;
this.State = EncryptionState.Encrypted;
}
protected override void ReadAdditionalAttributes(XmlDictionaryReader reader)
{
this.recipient = reader.GetAttribute(RecipientAttribute, null);
}
protected override void ReadAdditionalElements(XmlDictionaryReader reader)
{
if (reader.IsStartElement(ReferenceList.ElementName, EncryptedType.NamespaceUri))
{
this.referenceList = new ReferenceList();
this.referenceList.ReadFrom(reader);
}
if (reader.IsStartElement(CarriedKeyElementName, EncryptedType.NamespaceUri))
{
reader.ReadStartElement(CarriedKeyElementName, EncryptedType.NamespaceUri);
this.carriedKeyName = reader.ReadString();
reader.ReadEndElement();
}
}
protected override void ReadCipherData(XmlDictionaryReader reader)
{
this.wrappedKey = reader.ReadContentAsBase64();
}
protected override void ReadCipherData(XmlDictionaryReader reader, long maxBufferSize)
{
this.wrappedKey = SecurityUtils.ReadContentAsBase64(reader, maxBufferSize);
}
protected override void WriteAdditionalAttributes(XmlDictionaryWriter writer, DictionaryManager dictionaryManager)
{
if (this.recipient != null)
{
writer.WriteAttributeString(RecipientAttribute, null, this.recipient);
}
}
protected override void WriteAdditionalElements(XmlDictionaryWriter writer, DictionaryManager dictionaryManager)
{
if (this.carriedKeyName != null)
{
writer.WriteStartElement(CarriedKeyElementName, EncryptedType.NamespaceUri);
writer.WriteString(this.carriedKeyName);
writer.WriteEndElement(); // CarriedKeyName
}
if (this.referenceList != null)
{
this.referenceList.WriteTo(writer, dictionaryManager);
}
}
protected override void WriteCipherData(XmlDictionaryWriter writer)
{
writer.WriteBase64(this.wrappedKey, 0, this.wrappedKey.Length);
}
}
}
|