private void ReadFrom(XmlDictionaryReader reader)
        {
            try
            {
                reader.ReadFullStartElement(this.coordinationXmlDictionaryStrings.CreateCoordinationContext, this.coordinationXmlDictionaryStrings.Namespace);
                if (reader.IsStartElement(this.coordinationXmlDictionaryStrings.Expires, this.coordinationXmlDictionaryStrings.Namespace))
                {
                    int num = reader.ReadElementContentAsInt();
                    if (num < 0)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(Microsoft.Transactions.SR.GetString("InvalidMessageBody")));
                    }
                    this.expiration     = (uint)num;
                    this.expiresPresent = true;
                }
                if (reader.IsStartElement(this.coordinationXmlDictionaryStrings.CurrentContext, this.coordinationXmlDictionaryStrings.Namespace))
                {
                    this.CurrentContext = CoordinationContext.ReadFrom(reader, this.coordinationXmlDictionaryStrings.CurrentContext, this.coordinationXmlDictionaryStrings.Namespace, this.protocolVersion);
                }
                reader.MoveToStartElement(this.coordinationXmlDictionaryStrings.CoordinationType, this.coordinationXmlDictionaryStrings.Namespace);
                if (reader.ReadElementContentAsString().Trim() != this.atomicTransactionStrings.Namespace)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(Microsoft.Transactions.SR.GetString("InvalidMessageBody")));
                }
                if (!reader.IsStartElement(XD.DotNetAtomicTransactionExternalDictionary.IsolationLevel, XD.DotNetAtomicTransactionExternalDictionary.Namespace))
                {
                    goto Label_016B;
                }
                this.IsolationLevel = (System.Transactions.IsolationLevel)reader.ReadElementContentAsInt();
                if (((this.IsolationLevel >= System.Transactions.IsolationLevel.Serializable) && (this.IsolationLevel <= System.Transactions.IsolationLevel.Unspecified)) && (this.IsolationLevel != System.Transactions.IsolationLevel.Snapshot))
                {
                    goto Label_016B;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(Microsoft.Transactions.SR.GetString("InvalidMessageBody")));
Label_0165:
                reader.Skip();
Label_016B:
                if (reader.IsStartElement())
                {
                    goto Label_0165;
                }
                reader.ReadEndElement();
            }
            catch (XmlException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(Microsoft.Transactions.SR.GetString("InvalidMessageBody"), exception));
            }
            catch (InvalidCoordinationContextException exception2)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(Microsoft.Transactions.SR.GetString("InvalidMessageBody"), exception2));
            }
        }
        static IAuthorizationPolicy RetrievePolicyFromBlob(byte[] contextBlob, string id, DateTime expirationTime, IList <Type> knownTypes)
        {
            if (contextBlob == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contextBlob");
            }

            if (id == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("id");
            }

            SctClaimDictionary  claimDictionary = SctClaimDictionary.Instance;
            XmlDictionaryReader reader          = XmlDictionaryReader.CreateBinaryReader(contextBlob, 0, contextBlob.Length, claimDictionary, XmlDictionaryReaderQuotas.Max, null, null);
            IList <IIdentity>   identities      = null;
            IList <ClaimSet>    claimSets       = null;
            int versionNumber = -1;

            reader.ReadFullStartElement(claimDictionary.SecurityContextSecurityToken, claimDictionary.EmptyString);

            while (reader.IsStartElement())
            {
                if (reader.IsStartElement(claimDictionary.Version, claimDictionary.EmptyString))
                {
                    versionNumber = reader.ReadElementContentAsInt();

                    if (versionNumber != 1)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.SerializedAuthorizationContextVersionUnsupported, versionNumber)));
                    }
                }
                else
                {
                    if (reader.IsStartElement(claimDictionary.Identities, claimDictionary.EmptyString))
                    {
                        identities = SctClaimSerializer.DeserializeIdentities(reader, claimDictionary, DataContractSerializerDefaults.CreateSerializer(typeof(IIdentity), knownTypes, 0x7fffffff));
                        continue;
                    }
                    if (reader.IsStartElement(claimDictionary.ClaimSets, claimDictionary.EmptyString))
                    {
                        reader.ReadStartElement();
                        DataContractSerializer claimSetSerializer = DataContractSerializerDefaults.CreateSerializer(typeof(ClaimSet), knownTypes, 0x7fffffff);
                        DataContractSerializer claimSerializer    = DataContractSerializerDefaults.CreateSerializer(typeof(Claim), knownTypes, 0x7fffffff);
                        claimSets = new List <ClaimSet>(1);

                        while (reader.IsStartElement())
                        {
                            claimSets.Add(SctClaimSerializer.DeserializeClaimSet(reader, claimDictionary, claimSetSerializer, claimSerializer));
                        }

                        reader.ReadEndElement();
                        continue;
                    }
                }
            }
            reader.ReadEndElement();
            return(new SctUnconditionalPolicy(identities, id, claimSets, expirationTime));
        }
Beispiel #3
0
        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, byte[] array, int offset, int count)
        {
            XmlJsonReader.CheckArray(array, offset, count);
            int actual = 0;

            while (actual < count && reader.IsStartElement(JsonGlobals.itemString, string.Empty))
            {
                array[offset + actual] = ToByte(reader.ReadElementContentAsInt());
                actual++;
            }
            return(actual);
        }
