1. /*
  2. * @(#)KeyFactorySpi.java 1.5 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.security.spec.KeySpec;
  9. import java.security.spec.InvalidKeySpecException;
  10. /**
  11. * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
  12. * for the <code>KeyFactory</code> class.
  13. * All the abstract methods in this class must be implemented by each
  14. * cryptographic service provider who wishes to supply the implementation
  15. * of a key factory for a particular algorithm.
  16. *
  17. * <P> Key factories are used to convert <I>keys</I> (opaque
  18. * cryptographic keys of type <code>Key</code>) into <I>key specifications</I>
  19. * (transparent representations of the underlying key material), and vice
  20. * versa.
  21. *
  22. * <P> Key factories are bi-directional. That is, they allow you to build an
  23. * opaque key object from a given key specification (key material), or to
  24. * retrieve the underlying key material of a key object in a suitable format.
  25. *
  26. * <P> Multiple compatible key specifications may exist for the same key.
  27. * For example, a DSA public key may be specified using
  28. * <code>DSAPublicKeySpec</code> or
  29. * <code>X509EncodedKeySpec</code>. A key factory can be used to translate
  30. * between compatible key specifications.
  31. *
  32. * <P> A provider should document all the key specifications supported by its
  33. * key factory.
  34. *
  35. * @author Jan Luehe
  36. *
  37. * @version 1.5 01/11/29
  38. *
  39. * @see KeyFactory
  40. * @see Key
  41. * @see PublicKey
  42. * @see PrivateKey
  43. * @see java.security.spec.KeySpec
  44. * @see java.security.spec.DSAPublicKeySpec
  45. * @see java.security.spec.X509EncodedKeySpec
  46. *
  47. * @since JDK1.2
  48. */
  49. public abstract class KeyFactorySpi {
  50. /**
  51. * Generates a public key object from the provided key
  52. * specification (key material).
  53. *
  54. * @param keySpec the specification (key material) of the public key.
  55. *
  56. * @return the public key.
  57. *
  58. * @exception InvalidKeySpecException if the given key specification
  59. * is inappropriate for this key factory to produce a public key.
  60. */
  61. protected abstract PublicKey engineGeneratePublic(KeySpec keySpec)
  62. throws InvalidKeySpecException;
  63. /**
  64. * Generates a private key object from the provided key
  65. * specification (key material).
  66. *
  67. * @param keySpec the specification (key material) of the private key.
  68. *
  69. * @return the private key.
  70. *
  71. * @exception InvalidKeySpecException if the given key specification
  72. * is inappropriate for this key factory to produce a private key.
  73. */
  74. protected abstract PrivateKey engineGeneratePrivate(KeySpec keySpec)
  75. throws InvalidKeySpecException;
  76. /**
  77. * Returns a specification (key material) of the given key
  78. * object.
  79. * <code>keySpec</code> identifies the specification class in which
  80. * the key material should be returned. It could, for example, be
  81. * <code>DSAPublicKeySpec.class</code>, to indicate that the
  82. * key material should be returned in an instance of the
  83. * <code>DSAPublicKeySpec</code> class.
  84. *
  85. * @param key the key.
  86. *
  87. * @param keySpec the specification class in which
  88. * the key material should be returned.
  89. *
  90. * @return the underlying key specification (key material) in an instance
  91. * of the requested specification class.
  92. * @exception InvalidKeySpecException if the requested key specification is
  93. * inappropriate for the given key, or the given key cannot be dealt with
  94. * (e.g., the given key has an unrecognized format).
  95. */
  96. protected abstract KeySpec engineGetKeySpec(Key key, Class keySpec)
  97. throws InvalidKeySpecException;
  98. /**
  99. * Translates a key object, whose provider may be unknown or
  100. * potentially untrusted, into a corresponding key object of this key
  101. * factory.
  102. *
  103. * @param key the key whose provider is unknown or untrusted.
  104. *
  105. * @return the translated key.
  106. *
  107. * @exception InvalidKeyException if the given key cannot be processed
  108. * by this key factory.
  109. */
  110. protected abstract Key engineTranslateKey(Key key)
  111. throws InvalidKeyException;
  112. }