Package org.globus.gsi
Class OpenSSLKey
java.lang.Object
org.globus.gsi.OpenSSLKey
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
BouncyCastleOpenSSLKey
Represents a OpenSSL-style PEM-formatted private key. It supports encryption and decryption of the key. Currently,
only RSA keys are supported, and only TripleDES encryption is supported.
This is based on work done by Ming Yung at DSTC.
- Since:
- 1.0
- Version:
- ${version}
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
private String
private byte[]
private static final String
private IvParameterSpec
private PrivateKey
private boolean
private byte[]
private int
private String
private byte[]
private int
-
Constructor Summary
ConstructorsConstructorDescriptionReads a OpenSSL private key from the specified input stream.OpenSSLKey
(String file) Reads a OpenSSL private key from the specified file.OpenSSLKey
(String algorithm, byte[] data) Initializes the OpenSSL key from raw byte array.OpenSSLKey
(PrivateKey key) Converts a RSAPrivateCrtKey into OpenSSL key. -
Method Summary
Modifier and TypeMethodDescriptionvoid
decrypt
(byte[] password) Decrypts the private key with given password.void
Decrypts the private key with given password.void
encrypt
(byte[] password) Encrypts the private key with given password.void
Encrypts the private key with given password.boolean
private String
private String
private void
private Cipher
protected byte[]
protected abstract byte[]
getEncoded
(PrivateKey key) protected abstract PrivateKey
private String
getKeyAlgorithm
(String line) Returns the JCE (RSAPrivateCrtKey) key.protected String
private SecretKeySpec
getSecretKey
(byte[] pwd, byte[] keyInitializationVector) int
hashCode()
boolean
Check if the key was encrypted or not.private static boolean
objectsEquals
(Object a, Object b) private void
parseEncryptionInfo
(String line) private void
private void
private void
private void
void
Sets algorithm for encryption.private void
setIV
(byte[] data) private void
private String
toPEM()
void
writeTo
(OutputStream output) Writes the private key to the specified output stream in PEM format.void
Writes the private key to the specified writer in PEM format.void
Writes the private key to the specified file in PEM format.
-
Field Details
-
HEADER
- See Also:
-
keyAlg
-
isEncrypted
private boolean isEncrypted -
encodedKey
private byte[] encodedKey -
intKey
-
ivData
private byte[] ivData -
initializationVector
-
encAlgStr
-
encAlg
-
keyLength
private int keyLength -
ivLength
private int ivLength -
keyData
private byte[] keyData
-
-
Constructor Details
-
OpenSSLKey
Reads a OpenSSL private key from the specified input stream. The private key must be PEM encoded and can be encrypted.- Parameters:
is
- input stream with OpenSSL key in PEM format.- Throws:
IOException
- if I/O problems.GeneralSecurityException
- if problems with the key
-
OpenSSLKey
Reads a OpenSSL private key from the specified file. The private key must be PEM encoded and can be encrypted.- Parameters:
file
- file containing the OpenSSL key in PEM format.- Throws:
IOException
- if I/O problems.GeneralSecurityException
- if problems with the key
-
OpenSSLKey
Converts a RSAPrivateCrtKey into OpenSSL key.- Parameters:
key
- private key - must be a RSAPrivateCrtKey
-
OpenSSLKey
Initializes the OpenSSL key from raw byte array.- Parameters:
algorithm
- the algorithm of the key. Currently only RSA algorithm is supported.data
- the DER encoded key data. If RSA algorithm, the key must be in PKCS#1 format.- Throws:
GeneralSecurityException
- if any security problems.
-
-
Method Details
-
getEncoded
protected byte[] getEncoded() -
readPEM
- Throws:
IOException
GeneralSecurityException
-
extractKey
- Throws:
IOException
-
extractEncryptionInfo
private String extractEncryptionInfo(BufferedReader in) throws IOException, GeneralSecurityException - Throws:
IOException
GeneralSecurityException
-
parseKeyAlgorithm
- Throws:
IOException
InvalidKeyException
-
isEncrypted
public boolean isEncrypted()Check if the key was encrypted or not.- Returns:
- true if the key is encrypted, false otherwise.
-
decrypt
Decrypts the private key with given password. Does nothing if the key is not encrypted.- Parameters:
password
- password to decrypt the key with.- Throws:
GeneralSecurityException
- whenever an error occurs during decryption.
-
decrypt
Decrypts the private key with given password. Does nothing if the key is not encrypted.- Parameters:
password
- password to decrypt the key with.- Throws:
GeneralSecurityException
- whenever an error occurs during decryption.
-
encrypt
Encrypts the private key with given password. Does nothing if the key is encrypted already.- Parameters:
password
- password to encrypt the key with.- Throws:
GeneralSecurityException
- whenever an error occurs during encryption.
-
encrypt
Encrypts the private key with given password. Does nothing if the key is encrypted already.- Parameters:
password
- password to encrypt the key with.- Throws:
GeneralSecurityException
- whenever an error occurs during encryption.
-
setEncryptionAlgorithm
Sets algorithm for encryption.- Parameters:
alg
- algorithm for encryption- Throws:
GeneralSecurityException
- if algorithm is not supported
-
getPrivateKey
Returns the JCE (RSAPrivateCrtKey) key.- Returns:
- the private key, null if the key was not decrypted yet.
-
writeTo
Writes the private key to the specified output stream in PEM format. If the key was encrypted it will be encoded as an encrypted RSA key. If not, it will be encoded as a regular RSA key.- Parameters:
output
- output stream to write the key to.- Throws:
IOException
- if I/O problems writing the key
-
writeTo
Writes the private key to the specified writer in PEM format. If the key was encrypted it will be encoded as an encrypted RSA key. If not, it will be encoded as a regular RSA key.- Parameters:
w
- writer to output the key to.- Throws:
IOException
- if I/O problems writing the key
-
writeTo
Writes the private key to the specified file in PEM format. If the key was encrypted it will be encoded as an encrypted RSA key. If not, it will be encoded as a regular RSA key.- Parameters:
file
- file to write the key to.- Throws:
IOException
- if I/O problems writing the key
-
getEncoded
-
getKey
- Throws:
GeneralSecurityException
-
getProvider
-
getCipher
- Throws:
GeneralSecurityException
-
getKeyAlgorithm
-
parseEncryptionInfo
- Throws:
GeneralSecurityException
-
setAlgorithmSettings
- Throws:
GeneralSecurityException
-
setIV
- Throws:
GeneralSecurityException
-
generateIV
private void generateIV() -
setIV
private void setIV(byte[] data) -
getSecretKey
private SecretKeySpec getSecretKey(byte[] pwd, byte[] keyInitializationVector) throws GeneralSecurityException - Throws:
GeneralSecurityException
-
toPEM
-
readObject
- Throws:
IOException
ClassNotFoundException
-
equals
-
objectsEquals
-
hashCode
public int hashCode()
-