summary refs log tree commit diff
path: root/crypto/src/tls/DefaultTlsCredentialedSigner.cs
blob: 64bc30a8e018742b6aa5ca2163b286e97ac4cb0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;

using Org.BouncyCastle.Tls.Crypto;
using Org.BouncyCastle.Tls.Crypto.Impl;

namespace Org.BouncyCastle.Tls
{
    /// <summary>Container class for generating signatures that carries the signature type, parameters, public key
    /// certificate and public key's associated signer object.</summary>
    public class DefaultTlsCredentialedSigner
        : TlsCredentialedSigner
    {
        protected readonly TlsCryptoParameters m_cryptoParams;
        protected readonly Certificate m_certificate;
        protected readonly SignatureAndHashAlgorithm m_signatureAndHashAlgorithm;
        protected readonly TlsSigner m_signer;

        public DefaultTlsCredentialedSigner(TlsCryptoParameters cryptoParams, TlsSigner signer,
            Certificate certificate, SignatureAndHashAlgorithm signatureAndHashAlgorithm)
        {
            if (certificate == null)
                throw new ArgumentNullException("certificate");
            if (certificate.IsEmpty)
                throw new ArgumentException("cannot be empty", "certificate");
            if (signer == null)
                throw new ArgumentNullException("signer");

            this.m_cryptoParams = cryptoParams;
            this.m_certificate = certificate;
            this.m_signatureAndHashAlgorithm = signatureAndHashAlgorithm;
            this.m_signer = signer;
        }

        public virtual Certificate Certificate
        {
            get { return m_certificate; }
        }

        public virtual byte[] GenerateRawSignature(byte[] hash)
        {
            return m_signer.GenerateRawSignature(GetEffectiveAlgorithm(), hash);
        }

        public virtual SignatureAndHashAlgorithm SignatureAndHashAlgorithm
        {
            get { return m_signatureAndHashAlgorithm; }
        }

        public virtual TlsStreamSigner GetStreamSigner()
        {
            return m_signer.GetStreamSigner(GetEffectiveAlgorithm());
        }

        protected virtual SignatureAndHashAlgorithm GetEffectiveAlgorithm()
        {
            SignatureAndHashAlgorithm algorithm = null;
            if (TlsImplUtilities.IsTlsV12(m_cryptoParams))
            {
                algorithm = SignatureAndHashAlgorithm;
                if (algorithm == null)
                    throw new InvalidOperationException("'signatureAndHashAlgorithm' cannot be null for (D)TLS 1.2+");
            }
            return algorithm;
        }
    }
}