001: // CMP implementation copyright (c) 2003 NOVOSEC AG (http://www.novosec.com)
002: //
003: // Author: Maik Stohn
004: //
005: // Permission is hereby granted, free of charge, to any person obtaining a copy of this
006: // software and associated documentation files (the "Software"), to deal in the Software
007: // without restriction, including without limitation the rights to use, copy, modify, merge,
008: // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
009: // to whom the Software is furnished to do so, subject to the following conditions:
010: //
011: // The above copyright notice and this permission notice shall be included in all copies or
012: // substantial portions of the Software.
013: //
014: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
015: // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
016: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
017: // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
018: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
019:
020: package com.novosec.pkix.asn1.cmp;
021:
022: import java.util.Enumeration;
023:
024: import org.bouncycastle.asn1.ASN1EncodableVector;
025: import org.bouncycastle.asn1.ASN1Sequence;
026: import org.bouncycastle.asn1.ASN1TaggedObject;
027: import org.bouncycastle.asn1.DEREncodable;
028: import org.bouncycastle.asn1.DERObject;
029: import org.bouncycastle.asn1.DERSequence;
030: import org.bouncycastle.asn1.DERTaggedObject;
031:
032: import com.novosec.pkix.asn1.crmf.EncryptedValue;
033: import com.novosec.pkix.asn1.crmf.PKIPublicationInfo;
034:
035: /**
036: * ASN.1 structure DER En/DeCoder.
037: *
038: * <pre>
039: * CertifiedKeyPair ::= SEQUENCE {
040: * certOrEncCert CertOrEncCert,
041: * privateKey [0] EncryptedValue OPTIONAL,
042: * publicationInfo [1] PKIPublicationInfo OPTIONAL
043: * }
044: *
045: * </pre>
046: */
047: public class CertifiedKeyPair implements DEREncodable {
048: CertOrEncCert certOrEncCert;
049: EncryptedValue privateKey;
050: PKIPublicationInfo publicationInfo;
051:
052: public static CertifiedKeyPair getInstance(ASN1TaggedObject obj,
053: boolean explicit) {
054: return getInstance(ASN1Sequence.getInstance(obj, explicit));
055: }
056:
057: public static CertifiedKeyPair getInstance(Object obj) {
058: if (obj instanceof CertifiedKeyPair) {
059: return (CertifiedKeyPair) obj;
060: } else if (obj instanceof ASN1Sequence) {
061: return new CertifiedKeyPair((ASN1Sequence) obj);
062: }
063:
064: throw new IllegalArgumentException("unknown object in factory");
065: }
066:
067: public CertifiedKeyPair(ASN1Sequence seq) {
068: Enumeration e = seq.getObjects();
069:
070: certOrEncCert = CertOrEncCert.getInstance((DERObject) e
071: .nextElement());
072:
073: while (e.hasMoreElements()) {
074: ASN1TaggedObject tagObj = (ASN1TaggedObject) e
075: .nextElement();
076:
077: switch (tagObj.getTagNo()) {
078: case 0:
079: privateKey = EncryptedValue.getInstance(tagObj
080: .getObject());
081: break;
082: case 1:
083: publicationInfo = PKIPublicationInfo.getInstance(tagObj
084: .getObject());
085: break;
086: }
087: }
088: }
089:
090: public CertifiedKeyPair(CertOrEncCert certOrEncCert) {
091: this .certOrEncCert = certOrEncCert;
092: }
093:
094: public CertOrEncCert getCertOrEncCert() {
095: return certOrEncCert;
096: }
097:
098: public void setPrivateKey(EncryptedValue privateKey) {
099: this .privateKey = privateKey;
100: }
101:
102: public EncryptedValue getPrivateKey() {
103: return privateKey;
104: }
105:
106: public void setPublicationInfo(PKIPublicationInfo publicationInfo) {
107: this .publicationInfo = publicationInfo;
108: }
109:
110: public PKIPublicationInfo getPublicationInfo() {
111: return publicationInfo;
112: }
113:
114: public DERObject getDERObject() {
115: ASN1EncodableVector v = new ASN1EncodableVector();
116:
117: v.add(certOrEncCert);
118:
119: if (privateKey != null)
120: v.add(new DERTaggedObject(true, 0, privateKey));
121:
122: if (publicationInfo != null)
123: v.add(new DERTaggedObject(true, 1, publicationInfo));
124:
125: return new DERSequence(v);
126: }
127:
128: public String toString() {
129: String s = "CertifiedKeyPair: ( certOrEncCert: "
130: + this .getCertOrEncCert() + ", ";
131:
132: if (this .getPrivateKey() != null)
133: s += "privateKey: " + this .getPrivateKey() + ", ";
134:
135: if (this .getPublicationInfo() != null)
136: s += "publicationInfo: " + this .getPublicationInfo() + ", ";
137:
138: s += ")";
139:
140: return s;
141: }
142: }
|