001/* 002 * Copyright 2008-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2008-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2008-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.util.ssl; 037 038 039 040import java.security.KeyStoreException; 041import java.security.KeyStore; 042import javax.net.ssl.KeyManager; 043import javax.net.ssl.KeyManagerFactory; 044 045import com.unboundid.util.Debug; 046import com.unboundid.util.NotMutable; 047import com.unboundid.util.NotNull; 048import com.unboundid.util.Nullable; 049import com.unboundid.util.StaticUtils; 050import com.unboundid.util.ThreadSafety; 051import com.unboundid.util.ThreadSafetyLevel; 052 053import static com.unboundid.util.ssl.SSLMessages.*; 054 055 056 057/** 058 * This class provides an SSL key manager that may be used to retrieve 059 * certificates from a PKCS#11 token. 060 */ 061@NotMutable() 062@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 063public final class PKCS11KeyManager 064 extends WrapperKeyManager 065{ 066 /** 067 * The key store type to use to access PKCS#11 tokens. 068 */ 069 @NotNull private static final String PKCS11_KEY_STORE_TYPE = "PKCS11"; 070 071 072 073 /** 074 * Creates a new instance of this PKCS11 key manager that provides the ability 075 * to retrieve certificates from a PKCS#11 token. 076 * 077 * @param keyStorePIN The PIN to use to access the contents of the 078 * PKCS#11 token. It may be {@code null} if no PIN 079 * is required. 080 * @param certificateAlias The nickname of the certificate that should be 081 * selected. It may be {@code null} if any 082 * acceptable certificate found may be used. 083 * 084 * @throws KeyStoreException If a problem occurs while initializing this key 085 * manager. 086 */ 087 public PKCS11KeyManager(@Nullable final char[] keyStorePIN, 088 @Nullable final String certificateAlias) 089 throws KeyStoreException 090 { 091 super(getKeyManagers(keyStorePIN), certificateAlias); 092 } 093 094 095 096 /** 097 * Retrieves the set of key managers that will be wrapped by this key manager. 098 * 099 * @param keyStorePIN The PIN to use to access the contents of the PKCS#11 100 * token. It may be {@code null} if no PIN is required. 101 * 102 * @return The set of key managers that will be wrapped by this key manager. 103 * 104 * @throws KeyStoreException If a problem occurs while initializing this key 105 * manager. 106 */ 107 @NotNull() 108 private static KeyManager[] getKeyManagers(@Nullable final char[] keyStorePIN) 109 throws KeyStoreException 110 { 111 final KeyStore ks = KeyStore.getInstance(PKCS11_KEY_STORE_TYPE); 112 try 113 { 114 ks.load(null, keyStorePIN); 115 } 116 catch (final Exception e) 117 { 118 Debug.debugException(e); 119 120 throw new KeyStoreException( 121 ERR_PKCS11_CANNOT_ACCESS.get(StaticUtils.getExceptionMessage(e)), e); 122 } 123 124 try 125 { 126 final KeyManagerFactory factory = KeyManagerFactory.getInstance( 127 KeyManagerFactory.getDefaultAlgorithm()); 128 factory.init(ks, keyStorePIN); 129 return factory.getKeyManagers(); 130 } 131 catch (final Exception e) 132 { 133 Debug.debugException(e); 134 135 throw new KeyStoreException( 136 ERR_PKCS11_CANNOT_GET_KEY_MANAGERS.get( 137 StaticUtils.getExceptionMessage(e)), 138 e); 139 } 140 } 141}