Search in sources :

Example 91 with SecureRandom

use of java.security.SecureRandom in project OpenAttestation by OpenAttestation.

the class X509Builder method randomSerial.

public X509Builder randomSerial() {
    try {
        BigInteger sn = new BigInteger(64, new SecureRandom());
        certificateSerialNumber = new CertificateSerialNumber(sn);
        info.set(X509CertInfo.SERIAL_NUMBER, certificateSerialNumber);
    } catch (Exception e) {
        fault(e, "randomSerial");
    }
    return this;
}
Also used : CertificateSerialNumber(sun.security.x509.CertificateSerialNumber) BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom)

Example 92 with SecureRandom

use of java.security.SecureRandom in project OpenAttestation by OpenAttestation.

the class CertificateUtils method generateSelfSignedX509Certificate.

/**
	 * Generate a self signed X509 certificate with Bouncy Castle.
	 * @throws SignatureException 
	 * @throws IllegalStateException 
	 * @throws InvalidKeyException 
	 * @throws CertificateEncodingException 
	 */
public static X509Certificate generateSelfSignedX509Certificate() throws NoSuchAlgorithmException, NoSuchProviderException, CertificateEncodingException, InvalidKeyException, IllegalStateException, SignatureException {
    Security.addProvider(new BouncyCastleProvider());
    int validityDays = 3652;
    // GENERATE THE PUBLIC/PRIVATE RSA KEY PAIR
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
    keyPairGenerator.initialize(1024, new SecureRandom());
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    // GENERATE THE X509 CERTIFICATE
    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
    X500Principal dnName = new X500Principal("CN=OATServer");
    certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
    certGen.setSubjectDN(dnName);
    // use the same
    certGen.setIssuerDN(dnName);
    certGen.setNotBefore(new java.sql.Time(System.currentTimeMillis()));
    Calendar expiry = Calendar.getInstance();
    expiry.add(Calendar.DAY_OF_YEAR, validityDays);
    certGen.setNotAfter(expiry.getTime());
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
    X509Certificate cert = certGen.generate(keyPair.getPrivate(), "BC");
    return cert;
}
Also used : KeyPair(java.security.KeyPair) X509V1CertificateGenerator(org.bouncycastle.x509.X509V1CertificateGenerator) Calendar(java.util.Calendar) SecureRandom(java.security.SecureRandom) X500Principal(javax.security.auth.x500.X500Principal) KeyPairGenerator(java.security.KeyPairGenerator) X509Certificate(java.security.cert.X509Certificate) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 93 with SecureRandom

use of java.security.SecureRandom in project OpenAttestation by OpenAttestation.

the class CitrixClient method generateSessionId.

private String generateSessionId() throws NoSuchAlgorithmException {
    // Create a secure random number generator
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    // Get 1024 random bits
    byte[] seed = new byte[1];
    sr.nextBytes(seed);
    sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed);
    int nextInt = sr.nextInt();
    String sessionId = "" + ((nextInt < 0) ? nextInt * -1 : nextInt);
    log.info("Session Id Generated [" + sessionId + "]");
    return sessionId;
}
Also used : SecureRandom(java.security.SecureRandom)

Example 94 with SecureRandom

use of java.security.SecureRandom in project OpenAttestation by OpenAttestation.

the class CitrixClient method generateNonce.

public String generateNonce() {
    try {
        // Create a secure random number generator
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        // Get 1024 random bits
        byte[] bytes = new byte[16];
        sr.nextBytes(bytes);
        //            nonce = new BASE64Encoder().encode( bytes);
        String nonce = Base64.encodeBase64String(bytes);
        log.info("Nonce Generated " + nonce);
        return nonce;
    } catch (NoSuchAlgorithmException e) {
        throw new ASException(e);
    }
}
Also used : SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ASException(com.intel.mountwilson.as.common.ASException)

Example 95 with SecureRandom

use of java.security.SecureRandom in project languagetool by languagetool-org.

the class HTTPTools method disableCertChecks.

/**
   * For testing, we disable all checks because we use a self-signed certificate on the server
   * side and we want this test to run everywhere without importing the certificate into the JVM's trust store.
   *
   * See http://stackoverflow.com/questions/2893819/telling-java-to-accept-self-signed-ssl-certificate
   */
static void disableCertChecks() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = { new X509TrustManager() {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Aggregations

SecureRandom (java.security.SecureRandom)720 SSLContext (javax.net.ssl.SSLContext)106 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)97 IOException (java.io.IOException)87 Test (org.junit.Test)76 SecretKey (javax.crypto.SecretKey)62 X509Certificate (java.security.cert.X509Certificate)61 KeyGenerator (javax.crypto.KeyGenerator)57 TrustManager (javax.net.ssl.TrustManager)56 X509TrustManager (javax.net.ssl.X509TrustManager)47 Cipher (javax.crypto.Cipher)46 KeyPairGenerator (java.security.KeyPairGenerator)44 BigInteger (java.math.BigInteger)42 CertificateException (java.security.cert.CertificateException)40 InvalidKeyException (java.security.InvalidKeyException)35 KeyPair (java.security.KeyPair)34 KeyStore (java.security.KeyStore)34 SecretKeySpec (javax.crypto.spec.SecretKeySpec)30 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)28 KeyManagementException (java.security.KeyManagementException)28