01: package org.bouncycastle.tsp;
02:
03: import java.io.IOException;
04: import java.math.BigInteger;
05: import java.util.Hashtable;
06: import java.util.Vector;
07:
08: import org.bouncycastle.asn1.x509.X509Extension;
09: import org.bouncycastle.asn1.x509.X509Extensions;
10: import org.bouncycastle.asn1.tsp.MessageImprint;
11: import org.bouncycastle.asn1.tsp.TimeStampReq;
12: import org.bouncycastle.asn1.ASN1Encodable;
13: import org.bouncycastle.asn1.DERObjectIdentifier;
14: import org.bouncycastle.asn1.DERBoolean;
15: import org.bouncycastle.asn1.DERInteger;
16: import org.bouncycastle.asn1.DERNull;
17: import org.bouncycastle.asn1.DEROctetString;
18: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
19:
20: /**
21: * Generator for RFC 3161 Time Stamp Request objects.
22: */
23: public class TimeStampRequestGenerator {
24: private DERObjectIdentifier reqPolicy;
25:
26: private DERBoolean certReq;
27:
28: private Hashtable extensions = new Hashtable();
29: private Vector extOrdering = new Vector();
30:
31: public TimeStampRequestGenerator() {
32: }
33:
34: public void setReqPolicy(String reqPolicy) {
35: this .reqPolicy = new DERObjectIdentifier(reqPolicy);
36: }
37:
38: public void setCertReq(boolean certReq) {
39: this .certReq = new DERBoolean(certReq);
40: }
41:
42: /**
43: * add a given extension field for the standard extensions tag (tag 3)
44: * @throws IOException
45: */
46: public void addExtension(String OID, boolean critical,
47: ASN1Encodable value) throws IOException {
48: this .addExtension(OID, critical, value.getEncoded());
49: }
50:
51: /**
52: * add a given extension field for the standard extensions tag
53: * The value parameter becomes the contents of the octet string associated
54: * with the extension.
55: */
56: public void addExtension(String OID, boolean critical, byte[] value) {
57: DERObjectIdentifier oid = new DERObjectIdentifier(OID);
58: extensions.put(oid, new X509Extension(critical,
59: new DEROctetString(value)));
60: extOrdering.addElement(oid);
61: }
62:
63: public TimeStampRequest generate(String digestAlgorithm,
64: byte[] digest) {
65: return this .generate(digestAlgorithm, digest, null);
66: }
67:
68: public TimeStampRequest generate(String digestAlgorithmOID,
69: byte[] digest, BigInteger nonce) {
70: if (digestAlgorithmOID == null) {
71: throw new IllegalArgumentException(
72: "No digest algorithm specified");
73: }
74:
75: DERObjectIdentifier digestAlgOID = new DERObjectIdentifier(
76: digestAlgorithmOID);
77:
78: AlgorithmIdentifier algID = new AlgorithmIdentifier(
79: digestAlgOID, new DERNull());
80: MessageImprint messageImprint = new MessageImprint(algID,
81: digest);
82:
83: X509Extensions ext = null;
84:
85: if (extOrdering.size() != 0) {
86: ext = new X509Extensions(extOrdering, extensions);
87: }
88:
89: if (nonce != null) {
90: return new TimeStampRequest(new TimeStampReq(
91: messageImprint, reqPolicy, new DERInteger(nonce),
92: certReq, ext));
93: } else {
94: return new TimeStampRequest(new TimeStampReq(
95: messageImprint, reqPolicy, null, certReq, ext));
96: }
97: }
98: }
|