verifying detached signature with BC

后端 未结 3 1775
陌清茗
陌清茗 2020-12-16 03:54

How can I verify a detached signature (CMS/pkcs #7 signature) using the BouncyCastle provider in Java?

Currently, my code below throws an exception with the message

相关标签:
3条回答
  • 2020-12-16 04:30

    You can verify detached signature by the following code :

    public static boolean verif_Detached(String signed_file_name,String original_file_name) throws IOException, CMSException, NoSuchAlgorithmException, NoSuchProviderException, CertStoreException, CertificateExpiredException, CertificateNotYetValidException{
    
        boolean result= false;
        Security.addProvider(new BouncyCastleProvider()); 
    
        File f = new File(signed_file_name);
        byte[] Sig_Bytes = new byte[(int)f.length()];
        DataInputStream in = new DataInputStream(new FileInputStream(f));
        in.readFully(Sig_Bytes);
        in.close();
    
        File fi = new File(original_file_name);
        byte[] Data_Bytes = new byte[(int)fi.length()];
        DataInputStream input = new DataInputStream(new FileInputStream(fi));
        input.readFully(Data_Bytes);
        input.close();
    
        try{
            CMSSignedData cms = new CMSSignedData(new CMSProcessableByteArray(Data_Bytes), Sig_Bytes); 
            CertStore certStore = cms.getCertificatesAndCRLs("Collection", "BC"); 
            SignerInformationStore signers = cms.getSignerInfos(); 
            Collection c = signers.getSigners(); 
            Iterator it = c.iterator(); 
            while (it.hasNext()) { 
                SignerInformation signer = (SignerInformation) it.next(); 
                Collection certCollection = certStore.getCertificates(signer.getSID()); 
                Iterator certIt = certCollection.iterator(); 
                X509Certificate cert = (X509Certificate) certIt.next();
                cert_signer=cert;
                result=signer.verify(cert, "BC");
            }
        }catch(Exception e){
            e.printStackTrace();
            result=false;
        }
        return result; 
    }
    
    0 讨论(0)
  • 2020-12-16 04:43

    You can find the answer to this post here. This happening because how bouncy castle/open ssl treats the S/MIME message when S/MIME headers are not present.Solution is to add S/MIME headers to the message before signimg

    0 讨论(0)
  • 2020-12-16 04:50

    the key for verify detached pKCS7 is use of CMSTypedStream ,like code bellow:

    public void verifySign(byte[] signedData,byte[]bPlainText) throws Exception {
                    InputStream is  = new ByteArrayInputStream(bPlainText);             
                    CMSSignedDataParser sp = new CMSSignedDataParser(new CMSTypedStream (is),signedData);
                    CMSTypedStream signedContent = sp.getSignedContent();           
    
                     signedContent.drain();
    
    
    
    
    
                      //CMSSignedData s = new CMSSignedData(signedData); 
                      Store certStore = sp.getCertificates(); 
    
                      SignerInformationStore signers = sp.getSignerInfos(); 
                        Collection c = signers.getSigners();
                        Iterator it = c.iterator();
                        while (it.hasNext()) 
                        { 
                            SignerInformation signer = (SignerInformation)it.next(); 
                            Collection certCollection = certStore.getMatches(signer.getSID()); 
    
                            Iterator certIt = certCollection.iterator(); 
    
                            X509CertificateHolder certHolder = (X509CertificateHolder)certIt.next(); 
    
    
    
    
                            if ( !signer.verify(new 
                JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(certHolder))) 
                            { 
                                throw new DENException("Verification FAILED! "); 
    
                            } 
                            else
                            {
                                logger.debug("verify success" );
                            }
    
    
                        } 
        }
    
    0 讨论(0)
提交回复
热议问题