Example #1
0
        public void WriteBase64()
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[] fooBar = encoding.GetBytes("foobar");
            xw.WriteBase64(fooBar, 0, 6);
            Assert.AreEqual("Zm9vYmFy", Output);

            try {
                xw.WriteBase64(fooBar, 3, 6);
                Assert.Fail("Expected an Argument Exception to be thrown.");
            } catch (ArgumentException) {}

            try {
                xw.WriteBase64(fooBar, -1, 6);
                Assert.Fail("Expected an Argument Exception to be thrown.");
            } catch (ArgumentOutOfRangeException) {}

            try {
                xw.WriteBase64(fooBar, 3, -1);
                Assert.Fail("Expected an Argument Exception to be thrown.");
            } catch (ArgumentOutOfRangeException) {}

            try {
                xw.WriteBase64(null, 0, 6);
                Assert.Fail("Expected an Argument Exception to be thrown.");
            } catch (ArgumentNullException) {}
        }
Example #2
0
    private static void SimulateWriteFragment(XmlDictionaryWriter writer, bool useFragmentAPI, int nestedLevelsLeft)
    {
        if (nestedLevelsLeft <= 0)
        {
            return;
        }

        Random rndGen       = new Random(nestedLevelsLeft);
        int    signatureLen = rndGen.Next(100, 200);

        byte[] signature = new byte[signatureLen];
        rndGen.NextBytes(signature);

        MemoryStream fragmentStream = new MemoryStream();

        if (!useFragmentAPI) // simulating in the writer itself
        {
            writer.WriteStartElement("SignatureValue_" + nestedLevelsLeft);
            writer.WriteBase64(signature, 0, signatureLen);
            writer.WriteEndElement();
        }

        if (useFragmentAPI)
        {
            FragmentHelper.Start(writer, fragmentStream);
        }

        writer.WriteStartElement("Fragment" + nestedLevelsLeft);
        for (int i = 0; i < 5; i++)
        {
            writer.WriteStartElement(string.Format("Element{0}_{1}", nestedLevelsLeft, i));
            writer.WriteAttributeString("attr1", "value1");
            writer.WriteAttributeString("attr2", "value2");
        }
        writer.WriteString("This is a text with unicode characters: <>&;\u0301\u2234");
        for (int i = 0; i < 5; i++)
        {
            writer.WriteEndElement();
        }

        // write other nested fragments...
        SimulateWriteFragment(writer, useFragmentAPI, nestedLevelsLeft - 1);

        writer.WriteEndElement(); // Fragment{nestedLevelsLeft}
        writer.Flush();

        if (useFragmentAPI)
        {
            FragmentHelper.End(writer);
            writer.WriteStartElement("SignatureValue_" + nestedLevelsLeft);
            writer.WriteBase64(signature, 0, signatureLen);
            writer.WriteEndElement();

            FragmentHelper.Write(writer, fragmentStream.GetBuffer(), 0, (int)fragmentStream.Length);

            writer.Flush();
        }
    }
Example #3
0
 /// <summary>
 /// Write body contents
 /// </summary>
 protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 {
     writer.WriteStartElement("Binary");
     if (this.m_stream == null)
     {
         writer.WriteBase64(this.m_bytes, 0, this.m_bytes.Length);
     }
     else
     {
         byte[] buffer = new byte[this.m_stream.Length];
         this.m_stream.Read(buffer, 0, (int)this.m_stream.Length);
         writer.WriteBase64(buffer, 0, (int)this.m_stream.Length);
     }
     writer.WriteEndElement();
 }
