| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.tools; |
| 16 |
|
| 17 |
import org.globus.gsi.util.CertificateLoadUtil; |
| 18 |
|
| 19 |
import java.io.File; |
| 20 |
import java.io.IOException; |
| 21 |
import java.io.FileOutputStream; |
| 22 |
import java.security.PrivateKey; |
| 23 |
import java.security.KeyStore; |
| 24 |
import java.security.GeneralSecurityException; |
| 25 |
import java.security.cert.X509Certificate; |
| 26 |
|
| 27 |
import org.globus.gsi.OpenSSLKey; |
| 28 |
import org.globus.gsi.bc.BouncyCastleOpenSSLKey; |
| 29 |
import org.globus.common.Version; |
| 30 |
import org.globus.common.CoGProperties; |
| 31 |
import org.globus.util.Util; |
| 32 |
|
| 33 |
public class KeyStoreConvert { |
| 34 |
|
| 35 |
public static final String DEFAULT_ALIAS = "globus"; |
| 36 |
public static final String DEFAULT_PASSWORD = "globus"; |
| 37 |
public static final String DEFAULT_KEYSTORE_FILE = "globus.jks"; |
| 38 |
|
| 39 |
private static final String message = |
| 40 |
"\n" + |
| 41 |
"Syntax: java KeyStoreConvert [options]\n" + |
| 42 |
" java KeyStoreConvert -help\n\n" + |
| 43 |
"\tConverts Globus credentials (user key and certificate) into \n" + |
| 44 |
"\tJava keystore format (JKS format supported by Sun).\n\n" + |
| 45 |
"\tOptions\n" + |
| 46 |
"\t-help | -usage\n" + |
| 47 |
"\t\tDisplays usage.\n" + |
| 48 |
"\t-version\n" + |
| 49 |
"\t\tDisplays version.\n" + |
| 50 |
"\t-debug\n" + |
| 51 |
"\t\tEnables extra debug output.\n" + |
| 52 |
"\t-cert <certfile>\n" + |
| 53 |
"\t\tNon-standard location of user certificate.\n" + |
| 54 |
"\t-key <keyfile>\n" + |
| 55 |
"\t\tNon-standard location of user key.\n" + |
| 56 |
"\t-alias <alias>\n" + |
| 57 |
"\t\tKeystore alias entry. Defaults to '" +DEFAULT_ALIAS + "'\n" + |
| 58 |
"\t-password <password>\n" + |
| 59 |
"\t\tKeystore password. Defaults to '" +DEFAULT_PASSWORD + "'\n" + |
| 60 |
"\t-out <keystorefile>\n" + |
| 61 |
"\t\tLocation of the Java keystore file. Defaults to\n" + |
| 62 |
"\t\t'" + DEFAULT_KEYSTORE_FILE + "'\n\n"; |
| 63 |
|
| 64 |
public static void main(String args[]) { |
| 65 |
|
| 66 |
CoGProperties props = CoGProperties.getDefault(); |
| 67 |
|
| 68 |
boolean error = false; |
| 69 |
boolean debug = false; |
| 70 |
String alias = DEFAULT_ALIAS; |
| 71 |
String password = DEFAULT_PASSWORD; |
| 72 |
String keyFile = props.getUserKeyFile(); |
| 73 |
String certFile = props.getUserCertFile(); |
| 74 |
String keyStoreFile = DEFAULT_KEYSTORE_FILE; |
| 75 |
|
| 76 |
for (int i = 0; i < args.length; i++) { |
| 77 |
if (args[i].equalsIgnoreCase("-debug")) { |
| 78 |
debug = true; |
| 79 |
} else if (args[i].equalsIgnoreCase("-out")) { |
| 80 |
keyStoreFile = args[++i]; |
| 81 |
} else if (args[i].equalsIgnoreCase("-key")) { |
| 82 |
keyFile = args[++i]; |
| 83 |
} else if (args[i].equalsIgnoreCase("-cert")) { |
| 84 |
certFile = args[++i]; |
| 85 |
} else if (args[i].equalsIgnoreCase("-alias")) { |
| 86 |
alias = args[++i]; |
| 87 |
} else if (args[i].equalsIgnoreCase("-pwd") || |
| 88 |
args[i].equalsIgnoreCase("-password")) { |
| 89 |
password = args[++i]; |
| 90 |
} else if (args[i].equalsIgnoreCase("-version")) { |
| 91 |
System.err.println(Version.getVersion()); |
| 92 |
System.exit(1); |
| 93 |
} else if (args[i].equalsIgnoreCase("-help") || |
| 94 |
args[i].equalsIgnoreCase("-usage")) { |
| 95 |
System.err.println(message); |
| 96 |
System.exit(1); |
| 97 |
} else { |
| 98 |
System.err.println("Error: Argument not recognized: " + args[i]); |
| 99 |
error = true; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if (error) return; |
| 104 |
|
| 105 |
if (keyStoreFile == null) { |
| 106 |
System.err.println("Error: Java key store output file is not specified."); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
if (debug) { |
| 111 |
System.out.println("### Current settings ###"); |
| 112 |
System.out.println(" Certificate file : " + certFile); |
| 113 |
System.out.println(" SSLeay key file : " + keyFile); |
| 114 |
System.out.println(" Java keystore file : " + keyStoreFile); |
| 115 |
System.out.println(" Key entry Alias : " + alias); |
| 116 |
System.out.println(" Java keystore password : " + password); |
| 117 |
} |
| 118 |
|
| 119 |
File f = new File(keyStoreFile); |
| 120 |
if (f.exists()) { |
| 121 |
System.err.println("Error: Output file (" + keyStoreFile + ") already exists."); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
int rs = createKeyStore(certFile, |
| 126 |
keyFile, |
| 127 |
alias, |
| 128 |
password, |
| 129 |
keyStoreFile, |
| 130 |
debug); |
| 131 |
|
| 132 |
// Workaround to fix JNI bug (noticeable on some RedHat 6.1 and 7.1 systems) |
| 133 |
// for a description of the bug see http://java.sun.com/j2se/1.3/relnotes.html |
| 134 |
// and there grep for "ERROR REPORT" |
| 135 |
// gavin McCance <[email protected]> |
| 136 |
|
| 137 |
System.exit(rs); |
| 138 |
} |
| 139 |
|
| 140 |
private static int createKeyStore(String certFile, |
| 141 |
String keyFile, |
| 142 |
String alias, |
| 143 |
String password, |
| 144 |
String keyStoreFile, |
| 145 |
boolean debug) { |
| 146 |
|
| 147 |
X509Certificate [] certs = new X509Certificate[1]; |
| 148 |
PrivateKey key = null; |
| 149 |
|
| 150 |
try { |
| 151 |
certs[0] = CertificateLoadUtil.loadCertificate(certFile); |
| 152 |
} catch(Exception e) { |
| 153 |
System.err.println("Failed to load certificate: " + e.getMessage()); |
| 154 |
return -1; |
| 155 |
} |
| 156 |
|
| 157 |
try { |
| 158 |
OpenSSLKey sslkey = new BouncyCastleOpenSSLKey(keyFile); |
| 159 |
|
| 160 |
if (sslkey.isEncrypted()) { |
| 161 |
String pwd = Util.getPrivateInput("Enter pass phrase: "); |
| 162 |
|
| 163 |
if (pwd == null) { |
| 164 |
// user canceled |
| 165 |
return -2; |
| 166 |
} |
| 167 |
|
| 168 |
sslkey.decrypt(pwd); |
| 169 |
} |
| 170 |
|
| 171 |
key = sslkey.getPrivateKey(); |
| 172 |
|
| 173 |
} catch(IOException e) { |
| 174 |
System.err.println("Failed to load key: " + e.getMessage()); |
| 175 |
return -1; |
| 176 |
} catch(GeneralSecurityException e) { |
| 177 |
System.err.println("Error: Wrong pass phrase"); |
| 178 |
if (debug) { |
| 179 |
e.printStackTrace(); |
| 180 |
} |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
System.out.println("Creating Java keystore..."); |
| 185 |
|
| 186 |
FileOutputStream out = null; |
| 187 |
|
| 188 |
try { |
| 189 |
KeyStore ks = KeyStore.getInstance("JKS", "SUN"); |
| 190 |
ks.load(null, null); |
| 191 |
// this takes a while for some reason |
| 192 |
ks.setKeyEntry(alias, key, password.toCharArray(), certs); |
| 193 |
out = new FileOutputStream(keyStoreFile); |
| 194 |
ks.store(out, password.toCharArray()); |
| 195 |
} catch(Exception e) { |
| 196 |
System.err.println("Failed to create Java key store: " + e.getMessage()); |
| 197 |
return -1; |
| 198 |
} finally { |
| 199 |
if (out != null) { |
| 200 |
try { out.close(); } catch(IOException ee) {} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
System.out.println("Java keystore file (" + keyStoreFile + |
| 205 |
") successfully created."); |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
} |