DSSUtils { public static CertificateToken loadCertificate(final File file) { final InputStream inputStream = DSSUtils.toByteArrayInputStream(file); return loadCertificate(inputStream); } 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 testDontSkipCertificatesWhenMultipleAreFoundInP7c() throws IOException { try (FileInputStream fis = new FileInputStream("src/test/resources/certchain.p7c")) { DSSException exception = assertThrows(DSSException.class, () -> DSSUtils.loadCertificate(fis)); assertEquals("Could not parse certificate", exception.getMessage()); } }
@Test public void loadCertificate() throws Exception { CertificateToken certificate = DSSUtils.loadCertificate(new FileInputStream("src/test/resources/belgiumrs2.crt")); assertNotNull(certificate); FileInputStream fis = new FileInputStream("src/test/resources/belgiumrs2.crt"); byte[] byteArray = Utils.toByteArray(fis); logger.info(Utils.toBase64(byteArray)); Utils.closeQuietly(fis); CertificateToken certificate2 = DSSUtils.loadCertificate(byteArray); assertNotNull(certificate2); CertificateToken certificateNew = DSSUtils.loadCertificate(new FileInputStream("src/test/resources/belgiumrs2-new.crt")); assertNotNull(certificateNew); FileInputStream fisNew = new FileInputStream("src/test/resources/belgiumrs2-new.crt"); byte[] byteArrayNew = Utils.toByteArray(fisNew); logger.info(Utils.toBase64(byteArrayNew)); Utils.closeQuietly(fisNew); CertificateToken certificate2New = DSSUtils.loadCertificate(byteArrayNew); assertNotNull(certificate2New); }
@Test public void loadCertificateDoesNotThrowNullPointerExceptionWhenProvidedNonCertificateFile() throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream("test".getBytes("UTF-8"))) { assertThrows(DSSException.class, () -> DSSUtils.loadCertificate(bais)); } }
@Test public void testRootCA2s() { CertificateToken selfSign = DSSUtils.loadCertificate(new File("src/test/resources/belgiumrca2-self-sign.crt")); CertificateToken signed = DSSUtils.loadCertificate(new File("src/test/resources/belgiumrs2-signed.crt")); CertificateToken tsa = DSSUtils.loadCertificate(new File("src/test/resources/TSA_BE.cer")); logger.info(selfSign.toString()); logger.info(signed.toString()); logger.info(tsa.toString()); logger.info(tsa.getCertificate().toString()); assertTrue(selfSign.isSelfSigned()); assertFalse(signed.isSelfSigned()); assertFalse(tsa.isCA()); assertTrue(tsa.isSignedBy(signed)); assertTrue(tsa.isSignedBy(selfSign)); }
@Test public void testRSASSAPSS() { CertificateToken token = DSSUtils.loadCertificate(this.getClass().getResourceAsStream("/BA-QC-Wurzel-CA-2_PN.txt")); assertTrue(token.isSelfSigned()); assertTrue(token.isSignedBy(token)); assertEquals(SignatureAlgorithm.RSA_SSA_PSS_SHA256_MGF1, token.getSignatureAlgorithm()); }