Example #4
0
        protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
        {
            // Create the Nonce
            byte[] nonce = GenerateNonce();

            // Create the Created Date
            string created = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");

            // Create the WSSE Security Header, starting with the Username Element
            writer.WriteStartElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
            writer.WriteXmlnsAttribute("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
            writer.WriteStartElement("wsse", "Username", null);
            writer.WriteString(Username);
            writer.WriteEndElement();

            // Add the Password Element
            writer.WriteStartElement("wsse", "Password", null);
            writer.WriteAttributeString("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
            writer.WriteString(GeneratePasswordDigest(nonce, created, Password));
            writer.WriteEndElement();

            // Add the Nonce Element
            writer.WriteStartElement("wsse", "Nonce", null);
            writer.WriteAttributeString("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
            writer.WriteBase64(nonce, 0, nonce.Length);
            writer.WriteEndElement();

            // Lastly, add the Created Element
            writer.WriteStartElement("wsu", "Created", null);
            writer.WriteString(created);
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();
        }
Example #5
0
        private string ReadRawBody(ref Message message)
        {
            XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();

            bodyReader.ReadStartElement("Binary");
            byte[] bodyBytes = bodyReader.ReadContentAsBase64();
            //MemoryStream ms1 = new MemoryStream(bodyBytes);
            //StreamReader sr = new StreamReader(ms1);
            //JsonSerializer serializer = JsonHelper.GetJsonSerializer();
            //var obj = serializer.Deserialize(sr, typeof(ReturnMessage<object>));
            string messageBody = Encoding.UTF8.GetString(bodyBytes);
            // Now to recreate the message
            MemoryStream        ms     = new MemoryStream();
            XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms);

            writer.WriteStartElement("Binary");
            writer.WriteBase64(bodyBytes, 0, bodyBytes.Length);
            writer.WriteEndElement();
            writer.Flush();
            ms.Position = 0;
            XmlDictionaryReader reader     = XmlDictionaryReader.CreateBinaryReader(ms, XmlDictionaryReaderQuotas.Max);
            Message             newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);

            newMessage.Properties.CopyProperties(message.Properties);
            newMessage.Headers.CopyHeadersFrom(message);
            message = newMessage;
            return(messageBody);
        }
Example #6
0
        private void WriteHeader(XmlDictionaryWriter writer)
        {
            var nonce = new byte[64];

            RandomNumberGenerator.Create().GetBytes(nonce);
            string created = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.mszzzz");

            writer.WriteStartElement("wsse", "UsernameToken", null);
            writer.WriteAttributeString("wsu:Id", "UsernameToken-1");
            writer.WriteStartElement("wsse", "Username", null);
            writer.WriteString(SystemUser);
            writer.WriteEndElement();//End Username
            writer.WriteStartElement("wsse", "Password", null);
            writer.WriteAttributeString("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
            writer.WriteString(ComputePasswordDigest(SystemPassword, nonce, created));
            writer.WriteEndElement();//End Password
            writer.WriteStartElement("wsse", "Nonce", null);
            writer.WriteAttributeString("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
            writer.WriteBase64(nonce, 0, nonce.Length);
            writer.WriteEndElement();//End Nonce
            writer.WriteStartElement("wsu", "Created", null);
            writer.WriteString(created);
            writer.WriteEndElement(); //End Created
            writer.WriteEndElement(); //End UsernameToken
            writer.Flush();
        }
Example #7
0
 protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 {
     writer.WriteStartElement("Binary");
     byte[] bytes = Encoding.UTF8.GetBytes(this.contents);
     writer.WriteBase64(bytes, 0, bytes.Length);
     writer.WriteEndElement();
 }
Example #8
0
        /// <summary>
        /// This method is used to write a XML node to a XML writer.
        /// </summary>
        /// <param name="node">Specify the XML node.</param>
        /// <param name="writer">Specify the XML writer.</param>
        private void WriteNode(XmlNode node, XmlDictionaryWriter writer)
        {
            writer.WriteStartElement(node.Name);
            foreach (XmlAttribute xmlAttribute in node.Attributes)
            {
                writer.WriteAttributeString(xmlAttribute.Name, xmlAttribute.Value);
                if (!string.IsNullOrEmpty(xmlAttribute.Prefix))
                {
                    writer.WriteXmlnsAttribute(xmlAttribute.Prefix, xmlAttribute.NamespaceURI);
                }
            }

            if (node.Name == "SubRequestData")
            {
                string base64 = node.InnerText;
                byte[] bytes  = Convert.FromBase64String(base64);
                writer.WriteBase64(bytes, 0, bytes.Length);
            }
            else
            {
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    this.WriteNode(childNode, writer);
                }
            }

            writer.WriteEndElement();
        }
Example #9
0
        public static string ReadRawBody(ref Message message)
        {
            XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();

            bodyReader.ReadStartElement("Binary");
            byte[] bodyBytes   = bodyReader.ReadContentAsBase64();
            string messageBody = Encoding.UTF8.GetString(bodyBytes);

            // Now to recreate the message
            MemoryStream        ms     = new MemoryStream();
            XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms);

            writer.WriteStartElement("Binary");
            writer.WriteBase64(bodyBytes, 0, bodyBytes.Length);
            writer.WriteEndElement();
            writer.Flush();
            ms.Position = 0;
            XmlDictionaryReader reader     = XmlDictionaryReader.CreateBinaryReader(ms, XmlDictionaryReaderQuotas.Max);
            Message             newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);

            newMessage.Properties.CopyProperties(message.Properties);
            message = newMessage;

            return(messageBody);
        }
 /// <summary>
 /// Writes the body of an object in the output
 /// </summary>
 public override void WriteObjectContent(XmlDictionaryWriter writer, object graph)
 {
     if (writer == null)
     {
         throw new ArgumentNullException(nameof(writer));
     }
     if (graph == null)
     {
         writer.WriteAttributeString("nil", "true");
     }
     else
     {
         using MemoryStream ms = new MemoryStream();
         var state = ProtoWriter.State.Create(ms, model, null);
         try
         {
             if (!DynamicStub.TrySerializeRoot(type, model, ref state, graph))
             {
                 TypeModel.ThrowUnexpectedType(type, model);
             }
         }
         catch
         {
             state.Abandon();
             throw;
         }
         finally
         {
             state.Dispose();
         }
         Helpers.GetBuffer(ms, out var segment);
         writer.WriteBase64(segment.Array, segment.Offset, segment.Count);
     }
 }
