JDK 1.1 introduced the notion of a Cryptography Package Provider, or "provider" for short. This term refers to a package (or a set of packages) that supply a concrete implementation of a subset of the cryptography aspects of the Java Security API. In JDK 1.1 a provider could, for example, contain an implementation of one or more digital signature algorithms, message digest algorithms, and key generation algorithms. JDK 1.2 adds three additional types of services: key conversion, algorithm parameter management, and algorithm parameter generation.
A program wishing to use cryptography functionality may simply request a particular type of object (such as a Signature object) implementing a particular algorithm (such as DSA) and get an implementation from one of the installed providers. If an implementation from a particular provider is desired, the program can request that provider by name, along with the algorithm desired.
Each JDK installation has one or more provider packages installed. Clients may configure their runtimes with different providers, and specify a preference order for each of them. The preference order is the order in which providers are searched for requested algorithms when no particular provider is requested.
JDK 1.2 comes standard with a default provider, named "SUN". The "SUN" provider package includes:
- An implementation of the Digital Signature Algorithm (NIST FIPS 186).
- An implementation of the MD5 (RFC 1321) and SHA-1 (NIST FIPS 180-1) message digest algorithms.
- A DSA key pair generator for generating a pair of public and private keys suitable for the DSA algorithm.
- A DSA algorithm parameter generator.
- A DSA algorithm parameter manager.
- A DSA "key factory" providing bi-directional conversions between opaque DSA key representations and transparent representations of the underlying key material.
New providers may be added statically or dynamically. Clients may also query which providers are currently installed.
The different implementations may have different characteristics. Some may be software-based, while others may be hardware-based. Some may be platform-independent, while others may be platform-specific. Some provider source code may be available for review and evaluation, while some may not.
Who Should Read This Document
This document is intended for experienced programmers wishing to create their own provider packages supplying cryptographic service implementations. It documents what you need to do in order to integrate your provider into Java Security so that your algorithms and other services can be found when Java Security API clients request them. Programmers that only need to use the Java Security API to access existing cryptography algorithms and other services do not need to read this document.
Related Documentation
This document assumes you have already read the Java Cryptography Architecture API Specification and Reference.
It also discusses various classes and interfaces in the Java Security API. The complete reference documentation for the relevant Security API packages can be found in:
An "engine class" defines a cryptographic service in an abstract fashion (without a concrete implementation).
A cryptographic service is always associated with a particular algorithm, and it either provides cryptographic operations (like those for digital signatures or message digests), or generates or supplies the cryptographic material (keys or parameters) required for cryptographic operations. For example, two of the engine classes are the Signature and KeyFactory classes. The Signature class provides access to the functionality of a digital signature algorithm. A DSA KeyFactory supplies a DSA private or public key (from its encoding or transparent specification) in a format usable by the
initSign
orinitVerify
methods, respectively, of a DSA Signature object.The Java Cryptography Architecture encompasses the classes of the JDK1.2 Java Security package related to cryptography, including the engine classes. Users of the API request and utilize instances of the engine classes to carry out corresponding operations. The following engine classes are defined in JDK 1.2:
- MessageDigest - used to calculate the message digest (hash) of specified data.
- Signature - used to sign data and verify digital signatures.
- KeyPairGenerator - used to generate a pair of public and private keys suitable for a specified algorithm.
- KeyFactory - used to convert opaque cryptographic keys of type Key into key specifications (transparent representations of the underlying key material), and vice versa.
- AlgorithmParameters - used to manage the parameters for a particular algorithm, including parameter encoding and decoding.
- AlgorithmParameterGenerator - used to generate a set of parameters to be used with a certain algorithm.
An engine class provides the interface to the functionality of a specific type of cryptographic service (independent of a particular cryptographic algorithm). It defines "Application Programming Interface" (API) methods that allow applications to access the specific type of cryptographic service it provides. The actual implementations (from one or more providers) are those for specific algorithms. The Signature engine class, for example, provides access to the functionality of a digital signature algorithm. The actual implementation supplied in a SignatureSpi subclass (see next paragraph) would be that for a specific kind of signature algorithm, such as SHA-1 with DSA, SHA-1 with RSA, or MD5 with RSA.
The application interfaces supplied by an engine class are implemented in terms of a "Service Provider Interface" (SPI). That is, for each engine class, there is a corresponding abstract SPI class, which defines the Service Provider Interface methods that cryptographic service providers must implement.
An instance of an engine class, the "API object", encapsulates (as a private field) an instance of the corresponding SPI class, the "SPI object". All API methods of an API object are declared "final", and their implementations invoke the corresponding SPI methods of the encapsulated SPI object. An instance of an engine class (and of its corresponding SPI class) is created by a call to the
getInstance
factory method of the engine class.The name of each SPI class is the same as that of the corresponding engine class, followed by "Spi". For example, the SPI class corresponding to the Signature engine class is the SignatureSpi class.
Each SPI class is abstract. To supply the implementation of a particular type of service, for a specific algorithm, a provider must subclass the corresponding SPI class and provide implementations for all the abstract methods.
Another example of an engine class is the MessageDigest class, which provides access to a message digest algorithm. Its implementations, in MessageDigestSpi subclasses, may be those of various message digest algorithms such as SHA-1, MD5, or MD2.
As a final example, the KeyFactory engine class supports the conversion from opaque keys to transparent key specifications, and vice versa. (See Key Specification Interfaces and Classes Required by Key Factories.) The actual implementation supplied in a KeyFactorySpi subclass would be that for a specific type of keys, e.g., DSA public and private keys.
The steps required in order to implement a provider and integrate it into Java Security are the following:
- Step 1: Write your Service Implementation Code
- Step 2: Give your Provider a Name
- Step 3: Write your "Master Class", a subclass of Provider
- Step 4: Compile your Code
- Step 5: Prepare for Testing: Install the Provider
- Step 6: Write and Compile your Test Programs
- Step 7: Run your Test Programs
- Step 8: Document your Provider and its Supported Services
- Step 9: Make your Class Files and Documentation Available to Clients
Step 1: Write your Service Implementation Code
The first thing you need to do is write the code supplying algorithm-specific implementations of the cryptographic services you want to support. In JDK 1.2, you can supply signature, message digest, and key pair generation algorithms, as well as key conversion, algorithm parameter management, and algorithm parameter generation services.For each cryptographic service, you need to create a subclass of the appropriate SPI class: SignatureSpi, MessageDigestSpi, KeyPairGeneratorSpi, AlgorithmParameterGeneratorSpi, AlgorithmParametersSpi, or KeyFactorySpi. (See "Engine Classes and Corresponding SPI Classes".)
In your subclass, you need to
- supply implementations for the abstract methods, whose names usually begin with "engine". See Further Implementation Details and Requirements for additional information.
- ensure there is a public constructor without any arguments. Here's why: When one of your services is requested, Java Security looks up the subclass implementing that service, as specified by a property in your "master class" (see Step 3). Java Security then creates the Class object associated with your subclass, and creates an instance of your subclass by calling the
newInstance
method on that Class object.newInstance
requires your subclass to have a public constructor without any parameters.A default constructor without arguments will automatically be generated if your subclass doesn't have any constructors. But if your subclass defines any constructors, you must explicitly define a public constructor without arguments.
Step 2: Give your Provider a Name
Decide on a name for your provider. This is the name to be used by client applications to refer to your provider.Step 3: Write your "Master Class", a subclass of Provider
The third step is to create a subclass of the Provider class.Your subclass should be a
final
class, and its constructor should
- call
super
, specifying the provider name (see Step 2), version number, and a string of information about the provider and algorithms it supports. For example:super("ACME", 1.0, "ACME provider v1.0, implementing " + "RSA signing and key generation, SHA-1 and MD5 message digests.");- set the values of various properties that are required for the Java Security API to look up the cryptographic services implemented by the provider. For each service implemented by the provider, there must be a property whose name is the type of service (Signature, MessageDigest, KeyPairGenerator, KeyFactory, AlgorithmParameterGenerator, or AlgorithmParameters), followed by a period and the name of the algorithm to which the service applies. The property value must specify the fully qualified name of the class implementing the service.
The list below shows the various types of properties that must be defined for the various types of services, where the actual algorithm name is substitued for algName:
Signature.
algNameMessageDigest.
algNameKeyPairGenerator.
algNameKeyFactory.
algNameAlgorithmParameterGenerator.
algNameAlgorithmParameters.
algNameIn all of these, algName is the "standard" name of the algorithm. (See Appendix A of the Java Cryptography Architecture API Specification & Reference for the standard algorithm names that should be used.)
The value of each property must be the fully qualified name of the class implementing the specified algorithm. That is, it must be the package name followed by the class name, where the two are separated by a period.
As an example, the default provider named "SUN" implements the Digital Signature Algorithm (DSA) in a class named
DSA
in thesun.security.provider
package. Its subclass of Provider (which is theSun
class in thesun.security.provider
package) sets theSignature.DSA
property to have the value "sun.security.provider.DSA" via the following:put("Signature.DSA", "sun.security.provider.DSA")
For further master class property setting examples, see Appendix A to view the current JDK 1.2
Sun.java
source file. This shows how theSun
class constructor sets all the properties for the "SUN" provider.Note: The Provider subclass can get its information from wherever it wants. Thus, the information can be hard-wired in, or retrieved at runtime, e.g., from a file.
Step 4: Compile your Code
After you have created your implementation code (Step 1), given your provider a name (Step 2), and created the master class (Step 3), use the Java compiler to compile your files.Step 5: Prepare for Testing: Install the Provider
In order to prepare for testing your provider, you must install it in the same manner as will be done by clients wishing to use it. The installation enables Java Security to find your algorithm implementations when clients request them.There are two parts to installing a provider: installing the provider package classes, and configuring the provider.
Installing the Provider Classes
The first thing you must do is make your classes available so that they can be found when requested. There are a couple possible ways of doing this:
- Place the provider classes (the .class files) anywhere on your CLASSPATH. They should be in appropriate directories reflecting their package(s). For example, if the classes implementing the provider are in the
COM.acme.provider
package, install the classes in the following directory structure (within any directory on your CLASSPATH):
COM/acme/provider/
(Solaris)
COM\acme\provider\
(Windows)
- Place a zip or JAR (Java ARchive) file containing the classes anywhere on your CLASSPATH.
- Create a
classes
directory in the JDK installation directory, and install the .class files in that directory. For example, if the JDK is installed in a directory calledjdk1.1.1
, and the classes implementing the provider are in theCOM.acme.provider
package, install the classes in the directory
jdk1.1.1/classes/COM/acme/provider/ (Solaris)
jdk1.1.1\classes\COM\acme\provider\ (Windows)
However, please note that use of a
classes
directory is not recommended. A new "extension" mechanism will be introduced later in JDK 1.2, and it will provide a "standard" way of supplying and loading Java extensions.
Configuring the Provider
The next step is to add the provider to your list of approved providers. This is done statically by editing the
java.security
file in thelib/security
(lib\security
on Windows) directory of the JDK. Thus, if the JDK is installed in a directory calledjdk1.2
, the file is
jdk1.2/lib/security/java.security
(Solaris)
jdk1.2\lib\security\java.security
(Windows)For each provider, this file should have a statement of the following form:
security.provider.n=masterClassNameThis declares a provider, and specifies its preference order n. The preference order is the order in which providers are searched for requested algorithms when no specific provider is requested. The order is 1-based; 1 is the most preferred, followed by 2, and so on.
masterClassName must specify the fully qualified name of the provider's "master class", which you implemented in Step 3. This class is always a subclass of the Provider class.
Whenever the JDK is installed, it contains one built-in (default) provider, the provider referred to as "SUN". The
java.security
file has just the following provider specification:security.provider.1=sun.security.provider.Sun(Recall that the "SUN" provider's master class is theSun
class in thesun.security.provider
package.)Suppose that your master class is the
Acme
class in theCOM.acme.provider
package, and that you would like to make your provider the second preferred provider. To do so, add the following line to thejava.security
file below the line for the "SUN" provider:security.provider.2=COM.acme.provider.AcmeNote: Providers may also be registered dynamically. To do so, a program (such as your test program, to be written in Step 7) can call either theaddProvider
orinsertProviderAt
method in theSecurity
class. This type of registration is not persistent and can only be done by "trusted" programs. See the Security class section of the Java Cryptography Architecture API Specification and Reference.
Write and compile one or more test programs that test your provider's incorporation into the Security API as well as the correctness of its algorithm(s). Create any supporting files needed, such as those for test data to be hashed or signed.The first tests your program should perform are ones to ensure that your provider is found, and that its name, version number, and additional information is as expected. To do so, you could write code like the following, substituting your provider name for "MyPro":
import java.security.*; Provider p = Security.getProvider("MyPro"); System.out.println("MyPro provider name is " + p.getName()); System.out.println("MyPro provider version # is " + p.getVersion()); System.out.println("MyPro provider info is " + p.getInfo());Next, you should ensure that your services are found. For instance, if you implemented an SHA-1 message digest algorithm, you could check to ensure it's found when requested by using the following code (again substituting your provider name for "MyPro"):
MessageDigest sha = MessageDigest.getInstance("SHA-1", "MyPro"); System.out.println("My MessageDigest algorithm name is " + sha.getAlgorithm());If you don't specify a provider name in the call to
getInstance
, all registered providers will be searched, in preference order (see Configuring the Provider), until one implementing the algorithm is found.
Run your test program(s). Debug your code and continue testing as needed. If the Java Security API cannot seem to find one of your algorithms, review the steps above and ensure they are all completed.
The next-to-last step is to write documentation for your clients. At the minimum, you need to specifyIn addition, your documentation should specify anything else of interest to clients, such as any default algorithm parameters.
- the name programs should use to refer to your provider. Please note: As of this writing, provider name searches are case-sensitive. That is, if your master class specifies your provider name as "ACME" but a user requests "Acme", your provider will not be found. This behavior may change in the future, but for now be sure to warn your clients to use the exact case you specify.
- the types of algorithms and other services implemented by your provider.
- instructions for installing the provider, similar to those provided in Step 5, except that the information and examples should be specific to your provider.
Message Digests
For each message digest algorithm, tell whether or not your implementation is cloneable. This is not technically necessary, but it may save clients some time and coding by telling them whether or not intermediate hashes may be possible through cloning. Clients who do not know whether or not a message digest implementation is cloneable can find out by calling
boolean cloneable = sha instanceof Cloneable;wheresha
is the message digest object they received when they requested one via a call toMessageDigest.getInstance
.Signature Algorithms
For a signature algorithm, specify which parameters can be set (or gotten) via a call to the
setParameter
(orgetParameter
) method in theSignature
class. Tell the Strings that can be used for theparam
argument tosetParameter
orgetParameter
to indicate which parameter should be set or gotten. Also tell what parameter values can be specified in the setParametervalue
argument. See the Signature class documentation for further information aboutsetParameter
andgetParameter
.Key Pair Generators
For a key pair generator algorithm, in case the client does not explicitly initialize the key pair generator (via a call to an
initialize
method), each provider must supply and document a default initialization. For example, the "SUN" provider uses a default modulus size (strength) of 1024 bits.Key Factories
A provider should document all the key specifications supported by its key factory.Algorithm Parameter Generators
In case the client does not explicitly initialize the algorithm parameter generator (via a call to aninit
method in the AlgorithmParameterGenerator engine class), each provider must supply and document a default initialization. For example, the "SUN" provider uses a default modulus prime size of 1024 bits for the generation of DSA parameters.
The final step is to make your class files and documentation available to clients in whatever form (.class files, zip files, JAR files, ...) and methods (web download, floppy, mail, ...) you feel are appropriate.
Algorithm Aliases
For many cryptographic algorithms, there is a single official "standard name" defined in Appendix A of the Java Cryptography Architecture API Specification & Reference.For example, "MD5" is the standard name for the RSA-MD5 Message Digest algorithm defined by RSA DSI in RFC 1321.
In JDK 1.2, there is an aliasing scheme that enables clients to use aliases when referring to algorithms, rather than their standard names. For example, the "SUN" provider's master class (
Sun.java
) defines the alias "SHA-1/DSA" for the algorithm whose standard name is "DSA". Thus, the following statements are equivalent:Signature sig = Signature.getInstance("DSA", "SUN"); Signature sig = Signature.getInstance("SHA-1/DSA", "SUN");Aliases can be defined in your "master class" (see Step 3). To define an alias, create a property namedAlg.Alias.
engineClassName.aliasNamewhere engineClassName is either
Signature
,MessageDigest
,KeyPairGenerator
,KeyFactory
,AlgorithmParameterGenerator
, orAlgorithmParameters
, and aliasName is your alias name. The value of the property must be the standard algorithm name for the algorithm being aliased.As an example, the "SUN" provider defines the alias "SHA-1/DSA" for the signature algorithm whose standard name is "DSA" by setting a property named
Alg.Alias.Signature.SHA-1/DSA
to have the valueDSA
via the following:put("Alg.Alias.Signature.SHA-1/DSA", "DSA");Currently, aliases defined by the "SUN" provider are available to all clients, no matter which provider clients request. For example, if you create a provider named "MyPro" that implements the DSA algorithm, then even if you don't define any aliases for it, the "SHA-1/DSA" alias defined by "SUN" can be used to refer to your provider's DSA implementation as follows:
Signature sig = Signature.getInstance("SHA-1/DSA", "MyPro");
WARNING: The aliasing scheme may be changed or eliminated in future releases.
Service Interdependencies
Some algorithms require the use of other types of algorithms. For example, a signature algorithm usually needs to use a message digest algorithm in order to sign and verify data.If you are implementing one type of algorithm that requires another, you can do one of the following:
- Provide your own implementations for both.
- Let your implementation of one algorithm use an instance of the other type of algorithm, as supplied by the default "SUN" provider that is included with every JDK installation. For example, if you are implementing a signature algorithm that requires a message digest algorithm, you can obtain an instance of a class implementing the MD5 message digest algorithm by calling
MessageDigest.getInstance("MD5", "SUN")
- Let your implementation of one algorithm use an instance of the other type of algorithm, as supplied by another specific provider. This is only appropriate if you are sure that all clients who will use your provider will also have the other provider installed.
- Let your implementation of one algorithm use an instance of the other type of algorithm, as supplied by another (unspecified) provider. That is, you can request an algorithm by name, but without specifying any particular provider, as in
MessageDigest.getInstance("MD5")This is only appropriate if you are sure that there will be at least one implementation of the requested algorithm (in this case, MD5) installed on each Java platform where your provider will be used.Here are some common types of algorithm interdependencies:
Signature and Message Digest Algorithms
A signature algorithm often requires use of a message digest algorithm. For example, the DSA signature algorithm requires the SHA-1 message digest algorithm.Key Pair Generation and Message Digest Algorithms
A key pair generation algorithm often requires use of a message digest algorithm. For example, DSA keys are generated using the SHA-1 message digest algorithm.Algorithm Parameter Generation and Message Digest Algorithms
An algorithm parameter generator often requires use of a message digest algorithm. For example, DSA parameters are generated using the SHA-1 message digest algorithm.Key Pair Generation Algorithms and Algorithm Parameter Generators
A key pair generation algorithm sometimes needs to generate a new set of algorithm parameters. It can either generate the parameters directly, or use an algorithm parameter generator.
Algorithm Parameter Generators and Algorithm Parameters
An algorithm parameter generator's
engineGenerateParameters
method must return an AlgorithmParameters instance.Signature and Key Pair Generation Algorithms or Key Factories
If you are implementing a signature algorithm, your implementation's
engineInitSign
andengineInitVerify
methods will require passed-in keys that are valid for the underlying algorithm (e.g., DSA keys for the DSS algorithm). You can do one of the following:
- Also create your own classes implementing appropriate interfaces (e.g. classes implementing the DSAPrivateKey and DSAPublicKey interfaces from the package
java.security.interfaces
), and create your own key pair generator and/or key factory returning keys of those types. Require the keys passed toengineInitSign
andengineInitVerify
to be the types of keys you have implemented, that is, keys generated from your key pair generator or key factory. Or you can
- Accept keys from other key pair generators or other key factories, as long as they are instances of appropriate interfaces that enable your signature implementation to obtain the information it needs (such as the private and public keys and the key parameters). For example, the
engineInitSign
method for a DSS Signature class could accept any private keys that are instances ofjava.security.interfaces.DSAPrivateKey
.Default Initializations
In case the client does not explicitly initialize a key pair generator or an algorithm parameter generator, each provider of such a service must supply (and document) a default initialization. For example, the "SUN" provider uses a default modulus size (strength) of 1024 bits for the generation of DSA parameters.
Default Key Pair Generator Parameter Requirements
If you implement a key pair generator, your implementation should supply default parameters that are used when clients don't specify parameters. The documentation you supply (Step 8) should state what the default parameters are.For example, the DSA key pair generator in the "SUN" provider supplies a set of pre-computed
p
,q
, andg
default values for the generation of 512, 768, and 1024-bit key pairs. The followingp
,q
, andg
values are used as the default values for the generation of 1024-bit DSA key pairs:p = fd7f5381 1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669 455d4022 51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7 6b9950a5 a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb 83f6d3c5 1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7 q = 9760508f 15230bcc b292b982 a2eb840b f0581cf5 g = f7e1a085 d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267 5159578e bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1 3c167a8b 547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b cca4f1be a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a(The
p
andq
values given here were generated by the prime generation standard, using the 160-bitSEED: 8d515589 4229d5e6 89ee01e6 018a237e 2cae64cdWith this seed, the algorithm foundp
andq
when the counter was at 92.)DSA Interfaces and their Required Implementations
The Java Security API contains the following interfaces (in thejava.security.interfaces
package) for the convenience of programmers implementing DSA services: The following sections discuss requirements for implementations of these interfaces.DSAKeyPairGenerator Implementation
This interface is obsolete. It used to be needed to enable clients to provide DSA-specific parameters to be used rather than the default parameters your implementation supplies. However, in JDK 1.2 it is no longer necessary; a new KeyPairGenerator
initialize
method that takes an AlgorithmParameterSpec parameter enables clients to indicate algorithm-specific parameters.DSAParams Implementation
If you are implementing a DSA key pair generator, you need a class implementing DSAParams for holding and returning the
p
,q
, andg
parameters.A DSAParams implementation is also required if you implement the DSAPrivateKey and DSAPublicKey interfaces. DSAPublicKey and DSAPrivateKey both extend the DSAKey interface, which contains a
getParams
method that must return a DSAParams object. See DSAPrivateKey and DSAPublicKey Implementations for more information.Note: there is a DSAParams implementation built into the JDK: the
java.security.spec.DSAParameterSpec
class.DSAPrivateKey and DSAPublicKey Implementations
If you implement a DSA key pair generator, signature algorithm, or key factory, you need to create classes implementing the DSAPrivateKey and DSAPublicKey interfaces.If you implement a DSA key pair generator, your
generateKeyPair
method (in your KeyPairGeneratorSpi subclass) will return instances of those classes.If you implement a DSA signature algorithm, your
engineInitSign
method (in your SignatureSpi subclass) will expect to be passed a DSAPrivateKey and yourengineInitVerify
method will expect to be passed a DSAPublicKey.If you implement a DSA key factory, your
engineGeneratePrivate
method (in your KeyFactorySpi subclass) will return an instance of your DSAPrivateKey implementation, and yourengineGeneratePublic
method will return an instance of your DSAPublicKey implementation.Also, your
engineGetKeySpec
andengineTranslateKey
methods will expect the passed-in key to be an instance of a DSAPrivateKey or DSAPublicKey implementation. ThegetParams
method provided by the interface implementations is useful for obtaining and extracting the parameters from the keys and then using the parameters, for example as parameters to the DSAParameterSpec constructor called to create a parameter specification from parameter values that could be used to initialize a KeyPairGenerator object for DSA.Please note: The DSAPublicKey and DSAPrivateKey interfaces define a very generic, provider-independent interface to DSA public and private keys, respectively. The
engineGetKeySpec
andengineTranslateKey
methods could additionally check if the passed-in key is actually an instance of their provider's own implementation of DSAPrivateKey or DSAPublicKey, e.g., to take advantage of provider-specific implementation details.To see what methods need to be implemented by classes that implement the DSAPublicKey and DSAPrivateKey interfaces, first note the following interface signatures:
In the java.security.interfaces package: public interface DSAPrivateKey extends DSAKey, java.security.PrivateKey public interface DSAPublicKey extends DSAKey, java.security.PublicKey public interface DSAKey In the java.security package: public interface PrivateKey extends Key public interface PublicKey extends Key public interface Key extends java.io.SerializableIn order to implement the DSAPrivateKey and DSAPublicKey interfaces, you must implement the methods they define as well as those defined by interfaces they extend, directly or indirectly.
Thus, for private keys, you need to supply a class that implements
- the
getX
method from the DSAPrivateKey interface.
- the
getParams
method from the java.security.interfaces.DSAKey interface, since DSAPrivateKey extends DSAKey. Note: ThegetParam
s method returns a DSAParams object, so you must also have a DSAParams implementation.
- the
getAlgorithm
,getEncoded
, andgetFormat
methods from the java.security.Key interface, since DSAPrivateKey extendsjava.security.PrivateKey
, and PrivateKey extends Key. Similarly, for public DSA keys, you need to supply a class that implements
- the
getY
method from the DSAPublicKey interface.
- the
getParams
method from the java.security.interfaces.DSAKey interface, since DSAPublicKey extends DSAKey. Note: ThegetParam
s method returns a DSAParams object, so you must also have a DSAParams implementation.
- the
getAlgorithm
,getEncoded
, andgetFormat
methods from the java.security.Key interface, since DSAPublicKey extendsjava.security.PublicKey
, and PublicKey extends Key.Non-DSA Interfaces
As noted above, the Java Security API contains interfaces for the convenience of programmers implementing DSA services. The API does not at this time contain similar interfaces for any other type of algorithm. Thus, you need to define your own.If you are implementing a non-DSA key pair generator, you should create an interface with one or more
initialize
methods that clients can call when they want to provide algorithm-specific parameters to be used rather than the default parameters your implementation supplies. Your subclass of KeyPairGeneratorSpi should implement this interface.For private and public keys for non-DSA algorithms, there are currently no
java.security.interfaces
interfaces corresponding to the DSAPrivateKey and DSAPublicKey ones for DSA. It is recommended that you create similar interfaces and provide implementation classes. Your public key interface should extend the PublicKey interface. Similarly, your private key interface should extend the PrivateKey interface.Algorithm Parameter Specification Interfaces and Classes
An algorithm parameter specification is a transparent representation of the sets of parameters used with an algorithm.
A transparent representation of parameters means that you can access each value individually, through one of the "get" methods defined in the corresponding specification class (e.g., DSAParameterSpec defines
getP
,getQ
, andgetG
methods, to access the p, q, and g parameters, respectively).This is contrasted with an opaque representation, as supplied by the AlgorithmParameters engine class, in which you have no direct access to the key material values; you can only get the name of the algorithm associated with the parameter set (via
getAlgorithm
) and some kind of encoding for the parameter set (viagetEncoded
).If you supply an AlgorithmParametersSpi, AlgorithmParameterGeneratorSpi, or KeyPairGeneratorSpi implementation, you must utilize the AlgorithmParameterSpec interface, since each of those classes contain methods that take an AlgorithmParameterSpec parameter. Such methods need to determine which actual implementation of that interface has been passed in, and act accordingly.
JDK 1.2 contains one AlgorithmParameterSpec implementation, the DSAParameterSpec class. If you are working with DSA algorithm parameters, you can utilize this class. If you are operating on algorithm parameters that should be for a different type of algorithm, you will need to supply your own AlgorithmParameterSpec implementation appropriate for that type of algorithm.
JDK 1.2 defines the following algorithm parameter specification interfaces and classes in the
java.security.spec
package:The AlgorithmParameterSpec Interface
AlgorithmParameterSpec is an interface to a transparent specification of cryptographic parameters.This interface contains no methods or constants. Its only purpose is to group (and provide type safety for) all parameter specifications. All parameter specifications must implement this interface.
The DSAParameterSpec Class
This class (which implements the AlgorithmParameterSpec and DSAParams interfaces) specifies the set of parameters used with the DSA algorithm. It has the following methods:public BigInteger getP() public BigInteger getQ() public BigInteger getG()These methods return the DSA algorithm parameters: the primep
, the sub-primeq
, and the baseg
.Many types of DSA services will find this class useful - for example, it is utilized by the DSA signature, key pair generator, algorithm parameter generator, and algorithm parameters classes implemented by the "SUN" provider. As a specific example, an algorithm parameters implementation must include an implementation for the
getParameterSpec
method, which returns an AlgorithmParameterSpec. The DSA algorithm parameters implementation supplied by "SUN" returns an instance of the DSAParameterSpec class.Key Specification Interfaces and Classes Required by Key Factories
A key factory provides bi-directional conversions between opaque keys (of type Key) and key specifications. If you implement a key factory, you thus need to understand and utilize key specifications. In some cases, you also need to implement your own key specifications. Further information about key specifications, the interfaces and classes supplied in JDK 1.2, and key factory requirements with respect to specifications, is provided below.Key specifications are transparent representations of the key material that constitutes a key. If the key is stored on a hardware device, its specification may contain information that helps identify the key on the device.
A transparent representation of keys means that you can access each key material value individually, through one of the "get" methods defined in the corresponding specification class. For example,
java.security.spec.DSAPrivateKeySpec
definesgetX
,getP
,getQ
, andgetG
methods, to access the private keyx
, and the DSA algorithm parameters used to calculate the key: the primep
, the sub-primeq
, and the baseg
.This is contrasted with an opaque representation, as defined by the Key interface, in which you have no direct access to the parameter fields. In other words, an "opaque" representation gives you limited access to the key - just the three methods defined by the Key interface:
getAlgorithm
,getFormat
, andgetEncoded
.A key may be specified in an algorithm-specific way, or in an algorithm-independent encoding format (such as ASN.1). For example, a DSA private key may be specified by its components
x
,p
,q
, andg
(see DSAPrivateKeySpec), or it may be specified using its DER encoding (see PKCS8EncodedKeySpec).JDK 1.2 defines the following key specification interfaces and classes in the
java.security.spec
package:The KeySpec Interface
This interface contains no methods or constants. Its only purpose is to group (and provide type safety for) all key specifications. All key specifications must implement this interface.
JDK 1.2 supplies several classes implementing the KeySpec interface: DSAPrivateKeySpec, DSAPublicKeySpec, and EncodedKeySpec.
If your provider uses key types (e.g., "A_public" and "A_private") for which the JDK does not already provide corresponding KeySpec classes, there are two possible scenarios, one of which requires that you implement your own key specifications:
- If your users will never have to access specific key material values of your key type, you will not have to provide any KeySpec classes for your key type.
In this scenario, your users will always create "A_public" and "A_private" keys through the appropriate KeyPairGenerator supplied by your provider for that key type. If they want to store the generated keys for later usage, they retrieve the keys' encodings (using the "getEncoded" method of the "Key" interface). When they want to create an "A_public" or "A_private" key from the encoding (e.g., in order to initialize a Signature object for signing or verification), they create an instance of "X509EncoedKeySpec" or "PKCS8EncodedKeySpec" from the encoding, and feed it to the appropriate KeyFactory supplied by your provider for that algorithm, whose "generatePublic" and "generatePrivate" methods will return the requested "PublicKey" (an instance of "A_public") or "PrivateKey" (an instance of "A_private") object, respectively.
- If you anticipate a need for users to access specific key material values of your key type, or to construct a key of your key type from key material and associated parameter values, rather than from its encoding (as in the above case), you have to specify new KeySpec classes (classes that implement the "KeySpec" interface) with the appropriate constructor methods and "get" methods for returning key material fields and associated parameter values for your key type. You will specify those classes in a similar manner as is done by the DSAPrivateKeySpec and DSAPublicKeySpec classes provided in the JDK. You need to ship those classes along with your provider classes, for example, as part of your provider JAR file.
The DSAPrivateKeySpec Class
This class (which implements the KeySpec Interface) specifies a DSA private key with its associated parameters. It has the following methods:public BigInteger getX() public BigInteger getP() public BigInteger getQ() public BigInteger getG()These methods return the private keyx
, and the DSA algorithm parameters used to calculate the key: the primep
, the sub-primeq
, and the baseg
.The DSAPublicKeySpec Class
This class (which implements the KeySpec Interface) specifies a DSA public key with its associated parameters. It has the following methods:public BigInteger getY() public BigInteger getP() public BigInteger getQ() public BigInteger getG()These methods return the public keyy
, and the DSA algorithm parameters used to calculate the key: the primep
, the sub-primeq
, and the baseg
.The EncodedKeySpec Class
This abstract class (which implements the KeySpec Interface) represents a public or private key in encoded format. ItsgetEncoded
method returns the encoded key:public abstract byte[] getEncoded();and itsgetFormat
method returns the name of the encoding format:public abstract String getFormat();JDK 1.2 supplies two classes implementing the EncodedKeySpec interface: PKCS8EncodedKeySpec and X509EncodedKeySpec. If desired, you can supply your own EncodedKeySpec implementations for those or other types of key encodings.
The PKCS8EncodedKeySpec Class
This class, which is a subclass of EncodedKeySpec, represents the DER encoding of a private key, according to the format specified in the PKCS #8 standard.Its
getEncoded
method returns the key bytes, encoded according to the PKCS #8 standard. ItsgetFormat
method returns the string "PKCS#8".The X509EncodedKeySpec Class
This class, which is a subclass of EncodedKeySpec, represents the DER encoding of a public or private key, according to the format specified in the X.509 standard.Its
getEncoded
method returns the key bytes, encoded according to the X.509 standard. ItsgetFormat
method returns the string "X.509".
Below is an edited version of theSun.java
file, which contains a class namedSun
that is the master class for the provider named "SUN". (This provider is supplied with every JDK installation.)As with all master classes, this class is a subclass of Provider. It specifies the class names and package locations of all the cryptographic service implementations supplied by the "SUN" provider. Java Security uses this information to look up the various algorithms and other services when they are requested.
This code is supplied as an example of a master class.
/* * @(#)Sun.java 1.25 98/01/13 * * Copyright 1993-1997 Sun Microsystems, Inc. 901 San Antonio Road, * Palo Alto, California, 94303, U.S.A. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * CopyrightVersion 1.2 * */ package sun.security.provider; import java.io.*; import java.util.*; import java.security.*; /** * The SUN Security Provider. * * @author Benjamin Renaud * * @version 1.25, 98/01/13 */ /** * Defines the "SUN" provider. * * Algorithms supported, and their names: * * - SHA-1 is the message digest scheme decribed in FIPS 180-1. * An alias for SHA-1 is SHA. * * - DSA is the signature scheme described in FIPS 186. (SHA used in * DSA is SHA-1: FIPS 186 with Change No 1.) Aliases for DSA are * SHA/DSA, SHA-1/DSA, SHA1/DSA, DSS and the object identifier * strings "OID.1.3.14.3.2.13", "OID.1.3.14.3.2.27" and * "OID.1.2.840.10040.4.3". * * - DSA is the key generation scheme as described in FIPS 186. * Aliases for DSA include the OID strings "OID.1.3.14.3.2.12" * and "OID.1.2.840.10040.4.1". * * - MD5 is the message digest scheme described in RFC 1321. * There are no aliases for MD5. * * Notes: The name of algorithm described in FIPS-180 is SHA-0, and is * not supported by the SUN provider.) */ public final class Sun extends Provider { private static String info = "SUN " + "(DSA key/parameter generation; DSS signing; " + "SHA-1, MD5 digests)"; public Sun() { /* We are the SUN provider */ super("SUN", 1.2, info); try { AccessController.beginPrivileged(); /* * Signature engines */ put("Signature.DSA", "sun.security.provider.DSA"); put("Alg.Alias.Signature.SHA/DSA", "DSA"); put("Alg.Alias.Signature.SHA1/DSA", "DSA"); put("Alg.Alias.Signature.SHA-1/DSA", "DSA"); put("Alg.Alias.Signature.DSS", "DSA"); put("Alg.Alias.Signature.OID.1.3.14.3.2.13", "DSA"); put("Alg.Alias.Signature.OID.1.3.14.3.2.27", "DSA"); put("Alg.Alias.Signature.OID.1.2.840.10040.4.3", "DSA"); // the following are not according to our formal spec but // are still supported put("Alg.Alias.Signature.1.3.14.3.2.13", "DSA"); put("Alg.Alias.Signature.1.3.14.3.2.27", "DSA"); put("Alg.Alias.Signature.1.2.840.10040.4.3", "DSA"); put("Alg.Alias.Signature.SHAwithDSA", "DSA"); put("Alg.Alias.Signature.SHA1withDSA", "DSA"); /* * Key Pair Generator engines */ put("KeyPairGenerator.DSA", "sun.security.provider.DSAKeyPairGenerator"); put("Alg.Alias.KeyPairGenerator.OID.1.3.14.3.2.12", "DSA"); put("Alg.Alias.KeyPairGenerator.OID.1.2.840.10040.4.1", "DSA"); // the following are not according to our formal spec but // are still supported put("Alg.Alias.KeyPairGenerator.1.3.14.3.2.12", "DSA"); put("Alg.Alias.KeyPairGenerator.1.2.840.10040.4.1", "DSA"); /* * Digest engines */ put("MessageDigest.MD5", "sun.security.provider.MD5"); put("MessageDigest.SHA-1", "sun.security.provider.SHA"); put("Alg.Alias.MessageDigest.SHA", "SHA-1"); put("Alg.Alias.MessageDigest.SHA1", "SHA-1"); /* * Algorithm Parameter Generator engines */ put("AlgorithmParameterGenerator.DSA", "sun.security.provider.DSAParameterGenerator"); /* * Algorithm Parameter engines */ put("AlgorithmParameters.DSA", "sun.security.provider.DSAParameters"); put("Alg.Alias.AlgorithmParameters.1.3.14.3.2.12", "DSA"); put("Alg.Alias.AlgorithmParameters.1.2.840.10040.4.1", "DSA"); /* * Key factories */ put("KeyFactory.DSA", "sun.security.provider.DSAKeyFactory"); } finally { AccessController.endPrivileged(); } } }
Below is a copy of thejava.security
file that appears in every JDK installation. This file appears in thelib/security
(lib\security
on Windows) directory of the JDK. Thus, if the JDK is installed in a directory calledjdk1.2
, the file would be
See Step 5 for an example of adding information about your provider to this file.
jdk1.2/lib/security/java.security
(Solaris)
jdk1.2\lib\security\java.security
(Windows)# # This is the "master security properties file". # # In this file, various security properties are set for use by # java.security classes. This is where users can statically register # Cryptography Package Providers ("providers" for short). The term # "provider" refers to a package or set of packages that supply a # concrete implementation of a subset of the cryptography aspects of # the Java Security API. A provider may, for example, implement one or # more digital signature algorithms or message digest algorithms. # # Each provider must implement a subclass of the Provider class. # To register a provider in this master security properties file, # specify the Provider subclass name and priority in the format # # security.provider.n=className # # This declares a provider, and specifies its preference # order n. The preference order is the order in which providers are # searched for requested algorithms (when no specific provider is # requested). The order is 1-based; 1 is the most preferred, followed # by 2, and so on. # # className must specify the subclass of the Provider class whose # constructor sets the values of various properties that are required # for the Java Security API to look up the algorithms or other # facilities implemented by the provider. # # There must be at least one provider specification in java.security. # There is a default provider that comes standard with the JDK. It # is called the "SUN" provider, and its Provider subclass # named Sun appears in the sun.security.provider package. Thus, the # "SUN" provider is registered via the following: # # security.provider.1=sun.security.provider.Sun # # (The number 1 is used for the default provider.) # # Note: Statically registered Provider subclasses are instantiated # when the system is initialized. Providers can be dynamically # registered instead by calls to either the addProvider or # insertProviderAt method in the Security class. # # List of providers and their preference orders (see above): # security.provider.1=sun.security.provider.Sun # # Class to instantiate as the system Policy. This is the name of the class # that will be used as the Policy object. # policy.provider=java.security.PolicyFile # The default is to have a single system-wide policy file, # and a policy file in the user's home directory. policy.url.1=file:${java.home}/lib/security/java.policy policy.url.2=file:${user.home}/.java.policy # Whether or not we expand properties in the policy file. # If this is set to false, properties (${...}) will not be expanded in policy # files. policy.expandProperties=true # Whether or not we allow an extra policy to be passed on the command line # with -Djava.policy=somefile. Comment out this line to disable # this feature. policy.allowSystemProperty=true # The default user-defined keystore file keystore.user=${user.home}${/}.keystore # # Class to instantiate for X509Certificate. # Other implementations can be instantiated by modifying this # property to point to an appropriate implementation of # X509Certificate, i.e. only a statically registered implementation # can be invoked. # cert.provider.x509=sun.security.x509.X509CertImpl # # Class to instantiate for X509CRL. # Other implementations can be instantiated by modifying this # property to point to an appropriate implementation of # X509CRL, i.e. only a statically registered implementation # can be invoked. # crl.provider.x509=sun.security.x509.X509CRLImpl # # Class to instantiate for KeyStore. # Other implementations can be instantiated by modifying this # property to point to an appropriate implementation of # KeyStore, i.e., only a statically registered implementation # can be invoked. # keystore=sun.security.tools.JavaKeyStore
Copyright © 1996-98 Sun Microsystems, Inc. All Rights Reserved. Please send comments to: java-security@java.sun.com. This is not a subscription list. |
![]() JavaSoft |