Example usage for org.bouncycastle.jce.provider BouncyCastleProvider PROVIDER_NAME

List of usage examples for org.bouncycastle.jce.provider BouncyCastleProvider PROVIDER_NAME

Introduction

In this page you can find the example usage for org.bouncycastle.jce.provider BouncyCastleProvider PROVIDER_NAME.

Prototype

String PROVIDER_NAME

To view the source code for org.bouncycastle.jce.provider BouncyCastleProvider PROVIDER_NAME.

Click Source Link

Usage

From source file:com.cloud.utils.crypt.RSAHelper.java

License:Apache License

public static String encryptWithSSHPublicKey(String sshPublicKey, String content) {
    String returnString = null;//from w  w w .j  a va 2 s . c om
    try {
        RSAPublicKey publicKey = readKey(sshPublicKey);
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", BouncyCastleProvider.PROVIDER_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey, new SecureRandom());
        byte[] encrypted = cipher.doFinal(content.getBytes());
        returnString = Base64.encodeBase64String(encrypted);
    } catch (Exception e) {
    }

    return returnString;
}

From source file:com.eucalyptus.bootstrap.SystemBootstrapper.java

License:Open Source License

/**
 * {@inheritDoc #handleException(Throwable)}
 * /*from   w w w  .  j av  a 2  s.c  o  m*/
 * @return
 * @throws Throwable
 */
public boolean init() throws Throwable {
    ExpandoMetaClass.enableGlobally();
    Logs.init();
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            try {
                String stack = Joiner.on("\t\n").join(Thread.currentThread().getStackTrace());
                LOG.error(stack);
                LOG.error(e, e);
            } catch (Exception ex) {
                try {
                    System.out.println(Joiner.on("\t\n").join(Thread.currentThread().getStackTrace()));
                    e.printStackTrace();
                    ex.printStackTrace();
                } catch (Exception ex1) {
                    System.out.println("Failed because of badness in uncaught exception path.");
                    System.out.println("Thread:      " + t.toString());
                    System.out.println("Exception:   " + e.getClass());
                    System.out.println("Message:     " + e.getMessage());
                    System.out.println("All threads:\n");
                    for (Map.Entry<Thread, StackTraceElement[]> ent : Thread.getAllStackTraces().entrySet()) {

                    }
                }
            }
        }
    });
    OrderedShutdown.initialize();
    BootstrapArgs.init();
    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        if (Security.getProviders().length > 4) {
            Security.insertProviderAt(new BouncyCastleProvider(), 4); // EUCA-5833
        } else {
            Security.addProvider(new BouncyCastleProvider());
        }
    }
    try {//GRZE:HACK: need to remove the nss add-on in deb based distros as it breaks ssl.
        Groovyness.eval(
                "import sun.security.jca.*; Providers.setProviderList( ProviderList.remove( Providers.@providerList,\"SunPKCS11-NSS\") );");
    } catch (Exception ex) {
        LOG.error(ex, ex);
    }
    try {
        if (!BootstrapArgs.isInitializeSystem()) {
            Bootstrap.init();
            Bootstrap.Stage stage = Bootstrap.transition();
            stage.load();
        }
        return true;
    } catch (Throwable t) {
        SystemBootstrapper.handleException(t);
        return false;
    }
}

From source file:com.google.code.commons.checksum.digest.TestDigestUtils.java

License:Apache License

@Test
public void registerPreferredProvider() {
    Assert.assertTrue(DigestUtils.registerPreferredProvider("MD5", BouncyCastleProvider.PROVIDER_NAME));
    Assert.assertEquals(BouncyCastleProvider.PROVIDER_NAME,
            DigestUtils.getDigest("MD5").getProvider().getName());
}

From source file:com.google.code.commons.checksum.digest.TestDigestUtils.java

License:Apache License

@After
public void removeBouncyCastleProvider() throws Exception {
    Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
}

From source file:com.google.code.commons.checksum.digest.TestDigestUtilsWithoutBouncyCastle.java

License:Apache License

@Test
public void registerPreferredProvider() {
    Assert.assertFalse(DigestUtils.registerPreferredProvider("MD5", BouncyCastleProvider.PROVIDER_NAME));
    Assert.assertFalse(DigestUtils.registerPreferredProvider("RIPEMD128", "SUN"));
}

From source file:com.guardtime.ksi.hashing.DataHasher.java

License:Apache License

/**
 * Create new data hasher for specified algorithm.
 *
 * @param algorithm/* w w w .jav  a 2  s. co  m*/
 *         HashAlgorithm describing the algorithm to be used in hashing.
 * @throws HashException
 *         when hash algorithm is unknown or input algorithm is null
 */
public DataHasher(HashAlgorithm algorithm) throws HashException {
    if (algorithm == null) {
        throw new HashException("Invalid algorithm added to hasher: null");
    }

    /*
    If an algorithm is given which is not implemented, an HashAlgorithmNotImplementedException is thrown
    The developer must ensure that only implemented algorithms are used.
     */
    if (HashAlgorithm.Status.NOT_IMPLEMENTED.equals(algorithm.getStatus())) {
        throw new HashAlgorithmNotImplementedException(
                "Hash algorithm " + algorithm.name() + " is not implemented");
    }

    this.algorithm = algorithm;

    String provider = BouncyCastleProvider.PROVIDER_NAME;
    if (Security.getProvider(provider) == null) {
        Security.addProvider(new BouncyCastleProvider());
    }

    try {
        messageDigest = MessageDigest.getInstance(algorithm.getName(), provider);
    } catch (NoSuchAlgorithmException e) {
        throw new HashException("Hash algorithm not supported: " + algorithm.getName());
    } catch (NoSuchProviderException e) {
        throw new HashException("Cryptographic provider not found: " + provider, e);
    }
}