Example #11
0
 protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 {
     byte[] binaryContent = Encoding.UTF8.GetBytes("Hello world!");
     writer.WriteStartElement("Binary");
     writer.WriteBase64(binaryContent, 0, binaryContent.Length);
     writer.WriteEndElement();
 }
Example #12
0
            public override void WriteTokenCore(XmlDictionaryWriter writer, SecurityToken token)
            {
                SecurityContextSecurityToken sct = token as SecurityContextSecurityToken;

                writer.WriteStartElement(this.parent.SerializerDictionary.Prefix.Value, this.parent.SerializerDictionary.SecurityContextToken, this.parent.SerializerDictionary.Namespace);
                if (sct.Id != null)
                {
                    writer.WriteAttributeString(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace, sct.Id);
                }
                writer.WriteStartElement(this.parent.SerializerDictionary.Prefix.Value, this.parent.SerializerDictionary.Identifier, this.parent.SerializerDictionary.Namespace);
                XmlHelper.WriteStringAsUniqueId(writer, sct.ContextId);
                writer.WriteEndElement();
                this.WriteGeneration(writer, sct);
                if (sct.IsCookieMode)
                {
                    if (sct.CookieBlob == null)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError((Exception) new MessageSecurityException(SR.GetString("NoCookieInSct")));
                    }
#if FEATURE_CORECLR
                    throw new NotImplementedException("XD.DotNetSecurityDictionary is not supported in .NET Core");
#else
                    writer.WriteStartElement(XD.DotNetSecurityDictionary.Prefix.Value, this.parent.SerializerDictionary.Cookie, XD.DotNetSecurityDictionary.Namespace);
                    writer.WriteBase64(sct.CookieBlob, 0, sct.CookieBlob.Length);
                    writer.WriteEndElement();
#endif
                }
                writer.WriteEndElement();
            }
