use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class OpenSslPubUtil method load.
/**
* Load an unencrypted OpenSSL public key from the stream. The encoding of
* the public key may be PEM or DER.
*
* @param is
* Stream to load the unencrypted public key from
* @return The public key
* @throws CryptoException
* Problem encountered while loading the public key
* @throws IOException
* An I/O error occurred
*/
public static PublicKey load(InputStream is) throws CryptoException, IOException {
byte[] streamContents = ReadUtil.readFully(is);
// Check if stream is PEM encoded
PemInfo pemInfo = PemUtil.decode(new ByteArrayInputStream(streamContents));
if (pemInfo != null) {
// It is - get DER from PEM
streamContents = pemInfo.getContent();
}
try {
// DER-encoded subjectPublicKeyInfo structure - the OpenSSL format
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(streamContents);
return new JcaPEMKeyConverter().getPublicKey(publicKeyInfo);
} catch (Exception ex) {
throw new CryptoException(res.getString("NoLoadOpenSslPublicKey.exception.message"), ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class SecretKeyUtil method generateSecretKey.
/**
* Generate a secret key.
*
* @param secretKeyType
* Secret key type to generate
* @param keySize
* Key size of secret key
* @return Secret key
* @throws CryptoException
* If there was a problem generating the secret key
*/
public static SecretKey generateSecretKey(SecretKeyType secretKeyType, int keySize) throws CryptoException {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(secretKeyType.jce(), BOUNCY_CASTLE.jce());
keyGenerator.init(keySize, SecureRandom.getInstance("SHA1PRNG"));
return keyGenerator.generateKey();
} catch (GeneralSecurityException ex) {
throw new CryptoException(MessageFormat.format(res.getString("NoGenerateSecretKey.exception.message"), secretKeyType), ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class MidletSigner method signJarDigest.
private static byte[] signJarDigest(File jarFile, RSAPrivateKey privateKey) throws CryptoException {
// Create a SHA-1 signature for the supplied JAR file
try (FileInputStream fis = new FileInputStream(jarFile)) {
Signature signature = Signature.getInstance(SignatureType.SHA1_RSA.jce());
signature.initSign(privateKey);
byte[] buffer = new byte[1024];
int read = 0;
while ((read = fis.read(buffer)) != -1) {
signature.update(buffer, 0, read);
}
return signature.sign();
} catch (IOException ex) {
throw new CryptoException(res.getString("JarDigestSignatureFailed.exception.message"), ex);
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("JarDigestSignatureFailed.exception.message"), ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class KeyPairUtil method validKeyPair.
/**
* Check that the supplied private and public keys actually comprise a valid
* key pair.
*
* @param privateKey
* Private key
* @param publicKey
* Public key
* @return True if the private and public keys comprise a valid key pair,
* false otherwise.
* @throws CryptoException
* If there is a problem validating the key pair
*/
public static boolean validKeyPair(PrivateKey privateKey, PublicKey publicKey) throws CryptoException {
try {
String privateAlgorithm = privateKey.getAlgorithm();
String publicAlgorithm = publicKey.getAlgorithm();
if (!privateAlgorithm.equals(publicAlgorithm)) {
return false;
}
// private key and verifying the signature with the public key
if (privateAlgorithm.equals(RSA.jce())) {
byte[] toSign = "Rivest Shamir Adleman".getBytes();
String signatureAlgorithm = "SHA256withRSA";
byte[] signature = sign(toSign, privateKey, signatureAlgorithm);
return verify(toSign, signature, publicKey, signatureAlgorithm);
} else if (privateAlgorithm.equals(DSA.jce())) {
byte[] toSign = "Digital Signature Algorithm".getBytes();
String signatureAlgorithm = "SHA1withDSA";
byte[] signature = sign(toSign, privateKey, signatureAlgorithm);
return verify(toSign, signature, publicKey, signatureAlgorithm);
} else if (privateAlgorithm.equals(EC.jce())) {
byte[] toSign = "EC Digital Signature Algorithm".getBytes();
String signatureAlgorithm = "SHA256withECDSA";
byte[] signature = sign(toSign, privateKey, signatureAlgorithm);
return verify(toSign, signature, publicKey, signatureAlgorithm);
} else {
throw new CryptoException(MessageFormat.format(res.getString("NoCheckCompriseValidKeypairAlg.exception.message"), privateAlgorithm));
}
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("NoCheckCompriseValidKeypair.exception.message"), ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class KeyPairUtil method getKeyInfo.
/**
* Get the information about the supplied public key.
*
* @param publicKey
* The public key
* @return Key information
* @throws CryptoException
* If there is a problem getting the information
*/
public static KeyInfo getKeyInfo(PublicKey publicKey) throws CryptoException {
try {
String algorithm = publicKey.getAlgorithm();
if (algorithm.equals(RSA.jce())) {
KeyFactory keyFact = KeyFactory.getInstance(algorithm, BOUNCY_CASTLE.jce());
RSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class);
BigInteger modulus = keySpec.getModulus();
return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length());
} else if (algorithm.equals(DSA.jce())) {
KeyFactory keyFact = KeyFactory.getInstance(algorithm);
DSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, DSAPublicKeySpec.class);
BigInteger prime = keySpec.getP();
return new KeyInfo(ASYMMETRIC, algorithm, prime.toString(2).length());
} else if (algorithm.equals(EC.jce())) {
ECPublicKey pubk = (ECPublicKey) publicKey;
int size = pubk.getParams().getOrder().bitLength();
return new KeyInfo(ASYMMETRIC, algorithm, size);
}
// size unknown
return new KeyInfo(ASYMMETRIC, algorithm);
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("NoPublicKeysize.exception.message"), ex);
}
}
Aggregations