如何在签名哈希中的PADES-EPES中添加属性签名策略标识符

时间:2016-06-08 08:15:47

标签: java pdf itext

我需要在sign pdf中添加一个策略标识符,例如:

sigPolicyId 2.16.724.1.3.1.1.2.1.9
sigPolicyHash::hashAlgorithm    1.3.14.3.2.26
sigPolicyHash::hashValue    G7roucf600+f03r/o0bAOQ6WAs0=
sigPolicyQualifierId::SPuri     https://sede.060.gob.es/politica_de_firma_anexo_1.pdf

我需要帮助

我的代码是:

private byte[] generaHash(byte hash[], int contentEstimated, PrivateKey key, Certificate[] chain, Provider provider) throws Exception{

    byte signedHash[] = null; 
    try{

     Calendar cal = Calendar.getInstance();

     PdfPKCS7 sgn = new PdfPKCS7(key, chain, null, "SHA1", provider.getName(), false);
     log.debug("getDigestAlgorithm() ="+sgn.getDigestAlgorithm());
     byte[] ocsp = null;

     byte sh[] = sgn.getAuthenticatedAttributeBytes(hash, cal, ocsp);
     sgn.update(sh, 0, sh.length);
     byte[] encodedSig = null;
     try{
         encodedSig = sgn.getEncodedPKCS7(hash, cal, null, ocsp);
     }catch(Exception ex){
        ex.printStackTrace();
        throw new Exception(LabelManager.get("ERROR_OPEN_CERTIFICATE_STORE"));
     }

     if (contentEstimated/2 + 2 < encodedSig.length)
         throw new Exception("Not enough space");

     signedHash = new byte[contentEstimated/2];
     System.arraycopy(encodedSig, 0, signedHash, 0, encodedSig.length);

    }catch(Exception e){
        e.printStackTrace();  
        throw new Exception(e.getMessage());
    }

     return signedHash;
 }   

然后:

public void reconstruyePdf(byte[] signHash, String fileOut) throws IOException{

    String sigAreaHex = byteArrayToHexString( signHash );

    byte[] placeHolder = byteArrayToHexString(getPlaceHolderArr( contentEstimated )).getBytes();

  //need a byte array that exactly matches the 'placeHolder' length
    byte paddedHexArea[] = new byte[ placeHolder.length ];

    // fill with harmless data
    for( int i = 0; i < paddedHexArea.length; i++ ){
     paddedHexArea[i] = 0x30;
    }

    // copy the signature into the ( hopefully ) bigger byte array, what matches the 
    // pre-reserved space in the PDF file
    System.arraycopy( sigAreaHex.getBytes(), 0, paddedHexArea, 0, sigAreaHex.getBytes().length);

    // replace the signature within the PDF
    try {
            FilePatch.replace( fileOut, placeHolder, paddedHexArea );
    } catch (IOException e) {
        Trazas.imprimeErrorExtendido(e);
        throw e;
    }

}

过程如何添加政策标识符以签署pdf?

0 个答案:

没有答案