Example #13
0
            protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
            {
                byte[] nonce = new byte[64];
                RandomNumberGenerator.Create().GetBytes(nonce);
                string created = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ");

                writer.WriteStartElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                writer.WriteXmlnsAttribute("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
                writer.WriteAttributeString("wsu", "Id", null, "User");
                writer.WriteStartElement("wsse", "Username", null);
                writer.WriteString(username);
                writer.WriteEndElement();
                writer.WriteStartElement("wsse", "Password", null);
                writer.WriteAttributeString("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
                writer.WriteString(ComputeDigest(password, nonce, created));
                writer.WriteEndElement();
                writer.WriteStartElement("wsse", "Nonce", null);
                writer.WriteAttributeString("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
                writer.WriteBase64(nonce, 0, nonce.Length);
                writer.WriteEndElement();
                writer.WriteStartElement("wsu", "Created", null);
                writer.WriteString(created);
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Flush();
            }
 public static void WritePropagationTokenElement(XmlDictionaryWriter writer, byte[] propagationToken)
 {
     writer.WriteStartElement(XD.OleTxTransactionExternalDictionary.PropagationToken,
                              XD.OleTxTransactionExternalDictionary.Namespace);
     writer.WriteBase64(propagationToken, 0, propagationToken.Length);
     writer.WriteEndElement();
 }
            public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause keyIdentifierClause)
            {
                EncryptedKeyIdentifierClause clause = keyIdentifierClause as EncryptedKeyIdentifierClause;

                writer.WriteStartElement(XD.XmlEncryptionDictionary.Prefix.Value, XD.XmlEncryptionDictionary.EncryptedKey, this.NamespaceUri);
                if (clause.EncryptionMethod != null)
                {
                    writer.WriteStartElement(XD.XmlEncryptionDictionary.Prefix.Value, XD.XmlEncryptionDictionary.EncryptionMethod, this.NamespaceUri);
                    writer.WriteAttributeString(XD.XmlEncryptionDictionary.AlgorithmAttribute, null, clause.EncryptionMethod);
                    if (clause.EncryptionMethod == XD.SecurityAlgorithmDictionary.RsaOaepKeyWrap.Value)
                    {
                        writer.WriteStartElement("", XD.XmlSignatureDictionary.DigestMethod, XD.XmlSignatureDictionary.Namespace);
                        writer.WriteAttributeString(XD.XmlSignatureDictionary.Algorithm, null, "http://www.w3.org/2000/09/xmldsig#sha1");
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }
                if (clause.EncryptingKeyIdentifier != null)
                {
                    this.tokenSerializer.WriteKeyIdentifier(writer, clause.EncryptingKeyIdentifier);
                }
                writer.WriteStartElement(XD.XmlEncryptionDictionary.Prefix.Value, XD.XmlEncryptionDictionary.CipherData, this.NamespaceUri);
                writer.WriteStartElement(XD.XmlEncryptionDictionary.Prefix.Value, XD.XmlEncryptionDictionary.CipherValue, this.NamespaceUri);
                byte[] encryptedKey = clause.GetEncryptedKey();
                writer.WriteBase64(encryptedKey, 0, encryptedKey.Length);
                writer.WriteEndElement();
                writer.WriteEndElement();
                if (clause.CarriedKeyName != null)
                {
                    writer.WriteElementString(XD.XmlEncryptionDictionary.Prefix.Value, XD.XmlEncryptionDictionary.CarriedKeyName, this.NamespaceUri, clause.CarriedKeyName);
                }
                writer.WriteEndElement();
            }
Example #16
0
        private static void SerializeX509SecurityToken(XmlDictionaryWriter dictionaryWriter, X509SecurityToken token)
        {
            string id = token.Id;

            byte[] rawData = token.Certificate.GetRawCertData();

            if (rawData == null)
            {
                throw new ArgumentNullException("rawData");
            }

            dictionaryWriter.WriteStartElement(XD_SecurityJan2004Dictionary_Prefix_Value,
                                               XD_SecurityJan2004Dictionary_BinarySecurityToken,
                                               XD_SecurityJan2004Dictionary_Namespace);
            if (id != null)
            {
                dictionaryWriter.WriteAttributeString(XD_UtilityDictionary_Prefix_Value, XD_UtilityDictionary_IdAttribute,
                                                      XD_UtilityDictionary_Namespace, id);
            }
            dictionaryWriter.WriteAttributeString(XD_SecurityJan2004Dictionary_ValueType, null,
                                                  SecurityJan2004Strings_X509TokenType);
            // tokenSerializer EmitBspRequiredAttributes is set to true
            dictionaryWriter.WriteAttributeString(XD_SecurityJan2004Dictionary_EncodingType, null,
                                                  SecurityJan2004Strings_EncodingTypeValueBase64Binary);
            //EmitBspRequiredAttributes
            dictionaryWriter.WriteBase64(rawData, 0, rawData.Length);
            dictionaryWriter.WriteEndElement(); // BinarySecurityToken
        }
Example #17
0
 protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 {
     writer.WriteStartElement("Binary");
     byte[] buffer = JSONPSupportInspector.encoding.GetBytes(this.content);
     writer.WriteBase64(buffer, 0, buffer.Length);
     writer.WriteEndElement();
 }
Example #18
0
        private static void WriteStream(XmlDictionaryWriter writer, object value)
        {
            int blockSize = 256;
            int bytesRead = 0;

            byte[] block  = new byte[blockSize];
            var    stream = (Stream)value;

            stream.Position = 0;

            while (true)
            {
                bytesRead = stream.Read(block, 0, blockSize);
                if (bytesRead > 0)
                {
                    writer.WriteBase64(block, 0, bytesRead);
                }
                else
                {
                    break;
                }

                if (blockSize < 65536 && bytesRead == blockSize)
                {
                    blockSize = blockSize * 16;
                    block     = new byte[blockSize];
                }
            }
        }
Example #19
0
            public override void WriteTokenCore(XmlDictionaryWriter writer, SecurityToken token)
            {
                SecurityContextSecurityToken sct = (token as SecurityContextSecurityToken);

                // serialize the name and any wsu:Id attribute
                writer.WriteStartElement(parent.SerializerDictionary.Prefix.Value, parent.SerializerDictionary.SecurityContextToken, parent.SerializerDictionary.Namespace);
                if (sct.Id != null)
                {
                    writer.WriteAttributeString(CoreWCF.XD.UtilityDictionary.Prefix.Value, CoreWCF.XD.UtilityDictionary.IdAttribute, CoreWCF.XD.UtilityDictionary.Namespace, sct.Id);
                }

                // serialize the context id
                writer.WriteStartElement(parent.SerializerDictionary.Prefix.Value, parent.SerializerDictionary.Identifier, parent.SerializerDictionary.Namespace);
                XmlHelper.WriteStringAsUniqueId(writer, sct.ContextId);
                writer.WriteEndElement();

                WriteGeneration(writer, sct);

                // if cookie-mode, then it must have a cookie
                if (sct.IsCookieMode)
                {
                    if (sct.CookieBlob == null)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.NoCookieInSct)));
                    }

                    // if the token has a cookie, write it out
                    writer.WriteStartElement(CoreWCF.XD.DotNetSecurityDictionary.Prefix.Value, parent.SerializerDictionary.Cookie, CoreWCF.XD.DotNetSecurityDictionary.Namespace);
                    writer.WriteBase64(sct.CookieBlob, 0, sct.CookieBlob.Length);
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
            }
 /// <summary>
 /// Writes the body of an object in the output
 /// </summary>
 public override void WriteObjectContent(XmlDictionaryWriter writer, object graph)
 {
     if (writer == null)
     {
         throw new ArgumentNullException(nameof(writer));
     }
     if (graph == null)
     {
         writer.WriteAttributeString("nil", "true");
     }
     else
     {
         using (MemoryStream ms = new MemoryStream())
         {
             if (isList)
             {
                 model.Serialize(ms, graph, null);
             }
             else
             {
                 using (ProtoWriter protoWriter = new ProtoWriter(ms, model, null))
                 {
                     model.Serialize(key, graph, protoWriter);
                 }
             }
             byte[] buffer = ms.GetBuffer();
             writer.WriteBase64(buffer, 0, (int)ms.Length);
         }
     }
 }
        private string RawMessageToString(ref Message message)
        {
            XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();

            bodyReader.ReadStartElement("Binary");
            byte[] requestBody     = bodyReader.ReadContentAsBase64();
            string messageAsString = Encoding.UTF8.GetString(requestBody);

            MemoryStream        ms     = new MemoryStream();
            XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms);

            writer.WriteStartElement("Binary");
            writer.WriteBase64(requestBody, 0, requestBody.Length);
            writer.WriteEndElement();
            writer.Flush();

            ms.Position = 0;

            XmlDictionaryReader reader     = XmlDictionaryReader.CreateBinaryReader(ms, XmlDictionaryReaderQuotas.Max);
            Message             newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);

            newMessage.Properties.CopyProperties(message.Properties);
            message = newMessage;

            return(messageAsString);
        }
