KeyStoreManagingServlet.java 25.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.auth.cert.UserCertificateMapping
 *  com.adobe.granite.auth.cert.UserCertificateMappingException
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.adobe.granite.keystore.KeyStoreNotInitialisedException
 *  com.adobe.granite.keystore.KeyStoreService
 *  javax.servlet.ServletException
 *  org.apache.commons.io.IOUtils
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.User
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.security.user.internal.servlets;

import com.adobe.granite.auth.cert.UserCertificateMapping;
import com.adobe.granite.auth.cert.UserCertificateMappingException;
import com.adobe.granite.crypto.CryptoSupport;
import com.adobe.granite.keystore.KeyStoreNotInitialisedException;
import com.adobe.granite.keystore.KeyStoreService;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.x500.X500Principal;
import javax.servlet.ServletException;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(methods={"GET", "POST"}, paths={"rep/SystemUser/ks.json.GET.servlet", "rep/User/ks.json.GET.servlet", "/libs/granite/security/truststore.json", "rep/SystemUser/ks.html.POST.servlet", "rep/User/ks.html.POST.servlet", "/libs/granite/security/post/truststore"})
public class KeyStoreManagingServlet
extends SlingAllMethodsServlet {
    private static final Logger log = LoggerFactory.getLogger(KeyStoreManagingServlet.class);
    private static final String P_OPERATION = ":operation";
    private static final String OP_ADD_KEY_PAIR = "addKeyPair";
    private static final String KEYPAIR_ALGORITHM = "RSA";
    private static final String ALIAS_PARAM_NAME = "alias";
    private static final String KEYSTORE_PARAM_NAME = "keyStore";
    private static final String KEYSTORE_PASS_PARAM_NAME = "keyStorePass";
    private static final String KEYSTORE_TYPE_PARAM_NAME = "keyStoreType";
    private static final String KEY_PASS_PARAM_NAME = "keyPassword";
    private static final String NEW_ALIAS_PARAM_NAME = "newAlias";
    private static final String CERT_PARAM_NAME = "certificate";
    private static final String MAP_CERTIFICATE_PARAM_NAME = "mapCertificate";
    private static final String SEPARATOR = "#";
    private static final String PK_PARAM_NAME = "pk";
    private static final String CERT_CHAIN_PARAM_NAME = "cert-chain";
    private static final String ALIAS_REMOVE_PARAM_NAME = "removeAlias";
    private static final String SUBJECT_PROP_NAME = "subject";
    private static final String CERT_ALIAS_STRING = "certAlias___";
    private static final Logger LOG = LoggerFactory.getLogger(KeyStoreManagingServlet.class);
    static final String TRUSTSTORE_GET = "/libs/granite/security/truststore.json";
    static final String TRUSTSTORE_POST = "/libs/granite/security/post/truststore";
    @Reference
    private KeyStoreService keyStoreService = null;
    @Reference
    private CryptoSupport cryptoSupport = null;
    @Reference
    private UserCertificateMapping certMapping = null;
    private Map<String, String> mimeTypeToStoreTypeMap = new HashMap<String, String>();

    public KeyStoreManagingServlet() {
        this.mimeTypeToStoreTypeMap.put("application/x-pkcs12", "pkcs12");
        this.mimeTypeToStoreTypeMap.put("application/x-java-keystore", "jks");
        this.mimeTypeToStoreTypeMap.put("application/pkcs12", "pkcs12");
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        ResourceResolver resolver = request.getResourceResolver();
        Resource resource = request.getResource();
        this.setJSONHeader(response);
        JSONObject jsonResponse = new JSONObject();
        KeyStore store = null;
        try {
            boolean isTrustStore = false;
            Authorizable authorizable = null;
            try {
                if ("/libs/granite/security/truststore.json".equals(resource.getPath())) {
                    store = this.keyStoreService.getTrustStore(resolver);
                    isTrustStore = true;
                } else {
                    authorizable = (Authorizable)resource.adaptTo(Authorizable.class);
                    if (authorizable != null) {
                        store = this.getKeyStore(resolver, authorizable.getID());
                    }
                }
            }
            catch (KeyStoreNotInitialisedException e) {
                jsonResponse.put("exists", false);
            }
            if (store != null) {
                JSONArray aliasesJSONArray = new JSONArray();
                Enumeration<String> aliases = store.aliases();
                while (aliases.hasMoreElements()) {
                    String alias = aliases.nextElement();
                    JSONObject aliasJSON = new JSONObject();
                    aliasJSON.put("alias", (Object)alias);
                    KeyStore.TrustedCertificateEntry entry = isTrustStore ? new KeyStore.TrustedCertificateEntry(store.getCertificate(alias)) : this.keyStoreService.getKeyStoreEntry(resolver, authorizable.getID(), alias);
                    if (entry instanceof KeyStore.PrivateKeyEntry) {
                        aliasJSON.put("entryType", (Object)"privateKey");
                        KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)((Object)entry);
                        PrivateKey privateKey = pkEntry.getPrivateKey();
                        aliasJSON.put("algorithm", (Object)privateKey.getAlgorithm());
                        aliasJSON.put("format", (Object)privateKey.getFormat());
                        Certificate[] chain = pkEntry.getCertificateChain();
                        JSONArray chainJSONArray = new JSONArray();
                        for (Certificate aChain : chain) {
                            JSONObject certificateJSON = new JSONObject();
                            X509Certificate certificate = (X509Certificate)aChain;
                            certificateJSON.put("subject", (Object)certificate.getSubjectX500Principal().toString());
                            certificateJSON.put("issuer", (Object)certificate.getIssuerX500Principal().toString());
                            certificateJSON.put("notBefore", (Object)certificate.getNotBefore());
                            certificateJSON.put("notAfter", (Object)certificate.getNotAfter());
                            certificateJSON.put("serialNumber", (Object)certificate.getSerialNumber());
                            chainJSONArray.put((Object)certificateJSON);
                        }
                        aliasJSON.put("chain", (Object)chainJSONArray);
                    } else if (entry instanceof KeyStore.TrustedCertificateEntry) {
                        aliasJSON.put("entryType", (Object)"trustedCertificate");
                        KeyStore.TrustedCertificateEntry trustedCertificateEntry = entry;
                        X509Certificate certificate = (X509Certificate)trustedCertificateEntry.getTrustedCertificate();
                        aliasJSON.put("subject", (Object)certificate.getSubjectX500Principal().toString());
                        aliasJSON.put("issuer", (Object)certificate.getIssuerX500Principal().toString());
                        aliasJSON.put("notBefore", (Object)certificate.getNotBefore());
                        aliasJSON.put("notAfter", (Object)certificate.getNotAfter());
                        aliasJSON.put("serialNumber", (Object)certificate.getSerialNumber());
                    }
                    aliasesJSONArray.put((Object)aliasJSON);
                }
                jsonResponse.put("aliases", (Object)aliasesJSONArray);
            }
        }
        catch (Exception e) {
            LOG.error("Unable to retrieve the truststore's aliases.", (Throwable)e);
            response.sendError(500);
        }
        finally {
            response.getWriter().write(jsonResponse.toString());
        }
    }

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        try {
            ResourceResolver resolver = request.getResourceResolver();
            Resource currentResource = request.getResource();
            String currentPath = request.getRequestPathInfo().getResourcePath();
            RequestParameter aliasParam = request.getRequestParameter("alias");
            RequestParameter pkParam = request.getRequestParameter("pk");
            RequestParameter removeAlias = request.getRequestParameter("removeAlias");
            RequestParameter[] certChainParam = request.getRequestParameters("cert-chain");
            RequestParameter keyStoreParam = request.getRequestParameter("keyStore");
            RequestParameter keyStorePassParam = request.getRequestParameter("keyStorePass");
            RequestParameter keyStoreTypeParam = request.getRequestParameter("keyStoreType");
            RequestParameter keyPasswordParam = request.getRequestParameter("keyPassword");
            RequestParameter newAliasParam = request.getRequestParameter("newAlias");
            String newPassword = request.getParameter("newPassword");
            String rePassword = request.getParameter("newPassword");
            String currentPassword = request.getParameter("currentPassword");
            String operation = request.getParameter(":operation");
            if ("/libs/granite/security/post/truststore".equals(currentPath)) {
                boolean isRequestProcessed = false;
                if ("createStore".equals(operation)) {
                    this.createTrustStore(resolver, newPassword, rePassword);
                } else if ("changePassword".equals(operation)) {
                    this.changeTrustStorePass(resolver, currentPassword, newPassword, rePassword);
                } else if (removeAlias != null) {
                    this.removeAlias(StoreType.TRUSTSTORE, resolver, removeAlias.getString(), null);
                } else {
                    Enumeration parameterNames = request.getParameterNames();
                    while (parameterNames.hasMoreElements()) {
                        String mapToUser;
                        String parameterName = (String)parameterNames.nextElement();
                        if ("certificate".equals(parameterName)) {
                            String mapToUser2;
                            RequestParameter certificateParam = request.getRequestParameter(parameterName);
                            String string = mapToUser2 = request.getParameter("mapCertificate") != null ? resolver.getUserID() : null;
                            if (certificateParam == null || certificateParam.isFormField()) continue;
                            this.uploadCertToTrustStore(response, resolver, mapToUser2, certificateParam);
                            isRequestProcessed = true;
                            continue;
                        }
                        if (!"keyStore".equals(parameterName)) continue;
                        keyStoreParam = request.getRequestParameter(parameterName);
                        String string = mapToUser = request.getParameter("mapCertificate") != null ? resolver.getUserID() : null;
                        if (keyStoreParam == null || keyStoreParam.isFormField() || keyStorePassParam == null || aliasParam == null) continue;
                        this.importKeyFromKeyStore(resolver, mapToUser, keyStoreParam.getInputStream(), keyStoreParam.getContentType(), keyStorePassParam.getString(), keyStoreTypeParam == null ? null : keyStoreTypeParam.getString(), aliasParam.getString(), keyPasswordParam == null ? null : keyPasswordParam.getString(), newAliasParam == null ? null : newAliasParam.getString(), false);
                        isRequestProcessed = true;
                    }
                    if (!isRequestProcessed) {
                        response.sendError(500, "Cannot process request. Insufficient request parameters.");
                    }
                }
            } else {
                User user = (User)currentResource.adaptTo(User.class);
                if (user != null) {
                    String userId = user.getID();
                    if ("addKeyPair".equals(operation)) {
                        if (aliasParam == null) {
                            log.info("Missing parameter: {}", (Object)"alias");
                            response.sendError(412);
                            return;
                        }
                        KeyPair keyPair = this.cryptoSupport.createKeyPair("RSA");
                        this.keyStoreService.addKeyStoreKeyPair(resolver, userId, keyPair, aliasParam.getString());
                    } else if ("createStore".equals(operation)) {
                        this.createKeyStore(resolver, userId, newPassword, rePassword);
                    } else if ("changePassword".equals(operation)) {
                        this.changeKeyStorePass(resolver, userId, currentPassword, newPassword, rePassword);
                    } else if (removeAlias != null) {
                        this.removeAlias(StoreType.KEYSTORE, resolver, removeAlias.getString(), userId);
                    } else if (pkParam != null && !pkParam.isFormField() && certChainParam != null && certChainParam.length > 0) {
                        this.uploadPrivateKeyAndCertChain(response, resolver, aliasParam, pkParam, certChainParam, userId);
                    } else if (keyStoreParam != null && !keyStoreParam.isFormField() && keyStorePassParam != null && aliasParam != null) {
                        this.importKeyFromKeyStore(resolver, userId, keyStoreParam.getInputStream(), keyStoreParam.getContentType(), keyStorePassParam.getString(), keyStoreTypeParam == null ? null : keyStoreTypeParam.getString(), aliasParam.getString(), keyPasswordParam == null ? null : keyPasswordParam.getString(), newAliasParam == null ? null : newAliasParam.getString(), true);
                    }
                } else {
                    LOG.error("Cannot adapt to user from path {0}", (Object)currentResource.getPath());
                    response.sendError(500, "Cannot adapt to user from path [" + currentResource.getPath() + "]");
                }
            }
        }
        catch (Exception e) {
            throw new ServletException((Throwable)e);
        }
    }

    private void changeTrustStorePass(ResourceResolver resolver, String currentPassword, String newPassword, String rePassword) throws KeyStoreNotInitialisedException {
        if (newPassword.equals(rePassword)) {
            this.keyStoreService.changeTrustStorePassword(resolver, currentPassword.toCharArray(), newPassword.toCharArray());
        }
    }

    private void changeKeyStorePass(ResourceResolver resolver, String userId, String currentPassword, String newPassword, String rePassword) throws KeyStoreNotInitialisedException {
        if (newPassword.equals(rePassword)) {
            this.keyStoreService.changeKeyStorePassword(resolver, userId, currentPassword.toCharArray(), newPassword.toCharArray());
        }
    }

    private void createTrustStore(ResourceResolver resolver, String newPassword, String rePassword) {
        if (newPassword.equals(rePassword)) {
            this.keyStoreService.createTrustStore(resolver, newPassword.toCharArray());
        }
    }

    private void createKeyStore(ResourceResolver resolver, String userId, String newPassword, String rePassword) {
        if (newPassword.equals(rePassword)) {
            this.keyStoreService.createKeyStore(resolver, userId, newPassword.toCharArray());
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void uploadCertToTrustStore(SlingHttpServletResponse response, ResourceResolver resolver, String mapToUser, RequestParameter certificateParam) throws IOException {
        block14 : {
            InputStream certificateIS = certificateParam.getInputStream();
            try {
                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
                X509Certificate certificate = (X509Certificate)certificateFactory.generateCertificate(certificateIS);
                if (mapToUser != null && !"".equals(mapToUser)) {
                    this.certMapping.mapCertificate(resolver, mapToUser, certificate);
                    break block14;
                }
                KeyStore trustStore = this.keyStoreService.getTrustStore(resolver);
                String oldAlias = trustStore.getCertificateAlias(certificate);
                if (oldAlias != null) {
                    trustStore.deleteEntry(oldAlias);
                }
                String alias = "certAlias___" + System.currentTimeMillis();
                trustStore.setCertificateEntry(alias, certificate);
            }
            catch (CertificateException e) {
                LOG.error("Unable to extract a certificate from the uploaded file.", (Throwable)e);
                response.sendError(500);
            }
            catch (KeyStoreException e) {
                LOG.error(e.getMessage(), (Throwable)e);
                response.sendError(500);
            }
            catch (UserCertificateMappingException e) {
                LOG.error("Unable to add certificate to trustore.", (Throwable)e);
                response.sendError(500);
            }
            catch (KeyStoreNotInitialisedException e) {
                LOG.error("Trust store was not initialised.", (Throwable)e);
                response.sendError(500);
            }
            finally {
                if (certificateIS != null) {
                    certificateIS.close();
                }
            }
        }
    }

    private void setJSONHeader(SlingHttpServletResponse response) {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
    }

    private void uploadPrivateKeyAndCertChain(SlingHttpServletResponse response, ResourceResolver resolver, RequestParameter aliasParam, RequestParameter pkParam, RequestParameter[] certChainParam, String userId) throws IOException {
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray((InputStream)pkParam.getInputStream()));
            PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec);
            Certificate[] certChain = new X509Certificate[certChainParam.length];
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            int index = 0;
            for (RequestParameter certChainItem : certChainParam) {
                InputStream certificateIS = certChainItem.getInputStream();
                X509Certificate certificate = (X509Certificate)certificateFactory.generateCertificate(certificateIS);
                certChain[index++] = certificate;
            }
            this.keyStoreService.addKeyStoreKeyEntry(resolver, userId, aliasParam.getString(), (Key)privateKey, certChain);
        }
        catch (Exception e) {
            if (e instanceof IOException) {
                throw (IOException)e;
            }
            LOG.error(e.getMessage(), (Throwable)e);
            response.sendError(500);
        }
    }

    private void importKeyFromKeyStore(ResourceResolver resolver, String mapToUser, InputStream ksStream, String contentType, String keyStorePass, String keyStoreType, String keyAlias, String keyPassword, String newKeyAlias, boolean addPrivateKey) throws GeneralSecurityException, IOException, UserCertificateMappingException, KeyStoreNotInitialisedException {
        keyStoreType = keyStoreType != null ? keyStoreType : this.mimeTypeToStoreTypeMap.get(contentType);
        KeyStore inpKeyStore = KeyStore.getInstance(keyStoreType != null ? keyStoreType : KeyStore.getDefaultType());
        inpKeyStore.load(ksStream, keyStorePass.toCharArray());
        KeyStore.Entry entry = keyPassword == null || "".equals(keyPassword) ? inpKeyStore.getEntry(keyAlias, null) : inpKeyStore.getEntry(keyAlias, new KeyStore.PasswordProtection(keyPassword.toCharArray()));
        if (entry == null) {
            throw new IllegalArgumentException("Key [" + keyAlias + "] not found in keystore");
        }
        boolean isPrivateKey = entry instanceof KeyStore.PrivateKeyEntry;
        if (addPrivateKey && !isPrivateKey) {
            throw new IllegalArgumentException("Key [" + keyAlias + "] is not private key entry. ");
        }
        if (addPrivateKey) {
            if (newKeyAlias == null || "".equals(newKeyAlias)) {
                newKeyAlias = keyAlias;
            }
            KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)entry;
            this.keyStoreService.addKeyStoreKeyEntry(resolver, mapToUser, newKeyAlias, (Key)pkEntry.getPrivateKey(), pkEntry.getCertificateChain());
        } else {
            Certificate cert = null;
            if (entry instanceof KeyStore.PrivateKeyEntry) {
                cert = ((KeyStore.PrivateKeyEntry)entry).getCertificate();
            } else if (entry instanceof KeyStore.TrustedCertificateEntry) {
                cert = ((KeyStore.TrustedCertificateEntry)entry).getTrustedCertificate();
            }
            if (!(cert instanceof X509Certificate)) {
                throw new IllegalArgumentException("Only X509 certificate type supported. Certificate corresponding to [" + keyAlias + "] is of type [" + (cert != null ? cert.getType() : "(null)") + "].");
            }
            if (mapToUser != null && !"".equals(mapToUser)) {
                this.certMapping.mapCertificate(resolver, mapToUser, (X509Certificate)cert);
            } else {
                KeyStore trustStore = this.keyStoreService.getTrustStore(resolver);
                String alias = "certAlias___" + System.currentTimeMillis();
                trustStore.setCertificateEntry(alias, cert);
            }
        }
    }

    private KeyStore getKeyStore(ResourceResolver resolver, String userId) throws KeyStoreNotInitialisedException {
        KeyStore keyStore = resolver.getUserID().equals(userId) ? this.keyStoreService.getKeyStore(resolver) : this.keyStoreService.getKeyStore(resolver, userId);
        return keyStore;
    }

    private void removeAlias(StoreType storeType, ResourceResolver resolver, String alias, String userId) throws IOException {
        KeyStore store = null;
        try {
            switch (storeType) {
                case KEYSTORE: {
                    store = this.getKeyStore(resolver, userId);
                    break;
                }
                case TRUSTSTORE: {
                    store = this.keyStoreService.getTrustStore(resolver);
                }
            }
        }
        catch (KeyStoreNotInitialisedException e) {
            LOG.error("Unable to perform remove alias operation because the store was not initialised.");
        }
        if (store != null) {
            try {
                store.deleteEntry(alias);
            }
            catch (KeyStoreException e) {
                throw new IOException(e);
            }
        }
    }

    protected void bindKeyStoreService(KeyStoreService keyStoreService) {
        this.keyStoreService = keyStoreService;
    }

    protected void unbindKeyStoreService(KeyStoreService keyStoreService) {
        if (this.keyStoreService == keyStoreService) {
            this.keyStoreService = null;
        }
    }

    protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
        this.cryptoSupport = cryptoSupport;
    }

    protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
        if (this.cryptoSupport == cryptoSupport) {
            this.cryptoSupport = null;
        }
    }

    protected void bindCertMapping(UserCertificateMapping userCertificateMapping) {
        this.certMapping = userCertificateMapping;
    }

    protected void unbindCertMapping(UserCertificateMapping userCertificateMapping) {
        if (this.certMapping == userCertificateMapping) {
            this.certMapping = null;
        }
    }

    private static enum StoreType {
        KEYSTORE,
        TRUSTSTORE;
        

        private StoreType() {
        }
    }

}