最新バージョン (4.6.RC1) を使用して、開始点としてCAdES メソッドとdss-cookbookの例を使用して PDF ドキュメントに署名しようとしています。
の例に従って、SignPdfPadesBDetached.javaを使用して PDF ドキュメントに署名することに成功しましたPAdES。ただし、 の例がないためCAdES、上記の例を に適応させようとしましたCAdESが、うまくいきません。具体的には、生成された PDF ドキュメントのサイズが予想される 2.5 MB ではなく 7k しかなく、PDF を開こうとすると次のエラーが表示されます。使用する設定は次のとおりです。
- SignatureLevel.CAdES_BASELINE_B
- SignaturePackaging.DETACHED
- DigestAlgorithm.SHA256
そして、親戚のメソッドコードは現在これです:
public static void signPdfWithCades(DSSDocument toSignDocument) {
LOG.info("Signing PDF with CADES B");
try {
AbstractSignatureTokenConnection signingToken = new Pkcs12SignatureToken("password", KEYSTORE_PATH);
DSSPrivateKeyEntry privateKey = signingToken.getKeys().get(0);
// Preparing parameters for the CAdES signature
CAdESSignatureParameters parameters = new CAdESSignatureParameters();
// We choose the level of the signature (-B, -T, -LT, -LTA).
parameters.setSignatureLevel(SignatureLevel.CAdES_BASELINE_B);
// We choose the type of the signature packaging (ENVELOPING, DETACHED).
parameters.setSignaturePackaging(SignaturePackaging.DETACHED);
// We set the digest algorithm to use with the signature algorithm. You must use the
// same parameter when you invoke the method sign on the token. The default value is
// SHA256
parameters.setDigestAlgorithm(DigestAlgorithm.SHA256);
// We set the signing certificate
parameters.setSigningCertificate(privateKey.getCertificate());
// We set the certificate chain
parameters.setCertificateChain(privateKey.getCertificateChain());
// Create common certificate verifier
CommonCertificateVerifier commonCertificateVerifier = new CommonCertificateVerifier();
// Create PAdES xadesService for signature
CAdESService service = new CAdESService(commonCertificateVerifier);
// Get the SignedInfo segment that need to be signed.
ToBeSigned dataToSign = service.getDataToSign(toSignDocument, parameters);
// This function obtains the signature value for signed information using the
// private key and specified algorithm
DigestAlgorithm digestAlgorithm = parameters.getDigestAlgorithm();
SignatureValue signatureValue = signingToken.sign(dataToSign, digestAlgorithm, privateKey);
// We invoke the cadesService to sign the document with the signature value obtained in
// the previous step.
DSSDocument signedDocument = service.signDocument(toSignDocument, parameters, signatureValue);
LOG.info("Signed PDF size = " + signedDocument.getBytes().length);
//We use the DSSUtils to Save to file
DSSUtils.saveToFile(signedDocument.openStream(), "target/signedPdfCadesBDetached.pdf");
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
で署名するための対応する方法は、上記と同様であり、クラスPAdESに合わせて調整されていますPAdES(つまりPAdESSignatureParameters、 、SignatureLevel.PAdES_BASELINE_Bおよびを使用しました)。PAdESService
SD-DSS プロジェクトは Maven Central リポジトリでホストされていないため、明示的に参照する必要があることに注意してください。
<repositories>
<repository>
<id>europa</id>
<url>https://joinup.ec.europa.eu/nexus/content/groups/public/</url>
</repository>
</repositories>
さらに、必要な/対応するすべての依存関係を以下に含めたと思いますpom.xml。
<dependency>
<groupId>eu.europa.ec.joinup.sd-dss</groupId>
<artifactId>dss-token</artifactId>
<version>4.6.RC1</version>
</dependency>
<dependency>
<groupId>eu.europa.ec.joinup.sd-dss</groupId>
<artifactId>dss-pades</artifactId>
<version>4.6.RC1</version>
</dependency>
<dependency>
<groupId>eu.europa.ec.joinup.sd-dss</groupId>
<artifactId>dss-cades</artifactId>
<version>4.6.RC1</version>
</dependency>
<dependency>
<groupId>eu.europa.ec.joinup.sd-dss</groupId>
<artifactId>dss-document</artifactId>
<version>4.6.RC1</version>
</dependency>
これに先立って、 PDFBoxも試してみましたが、達成したいことによると、ドキュメントはあまり役に立ちませんでした。
ここで何が間違っているのですか?パッケージのエンベロープを変更しても違いはありません。CAdES で署名する方法は非常に異なるため、PAdES の例をガイドとして使用すべきではありませんか?