List of usage examples for java.security KeyPair KeyPair
public KeyPair(PublicKey publicKey, PrivateKey privateKey)
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.TestConsoleRSAKeyPairGenerator.java
@Test public void testDoInteractive02() throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); String privateFileName = "testDoInteractive01Private.key"; String publicFileName = "testDoInteractive01Public.key"; this.console.cli = EasyMock.createMockBuilder(CommandLine.class).withConstructor() .addMockedMethod("hasOption", String.class).addMockedMethod("getOptionValue", String.class) .createMock();/*from www . j av a 2 s. c o m*/ PrivateKey privateKey = EasyMock.createMock(PrivateKey.class); PublicKey publicKey = EasyMock.createMock(PublicKey.class); KeyPair keyPair = new KeyPair(publicKey, privateKey); this.device.printOutLn("Would you like to..."); EasyMock.expectLastCall(); this.device.printOutLn(" (1) Save the public and private keys to .key files?"); EasyMock.expectLastCall(); this.device.printOutLn(" (2) Generate compilable Java code with embedded keys?"); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Your selection (default 1)? ")).andReturn("1"); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Would you like to..."); EasyMock.expectLastCall(); this.device.printOutLn(" (1) Use the same password to encrypt both keys?"); EasyMock.expectLastCall(); this.device.printOutLn(" (2) Use a different password for each key?"); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Your selection (default 1)? ")).andReturn("2"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.promptForValidPassword(6, 32, "the public key")) .andReturn("publicPassword02".toCharArray()); this.device.printOutLn("Passwords match."); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.promptForValidPassword(6, 32, "the private key")) .andReturn("privatePassword02".toCharArray()); this.device.printOutLn("Passwords match."); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Please enter the name of a file to store the public key in: ")) .andReturn(publicFileName); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Please enter the name of a file to store the private key in: ")) .andReturn(privateFileName); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOut("Generating RSA key pair, 2048-bit long modulus"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); EasyMock.expect(this.generator.generateKeyPair()).andReturn(keyPair); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOut("Key pair generated. Encrypting keys with 128-bit AES security"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); final Capture<char[]> capture1 = new Capture<char[]>(); final Capture<char[]> capture2 = new Capture<char[]>(); this.generator.saveKeyPairToFiles(EasyMock.eq(keyPair), EasyMock.eq(privateFileName), EasyMock.eq(publicFileName), EasyMock.capture(capture1), EasyMock.capture(capture2)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The private key password should not be null.", capture1.getValue()); assertArrayEquals("The private key password is not correct.", "privatePassword02".toCharArray(), capture1.getValue()); assertNotNull("The public key password should not be null.", capture2.getValue()); assertArrayEquals("The public key password is not correct.", "publicPassword02".toCharArray(), capture2.getValue()); return null; } }); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Private key written to " + privateFileName); EasyMock.expectLastCall(); this.device.printOutLn("Public key written to " + publicFileName); EasyMock.replay(this.generator, this.device, this.console.cli, privateKey, publicKey); try { this.console.doInteractive(); assertNotNull("The private key password should still not be null.", capture1.getValue()); assertArrayEquals("The private key password should have been erased.", new char[capture1.getValue().length], capture1.getValue()); assertNotNull("The public key password should still not be null.", capture2.getValue()); assertArrayEquals("The public key password should have been erased.", new char[capture2.getValue().length], capture2.getValue()); } finally { EasyMock.verify(this.console.cli, privateKey, publicKey); } }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.TestConsoleRSAKeyPairGenerator.java
@Test public void testDoInteractive03() throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); this.console.cli = EasyMock.createMockBuilder(CommandLine.class).withConstructor() .addMockedMethod("hasOption", String.class).addMockedMethod("getOptionValue", String.class) .createMock();//from www .j a v a2 s .com PrivateKey privateKey = EasyMock.createMock(PrivateKey.class); PublicKey publicKey = EasyMock.createMock(PublicKey.class); KeyPair keyPair = new KeyPair(publicKey, privateKey); this.device.printOutLn("Would you like to..."); EasyMock.expectLastCall(); this.device.printOutLn(" (1) Save the public and private keys to .key files?"); EasyMock.expectLastCall(); this.device.printOutLn(" (2) Generate compilable Java code with embedded keys?"); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Your selection (default 1)? ")).andReturn("2"); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Would you like to..."); EasyMock.expectLastCall(); this.device.printOutLn(" (1) Use the same password to encrypt both keys?"); EasyMock.expectLastCall(); this.device.printOutLn(" (2) Use a different password for each key?"); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Your selection (default 1)? ")).andReturn("1"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.promptForValidPassword(6, 32, "both keys")) .andReturn("keyPassword03".toCharArray()); this.device.printOutLn("Passwords match."); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Please enter the name of a Java class to embed the public key in: ")) .andReturn("PublicKey01"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Enter an optional package name for the public key class: ")) .andReturn("org.example.licensing.public"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Please enter the name of a Java class to embed the private key in: ")) .andReturn("PrivateKey01"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Enter an optional package name for the private key class: ")) .andReturn("org.example.licensing.private"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device .readLine("If you wish to embed the key password in a Java class, " + "enter the class name now: ")) .andReturn("KeyPassword01"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("You can optionally enter a package name for the key storage class: ")) .andReturn("org.example.licensing.public"); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOut("Generating RSA key pair, 2048-bit long modulus"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); EasyMock.expect(this.generator.generateKeyPair()).andReturn(keyPair); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOut("Key pair generated. Encrypting keys with 128-bit AES security"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); final Capture<GeneratedClassDescriptor> descriptorA01 = new Capture<GeneratedClassDescriptor>(); final Capture<GeneratedClassDescriptor> descriptorA02 = new Capture<GeneratedClassDescriptor>(); final Capture<GeneratedClassDescriptor> descriptorB01 = new Capture<GeneratedClassDescriptor>(); final Capture<char[]> passwordA01 = new Capture<char[]>(); final Capture<char[]> passwordB01 = new Capture<char[]>(); this.generator.saveKeyPairToProviders(EasyMock.eq(keyPair), EasyMock.capture(descriptorA01), EasyMock.capture(descriptorA02), EasyMock.capture(passwordA01)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The public key password should not be null.", passwordA01.getValue()); assertArrayEquals("The public key password is not correct.", "keyPassword03".toCharArray(), passwordA01.getValue()); descriptorA01.getValue().setJavaFileContents("privateKeyContents01"); descriptorA02.getValue().setJavaFileContents("publicKeyContents01"); return null; } }); this.generator.savePasswordToProvider(EasyMock.capture(passwordB01), EasyMock.capture(descriptorB01)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The public key password should still not be null.", passwordB01.getValue()); assertArrayEquals("The public key password is now not correct.", "keyPassword03".toCharArray(), passwordB01.getValue()); descriptorB01.getValue().setJavaFileContents("publicPasswordContents01"); return null; } }); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Private key provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("privateKeyContents01"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Public key provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("publicKeyContents01"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Key password provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("publicPasswordContents01"); EasyMock.expectLastCall(); EasyMock.replay(this.generator, this.device, this.console.cli, privateKey, publicKey); try { this.console.doInteractive(); assertNotNull("The public key password should again still not be null.", passwordA01.getValue()); assertArrayEquals("The public key password should have been erased.", new char[passwordA01.getValue().length], passwordA01.getValue()); assertEquals("The private key package is not correct.", "org.example.licensing.private", descriptorA01.getValue().getPackageName()); assertEquals("The public key package is not correct.", "org.example.licensing.public", descriptorA02.getValue().getPackageName()); assertEquals("The public password package is not correct.", "org.example.licensing.public", descriptorB01.getValue().getPackageName()); assertEquals("The private key class is not correct.", "PrivateKey01", descriptorA01.getValue().getClassName()); assertEquals("The public key class is not correct.", "PublicKey01", descriptorA02.getValue().getClassName()); assertEquals("The public password class is not correct.", "KeyPassword01", descriptorB01.getValue().getClassName()); } finally { EasyMock.verify(this.console.cli, privateKey, publicKey); } }
From source file:com.trsst.Command.java
public static final KeyPair readKeyPairFromFile(String alias, File file, char[] pwd) { FileInputStream input = null; try {// w ww .j a va2 s .c o m KeyStore keyStore = KeyStore.getInstance("PKCS12"); input = new FileInputStream(file); keyStore.load(new FileInputStream(file), pwd); input.close(); KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, new KeyStore.PasswordProtection(pwd)); PrivateKey privateKey = pkEntry.getPrivateKey(); PublicKey publicKey = pkEntry.getCertificate().getPublicKey(); return new KeyPair(publicKey, privateKey); } catch (/* javax.crypto.BadPaddingException */IOException bpe) { log.error("Passphrase could not decrypt key: " + bpe.getMessage()); } catch (Throwable e) { log.error("Unexpected error while reading key: " + e.getMessage(), e); } finally { if (input != null) { try { input.close(); } catch (IOException e) { // ignore while closing log.trace("Error while closing: " + e.getMessage(), e); } } } return null; }
From source file:org.ejbca.core.model.ca.catoken.CATokenContainerImpl.java
/** * Method that import CA token keys from a P12 file. Was originally used when upgrading from * old EJBCA versions. Only supports SHA1 and SHA256 with RSA or ECDSA and SHA1 with DSA. */// w ww . ja va 2s .co m public void importKeys(String authenticationCode, PrivateKey privatekey, PublicKey publickey, PrivateKey privateEncryptionKey, PublicKey publicEncryptionKey, Certificate[] caSignatureCertChain) throws Exception { // If we don't give an authentication code, perhaps we have autoactivation enabled char[] authCode = getAuthCodeOrAutoactivationPin(authenticationCode); // Currently only RSA keys are supported KeyStore keystore = KeyStore.getInstance("PKCS12", "BC"); keystore.load(null, null); // The CAs certificate is first in chain Certificate cacert = caSignatureCertChain[0]; // Assume that the same hash algorithm is used for signing that was used to sign this CA cert String signatureAlgorithm = CertTools.getSignatureAlgorithm(cacert); String keyAlg = AlgorithmTools.getKeyAlgorithm(publickey); if (keyAlg == null) { throw new Exception( "Unknown public key type: " + publickey.getAlgorithm() + " (" + publickey.getClass() + ")"); } // If this is a CVC CA we need to find out the sequence if (cacert instanceof CardVerifiableCertificate) { CardVerifiableCertificate cvccacert = (CardVerifiableCertificate) cacert; log.debug("Getting sequence from holderRef in CV certificate."); String sequence = cvccacert.getCVCertificate().getCertificateBody().getHolderReference().getSequence(); log.debug("Setting sequence " + sequence); setKeySequence(sequence); log.debug("Setting default sequence format " + StringTools.KEY_SEQUENCE_FORMAT_NUMERIC); setKeySequenceFormat(StringTools.KEY_SEQUENCE_FORMAT_NUMERIC); } else { log.debug("Setting default sequence " + CATokenConstants.DEFAULT_KEYSEQUENCE); setKeySequence(CATokenConstants.DEFAULT_KEYSEQUENCE); log.debug("Setting default sequence format " + StringTools.KEY_SEQUENCE_FORMAT_NUMERIC); setKeySequenceFormat(StringTools.KEY_SEQUENCE_FORMAT_NUMERIC); } // import sign keys. String keyspec = AlgorithmTools.getKeySpecification(publickey); Certificate[] certchain = new Certificate[1]; certchain[0] = CertTools.genSelfCert("CN=dummy", 36500, null, privatekey, publickey, signatureAlgorithm, true); keystore.setKeyEntry(SoftCAToken.PRIVATESIGNKEYALIAS, privatekey, null, certchain); // generate enc keys. // Encryption keys must be RSA still String encryptionSignatureAlgorithm = AlgorithmTools.getEncSigAlgFromSigAlg(signatureAlgorithm); keyAlg = AlgorithmTools.getKeyAlgorithmFromSigAlg(encryptionSignatureAlgorithm); keyspec = "2048"; KeyPair enckeys = null; if (publicEncryptionKey == null || privateEncryptionKey == null) { enckeys = KeyTools.genKeys(keyspec, keyAlg); } else { enckeys = new KeyPair(publicEncryptionKey, privateEncryptionKey); } // generate dummy certificate certchain[0] = CertTools.genSelfCert("CN=dummy2", 36500, null, enckeys.getPrivate(), enckeys.getPublic(), encryptionSignatureAlgorithm, true); keystore.setKeyEntry(SoftCAToken.PRIVATEDECKEYALIAS, enckeys.getPrivate(), null, certchain); // Store keystore SoftCATokenInfo info = new SoftCATokenInfo(); info.setEncKeyAlgorithm(keyAlg); info.setEncKeySpec(keyspec); info.setEncryptionAlgorithm(encryptionSignatureAlgorithm); info.setSignKeyAlgorithm(keyAlg); info.setSignKeySpec(keyspec); info.setSignatureAlgorithm(signatureAlgorithm); storeSoftKeyStore(authCode, info, null, keystore); // Finally reset the token so it will be re-read when we want to use it this.catoken = null; }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.TestConsoleRSAKeyPairGenerator.java
@Test public void testDoInteractive04() throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); this.console.cli = EasyMock.createMockBuilder(CommandLine.class).withConstructor() .addMockedMethod("hasOption", String.class).addMockedMethod("getOptionValue", String.class) .createMock();/*from ww w .j a va2 s . c o m*/ PrivateKey privateKey = EasyMock.createMock(PrivateKey.class); PublicKey publicKey = EasyMock.createMock(PublicKey.class); KeyPair keyPair = new KeyPair(publicKey, privateKey); this.device.printOutLn("Would you like to..."); EasyMock.expectLastCall(); this.device.printOutLn(" (1) Save the public and private keys to .key files?"); EasyMock.expectLastCall(); this.device.printOutLn(" (2) Generate compilable Java code with embedded keys?"); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Your selection (default 1)? ")).andReturn("2"); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Would you like to..."); EasyMock.expectLastCall(); this.device.printOutLn(" (1) Use the same password to encrypt both keys?"); EasyMock.expectLastCall(); this.device.printOutLn(" (2) Use a different password for each key?"); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Your selection (default 1)? ")).andReturn("2"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.promptForValidPassword(6, 32, "the public key")) .andReturn("publicPassword04".toCharArray()); this.device.printOutLn("Passwords match."); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.promptForValidPassword(6, 32, "the private key")) .andReturn("privatePassword04".toCharArray()); this.device.printOutLn("Passwords match."); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Please enter the name of a Java class to embed the public key in: ")) .andReturn("PublicKey02"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Enter an optional package name for the public key class: ")) .andReturn("org.example.licensing.public"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Please enter the name of a Java class to embed the private key in: ")) .andReturn("PrivateKey02"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine("Enter an optional package name for the private key class: ")) .andReturn("org.example.licensing.private"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine( "If you wish to embed the public key password in a Java class, " + "enter the class name now: ")) .andReturn("PublicPassword02"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device .readLine("You can optionally enter a package name for the public key storage " + "class: ")) .andReturn("org.example.licensing.public"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device.readLine( "If you wish to embed the private key password in a Java class, " + "enter the class name now: ")) .andReturn("PrivatePassword02"); this.device.printOutLn(); EasyMock.expectLastCall(); EasyMock.expect(this.device .readLine("You can optionally enter a package name for the private key storage " + "class: ")) .andReturn("org.example.licensing.private"); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOut("Generating RSA key pair, 2048-bit long modulus"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); EasyMock.expect(this.generator.generateKeyPair()).andReturn(keyPair); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOut("Key pair generated. Encrypting keys with 128-bit AES security"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); final Capture<GeneratedClassDescriptor> descriptorA01 = new Capture<GeneratedClassDescriptor>(); final Capture<GeneratedClassDescriptor> descriptorA02 = new Capture<GeneratedClassDescriptor>(); final Capture<GeneratedClassDescriptor> descriptorB01 = new Capture<GeneratedClassDescriptor>(); final Capture<GeneratedClassDescriptor> descriptorB02 = new Capture<GeneratedClassDescriptor>(); final Capture<char[]> passwordA01 = new Capture<char[]>(); final Capture<char[]> passwordA02 = new Capture<char[]>(); final Capture<char[]> passwordB01 = new Capture<char[]>(); final Capture<char[]> passwordB02 = new Capture<char[]>(); this.generator.saveKeyPairToProviders(EasyMock.eq(keyPair), EasyMock.capture(descriptorA01), EasyMock.capture(descriptorA02), EasyMock.capture(passwordA01), EasyMock.capture(passwordA02)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The private key password should not be null.", passwordA01.getValue()); assertArrayEquals("The private key password is not correct.", "privatePassword04".toCharArray(), passwordA01.getValue()); assertNotNull("The public key password should not be null.", passwordA02.getValue()); assertArrayEquals("The public key password is not correct.", "publicPassword04".toCharArray(), passwordA02.getValue()); descriptorA01.getValue().setJavaFileContents("privateKeyContents02"); descriptorA02.getValue().setJavaFileContents("publicKeyContents02"); return null; } }); this.generator.savePasswordToProvider(EasyMock.capture(passwordB01), EasyMock.capture(descriptorB01)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The public key password should still not be null.", passwordB01.getValue()); assertArrayEquals("The public key password is now not correct.", "publicPassword04".toCharArray(), passwordB01.getValue()); descriptorB01.getValue().setJavaFileContents("publicPasswordContents02"); return null; } }); this.generator.savePasswordToProvider(EasyMock.capture(passwordB02), EasyMock.capture(descriptorB02)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The private key password should still not be null.", passwordB02.getValue()); assertArrayEquals("The private key password is now not correct.", "privatePassword04".toCharArray(), passwordB02.getValue()); descriptorB02.getValue().setJavaFileContents("privatePasswordContents02"); return null; } }); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Private key provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("privateKeyContents02"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Public key provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("publicKeyContents02"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Public key password provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("publicPasswordContents02"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Private key password provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("privatePasswordContents02"); EasyMock.expectLastCall(); EasyMock.replay(this.generator, this.device, this.console.cli, privateKey, publicKey); try { this.console.doInteractive(); assertNotNull("The public key password should again still not be null.", passwordA01.getValue()); assertArrayEquals("The public key password should have been erased.", new char[passwordA01.getValue().length], passwordA01.getValue()); assertNotNull("The private key password should again still not be null.", passwordA02.getValue()); assertArrayEquals("The private key password should have been erased.", new char[passwordA02.getValue().length], passwordA02.getValue()); assertEquals("The private key package is not correct.", "org.example.licensing.private", descriptorA01.getValue().getPackageName()); assertEquals("The public key package is not correct.", "org.example.licensing.public", descriptorA02.getValue().getPackageName()); assertEquals("The public password package is not correct.", "org.example.licensing.public", descriptorB01.getValue().getPackageName()); assertEquals("The private password package is not correct.", "org.example.licensing.private", descriptorB02.getValue().getPackageName()); assertEquals("The private key class is not correct.", "PrivateKey02", descriptorA01.getValue().getClassName()); assertEquals("The public key class is not correct.", "PublicKey02", descriptorA02.getValue().getClassName()); assertEquals("The public password class is not correct.", "PublicPassword02", descriptorB01.getValue().getClassName()); assertEquals("The private password class is not correct.", "PrivatePassword02", descriptorB02.getValue().getClassName()); } finally { EasyMock.verify(this.console.cli, privateKey, publicKey); } }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.TestConsoleRSAKeyPairGenerator.java
@Test public void testDoCommandLine01() throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); String privateFileName = "testDoCommandLine01Private.key"; String publicFileName = "testDoCommandLine01Public.key"; this.console.cli = EasyMock.createMockBuilder(CommandLine.class).withConstructor() .addMockedMethod("hasOption", String.class).addMockedMethod("getOptionValue", String.class) .createMock();/* w w w. j a va2 s. c om*/ PrivateKey privateKey = EasyMock.createMock(PrivateKey.class); PublicKey publicKey = EasyMock.createMock(PublicKey.class); KeyPair keyPair = new KeyPair(publicKey, privateKey); EasyMock.expect(this.console.cli.hasOption("classes")).andReturn(false); EasyMock.expect(this.console.cli.hasOption("privatePassword")).andReturn(false); EasyMock.expect(this.console.cli.getOptionValue("password")).andReturn("keyPassword01"); EasyMock.expect(this.console.cli.getOptionValue("public")).andReturn(publicFileName); EasyMock.expect(this.console.cli.getOptionValue("publicPackage")).andReturn(null); EasyMock.expect(this.console.cli.getOptionValue("private")).andReturn(privateFileName); EasyMock.expect(this.console.cli.getOptionValue("privatePackage")).andReturn(null); this.device.printOut("Generating RSA key pair, 2048-bit long modulus"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); EasyMock.expect(this.generator.generateKeyPair()).andReturn(keyPair); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOut("Key pair generated. Encrypting keys with 128-bit AES security"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); final Capture<char[]> capture1 = new Capture<char[]>(); this.generator.saveKeyPairToFiles(EasyMock.eq(keyPair), EasyMock.eq(privateFileName), EasyMock.eq(publicFileName), EasyMock.capture(capture1)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The captured key password should not be null.", capture1.getValue()); assertArrayEquals("The captured key password is not correct.", "keyPassword01".toCharArray(), capture1.getValue()); return null; } }); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Private key written to " + privateFileName); EasyMock.expectLastCall(); this.device.printOutLn("Public key written to " + publicFileName); EasyMock.replay(this.generator, this.device, this.console.cli, privateKey, publicKey); try { this.console.doCommandLine(); assertNotNull("The captured key password should still not be null.", capture1.getValue()); assertArrayEquals("The captured key password should have been erased.", new char[capture1.getValue().length], capture1.getValue()); } finally { EasyMock.verify(this.console.cli, privateKey, publicKey); } }
From source file:com.microsoft.azure.keyvault.test.CertificateOperationsTest.java
private void validateCertificateKeyInKeyStore(KeyStore keyStore, X509Certificate x509Certificate, String secretPassword) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { String defaultAlias = Collections.list(keyStore.aliases()).get(0); X509Certificate secretCertificate = (X509Certificate) keyStore.getCertificate(defaultAlias); Assert.assertNotNull(secretCertificate); Assert.assertTrue(secretCertificate.getSubjectX500Principal().getName() .equals(x509Certificate.getSubjectX500Principal().getName())); Assert.assertTrue(secretCertificate.getIssuerX500Principal().getName() .equals(x509Certificate.getIssuerX500Principal().getName())); Assert.assertTrue(secretCertificate.getSerialNumber().equals(x509Certificate.getSerialNumber())); // Validate the key in the KeyStore Key secretKey = keyStore.getKey(defaultAlias, secretPassword.toCharArray()); Assert.assertNotNull(secretKey);//from ww w . j a v a 2 s.c om Assert.assertTrue(secretKey instanceof PrivateKey); PrivateKey secretPrivateKey = (PrivateKey) secretKey; // Create a KeyPair with the private key from the KeyStore and public // key from the certificate to verify they match KeyPair keyPair = new KeyPair(secretCertificate.getPublicKey(), secretPrivateKey); Assert.assertNotNull(keyPair); verifyRSAKeyPair(keyPair); }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.TestConsoleRSAKeyPairGenerator.java
@Test public void testDoCommandLine02() throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); String privateFileName = "testDoCommandLine02Private.key"; String publicFileName = "testDoCommandLine02Public.key"; this.console.cli = EasyMock.createMockBuilder(CommandLine.class).withConstructor() .addMockedMethod("hasOption", String.class).addMockedMethod("getOptionValue", String.class) .createMock();//from ww w . jav a 2 s . com PrivateKey privateKey = EasyMock.createMock(PrivateKey.class); PublicKey publicKey = EasyMock.createMock(PublicKey.class); KeyPair keyPair = new KeyPair(publicKey, privateKey); EasyMock.expect(this.console.cli.hasOption("classes")).andReturn(false); EasyMock.expect(this.console.cli.hasOption("privatePassword")).andReturn(true); EasyMock.expect(this.console.cli.getOptionValue("password")).andReturn("publicPassword02"); EasyMock.expect(this.console.cli.getOptionValue("privatePassword")).andReturn("privatePassword02"); EasyMock.expect(this.console.cli.getOptionValue("public")).andReturn(publicFileName); EasyMock.expect(this.console.cli.getOptionValue("publicPackage")).andReturn(null); EasyMock.expect(this.console.cli.getOptionValue("private")).andReturn(privateFileName); EasyMock.expect(this.console.cli.getOptionValue("privatePackage")).andReturn(null); this.device.printOut("Generating RSA key pair, 2048-bit long modulus"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); EasyMock.expect(this.generator.generateKeyPair()).andReturn(keyPair); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOut("Key pair generated. Encrypting keys with 128-bit AES security"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); final Capture<char[]> capture1 = new Capture<char[]>(); final Capture<char[]> capture2 = new Capture<char[]>(); this.generator.saveKeyPairToFiles(EasyMock.eq(keyPair), EasyMock.eq(privateFileName), EasyMock.eq(publicFileName), EasyMock.capture(capture1), EasyMock.capture(capture2)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The private key password should not be null.", capture1.getValue()); assertArrayEquals("The private key password is not correct.", "privatePassword02".toCharArray(), capture1.getValue()); assertNotNull("The public key password should not be null.", capture2.getValue()); assertArrayEquals("The public key password is not correct.", "publicPassword02".toCharArray(), capture2.getValue()); return null; } }); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Private key written to " + privateFileName); EasyMock.expectLastCall(); this.device.printOutLn("Public key written to " + publicFileName); EasyMock.replay(this.generator, this.device, this.console.cli, privateKey, publicKey); try { this.console.doCommandLine(); assertNotNull("The private key password should still not be null.", capture1.getValue()); assertArrayEquals("The private key password should have been erased.", new char[capture1.getValue().length], capture1.getValue()); assertNotNull("The public key password should still not be null.", capture2.getValue()); assertArrayEquals("The public key password should have been erased.", new char[capture2.getValue().length], capture2.getValue()); } finally { EasyMock.verify(this.console.cli, privateKey, publicKey); } }
From source file:com.microsoft.azure.keyvault.test.CertificateOperationsTest.java
private void validatePem(CertificateBundle certificateBundle, String subjectName) throws CertificateException, IOException, KeyVaultErrorException, IllegalArgumentException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { // Load the CER part into X509Certificate object X509Certificate x509Certificate = loadCerToX509Certificate(certificateBundle); Assert.assertTrue(x509Certificate.getSubjectX500Principal().getName().equals(subjectName)); Assert.assertTrue(x509Certificate.getIssuerX500Principal().getName().equals(subjectName)); // Retrieve the secret backing the certificate SecretIdentifier secretIdentifier = certificateBundle.secretIdentifier(); SecretBundle secret = keyVaultClient.getSecret(secretIdentifier.baseIdentifier()); Assert.assertTrue(secret.managed()); String secretValue = secret.value(); // Extract private key from PEM PrivateKey secretPrivateKey = extractPrivateKeyFromPemContents(secretValue); Assert.assertNotNull(secretPrivateKey); // Extract certificates from PEM List<X509Certificate> certificates = extractCertificatesFromPemContents(secretValue); Assert.assertNotNull(certificates);/*from w ww.j av a 2 s .co m*/ Assert.assertTrue(certificates.size() == 1); // has the public key corresponding to the private key. X509Certificate secretCertificate = certificates.get(0); Assert.assertNotNull(secretCertificate); Assert.assertTrue(secretCertificate.getSubjectX500Principal().getName() .equals(x509Certificate.getSubjectX500Principal().getName())); Assert.assertTrue(secretCertificate.getIssuerX500Principal().getName() .equals(x509Certificate.getIssuerX500Principal().getName())); Assert.assertTrue(secretCertificate.getSerialNumber().equals(x509Certificate.getSerialNumber())); // Create a KeyPair with the private key from the KeyStore and public // key from the certificate to verify they match KeyPair keyPair = new KeyPair(secretCertificate.getPublicKey(), secretPrivateKey); Assert.assertNotNull(keyPair); verifyRSAKeyPair(keyPair); }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.TestConsoleRSAKeyPairGenerator.java
@Test public void testDoCommandLine03() throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); this.console.cli = EasyMock.createMockBuilder(CommandLine.class).withConstructor() .addMockedMethod("hasOption", String.class).addMockedMethod("getOptionValue", String.class) .createMock();/*w ww . ja v a 2s. co m*/ PrivateKey privateKey = EasyMock.createMock(PrivateKey.class); PublicKey publicKey = EasyMock.createMock(PublicKey.class); KeyPair keyPair = new KeyPair(publicKey, privateKey); EasyMock.expect(this.console.cli.hasOption("classes")).andReturn(true); EasyMock.expect(this.console.cli.hasOption("privatePassword")).andReturn(false); EasyMock.expect(this.console.cli.getOptionValue("password")).andReturn("keyPassword03"); EasyMock.expect(this.console.cli.getOptionValue("public")).andReturn("PublicKey01"); EasyMock.expect(this.console.cli.getOptionValue("publicPackage")).andReturn("com.example.licensing.public"); EasyMock.expect(this.console.cli.getOptionValue("private")).andReturn("PrivateKey01"); EasyMock.expect(this.console.cli.getOptionValue("privatePackage")) .andReturn("com.example.licensing.private"); EasyMock.expect(this.console.cli.getOptionValue("passwordClass")).andReturn("KeyPassword01"); EasyMock.expect(this.console.cli.getOptionValue("passwordPackage")) .andReturn("com.example.licensing.public"); this.device.printOut("Generating RSA key pair, 2048-bit long modulus"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); EasyMock.expect(this.generator.generateKeyPair()).andReturn(keyPair); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOut("Key pair generated. Encrypting keys with 128-bit AES security"); EasyMock.expectLastCall(); EasyMock.expect(this.device.out()).andReturn(new PrintStream(stream)); final Capture<GeneratedClassDescriptor> descriptorA01 = new Capture<GeneratedClassDescriptor>(); final Capture<GeneratedClassDescriptor> descriptorA02 = new Capture<GeneratedClassDescriptor>(); final Capture<GeneratedClassDescriptor> descriptorB01 = new Capture<GeneratedClassDescriptor>(); final Capture<char[]> passwordA01 = new Capture<char[]>(); final Capture<char[]> passwordB01 = new Capture<char[]>(); this.generator.saveKeyPairToProviders(EasyMock.eq(keyPair), EasyMock.capture(descriptorA01), EasyMock.capture(descriptorA02), EasyMock.capture(passwordA01)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The public key password should not be null.", passwordA01.getValue()); assertArrayEquals("The public key password is not correct.", "keyPassword03".toCharArray(), passwordA01.getValue()); descriptorA01.getValue().setJavaFileContents("privateKeyContents01"); descriptorA02.getValue().setJavaFileContents("publicKeyContents01"); return null; } }); this.generator.savePasswordToProvider(EasyMock.capture(passwordB01), EasyMock.capture(descriptorB01)); EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() { @Override public Void answer() throws Throwable { assertNotNull("The public key password should still not be null.", passwordB01.getValue()); assertArrayEquals("The public key password is now not correct.", "keyPassword03".toCharArray(), passwordB01.getValue()); descriptorB01.getValue().setJavaFileContents("publicPasswordContents01"); return null; } }); this.device.printOutLn("+++"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Private key provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("privateKeyContents01"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Public key provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("publicKeyContents01"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("Key password provider:"); EasyMock.expectLastCall(); this.device.printOutLn(); EasyMock.expectLastCall(); this.device.printOutLn("publicPasswordContents01"); EasyMock.expectLastCall(); EasyMock.replay(this.generator, this.device, this.console.cli, privateKey, publicKey); try { this.console.doCommandLine(); assertNotNull("The public key password should again still not be null.", passwordA01.getValue()); assertArrayEquals("The public key password should have been erased.", new char[passwordA01.getValue().length], passwordA01.getValue()); assertEquals("The private key package is not correct.", "com.example.licensing.private", descriptorA01.getValue().getPackageName()); assertEquals("The public key package is not correct.", "com.example.licensing.public", descriptorA02.getValue().getPackageName()); assertEquals("The public password package is not correct.", "com.example.licensing.public", descriptorB01.getValue().getPackageName()); assertEquals("The private key class is not correct.", "PrivateKey01", descriptorA01.getValue().getClassName()); assertEquals("The public key class is not correct.", "PublicKey01", descriptorA02.getValue().getClassName()); assertEquals("The public password class is not correct.", "KeyPassword01", descriptorB01.getValue().getClassName()); } finally { EasyMock.verify(this.console.cli, privateKey, publicKey); } }