summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/cert/CertificatePair.java
blob: effd86eddeefe717ae449a8552866226601e08b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmscore.cert;


import java.io.IOException;
import java.io.OutputStream;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import org.mozilla.jss.asn1.ANY;
import org.mozilla.jss.asn1.ASN1Value;
import org.mozilla.jss.asn1.InvalidBERException;
import org.mozilla.jss.asn1.SEQUENCE;
import org.mozilla.jss.asn1.Tag;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.ca.ICertificateAuthority;
import com.netscape.certsrv.cert.ICrossCertPairSubsystem;


/**
 * This class implements CertificatePair used for Cross Certification
 *
 * @author cfu
 * @version $Revision$, $Date$
 */
public class CertificatePair implements ASN1Value {
    private byte[] mForward; // cert cross-siged by another CA
    private byte[] mReverse; // subordinate cert signed by this CA
    private static final Tag TAG = SEQUENCE.TAG;

    /**
     * construct a CertificatePair.  It doesn't matter which is
     * forward and which is reverse in the parameters.  It will figure 
     * it out
     * @param cert1 one X509Certificate
     * @param cert2 one X509Certificate
     */
    public CertificatePair (X509Certificate cert1, X509Certificate cert2)
        throws EBaseException {
        if ((cert1 == null) || (cert2 == null))
            throw new EBaseException("CertificatePair: both certs can not be null");
        debug("in CertificatePair()");
        boolean rightOrder = certOrders(cert1, cert2);

        try {
            if (rightOrder == false) {
                mForward = cert2.getEncoded();
                mReverse = cert1.getEncoded();
            } else {
                mForward = cert1.getEncoded();
                mReverse = cert2.getEncoded();
            }
        } catch (CertificateException e) {
            throw new EBaseException("CertificatePair: constructor failed:" + e.toString());
        }
    }

    /**
     * construct a CertificatePair.  It doesn't matter which is
     * forward and which is reverse in the parameters.  It will figure 
     * it out
     * @param cert1 one certificate byte array
     * @param cert2 one certificate byte array
     */
    public CertificatePair (byte[] cert1, byte[] cert2)
        throws EBaseException {
        if ((cert1 == null) || (cert2 == null))
            throw new EBaseException("CertificatePair: both certs can not be null");
        boolean rightOrder = certOrders(cert1, cert2);

        if (rightOrder == false) {
            mForward = cert2;
            mReverse = cert1;
        } else {
            mForward = cert1;
            mReverse = cert2;
        }
    }

