1 7 8 package java.security.cert; 9 10 import java.io.IOException ; 11 import java.security.PublicKey ; 12 13 import javax.security.auth.x500.X500Principal ; 14 15 import sun.security.x509.NameConstraintsExtension; 16 import sun.security.x509.X500Name; 17 18 47 public class TrustAnchor { 48 49 static { 50 CertPathHelperImpl.initialize(); 51 } 52 53 private final PublicKey pubKey; 54 private final String caName; 55 private final X500Principal caPrincipal; 56 private final X509Certificate trustedCert; 57 private byte[] ncBytes; 58 private NameConstraintsExtension nc; 59 60 110 public TrustAnchor(X509Certificate trustedCert, byte[] nameConstraints) 111 { 112 if (trustedCert == null) 113 throw new NullPointerException ("the trustedCert parameter must " + 114 "be non-null"); 115 this.trustedCert = trustedCert; 116 this.pubKey = null; 117 this.caName = null; 118 this.caPrincipal = null; 119 setNameConstraints(nameConstraints); 120 } 121 122 149 public TrustAnchor(X500Principal caPrincipal, PublicKey pubKey, 150 byte[] nameConstraints) { 151 if ((caPrincipal == null) || (pubKey == null)) { 152 throw new NullPointerException (); 153 } 154 this.trustedCert = null; 155 this.caPrincipal = caPrincipal; 156 this.caName = caPrincipal.getName(); 157 this.pubKey = pubKey; 158 setNameConstraints(nameConstraints); 159 } 160 161 191 public TrustAnchor(String caName, PublicKey pubKey, byte[] nameConstraints) 192 { 193 if (pubKey == null) 194 throw new NullPointerException ("the pubKey parameter must be " + 195 "non-null"); 196 if (caName == null) 197 throw new NullPointerException ("the caName parameter must be " + 198 "non-null"); 199 if (caName.length() == 0) 200 throw new IllegalArgumentException ("the caName " + 201 "parameter must be a non-empty String"); 202 this.caPrincipal = new X500Principal (caName); 204 this.pubKey = pubKey; 205 this.caName = caName; 206 this.trustedCert = null; 207 setNameConstraints(nameConstraints); 208 } 209 210 216 public final X509Certificate getTrustedCert() { 217 return this.trustedCert; 218 } 219 220 228 public final X500Principal getCA() { 229 return this.caPrincipal; 230 } 231 232 240 public final String getCAName() { 241 return this.caName; 242 } 243 244 251 public final PublicKey getCAPublicKey() { 252 return this.pubKey; 253 } 254 255 258 private void setNameConstraints(byte[] bytes) { 259 if (bytes == null) { 260 ncBytes = null; 261 nc = null; 262 } else { 263 ncBytes = (byte []) bytes.clone(); 264 try { 266 nc = new NameConstraintsExtension(Boolean.FALSE, bytes); 267 } catch (IOException ioe) { 268 IllegalArgumentException iae = 269 new IllegalArgumentException (ioe.getMessage()); 270 iae.initCause(ioe); 271 throw iae; 272 } 273 } 274 } 275 276 296 public final byte [] getNameConstraints() { 297 return (ncBytes == null ? null : (byte []) ncBytes.clone()); 298 } 299 300 305 public String toString() { 306 StringBuffer sb = new StringBuffer (); 307 sb.append("[\n"); 308 if (pubKey != null) { 309 sb.append(" Trusted CA Public Key: " + pubKey.toString() + "\n"); 310 sb.append(" Trusted CA Issuer Name: " 311 + String.valueOf(caName) + "\n"); 312 } else { 313 sb.append(" Trusted CA cert: " + trustedCert.toString() + "\n"); 314 } 315 if (nc != null) 316 sb.append(" Name Constraints: " + nc.toString() + "\n"); 317 return sb.toString(); 318 } 319 } 320 | Popular Tags |