protected virtual ValueTask <RequestedProofToken> CreateRequestedProofTokenAsync(WsTrustSecurityTokenDescriptor descriptor, CancellationToken cancellationToken)
        {
            if (descriptor.ProofKey == null)
            {
                return(new ValueTask <RequestedProofToken>());
            }
            if (!(descriptor.ProofKey is SymmetricSecurityKey symmetric))
            {
                throw new NotSupportedException($"Asymmetric proof keys not supported for now.");
            }

            // The microsoft ws-trust serializer can't handle encrypted RequestedProofToken. Hard code unencrypted for now.
            var secret = new BinarySecret(symmetric.Key, Constants.WsTrustKeyTypes.Symmetric);

            return(new ValueTask <RequestedProofToken>(new RequestedProofToken(secret)));
        }
예제 #2
0
        //public override KeyInfo ReadKeyInfo(XmlReader reader)
        //{
        //    const string xmlenc = "http://www.w3.org/2001/04/xmlenc#";

        //    // buffer element
        //    var document = XDocument.Load(reader.ReadSubtree());
        //    var keyInfo = document.Root;
        //    var child = keyInfo.Nodes().OfType<XElement>().FirstOrDefault();
        //    reader.Read();

        //    if (child != null)
        //    {
        //        if (child.Name == XName.Get("EncryptedKey", xmlenc))
        //        {

        //        }

        //        if (child.Name == XName.Get("BinarySecret", WsTrustConstants.TrustFeb2005.Namespace) ||
        //            child.Name == XName.Get("BinarySecret", WsTrustConstants.Trust13.Namespace) ||
        //            child.Name == XName.Get("BinarySecret", WsTrustConstants.Trust14.Namespace))
        //        {
        //            var base64 = child.Value;
        //            var key = Convert.FromBase64String(base64);

        //            return new BinarySecretKeyInfo
        //            {
        //                Key = key
        //            };
        //        }

        //        if (child.Name == XName.Get("SecurityTokenReference", WsSecurityConstants.WsSecurity10.Namespace))
        //        {
        //            var keyIdentifier = child.Nodes().OfType<XElement>().FirstOrDefault(e => e.Name == XName.Get("KeyIdentifier", WsSecurityConstants.WsSecurity10.Namespace));
        //            var valueType = keyIdentifier?.Attribute(XName.Get("ValueType", string.Empty));
        //            var id = keyIdentifier?.Value;

        //            return new SecurityTokenReferenceKeyInfo
        //            {
        //                KeyIdValueType = valueType?.Value,
        //                KeyId = id
        //            };
        //        }
        //    }

        //    using (var temp = document.CreateReader())
        //    {
        //        temp.Settings.IgnoreWhitespace = true;
        //        temp.Read();
        //        return base.ReadKeyInfo(temp);
        //    }
        //}

        public override void WriteKeyInfo(XmlWriter writer, KeyInfo keyInfo)
        {
            if (keyInfo is EncryptedKeyInfo encrypted)
            {
                const string xmlenc = "http://www.w3.org/2001/04/xmlenc#";

                // <KeyInfo>
                writer.WriteStartElement(XmlSignatureConstants.Elements.KeyInfo, XmlSignatureConstants.Namespace);
                writer.WriteStartElement("EncryptedKey", xmlenc);

                writer.WriteStartElement("EncryptionMethod", xmlenc);
                writer.WriteAttributeString("Algorithm", encrypted.EncryptionMethod);
                if (!string.IsNullOrEmpty(encrypted.DigestMethod))
                {
                    writer.WriteStartElement("DigestMethod", XmlSignatureConstants.Namespace);
                    writer.WriteAttributeString("Algorithm", encrypted.DigestMethod);
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();

                WriteKeyInfo(writer, encrypted.KeyInfo);

                writer.WriteStartElement("CipherData", xmlenc);
                writer.WriteStartElement("CipherValue", xmlenc);
                writer.WriteString(Convert.ToBase64String(encrypted.CipherValue));
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndElement();
                return;
            }

            if (keyInfo is BinarySecretKeyInfo binary)
            {
                // <KeyInfo>
                writer.WriteStartElement(XmlSignatureConstants.Elements.KeyInfo, XmlSignatureConstants.Namespace);
                var binarySecret        = new BinarySecret(binary.Key);
                var xmlDictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);
                var version             = null as WsTrustVersion;
                if (xmlDictionaryWriter.LookupPrefix(WsTrustConstants.TrustFeb2005.Namespace) != null)
                {
                    version = WsTrustVersion.TrustFeb2005;
                }
                else if (xmlDictionaryWriter.LookupPrefix(WsTrustConstants.Trust14.Namespace) != null)
                {
                    version = WsTrustVersion.Trust14;
                }
                else
                {
                    version = WsTrustVersion.Trust13; // default to Trust 1.3
                }
                WsTrustSerializer.WriteBinarySecret(xmlDictionaryWriter, new WsSerializationContext(version), binarySecret);
                writer.WriteEndElement();
                return;
            }

            // this might be a bit naive
            if (keyInfo is SecurityTokenReferenceKeyInfo securityTokenReference)
            {
                // <KeyInfo>
                writer.WriteStartElement(XmlSignatureConstants.Elements.KeyInfo, XmlSignatureConstants.Namespace);
                writer.WriteStartElement(WsSecurityConstants.WsSecurity10.Prefix, "SecurityTokenReference", WsSecurityConstants.WsSecurity10.Namespace);
                writer.WriteStartElement(WsSecurityConstants.WsSecurity10.Prefix, "KeyIdentifier", WsSecurityConstants.WsSecurity10.Namespace);
                writer.WriteAttributeString("ValueType", securityTokenReference.KeyIdValueType);

                writer.WriteString(securityTokenReference.KeyId);

                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndElement();
                return;
            }

            base.WriteKeyInfo(writer, keyInfo);
        }