Home | History | Annotate | Download | only in jsse
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 package org.apache.harmony.xnet.provider.jsse;
     18 
     19 import java.io.File;
     20 import java.io.FileInputStream;
     21 import java.io.FileNotFoundException;
     22 import java.io.IOException;
     23 import java.security.AccessController;
     24 import java.security.InvalidAlgorithmParameterException;
     25 import java.security.KeyStore;
     26 import java.security.KeyStoreException;
     27 import java.security.NoSuchAlgorithmException;
     28 import java.security.UnrecoverableKeyException;
     29 import java.security.cert.CertificateException;
     30 import javax.net.ssl.KeyManager;
     31 import javax.net.ssl.KeyManagerFactorySpi;
     32 import javax.net.ssl.ManagerFactoryParameters;
     33 
     34 /**
     35  * KeyManagerFactory implementation.
     36  * @see javax.net.ssl.KeyManagerFactorySpi
     37  */
     38 public class KeyManagerFactoryImpl extends KeyManagerFactorySpi {
     39 
     40     // source of key material
     41     private KeyStore keyStore;
     42 
     43     //password
     44     private char[] pwd;
     45 
     46     /**
     47      * @see javax.net.ssl.KeyManagerFactorySpi#engineInit(KeyStore ks, char[]
     48      *      password)
     49      */
     50     @Override
     51     public void engineInit(KeyStore ks, char[] password)
     52             throws KeyStoreException, NoSuchAlgorithmException,
     53             UnrecoverableKeyException {
     54         if (ks != null) {
     55             keyStore = ks;
     56             if (password != null) {
     57                 pwd = password.clone();
     58             } else {
     59                 pwd = new char[0];
     60             }
     61         } else {
     62             keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
     63             String keyStoreName = AccessController
     64                     .doPrivileged(new java.security.PrivilegedAction<String>() {
     65                         public String run() {
     66                             return System.getProperty("javax.net.ssl.keyStore");
     67                         }
     68                     });
     69             String keyStorePwd = null;
     70             if (keyStoreName == null || keyStoreName.equalsIgnoreCase("NONE")
     71                     || keyStoreName.length() == 0) {
     72                 try {
     73                     keyStore.load(null, null);
     74                 } catch (IOException e) {
     75                     throw new KeyStoreException(e);
     76                 } catch (CertificateException e) {
     77                     throw new KeyStoreException(e);
     78                 }
     79             } else {
     80                 keyStorePwd = AccessController
     81                         .doPrivileged(new java.security.PrivilegedAction<String>() {
     82                             public String run() {
     83                                 return System
     84                                         .getProperty("javax.net.ssl.keyStorePassword");
     85                             }
     86                         });
     87                 if (keyStorePwd == null) {
     88                     pwd = new char[0];
     89                 } else {
     90                     pwd = keyStorePwd.toCharArray();
     91                 }
     92                 try {
     93                     keyStore.load(new FileInputStream(new File(keyStoreName)),
     94                             pwd);
     95 
     96                 } catch (FileNotFoundException e) {
     97                     throw new KeyStoreException(e);
     98                 } catch (IOException e) {
     99                     throw new KeyStoreException(e);
    100                 } catch (CertificateException e) {
    101                     throw new KeyStoreException(e);
    102                 }
    103             }
    104 
    105         }
    106 
    107     }
    108 
    109     /**
    110      * @see javax.net.ssl.KeyManagerFactorySpi#engineInit(ManagerFactoryParameters
    111      *      spec)
    112      */
    113     @Override
    114     public void engineInit(ManagerFactoryParameters spec)
    115             throws InvalidAlgorithmParameterException {
    116         throw new InvalidAlgorithmParameterException(
    117                 "ManagerFactoryParameters not supported");
    118 
    119     }
    120 
    121     /**
    122      * @see javax.net.ssl.KeyManagerFactorySpi#engineGetKeyManagers()
    123      */
    124     @Override
    125     public KeyManager[] engineGetKeyManagers() {
    126         if (keyStore == null) {
    127             throw new IllegalStateException("KeyManagerFactory is not initialized");
    128         }
    129         return new KeyManager[] { new KeyManagerImpl(keyStore, pwd) };
    130     }
    131 
    132 }
    133