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
18package org.apache.harmony.xnet.tests.support;
19
20import java.security.InvalidAlgorithmParameterException;
21import java.security.KeyStore;
22import java.security.KeyStoreException;
23import java.security.NoSuchAlgorithmException;
24import java.security.UnrecoverableKeyException;
25
26import javax.net.ssl.KeyManager;
27import javax.net.ssl.KeyManagerFactorySpi;
28import javax.net.ssl.ManagerFactoryParameters;
29
30/**
31 * Class for verification KeyManagerFactorySpi and KeyManagerFactory
32 * functionality
33 */
34
35public class MyKeyManagerFactorySpi extends KeyManagerFactorySpi {
36
37    @Override
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
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
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
76        public Parameters(KeyStore ks, char[] pass) {
77            this.keyStore = ks;
78            this.passWD = pass;
79        }
80
81        public KeyStore getKeyStore() {
82            return keyStore;
83        }
84
85        public char[] getPassword() {
86            return passWD;
87        }
88    }
89}
90