Example #22
0
            public override void WriteContent(XmlDictionaryWriter writer, SecurityKeyIdentifierClause clause)
            {
                writer.WriteStartElement(XD.SecurityJan2004Dictionary.Prefix.Value, XD.SecurityJan2004Dictionary.KeyIdentifier, XD.SecurityJan2004Dictionary.Namespace);
                writer.WriteAttributeString(XD.SecurityJan2004Dictionary.ValueType, null, ValueTypeUri);
                KerberosTicketHashKeyIdentifierClause binaryClause = clause as KerberosTicketHashKeyIdentifierClause;

                if (this.EmitBspRequiredAttributes)
                {
                    // Emit the encodingType attribute.
                    writer.WriteAttributeString(XD.SecurityJan2004Dictionary.EncodingType, null, DefaultEncodingType);
                }
                string encoding = DefaultEncodingType;

                byte[] keyIdentifier = binaryClause.GetBuffer();
                if (encoding == EncodingTypeValueBase64Binary)
                {
                    writer.WriteBase64(keyIdentifier, 0, keyIdentifier.Length);
                }
                else if (encoding == EncodingTypeValueHexBinary)
                {
                    writer.WriteBinHex(keyIdentifier, 0, keyIdentifier.Length);
                }
                else if (encoding == EncodingTypeValueText)
                {
                    writer.WriteString(new UTF8Encoding().GetString(keyIdentifier, 0, keyIdentifier.Length));
                }
                writer.WriteEndElement();
            }
