Skip to content

Commit 3b26163

Browse files
authored
Merge pull request #16 from DirectProjectJavaRI/develop
Releasing 8.1.2
2 parents 5f07ca8 + 3541909 commit 3b26163

File tree

6 files changed

+47
-36
lines changed

6 files changed

+47
-36
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<artifactId>direct-common</artifactId>
66
<name>Direct Project common APIs</name>
7-
<version>8.1.0</version>
7+
<version>8.1.2</version>
88
<description>Direct Project common APIs. Includes instrumentation, auditing, and other utility APIs</description>
99
<inceptionYear>2010</inceptionYear>
1010
<url>http://api.nhindirect.org/x/www/api.nhindirect.org/java/site/direct-common/${project.version}</url>

src/main/java/org/nhindirect/common/crypto/impl/StaticCachedPKCS11TokenKeyStoreProtectionManager.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,15 @@ public void initTokenStore() throws CryptoException
6969
// some HSMs only store references to the keys in these objects and
7070
// and still have to go back to the HSM to pull the actual key data
7171
// create a key object from the encoded data
72-
keystoreProtectionKey = new SecretKeySpec(keystoreProtectionKey.getEncoded(), "");
73-
privateKeyProtectionKey = new SecretKeySpec(privateKeyProtectionKey.getEncoded(), "");
72+
73+
System.out.print("keystoreProtectionKey is null: " + (keystoreProtectionKey.getEncoded() != null));
74+
System.out.print("privateKeyProtectionKey is null: " + (privateKeyProtectionKey.getEncoded() != null));
75+
76+
if (keystoreProtectionKey.getEncoded() != null)
77+
keystoreProtectionKey = new SecretKeySpec(keystoreProtectionKey.getEncoded(), "");
78+
79+
if (privateKeyProtectionKey.getEncoded() != null)
80+
privateKeyProtectionKey = new SecretKeySpec(privateKeyProtectionKey.getEncoded(), "");
7481

7582

7683

src/main/java/org/nhindirect/common/crypto/tools/PKCS11SecretKeyManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import java.io.IOException;
77
import java.io.InputStream;
88
import java.io.InputStreamReader;
9+
import java.security.Security;
910
import java.util.Properties;
1011

1112
import org.apache.commons.io.FileUtils;
1213
import org.apache.commons.lang3.StringUtils;
14+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1315
import org.nhindirect.common.crypto.MutableKeyStoreProtectionManager;
1416
import org.nhindirect.common.crypto.exceptions.CryptoException;
1517
import org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential;
@@ -31,10 +33,17 @@ public class PKCS11SecretKeyManager
3133
protected static String pkcs11ProviderCfg = null;
3234
protected static String keyStoreConfigFile = null;
3335

36+
static {
37+
Security.addProvider(new BouncyCastleProvider());
38+
}
39+
3440
public static void main(String[] argv)
3541
{
3642
String[] passArgs = null;
3743

44+
// make sure bouncy castle is initialized
45+
46+
3847

3948
// need to check if there is a configuration for the PKCS11
4049
// provider... if not, assume the JVM has already been configured for one

src/main/java/org/nhindirect/common/crypto/tools/commands/PKCS11Commands.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@
3838
import org.apache.commons.codec.binary.Base64;
3939
import org.apache.commons.io.FileUtils;
4040
import org.apache.commons.lang3.StringUtils;
41-
import org.bouncycastle.asn1.ASN1EncodableVector;
42-
import org.bouncycastle.asn1.DERSet;
4341
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
44-
import org.bouncycastle.asn1.x509.Attribute;
4542
import org.bouncycastle.asn1.x509.BasicConstraints;
4643
import org.bouncycastle.asn1.x509.ExtendedKeyUsage;
44+
import org.bouncycastle.asn1.x509.Extension;
45+
import org.bouncycastle.asn1.x509.ExtensionsGenerator;
4746
import org.bouncycastle.asn1.x509.GeneralName;
4847
import org.bouncycastle.asn1.x509.GeneralNames;
4948
import org.bouncycastle.asn1.x509.KeyPurposeId;
5049
import org.bouncycastle.asn1.x509.KeyUsage;
51-
import org.bouncycastle.asn1.x509.X509Extensions;
52-
import org.bouncycastle.asn1.x509.X509ExtensionsGenerator;
5350
import org.bouncycastle.crypto.prng.VMPCRandomGenerator;
54-
import org.bouncycastle.jce.PKCS10CertificationRequest;
5551
import org.bouncycastle.jce.X509Principal;
52+
import org.bouncycastle.operator.ContentSigner;
53+
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
54+
import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
55+
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
5656
import org.bouncycastle.x509.X509V3CertificateGenerator;
5757
import org.nhindirect.common.crypto.MutableKeyStoreProtectionManager;
5858
import org.nhindirect.common.crypto.WrappableKeyProtectionManager;
@@ -610,7 +610,7 @@ public void createCSR(String[] args)
610610
// create the CSR
611611

612612
// create the extensions that we want
613-
final X509ExtensionsGenerator extsGen = new X509ExtensionsGenerator();
613+
final ExtensionsGenerator extsGen = new ExtensionsGenerator();
614614

615615
// Key Usage
616616
int usage;
@@ -621,32 +621,23 @@ else if (keyUsage.compareToIgnoreCase("DigitalSignature") == 0)
621621
else
622622
usage = KeyUsage.keyEncipherment | KeyUsage.digitalSignature;
623623

624-
extsGen.addExtension(X509Extensions.KeyUsage, true, new KeyUsage(usage));
624+
extsGen.addExtension(Extension.keyUsage, true, new KeyUsage(usage));
625625

626626
// Subject Alt Name
627627
int nameType = subjectAltName.contains("@") ? GeneralName.rfc822Name : GeneralName.dNSName;
628628
final GeneralNames altName = new GeneralNames(new GeneralName(nameType, subjectAltName));
629-
extsGen.addExtension(X509Extensions.SubjectAlternativeName, false, altName);
629+
extsGen.addExtension(Extension.subjectAlternativeName, false, altName);
630630

631631
// Extended Key Usage
632-
final Vector<KeyPurposeId> purposes = new Vector<KeyPurposeId>();
633-
purposes.add(KeyPurposeId.id_kp_emailProtection);
634-
extsGen.addExtension(X509Extensions.ExtendedKeyUsage, false, new ExtendedKeyUsage(purposes));
632+
ExtendedKeyUsage eku = new ExtendedKeyUsage(KeyPurposeId.id_kp_emailProtection);
633+
634+
635+
extsGen.addExtension(Extension.extendedKeyUsage, false, eku);
635636

636637
// Basic constraint
637638
final BasicConstraints bc = new BasicConstraints(false);
638-
extsGen.addExtension(X509Extensions.BasicConstraints, true, bc);
639+
extsGen.addExtension(Extension.basicConstraints, true, bc);
639640

640-
// create the extension requests
641-
final X509Extensions exts = extsGen.generate();
642-
643-
final ASN1EncodableVector attributes = new ASN1EncodableVector();
644-
final Attribute attribute = new Attribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest,
645-
new DERSet(exts.toASN1Primitive()));
646-
647-
attributes.add(attribute);
648-
649-
final DERSet requestedAttributes = new DERSet(attributes);
650641

