001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.security.pkcs10;
019:
020: import java.util.List;
021:
022: import javax.security.auth.x500.X500Principal;
023:
024: import org.apache.harmony.security.asn1.ASN1Implicit;
025: import org.apache.harmony.security.asn1.ASN1Integer;
026: import org.apache.harmony.security.asn1.ASN1Sequence;
027: import org.apache.harmony.security.asn1.ASN1SetOf;
028: import org.apache.harmony.security.asn1.ASN1Type;
029: import org.apache.harmony.security.asn1.BerInputStream;
030: import org.apache.harmony.security.x501.AttributeTypeAndValue;
031: import org.apache.harmony.security.x501.Name;
032: import org.apache.harmony.security.x509.SubjectPublicKeyInfo;
033:
034: /**
035: CertificationRequestInfo ::= SEQUENCE {
036: version Version,
037: subject Name,
038: subjectPublicKeyInfo SubjectPublicKeyInfo,
039: attributes [0] IMPLICIT Attributes }
040:
041: Version ::= INTEGER
042:
043: Attributes ::= SET OF Attribute
044: */
045:
046: public class CertificationRequestInfo {
047: // version
048: private int version;
049:
050: // the value of subject field of the structure
051: private Name subject;
052:
053: // the value of subjectPublicKeyInfo field of the structure
054: private SubjectPublicKeyInfo subjectPublicKeyInfo;
055:
056: // the value of attributes field of the structure
057: private List attributes;
058:
059: // the ASN.1 encoded form of CertificationRequestInfo
060: private byte[] encoding;
061:
062: public CertificationRequestInfo(int version, Name subject,
063: SubjectPublicKeyInfo subjectPublicKeyInfo, List attributes) {
064: this .version = version;
065: this .subject = subject;
066: this .subjectPublicKeyInfo = subjectPublicKeyInfo;
067: this .attributes = attributes;
068: }
069:
070: // private constructor with encoding given
071: private CertificationRequestInfo(int version, Name subject,
072: SubjectPublicKeyInfo subjectPublicKeyInfo, List attributes,
073: byte[] encoding) {
074: this (version, subject, subjectPublicKeyInfo, attributes);
075: this .encoding = encoding;
076: }
077:
078: /**
079: * @return Returns the attributes.
080: */
081: public List getAttributes() {
082: return attributes;
083: }
084:
085: /**
086: * @return Returns the subject.
087: */
088: public Name getSubject() {
089: return subject;
090: }
091:
092: /**
093: * @return Returns the subjectPublicKeyInfo.
094: */
095: public SubjectPublicKeyInfo getSubjectPublicKeyInfo() {
096: return subjectPublicKeyInfo;
097: }
098:
099: /**
100: * @return Returns the version.
101: */
102: public int getVersion() {
103: return version;
104: }
105:
106: /**
107: * Returns ASN.1 encoded form of this CertificationRequestInfo.
108: * @return a byte array containing ASN.1 encode form.
109: */
110: public byte[] getEncoded() {
111: if (encoding == null) {
112: encoding = ASN1.encode(this );
113: }
114: return encoding;
115: }
116:
117: public String toString() {
118: StringBuffer res = new StringBuffer();
119: res.append("-- CertificationRequestInfo:"); //$NON-NLS-1$
120: res.append("\n version: "); //$NON-NLS-1$
121: res.append(version);
122: res.append("\n subject: "); //$NON-NLS-1$
123: res.append(subject.getName(X500Principal.CANONICAL));
124: res.append("\n subjectPublicKeyInfo: "); //$NON-NLS-1$
125: res.append("\n\t algorithm: " //$NON-NLS-1$
126: + subjectPublicKeyInfo.getAlgorithmIdentifier()
127: .getAlgorithm());
128: res
129: .append("\n\t public key: " + subjectPublicKeyInfo.getPublicKey()); //$NON-NLS-1$
130: res.append("\n attributes: "); //$NON-NLS-1$
131: if (attributes != null) {
132: res.append(attributes.toString());
133: } else {
134: res.append("none"); //$NON-NLS-1$
135: }
136: res.append("\n-- CertificationRequestInfo End\n"); //$NON-NLS-1$
137: return res.toString();
138: }
139:
140: public static final ASN1Sequence ASN1 = new ASN1Sequence(
141: new ASN1Type[] {
142: ASN1Integer.getInstance(), // version
143: Name.ASN1, // subject
144: SubjectPublicKeyInfo.ASN1, // subjectPublicKeyInfo
145: new ASN1Implicit(0, new ASN1SetOf(
146: AttributeTypeAndValue.ASN1)) // attributes
147: }) {
148:
149: protected Object getDecodedObject(BerInputStream in) {
150: Object[] values = (Object[]) in.content;
151: return new CertificationRequestInfo(ASN1Integer
152: .toIntValue(values[0]), (Name) values[1],
153: (SubjectPublicKeyInfo) values[2], (List) values[3],
154: in.getEncoded());
155: }
156:
157: protected void getValues(Object object, Object[] values) {
158: CertificationRequestInfo certReqInfo = (CertificationRequestInfo) object;
159:
160: values[0] = ASN1Integer.fromIntValue(certReqInfo.version);
161: values[1] = certReqInfo.subject;
162: values[2] = certReqInfo.subjectPublicKeyInfo;
163: values[3] = certReqInfo.attributes;
164: }
165: };
166:
167: }
|