001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.gae.auth;
018    
019    import java.io.InputStream;
020    import java.security.KeyStore;
021    import java.security.KeyStore.PasswordProtection;
022    import java.security.KeyStore.PrivateKeyEntry;
023    import java.security.PrivateKey;
024    
025    import org.springframework.core.io.Resource;
026    
027    /**
028     * A Java keystore specific key loader. 
029     */
030    public class GAuthJksLoader implements GAuthKeyLoader {
031    
032        private Resource keyStoreLocation;
033    
034        private String storePass;
035    
036        private String keyPass;
037    
038        private String keyAlias;
039    
040        public GAuthJksLoader() {
041            this(null, null, null, null);
042        }
043    
044        public GAuthJksLoader(Resource keyStoreLocation, String storePass, String keyPass, String keyAlias) {
045            this.keyStoreLocation = keyStoreLocation;
046            this.storePass = storePass;
047            this.keyPass = keyPass;
048            this.keyAlias = keyAlias;
049        }
050    
051        /**
052         * Set the location of the Java keystore.
053         * 
054         * @param keyStoreLocation
055         */
056        public void setKeyStoreLocation(Resource keyStoreLocation) {
057            this.keyStoreLocation = keyStoreLocation;
058        }
059    
060        /**
061         * Sets the password used to open the key store.
062         * 
063         * @param storePass
064         */
065        public void setStorePass(String storePass) {
066            this.storePass = storePass;
067        }
068    
069        /**
070         * Sets the password used to get access to a specific key.
071         * 
072         * @param keyPass
073         */
074        public void setKeyPass(String keyPass) {
075            this.keyPass = keyPass;
076        }
077    
078        /**
079         * Sets the alias of the key to be loaded.
080         * 
081         * @param keyAlias
082         */
083        public void setKeyAlias(String keyAlias) {
084            this.keyAlias = keyAlias;
085        }
086    
087        /**
088         * Loads a private key from a Java keystore depending on this loader's 
089         * properties.
090         */
091        public PrivateKey loadPrivateKey() throws Exception {
092            InputStream input = keyStoreLocation.getInputStream();
093            try {
094                return loadPrivateKey(input);
095            } finally {
096                input.close();
097            }
098        }
099    
100        private PrivateKey loadPrivateKey(InputStream input) throws Exception {
101            // Load keystore
102            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
103            keystore.load(input, storePass.toCharArray());
104    
105            // Retrieve private key
106            PrivateKeyEntry entry = (PrivateKeyEntry)keystore.getEntry(keyAlias, new PasswordProtection(keyPass.toCharArray()));
107            return entry.getPrivateKey();
108        }
109    
110    }