< prev index next >

test/jdk/sun/security/ec/xec/XECKeyFormat.java

Print this page

  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8213363
 27  * @summary Check for correct formatting of X25519/X448 private keys
 28  * @library /test/lib
 29  * @build jdk.test.lib.Convert
 30  * @modules java.base/sun.security.util
 31  * @run main XECKeyFormat
 32  */
 33 
 34 import java.security.*;
 35 import java.security.spec.*;
 36 import java.security.interfaces.*;
 37 import java.io.*;
 38 import java.nio.file.*;
 39 import java.math.*;
 40 import java.util.*;
 41 
 42 import jdk.test.lib.Convert;
 43 
 44 import sun.security.util.*;
 45 
 46 public class XECKeyFormat {
 47 
 48     private interface Test {
 49         public void runTest(Provider p) throws Exception;
 50     }
 51 
 52     private static void forEachProvider(Test t, String algName)
 53         throws Exception {
 54 
 55         int tested = 0;
 56         for (Provider p : Security.getProviders()) {
 57             Provider.Service s = p.getService("KeyPairGenerator", algName);
 58             if (s != null) {
 59                 t.runTest(p);
 60                 tested++;
 61             }
 62         }

 81     );
 82 
 83     public static void main(String[] args) throws Exception {
 84         privKeyTest("X25519");
 85         privKeyTest("X448");
 86         pubKeyTest();
 87     }
 88 
 89     private static void pubKeyTest() throws Exception {
 90         forEachProvider(XECKeyFormat::pubKeyTest, "XDH");
 91     }
 92 
 93     private static void pubKeyTest(Provider p) throws Exception {
 94         for (String s : pubKeys) {
 95             pubKeyTest(p, s);
 96         }
 97     }
 98 
 99     private static void pubKeyTest(Provider p, String key) throws Exception {
100         // ensure that a properly-formatted key can be read
101         byte[] encodedKey = Convert.hexStringToByteArray(key);
102         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
103         KeyFactory kf = KeyFactory.getInstance("XDH", p);
104         kf.generatePublic(keySpec);
105     }
106 
107     private static void privKeyTest(String algName) throws Exception {
108 
109         forEachProvider(p -> privKeyTest(algName, p), algName);
110     }
111 
112     private static void privKeyTest(String algName, Provider p)
113         throws Exception {
114 
115         System.out.println("Testing " + algName + " in " + p.getName());
116 
117         // ensure format produced is correct
118         KeyPairGenerator kpg = KeyPairGenerator.getInstance(algName, p);
119         KeyPair kp = kpg.generateKeyPair();
120         PrivateKey priv = kp.getPrivate();
121         checkPrivKeyFormat(priv.getEncoded());
122         KeyFactory kf = KeyFactory.getInstance(algName, p);
123         PKCS8EncodedKeySpec keySpec =
124             kf.getKeySpec(priv, PKCS8EncodedKeySpec.class);
125         checkPrivKeyFormat(keySpec.getEncoded());
126 
127         // ensure that a properly-formatted key can be read
128         byte[] encodedKey = Convert.hexStringToByteArray(privKeys.get(algName));
129         keySpec = new PKCS8EncodedKeySpec(encodedKey);
130         kf.generatePrivate(keySpec);
131     }
132 
133     private static void checkPrivKeyFormat(byte[] key) throws IOException {
134         // key value should be nested octet strings
135         DerValue val = new DerValue(new ByteArrayInputStream(key));
136         BigInteger version = val.data.getBigInteger();
137         DerValue algId = val.data.getDerValue();
138         byte[] keyValue = val.data.getOctetString();
139         val = new DerValue(new ByteArrayInputStream(keyValue));
140         if (val.tag != DerValue.tag_OctetString) {
141             throw new RuntimeException("incorrect format");
142         }
143     }
144 }
145 
146 

  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8213363
 27  * @summary Check for correct formatting of X25519/X448 private keys
 28  * @library /test/lib

 29  * @modules java.base/sun.security.util
 30  * @run main XECKeyFormat
 31  */
 32 
 33 import java.security.*;
 34 import java.security.spec.*;
 35 import java.security.interfaces.*;
 36 import java.io.*;
 37 import java.nio.file.*;
 38 import java.math.*;
 39 import java.util.*;
 40 
 41 import java.util.Hex;
 42 
 43 import sun.security.util.*;
 44 
 45 public class XECKeyFormat {
 46 
 47     private interface Test {
 48         public void runTest(Provider p) throws Exception;
 49     }
 50 
 51     private static void forEachProvider(Test t, String algName)
 52         throws Exception {
 53 
 54         int tested = 0;
 55         for (Provider p : Security.getProviders()) {
 56             Provider.Service s = p.getService("KeyPairGenerator", algName);
 57             if (s != null) {
 58                 t.runTest(p);
 59                 tested++;
 60             }
 61         }

 80     );
 81 
 82     public static void main(String[] args) throws Exception {
 83         privKeyTest("X25519");
 84         privKeyTest("X448");
 85         pubKeyTest();
 86     }
 87 
 88     private static void pubKeyTest() throws Exception {
 89         forEachProvider(XECKeyFormat::pubKeyTest, "XDH");
 90     }
 91 
 92     private static void pubKeyTest(Provider p) throws Exception {
 93         for (String s : pubKeys) {
 94             pubKeyTest(p, s);
 95         }
 96     }
 97 
 98     private static void pubKeyTest(Provider p, String key) throws Exception {
 99         // ensure that a properly-formatted key can be read
100         byte[] encodedKey = Hex.decoder().decode(key);
101         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
102         KeyFactory kf = KeyFactory.getInstance("XDH", p);
103         kf.generatePublic(keySpec);
104     }
105 
106     private static void privKeyTest(String algName) throws Exception {
107 
108         forEachProvider(p -> privKeyTest(algName, p), algName);
109     }
110 
111     private static void privKeyTest(String algName, Provider p)
112         throws Exception {
113 
114         System.out.println("Testing " + algName + " in " + p.getName());
115 
116         // ensure format produced is correct
117         KeyPairGenerator kpg = KeyPairGenerator.getInstance(algName, p);
118         KeyPair kp = kpg.generateKeyPair();
119         PrivateKey priv = kp.getPrivate();
120         checkPrivKeyFormat(priv.getEncoded());
121         KeyFactory kf = KeyFactory.getInstance(algName, p);
122         PKCS8EncodedKeySpec keySpec =
123             kf.getKeySpec(priv, PKCS8EncodedKeySpec.class);
124         checkPrivKeyFormat(keySpec.getEncoded());
125 
126         // ensure that a properly-formatted key can be read
127         byte[] encodedKey = Hex.decoder().decode(privKeys.get(algName));
128         keySpec = new PKCS8EncodedKeySpec(encodedKey);
129         kf.generatePrivate(keySpec);
130     }
131 
132     private static void checkPrivKeyFormat(byte[] key) throws IOException {
133         // key value should be nested octet strings
134         DerValue val = new DerValue(new ByteArrayInputStream(key));
135         BigInteger version = val.data.getBigInteger();
136         DerValue algId = val.data.getDerValue();
137         byte[] keyValue = val.data.getOctetString();
138         val = new DerValue(new ByteArrayInputStream(keyValue));
139         if (val.tag != DerValue.tag_OctetString) {
140             throw new RuntimeException("incorrect format");
141         }
142     }
143 }
144 
145 
< prev index next >