DSSUtils { public static CertificateToken loadCertificateFromBase64EncodedString(final String base64Encoded) { final byte[] bytes = Utils.fromBase64(base64Encoded); return loadCertificate(bytes); } private  DSSUtils(); static String formatInternal(final Date date); static String formatDateWithCustomFormat(final Date date, final String format); static String toHex(final byte[] value); static String convertToPEM(final CertificateToken cert); static boolean isStartWithASN1SequenceTag(InputStream is); static byte[] convertToDER(String pemContent); static CertificateToken loadCertificate(final File file); static CertificateToken loadCertificate(final InputStream inputStream); static Collection<CertificateToken> loadCertificateFromP7c(InputStream is); static CertificateToken loadCertificate(final byte[] input); static CertificateToken loadCertificateFromBase64EncodedString(final String base64Encoded); static Collection<CertificateToken> loadPotentialIssuerCertificates(final CertificateToken cert, final DataLoader loader); static String getSHA1Digest(final String stringToDigest); static byte[] digest(final DigestAlgorithm digestAlgorithm, final byte[] data); static MessageDigest getMessageDigest(DigestAlgorithm digestAlgorithm); static byte[] encodeRSADigest(final DigestAlgorithm digestAlgorithm, final byte[] digest); static byte[] digest(final DigestAlgorithm digestAlgo, final InputStream inputStream); static byte[] digest(DigestAlgorithm digestAlgorithm, DSSDocument document); static byte[] digest(DigestAlgorithm digestAlgorithm, byte[]... data); static InputStream toInputStream(final File file); static InputStream toByteArrayInputStream(final File file); static byte[] toByteArray(final File file); static DSSDocument splitDocument(DSSDocument origin, int start, int end); static byte[] toByteArray(final DSSDocument document); static byte[] toByteArray(final InputStream inputStream); static CMSSignedData toCMSSignedData(final DSSDocument document); static boolean isTimestampToken(final DSSDocument document); static long getFileByteSize(DSSDocument dssDocument); static void saveToFile(final byte[] bytes, final File file); static String getNormalizedString(final String str); static String getDeterministicId(final Date signingTime, TokenIdentifier id); static String getMD5Digest(byte[] bytes); static X500Principal getX500PrincipalOrNull(final String x500PrincipalString); static Date getUtcDate(final int year, final int month, final int day); static void printSecurityProviders(); static byte readFirstByte(final DSSDocument dssDocument); static boolean compareFirstBytes(final DSSDocument dssDocument, byte[] byteArray); static byte[] concatenate(byte[]... arrays); static String decodeUrl(String uri); static long skipAvailableBytes(InputStream is, long n); static long readAvailableBytes(DSSDocument dssDocument, byte[] b); static long readAvailableBytes(InputStream is, byte[] b); static long readAvailableBytes(InputStream is, byte[] b, int off, int len); static String encodeURI(String fileURI); static String getExceptionMessage(Exception e); static Digest getDigest(DigestAlgorithm digestAlgo, DSSDocument dssDocument); static String removeControlCharacters(String str); static final byte[] EMPTY_BYTE_ARRAY; }
@Test public void loadRootCA2NotSelfSign() throws Exception { String certBase64 = "MIIDjjCCAnagAwIBAgIIKv++n6Lw6YcwDQYJKoZIhvcNAQEFBQAwKDELMAkGA1UEBhMCQkUxGTAXBgNVBAMTEEJlbGdpdW0gUm9vdCBDQTIwHhcNMDcxMDA0MTAwMDAwWhcNMjExMjE1MDgwMDAwWjAoMQswCQYDVQQGEwJCRTEZMBcGA1UEAxMQQmVsZ2l1bSBSb290IENBMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMZzQh6S/3UPi790hqc/7bIYLS2X+an7mEoj39WN4IzGMhwWLQdC1i22bi+n9fzGhYJdld61IgDMqFNAn68KNaJ6x+HK92AQZw6nUHMXU5WfIp8MXW+2QbyM69odRr2nlL/zGsvU+40OHjPIltfsjFPekx40HopQcSZYtF3CiInaYNKJIT/e1wEYNm7hLHADBGXvmAYrXR5i3FVr/mZkIV/4L+HXmymvb82fqgxG0YjFnaKVn6w/Fa7yYd/vw2uaItgscf1YHewApDgglVrH1Tdjuk+bqv5WRi5j2Qsj1Yr6tSPwiRuhFA0m2kHwOI8w7QUmecFLTqG4flVSOmlGhHUCAwEAAaOBuzCBuDAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/BAUwAwEB/zBCBgNVHSAEOzA5MDcGBWA4CQEBMC4wLAYIKwYBBQUHAgEWIGh0dHA6Ly9yZXBvc2l0b3J5LmVpZC5iZWxnaXVtLmJlMB0GA1UdDgQWBBSFiuv0xbu+DlkDlN7WgAEV4xCcOTARBglghkgBhvhCAQEEBAMCAAcwHwYDVR0jBBgwFoAUhYrr9MW7vg5ZA5Te1oABFeMQnDkwDQYJKoZIhvcNAQEFBQADggEBAFHYhd27V2/MoGy1oyCcUwnzSgEMdL8rs5qauhjyC4isHLMzr87lEwEnkoRYmhC598wUkmt0FoqW6FHvv/pKJaeJtmMrXZRY0c8RcrYeuTlBFk0pvDVTC9rejg7NqZV3JcqUWumyaa7YwBO+mPyWnIR/VRPmPIfjvCCkpDZoa01gZhz5v6yAlGYuuUGK02XThIAC71AdXkbc98m6tTR8KvPG2F9fVJ3bTc0R5/0UAoNmXsimABKgX77OFP67H6dh96tK8QYUn8pJQsKpvO2FsauBQeYNxUJpU4c5nUwfAA4+Bw11V0SoU7Q2dmSZ3G7rPUZuFF1eR1ONeE3gJ7uOhXY="; CertificateToken rootCA2 = DSSUtils.loadCertificateFromBase64EncodedString(certBase64); logger.info(rootCA2.toString()); logger.info(rootCA2.getCertificate().toString()); assertTrue(rootCA2.isCA()); X509Certificate certificate = rootCA2.getCertificate(); certificate.verify(certificate.getPublicKey()); }
@Test public void loadEdDSACert() throws NoSuchAlgorithmException, IOException { Security.addProvider(DSSSecurityProvider.getSecurityProvider()); CertificateToken token = DSSUtils.loadCertificateFromBase64EncodedString( "MIIBLDCB36ADAgECAghWAUdKKo3DMDAFBgMrZXAwGTEXMBUGA1UEAwwOSUVURiBUZXN0IERlbW8wHhcNMTYwODAxMTIxOTI0WhcNNDAxMjMxMjM1OTU5WjAZMRcwFQYDVQQDDA5JRVRGIFRlc3QgRGVtbzAqMAUGAytlbgMhAIUg8AmJMKdUdIt93LQ+91oNvzoNJjga9OukqY6qm05qo0UwQzAPBgNVHRMBAf8EBTADAQEAMA4GA1UdDwEBAAQEAwIDCDAgBgNVHQ4BAQAEFgQUmx9e7e0EM4Xk97xiPFl1uQvIuzswBQYDK2VwA0EAryMB/t3J5v/BzKc9dNZIpDmAgs3babFOTQbs+BolzlDUwsPrdGxO3YNGhW7Ibz3OGhhlxXrCe1Cgw1AH9efZBw=="); assertNotNull(token); logger.info("{}", token); logger.info("{}", token.getPublicKey()); assertFalse(token.isSelfSigned()); assertFalse(token.isSignedBy(token)); assertEquals(SignatureAlgorithm.ED25519, token.getSignatureAlgorithm()); assertTrue(token.checkKeyUsage(KeyUsageBit.KEY_AGREEMENT)); assertEquals(EncryptionAlgorithm.X25519, EncryptionAlgorithm.forKey(token.getPublicKey())); X509CertificateHolder holder = new X509CertificateHolder(token.getEncoded()); SubjectPublicKeyInfo subjectPublicKeyInfo = holder.getSubjectPublicKeyInfo(); assertNotNull(subjectPublicKeyInfo); assertEquals(EncryptionAlgorithm.X25519.getOid(), subjectPublicKeyInfo.getAlgorithm().getAlgorithm().getId()); token = DSSUtils .loadCertificateFromBase64EncodedString( "MIIBCDCBuwIUGW78zw0OL0GptJi++a91dBa7DsQwBQYDK2VwMCcxCzAJBgNVBAYTAkRFMRgwFgYDVQQDDA93d3cuZXhhbXBsZS5jb20wHhcNMTkwMzMxMTc1MTIyWhcNMjEwMjI4MTc1MTIyWjAnMQswCQYDVQQGEwJERTEYMBYGA1UEAwwPd3d3LmV4YW1wbGUuY29tMCowBQYDK2VwAyEAK87g0b8CC1eA5mvKXt9uezZwJYWEyg74Y0xTZEkqCcwwBQYDK2VwA0EAIIu/aa3Qtr3IE5to/nvWVY9y3ciwG5DnA70X3ALUhFs+U5aLtfY8sNT1Ng72ht+UBwByuze20UsL9qMsmknQCA=="); assertNotNull(token); logger.info("{}", token); logger.info("{}", token.getPublicKey()); assertEquals(SignatureAlgorithm.ED25519, token.getSignatureAlgorithm()); assertEquals(EncryptionAlgorithm.ED25519, EncryptionAlgorithm.forKey(token.getPublicKey())); assertTrue(token.isSelfSigned()); assertTrue(token.isSignedBy(token)); }