Path: blob/master/src/java.base/share/classes/sun/security/rsa/PSSParameters.java
41159 views
/*1* Copyright (c) 2018, 2020, 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}104AlgorithmId params = AlgorithmId.parse(105new DerValue(val.getEncodedParams()));106String mgfDigestName = params.getName();107switch (mgfDigestName) {108case "SHA-1":109mgfSpec = MGF1ParameterSpec.SHA1;110break;111case "SHA-224":112mgfSpec = MGF1ParameterSpec.SHA224;113break;114case "SHA-256":115mgfSpec = MGF1ParameterSpec.SHA256;116break;117case "SHA-384":118mgfSpec = MGF1ParameterSpec.SHA384;119break;120case "SHA-512":121mgfSpec = MGF1ParameterSpec.SHA512;122break;123case "SHA-512/224":124mgfSpec = MGF1ParameterSpec.SHA512_224;125break;126case "SHA-512/256":127mgfSpec = MGF1ParameterSpec.SHA512_256;128break;129case "SHA3-224":130mgfSpec = MGF1ParameterSpec.SHA3_224;131break;132case "SHA3-256":133mgfSpec = MGF1ParameterSpec.SHA3_256;134break;135case "SHA3-384":136mgfSpec = MGF1ParameterSpec.SHA3_384;137break;138case "SHA3-512":139mgfSpec = MGF1ParameterSpec.SHA3_512;140break;141default:142throw new IOException143("Unrecognized message digest algorithm " +144mgfDigestName);145}146} else if (d.isContextSpecific((byte) 0x02)) {147// salt length148saltLength = d.data.getDerValue().getInteger();149if (saltLength < 0) {150throw new IOException("Negative value for saltLength");151}152} else if (d.isContextSpecific((byte) 0x03)) {153// trailer field154trailerField = d.data.getDerValue().getInteger();155if (trailerField != 1) {156throw new IOException("Unsupported trailerField value " +157trailerField);158}159} else {160throw new IOException("Invalid encoded PSSParameters");161}162}163164this.spec = new PSSParameterSpec(mdName, "MGF1", mgfSpec,165saltLength, trailerField);166}167168@Override169protected void engineInit(byte[] encoded, String decodingMethod)170throws IOException {171if ((decodingMethod != null) &&172(!decodingMethod.equalsIgnoreCase("ASN.1"))) {173throw new IllegalArgumentException("Only support ASN.1 format");174}175engineInit(encoded);176}177178@Override179protected <T extends AlgorithmParameterSpec>180T engineGetParameterSpec(Class<T> paramSpec)181throws InvalidParameterSpecException {182if (PSSParameterSpec.class.isAssignableFrom(paramSpec)) {183return paramSpec.cast(spec);184} else {185throw new InvalidParameterSpecException186("Inappropriate parameter specification");187}188}189190@Override191protected byte[] engineGetEncoded() throws IOException {192return getEncoded(spec);193}194195@Override196protected byte[] engineGetEncoded(String encMethod) throws IOException {197if ((encMethod != null) &&198(!encMethod.equalsIgnoreCase("ASN.1"))) {199throw new IllegalArgumentException("Only support ASN.1 format");200}201return engineGetEncoded();202}203204@Override205protected String engineToString() {206return spec.toString();207}208209/**210* Returns the encoding of a {@link PSSParameterSpec} object. This method211* is used in this class and {@link AlgorithmId}.212*213* @param spec a {@code PSSParameterSpec} object214* @return its DER encoding215* @throws IOException if the name of a MessageDigest or MaskGenAlgorithm216* is unsupported217*/218public static byte[] getEncoded(PSSParameterSpec spec) throws IOException {219220AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();221if (!(mgfSpec instanceof MGF1ParameterSpec)) {222throw new IOException("Cannot encode " + mgfSpec);223}224225MGF1ParameterSpec mgf1Spec = (MGF1ParameterSpec)mgfSpec;226227DerOutputStream tmp = new DerOutputStream();228DerOutputStream tmp2, tmp3;229230// MD231AlgorithmId mdAlgId;232try {233mdAlgId = AlgorithmId.get(spec.getDigestAlgorithm());234} catch (NoSuchAlgorithmException nsae) {235throw new IOException("AlgorithmId " + spec.getDigestAlgorithm() +236" impl not found");237}238if (!mdAlgId.getOID().equals(AlgorithmId.SHA_oid)) {239tmp2 = new DerOutputStream();240mdAlgId.derEncode(tmp2);241tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0),242tmp2);243}244245// MGF246AlgorithmId mgfDigestId;247try {248mgfDigestId = AlgorithmId.get(mgf1Spec.getDigestAlgorithm());249} catch (NoSuchAlgorithmException nase) {250throw new IOException("AlgorithmId " +251mgf1Spec.getDigestAlgorithm() + " impl not found");252}253254if (!mgfDigestId.getOID().equals(AlgorithmId.SHA_oid)) {255tmp2 = new DerOutputStream();256tmp2.putOID(AlgorithmId.MGF1_oid);257mgfDigestId.encode(tmp2);258tmp3 = new DerOutputStream();259tmp3.write(DerValue.tag_Sequence, tmp2);260tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 1),261tmp3);262}263264// SaltLength265if (spec.getSaltLength() != 20) {266tmp2 = new DerOutputStream();267tmp2.putInteger(spec.getSaltLength());268tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 2),269tmp2);270}271272// TrailerField273if (spec.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {274tmp2 = new DerOutputStream();275tmp2.putInteger(spec.getTrailerField());276tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 3),277tmp2);278}279280// Put all together under a SEQUENCE tag281DerOutputStream out = new DerOutputStream();282out.write(DerValue.tag_Sequence, tmp);283return out.toByteArray();284}285}286287288