Example #23
0
            public override void WriteTokenCore(XmlDictionaryWriter writer, SecurityToken token)
            {
                WriteBinaryCore(token, out string id, out byte[] rawData);

                if (rawData == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rawData));
                }

                writer.WriteStartElement(XD.SecurityJan2004Dictionary.Prefix.Value, s_elementName, XD.SecurityJan2004Dictionary.Namespace);
                if (id != null)
                {
                    writer.WriteAttributeString(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace, id);
                }
                if (_valueTypeUris != null)
                {
                    writer.WriteAttributeString(s_valueTypeAttribute, null, _valueTypeUris[0]);
                }
                if (_tokenSerializer.EmitBspRequiredAttributes)
                {
                    writer.WriteAttributeString(s_encodingTypeAttribute, null, EncodingTypeValueBase64Binary);
                }
                writer.WriteBase64(rawData, 0, rawData.Length);
                writer.WriteEndElement(); // BinarySecurityToken
            }
 /// <summary>
 /// Encodes the specified binary bytes as Base64 and writes out the resulting text.
 /// </summary>
 /// <param name="buffer">Byte array to encode.</param>
 /// <param name="index">The position in the buffer indicating the start of the bytes to write.</param>
 /// <param name="count">The number of bytes to write.</param>
 public override void WriteBase64(byte[] buffer, int index, int count)
 {
     _innerWriter.WriteBase64(buffer, index, count);
     if (_tracingWriter != null)
     {
         _tracingWriter.WriteBase64(buffer, index, count);
     }
 }
Example #25
0
        public override void WriteObject(XmlDictionaryWriter writer, object graph)
        {
            var data = EncodeObject(graph);

            writer.WriteStartElement("Data");
            writer.WriteBase64(data, 0, data.Length);
            writer.WriteEndElement();
        }
Example #26
0
            protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
            {
                writer.WriteStartElement("Binary");
                var buffer = Encoding.GetBytes(Content);

                writer.WriteBase64(buffer, 0, buffer.Length);
                writer.WriteEndElement();
            }
Example #27
0
        void WriteChunkCallback(XmlDictionaryWriter writer, object state)
        {
            ChunkState chunkState = (ChunkState)state;

            writer.WriteStartElement(ChunkingUtils.ChunkElement, ChunkingUtils.ChunkNs);
            writer.WriteBase64(chunkState.Buffer, 0, chunkState.Count);
            writer.WriteEndElement();
        }
