Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/rsa/PSSParameters.java
38830 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;129default:130throw new IOException131("Unrecognized message digest algorithm " +132mgfDigestName);133}134} else if (d.isContextSpecific((byte) 0x02)) {135// salt length136saltLength = d.data.getDerValue().getInteger();137if (saltLength < 0) {138throw new IOException("Negative value for saltLength");139}140} else if (d.isContextSpecific((byte) 0x03)) {141// trailer field142trailerField = d.data.getDerValue().getInteger();143if (trailerField != 1) {144throw new IOException("Unsupported trailerField value " +145trailerField);146}147} else {148throw new IOException("Invalid encoded PSSParameters");149}150}151152this.spec = new PSSParameterSpec(mdName, "MGF1", mgfSpec,153saltLength, trailerField);154}155156@Override157protected void engineInit(byte[] encoded, String decodingMethod)158throws IOException {159if ((decodingMethod != null) &&160(!decodingMethod.equalsIgnoreCase("ASN.1"))) {161throw new IllegalArgumentException("Only support ASN.1 format");162}163engineInit(encoded);164}165166@Override167protected <T extends AlgorithmParameterSpec>168T engineGetParameterSpec(Class<T> paramSpec)169throws InvalidParameterSpecException {170if (PSSParameterSpec.class.isAssignableFrom(paramSpec)) {171return paramSpec.cast(spec);172} else {173throw new InvalidParameterSpecException174("Inappropriate parameter specification");175}176}177178@Override179protected byte[] engineGetEncoded() throws IOException {180return getEncoded(spec);181}182183@Override184protected byte[] engineGetEncoded(String encMethod) throws IOException {185if ((encMethod != null) &&186(!encMethod.equalsIgnoreCase("ASN.1"))) {187throw new IllegalArgumentException("Only support ASN.1 format");188}189return engineGetEncoded();190}191192@Override193protected String engineToString() {194return spec.toString();195}196197/**198* Returns the encoding of a {@link PSSParameterSpec} object. This method199* is used in this class and {@link AlgorithmId}.200*201* @param spec a {@code PSSParameterSpec} object202* @return its DER encoding203* @throws IOException if the name of a MessageDigest or MaskGenAlgorithm204* is unsupported205*/206public static byte[] getEncoded(PSSParameterSpec spec) throws IOException {207208AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();209if (!(mgfSpec instanceof MGF1ParameterSpec)) {210throw new IOException("Cannot encode " + mgfSpec);211}212213MGF1ParameterSpec mgf1Spec = (MGF1ParameterSpec)mgfSpec;214215DerOutputStream tmp = new DerOutputStream();216DerOutputStream tmp2, tmp3;217218// MD219AlgorithmId mdAlgId;220try {221mdAlgId = AlgorithmId.get(spec.getDigestAlgorithm());222} catch (NoSuchAlgorithmException nsae) {223throw new IOException("AlgorithmId " + spec.getDigestAlgorithm() +224" impl not found");225}226if (!mdAlgId.getOID().equals(AlgorithmId.SHA_oid)) {227tmp2 = new DerOutputStream();228mdAlgId.derEncode(tmp2);229tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0),230tmp2);231}232233// MGF234AlgorithmId mgfDigestId;235try {236mgfDigestId = AlgorithmId.get(mgf1Spec.getDigestAlgorithm());237} catch (NoSuchAlgorithmException nase) {238throw new IOException("AlgorithmId " +239mgf1Spec.getDigestAlgorithm() + " impl not found");240}241242if (!mgfDigestId.getOID().equals(AlgorithmId.SHA_oid)) {243tmp2 = new DerOutputStream();244tmp2.putOID(AlgorithmId.mgf1_oid);245mgfDigestId.encode(tmp2);246tmp3 = new DerOutputStream();247tmp3.write(DerValue.tag_Sequence, tmp2);248tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 1),249tmp3);250}251252// SaltLength253if (spec.getSaltLength() != 20) {254tmp2 = new DerOutputStream();255tmp2.putInteger(spec.getSaltLength());256tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 2),257tmp2);258}259260// TrailerField261if (spec.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {262tmp2 = new DerOutputStream();263tmp2.putInteger(spec.getTrailerField());264tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 3),265tmp2);266}267268// Put all together under a SEQUENCE tag269DerOutputStream out = new DerOutputStream();270out.write(DerValue.tag_Sequence, tmp);271return out.toByteArray();272}273}274275276