org.opcfoundation.ua.transport.security
Class PrivKey

java.lang.Object
  extended by org.opcfoundation.ua.transport.security.PrivKey

public class PrivKey
extends Object

Valid and encodeable private key. Wrapper to PrivateKey


Field Summary
 RSAPrivateKey privateKey
           
 
Constructor Summary
PrivKey(byte[] encodedPrivateKey)
          Constructor for PrivKey.
PrivKey(RSAPrivateKey privateKey)
          Constructor for PrivKey.
 
Method Summary
 byte[] getEncodedPrivateKey()
          getEncodedPrivateKey.
 RSAPrivateKey getPrivateKey()
          Getter for the field privateKey.
static PrivKey load(byte[] keyBytes, String password)
          Load private key from key data
static PrivKey load(File file)
          Deprecated. Use load(File, String) or loadFromKeyStore(File, String) instead
static PrivKey load(File file, String password)
          Load private key from a PEM encoded file
static PrivKey load(InputStream is, String password)
          load.
static PrivKey loadFromKeyStore(File file, String password)
          Load private key from a key store (PKCS12) file
static PrivKey loadFromKeyStore(URL keystoreUrl, String password)
          Load private key from a PKCS12 key store
 void save(File file)
          Save the key in a binary file.
 void save(File file, String privateKeyPassword)
          Save the private key to a PEM file
 void saveToKeyStore(Cert cert, File file, String privateKeyPassword, String keyStorePassword, String keyStoreType)
          Save the identity to a password protected keystore.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

privateKey

public final RSAPrivateKey privateKey
Constructor Detail

PrivKey

public PrivKey(byte[] encodedPrivateKey)
        throws IOException,
               InvalidKeySpecException,
               NoSuchAlgorithmException

Constructor for PrivKey.

Parameters:
encodedPrivateKey - an array of byte.
Throws:
IOException - if any.
InvalidKeySpecException - if any.
NoSuchAlgorithmException - if any.

PrivKey

public PrivKey(RSAPrivateKey privateKey)

Constructor for PrivKey.

Parameters:
privateKey - a RSAPrivateKey object.
Method Detail

loadFromKeyStore

public static PrivKey loadFromKeyStore(URL keystoreUrl,
                                       String password)
                                throws IOException,
                                       UnrecoverableKeyException,
                                       NoSuchAlgorithmException,
                                       CertificateException,
                                       KeyStoreException
Load private key from a PKCS12 key store

Parameters:
keystoreUrl - url to key store
password - password to key store
Returns:
private key
Throws:
IOException - if any.
KeyStoreException - if any.
CertificateException - if any.
NoSuchAlgorithmException - if any.
UnrecoverableKeyException - if any.

load

public static PrivKey load(File file,
                           String password)
                    throws IOException,
                           NoSuchAlgorithmException,
                           InvalidKeySpecException,
                           NoSuchPaddingException,
                           InvalidKeyException,
                           InvalidAlgorithmParameterException,
                           IllegalBlockSizeException,
                           BadPaddingException,
                           InvalidParameterSpecException
Load private key from a PEM encoded file

Parameters:
file - a File object.
password - a String object.
Returns:
a PrivKey object.
Throws:
IOException - if any.
NoSuchAlgorithmException - if any.
InvalidKeySpecException - if any.
NoSuchPaddingException - if any.
InvalidAlgorithmParameterException - if any.
InvalidKeyException - if any.
BadPaddingException - if any.
IllegalBlockSizeException - if any.
InvalidParameterSpecException - if any.

load

public static PrivKey load(InputStream is,
                           String password)
                    throws IOException,
                           NoSuchAlgorithmException,
                           InvalidKeySpecException,
                           NoSuchPaddingException,
                           InvalidKeyException,
                           InvalidAlgorithmParameterException,
                           IllegalBlockSizeException,
                           BadPaddingException,
                           InvalidParameterSpecException

load.

Parameters:
is - a InputStream object.
password - a String object.
Returns:
a PrivKey object.
Throws:
IOException - if any.
NoSuchAlgorithmException - if any.
InvalidKeySpecException - if any.
NoSuchPaddingException - if any.
InvalidKeyException - if any.
InvalidAlgorithmParameterException - if any.
IllegalBlockSizeException - if any.
BadPaddingException - if any.
InvalidParameterSpecException - if any.

load

public static PrivKey load(byte[] keyBytes,
                           String password)
                    throws IOException,
                           NoSuchAlgorithmException,
                           InvalidKeySpecException,
                           NoSuchPaddingException,
                           InvalidKeyException,
                           InvalidAlgorithmParameterException,
                           IllegalBlockSizeException,
                           BadPaddingException,
                           InvalidParameterSpecException
Load private key from key data

Parameters:
password - a String object.
keyBytes - an array of byte.
Returns:
a PrivKey object.
Throws:
IOException - if any.
NoSuchAlgorithmException - if any.
InvalidKeySpecException - if any.
NoSuchPaddingException - if any.
InvalidAlgorithmParameterException - if any.
InvalidKeyException - if any.
BadPaddingException - if any.
IllegalBlockSizeException - if any.
InvalidParameterSpecException - if any.

load

@Deprecated
public static PrivKey load(File file)
                    throws IOException,
                           InvalidKeySpecException,
                           NoSuchAlgorithmException
Deprecated. Use load(File, String) or loadFromKeyStore(File, String) instead

Load private key from a file.

Parameters:
file - a File object.
Returns:
a PrivKey object.
Throws:
IOException - if any.
InvalidKeySpecException - if any.
NoSuchAlgorithmException - if any.

loadFromKeyStore

public static PrivKey loadFromKeyStore(File file,
                                       String password)
                                throws IOException,
                                       UnrecoverableKeyException,
                                       NoSuchAlgorithmException,
                                       CertificateException,
                                       KeyStoreException
Load private key from a key store (PKCS12) file

Parameters:
file - key store file
password - password to key store
Returns:
private key
Throws:
IOException - if any.
KeyStoreException - if any.
CertificateException - if any.
NoSuchAlgorithmException - if any.
UnrecoverableKeyException - if any.

save

public void save(File file)
          throws IOException
Save the key in a binary file. Note that the file is not secured by a password.

Parameters:
file - a File object.
Throws:
IOException - if any.

save

public void save(File file,
                 String privateKeyPassword)
          throws IOException
Save the private key to a PEM file

Parameters:
file - the file
privateKeyPassword - the password used to store the key
Throws:
IOException - if any.

getEncodedPrivateKey

public byte[] getEncodedPrivateKey()

getEncodedPrivateKey.

Returns:
an array of byte.

getPrivateKey

public RSAPrivateKey getPrivateKey()

Getter for the field privateKey.

Returns:
a RSAPrivateKey object.

saveToKeyStore

public void saveToKeyStore(Cert cert,
                           File file,
                           String privateKeyPassword,
                           String keyStorePassword,
                           String keyStoreType)
                    throws IOException,
                           KeyStoreException,
                           NoSuchProviderException,
                           NoSuchAlgorithmException,
                           CertificateException
Save the identity to a password protected keystore.

Parameters:
cert - the certificate used to chain the key
file - the file used to store the key
privateKeyPassword - the password to secure the private key, must not be null for JKS
keyStorePassword - the password to the key store, must not be null
keyStoreType - key store type, either "PKCS12" or "JKS"
Throws:
IOException - if the file cannot be written to
NoSuchProviderException - Bouncy Castle Provider not found
KeyStoreException - keystore failed
CertificateException - certificate problem
NoSuchAlgorithmException - cryptographic algorithm not found


Copyright © 2018. All rights reserved.