Path: blob/master/src/java.base/share/classes/sun/security/rsa/PSSParameters.java
67760 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.rsa;2627import java.io.*;28import sun.security.util.*;29import sun.security.x509.*;30import java.security.AlgorithmParametersSpi;31import java.security.NoSuchAlgorithmException;32import java.security.spec.AlgorithmParameterSpec;33import java.security.spec.InvalidParameterSpecException;34import java.security.spec.MGF1ParameterSpec;35import java.security.spec.PSSParameterSpec;36import static java.security.spec.PSSParameterSpec.DEFAULT;3738/**39* This class implements the PSS parameters used with the RSA40* signatures in PSS padding. Here is its ASN.1 definition:41* RSASSA-PSS-params ::= SEQUENCE {42* hashAlgorithm [0] HashAlgorithm DEFAULT sha1,43* maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1,44* saltLength [2] INTEGER DEFAULT 2045* trailerField [3] TrailerField DEFAULT trailerFieldBC46* }47*48* @author Valerie Peng49*50*/5152public final class PSSParameters extends AlgorithmParametersSpi {5354private PSSParameterSpec spec;5556public PSSParameters() {57}5859@Override60protected void engineInit(AlgorithmParameterSpec paramSpec)61throws InvalidParameterSpecException {62if (!(paramSpec instanceof PSSParameterSpec)) {63throw new InvalidParameterSpecException64("Inappropriate parameter specification");65}66PSSParameterSpec spec = (PSSParameterSpec) paramSpec;6768String mgfName = spec.getMGFAlgorithm();69if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1")) {70throw new InvalidParameterSpecException("Unsupported mgf " +71mgfName + "; MGF1 only");72}73AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();74if (!(mgfSpec instanceof MGF1ParameterSpec)) {75throw new InvalidParameterSpecException("Inappropriate mgf " +76"parameters; non-null MGF1ParameterSpec only");77}78this.spec = spec;79}8081@Override82protected void engineInit(byte[] encoded) throws IOException {83// first initialize with the DEFAULT values before84// retrieving from the encoding bytes85String mdName = DEFAULT.getDigestAlgorithm();86MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec) DEFAULT.getMGFParameters();87int saltLength = DEFAULT.getSaltLength();88int trailerField = DEFAULT.getTrailerField();8990DerInputStream der = new DerInputStream(encoded);91DerValue[] datum = der.getSequence(4);9293for (DerValue d : datum) {94if (d.isContextSpecific((byte) 0x00)) {95// hash algid96mdName = AlgorithmId.parse97(d.data.getDerValue()).getName();98} else if (d.isContextSpecific((byte) 0x01)) {99// mgf algid100AlgorithmId val = AlgorithmId.parse(d.data.getDerValue());101if (!val.getOID().equals(AlgorithmId.MGF1_oid)) {102throw new IOException("Only MGF1 mgf is supported");103}104105byte[] encodedParams = val.getEncodedParams();106if (encodedParams == null) {107throw new IOException("Missing MGF1 parameters");108}109AlgorithmId params = AlgorithmId.parse(110new DerValue(encodedParams));111String mgfDigestName = params.getName();112switch (mgfDigestName) {113case "SHA-1":114mgfSpec = MGF1ParameterSpec.SHA1;115break;116case "SHA-224":117mgfSpec = MGF1ParameterSpec.SHA224;118break;119case "SHA-256":120mgfSpec = MGF1ParameterSpec.SHA256;121break;122case "SHA-384":123mgfSpec = MGF1ParameterSpec.SHA384;124break;125case "SHA-512":126mgfSpec = MGF1ParameterSpec.SHA512;127break;128case "SHA-512/224":129mgfSpec = MGF1ParameterSpec.SHA512_224;130break;131case "SHA-512/256":132mgfSpec = MGF1ParameterSpec.SHA512_256;133break;134case "SHA3-224":135mgfSpec = MGF1ParameterSpec.SHA3_224;136break;137case "SHA3-256":138mgfSpec = MGF1ParameterSpec.SHA3_256;139break;140case "SHA3-384":141mgfSpec = MGF1ParameterSpec.SHA3_384;142break;143case "SHA3-512":144mgfSpec = MGF1ParameterSpec.SHA3_512;145break;146default:147throw new IOException148("Unrecognized message digest algorithm " +149mgfDigestName);150}151} else if (d.isContextSpecific((byte) 0x02)) {152// salt length153saltLength = d.data.getDerValue().getInteger();154if (saltLength < 0) {155throw new IOException("Negative value for saltLength");156}157} else if (d.isContextSpecific((byte) 0x03)) {158// trailer field159trailerField = d.data.getDerValue().getInteger();160if (trailerField != 1) {161throw new IOException("Unsupported trailerField value " +162trailerField);163}164} else {165throw new IOException("Invalid encoded PSSParameters");166}167}168169this.spec = new PSSParameterSpec(mdName, "MGF1", mgfSpec,170saltLength, trailerField);171}172173@Override174protected void engineInit(byte[] encoded, String decodingMethod)175throws IOException {176if ((decodingMethod != null) &&177(!decodingMethod.equalsIgnoreCase("ASN.1"))) {178throw new IllegalArgumentException("Only support ASN.1 format");179}180engineInit(encoded);181}182183@Override184protected <T extends AlgorithmParameterSpec>185T engineGetParameterSpec(Class<T> paramSpec)186throws InvalidParameterSpecException {187if (PSSParameterSpec.class.isAssignableFrom(paramSpec)) {188return paramSpec.cast(spec);189} else {190throw new InvalidParameterSpecException191("Inappropriate parameter specification");192}193}194195@Override196protected byte[] engineGetEncoded() throws IOException {197return getEncoded(spec);198}199200@Override201protected byte[] engineGetEncoded(String encMethod) throws IOException {202if ((encMethod != null) &&203(!encMethod.equalsIgnoreCase("ASN.1"))) {204throw new IllegalArgumentException("Only support ASN.1 format");205}206return engineGetEncoded();207}208209@Override210protected String engineToString() {211return spec.toString();212}213214/**215* Returns the encoding of a {@link PSSParameterSpec} object. This method216* is used in this class and {@link AlgorithmId}.217*218* @param spec a {@code PSSParameterSpec} object219* @return its DER encoding220* @throws IOException if the name of a MessageDigest or MaskGenAlgorithm221* is unsupported222*/223public static byte[] getEncoded(PSSParameterSpec spec) throws IOException {224225AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();226if (!(mgfSpec instanceof MGF1ParameterSpec)) {227throw new IOException("Cannot encode " + mgfSpec);228}229230MGF1ParameterSpec mgf1Spec = (MGF1ParameterSpec)mgfSpec;231232DerOutputStream tmp = new DerOutputStream();233DerOutputStream tmp2, tmp3;234235// MD236AlgorithmId mdAlgId;237try {238mdAlgId = AlgorithmId.get(spec.getDigestAlgorithm());239} catch (NoSuchAlgorithmException nsae) {240throw new IOException("AlgorithmId " + spec.getDigestAlgorithm() +241" impl not found");242}243if (!mdAlgId.getOID().equals(AlgorithmId.SHA_oid)) {244tmp2 = new DerOutputStream();245mdAlgId.derEncode(tmp2);246tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0),247tmp2);248}249250// MGF251AlgorithmId mgfDigestId;252try {253mgfDigestId = AlgorithmId.get(mgf1Spec.getDigestAlgorithm());254} catch (NoSuchAlgorithmException nase) {255throw new IOException("AlgorithmId " +256mgf1Spec.getDigestAlgorithm() + " impl not found");257}258259if (!mgfDigestId.getOID().equals(AlgorithmId.SHA_oid)) {260tmp2 = new DerOutputStream();261tmp2.putOID(AlgorithmId.MGF1_oid);262mgfDigestId.encode(tmp2);263tmp3 = new DerOutputStream();264tmp3.write(DerValue.tag_Sequence, tmp2);265tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 1),266tmp3);267}268269// SaltLength270if (spec.getSaltLength() != 20) {271tmp2 = new DerOutputStream();272tmp2.putInteger(spec.getSaltLength());273tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 2),274tmp2);275}276277// TrailerField278if (spec.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {279tmp2 = new DerOutputStream();280tmp2.putInteger(spec.getTrailerField());281tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 3),282tmp2);283}284285// Put all together under a SEQUENCE tag286DerOutputStream out = new DerOutputStream();287out.write(DerValue.tag_Sequence, tmp);288return out.toByteArray();289}290}291292293