2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / security / cert / TrustAnchor.java
blobccd2a9fadb3dd2a764d02314068e12ba73da5c5d
1 /* TrustAnchor.java -- an ultimately-trusted certificate.
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.security.cert;
41 import java.io.ByteArrayInputStream;
42 import java.io.IOException;
44 import java.security.PublicKey;
46 import gnu.java.security.x509.X500DistinguishedName;
48 /**
49 * An ultimately-trusted certificate to serve as the root of a
50 * certificate chain.
52 * @author Casey Marshall (rsdio@metastatic.org)
54 public class TrustAnchor
57 // Fields.
58 // ------------------------------------------------------------------------
60 /** The certificate authority's distinguished name. */
61 private final X500DistinguishedName caName;
63 /** The certficate authority's public key. */
64 private final PublicKey caKey;
66 /** The certficate authority's certificate. */
67 private final X509Certificate trustedCert;
69 /** The encoded name constraints bytes. */
70 private final byte[] nameConstraints;
72 // Constnuctors.
73 // ------------------------------------------------------------------------
75 /**
76 * Create a new trust anchor from a certificate and (optional) name
77 * constraints.
79 * <p>If the <i>nameConstraints</i> argument in non-null, it will be
80 * copied to prevent modification.
82 * @param trustedCert The trusted certificate.
83 * @param nameConstraints The encoded nameConstraints.
85 public TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints)
87 if (trustedCert == null)
88 throw new NullPointerException();
89 this.trustedCert = trustedCert;
90 caName = null;
91 caKey = null;
92 if (nameConstraints != null)
93 this.nameConstraints = (byte[]) nameConstraints.clone();
94 else
95 this.nameConstraints = null;
98 /**
99 * Create a new trust anchor from a certificate authority's
100 * distinguished name, public key, and (optional) name constraints.
102 * <p>If the <i>nameConstraints</i> argument in non-null, it will be
103 * copied to prevent modification.
105 * @params caName The CA's distinguished name.
106 * @params caKey The CA's public key.
107 * @params nameConstraints The encoded nameConstraints.
109 public TrustAnchor(String caName, PublicKey caKey, byte[] nameConstraints)
111 if (caName == null || caKey == null)
112 throw new NullPointerException();
113 if (caName.length() == 0)
114 throw new IllegalArgumentException();
115 trustedCert = null;
116 this.caName = new X500DistinguishedName(caName);
117 this.caKey = caKey;
118 if (nameConstraints != null)
119 this.nameConstraints = (byte[]) nameConstraints.clone();
120 else
121 this.nameConstraints = null;
124 // Instance methods.
125 // ------------------------------------------------------------------------
128 * Return the trusted certificate, or null if none was specified.
130 * @return The trusted certificate.
132 public final X509Certificate getTrustedCert()
134 return trustedCert;
138 * Return the certificate authority's distinguished name, or null if
139 * none was specified.
141 * @return The CA's distinguished name.
143 public final String getCAName()
145 if (caName != null)
146 return caName.toRFC2253();
147 return null;
151 * Return the certificate authority's public key, or null if none was
152 * specified.
154 * @return The CA's public key.
156 public final PublicKey getCAPublicKey()
158 return caKey;
162 * Return the encoded name constraints, or null if none was specified.
164 * <p>The name constraints byte array is copied when this method is
165 * called to prevent modification.
167 * @return The encoded name constraints.
169 public final byte[] getNameConstraints()
171 if (nameConstraints == null)
172 return null;
173 return (byte[]) nameConstraints.clone();
177 * Return a printable representation of this trust anchor.
179 * @return The printable representation.
181 public String toString()
183 if (trustedCert == null)
184 return "[ Trusted CA Public Key=" + caKey + ", Trusted CA Issuer Name="
185 + caName.toRFC2253() + " ]";
186 return "[ Trusted CA Certificate=" + trustedCert + " ]";