0

暗号化規格 PKCS # 7 について質問したいのですが、「PATH_DOC_NF1」というファイルがあり、PKCS # 7 暗号エンベロープを作成する必要があります。KeyStore と AIK-JCE (Java ベースの暗号化サービス プロバイダー) を使用します。

私が所有している X.509 証明書は次のとおりです。

バージョン: 3
シリアル番号: 231944
署名アルゴリズム: sha1WithRSAEncryption (1.2.840.113549.1.1.5)
発行者: CN=………,OU=………,O=……,C=IT
以前に有効でない: 2009 年 4 月 1 日:20:15 CEST 2011
not after: Tue Apr 01 09:20:13 CEST 2014
件名: CN=TRANTN59T46H703C-001,OU=……,O=………,C=IT
Sun RSA 公開鍵、1024 ビット
係数: ……<br /> 公開指数: …<br /> 証明書フィンガープリント (MD5) : …………<br /> 証明書フィンガープリント (SHA-1): …………<br />

エクステンション: 4

以下に記述されたコードを実行します。

public static void main(String[] args) {

    Path path = Paths.get(PATH_DOC_NF1);
    try {
        byte[] data = Files.readAllBytes(path);
        Security.addProvider(new IAIK()); 

        KeyStore ks = KeyStore.getInstance("IAIKKeyStore", "IAIK");
        InputStream is = new FileInputStream(PATH_KEYSTORE_TELEMACO);
        X509Certificate certX509;
        PrivateKey pk;

        String pathout=PATH_DOC_NF1+".p7m";

        File file = new File(pathout);

        if (file.exists()){
            System.out.println("Il file " + pathout +" esiste");
        }else if (file.createNewFile()){
            System.out.println("Il file " + pathout +" E' stato creato");
        }else{
            System.out.println("Il file " + pathout +" non puo' essere creato");
        }
        ByteArrayInputStream in = new ByteArrayInputStream(data);

        if (ks != null){
            ks.load(is, KEY_PASSWORD_TELEMACO);
            Enumeration<String> aliases = ks.aliases();
            for (; aliases.hasMoreElements(); ) {
                String alias = aliases.nextElement();
                if(ks.isKeyEntry(alias)){
                    pk = (PrivateKey) ks.getKey(alias,KEY_PASSWORD_TELEMACO);
                    Certificate[] chain = ks.getCertificateChain(alias);
                    certX509 = (X509Certificate) chain[0];


                    MessageDigest digestEngine = MessageDigest.getInstance("SHA");

                    ByteArrayOutputStream contentBuffer = new ByteArrayOutputStream();

                    byte[] contentHash = digestEngine.digest(data);

                    contentBuffer.close();

                    SignedData signedData = new SignedData(contentBuffer.toByteArray(), SignedData.IMPLICIT);

                    signedData.setCertificates(new X509Certificate[] { certX509 });

                    SignerInfo signerInfo = new SignerInfo(new IssuerAndSerialNumber(certX509), AlgorithmID.sha, null);

                    Attribute[] authenticatedAttributes = {
                            new Attribute(ObjectID.contentType,new ASN1Object[] {ObjectID.pkcs7_data}),
                            new Attribute(ObjectID.signingTime,new ASN1Object[] {new ChoiceOfTime().toASN1Object()}),
                            new Attribute(ObjectID.messageDigest,new ASN1Object[] {new OCTET_STRING(contentHash)})
                    };

                    signerInfo.setAuthenticatedAttributes(authenticatedAttributes);

                    byte[] toBeSigned = DerCoder.encode(ASN.createSetOf(authenticatedAttributes, true));

                    MessageDigest md = MessageDigest.getInstance("SHA");

                    byte[] hashToBeSigned = md.digest(toBeSigned);

                    DigestInfo digestInfoEngine = new DigestInfo(AlgorithmID.sha1WithRSAEncryption, hashToBeSigned);
                    byte[] toBeEncrypted = digestInfoEngine.toByteArray();


                    Signature sign = Signature.getInstance("RSA");
                    sign.initSign(pk);

                    sign.update(toBeEncrypted);
                    byte[] signatureValue = sign.sign();

                    signerInfo.setEncryptedDigest(signatureValue);

                    signedData.addSignerInfo(signerInfo);

                    OutputStream signatureOutput = new FileOutputStream(pathout);

                    ContentInfoStream cis = new ContentInfoStream(signedData);
                    BufferedOutputStream bos = new BufferedOutputStream(signatureOutput);

                    Base64OutputStream b64Out = new Base64OutputStream(bos);

                    cis.writeTo(b64Out);
                    b64Out.close();
                    in.close();

                    /****VERIFY_SIGN******/

                    InputStream encodedStream;

                    encodedStream = new FileInputStream(pathout);
                    ASN1InputStream asn1 = new ASN1InputStream(encodedStream);
                    ContentInfoStream cis2 = new ContentInfoStream(asn1);

                    SignedDataStream signedData2;

                    if (cis.getContentType().equals(ObjectID.cms_signedData)){

                        signedData2 = (SignedDataStream)cis2.getContent();  
                        InputStream contentStream = signedData2.getInputStream();

                        byte[] read = new byte[10240];

                        int byteRead;
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();

                        while((byteRead = contentStream.read(read)) > 0){
                            baos.write(read,0,byteRead);
                        }

                        SignerInfo[] signerInfos = signedData2.getSignerInfos();
                        System.out.println(pathout+"\n\n"+signerInfos[0].toString());

                        for (int i=0; i < signerInfos.length; i++){
                            try
                            {
                                X509Certificate signerCertificate = signedData.verify(i);
                            }
                            catch (SignatureException ex)
                            {
                                try {
                                    System.out.println("Signature ERROR from signer with certificate: "+signedData.getCertificate(signerInfos[i].getIssuerAndSerialNumber()));

                                } catch (PKCSException e) {
                                    e.printStackTrace();
                                }
                                ex.printStackTrace();
                            }
                        }
                    }
                    else
                    {
                        System.out.println("errore, ci aspettiamo un SignedData");
                    }
                }
            }
        }


    } catch (IOException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
    } catch (CodingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    } catch (PKCSException e) {
        e.printStackTrace();
    }

}

次のエラーが表示されます。

java.security.SignatureException: 署名検証エラー: メッセージ ハッシュ!
iaik.pkcs.pkcs7.SignedDataStream.verify (不明なソース)
で message.PKCS7.main (PKCS7.java:183) で

完全を期すために、作成した Signinfo を挿入します。

バージョン: 1シリアル番号 :
231944発行者 :
CN=………,OU=………,O=……,C=IT encrypted_digest : 128 バイト [7E:B4:19:95: 77 ... ] 2E...










私を助けてくれる人はいますか?

4

0 に答える 0