Beispiel #4
0
        protected override int ReadArray(XmlDictionaryReader reader, string localName, string namespaceUri, byte[] array, int offset, int count)
        {
            XmlJsonReader.CheckArray(array, offset, count);
            int num = 0;

            while ((num < count) && reader.IsStartElement("item", string.Empty))
            {
                array[offset + num] = this.ToByte(reader.ReadElementContentAsInt());
                num++;
            }
            return(num);
        }
        void ReadTokenContent()
        {
            switch (reader.NamespaceURI)
            {
            case Constants.WstNamespace:
                switch (reader.LocalName)
                {
                case "RequestType":
                    req.RequestType = reader.ReadElementContentAsString();
                    return;

                case "Entropy":
                    ReadEntropy();
                    return;

                case "KeySize":
                    req.KeySize = reader.ReadElementContentAsInt();
                    return;

                case "KeyType":
                    req.KeyType = reader.ReadElementContentAsString();
                    return;

                case "TokenType":
                    string tokenType = reader.ReadElementContentAsString();
                    if (tokenType != Constants.WsscContextToken)
                    {
                        throw new SecurityTokenException(String.Format("Unexpected TokenType: {0}", tokenType));
                    }
                    return;

                case "ComputedKeyAlgorithm":
                    req.ComputedKeyAlgorithm = reader.ReadElementContentAsString();
                    return;

                case "BinaryExchange":
                    ReadBinaryExchange();
                    return;
                }
                break;

            case Constants.WspNamespace:
                switch (reader.LocalName)
                {
                case "AppliesTo":
                    ReadAppliesTo();
                    return;
                }
                break;
            }
            throw new XmlException(String.Format("Unexpected RequestSecurityToken content element. Name is {0} and namespace URI is {1}{2}", reader.Name, reader.NamespaceURI, LineInfo()));
        }
Beispiel #6
0
            public object ReadValue(XmlDictionaryReader reader)
            {
                object value;

                if (_isArray)
                {
                    switch (_typeCode)
                    {
                    case TypeCode.Byte:
                        value = reader.ReadElementContentAsBase64();
                        break;

                    case TypeCode.Boolean:
                        if (!reader.IsEmptyElement)
                        {
                            reader.ReadStartElement();
                            value = reader.ReadBooleanArray(_itemName, _itemNamespace);
                            reader.ReadEndElement();
                        }
                        else
                        {
                            reader.Read();
                            value = Array.Empty <bool>();
                        }
                        break;

                    case TypeCode.DateTime:
                        if (!reader.IsEmptyElement)
                        {
                            reader.ReadStartElement();
                            value = reader.ReadDateTimeArray(_itemName, _itemNamespace);
                            reader.ReadEndElement();
                        }
                        else
                        {
                            reader.Read();
                            value = Array.Empty <DateTime>();
                        }
                        break;

                    case TypeCode.Decimal:
                        if (!reader.IsEmptyElement)
                        {
                            reader.ReadStartElement();
                            value = reader.ReadDecimalArray(_itemName, _itemNamespace);
                            reader.ReadEndElement();
                        }
                        else
                        {
                            reader.Read();
                            value = Array.Empty <Decimal>();
                        }
                        break;

                    case TypeCode.Int32:
                        if (!reader.IsEmptyElement)
                        {
                            reader.ReadStartElement();
                            value = reader.ReadInt32Array(_itemName, _itemNamespace);
                            reader.ReadEndElement();
                        }
                        else
                        {
                            reader.Read();
                            value = Array.Empty <Int32>();
                        }
                        break;

                    case TypeCode.Int64:
                        if (!reader.IsEmptyElement)
                        {
                            reader.ReadStartElement();
                            value = reader.ReadInt64Array(_itemName, _itemNamespace);
                            reader.ReadEndElement();
                        }
                        else
                        {
                            reader.Read();
                            value = Array.Empty <Int64>();
                        }
                        break;

                    case TypeCode.Single:
                        if (!reader.IsEmptyElement)
                        {
                            reader.ReadStartElement();
                            value = reader.ReadSingleArray(_itemName, _itemNamespace);
                            reader.ReadEndElement();
                        }
                        else
                        {
                            reader.Read();
                            value = Array.Empty <Single>();
                        }
                        break;

                    case TypeCode.Double:
                        if (!reader.IsEmptyElement)
                        {
                            reader.ReadStartElement();
                            value = reader.ReadDoubleArray(_itemName, _itemNamespace);
                            reader.ReadEndElement();
                        }
                        else
                        {
                            reader.Read();
                            value = Array.Empty <Double>();
                        }
                        break;

                    default:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxInvalidUseOfPrimitiveOperationFormatter));
                    }
                }
                else
                {
                    switch (_typeCode)
                    {
                    case TypeCode.Boolean:
                        value = reader.ReadElementContentAsBoolean();
                        break;

                    case TypeCode.DateTime:
                        value = reader.ReadElementContentAsDateTime();
                        break;

                    case TypeCode.Decimal:
                        value = reader.ReadElementContentAsDecimal();
                        break;

                    case TypeCode.Double:
                        value = reader.ReadElementContentAsDouble();
                        break;

                    case TypeCode.Int32:
                        value = reader.ReadElementContentAsInt();
                        break;

                    case TypeCode.Int64:
                        value = reader.ReadElementContentAsLong();
                        break;

                    case TypeCode.Single:
                        value = reader.ReadElementContentAsFloat();
                        break;

                    case TypeCode.String:
                        return(reader.ReadElementContentAsString());

                    default:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxInvalidUseOfPrimitiveOperationFormatter));
                    }
                }
                return(value);
            }
