01: package org.bouncycastle.asn1.crmf;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1EncodableVector;
05: import org.bouncycastle.asn1.ASN1Sequence;
06: import org.bouncycastle.asn1.ASN1TaggedObject;
07: import org.bouncycastle.asn1.DERObject;
08: import org.bouncycastle.asn1.DERSequence;
09:
10: import java.util.Enumeration;
11:
12: public class CertReqMsg extends ASN1Encodable {
13: private CertRequest certReq;
14: private ProofOfPossession pop;
15: private ASN1Sequence regInfo;
16:
17: private CertReqMsg(ASN1Sequence seq) {
18: Enumeration en = seq.getObjects();
19:
20: certReq = CertRequest.getInstance(en.nextElement());
21: while (en.hasMoreElements()) {
22: Object o = en.nextElement();
23:
24: if (o instanceof ASN1TaggedObject) {
25: pop = ProofOfPossession.getInstance(o);
26: } else {
27: regInfo = ASN1Sequence.getInstance(o);
28: }
29: }
30: }
31:
32: public static CertReqMsg getInstance(Object o) {
33: if (o instanceof CertReqMsg) {
34: return (CertReqMsg) o;
35: }
36:
37: if (o instanceof ASN1Sequence) {
38: return new CertReqMsg((ASN1Sequence) o);
39: }
40:
41: throw new IllegalArgumentException("Invalid object: "
42: + o.getClass().getName());
43: }
44:
45: public CertRequest getCertReq() {
46: return certReq;
47: }
48:
49: public ProofOfPossession getPop() {
50: return pop;
51: }
52:
53: public AttributeTypeAndValue[] getRegInfo() {
54: if (regInfo == null) {
55: return null;
56: }
57:
58: AttributeTypeAndValue[] results = new AttributeTypeAndValue[regInfo
59: .size()];
60:
61: for (int i = 0; i != results.length; i++) {
62: results[i] = AttributeTypeAndValue.getInstance(regInfo
63: .getObjectAt(i));
64: }
65:
66: return results;
67: }
68:
69: /**
70: * <pre>
71: * CertReqMsg ::= SEQUENCE {
72: * certReq CertRequest,
73: * pop ProofOfPossession OPTIONAL,
74: * -- content depends upon key type
75: * regInfo SEQUENCE SIZE(1..MAX) OF AttributeTypeAndValue OPTIONAL }
76: * </pre>
77: * @return a basic ASN.1 object representation.
78: */
79: public DERObject toASN1Object() {
80: ASN1EncodableVector v = new ASN1EncodableVector();
81:
82: v.add(certReq);
83:
84: addOptional(v, pop);
85: addOptional(v, regInfo);
86:
87: return new DERSequence(v);
88: }
89:
90: private void addOptional(ASN1EncodableVector v, ASN1Encodable obj) {
91: if (obj != null) {
92: v.add(obj);
93: }
94: }
95: }
|