src/java.base/share/classes/com/sun/crypto/provider/OAEPParameters.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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
    protected void engineInit(byte[] encoded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        DerInputStream der = new DerInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        mdName = "SHA-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        mgfSpec = MGF1ParameterSpec.SHA1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        p = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        DerValue[] datum = der.getSequence(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for (int i=0; i<datum.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            DerValue data = datum[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (data.isContextSpecific((byte) 0x00)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                // hash algid
13361
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents: 12685
diff changeset
   119
                mdName = AlgorithmId.parse
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents: 12685
diff changeset
   120
                    (data.data.getDerValue()).getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } else if (data.isContextSpecific((byte) 0x01)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                // mgf algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 25859
diff changeset
   124
                if (!val.getOID().equals(OID_MGF1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    throw new IOException("Only MGF1 mgf is supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   127
                AlgorithmId params = AlgorithmId.parse(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   128
                    new DerValue(val.getEncodedParams()));
13361
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents: 12685
diff changeset
   129
                String mgfDigestName = params.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if (mgfDigestName.equals("SHA-1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    mgfSpec = MGF1ParameterSpec.SHA1;
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   132
                } else if (mgfDigestName.equals("SHA-224")) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   133
                    mgfSpec = MGF1ParameterSpec.SHA224;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                } else if (mgfDigestName.equals("SHA-256")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    mgfSpec = MGF1ParameterSpec.SHA256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                } else if (mgfDigestName.equals("SHA-384")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    mgfSpec = MGF1ParameterSpec.SHA384;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                } else if (mgfDigestName.equals("SHA-512")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    mgfSpec = MGF1ParameterSpec.SHA512;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   140
                } else if (mgfDigestName.equals("SHA-512/224")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   141
                    mgfSpec = MGF1ParameterSpec.SHA512_224;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   142
                } else if (mgfDigestName.equals("SHA-512/256")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   143
                    mgfSpec = MGF1ParameterSpec.SHA512_256;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   145
                    throw new IOException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   146
                        "Unrecognized message digest algorithm");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } else if (data.isContextSpecific((byte) 0x02)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // pSource algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                AlgorithmId val = AlgorithmId.parse(data.data.getDerValue());
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 25859
diff changeset
   151
                if (!val.getOID().equals(OID_PSpecified)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    throw new IOException("Wrong OID for pSpecified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                DerInputStream dis = new DerInputStream(val.getEncodedParams());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                p = dis.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (dis.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    throw new IOException("Extra data for pSpecified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                throw new IOException("Invalid encoded OAEPParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected void engineInit(byte[] encoded, String decodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if ((decodingMethod != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            (!decodingMethod.equalsIgnoreCase("ASN.1"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new IllegalArgumentException("Only support ASN.1 format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        engineInit(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   174
    protected <T extends AlgorithmParameterSpec>
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   175
        T engineGetParameterSpec(Class<T> paramSpec)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (OAEPParameterSpec.class.isAssignableFrom(paramSpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   178
            return paramSpec.cast(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   179
                new OAEPParameterSpec(mdName, "MGF1", mgfSpec,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   180
                                      new PSource.PSpecified(p)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                ("Inappropriate parameter specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected byte[] engineGetEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        DerOutputStream tmp2, tmp3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // MD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        AlgorithmId mdAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            mdAlgId = AlgorithmId.get(mdName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new IOException("AlgorithmId " + mdName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                  " impl not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        tmp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        mdAlgId.derEncode(tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                      tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // MGF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        tmp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        tmp2.putOID(OID_MGF1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        AlgorithmId mgfDigestId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            mgfDigestId = AlgorithmId.get(mgfSpec.getDigestAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } catch (NoSuchAlgorithmException nase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throw new IOException("AlgorithmId " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    mgfSpec.getDigestAlgorithm() + " impl not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        mgfDigestId.encode(tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        tmp3 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        tmp3.write(DerValue.tag_Sequence, tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                  tmp3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // PSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        tmp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        tmp2.putOID(OID_PSpecified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        tmp2.putOctetString(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        tmp3 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        tmp3.write(DerValue.tag_Sequence, tmp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                  tmp3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // Put all together under a SEQUENCE tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        out.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    protected byte[] engineGetEncoded(String encodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if ((encodingMethod != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            (!encodingMethod.equalsIgnoreCase("ASN.1"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throw new IllegalArgumentException("Only support ASN.1 format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return engineGetEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    protected String engineToString() {
24578
bbf15045dfbb 8043342: Replace uses of StringBuffer with StringBuilder within crypto code
wetmore
parents: 13361
diff changeset
   245
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        sb.append("MD: " + mdName + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        sb.append("MGF: MGF1" + mgfSpec.getDigestAlgorithm() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        sb.append("PSource: PSpecified " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            (p.length==0? "":Debug.toHexString(new BigInteger(p))) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
}