1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
320 provider = s.getProvider();
321 this.spi = spi;
322 return spi;
323 } catch (NoSuchAlgorithmException e) {
324 // ignore
325 }
326 }
327 serviceIterator = null;
328 return null;
329 }
330 }
331
332 /**
333 * Generates a public key object from the provided key specification
334 * (key material).
335 *
336 * @param keySpec the specification (key material) of the public key.
337 *
338 * @return the public key.
339 *
340 * @exception InvalidKeySpecException if the given key specification
341 * is inappropriate for this key factory to produce a public key.
342 */
343 public final PublicKey generatePublic(KeySpec keySpec)
344 throws InvalidKeySpecException {
345 if (serviceIterator == null) {
346 return spi.engineGeneratePublic(keySpec);
347 }
348 Exception failure = null;
349 KeyFactorySpi mySpi = spi;
350 do {
351 try {
352 return mySpi.engineGeneratePublic(keySpec);
353 } catch (Exception e) {
354 if (failure == null) {
355 failure = e;
356 }
357 mySpi = nextSpi(mySpi);
358 }
359 } while (mySpi != null);
360 if (failure instanceof RuntimeException) {
361 throw (RuntimeException)failure;
362 }
363 if (failure instanceof InvalidKeySpecException) {
364 throw (InvalidKeySpecException)failure;
365 }
366 throw new InvalidKeySpecException
367 ("Could not generate public key", failure);
368 }
369
370 /**
371 * Generates a private key object from the provided key specification
372 * (key material).
373 *
374 * @param keySpec the specification (key material) of the private key.
375 *
376 * @return the private key.
377 *
378 * @exception InvalidKeySpecException if the given key specification
379 * is inappropriate for this key factory to produce a private key.
380 */
381 public final PrivateKey generatePrivate(KeySpec keySpec)
382 throws InvalidKeySpecException {
383 if (serviceIterator == null) {
384 return spi.engineGeneratePrivate(keySpec);
385 }
386 Exception failure = null;
387 KeyFactorySpi mySpi = spi;
388 do {
389 try {
390 return mySpi.engineGeneratePrivate(keySpec);
391 } catch (Exception e) {
392 if (failure == null) {
393 failure = e;
394 }
395 mySpi = nextSpi(mySpi);
396 }
397 } while (mySpi != null);
398 if (failure instanceof RuntimeException) {
406 }
407
408 /**
409 * Returns a specification (key material) of the given key object.
410 * {@code keySpec} identifies the specification class in which
411 * the key material should be returned. It could, for example, be
412 * {@code DSAPublicKeySpec.class}, to indicate that the
413 * key material should be returned in an instance of the
414 * {@code DSAPublicKeySpec} class.
415 *
416 * @param <T> the type of the key specification to be returned
417 *
418 * @param key the key.
419 *
420 * @param keySpec the specification class in which
421 * the key material should be returned.
422 *
423 * @return the underlying key specification (key material) in an instance
424 * of the requested specification class.
425 *
426 * @exception InvalidKeySpecException if the requested key specification is
427 * inappropriate for the given key, or the given key cannot be processed
428 * (e.g., the given key has an unrecognized algorithm or format).
429 */
430 public final <T extends KeySpec> T getKeySpec(Key key, Class<T> keySpec)
431 throws InvalidKeySpecException {
432 if (serviceIterator == null) {
433 return spi.engineGetKeySpec(key, keySpec);
434 }
435 Exception failure = null;
436 KeyFactorySpi mySpi = spi;
437 do {
438 try {
439 return mySpi.engineGetKeySpec(key, keySpec);
440 } catch (Exception e) {
441 if (failure == null) {
442 failure = e;
443 }
444 mySpi = nextSpi(mySpi);
445 }
446 } while (mySpi != null);
447 if (failure instanceof RuntimeException) {
448 throw (RuntimeException)failure;
449 }
450 if (failure instanceof InvalidKeySpecException) {
451 throw (InvalidKeySpecException)failure;
452 }
453 throw new InvalidKeySpecException
454 ("Could not get key spec", failure);
455 }
456
457 /**
458 * Translates a key object, whose provider may be unknown or potentially
459 * untrusted, into a corresponding key object of this key factory.
460 *
461 * @param key the key whose provider is unknown or untrusted.
462 *
463 * @return the translated key.
464 *
465 * @exception InvalidKeyException if the given key cannot be processed
466 * by this key factory.
467 */
468 public final Key translateKey(Key key) throws InvalidKeyException {
469 if (serviceIterator == null) {
470 return spi.engineTranslateKey(key);
471 }
472 Exception failure = null;
473 KeyFactorySpi mySpi = spi;
474 do {
475 try {
476 return mySpi.engineTranslateKey(key);
477 } catch (Exception e) {
478 if (failure == null) {
479 failure = e;
480 }
481 mySpi = nextSpi(mySpi);
482 }
483 } while (mySpi != null);
484 if (failure instanceof RuntimeException) {
485 throw (RuntimeException)failure;
|
1 /*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
320 provider = s.getProvider();
321 this.spi = spi;
322 return spi;
323 } catch (NoSuchAlgorithmException e) {
324 // ignore
325 }
326 }
327 serviceIterator = null;
328 return null;
329 }
330 }
331
332 /**
333 * Generates a public key object from the provided key specification
334 * (key material).
335 *
336 * @param keySpec the specification (key material) of the public key.
337 *
338 * @return the public key.
339 *
340 * @throws InvalidKeySpecException if the given key specification
341 * is inappropriate for this key factory to produce a public key.
342 */
343 public final PublicKey generatePublic(KeySpec keySpec)
344 throws InvalidKeySpecException {
345 if (serviceIterator == null) {
346 return spi.engineGeneratePublic(keySpec);
347 }
348 Exception failure = null;
349 KeyFactorySpi mySpi = spi;
350 do {
351 try {
352 return mySpi.engineGeneratePublic(keySpec);
353 } catch (Exception e) {
354 if (failure == null) {
355 failure = e;
356 }
357 mySpi = nextSpi(mySpi);
358 }
359 } while (mySpi != null);
360 if (failure instanceof RuntimeException) {
361 throw (RuntimeException)failure;
362 }
363 if (failure instanceof InvalidKeySpecException) {
364 throw (InvalidKeySpecException)failure;
365 }
366 throw new InvalidKeySpecException
367 ("Could not generate public key", failure);
368 }
369
370 /**
371 * Generates a private key object from the provided key specification
372 * (key material).
373 *
374 * @param keySpec the specification (key material) of the private key.
375 *
376 * @return the private key.
377 *
378 * @throws InvalidKeySpecException if the given key specification
379 * is inappropriate for this key factory to produce a private key.
380 */
381 public final PrivateKey generatePrivate(KeySpec keySpec)
382 throws InvalidKeySpecException {
383 if (serviceIterator == null) {
384 return spi.engineGeneratePrivate(keySpec);
385 }
386 Exception failure = null;
387 KeyFactorySpi mySpi = spi;
388 do {
389 try {
390 return mySpi.engineGeneratePrivate(keySpec);
391 } catch (Exception e) {
392 if (failure == null) {
393 failure = e;
394 }
395 mySpi = nextSpi(mySpi);
396 }
397 } while (mySpi != null);
398 if (failure instanceof RuntimeException) {
406 }
407
408 /**
409 * Returns a specification (key material) of the given key object.
410 * {@code keySpec} identifies the specification class in which
411 * the key material should be returned. It could, for example, be
412 * {@code DSAPublicKeySpec.class}, to indicate that the
413 * key material should be returned in an instance of the
414 * {@code DSAPublicKeySpec} class.
415 *
416 * @param <T> the type of the key specification to be returned
417 *
418 * @param key the key.
419 *
420 * @param keySpec the specification class in which
421 * the key material should be returned.
422 *
423 * @return the underlying key specification (key material) in an instance
424 * of the requested specification class.
425 *
426 * @throws InvalidKeySpecException if the requested key specification is
427 * inappropriate for the given key, or the given key cannot be processed
428 * (e.g., the given key has an unrecognized algorithm or format).
429 */
430 public final <T extends KeySpec> T getKeySpec(Key key, Class<T> keySpec)
431 throws InvalidKeySpecException {
432 if (serviceIterator == null) {
433 return spi.engineGetKeySpec(key, keySpec);
434 }
435 Exception failure = null;
436 KeyFactorySpi mySpi = spi;
437 do {
438 try {
439 return mySpi.engineGetKeySpec(key, keySpec);
440 } catch (Exception e) {
441 if (failure == null) {
442 failure = e;
443 }
444 mySpi = nextSpi(mySpi);
445 }
446 } while (mySpi != null);
447 if (failure instanceof RuntimeException) {
448 throw (RuntimeException)failure;
449 }
450 if (failure instanceof InvalidKeySpecException) {
451 throw (InvalidKeySpecException)failure;
452 }
453 throw new InvalidKeySpecException
454 ("Could not get key spec", failure);
455 }
456
457 /**
458 * Translates a key object, whose provider may be unknown or potentially
459 * untrusted, into a corresponding key object of this key factory.
460 *
461 * @param key the key whose provider is unknown or untrusted.
462 *
463 * @return the translated key.
464 *
465 * @throws InvalidKeyException if the given key cannot be processed
466 * by this key factory.
467 */
468 public final Key translateKey(Key key) throws InvalidKeyException {
469 if (serviceIterator == null) {
470 return spi.engineTranslateKey(key);
471 }
472 Exception failure = null;
473 KeyFactorySpi mySpi = spi;
474 do {
475 try {
476 return mySpi.engineTranslateKey(key);
477 } catch (Exception e) {
478 if (failure == null) {
479 failure = e;
480 }
481 mySpi = nextSpi(mySpi);
482 }
483 } while (mySpi != null);
484 if (failure instanceof RuntimeException) {
485 throw (RuntimeException)failure;
|