Beispiel #7
0
        private SecurityContextSecurityToken DeserializeContext(byte[] serializedContext, byte[] cookieBlob, string id, XmlDictionaryReaderQuotas quotas)
        {
            List <IAuthorizationPolicy> list3;
            SctClaimDictionary          instance = SctClaimDictionary.Instance;
            XmlDictionaryReader         reader   = XmlDictionaryReader.CreateBinaryReader(serializedContext, 0, serializedContext.Length, instance, quotas, null, null);
            int      num            = -1;
            UniqueId contextId      = null;
            DateTime minUtcDateTime = System.ServiceModel.Security.SecurityUtils.MinUtcDateTime;
            DateTime maxUtcDateTime = System.ServiceModel.Security.SecurityUtils.MaxUtcDateTime;

            byte[]            key               = null;
            string            str               = null;
            UniqueId          keyGeneration     = null;
            DateTime          keyEffectiveTime  = System.ServiceModel.Security.SecurityUtils.MinUtcDateTime;
            DateTime          keyExpirationTime = System.ServiceModel.Security.SecurityUtils.MaxUtcDateTime;
            List <ClaimSet>   claimSets         = null;
            IList <IIdentity> identities        = null;
            bool isCookieMode = true;

            reader.ReadFullStartElement(instance.SecurityContextSecurityToken, instance.EmptyString);
            while (reader.IsStartElement())
            {
                if (reader.IsStartElement(instance.Version, instance.EmptyString))
                {
                    num = reader.ReadElementContentAsInt();
                }
                else
                {
                    if (reader.IsStartElement(instance.ContextId, instance.EmptyString))
                    {
                        contextId = reader.ReadElementContentAsUniqueId();
                        continue;
                    }
                    if (reader.IsStartElement(instance.Id, instance.EmptyString))
                    {
                        str = reader.ReadElementContentAsString();
                        continue;
                    }
                    if (reader.IsStartElement(instance.EffectiveTime, instance.EmptyString))
                    {
                        minUtcDateTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc);
                        continue;
                    }
                    if (reader.IsStartElement(instance.ExpiryTime, instance.EmptyString))
                    {
                        maxUtcDateTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc);
                        continue;
                    }
                    if (reader.IsStartElement(instance.Key, instance.EmptyString))
                    {
                        key = reader.ReadElementContentAsBase64();
                        continue;
                    }
                    if (reader.IsStartElement(instance.KeyGeneration, instance.EmptyString))
                    {
                        keyGeneration = reader.ReadElementContentAsUniqueId();
                        continue;
                    }
                    if (reader.IsStartElement(instance.KeyEffectiveTime, instance.EmptyString))
                    {
                        keyEffectiveTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc);
                        continue;
                    }
                    if (reader.IsStartElement(instance.KeyExpiryTime, instance.EmptyString))
                    {
                        keyExpirationTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc);
                        continue;
                    }
                    if (reader.IsStartElement(instance.Identities, instance.EmptyString))
                    {
                        identities = SctClaimSerializer.DeserializeIdentities(reader, instance, DataContractSerializerDefaults.CreateSerializer(typeof(IIdentity), this.knownTypes, 0x7fffffff));
                        continue;
                    }
                    if (reader.IsStartElement(instance.ClaimSets, instance.EmptyString))
                    {
                        reader.ReadStartElement();
                        DataContractSerializer serializer      = DataContractSerializerDefaults.CreateSerializer(typeof(ClaimSet), this.knownTypes, 0x7fffffff);
                        DataContractSerializer claimSerializer = DataContractSerializerDefaults.CreateSerializer(typeof(Claim), this.knownTypes, 0x7fffffff);
                        claimSets = new List <ClaimSet>(1);
                        while (reader.IsStartElement())
                        {
                            claimSets.Add(SctClaimSerializer.DeserializeClaimSet(reader, instance, serializer, claimSerializer));
                        }
                        reader.ReadEndElement();
                        continue;
                    }
                    if (reader.IsStartElement(instance.IsCookieMode, instance.EmptyString))
                    {
                        isCookieMode = reader.ReadElementString() == "1";
                    }
                    else
                    {
                        OnInvalidCookieFailure(System.ServiceModel.SR.GetString("SctCookieXmlParseError"));
                    }
                }
            }
            reader.ReadEndElement();
            if (num != 1)
            {
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.ServiceModel.SR.GetString("SerializedTokenVersionUnsupported", new object[] { num })));
            }
            if (contextId == null)
            {
                OnInvalidCookieFailure(System.ServiceModel.SR.GetString("SctCookieValueMissingOrIncorrect", new object[] { "ContextId" }));
            }
            if ((key == null) || (key.Length == 0))
            {
                OnInvalidCookieFailure(System.ServiceModel.SR.GetString("SctCookieValueMissingOrIncorrect", new object[] { "Key" }));
            }
            if (str != id)
            {
                OnInvalidCookieFailure(System.ServiceModel.SR.GetString("SctCookieValueMissingOrIncorrect", new object[] { "Id" }));
            }
            if (claimSets != null)
            {
                list3 = new List <IAuthorizationPolicy>(1)
                {
                    new SctUnconditionalPolicy(identities, claimSets, maxUtcDateTime)
                };
            }
            else
            {
                list3 = null;
            }
            return(new SecurityContextSecurityToken(contextId, str, key, minUtcDateTime, maxUtcDateTime, (list3 != null) ? list3.AsReadOnly() : null, isCookieMode, cookieBlob, keyGeneration, keyEffectiveTime, keyExpirationTime));
        }
 private static void ReadFrom(CoordinationContext that, XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns, Microsoft.Transactions.Wsat.Protocol.ProtocolVersion protocolVersion)
 {
     try
     {
         Uri uri;
         CoordinationXmlDictionaryStrings strings  = CoordinationXmlDictionaryStrings.Version(protocolVersion);
         AtomicTransactionStrings         strings2 = AtomicTransactionStrings.Version(protocolVersion);
         reader.ReadFullStartElement(localName, strings.Namespace);
         reader.MoveToStartElement(strings.Identifier, strings.Namespace);
         that.unknownIdentifierAttributes = ReadOtherAttributes(reader, strings.Namespace);
         that.contextId = reader.ReadElementContentAsString().Trim();
         if ((that.contextId.Length == 0) || (that.contextId.Length > 0x100))
         {
             throw Microsoft.Transactions.Bridge.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidCoordinationContextException(Microsoft.Transactions.SR.GetString("InvalidCoordinationContext")));
         }
         if (!Uri.TryCreate(that.contextId, UriKind.Absolute, out uri))
         {
             throw Microsoft.Transactions.Bridge.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidCoordinationContextException(Microsoft.Transactions.SR.GetString("InvalidCoordinationContext")));
         }
         if (reader.IsStartElement(strings.Expires, strings.Namespace))
         {
             that.unknownExpiresAttributes = ReadOtherAttributes(reader, strings.Namespace);
             int num = reader.ReadElementContentAsInt();
             if (num < 0)
             {
                 throw Microsoft.Transactions.Bridge.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidCoordinationContextException(Microsoft.Transactions.SR.GetString("InvalidCoordinationContext")));
             }
             that.expiration     = (uint)num;
             that.expiresPresent = true;
         }
         reader.MoveToStartElement(strings.CoordinationType, strings.Namespace);
         if (reader.ReadElementContentAsString().Trim() != strings2.Namespace)
         {
             throw Microsoft.Transactions.Bridge.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidCoordinationContextException(Microsoft.Transactions.SR.GetString("InvalidCoordinationContext")));
         }
         that.registrationRef = EndpointAddress.ReadFrom(MessagingVersionHelper.AddressingVersion(protocolVersion), reader, strings.RegistrationService, strings.Namespace);
         if (reader.IsStartElement(XD.DotNetAtomicTransactionExternalDictionary.IsolationLevel, XD.DotNetAtomicTransactionExternalDictionary.Namespace))
         {
             that.isoLevel = (System.Transactions.IsolationLevel)reader.ReadElementContentAsInt();
             if (((that.IsolationLevel < System.Transactions.IsolationLevel.Serializable) || (that.IsolationLevel > System.Transactions.IsolationLevel.Unspecified)) || (that.IsolationLevel == System.Transactions.IsolationLevel.Snapshot))
             {
                 throw Microsoft.Transactions.Bridge.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidCoordinationContextException(Microsoft.Transactions.SR.GetString("InvalidCoordinationContext")));
             }
         }
         if (reader.IsStartElement(XD.DotNetAtomicTransactionExternalDictionary.IsolationFlags, XD.DotNetAtomicTransactionExternalDictionary.Namespace))
         {
             that.isoFlags = (System.ServiceModel.Transactions.IsolationFlags)reader.ReadElementContentAsInt();
         }
         if (reader.IsStartElement(XD.DotNetAtomicTransactionExternalDictionary.Description, XD.DotNetAtomicTransactionExternalDictionary.Namespace))
         {
             that.description = reader.ReadElementContentAsString().Trim();
         }
         if (reader.IsStartElement(XD.DotNetAtomicTransactionExternalDictionary.LocalTransactionId, XD.DotNetAtomicTransactionExternalDictionary.Namespace))
         {
             that.localTxId = reader.ReadElementContentAsGuid();
         }
         if (OleTxTransactionHeader.IsStartPropagationTokenElement(reader))
         {
             that.propToken = OleTxTransactionHeader.ReadPropagationTokenElement(reader);
         }
         if (reader.IsStartElement())
         {
             XmlDocument document = new XmlDocument();
             that.unknownData = new List <System.Xml.XmlNode>(5);
             while (reader.IsStartElement())
             {
                 System.Xml.XmlNode item = document.ReadNode(reader);
                 that.unknownData.Add(item);
             }
         }
         reader.ReadEndElement();
     }
     catch (XmlException exception)
     {
         throw Microsoft.Transactions.Bridge.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidCoordinationContextException(Microsoft.Transactions.SR.GetString("InvalidCoordinationContext"), exception));
     }
 }
        public static object Deserialize(XmlDictionaryReader reader, Type targetType)
        {
            object returnValue = null;

            if (reader.IsStartElement())
            {
                switch (reader.LocalName)
                {
                case XmlRpcProtocol.Nil:
                    returnValue = null;
                    break;

                case XmlRpcProtocol.Bool:
                    returnValue = Convert.ChangeType((reader.ReadElementContentAsInt() == 1), targetType);
                    break;

                case XmlRpcProtocol.ByteArray:
                    if (targetType == typeof(Stream))
                    {
                        returnValue = new MemoryStream(reader.ReadElementContentAsBase64());
                    }
                    else
                    {
                        returnValue = Convert.ChangeType(reader.ReadElementContentAsBase64(), targetType);
                    }
                    break;

                case XmlRpcProtocol.DateTime:
                    returnValue = Convert.ChangeType(reader.ReadElementContentAsDateTime(), targetType);
                    break;

                case XmlRpcProtocol.Double:
                    returnValue = Convert.ChangeType(reader.ReadElementContentAsDouble(), targetType);
                    break;

                case XmlRpcProtocol.Int32:
                case XmlRpcProtocol.Integer:
                    returnValue = Convert.ChangeType(reader.ReadElementContentAsString(), targetType);
                    break;

                case XmlRpcProtocol.String:
                    if (targetType == typeof(Uri))
                    {
                        returnValue = new Uri(reader.ReadElementContentAsString());
                    }
                    else
                    {
                        returnValue = Convert.ChangeType(reader.ReadElementContentAsString(), targetType);
                    }
                    break;

                case XmlRpcProtocol.Struct:
                    returnValue = DeserializeStruct(reader, targetType);
                    break;

                case XmlRpcProtocol.Array:
                    if (targetType.IsArray || targetType is IEnumerable || targetType is IList || targetType is ICollection)
                    {
                        reader.ReadStartElement(XmlRpcProtocol.Array);
                        ArrayList arrayData = new ArrayList();
                        reader.ReadStartElement(XmlRpcProtocol.Data);
                        reader.MoveToContent();
                        while (reader.IsStartElement(XmlRpcProtocol.Value))
                        {
                            reader.ReadStartElement();
                            arrayData.Add(Deserialize(reader, targetType.GetElementType()));
                            reader.ReadEndElement();
                            reader.MoveToContent();
                        }
                        if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == XmlRpcProtocol.Data)
                        {
                            reader.ReadEndElement();
                        }
                        reader.ReadEndElement();

                        if (targetType is IEnumerable || targetType is IList || targetType is ICollection)
                        {
                            returnValue = arrayData;
                        }
                        else
                        {
                            returnValue = arrayData.ToArray(targetType.GetElementType());
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                    break;
                }
            }
            return(returnValue);
        }
        public static SecurityContextSecurityToken ResolveCookie(byte [] bytes, byte [] cookie)
        {
            string   id           = null;
            UniqueId context      = null;
            DateTime validFrom    = DateTime.MinValue,
                     validTo      = DateTime.MaxValue,
                     keyEffective = DateTime.MinValue,
                     keyExpired   = DateTime.MaxValue;

            byte []               key    = null;
            X509Certificate2      cert   = null;
            X500DistinguishedName issuer = null;

            XmlDictionary dic = new XmlDictionary();

            for (int i = 0; i < 30; i++)
            {
                dic.Add("n" + i);
            }
            // FIXME: create proper quotas
            XmlDictionaryReaderQuotas quotas =
                new XmlDictionaryReaderQuotas();
            XmlDictionaryReader cr = XmlDictionaryReader.CreateBinaryReader(bytes, 0, bytes.Length, dic, quotas);

            cr.MoveToContent(); // -> n1
            cr.ReadStartElement("n0", String.Empty);
            do
            {
                cr.MoveToContent();
                if (cr.NodeType == XmlNodeType.EndElement)
                {
                    break;
                }
                if (cr.NodeType != XmlNodeType.Element)
                {
                    throw new Exception("Unxpected non-element content:" + cr.NodeType);
                }

                switch (cr.Name)
                {
                case "n1":
                    // FIXME: some integer here
                    int n1 = cr.ReadElementContentAsInt();
                    if (n1 != 1)
                    {
                        throw new Exception("INTERNAL ERROR: there was unexpected n2 content: " + n1);
                    }
                    break;

                case "n2":
                    context = cr.ReadElementContentAsUniqueId();
                    break;

                case "n3":
                    id = cr.ReadElementContentAsString();
                    break;

                case "n4":
                    key = cr.ReadElementContentAsBase64();
                    break;

                case "n7":
                    validFrom = new DateTime(cr.ReadElementContentAsLong());
                    break;

                case "n8":
                    validTo = new DateTime(cr.ReadElementContentAsLong());
                    break;

                case "n10":
                    keyEffective = new DateTime(cr.ReadElementContentAsLong());
                    break;

                case "n11":
                    keyExpired = new DateTime(cr.ReadElementContentAsLong());
                    break;

                case "n13":
                    // <n18>X509Certificate</n18>
                    cr.Read();
                    cr.MoveToContent();
                    cert = new X509Certificate2(cr.ReadElementContentAsBase64());
                    cr.ReadEndElement();
                    break;

                case "n15":
                    // <n16><n24 n25="IssuerName" /></n16>
                    cr.Read();
                    cr.ReadStartElement("n16", String.Empty);
                    issuer = new X500DistinguishedName(cr.GetAttribute("n25"));
                    bool empty = cr.IsEmptyElement;
                    cr.ReadStartElement("n24", String.Empty);
                    if (!empty)
                    {
                        cr.ReadEndElement(); // n24
                    }
                    cr.ReadEndElement();     // n16
                    cr.ReadEndElement();     // n15
                    break;

                default:
                    throw new Exception("INTERNAL ERROR: there was an unhandled element: " + cr.Name);
                }
            }while (true);

            SecurityContextSecurityToken sct = new SecurityContextSecurityToken(
                context, id, key, validFrom, validTo,
                null, keyEffective, keyExpired, null);

            sct.Cookie = cookie;
            return(sct);
        }
        void ReadTokenContent()
        {
            switch (reader.NamespaceURI)
            {
            case Constants.WstNamespace:
                switch (reader.LocalName)
                {
                case "RequestedSecurityToken":
                    res.RequestedSecurityToken = (SecurityContextSecurityToken)ReadToken();
                    return;

                case "RequestedProofToken":
#if true // FIXME: we can't handle it right now
                    string ens = EncryptedXml.XmlEncNamespaceUrl;
                    reader.Read();
                    reader.ReadStartElement("EncryptedKey", ens);
                    string alg     = reader.GetAttribute("Algorithm");
                    bool   isEmpty = reader.IsEmptyElement;
                    reader.ReadStartElement("EncryptionMethod", ens);
                    if (alg != negotiation_type)
                    {
                        throw new XmlException(String.Format("EncryptionMethod '{0}' is not supported in RequestedProofToken.", alg));
                    }
                    if (!isEmpty)
                    {
                        reader.ReadEndElement();
                    }
                    reader.ReadStartElement("CipherData", ens);
                    reader.ReadStartElement("CipherValue", ens);
                    byte [] pt = reader.ReadContentAsBase64();
                    res.RequestedProofToken = pt;
                    reader.ReadEndElement();
                    reader.ReadEndElement();
                    reader.ReadEndElement(); // EncryptedKey
                    reader.ReadEndElement(); // RPT
#else
                    reader.Read();
                    reader.MoveToContent();
                    if (serializer.CanReadToken(reader))
                    {
                        res.RequestedProofToken = serializer.ReadToken(reader, resolver);
                    }
                    else
                    {
                        res.RequestedProofToken = serializer.ReadKeyIdentifierClause(reader);
                    }
                    reader.ReadEndElement();
#endif
                    return;

                case "BinaryExchange":
                    ReadBinaryExchange();
                    return;

                case "TokenType":
                    res.TokenType = reader.ReadElementContentAsString();
                    return;

                case "Lifetime":
                    ReadLifetime();
                    return;

                case "KeySize":
                    res.KeySize = reader.ReadElementContentAsInt();
                    return;

                case "RequestedAttachedReference":
                    res.RequestedAttachedReference = ReadTokenReference();
                    return;

                case "RequestedUnattachedReference":
                    res.RequestedUnattachedReference = ReadTokenReference();
                    return;

                case "Authenticator":
                    ReadAuthenticator();
                    return;
                }
                break;
            }
            throw new XmlException(String.Format("Unexpected RequestSecurityTokenResponse content element. Name is {0} and namespace URI is {1} {2}", reader.Name, reader.NamespaceURI, LineInfo()));
        }
        SecurityContextSecurityToken DeserializeContext(byte[] serializedContext, byte[] cookieBlob, string id, XmlDictionaryReaderQuotas quotas)
        {
            SctClaimDictionary  dictionary = SctClaimDictionary.Instance;
            XmlDictionaryReader reader     = XmlDictionaryReader.CreateBinaryReader(serializedContext, 0, serializedContext.Length, dictionary, quotas, null, null);
            int      cookieVersion         = -1;
            UniqueId cookieContextId       = null;
            DateTime effectiveTime         = SecurityUtils.MinUtcDateTime;
            DateTime expiryTime            = SecurityUtils.MaxUtcDateTime;

            byte[]            key               = null;
            string            localId           = null;
            UniqueId          keyGeneration     = null;
            DateTime          keyEffectiveTime  = SecurityUtils.MinUtcDateTime;
            DateTime          keyExpirationTime = SecurityUtils.MaxUtcDateTime;
            List <ClaimSet>   claimSets         = null;
            IList <IIdentity> identities        = null;
            bool isCookie = true;

            reader.ReadFullStartElement(dictionary.SecurityContextSecurityToken, dictionary.EmptyString);

            while (reader.IsStartElement())
            {
                if (reader.IsStartElement(dictionary.Version, dictionary.EmptyString))
                {
                    cookieVersion = reader.ReadElementContentAsInt();
                }
                else if (reader.IsStartElement(dictionary.ContextId, dictionary.EmptyString))
                {
                    cookieContextId = reader.ReadElementContentAsUniqueId();
                }
                else if (reader.IsStartElement(dictionary.Id, dictionary.EmptyString))
                {
                    localId = reader.ReadElementContentAsString();
                }
                else if (reader.IsStartElement(dictionary.EffectiveTime, dictionary.EmptyString))
                {
                    effectiveTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc);
                }
                else if (reader.IsStartElement(dictionary.ExpiryTime, dictionary.EmptyString))
                {
                    expiryTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc);
                }
                else if (reader.IsStartElement(dictionary.Key, dictionary.EmptyString))
                {
                    key = reader.ReadElementContentAsBase64();
                }
                else if (reader.IsStartElement(dictionary.KeyGeneration, dictionary.EmptyString))
                {
                    keyGeneration = reader.ReadElementContentAsUniqueId();
                }
                else if (reader.IsStartElement(dictionary.KeyEffectiveTime, dictionary.EmptyString))
                {
                    keyEffectiveTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc);
                }
                else if (reader.IsStartElement(dictionary.KeyExpiryTime, dictionary.EmptyString))
                {
                    keyExpirationTime = new DateTime(XmlHelper.ReadElementContentAsInt64(reader), DateTimeKind.Utc);
                }
                else if (reader.IsStartElement(dictionary.Identities, dictionary.EmptyString))
                {
                    identities = SctClaimSerializer.DeserializeIdentities(reader, dictionary, DataContractSerializerDefaults.CreateSerializer(typeof(IIdentity), this.knownTypes, int.MaxValue));
                }
                else if (reader.IsStartElement(dictionary.ClaimSets, dictionary.EmptyString))
                {
                    reader.ReadStartElement();

                    DataContractSerializer claimSetSerializer = DataContractSerializerDefaults.CreateSerializer(typeof(ClaimSet), this.knownTypes, int.MaxValue);
                    DataContractSerializer claimSerializer    = DataContractSerializerDefaults.CreateSerializer(typeof(Claim), this.knownTypes, int.MaxValue);
                    claimSets = new List <ClaimSet>(1);
                    while (reader.IsStartElement())
                    {
                        claimSets.Add(SctClaimSerializer.DeserializeClaimSet(reader, dictionary, claimSetSerializer, claimSerializer));
                    }

                    reader.ReadEndElement();
                }
                else if (reader.IsStartElement(dictionary.IsCookieMode, dictionary.EmptyString))
                {
                    isCookie = reader.ReadElementString() == "1" ? true : false;
                }
                else
                {
                    OnInvalidCookieFailure(SR.GetString(SR.SctCookieXmlParseError));
                }
            }
            reader.ReadEndElement();
            if (cookieVersion != SupportedPersistanceVersion)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SerializedTokenVersionUnsupported, cookieVersion)));
            }
            if (cookieContextId == null)
            {
                OnInvalidCookieFailure(SR.GetString(SR.SctCookieValueMissingOrIncorrect, "ContextId"));
            }
            if (key == null || key.Length == 0)
            {
                OnInvalidCookieFailure(SR.GetString(SR.SctCookieValueMissingOrIncorrect, "Key"));
            }
            if (localId != id)
            {
                OnInvalidCookieFailure(SR.GetString(SR.SctCookieValueMissingOrIncorrect, "Id"));
            }
            List <IAuthorizationPolicy> authorizationPolicies;

            if (claimSets != null)
            {
                authorizationPolicies = new List <IAuthorizationPolicy>(1);
                authorizationPolicies.Add(new SctUnconditionalPolicy(identities, claimSets, expiryTime));
            }
            else
            {
                authorizationPolicies = null;
            }
            return(new SecurityContextSecurityToken(cookieContextId, localId, key, effectiveTime, expiryTime,
                                                    authorizationPolicies != null ? authorizationPolicies.AsReadOnly() : null, isCookie, cookieBlob, keyGeneration, keyEffectiveTime, keyExpirationTime));
        }
