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 
18 /**
19  * @author Vera Y. Petrashkova
20  */
21 
22 package javax.net.ssl;
23 
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 
30 /**
31  * Class for verification KeyManagerFactorySpi and KeyManagerFactory
32  * functionality
33  */
34 
35 public class MyKeyManagerFactorySpi extends KeyManagerFactorySpi {
36 
37     @Override
engineInit(KeyStore ks, char[] password)38     protected void engineInit(KeyStore ks, char[] password)
39             throws KeyStoreException, NoSuchAlgorithmException,
40             UnrecoverableKeyException {
41         if (password == null) {
42             throw new KeyStoreException("Incorrect password");
43         }
44         if (ks == null) {
45             throw new UnrecoverableKeyException("Incorrect keystore");
46         }
47     }
48 
49     @Override
engineInit(ManagerFactoryParameters spec)50     protected void engineInit(ManagerFactoryParameters spec)
51             throws InvalidAlgorithmParameterException {
52         if (spec == null) {
53             throw new InvalidAlgorithmParameterException("Incorrect parameter");
54         }
55         if (spec instanceof Parameters) {
56             try {
57                 engineInit(((Parameters) spec).getKeyStore(),
58                         ((Parameters) spec).getPassword());
59             } catch (Exception e) {
60                 throw new InvalidAlgorithmParameterException(e.toString());
61             }
62         } else {
63             throw new InvalidAlgorithmParameterException("Invalid parameter");
64         }
65     }
66 
67     @Override
engineGetKeyManagers()68     protected KeyManager[] engineGetKeyManagers() {
69         return null;
70     }
71 
72     public static class Parameters implements ManagerFactoryParameters {
73         private KeyStore keyStore;
74         private char[] passWD;
75 
Parameters(KeyStore ks, char[] pass)76         public Parameters(KeyStore ks, char[] pass) {
77             this.keyStore = ks;
78             this.passWD = pass;
79         }
80 
getKeyStore()81         public KeyStore getKeyStore() {
82             return keyStore;
83         }
84 
getPassword()85         public char[] getPassword() {
86             return passWD;
87         }
88     }
89 }
90