|
| 1 | +/* |
| 2 | + * ACS AEM Commons |
| 3 | + * |
| 4 | + * Copyright (C) 2024 Konrad Windszus |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.adobe.acs.commons.http.impl; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.security.KeyStore; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import javax.jcr.Node; |
| 25 | +import javax.jcr.Property; |
| 26 | +import javax.jcr.RepositoryException; |
| 27 | +import javax.net.ssl.X509TrustManager; |
| 28 | + |
| 29 | +import org.apache.jackrabbit.api.security.user.Authorizable; |
| 30 | +import org.apache.jackrabbit.api.security.user.User; |
| 31 | +import org.apache.jackrabbit.api.security.user.UserManager; |
| 32 | +import org.apache.sling.api.SlingIOException; |
| 33 | +import org.apache.sling.api.resource.LoginException; |
| 34 | +import org.apache.sling.api.resource.Resource; |
| 35 | +import org.apache.sling.api.resource.ResourceResolver; |
| 36 | +import org.apache.sling.api.resource.ResourceResolverFactory; |
| 37 | +import org.apache.sling.api.resource.ResourceUtil; |
| 38 | +import org.apache.sling.serviceusermapping.ServiceUserMapped; |
| 39 | +import org.jetbrains.annotations.NotNull; |
| 40 | +import org.osgi.service.component.annotations.Activate; |
| 41 | +import org.osgi.service.component.annotations.Component; |
| 42 | +import org.osgi.service.component.annotations.Reference; |
| 43 | + |
| 44 | +import com.adobe.granite.crypto.CryptoException; |
| 45 | +import com.adobe.granite.crypto.CryptoSupport; |
| 46 | +import com.adobe.granite.keystore.KeyStoreService; |
| 47 | + |
| 48 | +@Component(service=AemKeyStoreFactory.class) |
| 49 | +public class AemKeyStoreFactory { |
| 50 | + |
| 51 | + private static final String SUB_SERVICE_NAME = "key-store-factory"; |
| 52 | + |
| 53 | + private static final Map<String, Object> SERVICE_USER = Map.of(ResourceResolverFactory.SUBSERVICE, |
| 54 | + SUB_SERVICE_NAME); |
| 55 | + |
| 56 | + /** Defer starting the service until service user mapping is available. */ |
| 57 | + @Reference(target = "(|(" + ServiceUserMapped.SUBSERVICENAME + "=" + SUB_SERVICE_NAME + ")(!(" |
| 58 | + + ServiceUserMapped.SUBSERVICENAME + "=*)))") |
| 59 | + private ServiceUserMapped serviceUserMapped; |
| 60 | + |
| 61 | + private final ResourceResolverFactory resolverFactory; |
| 62 | + private final KeyStoreService keyStoreService; |
| 63 | + private final CryptoSupport cryptoSupport; |
| 64 | + |
| 65 | + @Activate() |
| 66 | + public AemKeyStoreFactory(@Reference ResourceResolverFactory resolverFactory, |
| 67 | + @Reference KeyStoreService keyStoreService, |
| 68 | + @Reference CryptoSupport cryptoSupport) { |
| 69 | + this.resolverFactory = resolverFactory; |
| 70 | + this.keyStoreService = keyStoreService; |
| 71 | + this.cryptoSupport = cryptoSupport; |
| 72 | + } |
| 73 | + |
| 74 | + /** @return the global AEM trust store |
| 75 | + * @throws LoginException |
| 76 | + * @see <a href= |
| 77 | + * "https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/security/call-internal-apis-having-private-certificate.html?lang=en">Call |
| 78 | + * internal APIs having private certificates</a> */ |
| 79 | + public @NotNull X509TrustManager getTrustManager() throws LoginException { |
| 80 | + try (final var serviceResolver = getKeyStoreResourceResolver()) { |
| 81 | + return (X509TrustManager) keyStoreService.getTrustManager(serviceResolver); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** @return the global AEM trust store |
| 86 | + * @throws LoginException |
| 87 | + * @see <a href= |
| 88 | + * "https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/security/call-internal-apis-having-private-certificate.html?lang=en">Call |
| 89 | + * internal APIs having private certificates</a> */ |
| 90 | + public @NotNull KeyStore getTrustStore() throws LoginException { |
| 91 | + try (final var serviceResolver = getKeyStoreResourceResolver()) { |
| 92 | + var aemTrustStore = keyStoreService.getTrustStore(serviceResolver); |
| 93 | + return aemTrustStore; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public @NotNull KeyStore getKeyStore(@NotNull final String userId) throws LoginException { |
| 98 | + try (final var serviceResolver = getKeyStoreResourceResolver()) { |
| 99 | + // using the password set for the user Id's keystore to decrypt the entry |
| 100 | + return keyStoreService.getKeyStore(serviceResolver, userId); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public @NotNull char[] getKeyStorePassword(@NotNull final String userId) throws LoginException { |
| 105 | + try (final var serviceResolver = getKeyStoreResourceResolver()) { |
| 106 | + User user = retrieveUser(serviceResolver, userId); |
| 107 | + String path = getKeyStorePathForUser(user, "store.p12"); |
| 108 | + return extractStorePassword(serviceResolver, path, cryptoSupport); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private @NotNull ResourceResolver getKeyStoreResourceResolver() throws LoginException { |
| 113 | + return this.resolverFactory.getServiceResourceResolver(SERVICE_USER); |
| 114 | + } |
| 115 | + |
| 116 | + // the following methods are extracted from com.adobe.granite.keystore.internal.KeyStoreServiceImpl, because there is no public method |
| 117 | + // for retrieving the keystore's password |
| 118 | + private static User retrieveUser(ResourceResolver resolver, String userId) |
| 119 | + throws IllegalArgumentException, SlingIOException { |
| 120 | + UserManager userManager = (UserManager) resolver.adaptTo(UserManager.class); |
| 121 | + if (userManager != null) { |
| 122 | + Authorizable authorizable; |
| 123 | + try { |
| 124 | + authorizable = userManager.getAuthorizable(userId); |
| 125 | + } catch (RepositoryException var6) { |
| 126 | + throw new SlingIOException(new IOException(var6)); |
| 127 | + } |
| 128 | + |
| 129 | + if (authorizable != null && !authorizable.isGroup()) { |
| 130 | + User user = (User) authorizable; |
| 131 | + return user; |
| 132 | + } else { |
| 133 | + throw new IllegalArgumentException("The provided userId does not identify an existing user."); |
| 134 | + } |
| 135 | + } else { |
| 136 | + throw new IllegalArgumentException("Cannot obtain a UserManager for the given resource resolver."); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private static String getKeyStorePathForUser(User user, String keyStoreFileName) throws SlingIOException { |
| 141 | + String userHome; |
| 142 | + try { |
| 143 | + userHome = user.getPath(); |
| 144 | + } catch (RepositoryException var4) { |
| 145 | + throw new SlingIOException(new IOException(var4)); |
| 146 | + } |
| 147 | + return userHome + "/" + "keystore" + "/" + keyStoreFileName; |
| 148 | + } |
| 149 | + |
| 150 | + private static char[] extractStorePassword(ResourceResolver resolver, String storePath, CryptoSupport cryptoSupport) |
| 151 | + throws SecurityException { |
| 152 | + Resource storeResource = resolver.getResource(storePath); |
| 153 | + if (storeResource != null) { |
| 154 | + Node storeParentNode = (Node) storeResource.getParent().adaptTo(Node.class); |
| 155 | + |
| 156 | + try { |
| 157 | + Property passwordProperty = storeParentNode.getProperty("keystorePassword"); |
| 158 | + if (passwordProperty != null) { |
| 159 | + return cryptoSupport.unprotect(passwordProperty.getString()).toCharArray(); |
| 160 | + } else { |
| 161 | + throw new SecurityException( |
| 162 | + "Missing 'keystorePassword' property on " + ResourceUtil.getParent(storePath)); |
| 163 | + } |
| 164 | + } catch (RepositoryException var6) { |
| 165 | + throw new SecurityException(var6); |
| 166 | + } catch (CryptoException var7) { |
| 167 | + throw new SecurityException(var7); |
| 168 | + } |
| 169 | + } else { |
| 170 | + return null; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments