/** * Generate version 1 self signed {@link java.security.cert.X509Certificate}.. * * @param caKeyPair the CA key pair * @param subject the subject name * * @return the x509 certificate * * @throws Exception the exception */ public static X509Certificate generateV1SelfSignedCertificate(KeyPair caKeyPair, String subject) { return generateV1SelfSignedCertificate(caKeyPair, subject, BigInteger.valueOf(System.currentTimeMillis())); }
public static X509Certificate generateV1SelfSignedCertificate(KeyPair caKeyPair, String subject, BigInteger serialNumber) { try { X500Name subjectDN = new X500Name("CN=" + subject); Date validityStartDate = new Date(System.currentTimeMillis() - 100000); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.YEAR, 10); Date validityEndDate = new Date(calendar.getTime().getTime()); SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(caKeyPair.getPublic().getEncoded()); X509v1CertificateBuilder builder = new X509v1CertificateBuilder(subjectDN, serialNumber, validityStartDate, validityEndDate, subjectDN, subPubKeyInfo); X509CertificateHolder holder = builder.build(createSigner(caKeyPair.getPrivate())); return new JcaX509CertificateConverter().getCertificate(holder); } catch (Exception e) { throw new RuntimeException("Error creating X509v1Certificate.", e); } }
public static void generateRealmCertificate(RealmModel realm) { X509Certificate certificate = null; try { certificate = CertificateUtils.generateV1SelfSignedCertificate(new KeyPair(realm.getPublicKey(), realm.getPrivateKey()), realm.getName()); } catch (Exception e) { throw new RuntimeException(e); } realm.setCertificate(certificate); }
public static CertificateRepresentation generateKeyPairCertificate(String subject) { KeyPair keyPair = null; try { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); keyPair = generator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } X509Certificate certificate = null; try { certificate = CertificateUtils.generateV1SelfSignedCertificate(keyPair, subject); } catch (Exception e) { throw new RuntimeException(e); } String privateKeyPem = KeycloakModelUtils.getPemFromKey(keyPair.getPrivate()); String certPem = KeycloakModelUtils.getPemFromCertificate(certificate); CertificateRepresentation rep = new CertificateRepresentation(); rep.setPrivateKey(privateKeyPem); rep.setCertificate(certPem); return rep; }
public static void generateRealmKeys(RealmModel realm) { KeyPair keyPair = null; try { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); keyPair = generator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } realm.setPrivateKey(keyPair.getPrivate()); realm.setPublicKey(keyPair.getPublic()); X509Certificate certificate = null; try { certificate = CertificateUtils.generateV1SelfSignedCertificate(keyPair, realm.getName()); } catch (Exception e) { throw new RuntimeException(e); } realm.setCertificate(certificate); realm.setCodeSecret(generateCodeSecret()); }
certificate = CertificateUtils.generateV1SelfSignedCertificate(keyPair, bindAddress); } catch (Exception e) { throw new RuntimeException(e);