1
/* CertificateUtils.java
2
Copyright (C) 2010 Red Hat, Inc.
4
This file is part of IcedTea.
6
IcedTea is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License as published by
8
the Free Software Foundation, version 2.
10
IcedTea is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with IcedTea; see the file COPYING. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
Linking this library statically or dynamically with other modules is
21
making a combined work based on this library. Thus, the terms and
22
conditions of the GNU General Public License cover the whole
25
As a special exception, the copyright holders of this library give you
26
permission to link this library with independent modules to produce an
27
executable, regardless of the license terms of these independent
28
modules, and to copy and distribute the resulting executable under
29
terms of your choice, provided that you also meet, for each linked
30
independent module, the terms and conditions of the license of that
31
module. An independent module is a module which is not derived from
32
or based on this library. If you modify this library, you may extend
33
this exception to your version of the library, but you are not
34
obligated to do so. If you do not wish to do so, delete this
35
exception statement from your version.
38
package net.sourceforge.jnlp.security;
40
import java.io.BufferedInputStream;
42
import java.io.FileInputStream;
43
import java.io.IOException;
44
import java.io.PrintStream;
45
import java.math.BigInteger;
46
import java.security.KeyStore;
47
import java.security.KeyStoreException;
48
import java.security.cert.Certificate;
49
import java.security.cert.CertificateException;
50
import java.security.cert.CertificateFactory;
51
import java.security.cert.X509Certificate;
52
import java.util.Random;
54
import net.sourceforge.jnlp.runtime.JNLPRuntime;
56
import sun.misc.BASE64Encoder;
57
import sun.security.provider.X509Factory;
60
* Common utilities to manipulate certificates. Provides methods to add
61
* Certificates to a KeyStores, check if certificates already exist in a
62
* KeyStore and printing certificates.
64
public class CertificateUtils {
67
* Adds the X509Certficate in the file to the KeyStore. Note that it does
68
* not update the copy of the KeyStore on disk.
70
public static final void addToKeyStore(File file, KeyStore ks) throws CertificateException,
71
IOException, KeyStoreException {
72
if (JNLPRuntime.isDebug()) {
73
System.out.println("Importing certificate from " + file + " into " + ks);
76
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
77
CertificateFactory cf = CertificateFactory.getInstance("X509");
78
X509Certificate cert = null;
81
cert = (X509Certificate) cf.generateCertificate(bis);
82
} catch (ClassCastException cce) {
83
throw new CertificateException("Input file is not an X509 Certificate", cce);
86
addToKeyStore(cert, ks);
90
* Adds an X509Certificate to the KeyStore. Note that it does not update the
91
* copy of the KeyStore on disk.
93
public static final void addToKeyStore(X509Certificate cert, KeyStore ks)
94
throws KeyStoreException {
95
if (JNLPRuntime.isDebug()) {
96
System.out.println("Importing " + cert.getSubjectX500Principal().getName());
101
// does this certificate already exist?
102
alias = ks.getCertificateAlias(cert);
107
// create a unique alias for this new certificate
108
Random random = new Random();
110
alias = new BigInteger(20, random).toString();
111
} while (ks.getCertificate(alias) != null);
113
ks.setCertificateEntry(alias, cert);
117
* Checks whether an X509Certificate is already in one of the keystores
118
* @param c the certificate
119
* @param keyStores the KeyStores to check in
120
* @return true if the certificate is present in one of the keystores, false otherwise
122
public static final boolean inKeyStores(X509Certificate c, KeyStore[] keyStores) {
123
for (int i = 0; i < keyStores.length; i++) {
125
if (keyStores[i].getCertificateAlias(c) != null) {
126
if (JNLPRuntime.isDebug()) {
127
System.out.println(c.getSubjectX500Principal().getName() + " found in cacerts");
131
} catch (KeyStoreException e) {
140
* Writes the certificate in base64 encoded from to the print stream.
141
* See http://tools.ietf.org/html/rfc4945#section-6.1 for more information
143
public static void dump(Certificate cert, PrintStream out) throws IOException,
144
CertificateException {
146
BASE64Encoder encoder = new BASE64Encoder();
147
out.println(X509Factory.BEGIN_CERT);
148
encoder.encodeBuffer(cert.getEncoded(), out);
149
out.println(X509Factory.END_CERT);