use of java.security.InvalidKeyException in project zaproxy by zaproxy.
the class RelaxedX509TrustManager method getTunnelSSLSocketFactory.
// ZAP: added new ServerSocketFaktory with support of dynamic SSL certificates
public SSLSocketFactory getTunnelSSLSocketFactory(String hostname) {
// KeyStore ks;
try {
SSLContext ctx = SSLContext.getInstance(SSL);
// Normally "SunX509", "IbmX509"...
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
SslCertificateService scs = CachedSslCertifificateServiceImpl.getService();
KeyStore ks = scs.createCertForHost(hostname);
kmf.init(ks, SslCertificateService.PASSPHRASE);
java.security.SecureRandom x = new java.security.SecureRandom();
x.setSeed(System.currentTimeMillis());
ctx.init(kmf.getKeyManagers(), null, x);
SSLSocketFactory tunnelSSLFactory = createDecoratedServerSslSocketFactory(ctx.getSocketFactory());
return tunnelSSLFactory;
} catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | UnrecoverableKeyException | KeyManagementException | InvalidKeyException | NoSuchProviderException | SignatureException | IOException e) {
// friendly way?
throw new RuntimeException(e);
}
}
use of java.security.InvalidKeyException in project XobotOS by xamarin.
the class Cipher method init.
/**
* Initializes this cipher instance with the public key from the specified
* certificate and a source of randomness.
* <p>
* The cipher will be initialized for the specified operation (one of:
* encryption, decryption, key wrapping or key unwrapping) depending on
* {@code opmode}.
* <p>
* It the type of the certificate is X.509 and the certificate has a <i>key
* usage</i> extension field marked as critical, the specified {@code
* opmode} has the be enabled for this key, otherwise an {@code
* InvalidKeyException} is thrown.
* <p>
* If this cipher instance needs any algorithm parameters that the key in
* the certificate can not provide, the underlying implementation of this
* cipher is supposed to generate the required parameters (using its
* provider or random values). Random values are generated using {@code
* random}.
* <p>
* When a cipher instance is initialized by a call to any of the {@code
* init} methods, the state of the instance is overridden, means it is
* equivalent to creating a new instance and calling it {@code init} method.
*
* @param opmode
* the operation this cipher instance should be initialized for
* (one of: {@code ENCRYPT_MODE}, {@code DECRYPT_MODE}, {@code
* WRAP_MODE} or {@code UNWRAP_MODE}).
* @param certificate
* the certificate.
* @param random
* the source of randomness to be used.
* @throws InvalidKeyException
* if the public key in the certificate can not be used to
* initialize this cipher instance.
*/
public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException {
checkMode(opmode);
if (certificate instanceof X509Certificate) {
Set<String> ce = ((X509Certificate) certificate).getCriticalExtensionOIDs();
boolean critical = false;
if (ce != null && !ce.isEmpty()) {
for (String oid : ce) {
if (oid.equals("2.5.29.15")) {
// KeyUsage OID = 2.5.29.15
critical = true;
break;
}
}
if (critical) {
boolean[] keyUsage = ((X509Certificate) certificate).getKeyUsage();
// decipherOnly (8) }
if (keyUsage != null) {
if (opmode == ENCRYPT_MODE && !keyUsage[3]) {
throw new InvalidKeyException("The public key in the certificate " + "cannot be used for ENCRYPT_MODE");
} else if (opmode == WRAP_MODE && !keyUsage[2]) {
throw new InvalidKeyException("The public key in the certificate " + "cannot be used for WRAP_MODE");
}
}
}
}
}
// FIXME InvalidKeyException
// if keysize exceeds the maximum allowable keysize
// (jurisdiction policy files)
spiImpl.engineInit(opmode, certificate.getPublicKey(), random);
mode = opmode;
}
use of java.security.InvalidKeyException in project XobotOS by xamarin.
the class EncryptedPrivateKeyInfo method getKeySpec.
/**
* Returns the {@code PKCS8EncodedKeySpec} object extracted from the
* encrypted data.
*
* @param decryptKey
* the key to decrypt the encrypted data with.
* @return the extracted {@code PKCS8EncodedKeySpec}.
* @throws NoSuchAlgorithmException
* if no usable cipher can be found to decrypt the encrypted
* data.
* @throws InvalidKeyException
* if {@code decryptKey} is not usable to decrypt the encrypted
* data.
* @throws NullPointerException
* if {@code decryptKey} is {@code null}.
*/
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey) throws NoSuchAlgorithmException, InvalidKeyException {
if (decryptKey == null) {
throw new NullPointerException("decryptKey == null");
}
try {
Cipher cipher = Cipher.getInstance(algName);
if (algParameters == null) {
cipher.init(Cipher.DECRYPT_MODE, decryptKey);
} else {
cipher.init(Cipher.DECRYPT_MODE, decryptKey, algParameters);
}
byte[] decryptedData = cipher.doFinal(encryptedData);
try {
ASN1PrivateKeyInfo.verify(decryptedData);
} catch (IOException e1) {
throw invalidKey();
}
return new PKCS8EncodedKeySpec(decryptedData);
} catch (NoSuchPaddingException e) {
throw new NoSuchAlgorithmException(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
throw new NoSuchAlgorithmException(e.getMessage());
} catch (IllegalStateException e) {
throw new InvalidKeyException(e.getMessage());
} catch (IllegalBlockSizeException e) {
throw new InvalidKeyException(e.getMessage());
} catch (BadPaddingException e) {
throw new InvalidKeyException(e.getMessage());
}
}
use of java.security.InvalidKeyException in project XobotOS by xamarin.
the class SealedObject method getObject.
/**
* Returns the wrapped object, decrypting it using the specified key.
*
* @param key
* the key to decrypt the data with.
* @return the encapsulated object.
* @throws IOException
* if deserialization fails.
* @throws ClassNotFoundException
* if deserialization fails.
* @throws NoSuchAlgorithmException
* if the algorithm to decrypt the data is not available.
* @throws InvalidKeyException
* if the specified key cannot be used to decrypt the data.
*/
public final Object getObject(Key key) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeyException {
if (key == null) {
throw new InvalidKeyException("key == null");
}
try {
Cipher cipher = Cipher.getInstance(sealAlg);
if ((paramsAlg != null) && (paramsAlg.length() != 0)) {
AlgorithmParameters params = AlgorithmParameters.getInstance(paramsAlg);
params.init(encodedParams);
cipher.init(Cipher.DECRYPT_MODE, key, params);
} else {
cipher.init(Cipher.DECRYPT_MODE, key);
}
byte[] serialized = cipher.doFinal(encryptedContent);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serialized));
return ois.readObject();
} catch (NoSuchPaddingException e) {
// with existing padding
throw new NoSuchAlgorithmException(e.toString());
} catch (InvalidAlgorithmParameterException e) {
// with correct algorithm parameters
throw new NoSuchAlgorithmException(e.toString());
} catch (IllegalBlockSizeException e) {
// was correctly made
throw new NoSuchAlgorithmException(e.toString());
} catch (BadPaddingException e) {
// was correctly made
throw new NoSuchAlgorithmException(e.toString());
} catch (IllegalStateException e) {
// should never be thrown because cipher is initialized
throw new NoSuchAlgorithmException(e.toString());
}
}
use of java.security.InvalidKeyException in project XobotOS by xamarin.
the class WrapCipherSpi method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
AlgorithmParameterSpec paramSpec = null;
if (params != null) {
for (int i = 0; i != availableSpecs.length; i++) {
try {
paramSpec = params.getParameterSpec(availableSpecs[i]);
break;
} catch (Exception e) {
// try next spec
}
}
if (paramSpec == null) {
throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
}
}
engineParams = params;
engineInit(opmode, key, paramSpec, random);
}
Aggregations