DSSASN1Utils { public static byte[] getDEREncoded(ASN1Encodable asn1Encodable) { return getEncoded(asn1Encodable, ASN1Encoding.DER); } private  DSSASN1Utils(); @SuppressWarnings("unchecked") static T toASN1Primitive(final byte[] bytes); static byte[] getDEREncoded(ASN1Encodable asn1Encodable); static byte[] getBEREncoded(ASN1Encodable asn1Encodable); static byte[] getEncoded(BasicOCSPResp basicOCSPResp); static Date toDate(final ASN1GeneralizedTime asn1Date); static String toString(final ASN1OctetString value); static byte[] getEncoded(final TimeStampToken timeStampToken); static byte[] getEncoded(final CMSSignedData cmsSignedData); static byte[] getDEREncoded(final TimeStampToken timeStampToken); static byte[] getDEREncoded(final CMSSignedData data); static byte[] getDEREncoded(final TimestampBinary timestampBinary); static byte[] getDEREncoded(final byte[] bytes); static ASN1Sequence getAsn1SequenceFromDerOctetString(byte[] bytes); static byte[] getAsn1SignaturePolicyDigest(DigestAlgorithm digestAlgorithm, byte[] policyBytes); static AlgorithmIdentifier getAlgorithmIdentifier(final ASN1Sequence atsHashIndexValue); static AlgorithmIdentifier getAlgorithmIdentifier(DigestAlgorithm digestAlgorithm); static ASN1Sequence getCertificatesHashIndex(final ASN1Sequence atsHashIndexValue); static ASN1Sequence getCRLHashIndex(final ASN1Sequence atsHashIndexValue); static ASN1Sequence getUnsignedAttributesHashIndex(final ASN1Sequence atsHashIndexValue); @SuppressWarnings("unchecked") static List<DEROctetString> getDEROctetStrings(final ASN1Sequence asn1Sequence); static boolean hasIdPkixOcspNoCheckExtension(CertificateToken token); static List<CertificatePolicy> getCertificatePolicies(final CertificateToken certToken); static List<String> getQCStatementsIdList(final CertificateToken certToken); static List<String> getQCTypesIdList(final CertificateToken certToken); static List<String> getQCLegislations(CertificateToken certToken); static byte[] getSki(final CertificateToken certificateToken); static byte[] getSki(final CertificateToken certificateToken, boolean computeIfMissing); static byte[] getAuthorityKeyIdentifier(CertificateToken certificateToken); static byte[] computeSkiFromCert(final CertificateToken certificateToken); static byte[] computeSkiFromCertPublicKey(final PublicKey publicKey); static boolean isSkiEqual(final byte[] ski, final CertificateToken certificateToken); static List<String> getCAAccessLocations(final CertificateToken certificate); static List<String> getOCSPAccessLocations(final CertificateToken certificate); static List<String> getCrlUrls(final CertificateToken certificateToken); static boolean isOCSPSigning(CertificateToken certToken); static boolean isExtendedKeyUsagePresent(CertificateToken certToken, ASN1ObjectIdentifier oid); static X509CertificateHolder getX509CertificateHolder(CertificateToken certToken); static CertificateToken getCertificate(final X509CertificateHolder x509CertificateHolder); static CertificateIdentifier toIssuerSerialInfo(SignerId signerId); static X500Principal toX500Principal(X500Name x500Name); static CertificateIdentifier toCertificateIdentifier(final X500Principal issuerX500Principal, final BigInteger serialNumber, final byte[] ski); static IssuerSerial getIssuerSerial(final CertificateToken certToken); static boolean x500PrincipalAreEquals(final X500Principal firstX500Principal, final X500Principal secondX500Principal); static Map<String, String> get(final X500Principal x500Principal); static X500Principal getNormalizedX500Principal(final X500Principal x500Principal); static String getUtf8String(final X500Principal x500Principal); static String getString(ASN1Encodable attributeValue); static String extractAttributeFromX500Principal(ASN1ObjectIdentifier identifier, X500PrincipalHelper principal); static String getSubjectCommonName(CertificateToken cert); static String getHumanReadableName(CertificateToken cert); static SignerInformation getFirstSignerInformation(final CMSSignedData cms); static boolean isASN1SequenceTag(byte tagByte); static Date getDate(ASN1Encodable encodable); static boolean isEmpty(AttributeTable attributeTable); static AttributeTable emptyIfNull(AttributeTable original); static List<String> getExtendedKeyUsage(CertificateToken certToken); static IssuerSerial getIssuerSerial(byte[] binaries); static CertificateIdentifier toCertificateIdentifier(IssuerSerial issuerAndSerial); static ASN1Sequence getAtsHashIndex(AttributeTable timestampUnsignedAttributes); static ASN1Sequence getAtsHashIndexByVersion(AttributeTable timestampUnsignedAttributes, 
			ASN1ObjectIdentifier atsHashIndexVersionIdentifier); static ASN1ObjectIdentifier getAtsHashIndexVersionIdentifier(AttributeTable timestampUnsignedAttributes); static List<byte[]> getOctetStringForAtsHashIndex(Attribute attribute, ASN1ObjectIdentifier atsHashIndexVersionIdentifier); static ASN1Encodable getAsn1Encodable(AttributeTable unsignedAttributes, ASN1ObjectIdentifier oid); static ASN1Set getAsn1AttributeSet(AttributeTable unsignedAttributes, ASN1ObjectIdentifier oid); static Attribute[] getAsn1Attributes(AttributeTable unsignedAttributes, ASN1ObjectIdentifier oid); static List<TimeStampToken> findArchiveTimeStampTokens(AttributeTable unsignedAttributes); static boolean isArchiveTimeStampToken(Attribute attribute); static boolean isAttributeOfType(Attribute attribute, ASN1ObjectIdentifier asn1ObjectIdentifier); static TimeStampToken getTimeStampToken(Attribute attribute); static CMSSignedData getCMSSignedData(Attribute attribute); static ASN1Encodable getAsn1Encodable(Attribute attribute); static Date getTimeStampTokenGenerationTime(TimeStampToken timeStampToken); static RevocationValues getRevocationValues(ASN1Encodable encodable); static CertificateRef getCertificateRef(OtherCertID otherCertId); static PSD2QcType getPSD2QcStatement(CertificateToken certToken); static List<String> getSubjectAlternativeNames(CertificateToken certToken); static SemanticsIdentifier getSemanticsIdentifier(CertificateToken certToken);  }