    /*
     * returns true if c1 is forward and cert2 is reverse
     * returns false if c2 is forward and cert1 is reverse
     */
    private boolean certOrders(X509Certificate c1, X509Certificate c2)
        throws EBaseException {
        debug("in certOrders() with X509Cert");

        ICertificateAuthority ca = (ICertificateAuthority) CMS.getSubsystem("ca");
        X509Certificate caCert = (X509Certificate) ca.getCACert();

        debug("got this caCert");
        // reverse cert is the one signed by this ca
        // more check really should be done here regarding the
        // validity of the two certs...later

        /* It looks the DN's returned are not normalized and fail
         * comparison

         if ((c1.getIssuerDN().equals((Object) caCert.getSubjectDN()))) 
         debug("myCA signed c1");
         else {
         debug("c1 issuerDN="+c1.getIssuerDN().toString());
         debug("myCA subjectDN="+caCert.getSubjectDN().toString());
         }

         if(caCert.getSubjectDN().equals((Object) c2.getSubjectDN()))
         debug("myCA subject == c2 subject");
         else {
         debug("caCert subjectDN="+caCert.getSubjectDN().toString());
         debug("c2 subjectDN="+c2.getSubjectDN().toString());
         }

         if ((c2.getIssuerDN().equals((Object) caCert.getSubjectDN())))
         debug("myCA signed c2");
         else {
         debug("c2 issuerDN="+c1.getIssuerDN().toString());
         debug("myCA subjectDN="+caCert.getSubjectDN().toString());
         }

         if(caCert.getSubjectDN().equals((Object) c1.getSubjectDN()))
         debug("myCA subject == c1 subject");
         else {
         debug("caCert subjectDN="+caCert.getSubjectDN().toString());
         debug("c1 subjectDN="+c1.getSubjectDN().toString());
         }

         if ((c1.getIssuerDN().equals((Object) caCert.getSubjectDN()))
         && (caCert.getSubjectDN().equals((Object) c2.getSubjectDN())))
         
         {
         return false;
         } else if ((c2.getIssuerDN().equals((Object) caCert.getSubjectDN()))
         && (caCert.getSubjectDN().equals((Object) c1.getSubjectDN())))
         {
         return true;
         } else {
         throw new EBaseException("CertificatePair: need correct forward and reverse relationship to construct CertificatePair");
         }
         */

        /*
         * my other attempt:
         * one of the certs has to share the same public key as this
         * CA, and that will be the "forward" cert; the other one is
         * assumed to be the "reverse" cert
         */
        byte[] caCertBytes = caCert.getPublicKey().getEncoded();

        if (caCertBytes != null)
            debug("got cacert public key bytes length=" + caCertBytes.length);
        else {
            debug("cacert public key bytes null");
            throw new EBaseException("CertificatePair: certOrders() fails to get this CA's signing certificate public key encoded");
        }

        byte[] c1Bytes = c1.getPublicKey().getEncoded();

        if (c1Bytes != null)
            debug("got c1 public key bytes length=" + c1Bytes.length);
        else {
            debug("c1 cert public key bytes length null");
            throw new EBaseException("CertificatePair::certOrders() public key bytes are of length null");
        }

        byte[] c2Bytes = c2.getPublicKey().getEncoded();

        if (c2Bytes != null)
            debug("got c2 public key bytes length=" + c2Bytes.length);
        else
            debug("c2 cert public key bytes length null");

        if (byteArraysAreEqual(c1Bytes, caCertBytes)) {
            debug("c1 has same public key as this ca");
            return true;
        } else if (byteArraysAreEqual(c2Bytes, caCertBytes)) {
            debug("c2 has same public key as this ca");

            return false;
        } else {
            debug("neither c1 nor c2 public key matches with this ca");
            throw new EBaseException("CertificatePair: need correct forward and reverse relationship to construct CertificatePair");
        }
    }

    /**
     * compares contents two byte arrays returning true if exactly same.
     */
    public boolean byteArraysAreEqual(byte[] a, byte[] b) {
        debug("in byteArraysAreEqual()");
        if (a.length != b.length) {
            debug("exiting byteArraysAreEqual(): false");
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i]) {
                debug("exiting byteArraysAreEqual(): false");
                return false;
            }
        }
        debug("exiting byteArraysAreEqual(): true");
        return true;
    }

    /*
     * returns true if cert1 is forward and cert2 is reverse
     * returns false if cert2 is forward and cert1 is reverse
     */
    private boolean certOrders(byte[] cert1, byte[] cert2)
        throws EBaseException {
        debug("in certOrders() with byte[]");
        ICrossCertPairSubsystem ccps =
            (ICrossCertPairSubsystem) CMS.getSubsystem("CrossCertPair");
        X509Certificate c1 = null;
        X509Certificate c2 = null;

        try {
            c1 = ccps.byteArray2X509Cert(cert1);
            c2 = ccps.byteArray2X509Cert(cert2);
        } catch (CertificateException e) {
            throw new EBaseException("CertificatePair: certOrders() failed:" + e.toString());
        }
        return certOrders(c1, c2);
    }

    public void encode(OutputStream os) throws IOException {
        encode(TAG, os);
    }

    public void encode(Tag implicitTag, OutputStream os) throws IOException {
        SEQUENCE seq = new SEQUENCE();

        if (mForward != null) {
            try {
                ANY any = new ANY(mForward);

                seq.addElement(any);
            } catch (InvalidBERException e) {
                debug("encode error:" + e.toString());
            }
        }
        if (mReverse != null) {
            try {
                ANY any = new ANY(mReverse);

                seq.addElement(any);
            } catch (InvalidBERException e) {
                debug("encode error:" + e.toString());
            }
        }

        seq.encode(implicitTag, os);
    }

    public Tag getTag() {
        return TAG;
    }

    private void debug(String msg) {
        CMS.debug("CertifiatePair: " + msg);
    }
}