651642
// create the DN
652643
final StringBuilder dnBuilder = new StringBuilder("CN=").append(commonName);
@@ -656,16 +647,20 @@ else if (keyUsage.compareToIgnoreCase("DigitalSignature") == 0)
656647

657648
final X500Principal subjectPrin = new X500Principal(dnBuilder.toString());
658649

659-
final X509Principal xName = new X509Principal(true, subjectPrin.getName());
650+
//final X509Principal xName = new X509Principal(true, subjectPrin.getName());
651+
PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(subjectPrin, storedCert.getPublicKey());
652+
builder.setAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, extsGen.generate());
653+
654+
JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder("SHA256withRSA");
655+
ContentSigner signer = csBuilder.build(privKey);
660656

661657
// create the CSR
662-
final PKCS10CertificationRequest request = new PKCS10CertificationRequest("SHA256WITHRSA", xName, storedCert.getPublicKey(),
663-
requestedAttributes, privKey, ks.getProvider().getName());
658+
664659

665-
final byte[] encodedCSR = request.getEncoded();
660+
final byte[] encodedCSR = builder.build(signer).getEncoded();
666661

667662
final String csrString = "-----BEGIN CERTIFICATE REQUEST-----\r\n" + Base64.encodeBase64String(encodedCSR)
668-
+ "-----END CERTIFICATE REQUEST-----";
663+
+ "\r\n-----END CERTIFICATE REQUEST-----";
669664

670665
final File csrFile = new File(alias + "-CSR.pem");
671666
FileUtils.writeStringToFile(csrFile, csrString);

src/test/java/org/nhindirect/common/crypto/impl/PKCS11OperationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void testSignDataOnToken() throws Exception
6565
{
6666
final KeyStore ks = KeyStore.getInstance("PKCS11");
6767

68-
ks.load(null, "1Kingpuff".toCharArray());
68+
ks.load(null, "1Kingpuff!".toCharArray());
6969

7070
final Enumeration<String> aliases = ks.aliases();
7171

@@ -117,7 +117,7 @@ public void testImportEncryptedPrivateKeyWithWrapping() throws Exception
117117

118118
if (!StringUtils.isEmpty(pkcs11ProvName))
119119
{
120-
final PKCS11Credential cred = new BootstrappedPKCS11Credential("1Kingpuff");
120+
final PKCS11Credential cred = new BootstrappedPKCS11Credential("1Kingpuff!");
121121
final StaticPKCS11TokenKeyStoreProtectionManager mgr =
122122
new StaticPKCS11TokenKeyStoreProtectionManager(cred, "KeyStoreProtKey", "PrivKeyProtKey");
123123

@@ -136,10 +136,10 @@ public void testImportEncryptedPrivateKeyWithWrapping() throws Exception
136136
* wrap it on the HSM.
137137
*/
138138
final KeyStore store = KeyStore.getInstance("pkcs12");
139-
store.load(FileUtils.openInputStream(new File("./src/test/resources/certs/gm2552encrypted.p12")), "1kingpuff".toCharArray());
139+
store.load(FileUtils.openInputStream(new File("./src/test/resources/certs/gm2552encrypted.p12")), "1Kingpuff!".toCharArray());
140140
// there should only be on entry
141141
final String alias = store.aliases().nextElement();
142-
final PrivateKey entry = (PrivateKey)store.getKey(alias, "1kingpuff".toCharArray());
142+
final PrivateKey entry = (PrivateKey)store.getKey(alias, "1Kingpuff!".toCharArray());
143143

144144
/*
145145
* 3. "Wrap" the private using secret key and AES128 encryption and write it to a file. The encryption is done

src/test/java/org/nhindirect/common/util/TestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void handle(Callback[] callbacks)
2525
if (callback instanceof PasswordCallback)
2626
{
2727

28-
((PasswordCallback)callback).setPassword("1Kingpuff".toCharArray());
28+
((PasswordCallback)callback).setPassword("1Kingpuff!".toCharArray());
2929

3030
}
3131
}

0 commit comments

Comments
 (0)