2 // BinaryMessageEncoder.cs
4 // Author: Atsushi Enomoto (atsushi@ximian.com)
6 // Copyright (C) 2005,2009 Novell, Inc (http://www.novell.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.ObjectModel;
30 using System.ServiceModel;
34 namespace System.ServiceModel.Channels
36 internal class BinaryMessageEncoder : MessageEncoder
38 static XmlDictionary soap_dictionary;
40 // See [MC-NBFS] in Microsoft OSP. The strings are copied from the PDF, so the actual values might be wrong.
41 static readonly string [] dict_strings = {
42 "mustUnderstand", "Envelope",
43 "http://www.w3.org/2003/05/soap-envelope",
44 "http://www.w3.org/2005/08/addressing", "Header", "Action", "To", "Body", "Algorithm", "RelatesTo",
45 "http://www.w3.org/2005/08/addressing/anonymous", "URI", "Reference", "MessageID", "Id", "Identifier",
46 "http://schemas.xmlsoap.org/ws/2005/02/rm", "Transforms", "Transform", "DigestMethod", "DigestValue", "Address", "ReplyTo", "SequenceAcknowledgement", "AcknowledgementRange", "Upper", "Lower", "BufferRemaining",
47 "http://schemas.microsoft.com/ws/2006/05/rm",
48 "http://schemas.xmlsoap.org/ws/2005/02/rm/SequenceAcknowledgement", "SecurityTokenReference", "Sequence", "MessageNumber",
49 "http://www.w3.org/2000/09/xmldsig#",
50 "http://www.w3.org/2000/09/xmldsig#enveloped-signature", "KeyInfo",
51 "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
52 "http://www.w3.org/2001/04/xmlenc#",
53 "http://schemas.xmlsoap.org/ws/2005/02/sc", "DerivedKeyToken", "Nonce", "Signature", "SignedInfo", "CanonicalizationMethod", "SignatureMethod", "SignatureValue", "DataReference", "EncryptedData", "EncryptionMethod", "CipherData", "CipherValue",
54 "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Security", "Timestamp", "Created", "Expires", "Length", "ReferenceList", "ValueType", "Type", "EncryptedHeader",
55 "http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd", "RequestSecurityTokenResponseCollection",
56 "http://schemas.xmlsoap.org/ws/2005/02/trust",
57 "http://schemas.xmlsoap.org/ws/2005/02/trust#BinarySecret",
58 "http://schemas.microsoft.com/ws/2006/02/transactions", "s", "Fault", "MustUnderstand", "role", "relay", "Code", "Reason", "Text", "Node", "Role", "Detail", "Value", "Subcode", "NotUnderstood", "qname", "", "From", "FaultTo", "EndpointReference", "PortType", "ServiceName", "PortName", "ReferenceProperties", "RelationshipType", "Reply", "a",
59 "http://schemas.xmlsoap.org/ws/2006/02/addressingidentity", "Identity", "Spn", "Upn", "Rsa", "Dns", "X509v3Certificate",
60 "http://www.w3.org/2005/08/addressing/fault", "ReferenceParameters", "IsReferenceParameter",
61 "http://www.w3.org/2005/08/addressing/reply",
62 "http://www.w3.org/2005/08/addressing/none", "Metadata",
63 "http://schemas.xmlsoap.org/ws/2004/08/addressing",
64 "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous",
65 "http://schemas.xmlsoap.org/ws/2004/08/addressing/fault",
66 "http://schemas.xmlsoap.org/ws/2004/06/addressingex", "RedirectTo", "Via",
67 "http://www.w3.org/2001/10/xml-exc-c14n#", "PrefixList", "InclusiveNamespaces", "ec", "SecurityContextToken", "Generation", "Label", "Offset", "Properties", "Cookie", "wsc",
68 "http://schemas.xmlsoap.org/ws/2004/04/sc",
69 "http://schemas.xmlsoap.org/ws/2004/04/security/sc/dk",
70 "http://schemas.xmlsoap.org/ws/2004/04/security/sc/sct",
71 "http://schemas.xmlsoap.org/ws/2004/04/security/trust/RST/SCT",
72 "http://schemas.xmlsoap.org/ws/2004/04/security/trust/RSTR/SCT", "RenewNeeded", "BadContextToken", "c",
73 "http://schemas.xmlsoap.org/ws/2005/02/sc/dk",
74 "http://schemas.xmlsoap.org/ws/2005/02/sc/sct",
75 "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT",
76 "http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/SCT",
77 "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT/Renew",
78 "http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/SCT/Renew",
79 "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT/Cancel",
80 "http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/SCT/Cancel",
81 "http://www.w3.org/2001/04/xmlenc#aes128-cbc",
82 "http://www.w3.org/2001/04/xmlenc#kw-aes128",
83 "http://www.w3.org/2001/04/xmlenc#aes192-cbc",
84 "http://www.w3.org/2001/04/xmlenc#kw-aes192",
85 "http://www.w3.org/2001/04/xmlenc#aes256-cbc",
86 "http://www.w3.org/2001/04/xmlenc#kw-aes256",
87 "http://www.w3.org/2001/04/xmlenc#des-cbc",
88 "http://www.w3.org/2000/09/xmldsig#dsa-sha1",
89 "http://www.w3.org/2001/10/xml-exc-c14n#WithComments",
90 "http://www.w3.org/2000/09/xmldsig#hmac-sha1",
91 "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256",
92 "http://schemas.xmlsoap.org/ws/2005/02/sc/dk/p_sha1",
93 "http://www.w3.org/2001/04/xmlenc#ripemd160",
94 "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p",
95 "http://www.w3.org/2000/09/xmldsig#rsa-sha1",
96 "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256",
97 "http://www.w3.org/2001/04/xmlenc#rsa-1_5",
98 "http://www.w3.org/2000/09/xmldsig#sha1",
99 "http://www.w3.org/2001/04/xmlenc#sha256",
100 "http://www.w3.org/2001/04/xmlenc#sha512",
101 "http://www.w3.org/2001/04/xmlenc#tripledes-cbc",
102 "http://www.w3.org/2001/04/xmlenc#kw-tripledes",
103 "http://schemas.xmlsoap.org/2005/02/trust/tlsnego#TLS_Wrap",
104 "http://schemas.xmlsoap.org/2005/02/trust/spnego#GSS_Wrap",
105 "http://schemas.microsoft.com/ws/2006/05/security", "dnse", "o", "Password", "PasswordText", "Username", "UsernameToken", "BinarySecurityToken", "EncodingType", "KeyIdentifier",
106 "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary",
107 "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#HexBinary",
108 "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Text",
109 "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier",
110 "http://docs.oasis-open.org/wss/oasis-wss-kerberos-token-profile-1.1#GSS_Kerberosv5_AP_REQ",
111 "http://docs.oasis-open.org/wss/oasis-wss-kerberos-token-profile-1.1#GSS_Kerberosv5_AP_REQ1510",
112 "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.0#SAMLAssertionID", "Assertion", "urn:oasis:names:tc:SAML:1.0:assertion",
113 "http://docs.oasis-open.org/wss/oasis-wss-rel-token-profile-1.0.pdf#license", "FailedAuthentication", "InvalidSecurityToken", "InvalidSecurity", "k", "SignatureConfirmation", "TokenType",
114 "http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#ThumbprintSHA1",
115 "http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#EncryptedKey",
116 "http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#EncryptedKeySHA1",
117 "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1",
118 "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0",
119 "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLID", "AUTH-HASH", "RequestSecurityTokenResponse", "KeySize", "RequestedTokenReference", "AppliesTo", "Authenticator", "CombinedHash", "BinaryExchange", "Lifetime", "RequestedSecurityToken", "Entropy", "RequestedProofToken", "ComputedKey", "RequestSecurityToken", "RequestType", "Context", "BinarySecret",
120 "http://schemas.xmlsoap.org/ws/2005/02/trust/spnego",
121 "http://schemas.xmlsoap.org/ws/2005/02/trust/tlsnego", "wst",
122 "http://schemas.xmlsoap.org/ws/2004/04/trust",
123 "http://schemas.xmlsoap.org/ws/2004/04/security/trust/RST/Issue",
124 "http://schemas.xmlsoap.org/ws/2004/04/security/trust/RSTR/Issue",
125 "http://schemas.xmlsoap.org/ws/2004/04/security/trust/Issue",
126 "http://schemas.xmlsoap.org/ws/2004/04/security/trust/CK/PSHA1",
127 "http://schemas.xmlsoap.org/ws/2004/04/security/trust/SymmetricKey",
128 "http://schemas.xmlsoap.org/ws/2004/04/security/trust/Nonce", "KeyType",
129 "http://schemas.xmlsoap.org/ws/2004/04/trust/SymmetricKey",
130 "http://schemas.xmlsoap.org/ws/2004/04/trust/PublicKey", "Claims", "InvalidRequest", "RequestFailed", "SignWith", "EncryptWith", "EncryptionAlgorithm", "CanonicalizationAlgorithm", "ComputedKeyAlgorithm", "UseKey",
131 "http://schemas.microsoft.com/net/2004/07/secext/WS-SPNego",
132 "http://schemas.microsoft.com/net/2004/07/secext/TLSNego", "t",
133 "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue",
134 "http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue",
135 "http://schemas.xmlsoap.org/ws/2005/02/trust/Issue",
136 "http://schemas.xmlsoap.org/ws/2005/02/trust/SymmetricKey",
137 "http://schemas.xmlsoap.org/ws/2005/02/trust/CK/PSHA1",
138 "http://schemas.xmlsoap.org/ws/2005/02/trust/Nonce", "RenewTarget", "CancelTarget", "RequestedTokenCancelled", "RequestedAttachedReference", "RequestedUnattachedReference", "IssuedTokens",
139 "http://schemas.xmlsoap.org/ws/2005/02/trust/Renew",
140 "http://schemas.xmlsoap.org/ws/2005/02/trust/Cancel",
141 "http://schemas.xmlsoap.org/ws/2005/02/trust/PublicKey", "Access", "AccessDecision", "Advice", "AssertionID", "AssertionIDReference", "Attribute", "AttributeName", "AttributeNamespace", "AttributeStatement", "AttributeValue", "Audience", "AudienceRestrictionCondition", "AuthenticationInstant", "AuthenticationMethod", "AuthenticationStatement", "AuthorityBinding", "AuthorityKind", "AuthorizationDecisionStatement", "Binding", "Condition", "Conditions", "Decision", "DoNotCacheCondition", "Evidence", "IssueInstant", "Issuer", "Location", "MajorVersion", "MinorVersion", "NameIdentifier", "Format", "NameQualifier", "Namespace", "NotBefore", "NotOnOrAfter", "saml", "Statement", "Subject", "SubjectConfirmation", "SubjectConfirmationData", "ConfirmationMethod", "urn:oasis:names:tc:SAML:1.0:cm:holder-of-key", "urn:oasis:names:tc:SAML:1.0:cm:sender-vouches", "SubjectLocality", "DNSAddress", "IPAddress", "SubjectStatement", "urn:oasis:names:tc:SAML:1.0:am:unspecified", "xmlns", "Resource", "UserName", "urn:oasis:names:tc:SAML:1.1:nameid-format:WindowsDomainQualifiedName", "EmailName", "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", "u", "ChannelInstance",
142 "http://schemas.microsoft.com/ws/2005/02/duplex", "Encoding", "MimeType", "CarriedKeyName", "Recipient", "EncryptedKey", "KeyReference", "e",
143 "http://www.w3.org/2001/04/xmlenc#Element",
144 "http://www.w3.org/2001/04/xmlenc#Content", "KeyName", "MgmtData", "KeyValue", "RSAKeyValue", "Modulus", "Exponent", "X509Data", "X509IssuerSerial", "X509IssuerName", "X509SerialNumber", "X509Certificate", "AckRequested",
145 "http://schemas.xmlsoap.org/ws/2005/02/rm/AckRequested", "AcksTo", "Accept", "CreateSequence",
146 "http://schemas.xmlsoap.org/ws/2005/02/rm/CreateSequence", "CreateSequenceRefused", "CreateSequenceResponse",
147 "http://schemas.xmlsoap.org/ws/2005/02/rm/CreateSequenceResponse", "FaultCode", "InvalidAcknowledgement", "LastMessage",
148 "http://schemas.xmlsoap.org/ws/2005/02/rm/LastMessage", "LastMessageNumberExceeded", "MessageNumberRollover", "Nack", "netrm", "Offer", "r", "SequenceFault", "SequenceTerminated", "TerminateSequence",
149 "http://schemas.xmlsoap.org/ws/2005/02/rm/TerminateSequence", "UnknownSequence",
150 "http://schemas.microsoft.com/ws/2006/02/tx/oletx", "oletx", "OleTxTransaction", "PropagationToken",
151 "http://schemas.xmlsoap.org/ws/2004/10/wscoor", "wscoor", "CreateCoordinationContext", "CreateCoordinationContextResponse", "CoordinationContext", "CurrentContext", "CoordinationType", "RegistrationService", "Register", "RegisterResponse", "ProtocolIdentifier", "CoordinatorProtocolService", "ParticipantProtocolService",
152 "http://schemas.xmlsoap.org/ws/2004/10/wscoor/CreateCoordinationContext",
153 "http://schemas.xmlsoap.org/ws/2004/10/wscoor/CreateCoordinationContextResponse",
154 "http://schemas.xmlsoap.org/ws/2004/10/wscoor/Register",
155 "http://schemas.xmlsoap.org/ws/2004/10/wscoor/RegisterResponse",
156 "http://schemas.xmlsoap.org/ws/2004/10/wscoor/fault", "ActivationCoordinatorPortType", "RegistrationCoordinatorPortType", "InvalidState", "InvalidProtocol", "InvalidParameters", "NoActivity", "ContextRefused", "AlreadyRegistered",
157 "http://schemas.xmlsoap.org/ws/2004/10/wsat", "wsat",
158 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Completion",
159 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Durable2PC",
160 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Volatile2PC", "Prepare", "Prepared", "ReadOnly", "Commit", "Rollback", "Committed", "Aborted", "Replay",
161 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Commit",
162 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Rollback",
163 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Committed",
164 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Aborted",
165 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Prepare",
166 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Prepared",
167 "http://schemas.xmlsoap.org/ws/2004/10/wsat/ReadOnly",
168 "http://schemas.xmlsoap.org/ws/2004/10/wsat/Replay",
169 "http://schemas.xmlsoap.org/ws/2004/10/wsat/fault", "CompletionCoordinatorPortType", "CompletionParticipantPortType", "CoordinatorPortType", "ParticipantPortType", "InconsistentInternalState", "mstx", "Enlistment", "protocol", "LocalTransactionId", "IsolationLevel", "IsolationFlags", "Description", "Loopback", "RegisterInfo", "ContextId", "TokenId", "AccessDenied", "InvalidPolicy", "CoordinatorRegistrationFailed", "TooManyEnlistments", "Disabled", "ActivityId",
170 "http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics",
171 "http://docs.oasis-open.org/wss/oasis-wss-kerberos-token-profile-1.1#Kerberosv5APREQSHA1",
172 "http://schemas.xmlsoap.org/ws/2002/12/policy", "FloodMessage", "LinkUtility", "Hops",
173 "http://schemas.microsoft.com/net/2006/05/peer/HopCount", "PeerVia",
174 "http://schemas.microsoft.com/net/2006/05/peer", "PeerFlooder", "PeerTo",
175 "http://schemas.microsoft.com/ws/2005/05/routing", "PacketRoutable",
176 "http://schemas.microsoft.com/ws/2005/05/addressing/none",
177 "http://schemas.microsoft.com/ws/2005/05/envelope/none",
178 "http://www.w3.org/2001/XMLSchema-instance",
179 "http://www.w3.org/2001/XMLSchema", "nil", "type", "char", "boolean", "byte", "unsignedByte", "short", "unsignedShort", "int", "unsignedInt", "long", "unsignedLong", "float", "double", "decimal", "dateTime", "string", "base64Binary", "anyType", "duration", "guid", "anyURI", "QName", "time", "date", "hexBinary", "gYearMonth", "gYear", "gMonthDay", "gDay", "gMonth", "integer", "positiveInteger", "negativeInteger", "nonPositiveInteger", "nonNegativeInteger", "normalizedString", "ConnectionLimitReached",
180 "http://schemas.xmlsoap.org/soap/envelope/", "Actor", "Faultcode", "Faultstring", "Faultactor", "Detail"
183 static BinaryMessageEncoder ()
185 var d = new XmlDictionary ();
187 foreach (var s in dict_strings)
191 public BinaryMessageEncoder ()
195 public BinaryMessageEncoder (BinaryMessageEncoderFactory owner, bool session)
198 this.session = session;
201 BinaryMessageEncoderFactory owner;
204 public override string ContentType {
205 get { return MediaType; }
208 public override string MediaType {
209 get { return session ? "application/soap+msbinsession1" : "application/soap+msbin1"; }
212 public override MessageVersion MessageVersion {
213 get { return MessageVersion.Default; }
217 public override Message ReadMessage (ArraySegment<byte> buffer,
218 BufferManager bufferManager, string contentType)
220 if (contentType != ContentType)
221 throw new ProtocolException ("Only content type 'application/soap+msbin1' is allowed.");
223 // FIXME: retrieve reader session and message body.
225 throw new NotImplementedException ();
228 // FIXME: use bufferManager
229 return Message.CreateMessage (
230 XmlDictionaryReader.CreateBinaryReader (
231 buffer.Array, buffer.Offset, buffer.Count,
233 owner != null ? owner.Owner.ReaderQuotas : new XmlDictionaryReaderQuotas ()),
234 int.MaxValue, MessageVersion);
238 // It is sort of nasty hack, but there is no other way to provide reader/writer session from TCP stream.
239 internal XmlBinaryReaderSession CurrentReaderSession { get; set; }
240 internal XmlBinaryWriterSession CurrentWriterSession { get; set; }
242 public override Message ReadMessage (Stream stream,
243 int maxSizeOfHeaders, string contentType)
245 if (contentType != ContentType)
246 throw new ProtocolException ("Only content type 'application/soap+msbin1' is allowed.");
248 return Message.CreateMessage (
249 XmlDictionaryReader.CreateBinaryReader (stream, soap_dictionary, owner != null ? owner.Owner.ReaderQuotas : new XmlDictionaryReaderQuotas (), session ? CurrentReaderSession : null),
250 maxSizeOfHeaders, MessageVersion);
253 public override void WriteMessage (Message message, Stream stream)
255 VerifyMessageVersion (message);
257 message.WriteMessage (XmlDictionaryWriter.CreateBinaryWriter (stream, soap_dictionary, CurrentWriterSession));
261 public override ArraySegment<byte> WriteMessage (
262 Message message, int maxMessageSize,
263 BufferManager bufferManager, int messageOffset)
265 VerifyMessageVersion (message);
267 throw new NotImplementedException ();