001: package org.bouncycastle.asn1.pkcs;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1Set;
007: import org.bouncycastle.asn1.DERInteger;
008: import org.bouncycastle.asn1.DERObject;
009: import org.bouncycastle.asn1.DERSequence;
010: import org.bouncycastle.asn1.DERTaggedObject;
011: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
012: import org.bouncycastle.asn1.x509.X509Name;
013:
014: /**
015: * PKCS10 CertificationRequestInfo object.
016: * <pre>
017: * CertificationRequestInfo ::= SEQUENCE {
018: * version INTEGER { v1(0) } (v1,...),
019: * subject Name,
020: * subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
021: * attributes [0] Attributes{{ CRIAttributes }}
022: * }
023: *
024: * Attributes { ATTRIBUTE:IOSet } ::= SET OF Attribute{{ IOSet }}
025: *
026: * Attribute { ATTRIBUTE:IOSet } ::= SEQUENCE {
027: * type ATTRIBUTE.&id({IOSet}),
028: * values SET SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{\@type})
029: * }
030: * </pre>
031: */
032: public class CertificationRequestInfo extends ASN1Encodable {
033: DERInteger version = new DERInteger(0);
034: X509Name subject;
035: SubjectPublicKeyInfo subjectPKInfo;
036: ASN1Set attributes = null;
037:
038: public static CertificationRequestInfo getInstance(Object obj) {
039: if (obj instanceof CertificationRequestInfo) {
040: return (CertificationRequestInfo) obj;
041: } else if (obj instanceof ASN1Sequence) {
042: return new CertificationRequestInfo((ASN1Sequence) obj);
043: }
044:
045: throw new IllegalArgumentException("unknown object in factory");
046: }
047:
048: public CertificationRequestInfo(X509Name subject,
049: SubjectPublicKeyInfo pkInfo, ASN1Set attributes) {
050: this .subject = subject;
051: this .subjectPKInfo = pkInfo;
052: this .attributes = attributes;
053:
054: if ((subject == null) || (version == null)
055: || (subjectPKInfo == null)) {
056: throw new IllegalArgumentException(
057: "Not all mandatory fields set in CertificationRequestInfo generator.");
058: }
059: }
060:
061: public CertificationRequestInfo(ASN1Sequence seq) {
062: version = (DERInteger) seq.getObjectAt(0);
063:
064: subject = X509Name.getInstance(seq.getObjectAt(1));
065: subjectPKInfo = SubjectPublicKeyInfo.getInstance(seq
066: .getObjectAt(2));
067:
068: //
069: // some CertificationRequestInfo objects seem to treat this field
070: // as optional.
071: //
072: if (seq.size() > 3) {
073: DERTaggedObject tagobj = (DERTaggedObject) seq
074: .getObjectAt(3);
075: attributes = ASN1Set.getInstance(tagobj, false);
076: }
077:
078: if ((subject == null) || (version == null)
079: || (subjectPKInfo == null)) {
080: throw new IllegalArgumentException(
081: "Not all mandatory fields set in CertificationRequestInfo generator.");
082: }
083: }
084:
085: public DERInteger getVersion() {
086: return version;
087: }
088:
089: public X509Name getSubject() {
090: return subject;
091: }
092:
093: public SubjectPublicKeyInfo getSubjectPublicKeyInfo() {
094: return subjectPKInfo;
095: }
096:
097: public ASN1Set getAttributes() {
098: return attributes;
099: }
100:
101: public DERObject toASN1Object() {
102: ASN1EncodableVector v = new ASN1EncodableVector();
103:
104: v.add(version);
105: v.add(subject);
106: v.add(subjectPKInfo);
107:
108: if (attributes != null) {
109: v.add(new DERTaggedObject(false, 0, attributes));
110: }
111:
112: return new DERSequence(v);
113: }
114: }
|