Path: blob/master/test/jdk/sun/security/rsa/SigRecord.java
41149 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedReader;24import java.io.File;25import java.io.FileInputStream;26import java.io.IOException;27import java.io.InputStreamReader;28import java.math.BigInteger;29import java.security.*;30import java.security.spec.*;31import java.util.ArrayList;32import java.util.HexFormat;33import java.util.List;3435public final class SigRecord {3637static final String TEST_SRC = System.getProperty("test.src", ".");3839public static final class SigVector {40// digest algorithm to use41final String mdAlg;4243// message to test44final String msg;4546// expected signature47final String sig;4849public SigVector(String mdAlg, String msg, String sig) {50if (mdAlg == null || mdAlg.isEmpty()) {51throw new IllegalArgumentException("Digest algo must be specified");52}53if (msg == null || mdAlg.isEmpty()) {54throw new IllegalArgumentException("Message must be specified");55}56if (sig == null || mdAlg.isEmpty()) {57throw new IllegalArgumentException("Signature must be specified");58}59this.mdAlg = mdAlg;60this.msg = msg;61this.sig = sig;62}6364@Override65public String toString() {66return (mdAlg + ": msg=" + msg + ": sig=" + sig);67}68}6970final String id;71// RSA private key value associated with the corresponding test vectors72final RSAPrivateKeySpec privKeySpec;7374// RSA public key value associated with the corresponding test vectors75final RSAPublicKeySpec pubKeySpec;7677// set of test vectors78final List<SigVector> testVectors;7980SigRecord(String mod, String pubExp, String privExp, List<SigVector> testVectors) {81if (mod == null || mod.isEmpty()) {82throw new IllegalArgumentException("Modulus n must be specified");83}84if (pubExp == null || pubExp.isEmpty()) {85throw new IllegalArgumentException("Public Exponent e must be specified");86}87if (privExp == null || privExp.isEmpty()) {88throw new IllegalArgumentException("Private Exponent d must be specified");89}90if (testVectors == null || (testVectors.size() == 0)) {91throw new IllegalArgumentException("One or more test vectors must be specified");92}9394BigInteger n = new BigInteger(1, HexFormat.of().parseHex(mod));95BigInteger e = new BigInteger(1, HexFormat.of().parseHex(pubExp));96BigInteger d = new BigInteger(1, HexFormat.of().parseHex(privExp));97this.id = ("n=" + mod + ", e=" + pubExp);98this.pubKeySpec = new RSAPublicKeySpec(n, e);99this.privKeySpec = new RSAPrivateKeySpec(n, d);100this.testVectors = testVectors;101}102103/*104* Read a data file into an ArrayList.105* This function will exit the program if reading the file fails106* or if the file is not in the expected format.107*/108public static List<SigRecord> read(String filename)109throws IOException {110111List<SigRecord> data = new ArrayList<>();112try (BufferedReader br = new BufferedReader(113new InputStreamReader(new FileInputStream(114TEST_SRC + File.separator + filename)))) {115String line;116String mod = null;117String pubExp = null;118String privExp = null;119List<SigVector> testVectors = new ArrayList<>();120while ((line = br.readLine()) != null) {121if (line.startsWith("n =")) {122mod = line.split("=")[1].trim();123} else if (line.startsWith("e =")) {124pubExp = line.split("=")[1].trim();125} else if (line.startsWith("d =")) {126privExp = line.split("=")[1].trim();127128// now should start parsing for test vectors129String mdAlg = null;130String msg = null;131String sig = null;132boolean sigVectorDone = false;133while ((line = br.readLine()) != null) {134// we only care for lines starting with135// SHAALG, Msg, S136if (line.startsWith("SHAAlg =")) {137mdAlg = line.split(" = ")[1].trim();138} else if (line.startsWith("Msg =")) {139msg = line.split(" = ")[1].trim();140} else if (line.startsWith("S =")) {141sig = line.split(" = ")[1].trim();142} else if (line.startsWith("[mod")) {143sigVectorDone = true;144}145146if ((mdAlg != null) && (msg != null) && (sig != null)) {147// finish off current SigVector148testVectors.add(new SigVector(mdAlg, msg, sig));149mdAlg = msg = sig = null;150}151if (sigVectorDone) {152break;153}154}155// finish off current SigRecord and clear data for next SigRecord156data.add(new SigRecord(mod, pubExp, privExp, testVectors));157mod = pubExp = privExp = null;158testVectors = new ArrayList<>();159}160}161162if (data.isEmpty()) {163throw new RuntimeException("Nothing read from file "164+ filename);165}166}167return data;168}169170@Override171public String toString() {172return (id + ", " + testVectors.size() + " test vectors");173}174}175176177