summary refs log tree commit diff
path: root/crypto/src/crmf/CertificateResponseBuilder.cs
blob: d1e85ea23149caa553ecd88eacbfe0b2179c7651 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.Crmf;
using Org.BouncyCastle.Cms;
using Org.BouncyCastle.X509;

namespace Org.BouncyCastle.Crmf
{
    /// <summary>Builder for CertificateResponse objects (the CertResponse CRMF equivalent).</summary>
    public class CertificateResponseBuilder
    {
        private readonly DerInteger m_certReqID;
        private readonly PkiStatusInfo m_statusInfo;

        private CertifiedKeyPair m_certKeyPair;
        private Asn1OctetString m_rspInfo;

        /**
         * Base constructor.
         *
         * @param certReqId the request ID for the response.
         * @param statusInfo the status info to associate with the response.
         */
        public CertificateResponseBuilder(DerInteger certReqID, PkiStatusInfo statusInfo)
        {
            m_certReqID = certReqID;
            m_statusInfo = statusInfo;
        }

        /**
         * Specify the certificate to assign to this response (in plaintext).
         *
         * @param certificate the X.509 PK certificate to include.
         * @return the current builder.
         */
        public virtual CertificateResponseBuilder WithCertificate(X509Certificate certificate)
        {
            if (m_certKeyPair != null)
                throw new InvalidOperationException("certificate in response already set");

            var cmpCertificate = new CmpCertificate(certificate.CertificateStructure);

            m_certKeyPair = new CertifiedKeyPair(new CertOrEncCert(cmpCertificate));

            return this;
        }

        /**
         * Specify the certificate to assign to this response (in plaintext).
         *
         * @param cmpCertificate the X.509 PK certificate to include.
         * @return the current builder.
         */
        public virtual CertificateResponseBuilder WithCertificate(CmpCertificate cmpCertificate)
        {
            if (m_certKeyPair != null)
                throw new InvalidOperationException("certificate in response already set");

            m_certKeyPair = new CertifiedKeyPair(new CertOrEncCert(cmpCertificate));

            return this;
        }

        /**
         * Specify the encrypted certificate to assign to this response (in plaintext).
         *
         * @param encryptedCertificate an encrypted
         * @return the current builder.
         */
        public virtual CertificateResponseBuilder WithCertificate(CmsEnvelopedData encryptedCertificate)
        {
            if (m_certKeyPair != null)
                throw new InvalidOperationException("certificate in response already set");

            var encryptedKey = new EncryptedKey(EnvelopedData.GetInstance(encryptedCertificate.ContentInfo.Content));

            m_certKeyPair = new CertifiedKeyPair(new CertOrEncCert(encryptedKey));

            return this;
        }

        /**
         * Specify the response info field on the response.
         *
         * @param responseInfo a response info string.
         * @return the current builder.
         */
        public virtual CertificateResponseBuilder WithResponseInfo(byte[] responseInfo)
        {
            if (m_rspInfo != null)
                throw new InvalidOperationException("response info already set");

            m_rspInfo = new DerOctetString(responseInfo);

            return this;
        }

        public virtual CertificateResponse Build() =>
            new CertificateResponse(new CertResponse(m_certReqID, m_statusInfo, m_certKeyPair, m_rspInfo));
    }
}