Skip to content

Commit

Permalink
Fix issue LibrePDF#668
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Raccagni committed Feb 22, 2022
1 parent 8e4109b commit 5a11cdc
Show file tree
Hide file tree
Showing 653 changed files with 475,883 additions and 5 deletions.
98 changes: 98 additions & 0 deletions __openpdf/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf-parent</artifactId>
<version>1.3.27-SNAPSHOT</version>
</parent>

<artifactId>openpdf</artifactId>

<properties>
<java-module-name>com.github.librepdf.openpdf</java-module-name>
</properties>

<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<optional>true</optional>
</dependency>

<!-- Test Deps -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<!-- unpack bundle, so humans can see the manifest without unpacking the jar -->
<unpackBundle>true</unpackBundle>
<instructions>
<!-- All com.lowagie.text.* packages are 'public' -->
<Export-Package>com.lowagie.text.*;version="${project.version}"</Export-Package>
<!-- Declare the Bouncycastle dependencies as optional -->
<Import-Package>org.bouncycastle.*;resolution:=optional,!com.lowagie.*,*</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.lowagie.bouncycastle;

import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfObject;

import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cms.CMSEnvelopedData;
import org.bouncycastle.cms.Recipient;
import org.bouncycastle.cms.RecipientInformation;
import org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient;

import java.io.IOException;
import java.security.Key;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.util.Collection;
import java.util.List;

public class BouncyCastleHelper {
public static void checkCertificateEncodingOrThrowException(Certificate certificate) {
// OJO...
try {
new X509CertificateHolder(certificate.getEncoded());
} catch (CertificateEncodingException | IOException f) {
throw new ExceptionConverter(f);
}
// ******************************************************************************
}

@SuppressWarnings("unchecked")
public static byte[] getEnvelopedData(PdfArray recipients, List<PdfObject> strings, Certificate certificate, Key certificateKey, String certificateKeyProvider) {
byte[] envelopedData = null;
for (PdfObject recipient : recipients.getElements()) {
strings.remove(recipient);
try {
CMSEnvelopedData data = new CMSEnvelopedData(recipient.getBytes());

final Collection<RecipientInformation> recipientInformations = data.getRecipientInfos().getRecipients();
for (RecipientInformation recipientInfo : recipientInformations) {
if (recipientInfo.getRID().match(certificate)) {
// OJO...
// https://www.bouncycastle.org/docs/pkixdocs1.5on/org/bouncycastle/cms/CMSEnvelopedData.html
Recipient rec = new JceKeyTransEnvelopedRecipient(
(PrivateKey) certificateKey)
.setProvider(certificateKeyProvider);
envelopedData = recipientInfo.getContent(rec);
// ******************************************************************************
break;
}

}
} catch (Exception f) {
throw new ExceptionConverter(f);
}
}
return envelopedData;
}
}
Loading

0 comments on commit 5a11cdc

Please sign in to comment.