Beispiel #13
0
        public void Load(XmlReader xmlReader)
        {
            XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(xmlReader);

            string ns = Constants.WsidNamespace;

            reader.MoveToContent();
            reader.ReadStartElement("RoamingStore", ns);
            reader.MoveToContent();
            reader.ReadStartElement("RoamingInformationCard", ns);
            reader.MoveToContent();
            lang = reader.GetAttribute("xml:lang");
            // metadata
            reader.ReadStartElement("InformationCardMetaData", ns);
            reader.MoveToContent();
            reader.ReadStartElement("InformationCardReference", ns);
            reader.MoveToContent();
            id = reader.ReadElementContentAsString("CardId", ns);
            reader.MoveToContent();
            version = reader.ReadElementContentAsString("CardVersion", ns);
            reader.MoveToContent();
            reader.ReadEndElement();
            reader.MoveToContent();
            name = reader.ReadElementContentAsString("CardName", ns);
            reader.MoveToContent();
            image_mime = reader.GetAttribute("MimeType");
            image      = Convert.FromBase64String(
                reader.ReadElementContentAsString("CardImage", ns));
            reader.MoveToContent();
            issuer = new Uri(
                reader.ReadElementContentAsString("Issuer", ns));
            reader.MoveToContent();
            issued = XmlConvert.ToDateTime(
                reader.ReadElementContentAsString("TimeIssued", ns), XmlDateTimeSerializationMode.Utc);
            reader.MoveToContent();
            expires = XmlConvert.ToDateTime(
                reader.ReadElementContentAsString("TimeExpires", ns), XmlDateTimeSerializationMode.Utc);
            reader.MoveToContent();
            if (reader.IsStartElement("TokenServiceList", ns))
            {
                reader.ReadStartElement("TokenServiceList", ns);
                reader.MoveToContent();
                for (reader.MoveToContent();
                     reader.NodeType == XmlNodeType.Element;
                     reader.MoveToContent())
                {
                    reader.ReadStartElement("TokenService", ns);
                    reader.MoveToContent();
                    TokenService ts = new TokenService();
                    ts.ReadXml(reader);
                    token_services.Add(ts);
                    reader.MoveToContent();
                    reader.ReadEndElement();
                }
                reader.ReadEndElement();
            }

            reader.MoveToContent();
            reader.ReadStartElement("SupportedTokenTypeList", ns);
            for (reader.MoveToContent();
                 reader.NodeType == XmlNodeType.Element;
                 reader.MoveToContent())
            {
                supported_token_types.Add(new Uri(
                                              reader.ReadElementContentAsString("TokenType", Constants.WstNamespace)));
            }
            reader.ReadEndElement();

            reader.MoveToContent();
            reader.ReadStartElement("SupportedClaimTypeList", ns);
            for (reader.MoveToContent();
                 reader.NodeType == XmlNodeType.Element;
                 reader.MoveToContent())
            {
                string uri = reader.GetAttribute("Uri");
                reader.ReadStartElement("SupportedClaimType", ns);
                string tag = reader.ReadElementContentAsString("DisplayTag", ns);
                reader.MoveToContent();
                string desc = reader.ReadElementContentAsString("Description", ns);
                reader.MoveToContent();
                reader.ReadEndElement();
                supported_claim_types.Add(new ClaimTypeDefinition(uri, tag, desc));
            }
            reader.ReadEndElement();

            reader.MoveToContent();
            self_issued = reader.ReadElementContentAsBoolean("IsSelfIssued", ns);
            reader.MoveToContent();
            hash_salt = Convert.FromBase64String(
                reader.ReadElementContentAsString("HashSalt", ns));
            reader.MoveToContent();
            last_updated = XmlConvert.ToDateTime(
                reader.ReadElementContentAsString("TimeLastUpdated", ns), XmlDateTimeSerializationMode.Utc);
            reader.MoveToContent();
            issuer_id = reader.ReadElementContentAsString("IssuerId", ns);
            reader.MoveToContent();
            issuer_name = reader.ReadElementContentAsString("IssuerName", ns);
            reader.MoveToContent();
            back_color = reader.ReadElementContentAsInt("BackgroundColor", ns);

            reader.MoveToContent();
            reader.ReadEndElement();              // InformationCardMetaData

            // private data
            reader.MoveToContent();
            reader.ReadStartElement("InformationCardPrivateData", ns);
            reader.MoveToContent();
            master_key = Convert.FromBase64String(
                reader.ReadElementContentAsString("MasterKey", ns));
            reader.MoveToContent();
            if (reader.IsStartElement("ClaimValueList", ns))
            {
                reader.ReadStartElement("ClaimValueList", ns);

                reader.MoveToContent();
                for (reader.MoveToContent();
                     reader.NodeType == XmlNodeType.Element;
                     reader.MoveToContent())
                {
                    string uri = reader.GetAttribute("Uri");
                    reader.ReadStartElement("ClaimValue", ns);
                    reader.MoveToContent();
                    string value = reader.ReadElementContentAsString("Value", ns);
                    reader.MoveToContent();
                    reader.ReadEndElement();
                    claim_values.Add(new ClaimValue(uri, value));
                }
                reader.ReadEndElement();
                reader.MoveToContent();
            }

            reader.ReadEndElement();              // InformationCardPrivateData

            reader.MoveToContent();
            reader.ReadEndElement();
            reader.MoveToContent();
            reader.ReadEndElement();
        }
