/* * Copyright (c) 2003, 2012, 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.util.*; 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; private static ObjectIdentifier OID_PSpecified; static { try { OID_MGF1 = new ObjectIdentifier(new int[] {1,2,840,113549,1,1,8}); } catch (IOException ioe) { // should not happen OID_MGF1 = null; } try { OID_PSpecified = new ObjectIdentifier(new int[] {1,2,840,113549,1,1,9}); } catch (IOException ioe) { // should not happen OID_PSpecified = null; } } 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 = new DerInputStream(encoded); mdName = "SHA-1"; mgfSpec = MGF1ParameterSpec.SHA1; p = new byte[0]; DerValue[] datum = der.getSequence(3); for (int i=0; i