| 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 java.security.GeneralSecurityException; |
| 18 |
import java.io.File; |
| 19 |
import java.io.IOException; |
| 20 |
import java.io.InputStream; |
| 21 |
import java.io.OutputStream; |
| 22 |
import java.io.FileInputStream; |
| 23 |
import java.io.FileOutputStream; |
| 24 |
|
| 25 |
import org.globus.gsi.OpenSSLKey; |
| 26 |
import org.globus.gsi.CertUtil; |
| 27 |
import org.globus.gsi.bc.BouncyCastleOpenSSLKey; |
| 28 |
import org.globus.util.Util; |
| 29 |
import org.globus.common.CoGProperties; |
| 30 |
import org.globus.common.Version; |
| 31 |
|
| 32 |
/** Changes the Passphrase. |
| 33 |
* <pre> |
| 34 |
* Syntax: java ChangePassPhrase [-help] [-version] [-file private_key_file] |
| 35 |
* Changes the passphrase that protects the private key. If the -file |
| 36 |
* argument is not given, the default location of the file containing |
| 37 |
* the private key is assumed: |
| 38 |
* -- Config.getUserKeyFile() |
| 39 |
* Options |
| 40 |
* -help, -usage Display usage |
| 41 |
* -version Display version |
| 42 |
* -file location Change passphrase on key stored in the |
| 43 |
* file at the non-standard |
| 44 |
* location 'location'; |
| 45 |
* </pre> |
| 46 |
*/ |
| 47 |
public class ChangePassPhrase { |
| 48 |
|
| 49 |
private static String message = |
| 50 |
"\n" + |
| 51 |
"Syntax: java ChangePassPhrase [-help] [-version] [-file private_key_file]\n\n" + |
| 52 |
"\tChanges the passphrase that protects the private key. If the\n" + |
| 53 |
"\t-file argument is not given, the default location of the file\n" + |
| 54 |
"\tcontaining the private key is assumed:\n\n" + |
| 55 |
"\t -- " + CoGProperties.getDefault().getUserKeyFile() + "\n\n" + |
| 56 |
"\tOptions\n" + |
| 57 |
"\t-help | -usage\n" + |
| 58 |
"\t\tDisplay usage.\n" + |
| 59 |
"\t-version\n" + |
| 60 |
"\t\tDisplay version.\n" + |
| 61 |
"\t-file location\n" + |
| 62 |
"\t\tChange passphrase on key stored in the file at\n" + |
| 63 |
"\t\tthe non-standard location 'location'.\n\n"; |
| 64 |
|
| 65 |
public static void main(String args[]) { |
| 66 |
|
| 67 |
String file = null; |
| 68 |
boolean error = false; |
| 69 |
boolean debug = false; |
| 70 |
|
| 71 |
for (int i = 0; i < args.length; i++) { |
| 72 |
if (args[i].equalsIgnoreCase("-file")) { |
| 73 |
file = args[++i]; |
| 74 |
} else if (args[i].equalsIgnoreCase("-version")) { |
| 75 |
System.err.println(Version.getVersion()); |
| 76 |
System.exit(1); |
| 77 |
} else if (args[i].equalsIgnoreCase("-debug")) { |
| 78 |
debug = true; |
| 79 |
} else if (args[i].equalsIgnoreCase("-help") || |
| 80 |
args[i].equalsIgnoreCase("-usage")) { |
| 81 |
System.err.println(message); |
| 82 |
System.exit(1); |
| 83 |
} else { |
| 84 |
System.err.println("Error: argument not recognized : " + args[i]); |
| 85 |
error = true; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (error) { |
| 90 |
System.err.println("\nUsage: java ChangePassPhrase [-help] [-version] [-file private_key_file]\n"); |
| 91 |
System.err.println("Use -help to display full usage."); |
| 92 |
System.exit(1); |
| 93 |
} |
| 94 |
|
| 95 |
CertUtil.init(); |
| 96 |
|
| 97 |
if (file == null) { |
| 98 |
file = CoGProperties.getDefault().getUserKeyFile(); |
| 99 |
} |
| 100 |
|
| 101 |
OpenSSLKey key = null; |
| 102 |
String pwd1, pwd2 = null; |
| 103 |
|
| 104 |
try { |
| 105 |
key = new BouncyCastleOpenSSLKey(file); |
| 106 |
|
| 107 |
if (key.isEncrypted()) { |
| 108 |
pwd1 = Util.getPrivateInput("Enter OLD pass phrase: "); |
| 109 |
if (pwd1 == null || pwd1.length() == 0) return; |
| 110 |
try { |
| 111 |
key.decrypt(pwd1); |
| 112 |
} catch(Exception e) { |
| 113 |
System.err.println("Error: Wrong pass phrase or key is invalid."); |
| 114 |
if (debug) { |
| 115 |
e.printStackTrace(); |
| 116 |
} |
| 117 |
System.exit(1); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
pwd1 = Util.getPrivateInput("Enter NEW pass phrase: "); |
| 122 |
if (pwd1 == null || pwd1.length() == 0) return; |
| 123 |
|
| 124 |
pwd2 = Util.getPrivateInput("Verifying password - Enter NEW pass phrase: "); |
| 125 |
if (pwd2 == null || pwd2.length() == 0) return; |
| 126 |
|
| 127 |
if (!pwd1.equals(pwd2)) { |
| 128 |
System.err.println("Error: Passwords do not match!"); |
| 129 |
System.exit(1); |
| 130 |
} |
| 131 |
|
| 132 |
key.encrypt(pwd1); |
| 133 |
|
| 134 |
File newFile = Util.createFile(file + ".new"); |
| 135 |
Util.setOwnerAccessOnly(newFile.getAbsolutePath()); |
| 136 |
File oldFile = Util.createFile(file + ".old"); |
| 137 |
Util.setOwnerAccessOnly(oldFile.getAbsolutePath()); |
| 138 |
File crFile = Util.createFile(file); |
| 139 |
Util.setOwnerAccessOnly(crFile.getAbsolutePath()); |
| 140 |
|
| 141 |
copy(crFile, oldFile); |
| 142 |
|
| 143 |
key.writeTo(newFile.getAbsolutePath()); |
| 144 |
|
| 145 |
if (!crFile.delete()) { |
| 146 |
System.err.println("Error: failed to remove " + file + " file."); |
| 147 |
System.exit(1); |
| 148 |
} |
| 149 |
|
| 150 |
if (newFile.renameTo(crFile)) { |
| 151 |
System.out.println("Pass phrase successfully changed."); |
| 152 |
} else { |
| 153 |
System.err.println("Error: failed to rename the files."); |
| 154 |
System.exit(1); |
| 155 |
} |
| 156 |
|
| 157 |
} catch(GeneralSecurityException e) { |
| 158 |
System.err.println("Error: " + e.getMessage()); |
| 159 |
System.exit(1); |
| 160 |
} catch(Exception e) { |
| 161 |
System.err.println("Unable to load the private key : " + e.getMessage()); |
| 162 |
System.exit(1); |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
private static void copy(File srcFile, File dstFile) |
| 168 |
throws IOException { |
| 169 |
|
| 170 |
InputStream in = null; |
| 171 |
OutputStream out = null; |
| 172 |
byte [] buffer = new byte[1024]; |
| 173 |
int bytes = 0; |
| 174 |
|
| 175 |
try { |
| 176 |
in = new FileInputStream(srcFile); |
| 177 |
out = new FileOutputStream(dstFile); |
| 178 |
|
| 179 |
Util.setOwnerAccessOnly(dstFile.getAbsolutePath()); |
| 180 |
|
| 181 |
while( (bytes = in.read(buffer)) != -1) { |
| 182 |
out.write(buffer, 0, bytes); |
| 183 |
out.flush(); |
| 184 |
} |
| 185 |
|
| 186 |
} finally { |
| 187 |
try { |
| 188 |
if (in != null) in.close(); |
| 189 |
if (out != null) out.close(); |
| 190 |
} catch(Exception e) {} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
} |