Beispiel #14
0
            public object ReadValue(XmlDictionaryReader reader)
            {
                object obj2;

                if (!this.isArray)
                {
                    switch (this.typeCode)
                    {
                    case TypeCode.Int32:
                        return(reader.ReadElementContentAsInt());

                    case TypeCode.Int64:
                        return(reader.ReadElementContentAsLong());

                    case TypeCode.Single:
                        return(reader.ReadElementContentAsFloat());

                    case TypeCode.Double:
                        return(reader.ReadElementContentAsDouble());

                    case TypeCode.Decimal:
                        return(reader.ReadElementContentAsDecimal());

                    case TypeCode.DateTime:
                        return(reader.ReadElementContentAsDateTime());

                    case TypeCode.String:
                        return(reader.ReadElementContentAsString());

                    case TypeCode.Boolean:
                        return(reader.ReadElementContentAsBoolean());
                    }
                    throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxInvalidUseOfPrimitiveOperationFormatter")));
                }
                switch (this.typeCode)
                {
                case TypeCode.Boolean:
                    if (reader.IsEmptyElement)
                    {
                        reader.Read();
                        return(new bool[0]);
                    }
                    reader.ReadStartElement();
                    obj2 = reader.ReadBooleanArray(this.itemName, this.itemNamespace);
                    reader.ReadEndElement();
                    return(obj2);

                case TypeCode.Byte:
                    return(reader.ReadElementContentAsBase64());

                case TypeCode.Int32:
                    if (reader.IsEmptyElement)
                    {
                        reader.Read();
                        return(new int[0]);
                    }
                    reader.ReadStartElement();
                    obj2 = reader.ReadInt32Array(this.itemName, this.itemNamespace);
                    reader.ReadEndElement();
                    return(obj2);

                case TypeCode.Int64:
                    if (reader.IsEmptyElement)
                    {
                        reader.Read();
                        return(new long[0]);
                    }
                    reader.ReadStartElement();
                    obj2 = reader.ReadInt64Array(this.itemName, this.itemNamespace);
                    reader.ReadEndElement();
                    return(obj2);

                case TypeCode.Single:
                    if (reader.IsEmptyElement)
                    {
                        reader.Read();
                        return(new float[0]);
                    }
                    reader.ReadStartElement();
                    obj2 = reader.ReadSingleArray(this.itemName, this.itemNamespace);
                    reader.ReadEndElement();
                    return(obj2);

                case TypeCode.Double:
                    if (reader.IsEmptyElement)
                    {
                        reader.Read();
                        return(new double[0]);
                    }
                    reader.ReadStartElement();
                    obj2 = reader.ReadDoubleArray(this.itemName, this.itemNamespace);
                    reader.ReadEndElement();
                    return(obj2);

                case TypeCode.Decimal:
                    if (reader.IsEmptyElement)
                    {
                        reader.Read();
                        return(new decimal[0]);
                    }
                    reader.ReadStartElement();
                    obj2 = reader.ReadDecimalArray(this.itemName, this.itemNamespace);
                    reader.ReadEndElement();
                    return(obj2);

                case TypeCode.DateTime:
                    if (reader.IsEmptyElement)
                    {
                        reader.Read();
                        return(new DateTime[0]);
                    }
                    reader.ReadStartElement();
                    obj2 = reader.ReadDateTimeArray(this.itemName, this.itemNamespace);
                    reader.ReadEndElement();
                    return(obj2);
                }
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxInvalidUseOfPrimitiveOperationFormatter")));
            }