@Test public void getDEREncoded() throws IOException, CMSException, TSPException { String berEncodedTST = "MIAGCSqGSIb3DQEHAqCAMIIIEwIBAzEPMA0GCWCGSAFlAwQCAwUAMIHdBgsqhkiG9w0BCRABBKCBzQSByjCBxwIBAQYGBACPZwEBMDEwDQYJYIZIAWUDBAIBBQAEIEx1HyJIzqt0xr8QBSNv5cRNSOac6X22MCn43LTUSuGQAgh47MXImQeQxBgPMjAxOTAyMTgxNDEyMjlaMAMCAQGgZ6RlMGMxCzAJBgNVBAYTAkVFMSIwIAYDVQQKDBlBUyBTZXJ0aWZpdHNlZXJpbWlza2Vza3VzMQwwCgYDVQQLDANUU0ExIjAgBgNVBAMMGVNLIFRJTUVTVEFNUElORyBBVVRIT1JJVFmgggQRMIIEDTCCAvWgAwIBAgIQJK/s6xJo0AJUF/eG7W8BWTANBgkqhkiG9w0BAQsFADB1MQswCQYDVQQGEwJFRTEiMCAGA1UECgwZQVMgU2VydGlmaXR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UF4B4dIoXOj4p26q1yYULF5ZkZHxhQFNi5uxak9tgCFlGtzXumjL5jBmtWeDTGE4YSa34pzDXjz8VAjPJ9sVuOmK2E0gyWxUTLXF9YevrWzRLzVFqw+qewBV2I4of/6miZOOT2wlA/meL7zr3hnfo7KSJQmMNUjZ6lh6RBIVvYI0t+A/fpTKiZfviz/Xn2e4PC6i57wmH5EgOOav0UKMYIDBjCCAwICAQEwgYkwdTELMAkGA1UEBhMCRUUxIjAgBgNVBAoMGUFTIFNlcnRpZml0c2VlcmltaXNrZXNrdXMxKDAmBgNVBAMMH0VFIENlcnRpZmljYXRpb24gQ2VudHJlIFJvb3QgQ0ExGDAWBgkqhkiG9w0BCQEWCXBraUBzay5lZQIQJK/s6xJo0AJUF/eG7W8BWTANBglghkgBZQMEAgMFAKCCAU0wGgYJKoZIhvcNAQkDMQ0GCyqGSIb3DQEJEAEEMBwGCSqGSIb3DQEJBTEPFw0xOTAyMTgxNDEyMjlaME8GCSqGSIb3DQEJBDFCBEAQowCFbttXzzmOv1nPKZ5V5Ju/vVB8fXGBlGofbvyAFZ0XMpuLOQVvtjCnrQ8VPtraSf87xHAk+DmAQhRCsO/rMIG/BgsqhkiG9w0BCRACDDGBrzCBrDCBqTCBpgQUstAhgvC5biocaH7OMjQII5gZMLYwgY0weaR3MHUxCzAJBgNVBAYTAkVFMSIwIAYDVQQKDBlBUyBTZXJ0aWZpdHNlZXJpbWlza2Vza3VzMSgwJgYDVQQDDB9FRSBDZXJ0aWZpY2F0aW9uIENlbnRyZSBSb290IENBMRgwFgYJKoZIhvcNAQkBFglwa2lAc2suZWUCECSv7OsSaNACVBf3hu1vAVkwDQYJKoZIhvcNAQEBBQAEggEAZIeCPyWt1WsuHwUJjL byte[] originalBinaries = Utils.fromBase64(berEncodedTST); try (ByteArrayInputStream bais = new ByteArrayInputStream(originalBinaries)) { CMSSignedData cms = new CMSSignedData(bais); TimeStampToken tst = new TimeStampToken(cms); byte[] defaultEncoded = tst.getEncoded(ASN1Encoding.BER); String defaultEncodedBase64 = Utils.toBase64(defaultEncoded); assertEquals(berEncodedTST, defaultEncodedBase64); ASN1InputStream asn1IS = new ASN1InputStream(defaultEncoded); ASN1Primitive firstObject = asn1IS.readObject(); byte[] expectedBinaries = firstObject.getEncoded(ASN1Encoding.DER); byte[] berBinaries = firstObject.getEncoded(ASN1Encoding.BER); asn1IS.close(); assertArrayEquals(originalBinaries, berBinaries); byte[] derEncoded = DSSASN1Utils.getDEREncoded(tst); assertNotNull(derEncoded); String derEncodedBase64 = Utils.toBase64(derEncoded); assertNotEquals(derEncodedBase64, defaultEncodedBase64); assertArrayEquals(expectedBinaries, derEncoded); asn1IS = new ASN1InputStream(derEncoded); DERSequence derSequence = new DERSequence(asn1IS.readObject()); assertNotNull(derSequence); asn1IS.close(); TimeStampToken rebuiltTST = new TimeStampToken(new CMSSignedData(derEncoded)); assertNotNull(rebuiltTST); } }