using System;
using System.IO;
namespace Org.BouncyCastle.Tls
{
public sealed class DigitallySigned
{
private readonly SignatureAndHashAlgorithm m_algorithm;
private readonly byte[] m_signature;
public DigitallySigned(SignatureAndHashAlgorithm algorithm, byte[] signature)
{
if (signature == null)
throw new ArgumentNullException("signature");
this.m_algorithm = algorithm;
this.m_signature = signature;
}
/// a (or null before TLS 1.2).
public SignatureAndHashAlgorithm Algorithm
{
get { return m_algorithm; }
}
public byte[] Signature
{
get { return m_signature; }
}
/// Encode this to a .
/// the to encode to.
///
public void Encode(Stream output)
{
if (m_algorithm != null)
{
m_algorithm.Encode(output);
}
TlsUtilities.WriteOpaque16(m_signature, output);
}
/// Parse a from a .
/// the of the current connection.
/// the to parse from.
/// a object.
///
public static DigitallySigned Parse(TlsContext context, Stream input)
{
SignatureAndHashAlgorithm algorithm = null;
if (TlsUtilities.IsTlsV12(context))
{
algorithm = SignatureAndHashAlgorithm.Parse(input);
if (SignatureAlgorithm.anonymous == algorithm.Signature)
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
}
byte[] signature = TlsUtilities.ReadOpaque16(input);
return new DigitallySigned(algorithm, signature);
}
}
}