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