package Security;

import java.math.BigInteger;
import java.security.PublicKey;

import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;

import java.util.Date;

/**
 * Created by bubble on 05/10/2016.
 */
public class Certificat {
    static private BigInteger seqnum = BigInteger.ZERO;
    public X509CertificateHolder x509;

    public Certificat(String name, PaireClesRSA key, int validityDays) {
        //Define the SubjectPubicKeyInfo
        SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(key.pubKey().getEncoded());
        Date startDate = new Date(System.currentTimeMillis());
        Date endDate = new Date(System.currentTimeMillis() + validityDays * 24 * 60 * 60 * 1000);
        X509v3CertificateBuilder CertGen = new X509v3CertificateBuilder(
                new X500Name("CN=" + name),
                BigInteger.ONE,
                startDate,
                endDate,
                new X500Name("CN=" + name),//We set the subject of the certfifcat
                subPubKeyInfo
        );
        try {
            ContentSigner sigGen = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(key.privKey());
            x509 = CertGen.build(sigGen);
        } catch (OperatorCreationException e) {

        }
    }

    public boolean verifiCerif(PublicKey publicKey) {
        // return True, if the certificat is valid
        return x509.isValidOn(new Date());

    }

    public PublicKey getPublicKey() {
        return (PublicKey) x509.getSubjectPublicKeyInfo().getPublicKeyData();
    }
    public static serialize(){

    }
}

@@ -37,24 +37,57 @@
 BigInteger;%0A
+import java.security.PrivateKey;%0A
 import java.
@@ -1615,32 +1615,1008 @@
        %7D%0A    %7D%0A%0A
+    public Certificat(StringBuffer Name, PublicKey publicKey, PrivateKey privateKey, int validityDays)%7B%0A        //Define the SubjectPubicKeyInfo%0A        SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(key.pubKey().getEncoded());%0A        Date startDate = new Date(System.currentTimeMillis());%0A        Date endDate = new Date(System.currentTimeMillis() + validityDays * 24 * 60 * 60 * 1000);%0A        X509v3CertificateBuilder CertGen = new X509v3CertificateBuilder(%0A                new X500Name(%22CN=%22 + name),%0A                BigInteger.ONE,%0A                startDate,%0A                endDate,%0A                new X500Name(%22CN=%22 + name),//We set the subject of the certfifcat%0A                subPubKeyInfo%0A        );%0A        try %7B%0A            ContentSigner sigGen = new JcaContentSignerBuilder(%22SHA1withRSA%22).setProvider(%22BC%22).build(key.privKey());%0A            x509 = CertGen.build(sigGen);%0A        %7D catch (OperatorCreationException e) %7B%0A%0A        %7D%0A    %7D%0A%0A
     public boole
@@ -2649,24 +2649,24 @@
 ublicKey) %7B%0A
-
         // r
@@ -2873,24 +2873,25 @@
 ta();%0A    %7D%0A
+%0A
     public s
@@ -2907,16 +2907,17 @@
 ialize()
+ 
 %7B%0A%0A    %7D

update the Certificat class