From source file:com.guardtime.ksi.trust.CMSSignatureVerifier.java

License:Apache License

private void verifyCmsSignerInfo(SignerInformation signerInfo, X509CertificateHolder certHolder)
        throws InvalidCmsSignatureException {
    try {/* ww w  .  j  a v a2 s. c  o m*/
        SignerInformationVerifier signerInformationVerifier = new JcaSimpleSignerInfoVerifierBuilder()
                .setProvider(BouncyCastleProvider.PROVIDER_NAME).build(certHolder);
        if (!signerInfo.verify(signerInformationVerifier)) {
            LOGGER.warn(
                    "Signer certificate verification failure. Signer info is {}, and certificate subjectDN is {}",
                    signerInfo, certHolder.getSubject());
            throw new InvalidCmsSignatureException("Signature verification failure");
        }
    } catch (CMSException e) {
        throw new InvalidCmsSignatureException("Invalid CMS signature. " + e.getMessage(), e);
    } catch (OperatorCreationException e) {
        throw new InvalidCmsSignatureException("CMS signature validation failed. " + e.getMessage(), e);
    } catch (CertificateException e) {
        throw new InvalidCmsSignatureException("CMS signature validation failed. " + e.getMessage(), e);
    }
}

From source file:com.guardtime.ksi.trust.CMSSignatureVerifier.java

License:Apache License

private X509Certificate getCertificate(X509CertificateHolder certHolder) throws InvalidCmsSignatureException {
    try {/*w  w w  .j  a  va  2s .c  o  m*/
        return new JcaX509CertificateConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME)
                .getCertificate(certHolder);
    } catch (CertificateException e) {
        throw new InvalidCmsSignatureException("Invalid certificate in CMS signature. " + e.getMessage(), e);
    }
}

From source file:com.guardtime.ksi.unisignature.verifier.rules.CalendarAuthenticationRecordSignatureVerificationRule.java

License:Apache License

public VerificationResultCode verifySignature(VerificationContext context) throws KSIException {
    CalendarAuthenticationRecord authenticationRecord = context.getCalendarAuthenticationRecord();
    SignatureData signatureData = authenticationRecord.getSignatureData();
    Certificate certificate = context.getCertificate(signatureData.getCertificateId());
    try {//from w w  w.jav a 2  s. c om
        Signature sig = Signature.getInstance(((X509Certificate) certificate).getSigAlgName(),
                BouncyCastleProvider.PROVIDER_NAME);
        sig.initVerify(certificate);
        sig.update(authenticationRecord.getPublicationData().getEncoded());
        if (!sig.verify(signatureData.getSignatureValue())) {
            LOGGER.info("Invalid calendar authentication record signature.");
            return VerificationResultCode.FAIL;
        }
    } catch (GeneralSecurityException e) {
        LOGGER.warn("General PKI security exception occurred when verifying KSI signature. " + e.getMessage(),
                e);
        return VerificationResultCode.FAIL;
    }

    return VerificationResultCode.OK;
}

From source file:com.guardtime.tsp.GTDataHash.java

License:Apache License

/**
 * Class constructor./* ww w  .j  a  va2 s.  co  m*/
 * <p>
 * Creates new hash object.
 *
 * @param hashAlgorithm hash algorithm to use in this hash object.
 * @param hashedMessage hash value. If set to {@code null}, hash object will
 *          be created with open hash calculator; otherwise, hash algorithm
 *          and hash value correspondence will be checked.
 *
 * @throws RuntimeException if required cryptographic provider is not set.
 */
private GTDataHash(GTHashAlgorithm hashAlgorithm, byte[] hashedMessage) {
    if (hashAlgorithm == null) {
        throw new IllegalArgumentException("invalid hash algorithm: null");
    }

    this.hashAlgorithm = hashAlgorithm;
    this.hashedMessage = hashedMessage;

    if (hashedMessage == null) { // No hashed message -- initialize new digest
        String provider = BouncyCastleProvider.PROVIDER_NAME;
        if (Security.getProvider(provider) == null) {
            Security.addProvider(new BouncyCastleProvider());
        }

        try {
            this.messageDigest = MessageDigest.getInstance(hashAlgorithm.getName(), provider);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Hash algorithm not supported: " + hashAlgorithm.getName());
        } catch (NoSuchProviderException e) {
            throw new RuntimeException("Cryptographic provider not found: " + provider, e);
        }

        setBufferSize(DEFAULT_BUFFER_SIZE);
    } else if (hashAlgorithm.getHashLength() != hashedMessage.length) {
        throw new IllegalArgumentException("hash length does not match with that defined in hash algorithm");
    } else { // Hashed message set -- no digest needed
        this.messageDigest = null;
    }
}