| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | namespace CoreWCF.Security |
| | | 5 | | { |
| | | 6 | | using System; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Xml; |
| | | 10 | | using CoreWCF; |
| | | 11 | | using CanonicalFormWriter = CoreWCF.IdentityModel.CanonicalFormWriter; |
| | | 12 | | using HashStream = IdentityModel.HashStream; |
| | | 13 | | using SignatureResourcePool = CoreWCF.IdentityModel.SignatureResourcePool; |
| | | 14 | | |
| | | 15 | | internal abstract class WSUtilitySpecificationVersion |
| | | 16 | | { |
| | 0 | 17 | | internal static readonly string[] AcceptedDateTimeFormats = new string[] |
| | 0 | 18 | | { |
| | 0 | 19 | | "yyyy-MM-ddTHH:mm:ss.fffffffZ", |
| | 0 | 20 | | "yyyy-MM-ddTHH:mm:ss.ffffffZ", |
| | 0 | 21 | | "yyyy-MM-ddTHH:mm:ss.fffffZ", |
| | 0 | 22 | | "yyyy-MM-ddTHH:mm:ss.ffffZ", |
| | 0 | 23 | | "yyyy-MM-ddTHH:mm:ss.fffZ", |
| | 0 | 24 | | "yyyy-MM-ddTHH:mm:ss.ffZ", |
| | 0 | 25 | | "yyyy-MM-ddTHH:mm:ss.fZ", |
| | 0 | 26 | | "yyyy-MM-ddTHH:mm:ssZ" |
| | 0 | 27 | | }; |
| | | 28 | | |
| | 3 | 29 | | internal WSUtilitySpecificationVersion(XmlDictionaryString namespaceUri) |
| | | 30 | | { |
| | 3 | 31 | | NamespaceUri = namespaceUri; |
| | 3 | 32 | | } |
| | | 33 | | |
| | | 34 | | public static WSUtilitySpecificationVersion Default |
| | | 35 | | { |
| | 131 | 36 | | get { return OneDotZero; } |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | internal XmlDictionaryString NamespaceUri { get; } |
| | | 40 | | |
| | | 41 | | public static WSUtilitySpecificationVersion OneDotZero |
| | | 42 | | { |
| | 131 | 43 | | get { return WSUtilitySpecificationVersionOneDotZero.Instance; } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | internal abstract bool IsReaderAtTimestamp(XmlDictionaryReader reader); |
| | | 47 | | |
| | | 48 | | internal abstract SecurityTimestamp ReadTimestamp(XmlDictionaryReader reader, string digestAlgorithm, SignatureR |
| | | 49 | | |
| | | 50 | | internal abstract void WriteTimestamp(XmlDictionaryWriter writer, SecurityTimestamp timestamp); |
| | | 51 | | |
| | | 52 | | internal abstract void WriteTimestampCanonicalForm(Stream stream, SecurityTimestamp timestamp, byte[] buffer); |
| | | 53 | | |
| | | 54 | | private sealed class WSUtilitySpecificationVersionOneDotZero : WSUtilitySpecificationVersion |
| | | 55 | | { |
| | | 56 | | private WSUtilitySpecificationVersionOneDotZero() |
| | 3 | 57 | | : base(XD.UtilityDictionary.Namespace) |
| | | 58 | | { |
| | 3 | 59 | | } |
| | | 60 | | |
| | 134 | 61 | | public static WSUtilitySpecificationVersionOneDotZero Instance { get; } = new WSUtilitySpecificationVersionO |
| | | 62 | | |
| | | 63 | | internal override bool IsReaderAtTimestamp(XmlDictionaryReader reader) |
| | | 64 | | { |
| | 188 | 65 | | return reader.IsStartElement(XD.UtilityDictionary.Timestamp, XD.UtilityDictionary.Namespace); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | internal override SecurityTimestamp ReadTimestamp(XmlDictionaryReader reader, string digestAlgorithm, Signat |
| | | 69 | | { |
| | 94 | 70 | | bool canonicalize = digestAlgorithm != null && reader.CanCanonicalize; |
| | 94 | 71 | | HashStream hashStream = null; |
| | | 72 | | |
| | 94 | 73 | | reader.MoveToStartElement(XD.UtilityDictionary.Timestamp, XD.UtilityDictionary.Namespace); |
| | 94 | 74 | | if (canonicalize) |
| | | 75 | | { |
| | 23 | 76 | | hashStream = resourcePool.TakeHashStream(digestAlgorithm); |
| | 23 | 77 | | reader.StartCanonicalization(hashStream, false, null); |
| | | 78 | | } |
| | 94 | 79 | | string id = reader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace); |
| | 94 | 80 | | reader.ReadStartElement(); |
| | | 81 | | |
| | 94 | 82 | | reader.ReadStartElement(XD.UtilityDictionary.CreatedElement, XD.UtilityDictionary.Namespace); |
| | 94 | 83 | | DateTime creationTimeUtc = reader.ReadContentAsDateTime().ToUniversalTime(); |
| | 94 | 84 | | reader.ReadEndElement(); |
| | | 85 | | |
| | | 86 | | DateTime expiryTimeUtc; |
| | 94 | 87 | | if (reader.IsStartElement(XD.UtilityDictionary.ExpiresElement, XD.UtilityDictionary.Namespace)) |
| | | 88 | | { |
| | 94 | 89 | | reader.ReadStartElement(); |
| | 94 | 90 | | expiryTimeUtc = reader.ReadContentAsDateTime().ToUniversalTime(); |
| | 94 | 91 | | reader.ReadEndElement(); |
| | | 92 | | } |
| | | 93 | | else |
| | | 94 | | { |
| | 0 | 95 | | expiryTimeUtc = SecurityUtils.MaxUtcDateTime; |
| | | 96 | | } |
| | | 97 | | |
| | 94 | 98 | | reader.ReadEndElement(); |
| | | 99 | | |
| | | 100 | | byte[] digest; |
| | 94 | 101 | | if (canonicalize) |
| | | 102 | | { |
| | 23 | 103 | | reader.EndCanonicalization(); |
| | 23 | 104 | | digest = hashStream.FlushHashAndGetValue(); |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | 71 | 108 | | digest = null; |
| | | 109 | | } |
| | 94 | 110 | | return new SecurityTimestamp(creationTimeUtc, expiryTimeUtc, id, digestAlgorithm, digest); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | internal override void WriteTimestamp(XmlDictionaryWriter writer, SecurityTimestamp timestamp) |
| | | 114 | | { |
| | 63 | 115 | | writer.WriteStartElement(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.Timestamp, XD.UtilityDi |
| | 63 | 116 | | writer.WriteAttributeString(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace, timestamp. |
| | | 117 | | |
| | 63 | 118 | | writer.WriteStartElement(XD.UtilityDictionary.CreatedElement, XD.UtilityDictionary.Namespace); |
| | 63 | 119 | | char[] creationTime = timestamp.GetCreationTimeChars(); |
| | 63 | 120 | | writer.WriteChars(creationTime, 0, creationTime.Length); |
| | 63 | 121 | | writer.WriteEndElement(); // wsu:Created |
| | | 122 | | |
| | 63 | 123 | | writer.WriteStartElement(XD.UtilityDictionary.ExpiresElement, XD.UtilityDictionary.Namespace); |
| | 63 | 124 | | char[] expiryTime = timestamp.GetExpiryTimeChars(); |
| | 63 | 125 | | writer.WriteChars(expiryTime, 0, expiryTime.Length); |
| | 63 | 126 | | writer.WriteEndElement(); // wsu:Expires |
| | | 127 | | |
| | 63 | 128 | | writer.WriteEndElement(); |
| | 63 | 129 | | } |
| | | 130 | | |
| | | 131 | | internal override void WriteTimestampCanonicalForm(Stream stream, SecurityTimestamp timestamp, byte[] workBu |
| | | 132 | | { |
| | 0 | 133 | | TimestampCanonicalFormWriter.Instance.WriteCanonicalForm( |
| | 0 | 134 | | stream, |
| | 0 | 135 | | timestamp.Id, timestamp.GetCreationTimeChars(), timestamp.GetExpiryTimeChars(), |
| | 0 | 136 | | workBuffer); |
| | 0 | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private sealed class TimestampCanonicalFormWriter : CanonicalFormWriter |
| | | 141 | | { |
| | | 142 | | private const string timestamp = UtilityStrings.Prefix + ":" + UtilityStrings.Timestamp; |
| | | 143 | | private const string created = UtilityStrings.Prefix + ":" + UtilityStrings.CreatedElement; |
| | | 144 | | private const string expires = UtilityStrings.Prefix + ":" + UtilityStrings.ExpiresElement; |
| | | 145 | | private const string idAttribute = UtilityStrings.Prefix + ":" + UtilityStrings.IdAttribute; |
| | | 146 | | private const string ns = "xmlns:" + UtilityStrings.Prefix + "=\"" + UtilityStrings.Namespace + "\""; |
| | | 147 | | |
| | | 148 | | private const string xml1 = "<" + timestamp + " " + ns + " " + idAttribute + "=\""; |
| | | 149 | | private const string xml2 = "\"><" + created + ">"; |
| | | 150 | | private const string xml3 = "</" + created + "><" + expires + ">"; |
| | | 151 | | private const string xml4 = "</" + expires + "></" + timestamp + ">"; |
| | | 152 | | |
| | | 153 | | private readonly byte[] _fragment1; |
| | | 154 | | private readonly byte[] _fragment2; |
| | | 155 | | private readonly byte[] _fragment3; |
| | | 156 | | private readonly byte[] _fragment4; |
| | | 157 | | |
| | 0 | 158 | | private TimestampCanonicalFormWriter() |
| | | 159 | | { |
| | 0 | 160 | | Encoding encoding = Utf8WithoutPreamble; |
| | 0 | 161 | | _fragment1 = encoding.GetBytes(xml1); |
| | 0 | 162 | | _fragment2 = encoding.GetBytes(xml2); |
| | 0 | 163 | | _fragment3 = encoding.GetBytes(xml3); |
| | 0 | 164 | | _fragment4 = encoding.GetBytes(xml4); |
| | 0 | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | public static TimestampCanonicalFormWriter Instance { get; } = new TimestampCanonicalFormWriter(); |
| | | 168 | | |
| | | 169 | | public void WriteCanonicalForm(Stream stream, string id, char[] created, char[] expires, byte[] workBuffer) |
| | | 170 | | { |
| | 0 | 171 | | stream.Write(_fragment1, 0, _fragment1.Length); |
| | 0 | 172 | | EncodeAndWrite(stream, workBuffer, id); |
| | 0 | 173 | | stream.Write(_fragment2, 0, _fragment2.Length); |
| | 0 | 174 | | EncodeAndWrite(stream, workBuffer, created); |
| | 0 | 175 | | stream.Write(_fragment3, 0, _fragment3.Length); |
| | 0 | 176 | | EncodeAndWrite(stream, workBuffer, expires); |
| | 0 | 177 | | stream.Write(_fragment4, 0, _fragment4.Length); |
| | 0 | 178 | | } |
| | | 179 | | } |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |