/extern/spongycastle/pkix/src/main/java/org/spongycastle/tsp/cms/CMSTimeStampedDataGenerator.java

https://gitlab.com/vizilo/fdroidclient · Java · 70 lines · 57 code · 13 blank · 0 comment · 6 complexity · daf673e7f023751f1c962b4b8e60e754 MD5 · raw file

  1. package org.spongycastle.tsp.cms;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import org.spongycastle.asn1.ASN1OctetString;
  7. import org.spongycastle.asn1.BEROctetString;
  8. import org.spongycastle.asn1.DERIA5String;
  9. import org.spongycastle.asn1.cms.CMSObjectIdentifiers;
  10. import org.spongycastle.asn1.cms.ContentInfo;
  11. import org.spongycastle.asn1.cms.Evidence;
  12. import org.spongycastle.asn1.cms.TimeStampAndCRL;
  13. import org.spongycastle.asn1.cms.TimeStampTokenEvidence;
  14. import org.spongycastle.asn1.cms.TimeStampedData;
  15. import org.spongycastle.cms.CMSException;
  16. import org.spongycastle.tsp.TimeStampToken;
  17. import org.spongycastle.util.io.Streams;
  18. public class CMSTimeStampedDataGenerator
  19. extends CMSTimeStampedGenerator
  20. {
  21. public CMSTimeStampedData generate(TimeStampToken timeStamp) throws CMSException
  22. {
  23. return generate(timeStamp, (InputStream)null);
  24. }
  25. public CMSTimeStampedData generate(TimeStampToken timeStamp, byte[] content) throws CMSException
  26. {
  27. return generate(timeStamp, new ByteArrayInputStream(content));
  28. }
  29. public CMSTimeStampedData generate(TimeStampToken timeStamp, InputStream content)
  30. throws CMSException
  31. {
  32. ByteArrayOutputStream contentOut = new ByteArrayOutputStream();
  33. if (content != null)
  34. {
  35. try
  36. {
  37. Streams.pipeAll(content, contentOut);
  38. }
  39. catch (IOException e)
  40. {
  41. throw new CMSException("exception encapsulating content: " + e.getMessage(), e);
  42. }
  43. }
  44. ASN1OctetString encContent = null;
  45. if (contentOut.size() != 0)
  46. {
  47. encContent = new BEROctetString(contentOut.toByteArray());
  48. }
  49. TimeStampAndCRL stamp = new TimeStampAndCRL(timeStamp.toCMSSignedData().toASN1Structure());
  50. DERIA5String asn1DataUri = null;
  51. if (dataUri != null)
  52. {
  53. asn1DataUri = new DERIA5String(dataUri.toString());
  54. }
  55. return new CMSTimeStampedData(new ContentInfo(CMSObjectIdentifiers.timestampedData, new TimeStampedData(asn1DataUri, metaData, encContent, new Evidence(new TimeStampTokenEvidence(stamp)))));
  56. }
  57. }