package org.cryptonit.cloud.timestamping;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;

/**
 * @author Mathias Brossard
 */
public class Authority {
    KeyPair generateKeypair(int size) {
        SecureRandom rand = new SecureRandom();
        KeyPairGenerator pairGenerator = null;
        try {
            pairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
            pairGenerator.initialize(size, rand);
            KeyPair kp = pairGenerator.generateKeyPair();
        return kp;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace(System.err);
        } catch (NoSuchProviderException e) {
            e.printStackTrace(System.err);
        }
        return null;
    }    
}

@@ -36,16 +36,45 @@
 mping;%0A%0A
+import java.math.BigInteger;%0A
 import j
@@ -248,28 +248,516 @@
 ecurity.
-SecureRandom
+PrivateKey;%0Aimport java.security.PublicKey;%0Aimport java.security.SecureRandom;%0Aimport java.security.cert.X509Certificate;%0Aimport java.util.Date;%0Aimport org.bouncycastle.asn1.x509.BasicConstraints;%0Aimport org.bouncycastle.asn1.x509.ExtendedKeyUsage;%0Aimport org.bouncycastle.asn1.x509.Extension;%0Aimport org.bouncycastle.asn1.x509.KeyPurposeId;%0Aimport org.bouncycastle.asn1.x509.X509Name;%0Aimport org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils;%0Aimport org.bouncycastle.x509.X509V3CertificateGenerator
 ;%0A%0A/**%0A 
@@ -1374,15 +1374,2126 @@
 l;%0A    %7D
+%0A%0A
     
+public static X509Certificate makeCertificate(KeyPair subjectKey, X509Name subjectDN,%0A                                                  KeyPair issuerKey, X509Name issuerDN,%0A                                                  BigInteger serial, boolean isCA)%0A    %7B%0A        JcaX509ExtensionUtils extUtils = null;%0A        try %7B%0A            extUtils= new JcaX509ExtensionUtils();%0A        %7D catch (NoSuchAlgorithmException e) %7B%0A            e.printStackTrace(System.err);%0A        %7D%0A%0A        PublicKey subjectPublicKey = subjectKey.getPublic();%0A        PrivateKey issuerPrivateKey = issuerKey.getPrivate();%0A        PublicKey issuerPublicKey = issuerKey.getPublic();%0A%0A        X509V3CertificateGenerator crtGen = new X509V3CertificateGenerator();%0A%0A        crtGen.reset();%0A        crtGen.setSerialNumber(serial);%0A        crtGen.setIssuerDN(issuerDN);%0A        crtGen.setNotBefore(new Date(System.currentTimeMillis()));%0A        crtGen.setNotAfter(new Date(System.currentTimeMillis()%0A                + (1000 * 86400 * 365)));%0A        crtGen.setSubjectDN(subjectDN);%0A        crtGen.setPublicKey(subjectPublicKey);%0A        crtGen.setSignatureAlgorithm(%22Sha256WithRSAEncryption%22);%0A%0A        if(extUtils != null) %7B%0A            crtGen.addExtension(Extension.subjectKeyIdentifier, false,%0A                    extUtils.createSubjectKeyIdentifier(subjectPublicKey));%0A%0A            crtGen.addExtension(Extension.authorityKeyIdentifier, false,%0A                    extUtils.createAuthorityKeyIdentifier(issuerPublicKey));%0A        %7D%0A%0A        if (isCA) %7B%0A            crtGen.addExtension(Extension.basicConstraints, false,%0A                    new BasicConstraints(isCA));%0A        %7D else %7B%0A            crtGen.addExtension(Extension.extendedKeyUsage, true,%0A                    new ExtendedKeyUsage(KeyPurposeId.id_kp_timeStamping));%0A        %7D%0A%0A        X509Certificate crt = null;%0A        try %7B%0A            crt = crtGen.generate(issuerPrivateKey);%0A            crt.checkValidity(new Date());%0A            crt.verify(issuerPublicKey);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace(System.err);%0A        %7D%0A%0A        return crt;%0A    %7D
 %0A%7D%0A

Test certificate generation