Debian Codegraph @

ssl-proxies/src/main/java/org/globus/gsi/bc/BouncyCastleCertProcessingFactory.java

raw
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.gsi.bc;
16
17 import org.bouncycastle.asn1.x500.style.BCStyle;
18 import org.globus.gsi.util.CertificateLoadUtil;
19 import org.globus.gsi.util.ProxyCertificateUtil;
20
21 import org.globus.gsi.X509Credential;
22
23 import org.globus.gsi.VersionUtil;
24 import java.math.BigInteger;
25 import java.security.cert.CertificateException;
26 import java.util.Random;
27 import java.util.GregorianCalendar;
28 import java.util.TimeZone;
29 import java.util.Iterator;
30 import java.util.Calendar;
31 import java.io.InputStream;
32 import java.io.IOException;
33 import java.io.ByteArrayInputStream;
34 import java.security.cert.X509Certificate;
35 import java.security.PrivateKey;
36 import java.security.PublicKey;
37 import java.security.GeneralSecurityException;
38 import java.security.KeyPairGenerator;
39 import java.security.KeyPair;
40 import org.globus.util.I18n;
41 import org.globus.gsi.GlobusCredential;
42 import org.globus.gsi.GSIConstants;
43 import org.globus.gsi.X509ExtensionSet;
44 import org.globus.gsi.proxy.ext.ProxyCertInfo;
45 import org.globus.gsi.proxy.ext.ProxyPolicy;
46 import org.globus.gsi.proxy.ext.ProxyCertInfoExtension;
47 import org.globus.gsi.proxy.ext.GlobusProxyCertInfoExtension;
48 import org.bouncycastle.x509.X509V3CertificateGenerator;
49 import org.bouncycastle.jce.PKCS10CertificationRequest;
50 import org.bouncycastle.jce.provider.X509CertificateObject;
51 import org.bouncycastle.asn1.DERSet;
52 import org.bouncycastle.asn1.DERBitString;
53 import org.bouncycastle.asn1.x509.X509Name;
54 import org.bouncycastle.asn1.ASN1Primitive;
55 import org.bouncycastle.asn1.ASN1InputStream;
56 import org.bouncycastle.asn1.ASN1Sequence;
57 import org.bouncycastle.asn1.x509.Certificate;
58 import org.bouncycastle.asn1.x509.TBSCertificateStructure;
59 import org.bouncycastle.asn1.x509.X509Extensions;
60 import org.bouncycastle.asn1.x509.X509Extension;
61 import org.bouncycastle.asn1.x509.KeyUsage;
62
63 /**
64 * Provides certificate processing API such as creating new certificates, certificate requests, etc.
65 */
66 public class BouncyCastleCertProcessingFactory {
67
68 private static I18n i18n = I18n.getI18n("org.globus.gsi.errors", BouncyCastleCertProcessingFactory.class
69 .getClassLoader());
70
71 private static BouncyCastleCertProcessingFactory factory;
72
73 protected BouncyCastleCertProcessingFactory() {
74 }
75
76 /**
77 * Returns an instance of this class..
78 *
79 * @return <code>BouncyCastleCertProcessingFactory</code> instance.
80 */
81 public static synchronized BouncyCastleCertProcessingFactory getDefault() {
82 if (factory == null) {
83 factory = new BouncyCastleCertProcessingFactory();
84 }
85 return factory;
86 }
87
88 /**
89 * Creates a proxy certificate from the certificate request.
90 *
91 * @see #createCertificate(InputStream, X509Certificate, PrivateKey, int, int, X509ExtensionSet, String)
92 * createCertificate
93 * @deprecated
94 */
95 public X509Certificate createCertificate(InputStream certRequestInputStream, X509Certificate cert,
96 PrivateKey privateKey, int lifetime, int delegationMode) throws IOException, GeneralSecurityException {
97 return createCertificate(certRequestInputStream, cert, privateKey, lifetime, delegationMode,
98 (X509ExtensionSet) null, null);
99 }
100
101 /**
102 * Creates a proxy certificate from the certificate request.
103 *
104 * @see #createCertificate(InputStream, X509Certificate, PrivateKey, int, int, X509ExtensionSet, String)
105 * createCertificate
106 * @deprecated
107 */
108 public X509Certificate createCertificate(InputStream certRequestInputStream, X509Certificate cert,
109 PrivateKey privateKey, int lifetime, int delegationMode, X509ExtensionSet extSet) throws IOException,
110 GeneralSecurityException {
111 return createCertificate(certRequestInputStream, cert, privateKey, lifetime, delegationMode, extSet, null);
112 }
113
114 /**
115 * Creates a proxy certificate from the certificate request. (Signs a certificate request creating a new
116 * certificate)
117 *
118 * @see #createProxyCertificate(X509Certificate, PrivateKey, PublicKey, int, int, X509ExtensionSet,
119 * String) createProxyCertificate
120 * @param certRequestInputStream
121 * the input stream to read the certificate request from.
122 * @param cert
123 * the issuer certificate
124 * @param privateKey
125 * the private key to sign the new certificate with.
126 * @param lifetime
127 * lifetime of the new certificate in seconds. If 0 (or less then) the new certificate will
128 * have the same lifetime as the issuing certificate.
129 * @param delegationMode
130 * the type of proxy credential to create
131 * @param extSet
132 * a set of X.509 extensions to be included in the new proxy certificate. Can be null. If
133 * delegation mode is {@link org.globus.gsi.GSIConstants.CertificateType#GSI_3_RESTRICTED_PROXY
134 * GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY} or {@link org.globus.gsi.GSIConstants.CertificateType#GSI_4_RESTRICTED_PROXY
135 * GSIConstants.CertificateType.GSI_4_RESTRICTED_PROXY} then
136 * {@link org.globus.gsi.proxy.ext.ProxyCertInfoExtension ProxyCertInfoExtension} must be
137 * present in the extension set.
138 * @param cnValue
139 * the value of the CN component of the subject of the new certificate. If null, the defaults
140 * will be used depending on the proxy certificate type created.
141 * @return <code>X509Certificate</code> the new proxy certificate
142 * @exception IOException
143 * if error reading the certificate request
144 * @exception GeneralSecurityException
145 * if a security error occurs.
146 * @deprecated
147 */
148 public X509Certificate createCertificate(InputStream certRequestInputStream, X509Certificate cert,
149 PrivateKey privateKey, int lifetime, int delegationMode, X509ExtensionSet extSet, String cnValue)
150 throws IOException, GeneralSecurityException {
151
152 ASN1InputStream derin = new ASN1InputStream(certRequestInputStream);
153 ASN1Primitive reqInfo = derin.readObject();
154 PKCS10CertificationRequest certReq = new PKCS10CertificationRequest((ASN1Sequence) reqInfo);
155
156 boolean rs = certReq.verify();
157
158 if (!rs) {
159 String err = i18n.getMessage("certReqVerification");
160 throw new GeneralSecurityException(err);
161 }
162
163 return createProxyCertificate(cert, privateKey, certReq.getPublicKey(), lifetime, delegationMode, extSet,
164 cnValue);
165 }
166
167 /**
168 * Creates a new proxy credential from the specified certificate chain and a private key.
169 *
170 * @see #createCredential(X509Certificate[], PrivateKey, int, int, int, X509ExtensionSet, String)
171 * createCredential
172 * @deprecated
173 */
174 public GlobusCredential createCredential(X509Certificate[] certs, PrivateKey privateKey, int bits, int lifetime,
175 int delegationMode) throws GeneralSecurityException {
176 return createCredential(certs, privateKey, bits, lifetime, delegationMode, (X509ExtensionSet) null, null);
177 }
178
179 /**
180 * Creates a new proxy credential from the specified certificate chain and a private key.
181 *
182 * @see #createCredential(X509Certificate[], PrivateKey, int, int, int, X509ExtensionSet, String)
183 * createCredential
184 * @deprecated
185 */
186 public GlobusCredential createCredential(X509Certificate[] certs, PrivateKey privateKey, int bits, int lifetime,
187 int delegationMode, X509ExtensionSet extSet) throws GeneralSecurityException {
188 return createCredential(certs, privateKey, bits, lifetime, delegationMode, extSet, null);
189 }
190
191 /**
192 * Creates a new proxy credential from the specified certificate chain and a private key. A set of X.509
193 * extensions can be optionally included in the new proxy certificate. This function automatically creates
194 * a "RSA"-based key pair.
195 *
196 * @see #createProxyCertificate(X509Certificate, PrivateKey, PublicKey, int, int, X509ExtensionSet,
197 * String) createProxyCertificate
198 * @param certs
199 * the certificate chain for the new proxy credential. The top-most certificate
200 * <code>cert[0]</code> will be designated as the issuing certificate.
201 * @param privateKey
202 * the private key of the issuing certificate. The new proxy certificate will be signed with
203 * that private key.
204 * @param bits
205 * the strength of the key pair for the new proxy certificate.
206 * @param lifetime
207 * lifetime of the new certificate in seconds. If 0 (or less then) the new certificate will
208 * have the same lifetime as the issuing certificate.
209 * @param delegationMode
210 * the type of proxy credential to create
211 * @param extSet
212 * a set of X.509 extensions to be included in the new proxy certificate. Can be null. If
213 * delegation mode is {@link org.globus.gsi.GSIConstants.CertificateType#GSI_3_RESTRICTED_PROXY
214 * GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY} or {@link org.globus.gsi.GSIConstants.CertificateType#GSI_4_RESTRICTED_PROXY
215 * GSIConstants.CertificateType.GSI_4_RESTRICTED_PROXY} then
216 * {@link org.globus.gsi.proxy.ext.ProxyCertInfoExtension ProxyCertInfoExtension} must be
217 * present in the extension set.
218 * @param cnValue
219 * the value of the CN component of the subject of the new proxy credential. If null, the
220 * defaults will be used depending on the proxy certificate type created.
221 * @return <code>GlobusCredential</code> the new proxy credential.
222 * @exception GeneralSecurityException
223 * if a security error occurs.
224 * @deprecated
225 */
226 public GlobusCredential createCredential(X509Certificate[] certs, PrivateKey privateKey, int bits, int lifetime,
227 int delegationMode, X509ExtensionSet extSet, String cnValue) throws GeneralSecurityException {
228
229 X509Certificate[] bcCerts = getX509CertificateObjectChain(certs);
230
231 KeyPairGenerator keyGen = null;
232 keyGen = KeyPairGenerator.getInstance("RSA", "BC");
233 keyGen.initialize(bits);
234 KeyPair keyPair = keyGen.genKeyPair();
235
236 X509Certificate newCert = createProxyCertificate(bcCerts[0], privateKey, keyPair.getPublic(), lifetime,
237 delegationMode, extSet, cnValue);
238
239 X509Certificate[] newCerts = new X509Certificate[bcCerts.length + 1];
240 newCerts[0] = newCert;
241 System.arraycopy(certs, 0, newCerts, 1, certs.length);
242
243 return new GlobusCredential(keyPair.getPrivate(), newCerts);
244 }
245
246 /**
247 * Creates a proxy certificate. A set of X.509 extensions can be optionally included in the new proxy
248 * certificate. <BR>
249 * If a GSI-2 proxy is created, the serial number of the proxy certificate will be the same as of the
250 * issuing certificate. Also, none of the extensions in the issuing certificate will be copied into the
251 * proxy certificate.<BR>
252 * If a GSI-3 or GSI 4 proxy is created, the serial number of the proxy certificate will be picked
253 * randomly. If the issuing certificate contains a <i>KeyUsage</i> extension, the extension will be copied
254 * into the proxy certificate with <i>keyCertSign</i> and <i>nonRepudiation</i> bits turned off. No other
255 * extensions are currently copied.
256 *
257 * The methods defaults to creating GSI 4 proxy
258 *
259 * @param issuerCert_
260 * the issuing certificate
261 * @param issuerKey
262 * private key matching the public key of issuer certificate. The new proxy certificate will be
263 * signed by that key.
264 * @param publicKey
265 * the public key of the new certificate
266 * @param lifetime
267 * lifetime of the new certificate in seconds. If 0 (or less then) the new certificate will
268 * have the same lifetime as the issuing certificate.
269 * @param proxyType
270 * can be one of {@link GSIConstants#DELEGATION_LIMITED GSIConstants.DELEGATION_LIMITED},
271 * {@link GSIConstants#DELEGATION_FULL GSIConstants.DELEGATION_FULL},
272 *
273 * {@link GSIConstants#GSI_2_LIMITED_PROXY GSIConstants.GSI_2_LIMITED_PROXY},
274 * {@link GSIConstants#GSI_2_PROXY GSIConstants.GSI_2_PROXY},
275 * {@link GSIConstants#GSI_3_IMPERSONATION_PROXY GSIConstants.GSI_3_IMPERSONATION_PROXY},
276 * {@link GSIConstants#GSI_3_LIMITED_PROXY GSIConstants.GSI_3_LIMITED_PROXY},
277 * {@link GSIConstants#GSI_3_INDEPENDENT_PROXY GSIConstants.GSI_3_INDEPENDENT_PROXY},
278 * {@link GSIConstants#GSI_3_RESTRICTED_PROXY GSIConstants.GSI_3_RESTRICTED_PROXY}.
279 * {@link GSIConstants#GSI_4_IMPERSONATION_PROXY GSIConstants.GSI_4_IMPERSONATION_PROXY},
280 * {@link GSIConstants#GSI_4_LIMITED_PROXY GSIConstants.GSI_3_LIMITED_PROXY},
281 * {@link GSIConstants#GSI_4_INDEPENDENT_PROXY GSIConstants.GSI_4_INDEPENDENT_PROXY},
282 * {@link GSIConstants#GSI_4_RESTRICTED_PROXY GSIConstants.GSI_4_RESTRICTED_PROXY}.
283 *
284 * If {@link GSIConstants#DELEGATION_LIMITED GSIConstants.DELEGATION_LIMITED} and if
285 * {@link VersionUtil#isGsi2Enabled() CertUtil.isGsi2Enabled} returns true then a GSI-2 limited
286 * proxy will be created. Else if {@link VersionUtil#isGsi3Enabled() CertUtil.isGsi3Enabled}
287 * returns true then a GSI-3 limited proxy will be created. If not, a GSI-4 limited proxy will
288 * be created.
289 *
290 * If {@link GSIConstants#DELEGATION_FULL GSIConstants.DELEGATION_FULL} and if
291 * {@link VersionUtil#isGsi2Enabled() CertUtil.isGsi2Enabled} returns true then a GSI-2 full proxy
292 * will be created. Else if {@link VersionUtil#isGsi3Enabled() CertUtil.isGsi3Enabled} returns
293 * true then a GSI-3 full proxy will be created. If not, a GSI-4 full proxy will be created.
294 *
295 * @param extSet
296 * a set of X.509 extensions to be included in the new proxy certificate. Can be null. If
297 * delegation mode is {@link GSIConstants#GSI_3_RESTRICTED_PROXY
298 * GSIConstants.GSI_3_RESTRICTED_PROXY} or {@link GSIConstants#GSI_4_RESTRICTED_PROXY
299 * GSIConstants.GSI_4_RESTRICTED_PROXY} then
300 * {@link org.globus.gsi.proxy.ext.ProxyCertInfoExtension ProxyCertInfoExtension} must be
301 * present in the extension set.
302 *
303 * @param cnValue
304 * the value of the CN component of the subject of the new certificate. If null, the defaults
305 * will be used depending on the proxy certificate type created.
306 * @return <code>X509Certificate</code> the new proxy certificate.
307 * @exception GeneralSecurityException
308 * if a security error occurs.
309 * @deprecated
310 */
311 public X509Certificate createProxyCertificate(X509Certificate issuerCert_, PrivateKey issuerKey,
312 PublicKey publicKey, int lifetime, int proxyType, X509ExtensionSet extSet, String cnValue)
313 throws GeneralSecurityException {
314
315 X509Certificate issuerCert = issuerCert_;
316 if (!(issuerCert_ instanceof X509CertificateObject)) {
317 issuerCert = CertificateLoadUtil.loadCertificate(new ByteArrayInputStream(issuerCert.getEncoded()));
318 }
319
320 if (proxyType == GSIConstants.DELEGATION_LIMITED) {
321 GSIConstants.CertificateType type = BouncyCastleUtil.getCertificateType(issuerCert);
322 if (ProxyCertificateUtil.isGsi4Proxy(type)) {
323 proxyType = GSIConstants.GSI_4_LIMITED_PROXY;
324 } else if (ProxyCertificateUtil.isGsi3Proxy(type)) {
325 proxyType = GSIConstants.GSI_3_LIMITED_PROXY;
326 } else if (ProxyCertificateUtil.isGsi2Proxy(type)) {
327 proxyType = GSIConstants.GSI_2_LIMITED_PROXY;
328 } else {
329 // default to RFC compliant proxy
330 if (VersionUtil.isGsi2Enabled()) {
331 proxyType = GSIConstants.GSI_2_LIMITED_PROXY;
332 } else {
333 proxyType = VersionUtil.isGsi3Enabled() ? GSIConstants.GSI_3_LIMITED_PROXY
334 : GSIConstants.GSI_4_LIMITED_PROXY;
335 }
336 }
337 } else if (proxyType == GSIConstants.DELEGATION_FULL) {
338 GSIConstants.CertificateType type = BouncyCastleUtil.getCertificateType(issuerCert);
339 if (ProxyCertificateUtil.isGsi4Proxy(type)) {
340 proxyType = GSIConstants.GSI_4_IMPERSONATION_PROXY;
341 } else if (ProxyCertificateUtil.isGsi3Proxy(type)) {
342 proxyType = GSIConstants.GSI_3_IMPERSONATION_PROXY;
343 } else if (ProxyCertificateUtil.isGsi2Proxy(type)) {
344 proxyType = GSIConstants.GSI_2_PROXY;
345 } else {
346 // Default to RFC complaint proxy
347 if (VersionUtil.isGsi2Enabled()) {
348 proxyType = GSIConstants.GSI_2_PROXY;
349 } else {
350 proxyType = (VersionUtil.isGsi3Enabled()) ? GSIConstants.GSI_3_IMPERSONATION_PROXY
351 : GSIConstants.GSI_4_IMPERSONATION_PROXY;
352 }
353 }
354 }
355
356 X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
357
358 org.globus.gsi.X509Extension x509Ext = null;
359 BigInteger serialNum = null;
360 String delegDN = null;
361
362 if (ProxyCertificateUtil.isGsi3Proxy(GSIConstants.CertificateType.get(proxyType)) ||
363 ProxyCertificateUtil.isGsi4Proxy(GSIConstants.CertificateType.get(proxyType))) {
364 Random rand = new Random();
365 delegDN = String.valueOf(Math.abs(rand.nextInt()));
366 serialNum = new BigInteger(20, rand);
367
368 if (extSet != null) {
369 x509Ext = extSet.get(ProxyCertInfo.OID.getId());
370 if (x509Ext == null) {
371 x509Ext = extSet.get(ProxyCertInfo.OLD_OID.getId());
372 }
373 }
374
375 if (x509Ext == null) {
376 // create ProxyCertInfo extension
377 ProxyPolicy policy = null;
378 if (ProxyCertificateUtil.isLimitedProxy(GSIConstants.CertificateType.get(proxyType))) {
379 policy = new ProxyPolicy(ProxyPolicy.LIMITED);
380 } else if (ProxyCertificateUtil.isIndependentProxy(GSIConstants.CertificateType.get(proxyType))) {
381 policy = new ProxyPolicy(ProxyPolicy.INDEPENDENT);
382 } else if (ProxyCertificateUtil.isImpersonationProxy(GSIConstants.CertificateType.get(proxyType))) {
383 // since limited has already been checked, this should work.
384 policy = new ProxyPolicy(ProxyPolicy.IMPERSONATION);
385 } else if ((proxyType == GSIConstants.GSI_3_RESTRICTED_PROXY)
386 || (proxyType == GSIConstants.GSI_4_RESTRICTED_PROXY)) {
387 String err = i18n.getMessage("restrictProxy");
388 throw new IllegalArgumentException(err);
389 } else {
390 String err = i18n.getMessage("invalidProxyType");
391 throw new IllegalArgumentException(err);
392 }
393
394 ProxyCertInfo proxyCertInfo = new ProxyCertInfo(policy);
395 x509Ext = new ProxyCertInfoExtension(proxyCertInfo);
396 if (ProxyCertificateUtil.isGsi4Proxy(GSIConstants.CertificateType.get(proxyType))) {
397 // RFC compliant OID
398 x509Ext = new ProxyCertInfoExtension(proxyCertInfo);
399 } else {
400 // old OID
401 x509Ext = new GlobusProxyCertInfoExtension(proxyCertInfo);
402 }
403 }
404
405 try {
406 // add ProxyCertInfo extension to the new cert
407 certGen.addExtension(x509Ext.getOid(), x509Ext.isCritical(), x509Ext.getValue());
408
409 // handle KeyUsage in issuer cert
410 TBSCertificateStructure crt = BouncyCastleUtil.getTBSCertificateStructure(issuerCert);
411
412 X509Extensions extensions = crt.getExtensions();
413 if (extensions != null) {
414 X509Extension ext;
415
416 // handle key usage ext
417 ext = extensions.getExtension(X509Extension.keyUsage);
418 if (ext != null) {
419
420 // TBD: handle this better
421 if (extSet != null && (extSet.get(X509Extension.keyUsage.getId()) != null)) {
422 String err = i18n.getMessage("keyUsageExt");
423 throw new GeneralSecurityException(err);
424 }
425
426 DERBitString bits = (DERBitString) BouncyCastleUtil.getExtensionObject(ext);
427
428 byte[] bytes = bits.getBytes();
429
430 // make sure they are disabled
431 if ((bytes[0] & KeyUsage.nonRepudiation) != 0) {
432 bytes[0] ^= KeyUsage.nonRepudiation;
433 }
434
435 if ((bytes[0] & KeyUsage.keyCertSign) != 0) {
436 bytes[0] ^= KeyUsage.keyCertSign;
437 }
438
439 bits = new DERBitString(bytes, bits.getPadBits());
440
441 certGen.addExtension(X509Extension.keyUsage, ext.isCritical(), bits);
442 }
443 }
444
445 } catch (IOException e) {
446 // but this should not happen
447 throw new GeneralSecurityException(e.getMessage());
448 }
449
450 } else if (proxyType == GSIConstants.GSI_2_LIMITED_PROXY) {
451 delegDN = "limited proxy";
452 serialNum = issuerCert.getSerialNumber();
453 } else if (proxyType == GSIConstants.GSI_2_PROXY) {
454 delegDN = "proxy";
455 serialNum = issuerCert.getSerialNumber();
456 } else {
457 String err = i18n.getMessage("unsupportedProxy", Integer.toString(proxyType));
458 throw new IllegalArgumentException(err);
459 }
460
461 // add specified extensions
462 if (extSet != null) {
463 Iterator iter = extSet.oidSet().iterator();
464 while (iter.hasNext()) {
465 String oid = (String) iter.next();
466 // skip ProxyCertInfo extension
467 if (oid.equals(ProxyCertInfo.OID.getId()) || oid.equals(ProxyCertInfo.OLD_OID.getId())) {
468 continue;
469 }
470 x509Ext = (org.globus.gsi.X509Extension) extSet.get(oid);
471 certGen.addExtension(x509Ext.getOid(), x509Ext.isCritical(), x509Ext.getValue());
472 }
473 }
474
475 X509Name issuerDN;
476 if (issuerCert.getSubjectDN() instanceof X509Name) {
477 issuerDN = (X509Name)issuerCert.getSubjectDN();
478 } else {
479 issuerDN = new X509Name(true,issuerCert.getSubjectX500Principal().getName());
480 }
481
482 X509NameHelper issuer = new X509NameHelper(issuerDN);
483
484 X509NameHelper subject = new X509NameHelper(issuerDN);
485 subject.add(BCStyle.CN, (cnValue == null) ? delegDN : cnValue);
486
487 certGen.setSubjectDN(subject.getAsName());
488 certGen.setIssuerDN(issuer.getAsName());
489
490 certGen.setSerialNumber(serialNum);
491 certGen.setPublicKey(publicKey);
492 certGen.setSignatureAlgorithm(issuerCert.getSigAlgName());
493
494 GregorianCalendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
495 /* Allow for a five minute clock skew here. */
496 date.add(Calendar.MINUTE, -5);
497 certGen.setNotBefore(date.getTime());
498
499 /* If hours = 0, then cert lifetime is set to user cert */
500 if (lifetime <= 0) {
501 certGen.setNotAfter(issuerCert.getNotAfter());
502 } else {
503 date.add(Calendar.MINUTE, 5);
504 date.add(Calendar.SECOND, lifetime);
505 certGen.setNotAfter(date.getTime());
506 }
507
508
509
510 return certGen.generateX509Certificate(issuerKey);
511 }
512
513 /**
514 * Creates a proxy certificate from the certificate request.
515 *
516 * @see #createCertificate(InputStream, X509Certificate, PrivateKey, int, int, X509ExtensionSet, String)
517 * createCertificate
518 */
519 public X509Certificate createCertificate(InputStream certRequestInputStream, X509Certificate cert,
520 PrivateKey privateKey, int lifetime, GSIConstants.CertificateType certType) throws IOException,
521 GeneralSecurityException {
522 return createCertificate(certRequestInputStream, cert, privateKey, lifetime, certType, (X509ExtensionSet) null,
523 null);
524 }
525
526 /**
527 * Creates a proxy certificate from the certificate request.
528 *
529 * @see #createCertificate(InputStream, X509Certificate, PrivateKey, int, GSIConstants.CertificateType, X509ExtensionSet, String)
530 * createCertificate
531 */
532 public X509Certificate createCertificate(InputStream certRequestInputStream, X509Certificate cert,
533 PrivateKey privateKey, int lifetime, GSIConstants.CertificateType certType, X509ExtensionSet extSet)
534 throws IOException, GeneralSecurityException {
535 return createCertificate(certRequestInputStream, cert, privateKey, lifetime, certType, extSet, null);
536 }
537
538 /**
539 * Creates a proxy certificate from the certificate request. (Signs a certificate request creating a new
540 * certificate)
541 *
542 * @see #createProxyCertificate(X509Certificate, PrivateKey, PublicKey, int, int, X509ExtensionSet,
543 * String) createProxyCertificate
544 * @param certRequestInputStream
545 * the input stream to read the certificate request from.
546 * @param cert
547 * the issuer certificate
548 * @param privateKey
549 * the private key to sign the new certificate with.
550 * @param lifetime
551 * lifetime of the new certificate in seconds. If 0 (or less then) the new certificate will
552 * have the same lifetime as the issuing certificate.
553 * @param certType
554 * the type of proxy credential to create
555 * @param extSet
556 * a set of X.509 extensions to be included in the new proxy certificate. Can be null. If
557 * delegation mode is {@link org.globus.gsi.GSIConstants.CertificateType#GSI_3_RESTRICTED_PROXY
558 * GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY} or {@link org.globus.gsi.GSIConstants.CertificateType#GSI_4_RESTRICTED_PROXY
559 * GSIConstants.CertificateType.GSI_4_RESTRICTED_PROXY} then
560 * {@link org.globus.gsi.proxy.ext.ProxyCertInfoExtension ProxyCertInfoExtension} must be
561 * present in the extension set.
562 * @param cnValue
563 * the value of the CN component of the subject of the new certificate. If null, the defaults
564 * will be used depending on the proxy certificate type created.
565 * @return <code>X509Certificate</code> the new proxy certificate
566 * @exception IOException
567 * if error reading the certificate request
568 * @exception GeneralSecurityException
569 * if a security error occurs.
570 */
571 public X509Certificate createCertificate(InputStream certRequestInputStream, X509Certificate cert,
572 PrivateKey privateKey, int lifetime, GSIConstants.CertificateType certType, X509ExtensionSet extSet,
573 String cnValue) throws IOException, GeneralSecurityException {
574
575 ASN1InputStream derin = new ASN1InputStream(certRequestInputStream);
576 ASN1Primitive reqInfo = derin.readObject();
577 PKCS10CertificationRequest certReq = new PKCS10CertificationRequest((ASN1Sequence) reqInfo);
578
579 boolean rs = certReq.verify();
580
581 if (!rs) {
582 String err = i18n.getMessage("certReqVerification");
583 throw new GeneralSecurityException(err);
584 }
585
586 return createProxyCertificate(cert, privateKey, certReq.getPublicKey(), lifetime, certType, extSet, cnValue);
587 }
588
589 /**
590 * Creates a new proxy credential from the specified certificate chain and a private key.
591 *
592 * @see #createCredential(X509Certificate[], PrivateKey, int, int, GSIConstants.CertificateType, X509ExtensionSet, String)
593 * createCredential
594 */
595 public X509Credential createCredential(X509Certificate[] certs, PrivateKey privateKey, int bits, int lifetime,
596 GSIConstants.CertificateType certType) throws GeneralSecurityException {
597 return createCredential(certs, privateKey, bits, lifetime, certType, (X509ExtensionSet) null, null);
598 }
599
600 /**
601 * Creates a new proxy credential from the specified certificate chain and a private key.
602 *
603 * @see #createCredential(X509Certificate[], PrivateKey, int, int, GSIConstants.CertificateType, X509ExtensionSet, String)
604 * createCredential
605 */
606 public X509Credential createCredential(X509Certificate[] certs, PrivateKey privateKey, int bits, int lifetime,
607 GSIConstants.CertificateType certType, X509ExtensionSet extSet) throws GeneralSecurityException {
608 return createCredential(certs, privateKey, bits, lifetime, certType, extSet, null);
609 }
610
611 /**
612 * Creates a new proxy credential from the specified certificate chain and a private key. A set of X.509
613 * extensions can be optionally included in the new proxy certificate. This function automatically creates
614 * a "RSA"-based key pair.
615 *
616 * @see #createProxyCertificate(X509Certificate, PrivateKey, PublicKey, int, int, X509ExtensionSet,
617 * String) createProxyCertificate
618 * @param certs
619 * the certificate chain for the new proxy credential. The top-most certificate
620 * <code>cert[0]</code> will be designated as the issuing certificate.
621 * @param privateKey
622 * the private key of the issuing certificate. The new proxy certificate will be signed with
623 * that private key.
624 * @param bits
625 * the strength of the key pair for the new proxy certificate.
626 * @param lifetime
627 * lifetime of the new certificate in seconds. If 0 (or less then) the new certificate will
628 * have the same lifetime as the issuing certificate.
629 * @param certType
630 * the type of proxy credential to create
631 * @param extSet
632 * a set of X.509 extensions to be included in the new proxy certificate. Can be null. If
633 * delegation mode is {@link org.globus.gsi.GSIConstants.CertificateType#GSI_3_RESTRICTED_PROXY
634 * GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY} or {@link org.globus.gsi.GSIConstants.CertificateType#GSI_4_RESTRICTED_PROXY
635 * GSIConstants.CertificateType.GSI_4_RESTRICTED_PROXY} then
636 * {@link org.globus.gsi.proxy.ext.ProxyCertInfoExtension ProxyCertInfoExtension} must be
637 * present in the extension set.
638 * @param cnValue
639 * the value of the CN component of the subject of the new proxy credential. If null, the
640 * defaults will be used depending on the proxy certificate type created.
641 * @return <code>GlobusCredential</code> the new proxy credential.
642 * @exception GeneralSecurityException
643 * if a security error occurs.
644 */
645 public X509Credential createCredential(X509Certificate[] certs, PrivateKey privateKey, int bits, int lifetime,
646 GSIConstants.CertificateType certType, X509ExtensionSet extSet, String cnValue) throws GeneralSecurityException {
647
648 X509Certificate[] bcCerts = getX509CertificateObjectChain(certs);
649
650 KeyPairGenerator keyGen = null;
651 keyGen = KeyPairGenerator.getInstance("RSA", "BC");
652 keyGen.initialize(bits);
653 KeyPair keyPair = keyGen.genKeyPair();
654
655 X509Certificate newCert = createProxyCertificate(bcCerts[0], privateKey, keyPair.getPublic(), lifetime,
656 certType, extSet, cnValue);
657
658 X509Certificate[] newCerts = new X509Certificate[bcCerts.length + 1];
659 newCerts[0] = newCert;
660 System.arraycopy(certs, 0, newCerts, 1, certs.length);
661
662 return new X509Credential(keyPair.getPrivate(), newCerts);
663 }
664
665 /**
666 * Creates a new proxy credential from the specified certificate chain and a private key,
667 * using the given delegation mode.
668 *
669 * @see #createCredential(X509Certificate[], PrivateKey, int, int, GSIConstants.CertificateType, X509ExtensionSet, String)
670 * createCredential
671 */
672 public X509Credential createCredential(X509Certificate[] certs, PrivateKey privateKey, int bits, int lifetime,
673 GSIConstants.DelegationType delegType) throws GeneralSecurityException {
674 return createCredential(certs, privateKey, bits, lifetime, delegType, (X509ExtensionSet) null, null);
675 }
676
677 /**
678 * Creates a new proxy credential from the specified certificate chain and a private key,
679 * using the given delegation mode.
680 *
681 * @see #createCredential(X509Certificate[], PrivateKey, int, int, GSIConstants.CertificateType, X509ExtensionSet, String)
682 * createCredential
683 */
684 public X509Credential createCredential(X509Certificate[] certs, PrivateKey privateKey, int bits, int lifetime,
685 GSIConstants.DelegationType delegType, X509ExtensionSet extSet) throws GeneralSecurityException {
686 return createCredential(certs, privateKey, bits, lifetime, delegType, extSet, null);
687 }
688
689 /**
690 * Creates a new proxy credential from the specified certificate chain and a private key,
691 * using the given delegation mode.
692 * @see #createCredential(X509Certificate[], PrivateKey, int, int, GSIConstants.CertificateType, X509ExtensionSet, String)
693 */
694 public X509Credential createCredential(X509Certificate[] certs, PrivateKey privateKey, int bits, int lifetime,
695 GSIConstants.DelegationType delegType, X509ExtensionSet extSet, String cnValue) throws GeneralSecurityException {
696
697 X509Certificate[] bcCerts = getX509CertificateObjectChain(certs);
698
699 return createCredential(bcCerts, privateKey, bits, lifetime, decideProxyType(bcCerts[0], delegType), extSet, cnValue);
700 }
701
702 /**
703 * Creates a proxy certificate. A set of X.509 extensions can be optionally included in the new proxy
704 * certificate. <BR>
705 * If a GSI-2 proxy is created, the serial number of the proxy certificate will be the same as of the
706 * issuing certificate. Also, none of the extensions in the issuing certificate will be copied into the
707 * proxy certificate.<BR>
708 * If a GSI-3 or GSI 4 proxy is created, the serial number of the proxy certificate will be picked
709 * randomly. If the issuing certificate contains a <i>KeyUsage</i> extension, the extension will be copied
710 * into the proxy certificate with <i>keyCertSign</i> and <i>nonRepudiation</i> bits turned off. No other
711 * extensions are currently copied.
712 *
713 * The methods defaults to creating GSI 4 proxy
714 *
715 * @param issuerCert_
716 * the issuing certificate
717 * @param issuerKey
718 * private key matching the public key of issuer certificate. The new proxy certificate will be
719 * signed by that key.
720 * @param publicKey
721 * the public key of the new certificate
722 * @param lifetime
723 * lifetime of the new certificate in seconds. If 0 (or less then) the new certificate will
724 * have the same lifetime as the issuing certificate.
725 * @param certType
726 * can be one of {@link org.globus.gsi.GSIConstants.CertificateType#GSI_2_LIMITED_PROXY GSIConstants.CertificateType.GSI_2_LIMITED_PROXY},
727 * {@link org.globus.gsi.GSIConstants.CertificateType#GSI_2_PROXY GSIConstants.CertificateType.GSI_2_PROXY},
728 * {@link org.globus.gsi.GSIConstants.CertificateType#GSI_3_IMPERSONATION_PROXY GSIConstants.CertificateType.GSI_3_IMPERSONATION_PROXY},
729 * {@link org.globus.gsi.GSIConstants.CertificateType#GSI_3_LIMITED_PROXY GSIConstants.CertificateType.GSI_3_LIMITED_PROXY},
730 * {@link org.globus.gsi.GSIConstants.CertificateType#GSI_3_INDEPENDENT_PROXY GSIConstants.CertificateType.GSI_3_INDEPENDENT_PROXY},
731 * {@link org.globus.gsi.GSIConstants.CertificateType#GSI_3_RESTRICTED_PROXY GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY}.
732 * {@link org.globus.gsi.GSIConstants.CertificateType#GSI_4_IMPERSONATION_PROXY GSIConstants.CertificateType.GSI_4_IMPERSONATION_PROXY},
733 * {@link org.globus.gsi.GSIConstants.CertificateType#GSI_4_LIMITED_PROXY GSIConstants.CertificateType.GSI_3_LIMITED_PROXY},
734 * {@link org.globus.gsi.GSIConstants.CertificateType#GSI_4_INDEPENDENT_PROXY GSIConstants.CertificateType.GSI_4_INDEPENDENT_PROXY},
735 * {@link org.globus.gsi.GSIConstants.CertificateType#GSI_4_RESTRICTED_PROXY GSIConstants.CertificateType.GSI_4_RESTRICTED_PROXY}.
736 *
737 * @param extSet
738 * a set of X.509 extensions to be included in the new proxy certificate. Can be null. If
739 * delegation mode is {@link org.globus.gsi.GSIConstants.CertificateType#GSI_3_RESTRICTED_PROXY
740 * GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY} or {@link org.globus.gsi.GSIConstants.CertificateType#GSI_4_RESTRICTED_PROXY
741 * GSIConstants.CertificateType.GSI_4_RESTRICTED_PROXY} then
742 * {@link org.globus.gsi.proxy.ext.ProxyCertInfoExtension ProxyCertInfoExtension} must be
743 * present in the extension set.
744 *
745 * @param cnValue
746 * the value of the CN component of the subject of the new certificate. If null, the defaults
747 * will be used depending on the proxy certificate type created.
748 * @return <code>X509Certificate</code> the new proxy certificate.
749 * @exception GeneralSecurityException
750 * if a security error occurs.
751 */
752 public X509Certificate createProxyCertificate(X509Certificate issuerCert_, PrivateKey issuerKey,
753 PublicKey publicKey, int lifetime, GSIConstants.CertificateType certType, X509ExtensionSet extSet,
754 String cnValue) throws GeneralSecurityException {
755
756 X509Certificate issuerCert = issuerCert_;
757 if (!(issuerCert_ instanceof X509CertificateObject)) {
758 issuerCert = CertificateLoadUtil.loadCertificate(new ByteArrayInputStream(issuerCert.getEncoded()));
759 }
760
761 X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
762
763 org.globus.gsi.X509Extension x509Ext = null;
764 BigInteger serialNum = null;
765 String delegDN = null;
766
767 if (ProxyCertificateUtil.isGsi3Proxy(certType) || ProxyCertificateUtil.isGsi4Proxy(certType)) {
768 Random rand = new Random();
769 delegDN = String.valueOf(Math.abs(rand.nextInt()));
770 serialNum = new BigInteger(20, rand);
771
772 if (extSet != null) {
773 x509Ext = extSet.get(ProxyCertInfo.OID.getId());
774 if (x509Ext == null) {
775 x509Ext = extSet.get(ProxyCertInfo.OLD_OID.getId());
776 }
777 }
778
779 if (x509Ext == null) {
780 // create ProxyCertInfo extension
781 ProxyPolicy policy = null;
782 if (ProxyCertificateUtil.isLimitedProxy(certType)) {
783 policy = new ProxyPolicy(ProxyPolicy.LIMITED);
784 } else if (ProxyCertificateUtil.isIndependentProxy(certType)) {
785 policy = new ProxyPolicy(ProxyPolicy.INDEPENDENT);
786 } else if (ProxyCertificateUtil.isImpersonationProxy(certType)) {
787 // since limited has already been checked, this should work.
788 policy = new ProxyPolicy(ProxyPolicy.IMPERSONATION);
789 } else if ((certType == GSIConstants.CertificateType.GSI_3_RESTRICTED_PROXY)
790 || (certType == GSIConstants.CertificateType.GSI_4_RESTRICTED_PROXY)) {
791 String err = i18n.getMessage("restrictProxy");
792 throw new IllegalArgumentException(err);
793 } else {
794 String err = i18n.getMessage("invalidProxyType");
795 throw new IllegalArgumentException(err);
796 }
797
798 ProxyCertInfo proxyCertInfo = new ProxyCertInfo(policy);
799 x509Ext = new ProxyCertInfoExtension(proxyCertInfo);
800 if (ProxyCertificateUtil.isGsi4Proxy(certType)) {
801 // RFC compliant OID
802 x509Ext = new ProxyCertInfoExtension(proxyCertInfo);
803 } else {
804 // old OID
805 x509Ext = new GlobusProxyCertInfoExtension(proxyCertInfo);
806 }
807 }
808
809 try {
810 // add ProxyCertInfo extension to the new cert
811 certGen.addExtension(x509Ext.getOid(), x509Ext.isCritical(), x509Ext.getValue());
812
813 // handle KeyUsage in issuer cert
814 TBSCertificateStructure crt = BouncyCastleUtil.getTBSCertificateStructure(issuerCert);
815
816 X509Extensions extensions = crt.getExtensions();
817 if (extensions != null) {
818 X509Extension ext;
819
820 // handle key usage ext
821 ext = extensions.getExtension(X509Extension.keyUsage);
822 if (ext != null) {
823
824 // TBD: handle this better
825 if (extSet != null && (extSet.get(X509Extension.keyUsage.getId()) != null)) {
826 String err = i18n.getMessage("keyUsageExt");
827 throw new GeneralSecurityException(err);
828 }
829
830 DERBitString bits = (DERBitString) BouncyCastleUtil.getExtensionObject(ext);
831
832 byte[] bytes = bits.getBytes();
833
834 // make sure they are disabled
835 if ((bytes[0] & KeyUsage.nonRepudiation) != 0) {
836 bytes[0] ^= KeyUsage.nonRepudiation;
837 }
838
839 if ((bytes[0] & KeyUsage.keyCertSign) != 0) {
840 bytes[0] ^= KeyUsage.keyCertSign;
841 }
842
843 bits = new DERBitString(bytes, bits.getPadBits());
844
845 certGen.addExtension(X509Extension.keyUsage, ext.isCritical(), bits);
846 }
847 }
848
849 } catch (IOException e) {
850 // but this should not happen
851 throw new GeneralSecurityException(e.getMessage());
852 }
853
854 } else if (certType == GSIConstants.CertificateType.GSI_2_LIMITED_PROXY) {
855 delegDN = "limited proxy";
856 serialNum = issuerCert.getSerialNumber();
857 } else if (certType == GSIConstants.CertificateType.GSI_2_PROXY) {
858 delegDN = "proxy";
859 serialNum = issuerCert.getSerialNumber();
860 } else {
861 String err = i18n.getMessage("unsupportedProxy", certType);
862 throw new IllegalArgumentException(err);
863 }
864
865 // add specified extensions
866 if (extSet != null) {
867 Iterator iter = extSet.oidSet().iterator();
868 while (iter.hasNext()) {
869 String oid = (String) iter.next();
870 // skip ProxyCertInfo extension
871 if (oid.equals(ProxyCertInfo.OID.getId()) || oid.equals(ProxyCertInfo.OLD_OID.getId())) {
872 continue;
873 }
874 x509Ext = (org.globus.gsi.X509Extension) extSet.get(oid);
875 certGen.addExtension(x509Ext.getOid(), x509Ext.isCritical(), x509Ext.getValue());
876 }
877 }
878
879 X509Name issuerDN;
880 if (issuerCert.getSubjectDN() instanceof X509Name) {
881 issuerDN = (X509Name)issuerCert.getSubjectDN();
882 } else {
883 issuerDN = new X509Name(true,issuerCert.getSubjectX500Principal().getName());
884 }
885 X509NameHelper issuer = new X509NameHelper(issuerDN);
886 X509NameHelper subject = new X509NameHelper(issuerDN);
887 subject.add(BCStyle.CN, (cnValue == null) ? delegDN : cnValue);
888
889 certGen.setSubjectDN(subject.getAsName());
890 certGen.setIssuerDN(issuer.getAsName());
891
892 certGen.setSerialNumber(serialNum);
893 certGen.setPublicKey(publicKey);
894 certGen.setSignatureAlgorithm(issuerCert.getSigAlgName());
895
896 GregorianCalendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
897 /* Allow for a five minute clock skew here. */
898 date.add(Calendar.MINUTE, -5);
899 certGen.setNotBefore(date.getTime());
900
901 /* If hours = 0, then cert lifetime is set to user cert */
902 if (lifetime <= 0) {
903 certGen.setNotAfter(issuerCert.getNotAfter());
904 } else {
905 date.add(Calendar.MINUTE, 5);
906 date.add(Calendar.SECOND, lifetime);
907 certGen.setNotAfter(date.getTime());
908 }
909
910
911 return certGen.generateX509Certificate(issuerKey);
912 }
913
914 /**
915 * Loads a X509 certificate from the specified input stream. Input stream must contain DER-encoded
916 * certificate.
917 *
918 * @param in
919 * the input stream to read the certificate from.
920 * @return <code>X509Certificate</code> the loaded certificate.
921 * @exception GeneralSecurityException
922 * if certificate failed to load.
923 */
924 public X509Certificate loadCertificate(InputStream in) throws IOException, GeneralSecurityException {
925 ASN1InputStream derin = new ASN1InputStream(in);
926 ASN1Primitive certInfo = derin.readObject();
927 ASN1Sequence seq = ASN1Sequence.getInstance(certInfo);
928 return new X509CertificateObject(Certificate.getInstance(seq));
929 }
930
931 /**
932 * Creates a certificate request from the specified subject DN and a key pair. The
933 * <I>"MD5WithRSAEncryption"</I> is used as the signing algorithm of the certificate request.
934 *
935 * @param subject
936 * the subject of the certificate request
937 * @param keyPair
938 * the key pair of the certificate request
939 * @return the certificate request.
940 * @exception GeneralSecurityException
941 * if security error occurs.
942 */
943 public byte[] createCertificateRequest(String subject, KeyPair keyPair) throws GeneralSecurityException {
944 X509Name name = new X509Name(subject);
945 return createCertificateRequest(name, "MD5WithRSAEncryption", keyPair);
946 }
947
948 /**
949 * Creates a certificate request from the specified certificate and a key pair. The certificate's subject
950 * DN with <I>"CN=proxy"</I> name component appended to the subject is used as the subject of the
951 * certificate request. Also the certificate's signing algorithm is used as the certificate request
952 * signing algorithm.
953 *
954 * @param cert
955 * the certificate to create the certificate request from.
956 * @param keyPair
957 * the key pair of the certificate request
958 * @return the certificate request.
959 * @exception GeneralSecurityException
960 * if security error occurs.
961 */
962 public byte[] createCertificateRequest(X509Certificate cert, KeyPair keyPair) throws GeneralSecurityException {
963
964 String issuer = cert.getSubjectDN().getName();
965 X509Name subjectDN = new X509Name(issuer + ",CN=proxy");
966 String sigAlgName = cert.getSigAlgName();
967 return createCertificateRequest(subjectDN, sigAlgName, keyPair);
968 }
969
970 /**
971 * Creates a certificate request from the specified subject name, signing algorithm, and a key pair.
972 *
973 * @param subjectDN
974 * the subject name of the certificate request.
975 * @param sigAlgName
976 * the signing algorithm name.
977 * @param keyPair
978 * the key pair of the certificate request
979 * @return the certificate request.
980 * @exception GeneralSecurityException
981 * if security error occurs.
982 */
983 public byte[] createCertificateRequest(X509Name subjectDN, String sigAlgName, KeyPair keyPair)
984 throws GeneralSecurityException {
985 DERSet attrs = null;
986 PKCS10CertificationRequest certReq = null;
987 certReq = new PKCS10CertificationRequest(sigAlgName, subjectDN, keyPair.getPublic(), attrs, keyPair
988 .getPrivate());
989
990 return certReq.getEncoded();
991 }
992
993 /**
994 * Given a delegation mode and an issuing certificate, decides an
995 * appropriate certificate type to use for proxies
996 * @param issuerCert the issuing certificate of a prospective proxy
997 * @param delegType the desired delegation mode
998 * @return the appropriate certificate type for proxies or
999 * GSIConstants.CertificateType.UNDEFINED when
1000 * GSIConstants.DelegationType.NONE was specified
1001 * @throws CertificateException when failing to get the certificate type
1002 * of the issuing certificate
1003 */
1004 public static GSIConstants.CertificateType decideProxyType(
1005 X509Certificate issuerCert, GSIConstants.DelegationType delegType)
1006 throws CertificateException {
1007 GSIConstants.CertificateType proxyType = GSIConstants.CertificateType.UNDEFINED;
1008 if (delegType == GSIConstants.DelegationType.LIMITED) {
1009 GSIConstants.CertificateType type = BouncyCastleUtil.getCertificateType(issuerCert);
1010 if (ProxyCertificateUtil.isGsi4Proxy(type)) {
1011 proxyType = GSIConstants.CertificateType.GSI_4_LIMITED_PROXY;
1012 } else if (ProxyCertificateUtil.isGsi3Proxy(type)) {
1013 proxyType = GSIConstants.CertificateType.GSI_3_LIMITED_PROXY;
1014 } else if (ProxyCertificateUtil.isGsi2Proxy(type)) {
1015 proxyType = GSIConstants.CertificateType.GSI_2_LIMITED_PROXY;
1016 } else {
1017 // default to RFC compliant proxy
1018 if (VersionUtil.isGsi2Enabled()) {
1019 proxyType = GSIConstants.CertificateType.GSI_2_LIMITED_PROXY;
1020 } else {
1021 proxyType = VersionUtil.isGsi3Enabled() ?
1022 GSIConstants.CertificateType.GSI_3_LIMITED_PROXY
1023 : GSIConstants.CertificateType.GSI_4_LIMITED_PROXY;
1024 }
1025 }
1026 } else if (delegType == GSIConstants.DelegationType.FULL) {
1027 GSIConstants.CertificateType type = BouncyCastleUtil.getCertificateType(issuerCert);
1028 if (ProxyCertificateUtil.isGsi4Proxy(type)) {
1029 proxyType = GSIConstants.CertificateType.GSI_4_IMPERSONATION_PROXY;
1030 } else if (ProxyCertificateUtil.isGsi3Proxy(type)) {
1031 proxyType = GSIConstants.CertificateType.GSI_3_IMPERSONATION_PROXY;
1032 } else if (ProxyCertificateUtil.isGsi2Proxy(type)) {
1033 proxyType = GSIConstants.CertificateType.GSI_2_PROXY;
1034 } else {
1035 // Default to RFC complaint proxy
1036 if (VersionUtil.isGsi2Enabled()) {
1037 proxyType = GSIConstants.CertificateType.GSI_2_PROXY;
1038 } else {
1039 proxyType = (VersionUtil.isGsi3Enabled()) ?
1040 GSIConstants.CertificateType.GSI_3_IMPERSONATION_PROXY
1041 : GSIConstants.CertificateType.GSI_4_IMPERSONATION_PROXY;
1042 }
1043 }
1044 }
1045 return proxyType;
1046 }
1047
1048 /**
1049 * Returns a chain of X509Certificate's that are instances of X509CertificateObject
1050 * This is related to http://bugzilla.globus.org/globus/show_bug.cgi?id=4933
1051 * @param certs input certificate chain
1052 * @return a new chain where all X509Certificate's are instances of X509CertificateObject
1053 * @throws GeneralSecurityException when failing to get load certificate from encoding
1054 */
1055 protected X509Certificate[] getX509CertificateObjectChain(X509Certificate[] certs)
1056 throws GeneralSecurityException {
1057 X509Certificate[] bcCerts = new X509Certificate[certs.length];
1058 for (int i = 0; i < certs.length; i++) {
1059 if (!(certs[i] instanceof X509CertificateObject)) {
1060 bcCerts[i] = CertificateLoadUtil.loadCertificate(new ByteArrayInputStream(certs[i].getEncoded()));
1061 } else {
1062 bcCerts[i] = certs[i];
1063 }
1064 }
1065 return bcCerts;
1066 }
1067 }