private InMemoryDocument getDataFile(byte[] proxyDocument) { try (ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(proxyDocument))) { ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { if (!entry.getName().startsWith(META_INF_FOLDER) && !entry.getName().equalsIgnoreCase(MIME_TYPE_FILE_NAME)) { return new InMemoryDocument(zipStream, entry.getName()); } } } catch (IOException e) { throw new RuntimeException(e); } throw new IllegalArgumentException("Invalid document"); }
private boolean isAsicsMimeType(ZipEntry entry, ZipInputStream zipStream) { return entry.getName().equals(MIME_TYPE_FILE_NAME) && ASICS_MIME_TYPE.equals(new String(new InMemoryDocument(zipStream, entry.getName()).getBytes())); }
private TimeStampToken getTimeStamp(List<InMemoryDocument> documents) { try { CMSSignedData cms = new CMSSignedData(documents.stream() .filter(d -> d.getName().toUpperCase().endsWith(TIMESTAMP_FILE)).findAny().orElseThrow(IllegalArgumentException::new).getBytes()); return new TimeStampToken(cms); } catch (CMSException | TSPException | IOException e) { throw new MalformedDocumentException(e); } }
DSSDocument createDssDocument(final ValidationDocument validationDocument) { if (validationDocument == null) { return null; } final InMemoryDocument dssDocument = new InMemoryDocument(validationDocument.getBytes()); dssDocument.setName(validationDocument.getName()); dssDocument.setMimeType(MimeType.fromFileName(validationDocument.getName())); return dssDocument; }
private void validateContainer(List<InMemoryDocument> documents) { documents.removeIf(d -> d.getName().equals(META_INF_FOLDER)); long dataFileCount = documents.stream() .filter(d -> !d.getName().startsWith(META_INF_FOLDER)) .filter(d -> !d.getName().endsWith(MIME_TYPE)).count(); long timeStampCount = documents.stream() .filter(d -> d.getName().startsWith(META_INF_FOLDER)) .filter(d -> d.getName().toUpperCase().endsWith(TIMESTAMP_FILE)).count(); long signatureFileCount = documents.stream() .filter(d -> d.getName().startsWith(META_INF_FOLDER)) .filter(d -> getFileFromFullPath(d.getName().toUpperCase()).equals(SIGNATURE_FILE_EXTENSION_P7S) || getFileFromFullPath(d.getName().toUpperCase()).equals(SIGNATURE_FILE_EXTENSION_XML) || getFileFromFullPath(d.getName().toUpperCase()).equals(EVIDENCE_RECORD_FILE_EXTENSION_ERS) || getFileFromFullPath(d.getName().toUpperCase()).equals(EVIDENCE_RECORD_FILE_EXTENSION_XML)).count(); if (dataFileCount != 1 || timeStampCount != 1 || signatureFileCount > 0) { throw new DocumentRequirementsException(); } }
@Override protected DSSDocument createDssDocument(final ValidationDocument validationDocument) { if (validationDocument == null) { return null; } return new InMemoryDocument(validationDocument.getBytes()); }
private List<Error> validateTimeStamp(List<InMemoryDocument> documents, TimeStampToken timeStampToken) { List<Error> errors = new ArrayList<>(); boolean isSignatureValid = isSignatureValid(timeStampToken); if (!isSignatureValid) { errors.add(mapError("Signature not intact")); } byte[] dataFile = documents.stream() .filter(d -> !d.getName().startsWith(META_INF_FOLDER)) .filter(d -> !d.getName().endsWith(MIME_TYPE)).findAny().orElseThrow(IllegalArgumentException::new).getBytes(); boolean isMessageImprintsValid = isMessageImprintsValid(dataFile, timeStampToken); if (isSignatureValid && !isMessageImprintsValid) { errors.add(mapError("Signature not intact")); } boolean isVersionValid = isVersionValid(timeStampToken); if (!isVersionValid) { errors.add(mapError("TST version not supported")); } return errors; }
/** * Creates in memory document container. * * @param data file content * @param fileName file name with path * @param mimeType MIME type of the data file, for example 'text/plain' or 'application/msword' */ public DataFile(byte[] data, String fileName, String mimeType) { logger.debug("File name: " + fileName + ", mime type: " + mimeType); ByteArrayInputStream stream = new ByteArrayInputStream(data); document = new InMemoryDocument(stream, fileName, getMimeType(mimeType)); IOUtils.closeQuietly(stream); }
private ProxyDocument generateDataFileProxyDocument(ProxyDocument proxyDocument) { ProxyDocument dataFileDocument = new ProxyDocument(); InMemoryDocument inMemoryDocument = getDataFile(proxyDocument.getBytes()); dataFileDocument.setName(inMemoryDocument.getName()); dataFileDocument.setSignaturePolicy(proxyDocument.getSignaturePolicy()); dataFileDocument.setBytes(inMemoryDocument.getBytes()); return dataFileDocument; }
private List<InMemoryDocument> getFilesFromContainer(ValidationDocument validationDocument) { List<InMemoryDocument> documents = new ArrayList<>(); try (ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(validationDocument.getBytes()));) { ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { documents.add(new InMemoryDocument(zipStream, entry.getName())); } } catch (IOException e) { throw new MalformedDocumentException(e); } return documents; }
private void extractSignature(ZipEntry entry) { logger.debug("Extracting signature"); InputStream zipFileInputStream = getZipEntryInputStream(entry); String fileName = entry.getName(); InMemoryDocument document = new InMemoryDocument(zipFileInputStream, fileName); signatures.add(document); extractSignatureAsicEntry(entry, document); }
/** * Creates in memory document container. * * @param stream file content from stream * @param fileName file name with path * @param mimeType MIME type of the stream file, for example 'text/plain' or 'application/msword' */ public DataFile(InputStream stream, String fileName, String mimeType) { logger.debug("File name: " + fileName + ", mime type: " + mimeType); try { document = new InMemoryDocument(stream, fileName, getMimeType(mimeType)); } catch (Exception e) { logger.error(e.getMessage()); throw new InvalidDataFileException(e); } }
@Override public Signature openAdESSignature(byte[] signatureDocument) { if (signatureDocument == null) { logger.error("Signature cannot be empty"); throw new InvalidSignatureException(); } InMemoryDocument document = new InMemoryDocument(signatureDocument); return createSignature(document); }
/** * Creates signature object from XadES signature xml. * * @param signatureDocument XadES signature xml bytes. * @return builder for creating a signature. */ public Signature openAdESSignature(byte[] signatureDocument) { if (signatureDocument == null) { logger.error("Signature cannot be empty"); throw new InvalidSignatureException(); } InMemoryDocument document = new InMemoryDocument(signatureDocument); return createSignature(document); }
/** * This method returns true if the inputStream starts with an ASN.1 Sequence * * @param is * the inputstream to be tested * @return true if DER encoded */ public static boolean isStartWithASN1SequenceTag(InputStream is) { byte firstByte = readFirstByte(new InMemoryDocument(is)); return DSSASN1Utils.isASN1SequenceTag(firstByte); }
private void parseZipFileManifest() { ZipEntry entry = zipFile.getEntry(MANIFEST); if (entry == null) { return; } try { InputStream manifestStream = getZipEntryInputStream(entry); InMemoryDocument manifestFile = new InMemoryDocument(IOUtils.toByteArray(manifestStream)); parseManifestEntry(manifestFile); } catch (IOException e) { logger.error("Error parsing manifest file: " + e.getMessage()); throw new TechnicalException("Error parsing manifest file", e); } } }
/** * This method creates a new InMemoryDocument with the {@link org.w3c.dom.Document} content and the given name * * @param document * the {@link org.w3c.dom.Document} to store * @param name * the ouput filename * @return a new instance of InMemoryDocument with the XML and the given filename */ public static DSSDocument createDssDocumentFromDomDocument(Document document, String name) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { DomUtils.writeDocumentTo(document, baos); return new InMemoryDocument(baos.toByteArray(), name, MimeType.XML); } catch (IOException e) { throw new DSSException(e); } }
private DSSDocument extractStreamDocument(ZipEntry entry) { logger.debug("Zip entry size is <{}> bytes", entry.getSize()); MimeType mimeTypeCode = MimeTypeUtil.mimeTypeOf(this.getDataFileMimeType(entry.getName())); if (this.storeDataFilesOnlyInMemory || entry.getSize() <= this.maxDataFileCachedInBytes) { return new InMemoryDocument(this.getZipEntryInputStream(entry), entry.getName(), mimeTypeCode); } else { return new StreamDocument(this.getZipEntryInputStream(entry), entry.getName(), mimeTypeCode); } }
private void extractMimeType(ZipEntry entry) { try { InputStream zipFileInputStream = getZipEntryInputStream(entry); BOMInputStream bomInputStream = new BOMInputStream(zipFileInputStream); DSSDocument document = new InMemoryDocument(bomInputStream); mimeType = StringUtils.trim(IOUtils.toString(getDocumentBytes(document), "UTF-8")); extractAsicEntry(entry, document); } catch (IOException e) { logger.error("Error parsing container mime type: " + e.getMessage()); throw new TechnicalException("Error parsing container mime type: " + e.getMessage(), e); } }
private DSSDocument surroundWithXadesXmlTag(DSSDocument signedDocument) { logger.debug("Surrounding signature document with xades tag"); Document signatureDom = DomUtils.buildDOM(signedDocument); Element signatureElement = signatureDom.getDocumentElement(); Document document = XmlDomCreator.createDocument(ASiCNamespace.NS, XmlDomCreator.ASICS_NS, signatureElement); byte[] documentBytes = DSSXMLUtils.serializeNode(document); return new InMemoryDocument(documentBytes); }