001: package org.bouncycastle.x509;
002:
003: import org.bouncycastle.asn1.ASN1InputStream;
004: import org.bouncycastle.asn1.x509.CertificatePair;
005: import org.bouncycastle.asn1.x509.X509CertificateStructure;
006: import org.bouncycastle.jce.provider.X509CertificateObject;
007:
008: import java.io.IOException;
009: import java.security.cert.CertificateEncodingException;
010: import java.security.cert.CertificateParsingException;
011: import java.security.cert.X509Certificate;
012:
013: /**
014: * This class contains a cross certificate pair. Cross certificates pairs may
015: * contain two cross signed certificates from two CAs. A certificate from the
016: * other CA to this CA is contained in the forward certificate, the certificate
017: * from this CA to the other CA is contained in the reverse certificate.
018: */
019: public class X509CertificatePair {
020: private X509Certificate forward;
021: private X509Certificate reverse;
022:
023: /**
024: * Constructor.
025: *
026: * @param forward Certificate from the other CA to this CA.
027: * @param reverse Certificate from this CA to the other CA.
028: */
029: public X509CertificatePair(X509Certificate forward,
030: X509Certificate reverse) {
031: this .forward = forward;
032: this .reverse = reverse;
033: }
034:
035: /**
036: * Constructor from a ASN.1 CertificatePair structure.
037: *
038: * @param pair The <code>CertificatePair</code> ASN.1 object.
039: */
040: public X509CertificatePair(CertificatePair pair)
041: throws CertificateParsingException {
042: if (pair.getForward() != null) {
043: this .forward = new X509CertificateObject(pair.getForward());
044: }
045: if (pair.getReverse() != null) {
046: this .reverse = new X509CertificateObject(pair.getReverse());
047: }
048: }
049:
050: public byte[] getEncoded() throws CertificateEncodingException {
051: X509CertificateStructure f = null;
052: X509CertificateStructure r = null;
053: try {
054: if (forward != null) {
055: f = X509CertificateStructure
056: .getInstance(new ASN1InputStream(forward
057: .getEncoded()).readObject());
058: }
059: if (reverse != null) {
060: r = X509CertificateStructure
061: .getInstance(new ASN1InputStream(reverse
062: .getEncoded()).readObject());
063: }
064: return new CertificatePair(f, r).getDEREncoded();
065: } catch (IllegalArgumentException e) {
066: throw new ExtCertificateEncodingException(e.toString(), e);
067: } catch (IOException e) {
068: throw new ExtCertificateEncodingException(e.toString(), e);
069: }
070: }
071:
072: /**
073: * Returns the certificate from the other CA to this CA.
074: *
075: * @return Returns the forward certificate.
076: */
077: public X509Certificate getForward() {
078: return forward;
079: }
080:
081: /**
082: * Return the certificate from this CA to the other CA.
083: *
084: * @return Returns the reverse certificate.
085: */
086: public X509Certificate getReverse() {
087: return reverse;
088: }
089:
090: public boolean equals(Object o) {
091: if (o == null) {
092: return false;
093: }
094: if (!(o instanceof X509CertificatePair)) {
095: return false;
096: }
097: X509CertificatePair pair = (X509CertificatePair) o;
098: boolean equalReverse = true;
099: boolean equalForward = true;
100: if (forward != null) {
101: equalForward = this .forward.equals(pair.forward);
102: } else {
103: if (pair.forward != null) {
104: equalForward = false;
105: }
106: }
107: if (reverse != null) {
108: equalReverse = this .reverse.equals(pair.reverse);
109: } else {
110: if (pair.reverse != null) {
111: equalReverse = false;
112: }
113: }
114: return equalForward && equalReverse;
115: }
116:
117: public int hashCode() {
118: int hash = 0;
119: if (forward != null) {
120: hash += forward.hashCode();
121: }
122: if (reverse != null) {
123: hash += reverse.hashCode();
124: }
125:
126: return hash;
127: }
128: }
|