01: /*
02: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.wso2.esb.security.util;
18:
19: import java.io.FileInputStream;
20: import java.io.FileOutputStream;
21: import java.security.KeyStore;
22: import java.security.cert.Certificate;
23:
24: /*
25: *
26: */
27:
28: public class ImportKey {
29: /**
30: * sourcekeystore sourceStorepass keyalias targetstore targetStorePass
31: *
32: * @param args
33: */
34: public static void main(String[] args) throws Exception {
35: System.out.println("About to import key...");
36: if (args.length != 5) {
37: throw new Exception("Incorrect number of parameters");
38: }
39:
40: try {
41: String sourceStorePath = args[0];
42: String sourceStorePass = args[1];
43: String keyAlias = args[2];
44: String targetStorePath = args[3];
45: String targetStorePass = args[4];
46:
47: KeyStore sourceStore = KeyStore.getInstance("JKS");
48: FileInputStream fis = new FileInputStream(sourceStorePath);
49: sourceStore.load(fis, sourceStorePass.toCharArray());
50:
51: Certificate cert = sourceStore
52: .getCertificateChain(keyAlias)[0];
53: KeyStore targetStore = KeyStore.getInstance("JKS");
54:
55: targetStore.load(null, null);
56: targetStore.setCertificateEntry(keyAlias, cert);
57: FileOutputStream fileOutputStream = new FileOutputStream(
58: targetStorePath);
59: targetStore.store(fileOutputStream, targetStorePass
60: .toCharArray());
61:
62: fis.close();
63: fileOutputStream.flush();
64: fileOutputStream.close();
65: } catch (Exception e) {
66: e.printStackTrace();
67: System.err.println("Importing of key failed");
68: throw e;
69: }
70: }
71: }
|