summary refs log tree commit diff
path: root/crypto/src/crmf/PkiArchiveControlBuilder.cs
blob: 8cfa4ef19b206a355082795a236fde1567a17485 (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
using System;
using System.IO;

using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.Crmf;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Cms;
using Org.BouncyCastle.Crypto;

namespace Org.BouncyCastle.Crmf
{
    public class PkiArchiveControlBuilder
    {
        private readonly CmsEnvelopedDataGenerator m_envGen;
        private readonly CmsProcessableByteArray m_keyContent;

        /// <summary>
        ///Basic constructor - specify the contents of the PKIArchiveControl structure.
        /// </summary>
        /// <param name="privateKeyInfo">the private key to be archived.</param>
        /// <param name="generalName">the general name to be associated with the private key.</param>
        ///
        public PkiArchiveControlBuilder(PrivateKeyInfo privateKeyInfo, GeneralName generalName)
        {
            EncKeyWithID encKeyWithID = new EncKeyWithID(privateKeyInfo, generalName);

            try
            {
                m_keyContent = new CmsProcessableByteArray(CrmfObjectIdentifiers.id_ct_encKeyWithID, encKeyWithID.GetEncoded());
            }
            catch (IOException e)
            {
                throw new InvalidOperationException("unable to encode key and general name info", e);
            }

            m_envGen = new CmsEnvelopedDataGenerator();
        }

        ///<summary>Add a recipient generator to this control.</summary>       
        ///<param name="recipientGen"> recipient generator created for a specific recipient.</param>
        ///<returns>this builder object.</returns>       
        public PkiArchiveControlBuilder AddRecipientGenerator(RecipientInfoGenerator recipientGen)
        {
            m_envGen.AddRecipientInfoGenerator(recipientGen);
            return this;
        }
       
        /// <summary>Build the PKIArchiveControl using the passed in encryptor to encrypt its contents.</summary>
        /// <param name="contentEncryptor">a suitable content encryptor.</param>
        /// <returns>a PKIArchiveControl object.</returns>
        public PkiArchiveControl Build(ICipherBuilderWithKey contentEncryptor)
        {                                            
            CmsEnvelopedData envContent = m_envGen.Generate(m_keyContent, contentEncryptor);
            EnvelopedData envD = EnvelopedData.GetInstance(envContent.ContentInfo.Content);        
            return new PkiArchiveControl(new PkiArchiveOptions(new EncryptedKey(envD)));
        }

        // TODO[crmf]
#if false
        /**
         * Build the PKIArchiveControl using the passed in encryptor to encrypt its contents.
         *
         * @param contentEncryptor a suitable content encryptor.
         * @return a PKIArchiveControl object.
         * @throws CMSException in the event the build fails.
         */
        public PkiArchiveControl Build(OutputEncryptor contentEncryptor)
        {
            CmsEnvelopedData envContent = m_envGen.Generate(m_keyContent, contentEncryptor);
            EnvelopedData envD = EnvelopedData.GetInstance(envContent.ContentInfo.Content);
            return new PkiArchiveControl(new PkiArchiveOptions(new EncryptedKey(envD)));
        }
#endif
    }
}