|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Red Hat, Inc. |
| 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 sun.security.pkcs11; |
| 27 | + |
| 28 | +import java.math.BigInteger; |
| 29 | +import java.security.KeyFactory; |
| 30 | +import java.security.Provider; |
| 31 | +import java.security.Security; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.concurrent.locks.ReentrantLock; |
| 35 | + |
| 36 | +import javax.crypto.Cipher; |
| 37 | +import javax.crypto.spec.DHPrivateKeySpec; |
| 38 | +import javax.crypto.spec.IvParameterSpec; |
| 39 | + |
| 40 | +import sun.security.jca.JCAUtil; |
| 41 | +import sun.security.pkcs11.TemplateManager; |
| 42 | +import sun.security.pkcs11.wrapper.CK_ATTRIBUTE; |
| 43 | +import sun.security.pkcs11.wrapper.CK_MECHANISM; |
| 44 | +import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
| 45 | +import sun.security.pkcs11.wrapper.PKCS11Exception; |
| 46 | +import sun.security.rsa.RSAUtil.KeyType; |
| 47 | +import sun.security.util.Debug; |
| 48 | +import sun.security.util.ECUtil; |
| 49 | + |
| 50 | +final class FIPSKeyImporter { |
| 51 | + |
| 52 | + private static final Debug debug = |
| 53 | + Debug.getInstance("sunpkcs11"); |
| 54 | + |
| 55 | + private static P11Key importerKey = null; |
| 56 | + private static final ReentrantLock importerKeyLock = new ReentrantLock(); |
| 57 | + private static CK_MECHANISM importerKeyMechanism = null; |
| 58 | + private static Cipher importerCipher = null; |
| 59 | + |
| 60 | + private static Provider sunECProvider = null; |
| 61 | + private static final ReentrantLock sunECProviderLock = new ReentrantLock(); |
| 62 | + |
| 63 | + private static KeyFactory DHKF = null; |
| 64 | + private static final ReentrantLock DHKFLock = new ReentrantLock(); |
| 65 | + |
| 66 | + static Long importKey(SunPKCS11 sunPKCS11, long hSession, CK_ATTRIBUTE[] attributes) |
| 67 | + throws PKCS11Exception { |
| 68 | + long keyID = -1; |
| 69 | + Token token = sunPKCS11.getToken(); |
| 70 | + if (debug != null) { |
| 71 | + debug.println("Private or Secret key will be imported in" + |
| 72 | + " system FIPS mode."); |
| 73 | + } |
| 74 | + if (importerKey == null) { |
| 75 | + importerKeyLock.lock(); |
| 76 | + try { |
| 77 | + if (importerKey == null) { |
| 78 | + if (importerKeyMechanism == null) { |
| 79 | + // Importer Key creation has not been tried yet. Try it. |
| 80 | + createImporterKey(token); |
| 81 | + } |
| 82 | + if (importerKey == null || importerCipher == null) { |
| 83 | + if (debug != null) { |
| 84 | + debug.println("Importer Key could not be" + |
| 85 | + " generated."); |
| 86 | + } |
| 87 | + throw new PKCS11Exception(CKR_GENERAL_ERROR); |
| 88 | + } |
| 89 | + if (debug != null) { |
| 90 | + debug.println("Importer Key successfully" + |
| 91 | + " generated."); |
| 92 | + } |
| 93 | + } |
| 94 | + } finally { |
| 95 | + importerKeyLock.unlock(); |
| 96 | + } |
| 97 | + } |
| 98 | + long importerKeyID = importerKey.getKeyID(); |
| 99 | + try { |
| 100 | + byte[] keyBytes = null; |
| 101 | + byte[] encKeyBytes = null; |
| 102 | + long keyClass = 0L; |
| 103 | + long keyType = 0L; |
| 104 | + Map<Long, CK_ATTRIBUTE> attrsMap = new HashMap<>(); |
| 105 | + for (CK_ATTRIBUTE attr : attributes) { |
| 106 | + if (attr.type == CKA_CLASS) { |
| 107 | + keyClass = attr.getLong(); |
| 108 | + } else if (attr.type == CKA_KEY_TYPE) { |
| 109 | + keyType = attr.getLong(); |
| 110 | + } |
| 111 | + attrsMap.put(attr.type, attr); |
| 112 | + } |
| 113 | + BigInteger v = null; |
| 114 | + if (keyClass == CKO_PRIVATE_KEY) { |
| 115 | + if (keyType == CKK_RSA) { |
| 116 | + if (debug != null) { |
| 117 | + debug.println("Importing an RSA private key..."); |
| 118 | + } |
| 119 | + keyBytes = sun.security.rsa.RSAPrivateCrtKeyImpl.newKey( |
| 120 | + KeyType.RSA, |
| 121 | + null, |
| 122 | + ((v = attrsMap.get(CKA_MODULUS).getBigInteger()) != null) |
| 123 | + ? v : BigInteger.ZERO, |
| 124 | + ((v = attrsMap.get(CKA_PUBLIC_EXPONENT).getBigInteger()) != null) |
| 125 | + ? v : BigInteger.ZERO, |
| 126 | + ((v = attrsMap.get(CKA_PRIVATE_EXPONENT).getBigInteger()) != null) |
| 127 | + ? v : BigInteger.ZERO, |
| 128 | + ((v = attrsMap.get(CKA_PRIME_1).getBigInteger()) != null) |
| 129 | + ? v : BigInteger.ZERO, |
| 130 | + ((v = attrsMap.get(CKA_PRIME_2).getBigInteger()) != null) |
| 131 | + ? v : BigInteger.ZERO, |
| 132 | + ((v = attrsMap.get(CKA_EXPONENT_1).getBigInteger()) != null) |
| 133 | + ? v : BigInteger.ZERO, |
| 134 | + ((v = attrsMap.get(CKA_EXPONENT_2).getBigInteger()) != null) |
| 135 | + ? v : BigInteger.ZERO, |
| 136 | + ((v = attrsMap.get(CKA_COEFFICIENT).getBigInteger()) != null) |
| 137 | + ? v : BigInteger.ZERO |
| 138 | + ).getEncoded(); |
| 139 | + } else if (keyType == CKK_DSA) { |
| 140 | + if (debug != null) { |
| 141 | + debug.println("Importing a DSA private key..."); |
| 142 | + } |
| 143 | + keyBytes = new sun.security.provider.DSAPrivateKey( |
| 144 | + ((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null) |
| 145 | + ? v : BigInteger.ZERO, |
| 146 | + ((v = attrsMap.get(CKA_PRIME).getBigInteger()) != null) |
| 147 | + ? v : BigInteger.ZERO, |
| 148 | + ((v = attrsMap.get(CKA_SUBPRIME).getBigInteger()) != null) |
| 149 | + ? v : BigInteger.ZERO, |
| 150 | + ((v = attrsMap.get(CKA_BASE).getBigInteger()) != null) |
| 151 | + ? v : BigInteger.ZERO |
| 152 | + ).getEncoded(); |
| 153 | + if (token.config.getNssNetscapeDbWorkaround() && |
| 154 | + attrsMap.get(CKA_NETSCAPE_DB) == null) { |
| 155 | + attrsMap.put(CKA_NETSCAPE_DB, |
| 156 | + new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO)); |
| 157 | + } |
| 158 | + } else if (keyType == CKK_EC) { |
| 159 | + if (debug != null) { |
| 160 | + debug.println("Importing an EC private key..."); |
| 161 | + } |
| 162 | + if (sunECProvider == null) { |
| 163 | + sunECProviderLock.lock(); |
| 164 | + try { |
| 165 | + if (sunECProvider == null) { |
| 166 | + sunECProvider = Security.getProvider("SunEC"); |
| 167 | + } |
| 168 | + } finally { |
| 169 | + sunECProviderLock.unlock(); |
| 170 | + } |
| 171 | + } |
| 172 | + keyBytes = ECUtil.generateECPrivateKey( |
| 173 | + ((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null) |
| 174 | + ? v : BigInteger.ZERO, |
| 175 | + ECUtil.getECParameterSpec(sunECProvider, |
| 176 | + attrsMap.get(CKA_EC_PARAMS).getByteArray())) |
| 177 | + .getEncoded(); |
| 178 | + if (token.config.getNssNetscapeDbWorkaround() && |
| 179 | + attrsMap.get(CKA_NETSCAPE_DB) == null) { |
| 180 | + attrsMap.put(CKA_NETSCAPE_DB, |
| 181 | + new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO)); |
| 182 | + } |
| 183 | + } else if (keyType == CKK_DH) { |
| 184 | + if (debug != null) { |
| 185 | + debug.println("Importing a Diffie-Hellman private key..."); |
| 186 | + } |
| 187 | + if (DHKF == null) { |
| 188 | + DHKFLock.lock(); |
| 189 | + try { |
| 190 | + if (DHKF == null) { |
| 191 | + DHKF = KeyFactory.getInstance( |
| 192 | + "DH", P11Util.getSunJceProvider()); |
| 193 | + } |
| 194 | + } finally { |
| 195 | + DHKFLock.unlock(); |
| 196 | + } |
| 197 | + } |
| 198 | + DHPrivateKeySpec spec = new DHPrivateKeySpec |
| 199 | + (((v = attrsMap.get(CKA_VALUE).getBigInteger()) != null) |
| 200 | + ? v : BigInteger.ZERO, |
| 201 | + ((v = attrsMap.get(CKA_PRIME).getBigInteger()) != null) |
| 202 | + ? v : BigInteger.ZERO, |
| 203 | + ((v = attrsMap.get(CKA_BASE).getBigInteger()) != null) |
| 204 | + ? v : BigInteger.ZERO); |
| 205 | + keyBytes = DHKF.generatePrivate(spec).getEncoded(); |
| 206 | + if (token.config.getNssNetscapeDbWorkaround() && |
| 207 | + attrsMap.get(CKA_NETSCAPE_DB) == null) { |
| 208 | + attrsMap.put(CKA_NETSCAPE_DB, |
| 209 | + new CK_ATTRIBUTE(CKA_NETSCAPE_DB, BigInteger.ZERO)); |
| 210 | + } |
| 211 | + } else { |
| 212 | + if (debug != null) { |
| 213 | + debug.println("Unrecognized private key type."); |
| 214 | + } |
| 215 | + throw new PKCS11Exception(CKR_GENERAL_ERROR); |
| 216 | + } |
| 217 | + } else if (keyClass == CKO_SECRET_KEY) { |
| 218 | + if (debug != null) { |
| 219 | + debug.println("Importing a secret key..."); |
| 220 | + } |
| 221 | + keyBytes = attrsMap.get(CKA_VALUE).getByteArray(); |
| 222 | + } |
| 223 | + if (keyBytes == null || keyBytes.length == 0) { |
| 224 | + if (debug != null) { |
| 225 | + debug.println("Private or secret key plain bytes could" + |
| 226 | + " not be obtained. Import failed."); |
| 227 | + } |
| 228 | + throw new PKCS11Exception(CKR_GENERAL_ERROR); |
| 229 | + } |
| 230 | + importerCipher.init(Cipher.ENCRYPT_MODE, importerKey, |
| 231 | + new IvParameterSpec((byte[])importerKeyMechanism.pParameter), |
| 232 | + null); |
| 233 | + attributes = new CK_ATTRIBUTE[attrsMap.size()]; |
| 234 | + attrsMap.values().toArray(attributes); |
| 235 | + encKeyBytes = importerCipher.doFinal(keyBytes); |
| 236 | + attributes = token.getAttributes(TemplateManager.O_IMPORT, |
| 237 | + keyClass, keyType, attributes); |
| 238 | + keyID = token.p11.C_UnwrapKey(hSession, |
| 239 | + importerKeyMechanism, importerKeyID, encKeyBytes, attributes); |
| 240 | + if (debug != null) { |
| 241 | + debug.println("Imported key ID: " + keyID); |
| 242 | + } |
| 243 | + } catch (Throwable t) { |
| 244 | + throw new PKCS11Exception(CKR_GENERAL_ERROR); |
| 245 | + } finally { |
| 246 | + importerKey.releaseKeyID(); |
| 247 | + } |
| 248 | + return Long.valueOf(keyID); |
| 249 | + } |
| 250 | + |
| 251 | + private static void createImporterKey(Token token) { |
| 252 | + if (debug != null) { |
| 253 | + debug.println("Generating Importer Key..."); |
| 254 | + } |
| 255 | + byte[] iv = new byte[16]; |
| 256 | + JCAUtil.getSecureRandom().nextBytes(iv); |
| 257 | + importerKeyMechanism = new CK_MECHANISM(CKM_AES_CBC_PAD, iv); |
| 258 | + try { |
| 259 | + CK_ATTRIBUTE[] attributes = token.getAttributes(TemplateManager.O_GENERATE, |
| 260 | + CKO_SECRET_KEY, CKK_AES, new CK_ATTRIBUTE[] { |
| 261 | + new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY), |
| 262 | + new CK_ATTRIBUTE(CKA_VALUE_LEN, 256 >> 3)}); |
| 263 | + Session s = null; |
| 264 | + try { |
| 265 | + s = token.getObjSession(); |
| 266 | + long keyID = token.p11.C_GenerateKey( |
| 267 | + s.id(), new CK_MECHANISM(CKM_AES_KEY_GEN), |
| 268 | + attributes); |
| 269 | + if (debug != null) { |
| 270 | + debug.println("Importer Key ID: " + keyID); |
| 271 | + } |
| 272 | + importerKey = (P11Key)P11Key.secretKey(s, keyID, "AES", |
| 273 | + 256 >> 3, null); |
| 274 | + } catch (PKCS11Exception e) { |
| 275 | + // best effort |
| 276 | + } finally { |
| 277 | + token.releaseSession(s); |
| 278 | + } |
| 279 | + if (importerKey != null) { |
| 280 | + importerCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 281 | + } |
| 282 | + } catch (Throwable t) { |
| 283 | + // best effort |
| 284 | + importerKey = null; |
| 285 | + importerCipher = null; |
| 286 | + // importerKeyMechanism value is kept initialized to indicate that |
| 287 | + // Importer Key creation has been tried and failed. |
| 288 | + } |
| 289 | + } |
| 290 | +} |
0 commit comments