The JavaTM Cryptography Extension (JCE) provides a framework and implementations for encryption, key generation and key agreement, and Message Authentication Code (MAC) algorithms. Support for encryption includes symmetric, asymmetric, block, and stream ciphers. The software also supports secure streams and sealed objects.
JCE was previously an optional package (extension) to the JavaTM 2 SDK, Standard Edition (J2SDK), versions 1.2.x and 1.3.x. JCE has now been integrated into the J2SDK, v 1.4.
JCE is based on the same design principles found elsewhere in the Java Cryptography Architecture framework utilized by all the cryptography-related security components of the Java 2 platform: implementation independence and, whenever possible, algorithm independence. It uses the same "provider" architecture, which uses the notion of a Cryptographic Service 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.
JCE extends the list of cryptographic services of which a provider can supply implementations. A provider could, for example, contain an implementation of one or more digital signature algorithms and one or more cipher algorithms.
A program wishing to use cryptography functionality may simply request a particular type of object (such as a Cipher object) implementing a particular algorithm (such as DES) 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 J2SDK installation has one or more provider packages installed. Each provider package supplies implementations of cryptographic services defined in one or more security components of the J2SDK (including JCE).
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.
The J2SDK, v 1.4 release comes standard with a JCE provider named "SunJCE", which comes pre-installed and registered and which supplies the following cryptographic services:
- An implementation of the DES (FIPS PUB 46-1), Triple DES, and Blowfish encryption algorithms in the Electronic Code Book (ECB), Cipher Block Chaining (CBC), Cipher Feedback (CFB), Output Feedback (OFB), and Propagating Cipher Block Chaining (PCBC) modes. (Note: Throughout this document, the terms "Triple DES" and "DES-EDE" will be used interchangeably.)
- Key generators for generating keys suitable for the DES, Triple DES, Blowfish, HMAC-MD5, and HMAC-SHA1 algorithms.
- An implementation of the MD5 with DES-CBC password-based encryption (PBE) algorithm defined in PKCS #5.
- "Secret-key factories" providing bi-directional conversions between opaque DES, Triple DES and PBE key objects and transparent representations of their underlying key material.
- An implementation of the Diffie-Hellman key agreement algorithm between two or more parties.
- A Diffie-Hellman key pair generator for generating a pair of public and private values suitable for the Diffie-Hellman algorithm.
- A Diffie-Hellman algorithm parameter generator.
- A Diffie-Hellman "key factory" providing bi-directional conversions between opaque Diffie-Hellman key objects and transparent representations of their underlying key material.
- Algorithm parameter managers for Diffie-Hellman, DES, Triple DES, Blowfish, and PBE parameters.
- An implementation of the HMAC-MD5 and HMAC-SHA1 keyed-hashing algorithms defined in RFC 2104.
- An implementation of the padding scheme described in PKCS#5.
- A keystore implementation for the proprietary keystore type named "JCEKS".
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
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.
This document is intended for developers of cryptographic service providers. 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.
Only providers signed by a trusted entity can be plugged into the JCE framework.
Related Documentation
This document assumes you have already read the following documents:
- Java Cryptography Architecture API Specification and Reference,
- How to Implement a Provider for the Java Cryptography Architecture, and
- JavaTM Cryptography Extension (JCE) Reference Guide for the JavaTM 2 SDK, Standard Edition, v 1.4.
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:
- java.security package
- java.security.spec package
- java.security.interfaces package
- javax.crypto package
- javax.crypto.spec package
- javax.crypto.interfaces package
A Note on Terminology
The JCE within the J2SDK, v 1.4 includes two software components:
Throughout this document, the term "JCE" by itself refers to the JCE framework in the J2SDK, v 1.4. Whenever the JCE provider supplied with the J2SDK, v 1.4 is mentioned, it will be referred to explicitly as the "SunJCE" provider.
- the framework that defines and supports cryptographic services that providers can supply implementations for. This framework includes everything in the
javax.crypto
package.
- a provider named "SunJCE"
These are the differences between JCE 1.2.1 and the JCE in the J2SDK, v 1.4 that affect providers:
- JCE Is Now in J2SDK
- Strong Cryptography Is the Default, Unlimited Is Available
- Provider Authentication of JCE Framework No Longer Required
JCE Is Now in J2SDK
JCE was previously an optional package (extension) to the JavaTM 2 SDK, Standard Edition (J2SDK), versions 1.2.x and 1.3.x. JCE has now been integrated into the J2SDK, v 1.4. The SunJCE provider is also included and is automatically registered in the
java.security
security properties file included with the J2SDK, v 1.4.Strong Cryptography Is the Default, Unlimited Is Available
Due to import control restrictions, the jurisdiction policy files shipped with the J2SDK, v 1.4 allow "strong" but limited cryptography to be used. An "unlimited" version of these files indicating no restrictions on cryptographic strengths is available for those living in eligible countries (which is most countries). You can download this version and replace the strong cryptography versions supplied with the J2SDK, v 1.4 with the unlimited ones. See
http://java.sun.com/products/jce/index-14.htmlfor information indicating where to go to download the unlimited version.The jurisdiction policy files have been relocated to
where <java-home> refers to the directory where the runtime software is installed, which is the top-level directory of the JavaTM 2 Runtime Environment (JRE) or the jre directory in the JavaTM 2 SDK (J2SDK) software. They have been moved to this standard location so that it is easy to replace the strong cryptography versions that come with the J2SDK, v 1.4 with the unlimited ones.<java-home>\lib\security [Win32] <java-home>/lib/security [Solaris]Provider Authentication of JCE Framework No Longer Required
In JCE 1.2.1, providers needed to include code to authenticate the JCE framework to assure themselves of the integrity and authenticity of the JCE that they plugged into. Now that JCE is integrated into the J2SDK, v 1.4, this is no longer necessary.
JCE 1.2.1 providers which follow the guidance in How to Implement a Provider for the Java Cryptography Extension 1.2.1 will continue to work with the JCE framework in the J2SDK, v 1.4.
However, a provider whose framework authentication code locates the JCE framework via protection domain instead of following the recommendations in the aforementioned JCE 1.2.1 JCE provider document will not work in the J2SDK, v 1.4. Now that JCE has been integrated into the J2SDK, v 1.4, the JCE framework has a null code source just like any other class in the J2SDK, v 1.4. You can either modify your provider to follow the recommended approach for authenticating the framework, or put in a conditional so that the framework authentication code is only executed when the provider is being run with JCE 1.2.1.
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 ciphers or key agreement protocols), or generates or supplies the cryptographic material (keys or parameters) required for cryptographic operations. For example, two of the engine classes are the Cipher and KeyAgreement classes. The Cipher class provides access to the functionality of an encryption algorithm (such as DES), and the KeyAgreement class provides access to the functionality of a key agreement protocol (such as Diffie-Hellman).
The Java Cryptography Architecture encompasses the classes of the J2SE 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.
JCE was previously an optional package (extension) to the JavaTM 2 SDK, Standard Edition (J2SDK), versions 1.2.x and 1.3.x. JCE has now been integrated into the J2SDK, v 1.4.
JCE defines the following engine classes:
- Cipher - used to encrypt or decrypt some specified data.
- KeyAgreement - used to execute a key agreement (key exchange) protocol between 2 or more parties.
- KeyGenerator - used to generate a secret (symmetric) key suitable for a specified algorithm.
- Mac - used to compute the message authentication code of some specified data.
- SecretKeyFactory - used to convert opaque cryptographic keys of type SecretKey into key specifications (transparent representations of the underlying key material), and vice versa.
- ExemptionMechanism - used to provide the functionality of an exemption mechanism such as key recovery, key weakening, key escrow, or any other (custom) exemption mechanism. Applications or applets that use an exemption mechanism may be granted stronger encryption capabilities than those which don't. However, please note that cryptographic restrictions are no longer required for most countries, and thus exemption mechanisms may only be useful in those few countries whose governments mandate restrictions.
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 Cipher engine class, for example, provides access to the functionality of a cipher algorithm. The actual implementation supplied in a CipherSpi subclass (see next paragraph) would be that for a specific kind of encryption algorithm, such as DES or Triple DES.
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 Cipher engine class is the CipherSpi 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 KeyAgreement class, which provides access to a key agreement (key exchange) algorithm. Its implementations, in KeyAgreementSpi subclasses, may be those of various key agreement algorithms such as Diffie-Hellman.
As a final example, the SecretKeyFactory engine class supports the conversion from opaque secret keys to transparent key specifications, and vice versa. (See Key Specification Classes Required by Key Factories.) The actual implementation supplied in a SecretKeyFactorySpi subclass would be that for a specific type of secret keys, e.g., DES keys.
The steps required in order to implement a provider and integrate it into JCE 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
- Step 5a: Get a Code-Signing Certificate
- Step 5b: Place Your Provider in a JAR File
- Step 5c: Sign Your Provider
- Step 5d: Install the Provider
- Step 5e: Set Provider Permissions
- Step 6: Write and Compile Test Programs
- Step 7: Run your Test Programs
- Step 8: Apply for U.S. Government Export Approval If Required
- Step 9: Document your Provider and its Supported Services
- Step 10: Make Your Provider Software 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.Note that your provider may supply implementations of cryptographic services defined in one or more of the security components of the J2SDK v 1.4, including JCE.
In JCE in the J2SDK, v 1.4 (as in the previous JCE 1.2.1 release), you can supply cipher, key agreement and MAC algorithms, as well as secret-key factories, secret-key generation services, and exemption mechanism implementations.
For cryptographic services not defined in JCE (e.g., signatures and message digests), please refer to Java Cryptography Architecture API Specification and Reference.
For each cryptographic service in the J2SDK (including JCE ones), you need to create a subclass of the appropriate SPI class. JCE defines the following engine classes: CipherSpi, KeyAgreementSpi, KeyGeneratorSpi, MacSpi, SecretKeyFactorySpi, and ExemptionMechanismSpi. (See "Engine Classes and Corresponding SPI Classes" within this document for information on the JCE classes and "Engine Classes and Corresponding SPI Classes" within "How to Implement a Provider for the Java Cryptography Architecture" for information on other cryptographic classes in the J2SDK, v 1.4.)
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.
Additional JCE Provider Requirements and Recommendations
When instantiating a provider's implementation (class) of a JCE service, the JCE framework will determine the provider's codebase (JAR file) and verify its signature. In this way, JCE authenticates the provider and ensures that only providers signed by a trusted entity can be plugged into JCE. Thus, one requirement for JCE providers is that they must be signed, as described in later steps.
In addition, each provider should perform self-integrity checking to ensure that the JAR file containing its code has not been manipulated in an attempt to invoke provider methods directly rather than through JCE. For further information, see How a Provider Can Do Self-Integrity Checking.
In order for provider classes to become unusable if instantiated by an application directly, bypassing JCE, providers should implement the following:
- All SPI implementation classes in a provider package should be declared final (so that they cannot be subclassed), and their (SPI) implementation methods should be declared protected.
- All crypto-related helper classes in a provider package should have package-private scope, so that they cannot be accessed from outside the provider package.
For providers that may be exported outside the U.S., CipherSpi implementations must include an implementation of the
engineGetKeySize
method which, given a Key, returns the key size. If there are restrictions on available cryptographic strength specified in jurisdiction policy files, each Cipher initialization method callsengineGetKeySize
and then compares the result with the maximum allowable key size for the particular location and circumstances of the applet or application being run. If the key size is too large, the initialization method throws an Exception.Additional optional features that providers may implement are
- the
engineWrap
andengineUnwrap
CipherSpi methods. "Wrapping" a key enables secure transfer of the key from one place to another. Information about wrapping and unwrapping keys is provided in the Wrapping and Unwrapping Keys section of the Java Cryptography Extension (JCE) Reference Guide.
- one or more "exemption mechanisms." An exemption mechanism is something such as key recovery, key escrow, or key weakening which, if implemented and enforced, may enable reduced cryptographic restrictions for an application (or applet) that utilizes it. For information about the requirements for apps that utilize exemption mechanisms, see How to Make Applications "Exempt" from Cryptographic Restrictions in the Java Cryptography Extension (JCE) Reference Guide.
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 thejava.security.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("CryptoX", 1.0, "CryptoX provider v1.0, implementing " + "RSA encryption and key pair generation, and DES encryption.");- 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 (Cipher, KeyAgreement, KeyGenerator, Mac, SecretKeyFactory, or ExemptionMechanism), 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 JCE services, where the actual algorithm name is substitued for algName:
Cipher.
algNameKeyAgreement.
algNameKeyGenerator.
algNameMac.
algNameSecretKeyFactory.
algNameExemptionMechanism.
algNameIn all of these except
ExemptionMechanism
andCipher
, algName is the "standard" name of the algorithm.In the case of
ExemptionMechanism
, algName refers to the name of the exemption mechanism, which can be one of the following: "KeyRecovery", "KeyEscrow", or "KeyWeakening". Case does not matter.In the case of
Cipher
, algName may actually represent a transformation, and may be composed of an algorithm name, a particular mode, and a padding scheme. (See Appendix A of the Java Cryptography Extension (JCE) Reference Guide 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 "SunJCE" provider implements the Diffie-Hellman key agreement algorithm in a class named
DHKeyAgreement
in thecom.sun.crypto.provider
package. Its subclass of Provider (which is theSunJCE
class in thecom.sun.crypto.provider
package) sets theKeyAgreement.DiffieHellman
property to have the value "com.sun.crypto.provider.DHKeyAgreement" via the following:put("KeyAgreement.DiffieHellman",
"com.sun.crypto.provider.DHKeyAgreement")
For further master class property setting examples, see Appendix A to view the current
SunJCE.java
source file. This shows how theSunJCE
class constructor sets all the properties for the "SunJCE" 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.
As mentioned above, in the case of a
Cipher
property, algName may actually represent a transformation. A transformation is a string that describes the operation (or set of operations) to be performed by a Cipher object on some given input. A transformation always includes the name of a cryptographic algorithm (e.g., DES), and may be followed by a mode and a padding scheme.A transformation is of the form:
- "algorithm/mode/padding" or
- "algorithm"
(In the latter case, provider-specific default values for the mode and padding scheme are used). For example, the following is a valid transformation:
Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");When requesting a block cipher in stream cipher mode (e.g.,
DES
inCFB
orOFB
mode), a client may optionally specify the number of bits to be processed at a time, by appending this number to the mode name as shown in the following sample transformations:
Cipher c1 = Cipher.getInstance("DES/CFB8/NoPadding"); Cipher c2 = Cipher.getInstance("DES/OFB32/PKCS5Padding");If a stream cipher mode is not followed by a number, a provider-specific default is used. (For example, the "SunJCE" provider uses a default of 64 bits.)
A provider may supply a separate class for each combination of algorithm/mode/padding, or may decide to provide more generic classes representing sub-transformations corresponding to algorithm or algorithm/mode or algorithm//padding (note the double slashes), in which case the requested mode and/or padding are set automatically by the
getInstance
methods ofCipher
, which invoke theengineSetMode
andengineSetPadding
methods of the provider's subclass ofCipherSpi
.That is, a
Cipher
property in a provider master class may have one of the following formats:
// provider's subclass of "CipherSpi" implements "algName" with // pluggable mode and paddingCipher.
algName // provider's subclass of "CipherSpi" implements "algName" in the // specified "mode", with pluggable paddingCipher.
algName/mode // provider's subclass of "CipherSpi" implements "algName" with // the specified "padding", with pluggable modeCipher.
algName//padding // provider's subclass of "CipherSpi" implements "algName" with // the specified "mode" and "padding"Cipher.
algName/mode/padding(See Appendix A of the Java Cryptography Extension (JCE) Reference Guide for the standard algorithm names, modes, and padding schemes that should be used.)
For example, a provider may supply a subclass of
CipherSpi
that implements DES/ECB/PKCS5Padding, one that implements DES/CBC/PKCS5Padding, one that implements DES/CFB/PKCS5Padding, and yet another one that implements DES/OFB/PKCS5Padding. That provider would have the followingCipher
properties in its master class:
Cipher.
DES/ECB/PKCS5PaddingCipher.
DES/CBC/PKCS5PaddingCipher.
DES/CFB/PKCS5PaddingCipher.
DES/OFB/PKCS5PaddingAnother provider may implement a class for each of the above modes (i.e., one class for ECB, one for CBC, one for CFB, and one for OFB), one class for PKCS5Padding, and a generic DES class that subclasses from
CipherSpi
. That provider would have the followingCipher
properties in its master class:
Cipher.
DESThe
getInstance
factory method of theCipher
engine class follows these rules in order to instantiate a provider's implementation ofCipherSpi
for a transformation of the form "algorithm":
- Check if the provider has registered a subclass of
CipherSpi
for the specified "algorithm".If the answer is YES, instantiate this class, for whose mode and padding scheme default values (as supplied by the provider) are used.
If the answer is NO, throw a
NoSuchAlgorithmException
exception.The
getInstance
factory method of theCipher
engine class follows these rules in order to instantiate a provider's implementation ofCipherSpi
for a transformation of the form "algorithm/mode/padding":
- Check if the provider has registered a subclass of
CipherSpi
for the specified "algorithm/mode/padding" transformation.If the answer is YES, instantiate it.
If the answer is NO, go to the next step.
- Check if the provider has registered a subclass of
CipherSpi
for the sub-transformation "algorithm/mode".If the answer is YES, instantiate it, and call
engineSetPadding(padding)
on the new instance.If the answer is NO, go to the next step.
- Check if the provider has registered a subclass of
CipherSpi
for the sub-transformation "algorithm//padding" (note the double slashes).If the answer is YES, instantiate it, and call
engineSetMode(mode)
on the new instance.If the answer is NO, go to the next step.
- Check if the provider has registered a subclass of
CipherSpi
for the sub-transformation "algorithm".If the answer is YES, instantiate it, and call
engineSetMode(mode)
andengineSetPadding(padding)
on the new instance.If the answer is NO, throw a
NoSuchAlgorithmException
exception.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
Step 5a: Get a Code-Signing Certificate
The next step is to request a code-signing certificate so that you can use it to sign your provider prior to testing. The certificate will be good for both testing and production. It will be valid for 5 years.
Below are the steps you should use to get a code-signing certificate. For more information on the keytool tool, see keytool (for Solaris) (for Microsoft Windows).
- Use keytool to generate a DSA keypair.
keytool -genkey -alias <alias> -keyalg DSA -keysize 1024 -dname "cn=<Company Name>,ou=Java Software Code Signing, o=Sun Microsystems Inc" -keystore <keystore file name> -storepass <keystore password>(Please note: This must be typed as a single line. Multiple lines and indentation are used in the examples just for legibility purposes.)
This will generate a DSA keypair (a public key and an associated private key) and store it in an entry in the specified keystore. The public key is stored in a self-signed certificate. The keystore entry can subsequently be accessed using the specified alias.
The option values in angle brackets ("<" & ">") represent the actual values that must be supplied. For example, <alias> must be replaced with whatever alias name you wish to be used to refer to the newly-generated keystore entry in the future, and <keystore file name> must be replaced with the name of the keystore to be used. Note: Do not surround actual values with angle brackets. For example, if you want your alias to be
myTestAlias
, specify the-alias
option as follows:-alias myTestAliasIf you specify a keystore that doesn't yet exist, it will be created.
Note: If command lines you type are not allowed to be as long as the
keytool -genkey
command you want to execute (for example, if you are typing to a Microsoft Windows DOS prompt), you can create and execute a plain-text batch file containing the command. That is, create a new text file that contains nothing but the fullkeytool -genkey
command. (Remember to type it all on one line.) Save the file with a .bat extension. Then in your DOS window, type the file name (with its path, if necessary). This will cause the command in the batch file to be executed.
- Use keytool to generate a certificate signing request.
Here, <alias> is the alias for the DSA keypair entry created in the previous step. This command generates a Certificate Signing Request (CSR), using the PKCS#10 format. It stores the CSR in the file whose name is specified in <csr file name>.keytool -certreq -alias <alias> -sigalg DSA -file <csr file name> -keystore <keystore file name> -storepass <keystore password>
- Send the CSR, contact information, and other required documentation to the JCE Code Signing Certification Authority.
Send, via email, the CSR and contact information (see below) to javasoft-cert-request@sun.com. Put the following in the Subject line of your email message:
Request a Certificate for Signing a JCE ProviderPut the contact information in the body of the message and send the CSR file as a plain text attachment to the message. If your mail tool has an option for specifying the encoding format to be used for attachments, select the "MIME" option. Note: The CSR file is just a plain text file, in Base 64 encoding. Only the first and last lines are human-readable.
Include the following contact information in the body of your message:
Company Name Street Address (Not a post office box) City State/Province Country Company Telephone Number Company Fax Number Requester Name Requester Telephone Number Requester Email Address Brief description of your company (size, line of business, etc.)All of the above information is required.
After the JCE Code Signing Certification Authority has received your email message, it will send you a request number, also via email. After receiving this request number, print, fill out, and mail the appropriate Certification Form to the address specified below. The form you need to send depends on your location (inside or outside the U.S.).
- If you are a vendor inside the U.S., fill out and send the following Certification Form for Domestic CSPs.
- If you are a vendor outside the U.S., fill out and send the following Certification Form for Foreign CSPs.
In all cases, the form should be mailed to the address below and you should include the request number on the form so that your hardcopy mailing can be matched to your email message containing the CSR and contact information. The form should be mailed to
Corporate Export Attn: Encryption export 901 San Antonio Road, UPAL01-541 Palo Alto, CA 94303After the JCE Code Signing Certification Authority has received both your email message and the required form, it will authenticate you, the requester. Then it will create and sign a code-signing certificate valid for 5 years. It will send you an email message containing two plain-text file attachments: one file containing this code-signing certificate and another file containing its own CA certificate, which authenticates its public key.
- Use keytool to import the certificates received from the CA.
Once you have received the two certificates from the JCE Code Signing Certification Authority, you can use keytool to import them into your keystore.
First import the CA's certificate as a "trusted certificate":
Then import the code-signing certificate:keytool -import -alias <alias for the CA cert> -file <CA cert file name> -keystore <keystore file name> -storepass <keystore password>Here, <alias> is the same alias as that which you created in step 1 where you generated a DSA keypair. This command replaces the self-signed certificate in the keystore entry specified by <alias> with the one signed by the JCE Code Signing Certification Authority.keytool -import -alias <alias> -file <code-signing cert file name> -keystore <keystore file name> -storepass <keystore password>Now that you have in your keystore a certificate from an entity trusted by JCE (the JCE Code Signing Certification Authority), you can place your provider code in a JAR file (Step 5b) and then use that certificate to sign the JAR file (Step 5c).
Step 5b: Place Your Provider in a JAR File
Place your provider code in a JAR file, in preparation for signing it in the next step. For more information on the jar tool, see jar (for Solaris) (for Microsoft Windows).
jar cvf <JAR file name> <list of classes, separated by spaces>This command creates a JAR file with the specified name containing the specified classes.
Step 5c: Sign Your Provider
Sign the JAR file created in the previous step with the code-signing certificate obtained in Step 5a. For more information on the jarsigner tool, see jarsigner (for Solaris) (for Microsoft Windows).
jarsigner -keystore <keystore file name> -storepass <keystore password> <JAR file name> <alias>Here, <alias> is the alias into the keystore for the entry containing the code-signing certificate received from the JCE Code Signing Certification Authority (the same alias as that specified in the commands in Step 5a).
You can test verification of the signature via the following:
jarsigner -verify <JAR file name>The text "jar verified" will be displayed if the verification was successful.
Step 5d: 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.Installing a provider is done in two steps: 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. You ship your provider classes as a JAR (Java ARchive) file.Install the JAR file containing the provider classes as an "installed" or "bundled" (also known as "download") extension.
Note: The provider JAR file will be considered an installed extension if it is placed in the standard place for the JAR files of an installed extension:
<java-home>/lib/ext [Solaris] <java-home>\lib\ext [Win32]
Here <java-home> refers to the directory where the runtime software is installed, which is the top-level directory of the JavaTM 2 Runtime Environment (JRE) or the jre directory in the JavaTM 2 SDK (J2SDK) software. For example, if you have the J2SDK, v 1.4 installed on Solaris in a directory named
/home/user1/j2sdk1.4.0
, or on Win32 in a directory namedC:\j2sdk1.4.0
, then you need to install the JAR file in the following directory:/home/user1/j2sdk1.4.0/jre/lib/ext [Solaris] C:\j2sdk1.4.0\jre\lib\ext [Win32]Similarly, if you have the JRE, v 1.4 installed on Solaris in a directory named
/home/user1/j2re1.4.0
, or on Win32 in a directory namedC:\j2re1.4.0
, you need to install the JAR file in the following directory:/home/user1/j2re1.4.0/lib/ext [Solaris] C:\j2re1.4.0\lib\ext [Win32]If your provider JAR file is placed anywhere other than the standard place for installed extensions (e.g., in a directory on your class path instead), it will be considered "bundled."
For more information on "installed" extensions, see Installed Extensions.
For more information on "bundled" extensions, see Bundled 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 security properties file
<java-home>/lib/security/java.security [Solaris] <java-home>\lib\security\java.security [Win32]Here <java-home> refers to the directory where the JRE was installed. For example, if you have the J2SDK v 1.4 installed on Solaris in a directory named
/home/user1/j2sdk1.4.0
, or on Win32 in a directory namedC:\j2sdk1.4.0
, then you need to edit the following file:/home/user1/j2sdk1.4.0/jre/lib/security/java.security [Solaris] C:\j2sdk1.4.0\jre\lib\security\java.security [Win32]Similarly, if you have the Java 2 Runtime Environment, v 1.4 installed on Solaris in a directory named
/home/user1/j2re1.4.0
, or on Win32 in a directory namedC:\j2re1.4.0
, then you need to edit this file:/home/user1/j2re1.4.0/lib/security/java.security [Solaris] C:\j2re1.4.0\lib\security\java.security [Win32]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.
The J2SDK, v 1.4 comes standard with a provider named "SUN", which is automatically configured as a static provider in the
java.security
properties file, as follows:security.provider.1=sun.security.provider.Sun(The "SUN" provider's master class is the
Sun
class in thesun.security.provider
package.)The JCE provider "SunJCE" and other security-related providers shipped with the Java 2 platform are also automatically configured as static providers.
To utilize another JCE provider, add a line registering the alternate provider, giving it whatever preference order you prefer (and making corresponding adjustments to the other providers' orders, if needed).
Suppose that your master class is the
CryptoX
class in thecom.cryptox.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, and increment the preference order numbers for all other providers whose numbers were greater than or equal to 2 before your addition:security.provider.2=com.cryptox.provider.CryptoXNote: Providers may also be registered dynamically. To do so, a program (such as your test program, to be written in Step 6) can call either theaddProvider
orinsertProviderAt
method in theSecurity
class. This type of registration is not persistent and can only be done by code which is granted the following permission:wherejava.security.SecurityPermission "insertProvider.{name}"{name}
is replaced by the actual provider name. For example, if the provider name is "MyJCE" and if the provider's code is in themyjce_provider.jar
file in the/localWork
directory, then here is a sample policy filegrant
statement granting that permission:grant codeBase "file:/localWork/myjce_provider.jar" { permission java.security.SecurityPermission "insertProvider.MyJCE"; };Step 5e: Set Provider Permissions
Whenever JCE providers are not installed extensions, permissions must be granted for when applets or applications using JCE are run while a security manager is installed. There is typically a security manager installed whenever an applet is running, and a security manager may be installed for an application either via code in the application itself or via a command-line argument. Permissions do not need to be granted to installed extensions, since the default system policy file grants all permissions to installed extensions.
Whenever a client does not install your provider as an installed extension, your provider may need the following permissions granted to it in the client environment:
java.lang.RuntimePermission
to get class protection domains. The provider may need to get its own protection domain in the process of doing self-integrity checking.java.security.SecurityPermission
to set provider properties.In order to ensure your provider works when a security manager is installed and the provider is not an installed extension, you need to test such an installation and execution environment and prior to testing you need to grant appropriate permissions to your provider and to any other providers it utilizes. For example, a sample statement granting permissions to a provider whose name is "MyJCE" and whose code is in
myjce_provider.jar
appears below. Such a statement could appear in a policy file. In this example, themyjce_provider.jar
file is assumed to be in the/localWork
directory.grant codeBase "file:/localWork/myjce_provider.jar" { permission java.lang.RuntimePermission "getProtectionDomain"; permission java.security.SecurityPermission "putProviderProperty.MyJCE"; };Step 6: Write and Compile your Test Programs
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 encrypted.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 the DES encryption algorithm, you could check to ensure it's found when requested by using the following code (again substituting your provider name for "MyPro"):
Cipher c = Cipher.getInstance("DES", "MyPro"); System.out.println("My Cipher algorithm name is " + c.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.If your provider implements an exemption mechanism, you should write a test applet or application that utilizes the exemption mechanism. Such an applet/application also needs to be signed, and needs to have a "permission policy file" bundled with it. See How to Make Applications "Exempt" from Cryptographic Restrictions in the Java Cryptography Extension (JCE) Reference Guide for complete information about creating and testing such an application.
Step 7: Run your Test Programs
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.
Be sure to include testing of your programs using different installation options (e.g. making the provider an installed extension or placing it on the class path) and execution environments (with or without a security manager running). Installation options are discussed in Step 5d. In particular, in order to ensure your provider works when a security manager is installed and the provider is not an installed extension -- and thus the provider must have permissions granted to it -- you need to test such an installation and execution environment, after granting required permissions to your provider and to any other providers it utilizes, as described in Step 5e.
If you find during testing that your code needs modifications, make the modifications, recompile (Step 4), place the updated provider code in a JAR file (Step 5b), sign the JAR file (Step 5c), re-install the provider (Step 5d), if needed fix or add to the permissions (Step 5e), and then re-test your programs and repeat these steps as needed.
Step 8: Apply for U.S. Government Export Approval If Required
All U.S. vendors whose providers may be exported outside the U.S. should apply to the Bureau of Export Administration in the U.S. Department of Commerce for export approval. Please consult your export counsel for more information.
Please note: If your provider calls
Cipher.getInstance()
and the returned Cipher object needs to perform strong cryptography regardless of what cryptographic strength is allowed by the user's downloaded jurisdiction policy files, you should include a copy of thecryptoPerms
permission policy file which you intend to bundle in the JAR file for your provider and which specifies an appropriate permission for the required cryptographic strength. The necessity for this file is just like the requirement that applets and applications "exempt" from cryptographic restrictions must include acryptoPerms
permission policy file in their JAR file. For more information about creation and inclusion of such a file, see How to Make Applications "Exempt" from Cryptographic Restrictions in the Java Cryptography Extension (JCE) Reference Guide.Here are a couple URLs that may be of use to you:
- U.S. Department of Commerce: http://www.doc.gov
- Bureau of Export Administration: http://www.bxa.doc.gov
Step 9: Document your Provider and its Supported Services
The next 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 "CryptoX" but a user requests "CRYPTOx", 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 5d, except that the information and examples should be specific to your provider.
- the permissions your provider will require if it is not installed as an installed extension and if a security manager is run, as described in Step 5e.
MACs
For each MAC 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 "message authentication codes" (MACs) may be possible through cloning. Clients who do not know whether or not a MAC implementation is cloneable can find out by attempting to clone the Mac object and catching the potential exception, as illustrated by the following example:
try { // try and clone it /* compute the MAC for i1 */ mac.update(i1); byte[] i1Mac = mac.clone().doFinal(); /* compute the MAC for i1 and i2 */ mac.update(i2); byte[] i12Mac = mac.clone().doFinal(); /* compute the MAC for i1, i2 and i3 */ mac.update(i3); byte[] i123Mac = mac.doFinal(); } catch (CloneNotSupportedException cnse) { // have to use an approach not involving cloning }where
mac
is the MAC object they received when they requested one via a call toMac.getInstance
,
i1
,i2
andi3
are input byte arrays, and
- they want to calculate separate hashes for:
i1
i1 and i2
i1, i2, and i3
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 Diffie-Hellman key pair generator supplied by the "SunJCE" provider uses a default prime modulus size (keysize) of 1024 bits.Key Factories
A provider should document all the key specifications supported by its (secret-)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 "SunJCE" provider uses a default prime modulus size (keysize) of 1024 bits for the generation of Diffie-Hellman parameters.Step 10: Make your Provider Software and Documentation Available to Clients
The final step is to make your provider software and documentation available to your customers.
Each provider should do self-integrity checking to ensure that the JAR file containing its code has not been tampered with, for example in an attempt to invoke provider methods directly rather than through JCE.
Providers that provide implementations for JCE services must be digitally signed. Certain Certification Authorities will be deemed to be "trusted" and any code signed using a certificate that can be traced up a certificate chain to a certificate for one of the trusted Certification Authorities will be considered trusted. Provider packages should embed within themselves the bytes for the certificates for the relevant trusted Certification Authorities, for example in an array like the
trustedCaCerts
array referenced in the Determining Whether Any of the Root Certificates is Trusted section below. At runtime, the embedded certificates will be used in determining whether or not the provider code is authentic.Currently, there are two trusted Certification Authorities: Sun Microsystems' JCE Code Signing CA, and IBM JCE Code Signing CA. Please refer to Step 5b for detailed information about how to get a code-signing certificate from Sun Microsystems' JCE Code Signing CA and the certificate of that CA.
The basic approach a provider can use to check its own integrity is:
- Determine the URL of the JAR file containing the provider code, and
- Verify the JAR file's digital signatures to ensure at least one signer of each entry of the JAR file is trusted.
Each of these steps is described below. For each, initially simplified sample basic code is provided, followed by a complete coding example. Here is an outline for the sections:
- Finding the Provider JAR File: Basics
- Determining the Provider's JAR File URL
- Creating a JarFile Referring to the JAR File
- Finding the Provider JAR File: Complete Example
- Verifying the Provider JAR File: Basics
- Verification Setup
- JAR File Signature Check
- Verifying Signatures
- Ensuring Signers Are Trusted
- Getting the List of Signer Certificates
- Determining the Roots of the Signer Certificate Chains
- Determining Whether Any of the Root Certificates is Trusted
- Verifying the Provider JAR File: Complete Example
IMPORTANT NOTE:In JCE 1.2.1, providers needed to include code to authenticate the JCE framework to assure themselves of the integrity and authenticity of the JCE that they plugged into. Now that JCE is integrated into the J2SDK, v 1.4, this is no longer necessary.
One implication is that a provider written just for JCE 1.2.1 will not work in the J2SDK, v 1.4 because the provider's JCE framework authentication check will not work; the JCE framework code is no longer where the provider expects it to be. If you want your provider to work only with the J2SDK, v 1.4, it should not have code to authenticate the JCE framework. If on the other hand you want your provider to work both with JCE 1.2.1 and with the JCE in the J2SDK, v 1.4, then put in a conditional so that the provider code to authenticate the JCE framework is only executed when the provider is being run with JCE 1.2.1. Here is sample code:
Class cipherCls = Class.forName("javax.crypto.Cipher"); CodeSource cs = cipherCls.getProtectionDomain().getCodeSource(); if (cs != null) { // authenticate JCE framework . . . }
Finding the Provider JAR File: Basics
Determining the Provider's JAR File URL
The URL for the provider's JAR file can be obtained by determining the provider's CodeSource and then calling the
getLocation
method on the CodeSource. The basic idea is:Class cc = this.getClass(); CodeSource s1 = cc.getProtectionDomain().getCodeSource(); URL providerURL = s1.getLocation();Creating a JarFile Referring to the JAR File
Once you have the URL for the provider's JAR file, you can create a
java.util.jar.JarFile
referring to the JAR file. This is needed in the step for verifying the Provider JAR file. How can you create the JarFile? First, open a connection to the specified URL by calling itsopenConnection
method. Since the URL is a JAR URL, the result is ajava.net.JarURLConnection
. The basic code is:Now that you have a JarURLConnection, you can call itsJarURLConnection jc = (JarURLConnection)providerURL.openConnection();getJarFile
method to get the JAR file:Then you can call theJarFile jf = (JarFile)jc.getJarFile();verifySingleJarFile
method shown in Verifying the Provider JAR File: Complete Example, which verifies the JAR file passed to it as an argument.verifySingleJarFile(jf);Finding the Provider JAR File: Complete Example
Below is a complete example of determining the URL of the provider JAR file (via the
getProviderURL
method) and then creating a JarFile referring to the JAR file and passing that as an argument to theverifySingleJarFile
method described in the following sections and shown in full in Verifying the Provider JAR File: Complete Example. Included is all the basic code described above, plus further details such as error handling and use ofAccessController.doPrivileged
where needed. (See API For Privileged Blocks for information on the use ofdoPrivileged
.)Here we define a sample method called
selfIntegrityChecking
that returns aboolean
value telling whether or not the integrity check succeeds. Each SPI implementation class constructor should call this method and throw a SecurityException if it returnsfalse
.// For efficiency, keep track of whether or not the provider // has already been verified to avoid doing it // multiple times unnecessarily. private static boolean verifiedSelfIntegrity = false; // Provider self-integrity checking. // Call the following method in the constructor // of your SPI implementation classes. private static final synchronized boolean selfIntegrityChecking() { if (verifiedSelfIntegrity) { return true; } URL providerURL = getProviderURL(this.getClass()); if (providerURL == null) { return false; } // Open a connnection to the provider JAR file JarFile jf; try { final URL url = providerURL.getProtocol().equalsIgnoreCase("jar") ? providerURL : new URL("jar:" + providerURL.toString() + "!/"); jf = (JarFile)AccessController.doPrivileged( new PrivilegedExceptionAction() { public Object run() throws Exception { return ((JarURLConnection) url.openConnection()).getJarFile(); } }); } catch (java.security.PrivilegedActionException pae) { return false; } // Verify the provider JAR file try { verifySingleJarFile(jf); } catch (Exception e) { return false; } verifiedSelfIntegrity = true; return true; } /* * Returns the URL of the JAR file containing the specified class. */ private static URL getProviderURL(Class c) { final Class cc = c; return (URL)AccessController.doPrivileged( new PrivilegedAction() { public Object run() { CodeSource s1 = cc.getProtectionDomain().getCodeSource(); return s1.getLocation(); } }); }Verifying the Provider JAR File: Basics
Once you have determined the URL for your provider JAR file and you have created a JarFile referring to the JAR file, as shown in the Finding the Provider JAR File: Complete Example above, you can then verify the file.
The basic approach is:
- Ensure the JAR file is a signed JAR file,
- Go through all the entries in the JAR file and ensure the signature on each one verifies correctly, and
- Ensure at least one of each entry's signer's certificates can be traced back to a trusted Certification Authority.
Sample code for each of these steps is presented and described below, followed by the complete code example. Here is an outline for the sections, starting with the current one:
- Verifying the Provider JAR File: Basics
- Verification Setup
- JAR File Signature Check
- Verifying Signatures
- Ensuring Signers Are Trusted
- Getting the List of Signer Certificates
- Determining the Roots of the Signer Certificate Chains
- Determining Whether Any of the Root Certificates is Trusted
- Verifying the Provider JAR File: Complete Example
Verification Setup
Our approach will define a method
verifySingleJarFile
which takes as a parameter the JarFile created in the previous step.
verifySingleJarFile
will basically go through the JAR file entries twice. As an efficiency measure, it will allocate a VectorentriesVec
and the first time through the loop processing the JAR file entries, it will add each entry toentriesVec
, for use by the second loop. Here is the first part of our method:private static void verifySingleJarFile(JarFile jf) throws IOException, CertificateException { Vector entriesVec = new Vector();JAR File Signature Check
An authentic provider JAR file is signed. So the JAR file has been tampered with if it isn't signed:
Manifest man = jf.getManifest(); if (man == null) throw new SecurityException( "The provider is not signed");Verifying Signatures
The next step is to go through all the entries in the JAR file and ensure the signature on each one verifies correctly. One possible way to verify the signature on a JAR file entry is to simply read the file. If a JAR file is signed, the
read
method itself automatically performs the signature verification. Here is sample code:byte[] buffer = new byte[8192]; Enumeration entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry je = (JarEntry)entries.nextElement(); entriesVec.addElement(je); InputStream is = jf.getInputStream(je); int n; while ((n = is.read(buffer, 0, buffer.length)) != -1) { // we just read. this will throw a SecurityException // if a signature/digest check fails. } is.close(); } jf.close();Ensuring Signers Are Trusted
The code in the previous section verified the signatures of all the provider JAR file entries. The fact that they all verify correctly is a requirement, but it is not sufficient to verify the authenticity of the JAR file. A final requirement is that the signatures were generated by an entity that JCE trusts. In order to test that, we can again go through each entry in the JAR file (this time using the
entriesVec
built in the previous step), and for each entry that must be signed (that is, each entry that is not a directory and that is not in the META-INF directory):The loop setup is the following:
- Get the list of signer certificates for the entry.
- Determine the roots of the signer certificate chains.
- Determine whether any of the root certificates is trusted. At least one of the signers must be trusted.
Enumeration e = entriesVec.elements(); while (e.hasMoreElements()) { JarEntry je = (JarEntry) e.nextElement();Getting the List of Signer Certificates
The certificates for the signers of a JAR file entry JarEntry can be obtained simply by calling the JarEntry
getCertificates
method:Certificate[] certs = je.getCertificates();Adding this to the previous loop setup code, and adding code to ignore directories and files in the META-INF directory gives us:
Enumeration e = entriesVec.elements(); while (e.hasMoreElements()) { JarEntry je = (JarEntry) e.nextElement(); if (je.isDirectory()) continue; // Every file must be signed - except // files in META-INF Certificate[] certs = je.getCertificates(); if ((certs == null) || (certs.length == 0)) { if (!je.getName().startsWith("META-INF")) throw new SecurityException("The provider " + "has unsigned " + "class files."); } else { // Check whether the file // is signed as expected. . . . }Determining the Roots of the Signer Certificate Chains
The certificate array returned by the JarEntry
getCertificates
method contains one or more certificate chains. There is one chain per signer of the entry. Each chain contains one or more certificates. Each certificate in a chain authenticates the public key in the previous certificate. The first certificate in a chain is the certificate containing the public key corresponding to the private key actually used to sign the entry. Each subsequent certificate is a certificate for the entity who signed the previous certificate. The last certificate in a chain is eitherWe need to go through the array of certificate chains to determine the final certificate in each chain, referred to as the "root" certificate:
- the certificate for a Certification Authority, or
- a certificate issued by a Certification Authority
TheCertificate[] chainRoots = getChainRoots(certs);getChainRoots
method is defined as follows:private static Certificate[] getChainRoots(Certificate[] certs) { Vector result = new Vector(3); // choose a Vector size that seems reasonable for (int i = 0; i < certs.length - 1; i++) { if (!((X509Certificate)certs[i + 1]).getSubjectDN().equals( ((X509Certificate)certs[i]).getIssuerDN())) { // We've reached the end of a chain result.addElement(certs[i]); } } // The final entry in the certs array is always // a "root" certificate result.addElement(certs[certs.length - 1]); Certificate[] ret = new Certificate[result.size()]; result.copyInto(ret); return ret; }Determining Whether Any of the Root Certificates is Trusted
Now that we have (in the
chainRoots
array) the list of root certificates for all the signers of a provider JAR file entry, we can go through that array and determine whether or not any of the root certificates is a certificate for a "trusted" entity. At least one of the root certificates for each JAR file entry must be trusted. Assume thattrustedCaCerts
is an array of certificates for trusted Certification Authorities. Then sample code is the following:A root certificate is considered "trusted" if one of the following is true:boolean signedAsExpected = false; for (int i = 0; i < chainRoots.length; i++) { if (isTrusted((X509Certificate)chainRoots[i], trustedCaCerts)) { signedAsExpected = true; break; } } if (!signedAsExpected) { throw new SecurityException("The provider " + "is not signed by a " + "trusted signer"); }
- It is the certificate for a trusted Certification Authority. (In the following sample code, this is true if it is the same as a certificate in the
trustedCaCerts
array.)
- It is a certificate issued by a trusted Certification Authority. This is true if the certificate's IssuerDN is the same as the SubjectDN of one of the
trustedCaCerts
certificates and you can verify the signature. (The IssuerDN of a certificate is the name of the entity that signed the certificate, while the SubjectDN identifies the entity whose public key is being identified by the certificate.)Here is sample code:
private static boolean isTrusted(X509Certificate cert, X509Certificate[] trustedCaCerts) { // Return true iff either of the following is true: // 1) the cert is in the trustedCaCerts. // 2) the cert is issued by a trusted CA. // Check whether the cert is in the trustedCaCerts for (int i = 0; i < trustedCaCerts.length; i++) { // If the cert has the same SubjectDN // as a trusted CA, check whether // the two certs are the same. if (cert.getSubjectDN().equals( trustedCaCerts[i].getSubjectDN())) { if (cert.equals(trustedCaCerts[i])) { return true; } } } // Check whether the cert is issued by a trusted CA. // Signature verification is expensive. So we check // whether the cert is issued // by one of the trusted CAs iff the above loop failed. for (int i = 0; i < trustedCaCerts.length; i++) { // If the issuer of the cert has the same name as // a trusted CA, check whether that trusted CA // actually issued the cert. if (cert.getIssuerDN().equals( trustedCaCerts[i].getSubjectDN())) { try { cert.verify(trustedCaCerts[i].getPublicKey()); return true; } catch (Exception e) { // Do nothing. } } } return false; }Verifying the Provider JAR File: Complete Example
Below is the complete code for verifying the provider JAR file. It assumes the parameter
jf
refers to a JarFile that is open (and thus needs a close). It also assumestrustedCaCerts
is the array of Certificates for trusted Certification Authorities.private static void verifySingleJarFile(JarFile jf) throws IOException, CertificateException { Vector entriesVec = new Vector(); // Ensure there is a manifest file Manifest man = jf.getManifest(); if (man == null) throw new SecurityException( "The provider is not signed"); // Ensure all the entries' signatures verify correctly byte[] buffer = new byte[8192]; Enumeration entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry je = (JarEntry)entries.nextElement(); entriesVec.addElement(je); InputStream is = jf.getInputStream(je); int n; while ((n = is.read(buffer, 0, buffer.length)) != -1) { // we just read. this will throw a SecurityException // if a signature/digest check fails. } is.close(); } jf.close(); // Get the list of signer certificates Enumeration e = entriesVec.elements(); while (e.hasMoreElements()) { JarEntry je = (JarEntry) e.nextElement(); if (je.isDirectory()) continue; // Every file must be signed - except // files in META-INF Certificate[] certs = je.getCertificates(); if ((certs == null) || (certs.length == 0)) { if (!je.getName().startsWith("META-INF")) throw new SecurityException("The provider " + "has unsigned " + "class files."); } else { // Check whether the file // is signed as expected. // The framework may be signed by // multiple signers. At least one of // the signers must be a trusted signer. // First, determine the roots of the certificate chains Certificate[] chainRoots = getChainRoots(certs); boolean signedAsExpected = false; for (int i = 0; i < chainRoots.length; i++) { if (isTrusted((X509Certificate)chainRoots[i], trustedCaCerts)) { signedAsExpected = true; break; } } if (!signedAsExpected) { throw new SecurityException("The provider " + "is not signed by a " + "trusted signer"); } } } } private static boolean isTrusted(X509Certificate cert, X509Certificate[] trustedCaCerts) { // Return true iff either of the following is true: // 1) the cert is in the trustedCaCerts. // 2) the cert is issued by a trusted CA. // Check whether the cert is in the trustedCaCerts for (int i = 0; i < trustedCaCerts.length; i++) { // If the cert has the same SubjectDN // as a trusted CA, check whether // the two certs are the same. if (cert.getSubjectDN().equals( trustedCaCerts[i].getSubjectDN())) { if (cert.equals(trustedCaCerts[i])) { return true; } } } // Check whether the cert is issued by a trusted CA. // Signature verification is expensive. So we check // whether the cert is issued // by one of the trusted CAs iff the above loop failed. for (int i = 0; i < trustedCaCerts.length; i++) { // If the issuer of the cert has the same name as // a trusted CA, check whether that trusted CA // actually issued the cert. if (cert.getIssuerDN().equals( trustedCaCerts[i].getSubjectDN())) { try { cert.verify(trustedCaCerts[i].getPublicKey()); return true; } catch (Exception e) { // Do nothing. } } } return false; } private static Certificate[] getChainRoots(Certificate[] certs) { Vector result = new Vector(3); // choose a Vector size that seems reasonable for (int i = 0; i < certs.length - 1; i++) { if (!((X509Certificate)certs[i + 1]).getSubjectDN().equals( ((X509Certificate)certs[i]).getIssuerDN())) { // We've reached the end of a chain result.addElement(certs[i]); } } // The final entry in the certs array is always // a "root" certificate result.addElement(certs[certs.length - 1]); Certificate[] ret = new Certificate[result.size()]; result.copyInto(ret); return ret; }
Algorithm Aliases
For many cryptographic algorithms, there is a single official "standard name". The standard names defined by JCE in the J2SDK, v 1.4 are listed in Appendix A of the Java Cryptography Extension (JCE) Reference Guide.For example, "DiffieHellman" is the standard name for the Diffie-Hellman key agreement algorithm defined in PKCS #3.
JCE uses the same aliasing scheme for algorithm names as the rest of the security products in the J2SDK, v 1.4. That scheme enables clients to use aliases when referring to algorithms, rather than their standard names. For example, the "SunJCE" provider's master class (
SunJCE.java
) defines the alias "DH" for the key agreement whose standard name is "DiffieHellman". Thus, the following statements are equivalent:KeyAgreement ka = KeyAgreement.getInstance("DiffieHellman", "SunJCE"); KeyAgreement ka = KeyAgreement.getInstance("DH", "SunJCE");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 Cipher, KeyAgreement, KeyGenerator, Mac, SecretKeyFactory, or ExemptionMechanism, and aliasName is your alias name. For all but ExemptionMechanism, the value of the property must be the standard algorithm name for the algorithm being aliased. For ExemptionMechanism, the value is the exemption mechanism name ("KeyRecovery", "KeyEscrow", or "KeyWeakening").
As an example, the "SunJCE" provider defines the alias "DH" for the key agreement algorithm whose standard name is "DiffieHellman" by setting a property named
Alg.Alias.KeyAgreement.DH
to have the valueDiffieHellman
via the following:put("Alg.Alias.KeyAgreement.DH", "DiffieHellman");Currently, aliases defined by the "SunJCE" provider are available to all clients, no matter which provider clients request. For example, if you create a provider named "MyPro" that implements the Diffie-Hellman algorithm, then even if you don't define any aliases for it, the "DH" alias defined by "SunJCE" can be used to refer to your provider's Diffie-Hellman implementation as follows:
KeyAgreement ka = KeyAgreement.getInstance("DH", "MyPro");
Service Interdependencies
Some algorithms require the use of other types of algorithms. For example, a PBE algorithm usually needs to use a message digest algorithm in order to transform a password into a key.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 Java 2 Platform installation. For example, if you are implementing a PBE 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.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 "SunJCE" provider uses a default modulus size (keysize) of 1024 bits for the generation of Diffie-Hellman parameters.
Diffie-Hellman Interfaces and their Required Implementations
JCE contains the following interfaces (in thejavax.crypto.interfaces
package) for the convenience of programmers implementing Diffie-Hellman services: The following sections discuss requirements for implementations of these interfaces.DHPrivateKey and DHPublicKey Implementations
If you implement a Diffie-Hellman key pair generator or key factory, you need to create classes implementing the DHPrivateKey and DHPublicKey interfaces.If you implement a Diffie-Hellman key pair generator, your
generateKeyPair
method (in your KeyPairGeneratorSpi subclass) will return instances of your implementations of those interfaces.If you implement a Diffie-Hellman key factory, your
engineGeneratePrivate
method (in your KeyFactorySpi subclass) will return an instance of your DHPrivateKey implementation, and yourengineGeneratePublic
method will return an instance of your DHPublicKey implementation.Also, your
engineGetKeySpec
andengineTranslateKey
methods will expect the passed-in key to be an instance of a DHPrivateKey or DHPublicKey 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 DHParameterSpec constructor called to create a parameter specification from parameter values that could be used to initialize a KeyPairGenerator object for Diffie-Hellman.If you implement the Diffie-Hellman key agreement algorithm, your
engineInit
method (in your KeyAgreementSpi subclass) will expect to be passed a DHPrivateKey and yourengineDoPhase
method will expect to be passed a DHPublicKey.Please note: The DHPublicKey and DHPrivateKey interfaces define a very generic, provider-independent interface to Diffie-Hellman public and private keys, respectively. The
engineGetKeySpec
andengineTranslateKey
methods (in your KeyFactorySpi subclass) could additionally check if the passed-in key is actually an instance of their provider's own implementation of DHPrivateKey or DHPublicKey, e.g., to take advantage of provider-specific implementation details. The same is true for the Diffie-Hellman algorithmengineInit
andengineDoPhase
methods (in your KeyAgreementSpi subclass).To see what methods need to be implemented by classes that implement the DHPublicKey and DHPrivateKey interfaces, first note the following interface signatures:
In the javax.crypto.interfaces package: public interface DHPrivateKey extends DHKey, java.security.PrivateKey public interface DHPublicKey extends DHKey, java.security.PublicKey public interface DHKey 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 DHPrivateKey and DHPublicKey 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
Similarly, for public Diffie-Hellman keys, you need to supply a class that implements
- the
getX
method from the DHPrivateKey interface.
- the
getParams
method from the javax.crypto.interfaces.DHKey interface, since DHPrivateKey extends DHKey.- the
getAlgorithm
,getEncoded
, andgetFormat
methods from the java.security.Key interface, since DHPrivateKey extendsjava.security.PrivateKey
, and PrivateKey extends Key.
- the
getY
method from the DHPublicKey interface.
- the
getParams
method from the javax.crypto.interfaces.DHKey interface, since DHPublicKey extends DHKey.- the
getAlgorithm
,getEncoded
, andgetFormat
methods from the java.security.Key interface, since DHPublicKey extendsjava.security.PublicKey
, and PublicKey extends Key.Algorithm Parameter Specification 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., DHParameterSpec defines
getP
,getG
, andgetL
methods, to access the p, g, and l 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.
JCE contains a number of AlgorithmParameterSpec implementations for the most frequently used cipher and key agreement algorithm parameters. If you are operating on algorithm parameters that should be for a different type of algorithm not provided by JCE, you will need to supply your own AlgorithmParameterSpec implementation appropriate for that type of algorithm.
JCE defines the following algorithm parameter specification classes in the
javax.crypto.spec
package:The IvParameterSpec Class
This class (which implements the AlgorithmParameterSpec interface) specifies the initialization vector (IV) used with a cipher in feedback mode. It has the following method:public byte[] getIV()This method returns the IV.The PBEParameterSpec Class
This class (which implements the AlgorithmParameterSpec interface) specifies the set of parameters used with a password-based encryption (PBE) algorithm. It has the following methods:public byte[] getSalt() public int getIterationCount()These methods return the PBE parameters: the salt and the iteration count.The RC2ParameterSpec Class
This class (which implements the AlgorithmParameterSpec interface) specifies the set of parameters used with the RC2 algorithm. It has the following methods:public int getEffectiveKeyBits() public byte[] getIV()These methods return the RC2 algorithm parameters: the effective key size (in bits) and the IV.The RC5ParameterSpec Class
This class (which implements the AlgorithmParameterSpec interface) specifies the set of parameters used with the RC5 algorithm. It has the following methods:public int getVersion() public int getRounds() public int getWordSize() public byte[] getIV()These methods return the RC5 algorithm parameters: the version number, number of rounds, word size (in bits), and the IV.The DHParameterSpec Class
This class (which implements the AlgorithmParameterSpec interface) specifies the set of parameters used with the Diffie-Hellman algorithm. It has the following methods:public BigInteger getP() public BigInteger getG() public int getL()These methods return the Diffie-Hellman algorithm parameters: the prime modulusp
, the base generatorg
, and the size in bits of the random exponent (private value),l
.Many types of Diffie-Hellman services will find this class useful - for example, it is utilized by the Diffie-Hellman key agreement, key pair generator, algorithm parameter generator, and algorithm parameters classes implemented by the "SunJCE" provider. As a specific example, an algorithm parameters implementation must include an implementation for the
getParameterSpec
method, which returns an AlgorithmParameterSpec. The Diffie-Hellman algorithm parameters implementation supplied by "SunJCE" returns an instance of the DHParameterSpec class.Key Specification Classes Required by Key Factories
Key specifications are transparent representations of the key material that constitutes a key. JCE defines the following key specification classes in the
javax.crypto.spec
package: DHPrivateKeySpec, DHPublicKeySpec, DESKeySpec, and DESedeKeySpec.The DHPrivateKeySpec Class
This class (which implements the KeySpec interface) specifies a Diffie-Hellman private key with its associated parameters. It has the following methods:public BigInteger getX() public BigInteger getP() public BigInteger getG()These methods return the private valuex
, and the Diffie-Hellman algorithm parameters used to calculate it: the prime modulusp
and the base generatorg
.The DHPublicKeySpec Class
This class (which implements the KeySpec interface) specifies a Diffie-Hellman public key with its associated parameters. It has the following methods:public BigInteger getY() public BigInteger getP() public BigInteger getG()These methods return the public valuey
, and the Diffie-Hellman algorithm parameters used to calculate it: the prime modulusp
and the base generatorg
.The DESKeySpec Class
This class (which implements the KeySpec interface) specifies a DES key. It has the following methods:public byte[] getKey() public static boolean isParityAdjusted(byte[] key, int offset) public static boolean isWeak(byte[] key, int offset)The first method returns the DES key bytes. The other (class) methods check if a given DES key is parity adjusted or weak, respectively.The DESedeKeySpec Class
This class (which implements the KeySpec interface) specifies a DES-EDE (Triple DES) key. It has the following methods:public byte[] getKey() public static boolean isParityAdjusted(byte[] key, int offset)The first method returns the DES-EDE key bytes. The other (class) method checks if a given DES-EDE key is parity adjusted.The SecretKeySpec Class
This class implements the KeySpec interface. Since it also implements the SecretKey interface, it can be used to construct aSecretKey
object in a provider-independent fashion, i.e., without having to go through a provider-basedSecretKeyFactory
. It has the following methods:public SecretKeySpec(byte[] key, String algorithm) public SecretKeySpec(byte[] key, int offset, int len, String algorithm) public String getAlgorithm() public String getFormat() public byte[] getEncoded()The two constructors take a byte array that constitutes the secret-key material, and the name of the associated secret-key algorithm.
The other methods return the secret-key algorithm name, key format (given as the string "RAW"), and secret-key bytes, respectively.
Secret-Key Generation
If you provide a secret-key generator (subclass ofjavax.crypto.KeyGeneratorSpi
) for a particular secret-key algorithm, you may return the generated secret-key object (which must be an instance ofjavax.crypto.SecretKey
, see engineGenerateKey) in one of the following ways:
- You implement a class whose instances represent secret-keys of the algorithm associated with your key generator. Your key generator implementation returns instances of that class. This approach is useful if the keys generated by your key generator have provider-specific properties.
- Your key generator returns an instance of SecretKeySpec, which already implements the
javax.crypto.SecretKey
interface. You pass the (raw) key bytes and the name of the secret-key algorithm associated with your key generator to theSecretKeySpec
constructor. This approach is useful if the underlying (raw) key bytes can be represented as a byte array and have no key-parameters associated with them.Ensuring Exportability
A key feature of JCE is the exportability of the JCE framework and of the provider cryptography implementations if certain conditions are met.
Due to import control restrictions by the governments of a few countries, the jurisdiction policy files shipped with the J2SDK, v 1.4 from Sun Microsystems specify that "strong" but limited cryptography may be used. An "unlimited" version of these files indicating no restrictions on cryptographic strengths is available for those living in eligible countries (which is most countries). But only the "strong" version can be imported into those countries whose governments mandate restrictions. The JCE framework will enforce the restrictions specified in the installed jurisdiction policy files.
As noted elsewhere, you can write just one version of your provider software, implementing cryptography of maximum strength. It is up to JCE, not your provider, to enforce any jurisdiction policy file-mandated restrictions regarding the cryptographic algorithms and maximum cryptographic strengths available to applets/applications in different locations.
The conditions that must be met by your provider in order to enable it to be plugged into JCE in the J2SDK, v 1.4 are the following:
- The constructor of each SPI implementation class should do self-integrity checking, as described in How a Provider Can Do Self-Integrity Checking.
- The provider code should be written in such a way that provider classes become unusable if instantiated by an application directly, bypassing JCE. See Step 1: Write Your Service Implementation Code in the Steps to Implement and Integrate a Provider section.
- The provider package must be signed by an entity trusted by the JCE framework. (See Step 5a through Step 5c.) U.S. vendors whose providers may be exported outside the U.S. first need to apply for U.S. government export approval. (See Step 8.)
Below is an edited version of theSunJCE.java
file, which contains a class namedSunJCE
that is the master class for the provider named "SunJCE".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 "SunJCE" provider. This information is used by the
getInstance
methods of the engine classes to look up the various algorithms and other services when they are requested.This code is supplied as an example of a provider master class.
/* * Copyright 1997-2001 by 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. */ package com.sun.crypto.provider; import java.security.AccessController; import java.security.Provider; . . . /** * The "SunJCE" Cryptographic Service Provider. * * @author Jan Luehe * @author Sharon Liu * * @version 1.42, 01/08/01 */ /** * Defines the "SunJCE" provider. * * Supported algorithms and their names: * * - DES (ECB, CBC, CFB, OFB, PCBC) * * - DES-EDE (ECB, CBC, CFB, OFB, PCBC) * * - Password-based Encryption (PBE) * * - Blowfish * * - Diffie-Hellman Key Agreement * * - HMAC-MD5, HMAC-SHA1 * * - PKCS5Padding */ public final class SunJCE extends Provider { private static String info = "SunJCE Provider " + "(implements DES, Triple DES, Blowfish, PBE, Diffie-Hellman, HMAC-MD5, " + "HMAC-SHA1)"; public SunJCE() { /* We are the "SunJCE" provider */ super("SunJCE", 1.2, info); AccessController.doPrivileged(new java.security.PrivilegedAction() { public Object run() { /* * Cipher engines */ put("Cipher.DES", "com.sun.crypto.provider.DESCipher"); put("Cipher.DESede", "com.sun.crypto.provider.DESedeCipher"); put("Alg.Alias.Cipher.TripleDES", "DESede"); put("Cipher.PBEWithMD5AndDES", "com.sun.crypto.provider.PBEWithMD5AndDESCipher"); put("Cipher.PBEWithMD5AndTripleDES", "com.sun.crypto.provider.PBEWithMD5AndTripleDESCipher"); put("Cipher.Blowfish", "com.sun.crypto.provider.BlowfishCipher"); /* * Key(pair) Generator engines */ put("KeyGenerator.DES", "com.sun.crypto.provider.DESKeyGenerator"); put("KeyGenerator.DESede", "com.sun.crypto.provider.DESedeKeyGenerator"); put("Alg.Alias.KeyGenerator.TripleDES", "DESede"); put("KeyGenerator.Blowfish", "com.sun.crypto.provider.BlowfishKeyGenerator"); put("KeyGenerator.HmacMD5", "com.sun.crypto.provider.HmacMD5KeyGenerator"); put("KeyGenerator.HmacSHA1", "com.sun.crypto.provider.HmacSHA1KeyGenerator"); put("KeyPairGenerator.DiffieHellman", "com.sun.crypto.provider.DHKeyPairGenerator"); put("Alg.Alias.KeyPairGenerator.DH", "DiffieHellman"); /* * Algorithm parameter generation engines */ put("AlgorithmParameterGenerator.DiffieHellman", "com.sun.crypto.provider.DHParameterGenerator"); put("Alg.Alias.AlgorithmParameterGenerator.DH", "DiffieHellman"); /* * Key Agreement engines */ put("KeyAgreement.DiffieHellman", "com.sun.crypto.provider.DHKeyAgreement"); put("Alg.Alias.KeyAgreement.DH", "DiffieHellman"); /* * Algorithm Parameter engines */ put("AlgorithmParameters.DiffieHellman", "com.sun.crypto.provider.DHParameters"); put("Alg.Alias.AlgorithmParameters.DH", "DiffieHellman"); put("AlgorithmParameters.DES", "com.sun.crypto.provider.DESParameters"); put("AlgorithmParameters.DESede", "com.sun.crypto.provider.DESedeParameters"); put("Alg.Alias.AlgorithmParameters.TripleDES", "DESede"); put("AlgorithmParameters.PBE", "com.sun.crypto.provider.PBEParameters"); put("Alg.Alias.AlgorithmParameters.PBEWithMD5AndDES", "PBE"); put("AlgorithmParameters.Blowfish", "com.sun.crypto.provider.BlowfishParameters"); /* * Key factories */ put("KeyFactory.DiffieHellman", "com.sun.crypto.provider.DHKeyFactory"); put("Alg.Alias.KeyFactory.DH", "DiffieHellman"); /* * Secret-key factories */ put("SecretKeyFactory.DES", "com.sun.crypto.provider.DESKeyFactory"); put("SecretKeyFactory.DESede", "com.sun.crypto.provider.DESedeKeyFactory"); put("Alg.Alias.SecretKeyFactory.TripleDES", "DESede"); put("SecretKeyFactory.PBEWithMD5AndDES", "com.sun.crypto.provider.PBEKeyFactory"); /* * MAC */ put("Mac.HmacMD5", "com.sun.crypto.provider.HmacMD5"); put("Mac.HmacSHA1", "com.sun.crypto.provider.HmacSHA1"); /* * KeyStore */ put("KeyStore.JCEKS", "com.sun.crypto.provider.JceKeyStore"); return null; } }); } }
Below is an example of anEMProvider.java
file, which contains a class namedEMProvider
that is the master class for the provider named "EMProvider".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 "EMProvider" provider. This information is used by the
getInstance
methods of the engine classes to look up the various algorithms and other services when they are requested.This code is supplied as an example of a master class for a provider that implements an exemption mechanism. It is included in addition to the Appendix A showing the master class for the "SunJCE" provider because the "SunJCE" provider does not include any exemption mechanism implementations. Note: A provider can implement both cryptographic services and exemption mechanism services.
package com.abc.crypto.provider; import java.security.AccessController; import java.security.Provider; /** * The "EMProvider" Cryptographic Service Provider. * * @version 1.00, 03/15/2000 */ /** * Defines the "EMProvider" provider. * * Supported algorithm(s) and their name(s): * * - Key Recovery */ public final class EMProvider extends Provider { private static String info = "EMProvider Exemption Mechanism Provider " + "(implements KeyRecovery)"; public EMProvider() { /* We are the "EMProvider" provider */ super("EMProvider", 1.2, info); AccessController.doPrivileged(new java.security.PrivilegedAction() { public Object run() { /* * Algorithm Parameter engines */ put("AlgorithmParameters.KeyRecovery", "com.abc.crypto.provider.KeyRecoveryParameters"); put("Alg.Alias.AlgorithmParameters.KR", "KeyRecovery"); /* * ExemptionMechanism */ put("ExemptionMechanism.KeyRecovery", "com.abc.crypto.provider.KeyRecovery"); put("Alg.Alias.ExemptionMechanism.KR", "KeyRecovery"); return null; } }); } }
Below is a copy of thejava.security
file that appears in every JRE installation. This file appears atHere <java-home> refers to the directory where the JRE was installed. Thus, if you have the J2SDK v 1.4 installed on Solaris in a directory named<java-home>/lib/security/java.security [Solaris] <java-home>\lib\security\java.security [Win32]/home/user1/j2sdk1.4.0
, or on Win32 in a directory namedC:\j2sdk1.4.0
, then the file would beSimilarly, if you have Java 2 Runtime Environment v 1.4 installed on Solaris in a directory named/home/user1/j2sdk1.4.0/jre/lib/security/java.security [Solaris] C:\j2sdk1.4.0\jre\lib\security\java.security [Win32]/home/user1/j2re1.4.0
, or on Win32 in a directory namedC:\j2re1.4.0
, then the file would beSee Step 5d for an example of adding information about your provider to this file./home/user1/j2re1.4.0/lib/security/java.security [Solaris] C:\j2re1.4.0\lib\security\java.security [Win32]# # 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 security.provider.2=com.sun.net.ssl.internal.ssl.Provider security.provider.3=com.sun.rsajca.Provider security.provider.4=com.sun.crypto.provider.SunJCE security.provider.5=sun.security.jgss.SunProvider # # Select the source of seed data for SecureRandom. By default it uses # a system/thread activity algorithm. Optionally, if the platform supports # it an entropy gathering device can be selected. # #securerandom.source=file:/dev/random # # The entropy gathering device is described as a URL and can # also be specified with the property "java.security.egd". For example, # -Djava.security.egd=file:/dev/urandom # Specifying this property will override the securerandom.source setting. # # Class to instantiate as the system Policy. This is the name of the class # that will be used as the Policy object. # policy.provider=sun.security.provider.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.security.policy=somefile. Comment out this line to disable # this feature. policy.allowSystemProperty=true # whether or not we look into the IdentityScope for trusted Identities # when encountering a 1.1 signed JAR file. If the identity is found # and is trusted, we grant it AllPermission. policy.ignoreIdentityScope=false # # Default keystore type. # keystore.type=jks # # Class to instantiate as the system scope: # system.scope=sun.security.provider.IdentityDatabase # # List of comma-separated packages that start with or equal this string # will cause a security exception to be thrown when # passed to checkPackageAccess unless the # corresponding RuntimePermission ("accessClassInPackage."+package) has # been granted. package.access=sun. # # List of comma-separated packages that start with or equal this string # will cause a security exception to be thrown when # passed to checkPackageDefinition unless the # corresponding RuntimePermission ("defineClassInPackage."+package) has # been granted. # # by default, no packages are restricted for definition, and none of # the class loaders supplied with the JDK call checkPackageDefinition. # #package.definition= # # Determines whether this properties file can be appended to # or overridden on the command line via -Djava.security.properties # security.overridePropertiesFile=true # # Determines the default key and trust manager factory algorithms for # the javax.net.ssl package. # ssl.KeyManagerFactory.algorithm=SunX509 ssl.TrustManagerFactory.algorithm=SunX509 # # Determines the default SSLSocketFactory and SSLServerSocketFactory # provider implementations for the javax.net.ssl package. If, due to # export and/or import regulations, the providers are not allowed to be # replaced, changing these values will produce non-functional # SocketFactory or ServerSocketFactory implementations. # #ssl.SocketFactory.provider= #ssl.ServerSocketFactory.provider=
Copyright © 1996-2001 Sun Microsystems, Inc. All Rights Reserved. Please send comments to: java-security@java.sun.com. |
![]() Java Software |