src/java.base/share/classes/com/sun/crypto/provider/SealedObjectForKeyProtector.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 49783 977c6dd636bd
child 57950 4612a3cfb927
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
     1
/*
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
     4
 *
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    10
 *
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    15
 * accompanied this code).
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    16
 *
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    20
 *
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    23
 * questions.
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    24
 */
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    25
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    26
package com.sun.crypto.provider;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    27
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49783
diff changeset
    28
import jdk.internal.access.SharedSecrets;
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    29
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    30
import java.io.*;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    31
import java.security.*;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    32
import javax.crypto.*;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    33
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    34
final class SealedObjectForKeyProtector extends SealedObject {
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    35
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    36
    static final long serialVersionUID = -3650226485480866989L;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    37
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    38
    /**
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    39
     * The InputStreamFilter for a Key object inside this SealedObject. It can
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    40
     * be either provided as a {@link Security} property or a system property
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    41
     * (when provided as latter, it shadows the former). If the result of this
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    42
     * filter is {@link java.io.ObjectInputFilter.Status.UNDECIDED}, the system
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    43
     * level filter defined by jdk.serialFilter will be consulted. The value
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    44
     * of this property uses the same format of jdk.serialFilter.
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    45
     */
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    46
    private static final String KEY_SERIAL_FILTER = "jceks.key.serialFilter";
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    47
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    48
    SealedObjectForKeyProtector(Serializable object, Cipher c)
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    49
            throws IOException, IllegalBlockSizeException {
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    50
        super(object, c);
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    51
    }
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    52
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    53
    SealedObjectForKeyProtector(SealedObject so) {
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    54
        super(so);
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    55
    }
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    56
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    57
    AlgorithmParameters getParameters() {
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    58
        AlgorithmParameters params = null;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    59
        if (super.encodedParams != null) {
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    60
            try {
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    61
                params = AlgorithmParameters.getInstance("PBE",
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    62
                    SunJCE.getInstance());
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    63
                params.init(super.encodedParams);
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    64
            } catch (NoSuchAlgorithmException nsae) {
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    65
                throw new RuntimeException(
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    66
                    "SunJCE provider is not configured properly");
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    67
            } catch (IOException io) {
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    68
                throw new RuntimeException("Parameter failure: "+
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    69
                    io.getMessage());
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    70
            }
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    71
        }
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    72
        return params;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    73
    }
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    74
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    75
    final Key getKey(Cipher c)
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    76
            throws IOException, ClassNotFoundException, IllegalBlockSizeException,
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    77
            BadPaddingException {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    78
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    79
        try (ObjectInputStream ois = SharedSecrets.getJavaxCryptoSealedObjectAccess()
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    80
                .getExtObjectInputStream(this, c)) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    81
            AccessController.doPrivileged(
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    82
                    (PrivilegedAction<Void>) () -> {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    83
                        ois.setObjectInputFilter(DeserializationChecker.ONE_FILTER);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    84
                        return null;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    85
                    });
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    86
            try {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    87
                @SuppressWarnings("unchecked")
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    88
                Key t = (Key) ois.readObject();
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    89
                return t;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    90
            } catch (InvalidClassException ice) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    91
                String msg = ice.getMessage();
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    92
                if (msg.contains("REJECTED")) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    93
                    throw new IOException("Rejected by the"
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    94
                            + " jceks.key.serialFilter or jdk.serialFilter"
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    95
                            + " property", ice);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    96
                } else {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    97
                    throw ice;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    98
                }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    99
            }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   100
        }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   101
    }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   102
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   103
    /**
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   104
     * The filter for the content of a SealedObjectForKeyProtector.
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   105
     *
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   106
     * First, the jceks.key.serialFilter will be consulted. If the result
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   107
     * is UNDECIDED, the system level jdk.serialFilter will be consulted.
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   108
     */
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   109
    private static class DeserializationChecker implements ObjectInputFilter {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   110
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   111
        private static final ObjectInputFilter ONE_FILTER;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   112
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   113
        static {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   114
            String prop = AccessController.doPrivileged(
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   115
                    (PrivilegedAction<String>) () -> {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   116
                        String tmp = System.getProperty(KEY_SERIAL_FILTER);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   117
                        if (tmp != null) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   118
                            return tmp;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   119
                        } else {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   120
                            return Security.getProperty(KEY_SERIAL_FILTER);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   121
                        }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   122
                    });
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   123
            ONE_FILTER = new DeserializationChecker(prop == null ? null
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   124
                    : ObjectInputFilter.Config.createFilter(prop));
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   125
        }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   126
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   127
        private final ObjectInputFilter base;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   128
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   129
        private DeserializationChecker(ObjectInputFilter base) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   130
            this.base = base;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   131
        }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   132
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   133
        @Override
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   134
        public ObjectInputFilter.Status checkInput(
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   135
                ObjectInputFilter.FilterInfo info) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   136
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   137
            if (info.serialClass() == Object.class) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   138
                return Status.UNDECIDED;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   139
            }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   140
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   141
            if (base != null) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   142
                Status result = base.checkInput(info);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   143
                if (result != Status.UNDECIDED) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   144
                    return result;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   145
                }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   146
            }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   147
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   148
            ObjectInputFilter defaultFilter =
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   149
                    ObjectInputFilter.Config.getSerialFilter();
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   150
            if (defaultFilter != null) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   151
                return defaultFilter.checkInput(info);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   152
            }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   153
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   154
            return Status.UNDECIDED;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   155
        }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   156
    }
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
   157
}