~ubuntu-branches/ubuntu/natty/icedtea-web/natty-proposed

« back to all changes in this revision

Viewing changes to netx/net/sourceforge/jnlp/security/CertificateUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-11-24 13:23:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101124132328-2xb9z39vxga63vr9
Tags: 1.0~20101124-0ubuntu1
* Update to hg 20101124.
* Fix xulrunner dependencies for natty.
* Build-depend on pkg-config and libgtk2.0-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* CertificateUtils.java
 
2
   Copyright (C) 2010 Red Hat, Inc.
 
3
 
 
4
This file is part of IcedTea.
 
5
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
02110-1301 USA.
 
19
 
 
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
 
23
combination.
 
24
 
 
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.
 
36
*/
 
37
 
 
38
package net.sourceforge.jnlp.security;
 
39
 
 
40
import java.io.BufferedInputStream;
 
41
import java.io.File;
 
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;
 
53
 
 
54
import net.sourceforge.jnlp.runtime.JNLPRuntime;
 
55
 
 
56
import sun.misc.BASE64Encoder;
 
57
import sun.security.provider.X509Factory;
 
58
 
 
59
/**
 
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.
 
63
 */
 
64
public class CertificateUtils {
 
65
 
 
66
    /**
 
67
     * Adds the X509Certficate in the file to the KeyStore. Note that it does
 
68
     * not update the copy of the KeyStore on disk.
 
69
     */
 
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);
 
74
        }
 
75
 
 
76
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
 
77
        CertificateFactory cf = CertificateFactory.getInstance("X509");
 
78
        X509Certificate cert = null;
 
79
 
 
80
        try {
 
81
            cert = (X509Certificate) cf.generateCertificate(bis);
 
82
        } catch (ClassCastException cce) {
 
83
            throw new CertificateException("Input file is not an X509 Certificate", cce);
 
84
        }
 
85
 
 
86
        addToKeyStore(cert, ks);
 
87
    }
 
88
 
 
89
    /**
 
90
     * Adds an X509Certificate to the KeyStore. Note that it does not update the
 
91
     * copy of the KeyStore on disk.
 
92
     */
 
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());
 
97
        }
 
98
 
 
99
        String alias = null;
 
100
 
 
101
        // does this certificate already exist?
 
102
        alias = ks.getCertificateAlias(cert);
 
103
        if (alias != null) {
 
104
            return;
 
105
        }
 
106
 
 
107
        // create a unique alias for this new certificate
 
108
        Random random = new Random();
 
109
        do {
 
110
            alias = new BigInteger(20, random).toString();
 
111
        } while (ks.getCertificate(alias) != null);
 
112
 
 
113
        ks.setCertificateEntry(alias, cert);
 
114
    }
 
115
 
 
116
    /**
 
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
 
121
     */
 
122
    public static final boolean inKeyStores(X509Certificate c, KeyStore[] keyStores) {
 
123
        for (int i = 0; i < keyStores.length; i++) {
 
124
            try {
 
125
                if (keyStores[i].getCertificateAlias(c) != null) {
 
126
                    if (JNLPRuntime.isDebug()) {
 
127
                        System.out.println(c.getSubjectX500Principal().getName() + " found in cacerts");
 
128
                    }
 
129
                    return true;
 
130
                }
 
131
            } catch (KeyStoreException e) {
 
132
                e.printStackTrace();
 
133
                // continue
 
134
            }
 
135
        }
 
136
        return false;
 
137
    }
 
138
 
 
139
    /**
 
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
 
142
     */
 
143
    public static void dump(Certificate cert, PrintStream out) throws IOException,
 
144
            CertificateException {
 
145
 
 
146
        BASE64Encoder encoder = new BASE64Encoder();
 
147
        out.println(X509Factory.BEGIN_CERT);
 
148
        encoder.encodeBuffer(cert.getEncoded(), out);
 
149
        out.println(X509Factory.END_CERT);
 
150
    }
 
151
}