jdk/src/share/classes/com/sun/crypto/provider/OAEPParameters.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 10336 0bb1999251f8
child 13361 bda5c2354fc6
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AlgorithmParametersSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.MGF1ParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.spec.PSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.spec.OAEPParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class implements the OAEP parameters used with the RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * algorithm in OAEP padding. Here is its ASN.1 definition:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * RSAES-OAEP-params ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   hashAlgorithm      [0] HashAlgorithm     DEFAULT sha1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   maskGenAlgorithm   [1] MaskGenAlgorithm  DEFAULT mgf1SHA1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   pSourceAlgorithm   [2] PSourceAlgorithm  DEFAULT pSpecifiedEmpty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public final class OAEPParameters extends AlgorithmParametersSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private String mdName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private MGF1ParameterSpec mgfSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private byte[] p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static ObjectIdentifier OID_MGF1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static ObjectIdentifier OID_PSpecified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            OID_MGF1 = new ObjectIdentifier(new int[] {1,2,840,113549,1,1,8});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            OID_MGF1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            OID_PSpecified =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                new ObjectIdentifier(new int[] {1,2,840,113549,1,1,9});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            OID_PSpecified = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public OAEPParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected void engineInit(AlgorithmParameterSpec paramSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (!(paramSpec instanceof OAEPParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                ("Inappropriate parameter specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        mdName = spec.getDigestAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String mgfName = spec.getMGFAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (!mgfName.equalsIgnoreCase("MGF1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new InvalidParameterSpecException("Unsupported mgf " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                mgfName + "; MGF1 only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (!(mgfSpec instanceof MGF1ParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new InvalidParameterSpecException("Inappropriate mgf " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                "parameters; non-null MGF1ParameterSpec only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.mgfSpec = (MGF1ParameterSpec) mgfSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        PSource pSrc = spec.getPSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (pSrc.getAlgorithm().equals("PSpecified")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            p = ((PSource.PSpecified) pSrc).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new InvalidParameterSpecException("Unsupported pSource " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                pSrc.getAlgorithm() + "; PSpecified only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static String convertToStandardName(String internalName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (internalName.equals("SHA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return "SHA-1";
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   111
        } else if (internalName.equals("SHA224")) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   112
            return "SHA-224";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } else if (internalName.equals("SHA256")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return "SHA-256";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } else if (internalName.equals("SHA384")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return "SHA-384";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else if (internalName.equals("SHA512")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return "SHA-512";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return internalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected void engineInit(byte[] encoded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        DerInputStream der = new DerInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        mdName = "SHA-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        mgfSpec = MGF1ParameterSpec.SHA1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        p = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        DerValue[] datum = der.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        for (int i=0; i<datum.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            DerValue data = datum[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (data.isContextSpecific((byte) 0x00)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // hash algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                mdName = convertToStandardName(AlgorithmId.parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    (data.data.getDerValue()).getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else if (data.isContextSpecific((byte) 0x01)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                // mgf algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if (!val.getOID().equals((Object) OID_MGF1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    throw new IOException("Only MGF1 mgf is supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   143
                AlgorithmId params = AlgorithmId.parse(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   144
                    new DerValue(val.getEncodedParams()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                String mgfDigestName = convertToStandardName(params.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (mgfDigestName.equals("SHA-1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    mgfSpec = MGF1ParameterSpec.SHA1;
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   148
                } else if (mgfDigestName.equals("SHA-224")) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   149
                    mgfSpec = MGF1ParameterSpec.SHA224;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                } else if (mgfDigestName.equals("SHA-256")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    mgfSpec = MGF1ParameterSpec.SHA256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                } else if (mgfDigestName.equals("SHA-384")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    mgfSpec = MGF1ParameterSpec.SHA384;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                } else if (mgfDigestName.equals("SHA-512")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    mgfSpec = MGF1ParameterSpec.SHA512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   157
                    throw new IOException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   158
                        "Unrecognized message digest algorithm");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } else if (data.isContextSpecific((byte) 0x02)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // pSource algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (!val.getOID().equals((Object) OID_PSpecified)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    throw new IOException("Wrong OID for pSpecified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                DerInputStream dis = new DerInputStream(val.getEncodedParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                p = dis.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (dis.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    throw new IOException("Extra data for pSpecified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                throw new IOException("Invalid encoded OAEPParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected void engineInit(byte[] encoded, String decodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if ((decodingMethod != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            (!decodingMethod.equalsIgnoreCase("ASN.1"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new IllegalArgumentException("Only support ASN.1 format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        engineInit(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   186
    protected <T extends AlgorithmParameterSpec>
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   187
        T engineGetParameterSpec(Class<T> paramSpec)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (OAEPParameterSpec.class.isAssignableFrom(paramSpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   190
            return paramSpec.cast(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   191
                new OAEPParameterSpec(mdName, "MGF1", mgfSpec,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   192
                                      new PSource.PSpecified(p)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                ("Inappropriate parameter specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected byte[] engineGetEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        DerOutputStream tmp2, tmp3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // MD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        AlgorithmId mdAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            mdAlgId = AlgorithmId.get(mdName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throw new IOException("AlgorithmId " + mdName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                  " impl not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        tmp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        mdAlgId.derEncode(tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                      tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // MGF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        tmp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        tmp2.putOID(OID_MGF1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        AlgorithmId mgfDigestId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            mgfDigestId = AlgorithmId.get(mgfSpec.getDigestAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } catch (NoSuchAlgorithmException nase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new IOException("AlgorithmId " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    mgfSpec.getDigestAlgorithm() + " impl not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        mgfDigestId.encode(tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        tmp3 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        tmp3.write(DerValue.tag_Sequence, tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                  tmp3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // PSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        tmp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        tmp2.putOID(OID_PSpecified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        tmp2.putOctetString(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        tmp3 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        tmp3.write(DerValue.tag_Sequence, tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                  tmp3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // Put all together under a SEQUENCE tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    protected byte[] engineGetEncoded(String encodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if ((encodingMethod != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            (!encodingMethod.equalsIgnoreCase("ASN.1"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new IllegalArgumentException("Only support ASN.1 format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return engineGetEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    protected String engineToString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        sb.append("MD: " + mdName + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        sb.append("MGF: MGF1" + mgfSpec.getDigestAlgorithm() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        sb.append("PSource: PSpecified " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            (p.length==0? "":Debug.toHexString(new BigInteger(p))) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}