~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/cert/crmf/ProofOfPossessionSigningKeyBuilder.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.cert.crmf;
 
2
 
 
3
import org.bouncycastle.asn1.DERBitString;
 
4
import org.bouncycastle.asn1.crmf.PKMACValue;
 
5
import org.bouncycastle.asn1.crmf.POPOSigningKey;
 
6
import org.bouncycastle.asn1.crmf.POPOSigningKeyInput;
 
7
import org.bouncycastle.asn1.x509.GeneralName;
 
8
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
 
9
import org.bouncycastle.operator.ContentSigner;
 
10
 
 
11
public class ProofOfPossessionSigningKeyBuilder
 
12
{
 
13
    private SubjectPublicKeyInfo pubKeyInfo;
 
14
    private GeneralName name;
 
15
    private PKMACValue publicKeyMAC;
 
16
 
 
17
    public ProofOfPossessionSigningKeyBuilder(SubjectPublicKeyInfo pubKeyInfo)
 
18
    {
 
19
        this.pubKeyInfo = pubKeyInfo;
 
20
    }
 
21
 
 
22
    public ProofOfPossessionSigningKeyBuilder setSender(GeneralName name)
 
23
    {
 
24
        this.name = name;
 
25
 
 
26
        return this;
 
27
    }
 
28
 
 
29
    public ProofOfPossessionSigningKeyBuilder setPublicKeyMac(PKMACValueGenerator generator, char[] password)
 
30
        throws CRMFException
 
31
    {
 
32
        this.publicKeyMAC = generator.generate(password, pubKeyInfo);
 
33
 
 
34
        return this;
 
35
    }
 
36
 
 
37
    public POPOSigningKey build(ContentSigner signer)
 
38
    {
 
39
        if (name != null && publicKeyMAC != null)
 
40
        {
 
41
            throw new IllegalStateException("name and publicKeyMAC cannot both be set.");
 
42
        }
 
43
 
 
44
        POPOSigningKeyInput popo;
 
45
 
 
46
        if (name != null)
 
47
        {
 
48
            popo = new POPOSigningKeyInput(name, pubKeyInfo);
 
49
        }
 
50
        else
 
51
        {
 
52
            popo = new POPOSigningKeyInput(publicKeyMAC, pubKeyInfo);
 
53
        }
 
54
 
 
55
        CRMFUtil.derEncodeToStream(popo, signer.getOutputStream());
 
56
 
 
57
        return new POPOSigningKey(popo, signer.getAlgorithmIdentifier(), new DERBitString(signer.getSignature()));
 
58
    }
 
59
}