src/java.base/share/classes/com/sun/crypto/provider/SealedObjectForKeyProtector.java
author darcy
Thu, 29 Aug 2019 10:52:21 -0700
changeset 57950 4612a3cfb927
parent 52427 3c6aa484536c
permissions -rw-r--r--
8229999: Apply java.io.Serial annotations to security types in java.base Reviewed-by: rriggs, mullan
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
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 52427
diff changeset
     2
 * Copyright (c) 1998, 2019, 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
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 52427
diff changeset
    36
    @java.io.Serial
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    37
    static final long serialVersionUID = -3650226485480866989L;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    38
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    39
    /**
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    40
     * The InputStreamFilter for a Key object inside this SealedObject. It can
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    41
     * be either provided as a {@link Security} property or a system property
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    42
     * (when provided as latter, it shadows the former). If the result of this
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    43
     * filter is {@link java.io.ObjectInputFilter.Status.UNDECIDED}, the system
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    44
     * level filter defined by jdk.serialFilter will be consulted. The value
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    45
     * of this property uses the same format of jdk.serialFilter.
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    46
     */
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    47
    private static final String KEY_SERIAL_FILTER = "jceks.key.serialFilter";
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    48
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    49
    SealedObjectForKeyProtector(Serializable object, Cipher c)
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    50
            throws IOException, IllegalBlockSizeException {
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    51
        super(object, c);
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
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    54
    SealedObjectForKeyProtector(SealedObject so) {
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    55
        super(so);
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
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    58
    AlgorithmParameters getParameters() {
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    59
        AlgorithmParameters params = null;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    60
        if (super.encodedParams != null) {
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    61
            try {
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    62
                params = AlgorithmParameters.getInstance("PBE",
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    63
                    SunJCE.getInstance());
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    64
                params.init(super.encodedParams);
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    65
            } catch (NoSuchAlgorithmException nsae) {
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    66
                throw new RuntimeException(
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    67
                    "SunJCE provider is not configured properly");
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    68
            } catch (IOException io) {
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    69
                throw new RuntimeException("Parameter failure: "+
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 14317
diff changeset
    70
                    io.getMessage());
14317
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
        }
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    73
        return params;
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
    74
    }
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    75
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    76
    final Key getKey(Cipher c)
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    77
            throws IOException, ClassNotFoundException, IllegalBlockSizeException,
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    78
            BadPaddingException {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    79
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    80
        try (ObjectInputStream ois = SharedSecrets.getJavaxCryptoSealedObjectAccess()
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    81
                .getExtObjectInputStream(this, c)) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    82
            AccessController.doPrivileged(
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    83
                    (PrivilegedAction<Void>) () -> {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    84
                        ois.setObjectInputFilter(DeserializationChecker.ONE_FILTER);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    85
                        return null;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    86
                    });
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    87
            try {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    88
                @SuppressWarnings("unchecked")
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    89
                Key t = (Key) ois.readObject();
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    90
                return t;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    91
            } catch (InvalidClassException ice) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    92
                String msg = ice.getMessage();
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    93
                if (msg.contains("REJECTED")) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    94
                    throw new IOException("Rejected by the"
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    95
                            + " jceks.key.serialFilter or jdk.serialFilter"
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    96
                            + " property", ice);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    97
                } else {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
    98
                    throw ice;
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
    /**
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   105
     * The filter for the content of a SealedObjectForKeyProtector.
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   106
     *
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   107
     * First, the jceks.key.serialFilter will be consulted. If the result
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   108
     * is UNDECIDED, the system level jdk.serialFilter will be consulted.
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   109
     */
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   110
    private static class DeserializationChecker implements ObjectInputFilter {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   111
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   112
        private static final ObjectInputFilter ONE_FILTER;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   113
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   114
        static {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   115
            String prop = AccessController.doPrivileged(
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   116
                    (PrivilegedAction<String>) () -> {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   117
                        String tmp = System.getProperty(KEY_SERIAL_FILTER);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   118
                        if (tmp != null) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   119
                            return tmp;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   120
                        } else {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   121
                            return Security.getProperty(KEY_SERIAL_FILTER);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   122
                        }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   123
                    });
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   124
            ONE_FILTER = new DeserializationChecker(prop == null ? null
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   125
                    : ObjectInputFilter.Config.createFilter(prop));
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   126
        }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   127
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   128
        private final ObjectInputFilter base;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   129
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   130
        private DeserializationChecker(ObjectInputFilter base) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   131
            this.base = base;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   132
        }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   133
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   134
        @Override
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   135
        public ObjectInputFilter.Status checkInput(
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   136
                ObjectInputFilter.FilterInfo info) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   137
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   138
            if (info.serialClass() == Object.class) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   139
                return Status.UNDECIDED;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   140
            }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   141
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   142
            if (base != null) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   143
                Status result = base.checkInput(info);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   144
                if (result != Status.UNDECIDED) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   145
                    return result;
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
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   149
            ObjectInputFilter defaultFilter =
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   150
                    ObjectInputFilter.Config.getSerialFilter();
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   151
            if (defaultFilter != null) {
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   152
                return defaultFilter.checkInput(info);
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   153
            }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   154
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   155
            return Status.UNDECIDED;
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   156
        }
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47216
diff changeset
   157
    }
14317
1e855efb3783 8001419: Build the JCE portion of JDK-8000970
ohrstrom
parents:
diff changeset
   158
}