Example #28
0
        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            writer.WriteStartElement(RawMessageEncoder.StreamElementName, string.Empty);
            if (this.bodyStream != null)
            {
                int    len = (int)this.bodyStream.Length;
                byte[] buf = new byte[len];
                this.bodyStream.Read(buf, 0, len);
                writer.WriteBase64(buf, 0, len);
            }
            else
            {
                writer.WriteBase64(this.bodyBytes, this.index, this.count);
            }

            writer.WriteEndElement();
        }
Example #29
0
        private void WriteChunkCallback(XmlDictionaryWriter writer, object state)
        {
            ChunkState chunkState = (ChunkState)state;

            writer.WriteStartElement("chunk", "http://bam.nexon.com/chunking");
            writer.WriteBase64(chunkState.Buffer, 0, chunkState.Count);
            writer.WriteEndElement();
        }
Example #30
0
            public override void WriteTokenCore(XmlDictionaryWriter writer, SecurityToken token)
            {
                DerivedKeySecurityToken derivedKeyToken = token as DerivedKeySecurityToken;
                string serializerPrefix = parent.SerializerDictionary.Prefix.Value;

                writer.WriteStartElement(serializerPrefix, parent.SerializerDictionary.DerivedKeyToken, parent.SerializerDictionary.Namespace);
                if (derivedKeyToken.Id != null)
                {
                    writer.WriteAttributeString(CoreWCF.XD.UtilityDictionary.Prefix.Value, CoreWCF.XD.UtilityDictionary.IdAttribute, CoreWCF.XD.UtilityDictionary.Namespace, derivedKeyToken.Id);
                }
                if (derivedKeyToken.KeyDerivationAlgorithm != parent.DerivationAlgorithm)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.UnsupportedKeyDerivationAlgorithm, derivedKeyToken.KeyDerivationAlgorithm)));
                }
                parent.WSSecurityTokenSerializer.WriteKeyIdentifierClause(writer, derivedKeyToken.TokenToDeriveIdentifier);

                // Don't support Properties element
                if (derivedKeyToken.Generation > 0 || derivedKeyToken.Offset > 0 || derivedKeyToken.Length != 32)
                {
                    // this means they're both specified (offset must be gen * length) - we'll write generation
                    if (derivedKeyToken.Generation >= 0 && derivedKeyToken.Offset >= 0)
                    {
                        writer.WriteStartElement(serializerPrefix, parent.SerializerDictionary.Generation, parent.SerializerDictionary.Namespace);
                        writer.WriteValue(derivedKeyToken.Generation);
                        writer.WriteEndElement();
                    }
                    else if (derivedKeyToken.Generation != -1)
                    {
                        writer.WriteStartElement(serializerPrefix, parent.SerializerDictionary.Generation, parent.SerializerDictionary.Namespace);
                        writer.WriteValue(derivedKeyToken.Generation);
                        writer.WriteEndElement();
                    }
                    else if (derivedKeyToken.Offset != -1)
                    {
                        writer.WriteStartElement(serializerPrefix, parent.SerializerDictionary.Offset, parent.SerializerDictionary.Namespace);
                        writer.WriteValue(derivedKeyToken.Offset);
                        writer.WriteEndElement();
                    }

                    if (derivedKeyToken.Length != 32)
                    {
                        writer.WriteStartElement(serializerPrefix, parent.SerializerDictionary.Length, parent.SerializerDictionary.Namespace);
                        writer.WriteValue(derivedKeyToken.Length);
                        writer.WriteEndElement();
                    }
                }

                if (derivedKeyToken.Label != null)
                {
                    writer.WriteStartElement(serializerPrefix, parent.SerializerDictionary.Generation, parent.SerializerDictionary.Namespace);
                    writer.WriteString(derivedKeyToken.Label);
                    writer.WriteEndElement();
                }
                writer.WriteStartElement(serializerPrefix, parent.SerializerDictionary.Nonce, parent.SerializerDictionary.Namespace);
                writer.WriteBase64(derivedKeyToken.Nonce, 0, derivedKeyToken.Nonce.Length);
                writer.WriteEndElement();
                writer.WriteEndElement();
            }