use of java.security.PrivateKey in project Openfire by igniterealtime.
the class CertificateManager method installReply.
/**
* Installs the Certificate Authority reply returned as part of the signing request. The certificate
* being signed will get its certificate chain updated with the imported certificate(s). An exception
* will be thrown if the replied certificate does not match a local certificate or if the signing
* authority is not known by the server (i.e. keystore and truststore files)
*
* The identity of the entity that has signed the reply is verified against the provided trust store.
*
* The
*
* @param keyStore key store where the certificate is stored.
* @param trustStore key store where ca certificates are stored.
* @param keyPassword password of the keystore.
* @param alias the alias of the existing certificate being signed.
* @param inputStream the stream containing the CA reply.
* @return true if the CA reply was successfully processed.
* @throws Exception
*/
public static boolean installReply(KeyStore keyStore, KeyStore trustStore, char[] keyPassword, String alias, InputStream inputStream) throws Exception {
// Check that there is a certificate for the specified alias
X509Certificate certificate = (X509Certificate) keyStore.getCertificate(alias);
if (certificate == null) {
Log.warn("Certificate not found for alias: " + alias);
return false;
}
// Retrieve the private key of the stored certificate
PrivateKey privKey = (PrivateKey) keyStore.getKey(alias, keyPassword);
// Load certificates found in the PEM input stream
Collection<X509Certificate> certs = parseCertificates(inputStream);
if (certs.isEmpty()) {
throw new Exception("Reply has no certificates");
}
List<X509Certificate> newCerts;
if (certs.size() == 1) {
// Reply has only one certificate
newCerts = establishCertChain(keyStore, trustStore, null, certs.iterator().next());
} else {
// Reply has a chain of certificates
newCerts = validateReply(keyStore, trustStore, alias, null, certs);
}
if (newCerts == null) {
return false;
}
keyStore.setKeyEntry(alias, privKey, keyPassword, newCerts.toArray(new X509Certificate[newCerts.size()]));
// Notify listeners that a new certificate has been created
for (CertificateEventListener listener : listeners) {
try {
listener.certificateSigned(keyStore, alias, newCerts);
} catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
return true;
}
use of java.security.PrivateKey in project PushSms by koush.
the class MiddlewareService method getOrCreateKeyPair.
// create/read the keypair as necessary
private void getOrCreateKeyPair() {
String encodedKeyPair = settings.getString("keypair", null);
if (encodedKeyPair != null) {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
ByteArrayInputStream bin = new ByteArrayInputStream(Base64.decode(encodedKeyPair, Base64.DEFAULT));
ObjectInputStream in = new ObjectInputStream(bin);
rsaPublicKeySpec = new RSAPublicKeySpec((BigInteger) in.readObject(), (BigInteger) (in.readObject()));
RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec((BigInteger) in.readObject(), (BigInteger) (in.readObject()));
PublicKey pub = keyFactory.generatePublic(rsaPublicKeySpec);
PrivateKey priv = keyFactory.generatePrivate(rsaPrivateKeySpec);
keyPair = new KeyPair(pub, priv);
return;
} catch (Exception e) {
Log.e(LOGTAG, "KeyPair load error", e);
}
}
try {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(2048);
keyPair = gen.generateKeyPair();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
rsaPublicKeySpec = keyFactory.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
RSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(rsaPublicKeySpec.getModulus());
out.writeObject(rsaPublicKeySpec.getPublicExponent());
out.writeObject(privateKeySpec.getModulus());
out.writeObject(privateKeySpec.getPrivateExponent());
out.flush();
settings.edit().putString("keypair", Base64.encodeToString(bout.toByteArray(), Base64.DEFAULT)).commit();
settings.edit().putBoolean("needs_register", true).commit();
} catch (Exception e) {
Log.wtf(LOGTAG, "KeyPair generation error", e);
keyPair = null;
}
}
use of java.security.PrivateKey in project jjwt by jwtk.
the class EllipticCurveSigner method doSign.
protected byte[] doSign(byte[] data) throws InvalidKeyException, java.security.SignatureException, JwtException {
PrivateKey privateKey = (PrivateKey) key;
Signature sig = createSignatureInstance();
sig.initSign(privateKey);
sig.update(data);
return transcodeSignatureToConcat(sig.sign(), getSignatureByteArrayLength(alg));
}
use of java.security.PrivateKey in project jjwt by jwtk.
the class RsaSigner method doSign.
protected byte[] doSign(byte[] data) throws InvalidKeyException, java.security.SignatureException {
PrivateKey privateKey = (PrivateKey) key;
Signature sig = createSignatureInstance();
sig.initSign(privateKey);
sig.update(data);
return sig.sign();
}
use of java.security.PrivateKey in project neo4j by neo4j.
the class Certificates method createSelfSignedCertificate.
public void createSelfSignedCertificate(File certificatePath, File privateKeyPath, String hostName) throws GeneralSecurityException, IOException, OperatorCreationException {
installCleanupHook(certificatePath, privateKeyPath);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(DEFAULT_ENCRYPTION);
keyGen.initialize(2048, random);
KeyPair keypair = keyGen.generateKeyPair();
// Prepare the information required for generating an X.509 certificate.
X500Name owner = new X500Name("CN=" + hostName);
X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(owner, new BigInteger(64, random), NOT_BEFORE, NOT_AFTER, owner, keypair.getPublic());
PrivateKey privateKey = keypair.getPrivate();
ContentSigner signer = new JcaContentSignerBuilder("SHA512WithRSAEncryption").build(privateKey);
X509CertificateHolder certHolder = builder.build(signer);
X509Certificate cert = new JcaX509CertificateConverter().setProvider(PROVIDER).getCertificate(certHolder);
//check so that cert is valid
cert.verify(keypair.getPublic());
//write to disk
writePem("CERTIFICATE", cert.getEncoded(), certificatePath);
writePem("PRIVATE KEY", privateKey.getEncoded(), privateKeyPath);
// Mark as done so we don't clean up certificates
cleanupRequired = false;
}
Aggregations