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