jdk/test/sun/security/pkcs11/ec/TestECDSA.java
changeset 35379 1e8e336ef66b
parent 32138 23830562d3d1
child 40975 680639c9b307
equal deleted inserted replaced
35378:7e19fa0e4e5b 35379:1e8e336ef66b
     1 /*
     1 /*
     2  * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    27  * @summary basic test of SHA1withECDSA and NONEwithECDSA signing/verifying
    27  * @summary basic test of SHA1withECDSA and NONEwithECDSA signing/verifying
    28  * @author Andreas Sterbenz
    28  * @author Andreas Sterbenz
    29  * @library ..
    29  * @library ..
    30  * @library ../../../../java/security/testlibrary
    30  * @library ../../../../java/security/testlibrary
    31  * @key randomness
    31  * @key randomness
       
    32  * @run main/othervm TestECDSA
       
    33  * @run main/othervm TestECDSA sm policy
    32  */
    34  */
    33 
    35 
    34 import java.util.*;
    36 import java.security.KeyFactory;
    35 
    37 import java.security.KeyPair;
    36 import java.security.*;
    38 import java.security.KeyPairGenerator;
    37 import java.security.spec.*;
    39 import java.security.MessageDigest;
    38 import java.security.interfaces.*;
    40 import java.security.PrivateKey;
       
    41 import java.security.Provider;
       
    42 import java.security.PublicKey;
       
    43 import java.security.Signature;
       
    44 import java.security.interfaces.ECPublicKey;
       
    45 import java.security.spec.PKCS8EncodedKeySpec;
       
    46 import java.security.spec.X509EncodedKeySpec;
       
    47 import java.util.Random;
    39 
    48 
    40 public class TestECDSA extends PKCS11Test {
    49 public class TestECDSA extends PKCS11Test {
    41 
    50 
    42     // values of the keys we use for the tests
    51     // values of the keys we use for the tests
    43 
    52 
    77 
    86 
    78     // data for test 2 (invalid signatures)
    87     // data for test 2 (invalid signatures)
    79     private final static byte[] data2Raw = {};
    88     private final static byte[] data2Raw = {};
    80     private final static byte[] data2SHA = b("da:39:a3:ee:5e:6b:4b:0d:32:55:bf:ef:95:60:18:90:af:d8:07:09");
    89     private final static byte[] data2SHA = b("da:39:a3:ee:5e:6b:4b:0d:32:55:bf:ef:95:60:18:90:af:d8:07:09");
    81 
    90 
    82     private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {
    91     private static void verify(Provider provider, String alg, PublicKey key,
       
    92             byte[] data, byte[] sig, boolean result) throws Exception {
    83         Signature s = Signature.getInstance(alg, provider);
    93         Signature s = Signature.getInstance(alg, provider);
    84         s.initVerify(key);
    94         s.initVerify(key);
    85         boolean r;
    95         boolean r;
    86         s.update(data);
    96         s.update(data);
    87         r = s.verify(sig);
    97         r = s.verify(sig);
   103         byte[] sig = s.sign();
   113         byte[] sig = s.sign();
   104         System.out.println(toString(sig));
   114         System.out.println(toString(sig));
   105     }
   115     }
   106 
   116 
   107     public static void main(String[] args) throws Exception {
   117     public static void main(String[] args) throws Exception {
   108         main(new TestECDSA());
   118         main(new TestECDSA(), args);
   109     }
   119     }
   110 
   120 
       
   121     @Override
   111     public void main(Provider provider) throws Exception {
   122     public void main(Provider provider) throws Exception {
   112         long start = System.currentTimeMillis();
   123         long start = System.currentTimeMillis();
   113 
   124 
   114         if (provider.getService("Signature", "SHA1withECDSA") == null) {
   125         if (provider.getService("Signature", "SHA1withECDSA") == null) {
   115             System.out.println("ECDSA not supported, skipping");
   126             System.out.println("ECDSA not supported, skipping");
   116             return;
   127             return;
   117         }
   128         }
   118 
   129 
   119         if (isNSS(provider) && getNSSVersion() >= 3.11 &&
   130         if (isBadNSSVersion(provider)) {
   120                 getNSSVersion() < 3.12) {
   131             return;
   121             System.out.println("NSS 3.11 has a DER issue that recent " +
   132         }
   122                     "version do not.");
   133 
   123             return;
   134         if (isBadSolarisSparc(provider)) {
   124         }
       
   125 
       
   126         /*
       
   127          * Use Solaris SPARC 11.2 or later to avoid an intermittent failure
       
   128          * when running SunPKCS11-Solaris (8044554)
       
   129          */
       
   130         if (provider.getName().equals("SunPKCS11-Solaris") &&
       
   131             System.getProperty("os.name").equals("SunOS") &&
       
   132             System.getProperty("os.arch").equals("sparcv9") &&
       
   133             System.getProperty("os.version").compareTo("5.11") <= 0 &&
       
   134             getDistro().compareTo("11.2") < 0) {
       
   135 
       
   136             System.out.println("SunPKCS11-Solaris provider requires " +
       
   137                 "Solaris SPARC 11.2 or later, skipping");
       
   138             return;
   135             return;
   139         }
   136         }
   140 
   137 
   141         /*
   138         /*
   142          * PKCS11Test.main will remove this provider if needed
   139          * PKCS11Test.main will remove this provider if needed