| 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 |
import org.globus.gsi.util.CertificateUtil; |
| 19 |
import org.globus.gsi.util.ProxyCertificateUtil; |
| 20 |
|
| 21 |
import org.globus.gsi.trustmanager.X509ProxyCertPathValidator; |
| 22 |
|
| 23 |
import org.globus.gsi.stores.ResourceSigningPolicyStore; |
| 24 |
import org.globus.gsi.stores.Stores; |
| 25 |
|
| 26 |
import org.globus.gsi.X509ProxyCertPathParameters; |
| 27 |
|
| 28 |
import org.globus.gsi.provider.GlobusProvider; |
| 29 |
|
| 30 |
import java.security.Security; |
| 31 |
import java.util.HashMap; |
| 32 |
import java.util.Map; |
| 33 |
import java.security.cert.CertStore; |
| 34 |
import java.security.KeyStore; |
| 35 |
import org.globus.gsi.X509ExtensionSet; |
| 36 |
import java.security.cert.CertPathValidatorException; |
| 37 |
import java.security.cert.CertPath; |
| 38 |
import java.security.GeneralSecurityException; |
| 39 |
import java.security.PrivateKey; |
| 40 |
import java.security.cert.X509Certificate; |
| 41 |
import java.security.interfaces.RSAPrivateKey; |
| 42 |
import java.security.interfaces.RSAPublicKey; |
| 43 |
import java.io.OutputStream; |
| 44 |
import java.io.FileOutputStream; |
| 45 |
import java.io.FileInputStream; |
| 46 |
import java.io.IOException; |
| 47 |
import java.io.EOFException; |
| 48 |
import java.io.File; |
| 49 |
import org.globus.common.CoGProperties; |
| 50 |
import org.globus.common.Version; |
| 51 |
import org.globus.gsi.OpenSSLKey; |
| 52 |
import org.globus.gsi.CertUtil; |
| 53 |
import org.globus.gsi.GlobusCredential; |
| 54 |
import org.globus.gsi.GSIConstants; |
| 55 |
import org.globus.gsi.bc.BouncyCastleOpenSSLKey; |
| 56 |
import org.globus.gsi.bc.BouncyCastleCertProcessingFactory; |
| 57 |
import org.globus.gsi.proxy.ext.ProxyPolicy; |
| 58 |
import org.globus.gsi.proxy.ext.ProxyCertInfo; |
| 59 |
import org.globus.gsi.proxy.ext.ProxyCertInfoExtension; |
| 60 |
import org.globus.gsi.proxy.ext.GlobusProxyCertInfoExtension; |
| 61 |
import org.globus.gsi.proxy.ProxyPolicyHandler; |
| 62 |
import org.globus.util.Util; |
| 63 |
|
| 64 |
|
| 65 |
/* ########################################################################### |
| 66 |
* This is a command-line tool and was specifically designed to be used as so. |
| 67 |
* Do not use it as a library. |
| 68 |
* ######################################################################## */ |
| 69 |
|
| 70 |
/** |
| 71 |
* Initializes/creates a new globus proxy. This is a command-line tool. Please |
| 72 |
* do not use it as a library. |
| 73 |
*/ |
| 74 |
public abstract class ProxyInit { |
| 75 |
|
| 76 |
static { |
| 77 |
Security.addProvider(new GlobusProvider()); |
| 78 |
} |
| 79 |
|
| 80 |
public static final String GENERIC_POLICY_OID = "1.3.6.1.4.1.3536.1.1.1.8"; |
| 81 |
|
| 82 |
private static final String message = |
| 83 |
"\n" + |
| 84 |
"Syntax: java ProxyInit [options]\n" + |
| 85 |
" java ProxyInit -help\n\n" + |
| 86 |
" Options:\n" + |
| 87 |
" -help | -usage\t\tDisplays usage.\n" + |
| 88 |
" -version\t\t\tDisplays version.\n" + |
| 89 |
" -debug\t\t\tEnables extra debug output.\n" + |
| 90 |
" -verify\t\t\tPerforms proxy verification tests (default).\n" + |
| 91 |
" -pwstdin\t\t\tAllows passphrase from stdin.\n" + |
| 92 |
" -noverify\t\t\tDisables proxy verification tests.\n" + |
| 93 |
" -quiet | -q\t\t\tQuiet mode, minimal output\n" + |
| 94 |
" -limited\t\t\tCreates a limited proxy.\n" + |
| 95 |
" -independent\t\tCreates a independent globus proxy.\n" + |
| 96 |
" -old\t\t\tCreates a legacy globus proxy.\n" + |
| 97 |
" -valid <seconds>\t\tProxy is valid for S seconds (default:12 " + |
| 98 |
"hours)\n" + |
| 99 |
" -valid <hours:minutes>\tProxy is valid for H hours and M \n" + |
| 100 |
" \tminutes. (default: 12 hours)\n" + |
| 101 |
" -hours <hours>\t\tProxy is valid for H hours (default:12).\n" + |
| 102 |
" -bits <bits>\t\tNumber of bits in key {512|1024|2048|4096}.\n" + |
| 103 |
" -globus\t\t\tPrints user identity in globus format.\n" + |
| 104 |
" -policy <policyfile>\tFile containing policy to store in the\n" + |
| 105 |
" \tProxyCertInfo extension\n" + |
| 106 |
" -pl <oid>\t\t\tOID string for the policy language.\n" + |
| 107 |
" -policy-language <oid>\tused in the policy file.\n" + |
| 108 |
" -path-length <l>\t\tAllow a chain of at most l proxies to be \n" + |
| 109 |
" \t\tgenerated from this one\n" + |
| 110 |
" -cert <certfile>\t\tNon-standard location of user certificate\n" + |
| 111 |
" -key <keyfile>\t\tNon-standard location of user key\n" + |
| 112 |
" -out <proxyfile>\t\tNon-standard location of new proxy cert.\n" + |
| 113 |
" -pkcs11\t\t\tEnables the PKCS11 support module. The\n" + |
| 114 |
" \t\t\t-cert and -key arguments are used as labels\n" + |
| 115 |
" \t\t\tto find the credentials on the device.\n" + |
| 116 |
" -rfc\t\t\tCreates RFC 3820 compliant proxy. (Default)\n" + |
| 117 |
" -draft\t\t\tCreates RFC draft compliant proxy\n"; |
| 118 |
|
| 119 |
protected X509Certificate[] certificates; |
| 120 |
protected int bits = 512; |
| 121 |
protected int lifetime = 3600 * 12; |
| 122 |
|
| 123 |
protected ProxyCertInfo proxyCertInfo = null; |
| 124 |
protected GSIConstants.CertificateType proxyType; |
| 125 |
|
| 126 |
protected boolean quiet = false; |
| 127 |
protected boolean debug = false; |
| 128 |
protected boolean stdin = false; |
| 129 |
|
| 130 |
protected GlobusCredential proxy = null; |
| 131 |
|
| 132 |
public abstract void init(String [] args); |
| 133 |
public abstract void loadCertificates(String args); |
| 134 |
public abstract void loadKey(String arg); |
| 135 |
public abstract void sign(); |
| 136 |
|
| 137 |
public X509Certificate getCertificate() { |
| 138 |
return this.certificates[0]; |
| 139 |
} |
| 140 |
|
| 141 |
public void dispose() { |
| 142 |
} |
| 143 |
|
| 144 |
// verifies the proxy credential |
| 145 |
public void verify() throws Exception { |
| 146 |
|
| 147 |
Map<String, ProxyPolicyHandler> handlers = null; |
| 148 |
if (proxyCertInfo != null) { |
| 149 |
String oid = proxyCertInfo.getProxyPolicy().getPolicyLanguage().getId(); |
| 150 |
handlers = new HashMap<String,ProxyPolicyHandler>(); |
| 151 |
handlers.put(oid, new ProxyPolicyHandler() { |
| 152 |
public void validate(ProxyCertInfo proxyCertInfo, |
| 153 |
CertPath certPath, |
| 154 |
int index) |
| 155 |
throws CertPathValidatorException { |
| 156 |
// ignore policy - this is just for proxy init case |
| 157 |
System.out.println("Proxy verify: Ignoring proxy policy"); |
| 158 |
if (debug) { |
| 159 |
String policy = |
| 160 |
new String(proxyCertInfo.getProxyPolicy().getPolicy()); |
| 161 |
System.out.println("Policy:"); |
| 162 |
System.out.println(policy); |
| 163 |
} |
| 164 |
} |
| 165 |
}); |
| 166 |
} |
| 167 |
|
| 168 |
KeyStore keyStore = Stores.getDefaultTrustStore(); |
| 169 |
CertStore crlStore = Stores.getDefaultCRLStore(); |
| 170 |
ResourceSigningPolicyStore sigPolStore = Stores.getDefaultSigningPolicyStore(); |
| 171 |
X509ProxyCertPathParameters parameters = new X509ProxyCertPathParameters(keyStore, crlStore, sigPolStore, false, handlers); |
| 172 |
X509ProxyCertPathValidator validator = new X509ProxyCertPathValidator(); |
| 173 |
validator.engineValidate(CertificateUtil.getCertPath(proxy.getCertificateChain()), parameters); |
| 174 |
} |
| 175 |
|
| 176 |
public void setBits(int bits) { |
| 177 |
this.bits = bits; |
| 178 |
} |
| 179 |
|
| 180 |
public void setLifetime(int lifetime) { |
| 181 |
this.lifetime = lifetime; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @param proxyType |
| 186 |
* @deprecated |
| 187 |
*/ |
| 188 |
public void setProxyType(int proxyType) { |
| 189 |
this.proxyType = GSIConstants.CertificateType.get(proxyType); |
| 190 |
} |
| 191 |
|
| 192 |
public void setProxyType(GSIConstants.CertificateType proxyType) { |
| 193 |
this.proxyType = proxyType; |
| 194 |
} |
| 195 |
|
| 196 |
public void setProxyCertInfo(ProxyCertInfo proxyCertInfo) { |
| 197 |
this.proxyCertInfo = proxyCertInfo; |
| 198 |
} |
| 199 |
|
| 200 |
public void setDebug(boolean debug) { |
| 201 |
this.debug = debug; |
| 202 |
} |
| 203 |
|
| 204 |
public void setQuiet(boolean quiet) { |
| 205 |
this.quiet = quiet; |
| 206 |
} |
| 207 |
|
| 208 |
public void setStdin(boolean stdin) { |
| 209 |
this.stdin = stdin; |
| 210 |
} |
| 211 |
|
| 212 |
public void createProxy(String cert, |
| 213 |
String key, |
| 214 |
boolean verify, |
| 215 |
boolean globusStyle, |
| 216 |
String proxyFile) { |
| 217 |
|
| 218 |
init(new String [] {cert, key}); |
| 219 |
|
| 220 |
loadCertificates(cert); |
| 221 |
|
| 222 |
if (!quiet) { |
| 223 |
String dn = null; |
| 224 |
if (globusStyle) { |
| 225 |
dn = CertificateUtil.toGlobusID(getCertificate().getSubjectDN()); |
| 226 |
} else { |
| 227 |
dn = getCertificate().getSubjectDN().getName(); |
| 228 |
} |
| 229 |
System.out.println("Your identity: " + dn); |
| 230 |
} |
| 231 |
|
| 232 |
loadKey(key); |
| 233 |
|
| 234 |
if (debug) { |
| 235 |
System.out.println("Using " + bits + " bits for private key"); |
| 236 |
} |
| 237 |
|
| 238 |
if (!quiet) { |
| 239 |
System.out.println("Creating proxy, please wait..."); |
| 240 |
} |
| 241 |
|
| 242 |
sign(); |
| 243 |
|
| 244 |
if (verify) { |
| 245 |
try { |
| 246 |
verify(); |
| 247 |
System.out.println("Proxy verify OK"); |
| 248 |
} catch(Exception e) { |
| 249 |
System.out.println("Proxy verify failed: " + e.getMessage()); |
| 250 |
if (debug) { |
| 251 |
e.printStackTrace(); |
| 252 |
} |
| 253 |
System.exit(-1); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if (debug) { |
| 258 |
System.out.println("Saving proxy to: " + proxyFile); |
| 259 |
} |
| 260 |
|
| 261 |
if (!quiet) { |
| 262 |
System.out.println("Your proxy is valid until " + |
| 263 |
proxy.getCertificateChain()[0].getNotAfter()); |
| 264 |
} |
| 265 |
|
| 266 |
OutputStream out = null; |
| 267 |
try { |
| 268 |
File file = Util.createFile(proxyFile); |
| 269 |
// set read only permissions |
| 270 |
if (!Util.setOwnerAccessOnly(proxyFile)) { |
| 271 |
System.err.println("Warning: Please check file permissions for your proxy file."); |
| 272 |
} |
| 273 |
out = new FileOutputStream(file); |
| 274 |
// write the contents |
| 275 |
proxy.save(out); |
| 276 |
} catch (Exception e) { |
| 277 |
System.err.println("Failed to save proxy to a file: " + |
| 278 |
e.getMessage()); |
| 279 |
System.exit(-1); |
| 280 |
} finally { |
| 281 |
if (out != null) { |
| 282 |
try { out.close(); } catch(Exception e) {} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
dispose(); |
| 287 |
} |
| 288 |
|
| 289 |
public static void main(String args[]) { |
| 290 |
|
| 291 |
int bits = 512; |
| 292 |
int lifetime = 3600 * 12; |
| 293 |
boolean debug = false; |
| 294 |
boolean quiet = false; |
| 295 |
boolean verify = true; |
| 296 |
boolean pkcs11 = false; |
| 297 |
boolean limited = false; |
| 298 |
int pathLen = -1; |
| 299 |
GSIConstants.CertificateType proxyType = GSIConstants.CertificateType.UNDEFINED; |
| 300 |
// 0 is old, 1 is Globus (draft compliant) oid, 2 is rfc oid |
| 301 |
int oid = 2; |
| 302 |
String policyLanguage = null; |
| 303 |
String policyFile = null; |
| 304 |
boolean stdin = false; |
| 305 |
boolean independent = false; |
| 306 |
|
| 307 |
CoGProperties properties = CoGProperties.getDefault(); |
| 308 |
|
| 309 |
boolean globusStyle = false; |
| 310 |
String proxyFile = properties.getProxyFile(); |
| 311 |
String keyFile = null; |
| 312 |
String certFile = null; |
| 313 |
|
| 314 |
for (int i = 0; i < args.length; i++) { |
| 315 |
if (args[i].equalsIgnoreCase("-hours")) { |
| 316 |
if (i+1 >= args.length) { |
| 317 |
argError("-hours argument missing"); |
| 318 |
} |
| 319 |
lifetime = 3600 * Integer.parseInt(args[++i]); |
| 320 |
} else if (args[i].equalsIgnoreCase("-bits")) { |
| 321 |
if (i+1 >= args.length) { |
| 322 |
argError("-bits argument missing"); |
| 323 |
} |
| 324 |
bits = Integer.parseInt(args[++i]); |
| 325 |
} else if (args[i].equalsIgnoreCase("-pwstdin")) { |
| 326 |
stdin = true; |
| 327 |
} else if (args[i].equalsIgnoreCase("-limited")) { |
| 328 |
limited = true; |
| 329 |
} else if (args[i].equalsIgnoreCase("-independent")) { |
| 330 |
independent = true; |
| 331 |
} else if (args[i].equalsIgnoreCase("-old")) { |
| 332 |
oid = 0; |
| 333 |
} else if (args[i].equalsIgnoreCase("-path-length")) { |
| 334 |
if (i+1 >= args.length) { |
| 335 |
argError("-path-length argument missing"); |
| 336 |
} |
| 337 |
pathLen = Integer.parseInt(args[++i]); |
| 338 |
} else if (args[i].equalsIgnoreCase("-pl") || |
| 339 |
args[i].equalsIgnoreCase("-policy-language")) { |
| 340 |
if (i+1 >= args.length) { |
| 341 |
argError("-policy-language argument missing"); |
| 342 |
} |
| 343 |
policyLanguage = args[++i]; |
| 344 |
} else if (args[i].equalsIgnoreCase("-policy")) { |
| 345 |
if (i+1 >= args.length) { |
| 346 |
argError("-policy argument missing"); |
| 347 |
} |
| 348 |
policyFile = args[++i]; |
| 349 |
} else if (args[i].equalsIgnoreCase("-debug")) { |
| 350 |
debug = true; |
| 351 |
} else if (args[i].equalsIgnoreCase("-verify")) { |
| 352 |
verify = true; |
| 353 |
} else if (args[i].equalsIgnoreCase("-noverify")) { |
| 354 |
verify = false; |
| 355 |
} else if (args[i].equalsIgnoreCase("-out")) { |
| 356 |
if (i+1 >= args.length) { |
| 357 |
argError("-out argument missing"); |
| 358 |
} |
| 359 |
proxyFile = args[++i]; |
| 360 |
} else if (args[i].equals("-q") || |
| 361 |
args[i].equalsIgnoreCase("-quiet")) { |
| 362 |
quiet = true; |
| 363 |
} else if (args[i].equalsIgnoreCase("-globus")) { |
| 364 |
globusStyle = true; |
| 365 |
} else if (args[i].equalsIgnoreCase("-pkcs11")) { |
| 366 |
pkcs11 = true; |
| 367 |
} else if (args[i].equalsIgnoreCase("-key")) { |
| 368 |
if (i+1 >= args.length) { |
| 369 |
argError("-key argument missing"); |
| 370 |
} |
| 371 |
keyFile = args[++i]; |
| 372 |
} else if (args[i].equalsIgnoreCase("-cert")) { |
| 373 |
if (i+1 >= args.length) { |
| 374 |
argError("-cert argument missing"); |
| 375 |
} |
| 376 |
certFile = args[++i]; |
| 377 |
} else if (args[i].equalsIgnoreCase("-valid")) { |
| 378 |
String validity = args[++i]; |
| 379 |
int delimiterIndex = validity.indexOf(":"); |
| 380 |
if (delimiterIndex == -1) { |
| 381 |
lifetime = Integer.parseInt(validity); |
| 382 |
} else { |
| 383 |
String hours = validity.substring(0, delimiterIndex); |
| 384 |
String minutes = validity.substring(delimiterIndex+1, |
| 385 |
validity.length()); |
| 386 |
int hoursInt = Integer.parseInt(hours); |
| 387 |
int minsInt = Integer.parseInt(minutes); |
| 388 |
lifetime = (minsInt * 60) + (hoursInt * 60 * 60); |
| 389 |
} |
| 390 |
} else if (args[i].equalsIgnoreCase("-version")) { |
| 391 |
System.err.println(Version.getVersion()); |
| 392 |
System.exit(1); |
| 393 |
} else if (args[i].equalsIgnoreCase("-help") || |
| 394 |
args[i].equalsIgnoreCase("-usage")) { |
| 395 |
System.err.println(message); |
| 396 |
System.exit(1); |
| 397 |
} else if (args[i].equalsIgnoreCase("-draft")) { |
| 398 |
oid = 1; |
| 399 |
} else if (args[i].equalsIgnoreCase("-rfc")) { |
| 400 |
oid = 2; |
| 401 |
} else { |
| 402 |
argError("Argument not recognized: " + args[i]); |
| 403 |
break; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
if (proxyFile == null) { |
| 408 |
error("Proxy file not specified."); |
| 409 |
} |
| 410 |
|
| 411 |
boolean restricted = (policyFile != null || policyLanguage != null); |
| 412 |
|
| 413 |
if (independent) { |
| 414 |
if (oid == 0) { |
| 415 |
error("-old and -independent are exclusive"); |
| 416 |
} |
| 417 |
if (limited) { |
| 418 |
error("-limited and -independent are exclusive"); |
| 419 |
} |
| 420 |
if (restricted) { |
| 421 |
error("-indepedent and -policy or -pl are exclusive"); |
| 422 |
} |
| 423 |
if (oid == 1) { |
| 424 |
proxyType = GSIConstants.CertificateType.GSI_3_INDEPENDENT_PROXY; |
| 425 |
} else { |
| 426 |
// oid has to be 2 |
| 427 |
proxyType = GSIConstants.CertificateType.GSI_4_INDEPENDENT_PROXY; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
if (restricted) { |
| 432 |
if (oid == 0) { |
| 433 |
error("-old and -policy or -pl are exclusive"); |
| 434 |
} |
| 435 |
if (limited) { |
| 436 |
error("-limited and -policy or -pl are exclusive"); |
| 437 |
} |
| 438 |
// XXX: check if proxyType == -1? |
| 439 |
if (oid == 1) { |
| 440 |
proxyType = GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY; |
| 441 |
} else { |
| 442 |
// oid has to be 2 |
| 443 |
proxyType = GSIConstants.CertificateType.GSI_4_RESTRICTED_PROXY; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if (proxyType == GSIConstants.CertificateType.UNDEFINED) { |
| 448 |
if (oid == 1) { |
| 449 |
proxyType = (limited) ? |
| 450 |
GSIConstants.CertificateType.GSI_3_LIMITED_PROXY : |
| 451 |
GSIConstants.CertificateType.GSI_3_IMPERSONATION_PROXY; |
| 452 |
} else if (oid == 2) { |
| 453 |
proxyType = (limited) ? |
| 454 |
GSIConstants.CertificateType.GSI_4_LIMITED_PROXY : |
| 455 |
GSIConstants.CertificateType.GSI_4_IMPERSONATION_PROXY; |
| 456 |
} else { |
| 457 |
proxyType = (limited) ? |
| 458 |
GSIConstants.CertificateType.GSI_2_LIMITED_PROXY : |
| 459 |
GSIConstants.CertificateType.GSI_2_PROXY; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
ProxyInit init = null; |
| 464 |
if (pkcs11) { |
| 465 |
if (keyFile == null) { |
| 466 |
if (certFile == null) { |
| 467 |
keyFile = certFile = properties.getDefaultPKCS11Handle(); |
| 468 |
} else { |
| 469 |
keyFile = certFile; |
| 470 |
} |
| 471 |
} else { |
| 472 |
if (certFile == null) { |
| 473 |
certFile = keyFile; |
| 474 |
} |
| 475 |
} |
| 476 |
try { |
| 477 |
Class iClass = |
| 478 |
Class.forName("org.globus.pkcs11.tools.PKCS11ProxyInit"); |
| 479 |
init = (ProxyInit)iClass.newInstance(); |
| 480 |
} catch (ClassNotFoundException e) { |
| 481 |
System.err.println("Error: Failed to load PKCS11 module."); |
| 482 |
System.exit(-1); |
| 483 |
} catch (InstantiationException e) { |
| 484 |
System.err.println("Error: Failed to instantiate PKCS11 module: " + e.getMessage()); |
| 485 |
System.exit(-1); |
| 486 |
} catch (IllegalAccessException e) { |
| 487 |
System.err.println("Error: Failed to initialize PKCS11 module: " + e.getMessage()); |
| 488 |
System.exit(-1); |
| 489 |
} |
| 490 |
} else { |
| 491 |
if (keyFile == null) { |
| 492 |
keyFile = properties.getUserKeyFile(); |
| 493 |
} |
| 494 |
if (certFile == null) { |
| 495 |
certFile = properties.getUserCertFile(); |
| 496 |
} |
| 497 |
init = new DefaultProxyInit(); |
| 498 |
} |
| 499 |
|
| 500 |
if (debug) { |
| 501 |
System.err.println("Files used: "); |
| 502 |
System.err.println(" proxy : " + ((proxyFile == null) ? "none" : proxyFile)); |
| 503 |
System.err.println(" user key : " + ((keyFile == null) ? "none" : keyFile)); |
| 504 |
System.err.println(" user cert : " + ((certFile == null) ? "none" : certFile)); |
| 505 |
} |
| 506 |
|
| 507 |
CertUtil.init(); |
| 508 |
|
| 509 |
ProxyCertInfo proxyCertInfo = null; |
| 510 |
|
| 511 |
if ((ProxyCertificateUtil.isGsi3Proxy(proxyType)) || |
| 512 |
(ProxyCertificateUtil.isGsi4Proxy(proxyType))) { |
| 513 |
ProxyPolicy policy = null; |
| 514 |
if (ProxyCertificateUtil.isLimitedProxy(proxyType)) { |
| 515 |
policy = new ProxyPolicy(ProxyPolicy.LIMITED); |
| 516 |
} else if (ProxyCertificateUtil.isIndependentProxy(proxyType)) { |
| 517 |
policy = new ProxyPolicy(ProxyPolicy.INDEPENDENT); |
| 518 |
} else if (ProxyCertificateUtil.isImpersonationProxy(proxyType)) { |
| 519 |
// since limited has already been checked, this should work. |
| 520 |
policy = new ProxyPolicy(ProxyPolicy.IMPERSONATION); |
| 521 |
} else if ((proxyType == GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY) || |
| 522 |
(proxyType == GSIConstants.CertificateType.GSI_4_RESTRICTED_PROXY)) { |
| 523 |
if (policyFile == null) { |
| 524 |
error("Policy file required."); |
| 525 |
} |
| 526 |
if (policyLanguage == null) { |
| 527 |
policyLanguage = GENERIC_POLICY_OID; |
| 528 |
} |
| 529 |
byte [] policyData = null; |
| 530 |
try { |
| 531 |
policyData = readPolicyFile(policyFile); |
| 532 |
} catch (IOException e) { |
| 533 |
error("Failed to load policy file: " + e.getMessage()); |
| 534 |
} |
| 535 |
policy = new ProxyPolicy(policyLanguage, |
| 536 |
policyData); |
| 537 |
} else { |
| 538 |
throw new IllegalArgumentException("Invalid proxyType"); |
| 539 |
} |
| 540 |
if (pathLen >= 0) { |
| 541 |
proxyCertInfo = new ProxyCertInfo(pathLen, policy); |
| 542 |
} else { |
| 543 |
proxyCertInfo = new ProxyCertInfo(policy); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
init.setBits(bits); |
| 548 |
init.setLifetime(lifetime); |
| 549 |
|
| 550 |
init.setProxyType(proxyType); |
| 551 |
init.setProxyCertInfo(proxyCertInfo); |
| 552 |
|
| 553 |
init.setDebug(debug); |
| 554 |
init.setQuiet(quiet); |
| 555 |
init.setStdin(stdin); |
| 556 |
|
| 557 |
init.createProxy(certFile, |
| 558 |
keyFile, |
| 559 |
verify, |
| 560 |
globusStyle, |
| 561 |
proxyFile); |
| 562 |
} |
| 563 |
|
| 564 |
private static void argError(String error) { |
| 565 |
System.err.println("Error: " + error); |
| 566 |
System.err.println(); |
| 567 |
System.err.println("Usage: java ProxyInit [-help][-limited][-hours H] ..."); |
| 568 |
System.err.println(); |
| 569 |
System.err.println("Use -help to display full usage"); |
| 570 |
System.exit(1); |
| 571 |
} |
| 572 |
|
| 573 |
protected static void error(String error) { |
| 574 |
System.err.println("Error: " + error); |
| 575 |
System.exit(1); |
| 576 |
} |
| 577 |
|
| 578 |
private static byte[] readPolicyFile(String file) |
| 579 |
throws IOException { |
| 580 |
File f = new File(file); |
| 581 |
FileInputStream in = new FileInputStream(f); |
| 582 |
byte [] data = new byte[(int)f.length()]; |
| 583 |
int left = data.length; |
| 584 |
int off = 0; |
| 585 |
int bytes = 0; |
| 586 |
try { |
| 587 |
while (left > 0) { |
| 588 |
bytes = in.read(data, off, left); |
| 589 |
if (bytes == -1) { |
| 590 |
throw new EOFException(); |
| 591 |
} |
| 592 |
off += bytes; |
| 593 |
left -= bytes; |
| 594 |
} |
| 595 |
} finally { |
| 596 |
if (in != null) { |
| 597 |
in.close(); |
| 598 |
} |
| 599 |
} |
| 600 |
return data; |
| 601 |
} |
| 602 |
|
| 603 |
} |
| 604 |
|
| 605 |
class DefaultProxyInit extends ProxyInit { |
| 606 |
|
| 607 |
private PrivateKey userKey = null; |
| 608 |
|
| 609 |
public void init(String [] args) { |
| 610 |
verify(args[1], "User key"); |
| 611 |
verify(args[0], "User certificate"); |
| 612 |
} |
| 613 |
|
| 614 |
public void verify() throws Exception { |
| 615 |
RSAPublicKey pkey = (RSAPublicKey)getCertificate().getPublicKey(); |
| 616 |
RSAPrivateKey prkey = (RSAPrivateKey)userKey; |
| 617 |
|
| 618 |
if (!pkey.getModulus().equals(prkey.getModulus())) { |
| 619 |
throw new Exception("Certificate and private key specified do not match"); |
| 620 |
} |
| 621 |
|
| 622 |
super.verify(); |
| 623 |
} |
| 624 |
|
| 625 |
private static void verify(String file, String msg) { |
| 626 |
if (file == null) error(msg + " not specified."); |
| 627 |
|
| 628 |
File f = new File(file); |
| 629 |
if (!f.exists() || f.isDirectory()) error(msg + " not found."); |
| 630 |
} |
| 631 |
|
| 632 |
public void loadCertificates(String arg) { |
| 633 |
try { |
| 634 |
certificates = CertificateLoadUtil.loadCertificates(arg); |
| 635 |
} catch(IOException e) { |
| 636 |
System.err.println("Error: Failed to load cert: " + arg); |
| 637 |
System.exit(-1); |
| 638 |
} catch(GeneralSecurityException e) { |
| 639 |
System.err.println("Error: Unable to load user certificate: " + |
| 640 |
e.getMessage()); |
| 641 |
System.exit(-1); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
public void loadKey(String arg) { |
| 646 |
try { |
| 647 |
OpenSSLKey key = new BouncyCastleOpenSSLKey(arg); |
| 648 |
|
| 649 |
if (key.isEncrypted()) { |
| 650 |
String prompt = (quiet) ? |
| 651 |
"Enter GRID pass phrase: " : |
| 652 |
"Enter GRID pass phrase for this identity: "; |
| 653 |
|
| 654 |
String pwd = (stdin) ? |
| 655 |
Util.getInput(prompt) : |
| 656 |
Util.getPrivateInput(prompt); |
| 657 |
|
| 658 |
if (pwd == null) { |
| 659 |
System.exit(1); |
| 660 |
} |
| 661 |
|
| 662 |
key.decrypt(pwd); |
| 663 |
} |
| 664 |
|
| 665 |
userKey = key.getPrivateKey(); |
| 666 |
|
| 667 |
} catch(IOException e) { |
| 668 |
System.err.println("Error: Failed to load key: " + arg); |
| 669 |
System.err.println("Error: " + e.getMessage()); |
| 670 |
System.exit(-1); |
| 671 |
} catch(GeneralSecurityException e) { |
| 672 |
System.err.println("Error: Wrong pass phrase"); |
| 673 |
System.err.println("Error: " + e.getMessage()); |
| 674 |
if (debug) { |
| 675 |
e.printStackTrace(); |
| 676 |
} |
| 677 |
System.exit(-1); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
public void sign() { |
| 682 |
try { |
| 683 |
BouncyCastleCertProcessingFactory factory = |
| 684 |
BouncyCastleCertProcessingFactory.getDefault(); |
| 685 |
|
| 686 |
X509ExtensionSet extSet = null; |
| 687 |
if (proxyCertInfo != null) { |
| 688 |
extSet = new X509ExtensionSet(); |
| 689 |
if (ProxyCertificateUtil.isGsi4Proxy(proxyType)) { |
| 690 |
// RFC compliant OID |
| 691 |
extSet.add(new ProxyCertInfoExtension(proxyCertInfo)); |
| 692 |
} else { |
| 693 |
// old OID |
| 694 |
extSet.add(new GlobusProxyCertInfoExtension(proxyCertInfo)); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
proxy = factory.createCredential(certificates, |
| 699 |
userKey, |
| 700 |
bits, |
| 701 |
lifetime, |
| 702 |
proxyType.getCode(), |
| 703 |
extSet); |
| 704 |
} catch (GeneralSecurityException e) { |
| 705 |
System.err.println("Failed to create a proxy: " + e.getMessage()); |
| 706 |
System.exit(-1); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
} |