src/jdk.crypto.ucrypto/solaris/classes/com/oracle/security/ucrypto/NativeRSACipher.java
author eosterlund
Mon, 29 Jul 2019 13:57:54 +0200
changeset 57582 a79a819a8218
parent 47216 71c04702a3d5
permissions -rw-r--r--
8227407: ZGC: C2 loads and load barriers can get separated by safepoints Reviewed-by: neliasso, smonteith, roland
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     1
/*
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     4
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    10
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    15
 * accompanied this code).
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    16
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    20
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    23
 * questions.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    24
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    25
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    26
package com.oracle.security.ucrypto;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    27
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    28
import java.util.Arrays;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    29
import java.util.WeakHashMap;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    30
import java.util.Collections;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    31
import java.util.Map;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    32
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    33
import java.security.AlgorithmParameters;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    34
import java.security.InvalidAlgorithmParameterException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    35
import java.security.InvalidKeyException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    36
import java.security.Key;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    37
import java.security.PublicKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    38
import java.security.PrivateKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    39
import java.security.spec.RSAPrivateCrtKeySpec;
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
    40
import java.security.spec.RSAPrivateKeySpec;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    41
import java.security.spec.RSAPublicKeySpec;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    42
import java.security.interfaces.RSAKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    43
import java.security.interfaces.RSAPrivateCrtKey;
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
    44
import java.security.interfaces.RSAPrivateKey;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    45
import java.security.interfaces.RSAPublicKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    46
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    47
import java.security.KeyFactory;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    48
import java.security.NoSuchAlgorithmException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    49
import java.security.SecureRandom;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    50
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    51
import java.security.spec.AlgorithmParameterSpec;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    52
import java.security.spec.InvalidParameterSpecException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    53
import java.security.spec.InvalidKeySpecException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    54
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    55
import javax.crypto.BadPaddingException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    56
import javax.crypto.Cipher;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    57
import javax.crypto.CipherSpi;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    58
import javax.crypto.SecretKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    59
import javax.crypto.IllegalBlockSizeException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    60
import javax.crypto.NoSuchPaddingException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    61
import javax.crypto.ShortBufferException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    62
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    63
import javax.crypto.spec.SecretKeySpec;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    64
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    65
import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
35287
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 32646
diff changeset
    66
import sun.security.jca.JCAUtil;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    67
import sun.security.util.KeyUtil;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    68
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    69
/**
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    70
 * Asymmetric Cipher wrapper class utilizing ucrypto APIs. This class
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    71
 * currently supports
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    72
 * - RSA/ECB/NOPADDING
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    73
 * - RSA/ECB/PKCS1PADDING
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    74
 *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 35287
diff changeset
    75
 * @since 9
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    76
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    77
public class NativeRSACipher extends CipherSpi {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    78
    // fields set in constructor
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    79
    private final UcryptoMech mech;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    80
    private final int padLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    81
    private final NativeRSAKeyFactory keyFactory;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    82
    private AlgorithmParameterSpec spec;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    83
    private SecureRandom random;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    84
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    85
    // Keep a cache of RSA keys and their RSA NativeKey for reuse.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    86
    // When the RSA key is gc'ed, we let NativeKey phatom references cleanup
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    87
    // the native allocation
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    88
    private static final Map<Key, NativeKey> keyList =
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    89
            Collections.synchronizedMap(new WeakHashMap<Key, NativeKey>());
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    90
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    91
    //
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    92
    // fields (re)set in every init()
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    93
    //
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    94
    private NativeKey key = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    95
    private int outputSize = 0; // e.g. modulus size in bytes
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    96
    private boolean encrypt = true;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    97
    private byte[] buffer;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    98
    private int bufOfs = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    99
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   100
    // public implementation classes
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   101
    public static final class NoPadding extends NativeRSACipher {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   102
        public NoPadding() throws NoSuchAlgorithmException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   103
            super(UcryptoMech.CRYPTO_RSA_X_509, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   104
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   105
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   106
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   107
    public static final class PKCS1Padding extends NativeRSACipher {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   108
        public PKCS1Padding() throws NoSuchAlgorithmException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   109
            super(UcryptoMech.CRYPTO_RSA_PKCS, 11);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   110
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   111
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   112
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   113
    NativeRSACipher(UcryptoMech mech, int padLen)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   114
        throws NoSuchAlgorithmException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   115
        this.mech = mech;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   116
        this.padLen = padLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   117
        this.keyFactory = new NativeRSAKeyFactory();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   118
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   119
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   120
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   121
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   122
        // Disallow change of mode for now since currently it's explicitly
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   123
        // defined in transformation strings
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   124
        throw new NoSuchAlgorithmException("Unsupported mode " + mode);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   125
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   126
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   127
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   128
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   129
    protected void engineSetPadding(String padding)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   130
            throws NoSuchPaddingException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   131
        // Disallow change of padding for now since currently it's explicitly
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   132
        // defined in transformation strings
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   133
        throw new NoSuchPaddingException("Unsupported padding " + padding);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   134
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   135
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   136
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   137
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   138
    protected int engineGetBlockSize() {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   139
        return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   140
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   141
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   142
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   143
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   144
    protected synchronized int engineGetOutputSize(int inputLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   145
        return outputSize;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   146
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   147
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   148
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   149
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   150
    protected byte[] engineGetIV() {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   151
        return null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   152
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   153
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   154
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   155
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   156
    protected AlgorithmParameters engineGetParameters() {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   157
        return null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   158
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   159
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   160
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   161
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   162
        if (!(key instanceof RSAKey)) {
32646
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   163
            throw new InvalidKeyException("RSAKey required. Got: " +
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   164
                key.getClass().getName());
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   165
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   166
        int n = ((RSAKey)key).getModulus().bitLength();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   167
        // strip off the leading extra 0x00 byte prefix
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   168
        int realByteSize = (n + 7) >> 3;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   169
        return realByteSize * 8;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   170
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   171
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   172
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   173
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   174
    protected synchronized void engineInit(int opmode, Key key, SecureRandom random)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   175
            throws InvalidKeyException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   176
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   177
            engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   178
        } catch (InvalidAlgorithmParameterException e) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   179
            throw new InvalidKeyException("init() failed", e);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   180
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   181
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   183
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   184
    @Override
28092
a67de45e0b25 8067088: Suppress solaris-specific deprecation warnings in the jdk.crypto.ucrypto module
darcy
parents: 27182
diff changeset
   185
    @SuppressWarnings("deprecation")
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   186
    protected synchronized void engineInit(int opmode, Key newKey,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   187
            AlgorithmParameterSpec params, SecureRandom random)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   188
            throws InvalidKeyException, InvalidAlgorithmParameterException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   189
        if (newKey == null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   190
            throw new InvalidKeyException("Key cannot be null");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   191
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   192
        if (opmode != Cipher.ENCRYPT_MODE &&
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   193
            opmode != Cipher.DECRYPT_MODE &&
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   194
            opmode != Cipher.WRAP_MODE &&
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   195
            opmode != Cipher.UNWRAP_MODE) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   196
            throw new InvalidAlgorithmParameterException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   197
                ("Unsupported mode: " + opmode);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   198
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   199
        if (params != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   200
            if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   201
                throw new InvalidAlgorithmParameterException(
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   202
                        "No Parameters can be specified");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   203
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   204
            spec = params;
35287
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 32646
diff changeset
   205
            if (random == null) {
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 32646
diff changeset
   206
                random = JCAUtil.getSecureRandom();
e59d934ce2ba 8134605: Partial rework of the fix for 8081297
igerasim
parents: 32646
diff changeset
   207
            }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   208
            this.random = random;   // for TLS RSA premaster secret
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   209
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   210
        boolean doEncrypt = (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   211
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   212
        // Make sure the proper opmode uses the proper key
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   213
        if (doEncrypt && (!(newKey instanceof RSAPublicKey))) {
32646
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   214
            throw new InvalidKeyException("RSAPublicKey required for encryption." +
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   215
                " Received: " + newKey.getClass().getName());
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   216
        } else if (!doEncrypt && (!(newKey instanceof RSAPrivateKey))) {
32646
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   217
            throw new InvalidKeyException("RSAPrivateKey required for decryption." +
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   218
                " Received: " + newKey.getClass().getName());
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   219
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   220
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   221
        NativeKey nativeKey = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   222
        // Check keyList cache for a nativeKey
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   223
        nativeKey = keyList.get(newKey);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   224
        if (nativeKey == null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   225
            // With no existing nativeKey for this newKey, create one
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   226
            if (doEncrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   227
                RSAPublicKey publicKey = (RSAPublicKey) newKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   228
                try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   229
                    nativeKey = (NativeKey) keyFactory.engineGeneratePublic
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   230
                        (new RSAPublicKeySpec(publicKey.getModulus(), publicKey.getPublicExponent()));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   231
                } catch (InvalidKeySpecException ikse) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   232
                    throw new InvalidKeyException(ikse);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   233
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   234
            } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   235
                try {
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   236
                    if (newKey instanceof RSAPrivateCrtKey) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   237
                        RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) newKey;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   238
                        nativeKey = (NativeKey) keyFactory.engineGeneratePrivate
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   239
                            (new RSAPrivateCrtKeySpec(privateKey.getModulus(),
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   240
                                                      privateKey.getPublicExponent(),
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   241
                                                      privateKey.getPrivateExponent(),
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   242
                                                      privateKey.getPrimeP(),
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   243
                                                      privateKey.getPrimeQ(),
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   244
                                                      privateKey.getPrimeExponentP(),
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   245
                                                      privateKey.getPrimeExponentQ(),
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   246
                                                      privateKey.getCrtCoefficient()));
32646
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   247
                    } else if (newKey instanceof RSAPrivateKey) {
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   248
                        RSAPrivateKey privateKey = (RSAPrivateKey) newKey;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   249
                        nativeKey = (NativeKey) keyFactory.engineGeneratePrivate
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   250
                            (new RSAPrivateKeySpec(privateKey.getModulus(),
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   251
                                                   privateKey.getPrivateExponent()));
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   252
                    } else {
32646
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   253
                        throw new InvalidKeyException("Unsupported type of RSAPrivateKey." +
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   254
                            " Received: " + newKey.getClass().getName());
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 32275
diff changeset
   255
                    }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   256
                } catch (InvalidKeySpecException ikse) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   257
                    throw new InvalidKeyException(ikse);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   258
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   259
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   260
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   261
            // Add nativeKey to keyList cache and associate it with newKey
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   262
            keyList.put(newKey, nativeKey);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   263
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   264
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   265
        init(doEncrypt, nativeKey);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   266
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   267
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   268
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   269
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   270
    protected synchronized void engineInit(int opmode, Key key, AlgorithmParameters params,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   271
            SecureRandom random)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   272
            throws InvalidKeyException, InvalidAlgorithmParameterException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   273
        if (params != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   274
            throw new InvalidAlgorithmParameterException("No Parameters can be specified");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   275
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   276
        engineInit(opmode, key, (AlgorithmParameterSpec) null, random);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   277
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   278
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   279
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   280
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   281
    protected synchronized byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   282
        if (inLen > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   283
            update(in, inOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   284
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   285
        return null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   286
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   287
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   288
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   289
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   290
    protected synchronized int engineUpdate(byte[] in, int inOfs, int inLen, byte[] out,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   291
            int outOfs) throws ShortBufferException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   292
        if (out.length - outOfs < outputSize) {
32646
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   293
            throw new ShortBufferException("Output buffer too small. outputSize: " +
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   294
                outputSize + ". out.length: " + out.length + ". outOfs: " + outOfs);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   295
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   296
        if (inLen > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   297
            update(in, inOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   298
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   299
        return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   300
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   301
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   302
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   303
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   304
    protected synchronized byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   305
            throws IllegalBlockSizeException, BadPaddingException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   306
        byte[] out = new byte[outputSize];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   307
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   308
            // delegate to the other engineDoFinal(...) method
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   309
            int actualLen = engineDoFinal(in, inOfs, inLen, out, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   310
            if (actualLen != outputSize) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   311
                return Arrays.copyOf(out, actualLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   312
            } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   313
                return out;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   314
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   315
        } catch (ShortBufferException e) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   316
            throw new UcryptoException("Internal Error", e);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   317
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   318
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   319
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   320
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   321
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   322
    protected synchronized int engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   323
                                             int outOfs)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   324
        throws ShortBufferException, IllegalBlockSizeException,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   325
               BadPaddingException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   326
        if (inLen != 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   327
            update(in, inOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   328
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   329
        return doFinal(out, outOfs, out.length - outOfs);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   330
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   331
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   332
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   333
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   334
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   335
    protected synchronized byte[] engineWrap(Key key) throws IllegalBlockSizeException,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   336
                                                             InvalidKeyException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   337
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   338
            byte[] encodedKey = key.getEncoded();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   339
            if ((encodedKey == null) || (encodedKey.length == 0)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   340
                throw new InvalidKeyException("Cannot get an encoding of " +
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   341
                                              "the key to be wrapped");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   342
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   343
            if (encodedKey.length > buffer.length) {
32646
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   344
                throw new InvalidKeyException("Key is too long for wrapping. " +
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   345
                    "encodedKey.length: " + encodedKey.length +
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   346
                    ". buffer.length: " + buffer.length);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   347
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   348
            return engineDoFinal(encodedKey, 0, encodedKey.length);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   349
        } catch (BadPaddingException e) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   350
            // Should never happen for key wrapping
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   351
            throw new UcryptoException("Internal Error", e);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   352
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   353
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   354
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   355
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   356
    @Override
28092
a67de45e0b25 8067088: Suppress solaris-specific deprecation warnings in the jdk.crypto.ucrypto module
darcy
parents: 27182
diff changeset
   357
    @SuppressWarnings("deprecation")
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   358
    protected synchronized Key engineUnwrap(byte[] wrappedKey,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   359
            String wrappedKeyAlgorithm, int wrappedKeyType)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   360
            throws InvalidKeyException, NoSuchAlgorithmException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   361
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   362
        if (wrappedKey.length > buffer.length) {
32646
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   363
            throw new InvalidKeyException("Key is too long for unwrapping." +
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   364
                " wrappedKey.length: " + wrappedKey.length +
db7c5592a47f 8133535: Better exception messaging in Ucrypto code
coffeys
parents: 32472
diff changeset
   365
                ". buffer.length: " + buffer.length);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   366
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   367
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   368
        boolean isTlsRsaPremasterSecret =
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   369
                wrappedKeyAlgorithm.equals("TlsRsaPremasterSecret");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   370
        Exception failover = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   371
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   372
        byte[] encodedKey = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   373
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   374
            encodedKey = engineDoFinal(wrappedKey, 0, wrappedKey.length);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   375
        } catch (BadPaddingException bpe) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   376
            if (isTlsRsaPremasterSecret) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   377
                failover = bpe;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   378
            } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   379
                throw new InvalidKeyException("Unwrapping failed", bpe);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   380
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   381
        } catch (Exception e) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   382
            throw new InvalidKeyException("Unwrapping failed", e);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   383
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   384
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   385
        if (isTlsRsaPremasterSecret) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   386
            if (!(spec instanceof TlsRsaPremasterSecretParameterSpec)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   387
                throw new IllegalStateException(
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   388
                        "No TlsRsaPremasterSecretParameterSpec specified");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   389
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   390
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   391
            // polish the TLS premaster secret
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   392
            encodedKey = KeyUtil.checkTlsPreMasterSecretKey(
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   393
                ((TlsRsaPremasterSecretParameterSpec)spec).getClientVersion(),
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   394
                ((TlsRsaPremasterSecretParameterSpec)spec).getServerVersion(),
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   395
                random, encodedKey, (failover != null));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   396
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   397
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   398
        return NativeCipher.constructKey(wrappedKeyType,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   399
                encodedKey, wrappedKeyAlgorithm);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   400
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   401
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   402
    /**
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   403
     * calls ucrypto_encrypt(...) or ucrypto_decrypt(...)
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28092
diff changeset
   404
     * @return the length of output or an negative error status code
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   405
     */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   406
    private native static int nativeAtomic(int mech, boolean encrypt,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   407
                                           long keyValue, int keyLength,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   408
                                           byte[] in, int inLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   409
                                           byte[] out, int ouOfs, int outLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   410
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   411
    // do actual initialization
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   412
    private void init(boolean encrypt, NativeKey key) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   413
        this.encrypt = encrypt;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   414
        this.key = key;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   415
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   416
            this.outputSize = engineGetKeySize(key)/8;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   417
        } catch (InvalidKeyException ike) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   418
            throw new UcryptoException("Internal Error", ike);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   419
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   420
        this.buffer = new byte[outputSize];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   421
        this.bufOfs = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   422
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   423
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   424
    // store the specified input into the internal buffer
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   425
    private void update(byte[] in, int inOfs, int inLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   426
        if ((inLen <= 0) || (in == null)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   427
            return;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   428
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   429
        // buffer bytes internally until doFinal is called
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   430
        if ((bufOfs + inLen + (encrypt? padLen:0)) > buffer.length) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   431
            // lead to IllegalBlockSizeException when doFinal() is called
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   432
            bufOfs = buffer.length + 1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   433
            return;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   434
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   435
        System.arraycopy(in, inOfs, buffer, bufOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   436
        bufOfs += inLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   437
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   438
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   439
    // return the actual non-negative output length
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   440
    private int doFinal(byte[] out, int outOfs, int outLen)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   441
            throws ShortBufferException, IllegalBlockSizeException,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   442
            BadPaddingException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   443
        if (bufOfs > buffer.length) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   444
            throw new IllegalBlockSizeException(
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   445
                "Data must not be longer than " +
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   446
                (buffer.length - (encrypt ? padLen : 0)) + " bytes");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   447
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   448
        if (outLen < outputSize) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   449
            throw new ShortBufferException();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   450
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   451
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   452
            long keyValue = key.value();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   453
            int k = nativeAtomic(mech.value(), encrypt, keyValue,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   454
                                 key.length(), buffer, bufOfs,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   455
                                 out, outOfs, outLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   456
            if (k < 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   457
                if ( k == -16 || k == -64) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   458
                    // -16: CRYPTO_ENCRYPTED_DATA_INVALID
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   459
                    // -64: CKR_ENCRYPTED_DATA_INVALID, see bug 17459266
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   460
                    UcryptoException ue = new UcryptoException(16);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   461
                    BadPaddingException bpe =
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   462
                        new BadPaddingException("Invalid encryption data");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   463
                    bpe.initCause(ue);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   464
                    throw bpe;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   465
                }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   466
                throw new UcryptoException(-k);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   467
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   468
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   469
            return k;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   470
        } finally {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   471
            bufOfs = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   472
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   473
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   474
}