Advertisement
chirihop

TSA2

Mar 7th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. /*
  2. * This class is part of the white paper entitled
  3. * "Digital Signatures for PDF documents"
  4. * written by Bruno Lowagie
  5. *
  6. * For more info, go to: http://itextpdf.com/learn
  7. */
  8. package signatures.chapter3;
  9.  
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12. import java.security.GeneralSecurityException;
  13. import java.security.KeyStore;
  14. import java.security.PrivateKey;
  15. import java.security.Security;
  16. import java.security.cert.Certificate;
  17. import java.util.Properties;
  18.  
  19. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  20.  
  21. import com.itextpdf.text.DocumentException;
  22. import com.itextpdf.text.pdf.security.DigestAlgorithms;
  23. import com.itextpdf.text.pdf.security.MakeSignature.CryptoStandard;
  24. import com.itextpdf.text.pdf.security.OcspClient;
  25. import com.itextpdf.text.pdf.security.OcspClientBouncyCastle;
  26. import com.itextpdf.text.pdf.security.TSAClient;
  27. import com.itextpdf.text.pdf.security.TSAClientBouncyCastle;
  28.  
  29. public class C3_09_SignWithTSA extends C3_01_SignWithCAcert {
  30. public static final String SRC = "src/main/resources/hello.pdf";
  31. public static final String DEST = "results/chapter3/hello_cacert_ocsp_ts.pdf";
  32.  
  33. public static void main(String[] args) throws IOException, GeneralSecurityException, DocumentException {
  34. Properties properties = new Properties();
  35. //properties.load(new FileInputStream("c:/home/blowagie/key.properties"));
  36. properties.setProperty("PRIVATE", "src/main/resources/new/identity.p12");
  37. properties.setProperty("PASSWORD", "password");
  38. properties.setProperty("TSAURL", "http://localhost:3000");
  39. //properties.setProperty("TSAURL", "https://freetsa.org/tsr");
  40. //properties.setProperty("TSAUSERNAME", "tsausername");
  41. //properties.setProperty("TSAPASSWORD", "tsapassword");
  42.  
  43. String path = properties.getProperty("PRIVATE");
  44. char[] pass = properties.getProperty("PASSWORD").toCharArray();
  45. String tsaUrl = properties.getProperty("TSAURL");
  46. String tsaUser = properties.getProperty("TSAUSERNAME");
  47. String tsaPass = properties.getProperty("TSAPASSWORD");
  48.  
  49. BouncyCastleProvider provider = new BouncyCastleProvider();
  50. Security.addProvider(provider);
  51. KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName());
  52. ks.load(new FileInputStream(path), pass);
  53. String alias = (String)ks.aliases().nextElement();
  54. PrivateKey pk = (PrivateKey) ks.getKey(alias, pass);
  55. Certificate[] chain = ks.getCertificateChain(alias);
  56. OcspClient ocspClient = new OcspClientBouncyCastle();
  57.  
  58. TSAClient tsaClient = new TSAClientBouncyCastle(tsaUrl); //, tsaUser, tsaPass)
  59. System.out.println(tsaClient.toString());
  60. //System.out.println(tsaClient.getMessageDigest());
  61. C3_09_SignWithTSA app = new C3_09_SignWithTSA();
  62. app.sign(SRC, DEST, chain, pk, DigestAlgorithms.SHA256, provider.getName(), CryptoStandard.CMS, "Test", "Madrid",
  63. null, ocspClient, tsaClient, 0);
  64.  
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement