1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.security.auth.x500;
  27 
  28 import java.security.PrivateKey;
  29 import java.security.cert.X509Certificate;
  30 import javax.security.auth.Destroyable;
  31 
  32 /**
  33  * <p> This class represents an {@code X500PrivateCredential}.
  34  * It associates an X.509 certificate, corresponding private key and the
  35  * KeyStore alias used to reference that exact key pair in the KeyStore.
  36  * This enables looking up the private credentials for an X.500 principal
  37  * in a subject.
  38  *
  39  * @since 1.4
  40  */
  41 public final class X500PrivateCredential implements Destroyable {
  42     private X509Certificate cert;
  43     private PrivateKey key;
  44     private String alias;
  45 
  46     /**
  47      * Creates an X500PrivateCredential that associates an X.509 certificate,
  48      * a private key and the KeyStore alias.
  49      *
  50      * @param cert X509Certificate
  51      * @param key  PrivateKey for the certificate
  52      * @exception IllegalArgumentException if either {@code cert} or
  53      * {@code key} is null
  54      *
  55      */
  56 
  57     public X500PrivateCredential(X509Certificate cert, PrivateKey key) {
  58         if (cert == null || key == null )
  59             throw new IllegalArgumentException();
  60         this.cert = cert;
  61         this.key = key;
  62         this.alias=null;
  63     }
  64 
  65     /**
  66      * Creates an X500PrivateCredential that associates an X.509 certificate,
  67      * a private key and the KeyStore alias.
  68      *
  69      * @param cert X509Certificate
  70      * @param key  PrivateKey for the certificate
  71      * @param alias KeyStore alias
  72      * @exception IllegalArgumentException if either {@code cert},
  73      * {@code key} or {@code alias} is null
  74      *
  75      */
  76     public X500PrivateCredential(X509Certificate cert, PrivateKey key,
  77                                  String alias) {
  78         if (cert == null || key == null|| alias == null )
  79             throw new IllegalArgumentException();
  80         this.cert = cert;
  81         this.key = key;
  82         this.alias=alias;
  83     }
  84 
  85     /**
  86      * Returns the X.509 certificate.
  87      *
  88      * @return the X509Certificate
  89      */
  90 
  91     public X509Certificate getCertificate() {
  92         return cert;
  93     }
  94 
  95     /**
  96      * Returns the PrivateKey.
  97      *
  98      * @return the PrivateKey
  99      */
 100     public PrivateKey getPrivateKey() {
 101         return key;
 102     }
 103 
 104     /**
 105      * Returns the KeyStore alias.
 106      *
 107      * @return the KeyStore alias
 108      */
 109 
 110     public String getAlias() {
 111         return alias;
 112     }
 113 
 114     /**
 115      * Clears the references to the X.509 certificate, private key and the
 116      * KeyStore alias in this object.
 117      */
 118 
 119     public void destroy() {
 120         cert = null;
 121         key = null;
 122         alias =null;
 123     }
 124 
 125     /**
 126      * Determines if the references to the X.509 certificate and private key
 127      * in this object have been cleared.
 128      *
 129      * @return true if X509Certificate and the PrivateKey are null
 130 
 131      */
 132     public boolean isDestroyed() {
 133         return cert == null && key == null && alias==null;
 134     }
 135 }