Java - Create XML Digital Signature using ECDSA (Elliptic Curve)
Asked Answered
Q

1

7

We can create XML Digital Signature using RSA keys. But how do I use elliptic curve keys to sign xml files ? I get error messages such as -

Exception in thread "main" java.security.KeyException: ECKeyValue not supported
    at org.jcp.xml.dsig.internal.dom.DOMKeyValue$EC.<init>(DOMKeyValue.java:350)
    at org.jcp.xml.dsig.internal.dom.DOMKeyInfoFactory.newKeyValue(DOMKeyInfoFactory.java:71)
    at csr.ExtractEC.main(XMLSignatureECTest.java:57)
Caused by: java.lang.ClassNotFoundException: sun/security/ec/ECParameters
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at org.jcp.xml.dsig.internal.dom.DOMKeyValue$EC.getMethods(DOMKeyValue.java:367)
    at org.jcp.xml.dsig.internal.dom.DOMKeyValue$EC$1.run(DOMKeyValue.java:343)
    at org.jcp.xml.dsig.internal.dom.DOMKeyValue$EC$1.run(DOMKeyValue.java:339)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.jcp.xml.dsig.internal.dom.DOMKeyValue$EC.<init>(DOMKeyValue.java:338)
    ... 2 more

I used below code to create SignatureMethod and KeyInfo -

String url = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256";
        SignatureMethod signatureMethod = factory.newSignatureMethod(url, null);
        SignedInfo signedInfo = factory.newSignedInfo(c14n, signatureMethod, Collections.singletonList(reference));

        PrivateKey privateKey = Utils.generatePrivateEC("e:\\certs\\ec\\ec.key.p8");
        Certificate certificate = Utils.generatePublic("e:\\certs\\ec\\ec.cer");
        KeyInfoFactory keyInfoFactory = factory.getKeyInfoFactory();
        KeyValue keyValue = keyInfoFactory.newKeyValue(certificate.getPublicKey());
        KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyValue));

JDK - Oracle JDK 8 Security Providers - BouncyCastle and Sun.

Question answered 22/8, 2018 at 7:26 Comment(8)
I do not understand why people just down votes any question, if it they are not interested. They creates problem for the community.Question
it is not just a downvote. There is no minimal reproducible example no informations about which JDK/JRE you useMontage
Maybe this helpsMontage
@Jens: Ugh, no workarounds given.Unkindly
Really strange what's going on: it fails on Class.forName("sun.security.ec.ECParameters") but if you statically access the same class, or call Class.forName yourself it works, only to fail later in the library call. Are they messing with the class loaders?!Isom
Have you seen this test class? It seems you need exactly this...Kerek
Can you show how you're creating the factory? Also, where is this running? In JBoss, Tomcat, standalone, etc.?Fungus
@Kerek please post the link in answer so that i can select it as the correct answer and 50 points. Thanks for answer.Question
K
1

It seems in this junit test someone is make an example for you.

Kerek answered 1/9, 2018 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.