分离的数字签名验证失败,但没有以下算法例外:提供者SUN的SHA256WITHECDSA

时间:2019-10-22 10:28:44

标签: java digital-signature bouncycastle pkcs#7

我正在使用外部签名API在分离模式下对某些内容进行签名,外部API进行内容哈希(SHA256)并以PKCS#7(Base64编码)和X509证书签名者返回已签名的字节。现在,我要验证签名,为此,我正在使用以下代码。

Pom.xml

<?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>

    <groupId>nic.oad</groupId>
    <artifactId>verifysig</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on -->
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.63</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk15on</artifactId>
            <version>1.63</version>
        </dependency>
    </dependencies>

</project>

VerifyDetachedSignature.java



import org.bouncycastle.cms.*;
import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.OperatorCreationException;

import java.io.ByteArrayInputStream;
import java.security.Security;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Base64;

public class VerifyDetachedSignature {
    public static void main (String ... args){
        String origContentBeforeSign = "hello dear";

        //Input for external API
        String sha256OfOrigContent = "b17c48764e2ffbfecaa252c449b38185a6ef1f47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U8qtTCRDvDVWMbBNL2FOxv5pBh4870mqhVD0ikJLkzPibt+0+d3Tg8DBbDCmXranVy5E8GB3WQZG4wFA+YZgioS910Bm1zKVcuYTcdpaeCoThtib11Xel6+/eHNk9rPPR8tUIt3EQAiDhsBx9oZvKFePgmZfgAeTkeg1NjTzWao/wYD3wLIYgGbNUqbIzJ1FresJEZ4eZMNEhzdUgDYYCxb/4D5nqHcE2hdLBPYADhKpopUCSkdaahiVtvQm7SpMHNCy39mz3EX8FMYIBtTCCAbECAQEwgdowgcExCzAJBgNVBAYTAklOMQ4wDAYDVQQKEwVDLURBQzEgMB4GA1UECxMXQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkxDzANBgNVBBETBjQxMTAwNzEUMBIGA1UECBMLTWFoYXJhc2h0cmExJTAjBgNVBAkTHFB1bmUgVW5pdmVyc2l0eSBDYW1wdXMsIFB1bmUxGjAYBgNVBDMTETFzdCBmbG9vciwgS2hvc2xhMRYwFAYDVQQDEw1DLURBQyBDQSAyMDE0AhQAjf6ieYPBMdWstvRlCN/qJ5rNzTANBglghkgBZQMEAgEFAKBpMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTE5MTAyMTExMTcwMVowLwYJKoZIhvcNAQkEMSIEILF8SHZOL/v+yqJSxEmzgYWm7x9H8Xn2DVd20mYPVoKYMAwGCCqGSM49BAMCBQAERzBFAiEA5yYdKoT8jppsvsajBDDwCIUMHjB7ZE1Fz4Lzdu+/r5YCIBYfT2JWSiVfn51pyC/iAwdC+x6zvYOk1fPMcrGj79fU";

        try {
            byte[] signedByte = Base64.getDecoder().decode(signedContentPKCS7);
            byte base64EncodedCert[] = Base64.getDecoder().decode(userX509Cert);
            ByteArrayInputStream inputStream  =  new ByteArrayInputStream(base64EncodedCert);

            Security.addProvider(new BouncyCastleProvider());

            CMSSignedData s = new CMSSignedData(new CMSProcessableByteArray(sha256OfOrigContent.getBytes()), signedByte);
            SignerInformationStore signers = s.getSignerInfos();
            SignerInformation signerInfo = (SignerInformation)signers.getSigners().iterator().next();

            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate)cf.generateCertificates(inputStream).iterator().next();
            System.out.println(cert.getSigAlgName());
            JcaSimpleSignerInfoVerifierBuilder obj = new JcaSimpleSignerInfoVerifierBuilder();
            obj.setProvider("SUN");
            boolean result = signerInfo.verify(obj.build(cert.getPublicKey()));
            System.out.println("Verified: "+result);
        } catch (CertificateException | CMSException
                | OperatorCreationException
                e) {
            e.printStackTrace();
        }



    }
}

我在执行代码时遇到异常。

org.bouncycastle.cms.CMSException: can't create content verifier: exception on setup: java.security.NoSuchAlgorithmException: no such algorithm: SHA256WITHECDSA for provider SUN
    at org.bouncycastle.cms.SignerInformation.doVerify(Unknown Source)
    at org.bouncycastle.cms.SignerInformation.verify(Unknown Source)
    at VerifyDetachedSignature.main(VerifyDetachedSignature.java:41)
Caused by: org.bouncycastle.operator.OperatorCreationException: exception on setup: java.security.NoSuchAlgorithmException: no such algorithm: SHA256WITHECDSA for provider SUN
    at org.bouncycastle.operator.jcajce.JcaContentVerifierProviderBuilder.createSignature(Unknown Source)
    at org.bouncycastle.operator.jcajce.JcaContentVerifierProviderBuilder.access$200(Unknown Source)
    at org.bouncycastle.operator.jcajce.JcaContentVerifierProviderBuilder$2.get(Unknown Source)
    at org.bouncycastle.cms.SignerInformationVerifier.getContentVerifier(Unknown Source)
    ... 3 more
Caused by: java.security.NoSuchAlgorithmException: no such algorithm: SHA256WITHECDSA for provider SUN
    at sun.security.jca.GetInstance.getService(GetInstance.java:87)
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:206)
    at java.security.Signature.getInstance(Signature.java:361)
    at org.bouncycastle.jcajce.util.NamedJcaJceHelper.createSignature(Unknown Source)
    at org.bouncycastle.operator.jcajce.OperatorHelper.createSignature(Unknown Source)
    ... 7 more
Disconnected from the target VM, address: '127.0.0.1:63442', transport: 'socket'

Process finished with exit code 0

我正在使用Java版本1.8.0_221 我还尝试过使用here

中提到的JDK / JRE的Java密码学扩展(JCE)无限强度管辖权策略文件

有人可以指导我,我想念什么或做错什么了吗?

0 个答案:

没有答案