package org.cryptonit.cloud.timestamping;

import java.math.BigInteger;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.tsp.*;
import org.bouncycastle.util.Store;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Mathias Brossard
 */
public class Authority {
    public static final Set ALLOWED = new HashSet(Arrays.asList(new ASN1ObjectIdentifier[] {
        TSPAlgorithms.SHA1, TSPAlgorithms.SHA256, TSPAlgorithms.SHA384, TSPAlgorithms.SHA512
    }));
    private final BigInteger one = new BigInteger("1");
    private static Logger LOGGER;
    ASN1ObjectIdentifier policy;
    X509Certificate crt;
    KeyPair key;
    Store certs;
    BigInteger serial;

    synchronized private BigInteger getNextSerial() {
        BigInteger s = serial;
        serial.add(one);
        return s;
    }

    public TimeStampResponse timestamp(TimeStampRequest request) {
        TimeStampResponse response = null;
        return response;
    }

    public Authority(KeyPair key, X509Certificate crt, Store certs, ASN1ObjectIdentifier policy) {
        LOGGER = LoggerFactory.getLogger(Authority.class);
        
        this.key = key;
        this.crt = crt;
        this.certs = certs;
        this.policy = policy;
        this.serial = new BigInteger("1");
    }
}

@@ -276,24 +276,202 @@
 Identifier;%0A
+import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;%0Aimport org.bouncycastle.cms.SignerInfoGenerator;%0Aimport org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoGeneratorBuilder;%0A
 import org.b
@@ -937,182 +937,405 @@
     
-ASN1ObjectIdentifier policy;%0A    X509Certificate crt;%0A    KeyPair key;%0A    Store certs;%0A    BigInteger serial;%0A%0A    synchronized private BigInteger getNextSerial() %7B%0A
+TimeStampResponseGenerator generator;%0A    BigInteger serial;%0A%0A    public Authority(KeyPair key, X509Certificate crt, Store certs, ASN1ObjectIdentifier policy) %7B%0A        LOGGER = LoggerFactory.getLogger(Authority.class);%0A%0A        TimeStampTokenGenerator tokenGen = null;%0A        try %7B%0A            JcaSimpleSignerInfoGeneratorBuilder builder = new JcaSimpleSignerInfoGeneratorBuilder();%0A    
         
 BigI
@@ -1334,537 +1334,727 @@
     
-BigInteger s = serial;%0A        serial.add(one);%0A        return s;%0A    %7D%0A%0A    public TimeStampResponse timestamp(TimeStampRequest request) %7B%0A        TimeStampResponse response = null;%0A        return response;%0A    %7D%0A%0A    public Authority(KeyPair key, X509Certificate crt, Store certs, ASN1ObjectIdentifier policy) %7B%0A        LOGGER = LoggerFactory.getLogger(Authority.class);%0A        %0A        this.key = key;%0A        this.crt = crt
+SignerInfoGenerator signer = builder.build(%22SHA256withRSA%22, key.getPrivate(), crt);%0A            tokenGen = new TimeStampTokenGenerator(signer, new GenericDigestCalculator(NISTObjectIdentifiers.id_sha256), policy);%0A            tokenGen.addCertificates(certs);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        generator = new TimeStampResponseGenerator(tokenGen, ALLOWED);%0A        serial = new BigInteger(%221%22);%0A    %7D%0A%0A    synchronized private BigInteger getNextSerial() %7B%0A        BigInteger s = serial;%0A        serial.add(one);%0A        return s
 ;%0A    
+%7D%0A%0A
     
-this.certs = certs;%0A        this.policy = policy;%0A        this.serial = new BigInteger(%221%22)
+public TimeStampResponse timestamp(TimeStampRequest request) %7B%0A        TimeStampResponse response = null;%0A        return response
 ;%0A  

Build TimestampResponseGenerator object in constructor