summaryrefslogtreecommitdiffstats
path: root/pki/base/util/src/netscape/security/x509/CertificateChain.java
blob: c86b6dbc0b10a9ff784e8b45ca3dc82ff47f6101 (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
// --- 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 netscape.security.x509;
import java.security.cert.*;
import netscape.security.pkcs.*;
import java.io.*;

public class CertificateChain implements Serializable
{
	public CertificateChain() { }

	/**
	 * constructs a certificate chain from a certificate.
	 * @param cert a certificate
	 */
	public CertificateChain(X509Certificate cert)
	{
		mChain = new X509Certificate[1];
		mChain[0] = cert;
	}

	/**
	 * constructs a certificate chain from a X509 certificate array.
	 * @param chain a certificate array.
	 */
	public CertificateChain(X509Certificate[] chain)
	{
		mChain = (X509Certificate[])chain.clone();
	}

	/**
	 * returns the certificate at specified index in chain.
	 * @param index the index.
	 * @return the X509 certificate at the given index.
	 */
	public X509Certificate getCertificate(int index)
	{
		return mChain[index];
	}

	/**
	 * returns the first certificate in chain.
	 * @return the X509 certificate at the given index.
	 */
	public X509Certificate getFirstCertificate()
	{
		return mChain[0];
	}

	/**
	 * returns the certificate chain as an array of X509 certificates.
	 * @return an array of X509 Certificates.
	 */
	public X509Certificate[] getChain()
	{
		return (X509Certificate[])mChain.clone();
	}

	public void encode(OutputStream out) 
		throws IOException 
	{
		encode(out, true);
	}

	/**
	 * encode in PKCS7 blob.
	 */
	public void encode(OutputStream out, boolean sort) 
		throws IOException
	{
		PKCS7 p7 = new PKCS7(new AlgorithmId[0],
                             new ContentInfo(new byte[0]), mChain,
                             new SignerInfo[0]);
		p7.encodeSignedData(out, sort);
	}

	/**
	 * decode from PKCS7 blob.
	 */
	public void decode(InputStream in) 
		throws IOException
	{
		PKCS7 p7 = new PKCS7(in); 
		mChain = p7.getCertificates();
	}

	/**
	 * for serialization
	 */
	private void writeObject(java.io.ObjectOutputStream out)
		throws IOException
	{
		encode(out);
	}

	/**
	 * for serialization
	 */
	private void readObject(java.io.ObjectInputStream in)
		throws IOException
	{
		decode(in);
	}

	/**
	 * Converts the certificate chain to a readable string.
	 */
	public String toString() {
		String s = "[\n";
		if (mChain == null) 
			return "[empty]";
		for (int i = 0; i < mChain.length; i++) {
			s += mChain[i].toString();
		}
		s += "]\n";
		return s;
	}

	private X509Certificate[] mChain = null;
}