public override SecurityKeyIdentifierClause ReadClause(XmlDictionaryReader reader, byte[] derivationNonce, int derivationLength, string tokenType) { string encodingType = reader.GetAttribute(XD.SecurityJan2004Dictionary.EncodingType, null); if (encodingType == null) { encodingType = DefaultEncodingType; } reader.ReadStartElement(); byte[] bytes; if (encodingType == EncodingTypeValueBase64Binary) { bytes = reader.ReadContentAsBase64(); } else if (encodingType == EncodingTypeValueHexBinary) { bytes = HexBinary.Parse(reader.ReadContentAsString()).Value; } else if (encodingType == EncodingTypeValueText) { bytes = new UTF8Encoding().GetBytes(reader.ReadContentAsString()); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityMessageSerializationException(SR.GetString(SR.UnknownEncodingInKeyIdentifier))); } reader.ReadEndElement(); return(CreateClause(bytes, derivationNonce, derivationLength)); }
public override SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXmlCore(XmlElement issuedTokenXml, SecurityTokenReferenceStyle tokenReferenceStyle) { TokenReferenceStyleHelper.Validate(tokenReferenceStyle); switch (tokenReferenceStyle) { case SecurityTokenReferenceStyle.Internal: return(CreateDirectReference(issuedTokenXml, UtilityStrings.IdAttribute, UtilityStrings.Namespace, TokenType)); case SecurityTokenReferenceStyle.External: string encoding = issuedTokenXml.GetAttribute(EncodingTypeAttributeString, null); string encodedData = issuedTokenXml.InnerText; byte[] binaryData; if (encoding == null || encoding == EncodingTypeValueBase64Binary) { binaryData = Convert.FromBase64String(encodedData); } else if (encoding == EncodingTypeValueHexBinary) { binaryData = HexBinary.Parse(encodedData).Value; } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.UnknownEncodingInBinarySecurityToken)); } return(CreateKeyIdentifierClauseFromBinaryCore(binaryData)); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(tokenReferenceStyle))); } }
int ReadBinaryContent(byte[] buffer, int offset, int count, bool isBase64) { CryptoHelper.ValidateBufferBounds(buffer, offset, count); // // Concatentate text nodes to get entire element value before attempting to convert // XmlDictionaryReader.CreateDictionaryReader( XmlReader ) creates a reader that returns base64 in a single text node // XmlDictionaryReader.CreateTextReader( Stream ) creates a reader that produces multiple text and whitespace nodes // Attribute nodes consist of only a single value // if (this.contentStream == null) { string encodedValue; if (NodeType == XmlNodeType.Attribute) { encodedValue = Value; } else { StringBuilder fullText = new StringBuilder(1000); while (NodeType != XmlNodeType.Element && NodeType != XmlNodeType.EndElement) { switch (NodeType) { // concatenate text nodes case XmlNodeType.Text: fullText.Append(Value); break; // skip whitespace case XmlNodeType.Whitespace: break; } Read(); } encodedValue = fullText.ToString(); } byte[] value = isBase64 ? Convert.FromBase64String(encodedValue) : HexBinary.Parse(encodedValue).Value; this.contentStream = new MemoryStream(value); } int read = this.contentStream.Read(buffer, offset, count); if (read == 0) { this.contentStream.Close(); this.contentStream = null; } return(read); }
public override SecurityToken ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver) { string wsuId = reader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace); string valueTypeUri = reader.GetAttribute(ValueTypeAttribute, null); string encoding = reader.GetAttribute(EncodingTypeAttribute, null); byte[] binaryData; if (encoding == null || encoding == EncodingTypeValueBase64Binary) { binaryData = reader.ReadElementContentAsBase64(); } else if (encoding == EncodingTypeValueHexBinary) { binaryData = HexBinary.Parse(reader.ReadElementContentAsString()).Value; } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.UnknownEncodingInBinarySecurityToken)); } return(ReadBinaryCore(wsuId, valueTypeUri, binaryData)); }