jdk/src/share/classes/com/sun/crypto/provider/OAEPParameters.java
author xuelei
Mon, 01 Nov 2010 22:02:35 -0700
changeset 7043 5e2d1edeb2c7
parent 5506 202f599c92aa
child 10336 0bb1999251f8
permissions -rw-r--r--
6916074: Add support for TLS 1.2 6985179: To support Server Name Indication extension for JSSE client Summary: Introduces the algorithm constraints to support signature and hash algorithm selection. Includes contributions from wetmore and weijung. Reviewed-by: wetmore, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, 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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AlgorithmParametersSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.spec.MGF1ParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.spec.PSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.crypto.spec.OAEPParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class implements the OAEP parameters used with the RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * algorithm in OAEP padding. Here is its ASN.1 definition:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * RSAES-OAEP-params ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   hashAlgorithm      [0] HashAlgorithm     DEFAULT sha1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   maskGenAlgorithm   [1] MaskGenAlgorithm  DEFAULT mgf1SHA1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   pSourceAlgorithm   [2] PSourceAlgorithm  DEFAULT pSpecifiedEmpty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public final class OAEPParameters extends AlgorithmParametersSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private String mdName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private MGF1ParameterSpec mgfSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private byte[] p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static ObjectIdentifier OID_MGF1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static ObjectIdentifier OID_PSpecified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            OID_MGF1 = new ObjectIdentifier(new int[] {1,2,840,113549,1,1,8});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            OID_MGF1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            OID_PSpecified =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                new ObjectIdentifier(new int[] {1,2,840,113549,1,1,9});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            OID_PSpecified = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public OAEPParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected void engineInit(AlgorithmParameterSpec paramSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (!(paramSpec instanceof OAEPParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                ("Inappropriate parameter specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        OAEPParameterSpec spec = (OAEPParameterSpec) paramSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        mdName = spec.getDigestAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        String mgfName = spec.getMGFAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (!mgfName.equalsIgnoreCase("MGF1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new InvalidParameterSpecException("Unsupported mgf " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                mgfName + "; MGF1 only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (!(mgfSpec instanceof MGF1ParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new InvalidParameterSpecException("Inappropriate mgf " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                "parameters; non-null MGF1ParameterSpec only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.mgfSpec = (MGF1ParameterSpec) mgfSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        PSource pSrc = spec.getPSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (pSrc.getAlgorithm().equals("PSpecified")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            p = ((PSource.PSpecified) pSrc).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new InvalidParameterSpecException("Unsupported pSource " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                pSrc.getAlgorithm() + "; PSpecified only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static String convertToStandardName(String internalName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (internalName.equals("SHA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return "SHA-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else if (internalName.equals("SHA256")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return "SHA-256";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else if (internalName.equals("SHA384")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return "SHA-384";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else if (internalName.equals("SHA512")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return "SHA-512";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return internalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    protected void engineInit(byte[] encoded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        DerInputStream der = new DerInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        mdName = "SHA-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        mgfSpec = MGF1ParameterSpec.SHA1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        p = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        DerValue[] datum = der.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (int i=0; i<datum.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            DerValue data = datum[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (data.isContextSpecific((byte) 0x00)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                // hash algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                mdName = convertToStandardName(AlgorithmId.parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    (data.data.getDerValue()).getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } else if (data.isContextSpecific((byte) 0x01)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                // mgf algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                if (!val.getOID().equals((Object) OID_MGF1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    throw new IOException("Only MGF1 mgf is supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   142
                AlgorithmId params = AlgorithmId.parse(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   143
                    new DerValue(val.getEncodedParams()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                String mgfDigestName = convertToStandardName(params.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if (mgfDigestName.equals("SHA-1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    mgfSpec = MGF1ParameterSpec.SHA1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                } else if (mgfDigestName.equals("SHA-256")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    mgfSpec = MGF1ParameterSpec.SHA256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                } else if (mgfDigestName.equals("SHA-384")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    mgfSpec = MGF1ParameterSpec.SHA384;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                } else if (mgfDigestName.equals("SHA-512")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    mgfSpec = MGF1ParameterSpec.SHA512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   154
                    throw new IOException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   155
                        "Unrecognized message digest algorithm");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } else if (data.isContextSpecific((byte) 0x02)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                // pSource algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (!val.getOID().equals((Object) OID_PSpecified)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    throw new IOException("Wrong OID for pSpecified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                DerInputStream dis = new DerInputStream(val.getEncodedParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                p = dis.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                if (dis.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    throw new IOException("Extra data for pSpecified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw new IOException("Invalid encoded OAEPParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected void engineInit(byte[] encoded, String decodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if ((decodingMethod != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            (!decodingMethod.equalsIgnoreCase("ASN.1"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new IllegalArgumentException("Only support ASN.1 format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        engineInit(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (OAEPParameterSpec.class.isAssignableFrom(paramSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return new OAEPParameterSpec(mdName, "MGF1", mgfSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                new PSource.PSpecified(p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                ("Inappropriate parameter specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    protected byte[] engineGetEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        DerOutputStream tmp2, tmp3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // MD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        AlgorithmId mdAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            mdAlgId = AlgorithmId.get(mdName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new IOException("AlgorithmId " + mdName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                  " impl not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        tmp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        mdAlgId.derEncode(tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                      tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // MGF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        tmp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        tmp2.putOID(OID_MGF1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        AlgorithmId mgfDigestId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            mgfDigestId = AlgorithmId.get(mgfSpec.getDigestAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } catch (NoSuchAlgorithmException nase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new IOException("AlgorithmId " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    mgfSpec.getDigestAlgorithm() + " impl not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        mgfDigestId.encode(tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        tmp3 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        tmp3.write(DerValue.tag_Sequence, tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                  tmp3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // PSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        tmp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        tmp2.putOID(OID_PSpecified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        tmp2.putOctetString(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        tmp3 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        tmp3.write(DerValue.tag_Sequence, tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                  tmp3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // Put all together under a SEQUENCE tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    protected byte[] engineGetEncoded(String encodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if ((encodingMethod != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            (!encodingMethod.equalsIgnoreCase("ASN.1"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw new IllegalArgumentException("Only support ASN.1 format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return engineGetEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    protected String engineToString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        sb.append("MD: " + mdName + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        sb.append("MGF: MGF1" + mgfSpec.getDigestAlgorithm() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        sb.append("PSource: PSpecified " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            (p.length==0? "":Debug.toHexString(new BigInteger(p))) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}