/* * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.crypto.provider; import java.math.BigInteger; import java.io.*; import sun.security.util.*; import sun.security.x509.*; import java.security.AlgorithmParametersSpi; import java.security.NoSuchAlgorithmException; import java.security.spec.AlgorithmParameterSpec; import java.security.spec.InvalidParameterSpecException; import java.security.spec.MGF1ParameterSpec; import javax.crypto.spec.PSource; import javax.crypto.spec.OAEPParameterSpec; /** * This class implements the OAEP parameters used with the RSA * algorithm in OAEP padding. Here is its ASN.1 definition: *
* RSAES-OAEP-params ::= SEQUENCE { * hashAlgorithm [0] HashAlgorithm DEFAULT sha1, * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1, * pSourceAlgorithm [2] PSourceAlgorithm DEFAULT pSpecifiedEmpty * } ** * @author Valerie Peng */ public final class OAEPParameters extends AlgorithmParametersSpi { private String mdName; private MGF1ParameterSpec mgfSpec; private byte[] p; private static ObjectIdentifier OID_MGF1 = ObjectIdentifier.of(KnownOIDs.MGF1); private static ObjectIdentifier OID_PSpecified = ObjectIdentifier.of(KnownOIDs.PSpecified); public OAEPParameters() { } protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException { if (!(paramSpec instanceof OAEPParameterSpec)) { throw new InvalidParameterSpecException ("Inappropriate parameter specification"); } OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec; mdName = spec.getDigestAlgorithm(); String mgfName = spec.getMGFAlgorithm(); if (!mgfName.equalsIgnoreCase("MGF1")) { throw new InvalidParameterSpecException("Unsupported mgf " + mgfName + "; MGF1 only"); } AlgorithmParameterSpec mgfSpec = spec.getMGFParameters(); if (!(mgfSpec instanceof MGF1ParameterSpec)) { throw new InvalidParameterSpecException("Inappropriate mgf " + "parameters; non-null MGF1ParameterSpec only"); } this.mgfSpec = (MGF1ParameterSpec) mgfSpec; PSource pSrc = spec.getPSource(); if (pSrc.getAlgorithm().equals("PSpecified")) { p = ((PSource.PSpecified) pSrc).getValue(); } else { throw new InvalidParameterSpecException("Unsupported pSource " + pSrc.getAlgorithm() + "; PSpecified only"); } } protected void engineInit(byte[] encoded) throws IOException { DerInputStream der = DerValue.wrap(encoded).data(); var sub = der.getOptionalExplicitContextSpecific(0); if (sub.isPresent()) { mdName = AlgorithmId.parse(sub.get()).getName(); } else { mdName = "SHA-1"; } sub = der.getOptionalExplicitContextSpecific(1); if (sub.isPresent()) { AlgorithmId val = AlgorithmId.parse(sub.get()); if (!val.getOID().equals(OID_MGF1)) { throw new IOException("Only MGF1 mgf is supported"); } AlgorithmId params = AlgorithmId.parse( new DerValue(val.getEncodedParams())); mgfSpec = switch (params.getName()) { case "SHA-1" -> MGF1ParameterSpec.SHA1; case "SHA-224" -> MGF1ParameterSpec.SHA224; case "SHA-256" -> MGF1ParameterSpec.SHA256; case "SHA-384" -> MGF1ParameterSpec.SHA384; case "SHA-512" -> MGF1ParameterSpec.SHA512; case "SHA-512/224" -> MGF1ParameterSpec.SHA512_224; case "SHA-512/256" -> MGF1ParameterSpec.SHA512_256; default -> throw new IOException( "Unrecognized message digest algorithm"); }; } else { mgfSpec = MGF1ParameterSpec.SHA1; } sub = der.getOptionalExplicitContextSpecific(2); if (sub.isPresent()) { AlgorithmId val = AlgorithmId.parse(sub.get()); if (!val.getOID().equals(OID_PSpecified)) { throw new IOException("Wrong OID for pSpecified"); } p = DerValue.wrap(val.getEncodedParams()).getOctetString(); } else { p = new byte[0]; } der.atEnd(); } protected void engineInit(byte[] encoded, String decodingMethod) throws IOException { if ((decodingMethod != null) && (!decodingMethod.equalsIgnoreCase("ASN.1"))) { throw new IllegalArgumentException("Only support ASN.1 format"); } engineInit(encoded); } protected