Constructors Summary |
---|
private KeyHolder()Creates a new instance of KeyHolder
|
public KeyHolder(String keyStoreFileName, String keyStorePassword, String keyAlias, String keyAliasPassword, String keyStoreType)Creates a new instance of KeyHolder using {@link java.security.KeyStore} related parameters.
try {
InitJCE.init();
} catch (InstantiationException e) {
NoSuchProviderException ex = new NoSuchProviderException("Error during cryptography provider initialization. Has bcprov-jdkxx-yyy.jar been copied in the lib directory or installed in the system?");
ex.initCause(e);
throw ex;
} catch (IllegalAccessException e) {
NoSuchProviderException ex = new NoSuchProviderException("Error during cryptography provider initialization. Has bcprov-jdkxx-yyy.jar been copied in the lib directory or installed in the system?");
ex.initCause(e);
throw ex;
} catch (ClassNotFoundException e) {
NoSuchProviderException ex = new NoSuchProviderException("Error during cryptography provider initialization. Has bcprov-jdkxx-yyy.jar been copied in the lib directory or installed in the system?");
ex.initCause(e);
throw ex;
}
if (keyStoreType == null) {
keyStoreType = KeyStore.getDefaultType();
}
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(new BufferedInputStream(new FileInputStream(keyStoreFileName)), keyStorePassword.toCharArray());
Enumeration aliases = keyStore.aliases();
if (keyAlias == null) {
if(aliases.hasMoreElements()) {
keyAlias = (String) aliases.nextElement();
} else {
throw new KeyStoreException("No alias was found in keystore.");
}
if (aliases.hasMoreElements()) {
throw new KeyStoreException("No <keyAlias> was given and more than one alias was found in keystore.");
}
}
if (keyAliasPassword == null) {
keyAliasPassword = keyStorePassword;
}
this.privateKey = (PrivateKey) keyStore.getKey(keyAlias, keyAliasPassword.toCharArray());
if (this.privateKey == null) {
throw new KeyStoreException("The \"" + keyAlias + "\" PrivateKey alias was not found in keystore.");
}
this.certificate = (X509Certificate) keyStore.getCertificate(keyAlias);
if (this.certificate == null) {
throw new KeyStoreException("The \"" + keyAlias + "\" X509Certificate alias was not found in keystore.");
}
java.security.cert.Certificate[] certificateChain = keyStore.getCertificateChain(keyAlias);
ArrayList certList = new ArrayList();
if (certificateChain == null) {
certList.add(this.certificate);
} else {
for (int i = 0; i < certificateChain.length; i++) {
certList.add(certificateChain[i]);
}
}
// create a CertStore containing the certificates we want carried
// in the signature
this.certStore = CertStore.getInstance("Collection",
new CollectionCertStoreParameters(certList), "BC");
|
Methods Summary |
---|
public org.bouncycastle.mail.smime.SMIMESignedGenerator | createGenerator()Creates an SMIMESignedGenerator . Includes a signer private key and certificate,
and a pool of certs and cerls (if any) to go with the signature.
// create the generator for creating an smime/signed message
SMIMESignedGenerator generator = new SMIMESignedGenerator();
// add a signer to the generator - this specifies we are using SHA1
// the encryption algorithm used is taken from the key
generator.addSigner(this.privateKey, this.certificate, SMIMESignedGenerator.DIGEST_SHA1);
// add our pool of certs and cerls (if any) to go with the signature
generator.addCertificatesAndCRLs(this.certStore);
return generator;
|
private static java.lang.String | extractAttribute(java.lang.String DistinguishedName, java.lang.String attributeName)
int i = DistinguishedName.indexOf(attributeName);
if (i < 0) {
return null;
}
i += attributeName.length();
int j = DistinguishedName.indexOf(",", i);
if (j - 1 <= 0) {
return null;
}
return DistinguishedName.substring(i, j).trim();
|
public javax.mail.internet.MimeMultipart | generate(javax.mail.internet.MimeMessage message)Generates a signed MimeMultipart from a MimeMessage.
// create the generator for creating an smime/signed MimeMultipart
SMIMESignedGenerator generator = createGenerator();
// do it
return generator.generate(message, "BC");
|
public javax.mail.internet.MimeMultipart | generate(javax.mail.internet.MimeBodyPart content)Generates a signed MimeMultipart from a MimeBodyPart.
// create the generator for creating an smime/signed MimeMultipart
SMIMESignedGenerator generator = createGenerator();
// do it
return generator.generate(content, "BC");
|
public java.security.cert.CertStore | getCertStore()Getter for property certStore.
return this.certStore;
|
public java.security.cert.X509Certificate | getCertificate()Getter for property certificate.
return this.certificate;
|
public static java.lang.String | getDefaultType()Returns the default keystore type as specified in the Java security properties file,
or the string "jks" (acronym for "Java keystore") if no such property exists.
return KeyStore.getDefaultType();
|
public java.security.PrivateKey | getPrivateKey()Getter for property privateKey.
return this.privateKey;
|
public static java.lang.String | getSignerAddress(java.security.cert.X509Certificate certificate)Extracts the signer email address (EMAILADDRESS=) from an X509Certificate distinguished name.
return extractAttribute(certificate.getSubjectDN().toString(), "EMAILADDRESS=");
|
public java.lang.String | getSignerAddress()Getter for property signerAddress.
return getSignerAddress(getCertificate());
|
public static java.lang.String | getSignerCN(java.security.cert.X509Certificate certificate)Extracts the signer common name (CN=) from an X509Certificate distinguished name.
return extractAttribute(certificate.getSubjectDN().toString(), "CN=");
|
public java.lang.String | getSignerCN()Getter for property signerCN.
return getSignerCN(getCertificate());
|
public static java.lang.String | getSignerDistinguishedName(java.security.cert.X509Certificate certificate)Extracts the signer distinguished name (DN) from an X509Certificate .
return certificate.getSubjectDN().toString();
|
public java.lang.String | getSignerDistinguishedName()Getter for property signerDistinguishedName.
return getSignerDistinguishedName(getCertificate());
|