use of org.mozilla.jss.NotInitializedException in project jss by dogtagpki.
the class Certificate method verify.
/**
* Verifies the signature on this certificate, using the given public key.
* Does not indicate the certificate is valid at any specific time.
*/
public void verify(PublicKey key) throws InvalidKeyException, NoSuchAlgorithmException, CertificateException, SignatureException {
try {
CryptoManager cm = CryptoManager.getInstance();
verify(key, cm.getInternalCryptoToken());
} catch (NotInitializedException e) {
throw new SignatureException("CryptoManager not initialized");
}
}
use of org.mozilla.jss.NotInitializedException in project jss by dogtagpki.
the class JSSTrustManager method getAcceptedIssuers.
@Override
public X509Certificate[] getAcceptedIssuers() {
logger.debug("JSSTrustManager: getAcceptedIssuers():");
Collection<X509Certificate> caCerts = new ArrayList<>();
try {
CryptoManager manager = CryptoManager.getInstance();
for (org.mozilla.jss.crypto.X509Certificate cert : manager.getCACerts()) {
logger.debug("JSSTrustManager: - " + cert.getSubjectDN());
try {
PK11Cert caCert = (PK11Cert) cert;
caCert.checkValidity();
caCerts.add(caCert);
} catch (Exception e) {
logger.debug("JSSTrustManager: invalid CA certificate: " + e);
}
}
} catch (NotInitializedException e) {
logger.error("JSSTrustManager: Unable to get CryptoManager: " + e, e);
throw new RuntimeException(e);
}
return caCerts.toArray(new X509Certificate[caCerts.size()]);
}
use of org.mozilla.jss.NotInitializedException in project jss by dogtagpki.
the class EncryptedPrivateKeyInfo method createPBE.
/**
* Creates a new EncryptedPrivateKeyInfo, where the data is encrypted
* with a password-based key-
* with wrapping/unwrapping happening on token.
*
* @param pbeAlg The algorithm for generating a symmetric key from
* a password, salt, and iteration count.
* @param password The password to use in generating the key.
* @param salt The salt to use in generating the key.
* @param iterationCount The number of hashing iterations to perform
* while generating the key.
* @param charToByteConverter The mechanism for converting the characters
* in the password into bytes. If null, the default mechanism
* will be used, which is UTF8.
* @param pri The PrivateKey to be encrypted and stored in the
* EncryptedContentInfo.
*/
public static EncryptedPrivateKeyInfo createPBE(PBEAlgorithm pbeAlg, Password password, byte[] salt, int iterationCount, KeyGenerator.CharToByteConverter charToByteConverter, PrivateKey pri, CryptoToken token) throws NotInitializedException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, TokenException, CharConversionException {
try {
// generate key
KeyGenerator kg = token.getKeyGenerator(pbeAlg);
PBEKeyGenParams pbekgParams = new PBEKeyGenParams(password, salt, iterationCount);
if (charToByteConverter != null) {
kg.setCharToByteConverter(charToByteConverter);
}
kg.initialize(pbekgParams);
kg.temporaryKeys(true);
SymmetricKey key = kg.generate();
// generate IV
EncryptionAlgorithm encAlg = pbeAlg.getEncryptionAlg();
AlgorithmParameterSpec params = null;
Class<?>[] paramClasses = encAlg.getParameterClasses();
for (int i = 0; i < paramClasses.length; i++) {
if (paramClasses[i].equals(javax.crypto.spec.IvParameterSpec.class)) {
params = new IVParameterSpec(kg.generatePBE_IV());
break;
}
}
// wrap the key
KeyWrapper wrapper = token.getKeyWrapper(KeyWrapAlgorithm.fromOID(encAlg.toOID()));
wrapper.initWrap(key, params);
byte[] encrypted = wrapper.wrap(pri);
// make encryption algorithm identifier
PBEParameter pbeParam = new PBEParameter(salt, iterationCount);
AlgorithmIdentifier encAlgID = new AlgorithmIdentifier(pbeAlg.toOID(), pbeParam);
// create EncryptedPrivateKeyInfo
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(encAlgID, new OCTET_STRING(encrypted));
return epki;
} catch (Exception e) {
System.out.println("createPBE: exception:" + e.toString());
throw new RuntimeException("Exception in EncryptedPrivateKeyInfo" + ".createPBE: " + e.getMessage(), e);
}
}
use of org.mozilla.jss.NotInitializedException in project jss by dogtagpki.
the class JSSKeyStoreSpi method engineDeleteEntry.
@Override
public void engineDeleteEntry(String alias) throws KeyStoreException {
try {
CryptoManager manager = CryptoManager.getInstance();
try {
logger.debug("JSSKeyStoreSpi: searching for cert");
X509Certificate cert = manager.findCertByNickname(alias);
CryptoToken token;
if (cert instanceof TokenCertificate) {
TokenCertificate tokenCert = (TokenCertificate) cert;
token = tokenCert.getOwningToken();
} else {
token = manager.getInternalKeyStorageToken();
}
CryptoStore store = token.getCryptoStore();
logger.debug("JSSKeyStoreSpi: deleting cert: " + alias);
store.deleteCertOnly(cert);
return;
} catch (ObjectNotFoundException e) {
logger.debug("JSSKeyStoreSpi: cert not found, searching for key");
}
String[] parts = parseAlias(alias);
String tokenName = parts[0];
String nickname = parts[1];
CryptoToken token;
if (tokenName == null) {
token = manager.getInternalKeyStorageToken();
} else {
token = manager.getTokenByName(tokenName);
}
CryptoStore store = token.getCryptoStore();
logger.debug("JSSKeyStoreSpi: searching for private key");
for (PrivateKey privateKey : store.getPrivateKeys()) {
// convert key ID into hexadecimal
String keyID = Utils.HexEncode(privateKey.getUniqueID());
logger.debug("JSSKeyStoreSpi: - " + keyID);
if (!nickname.equals(keyID)) {
continue;
}
try {
logger.debug("JSSKeyStoreSpi: searching for public key: " + nickname);
PublicKey publicKey = store.findPublicKey(privateKey);
logger.debug("JSSKeyStoreSpi: deleting public key: " + nickname);
store.deletePublicKey(publicKey);
} catch (ObjectNotFoundException e) {
logger.debug("JSSKeyStoreSpi: public key not found: " + nickname);
}
logger.debug("JSSKeyStoreSpi: deleting private key: " + nickname);
store.deletePrivateKey(privateKey);
return;
}
logger.debug("JSSKeyStoreSpi: entry not found: " + alias);
throw new KeyStoreException("Entry not found: " + alias);
} catch (NotInitializedException e) {
throw new KeyStoreException(e);
} catch (NoSuchTokenException e) {
throw new KeyStoreException(e);
} catch (TokenException e) {
throw new KeyStoreException(e);
} catch (NoSuchItemOnTokenException e) {
throw new KeyStoreException(e);
}
}
use of org.mozilla.jss.NotInitializedException in project jss by dogtagpki.
the class JSSKeyStoreSpi method engineIsCertificateEntry.
/**
* Returns true if there is a cert with this nickname but there is no
* key associated with the cert.
*/
@Override
public boolean engineIsCertificateEntry(String alias) {
logger.debug("JSSKeyStoreSpi: engineIsCertificateEntry(" + alias + ")");
try {
CryptoManager cm = CryptoManager.getInstance();
cm.findCertByNickname(alias);
logger.debug("JSSKeyStoreSpi: cert found: " + alias);
return true;
} catch (ObjectNotFoundException e) {
logger.debug("JSSKeyStoreSpi: cert not found: " + alias);
return false;
} catch (NotInitializedException e) {
throw new RuntimeException(e);
} catch (TokenException e) {
throw new RuntimeException(e);
}
}
Aggregations