Skip to content

Commit

Permalink
Add an utility class for gateway certificate management
Browse files Browse the repository at this point in the history
Add an utility class for gateway certificate management
Add new configuration enable_certificate_chain_validation to api-manager.xml.j2
  • Loading branch information
SavinduDimal committed Mar 20, 2024
1 parent 0bea973 commit 60f0dab
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.wso2.carbon.apimgt.impl.caching.CacheProvider;
import org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.apimgt.impl.utils.GatewayCertificateMgtUtil;
import org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder;
import org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore;
import org.wso2.carbon.context.PrivilegedCarbonContext;
Expand Down Expand Up @@ -564,10 +565,10 @@ private static boolean isClientCertificateEncoded() {
public static X509Certificate getCertificateFromListenerTrustStore(String certSubjectDN)
throws APIManagementException {

Enumeration<String> aliases = APIUtil.getAliasesFromListenerTrustStore();
Enumeration<String> aliases = GatewayCertificateMgtUtil.getAliasesFromListenerTrustStore();

Check warning on line 568 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/Utils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/Utils.java#L568

Added line #L568 was not covered by tests
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
Certificate certificate = APIUtil.getCertificateFromListenerTrustStore(alias);
Certificate certificate = GatewayCertificateMgtUtil.getCertificateFromListenerTrustStore(alias);

Check warning on line 571 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/Utils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/Utils.java#L570-L571

Added lines #L570 - L571 were not covered by tests
if (certificate instanceof X509Certificate) {
X509Certificate x509Certificate = (X509Certificate) certificate;

Check warning on line 573 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/Utils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/Utils.java#L573

Added line #L573 was not covered by tests
if (StringUtils.equals(x509Certificate.getSubjectDN().getName(), certSubjectDN)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private void setAuthContext(MessageContext messageContext, Certificate[] certifi
subjectDNIdentifiers.add(subjectDNIdentifier);

Check warning on line 207 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/authenticator/MutualSSLAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/authenticator/MutualSSLAuthenticator.java#L205-L207

Added lines #L205 - L207 were not covered by tests
for (Map.Entry<String, String> entry : certificates.entrySet()) {
String key = entry.getKey();

Check warning on line 209 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/authenticator/MutualSSLAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/authenticator/MutualSSLAuthenticator.java#L209

Added line #L209 was not covered by tests
if (key.contains(subjectDNIdentifier)) {
if (StringUtils.equals(subjectDNIdentifier, key)) {
uniqueIdentifier = key;
tier = entry.getValue();
break;

Check warning on line 213 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/authenticator/MutualSSLAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/authenticator/MutualSSLAuthenticator.java#L211-L213

Added lines #L211 - L213 were not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8330,30 +8330,6 @@ public static Certificate getCertificateFromParentTrustStore(String certAlias) t
return publicCert;
}

/**
* Fetches certificate for given certificate alias from listener trust store.
* @param certAlias Certificate alias
* @return Certificate
* @throws APIManagementException
*/
public static Certificate getCertificateFromListenerTrustStore(String certAlias) throws APIManagementException {

Certificate publicCert = null;
try {
KeyStore trustStore = ServiceReferenceHolder.getInstance().getListenerTrustStore();
if (trustStore != null) {
// Read public certificate from trust store
publicCert = trustStore.getCertificate(certAlias);
}
} catch (KeyStoreException e) {
String msg = "Error while retrieving public certificate with alias : "
+ certAlias;
log.error(msg, e);
throw new APIManagementException(msg, e);
}
return publicCert;
}

/**
* Verify the JWT token signature.
* <p>
Expand Down Expand Up @@ -8785,26 +8761,6 @@ public static boolean isCertificateExistsInListenerTrustStore(Certificate certif
return false;
}

/**
* Fetches all the trusted certificate aliases from listener trust store.
* @return Trusted certificate aliases
* @throws APIManagementException
*/
public static Enumeration<String> getAliasesFromListenerTrustStore() throws APIManagementException {

try {
KeyStore trustStore = ServiceReferenceHolder.getInstance().getListenerTrustStore();
if (trustStore != null) {
return trustStore.aliases();
}
} catch (KeyStoreException e) {
String msg = "Error getting certificate aliases from trust store";
log.error(msg, e);
throw new APIManagementException(msg, e);
}
return null;
}

public static boolean isDevPortalAnonymous() {

APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.apimgt.impl.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;

import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.Certificate;
import java.util.Enumeration;

public class GatewayCertificateMgtUtil {

Check warning on line 30 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java#L30

Added line #L30 was not covered by tests

private static final Log log = LogFactory.getLog(GatewayCertificateMgtUtil.class);

Check warning on line 32 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java#L32

Added line #L32 was not covered by tests

/**
* Fetches all the trusted certificate aliases from listener trust store.
*
* @return Trusted certificate aliases
* @throws APIManagementException
*/
public static Enumeration<String> getAliasesFromListenerTrustStore() throws APIManagementException {

try {
KeyStore trustStore = ServiceReferenceHolder.getInstance().getListenerTrustStore();

Check warning on line 43 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java#L43

Added line #L43 was not covered by tests
if (trustStore != null) {
return trustStore.aliases();

Check warning on line 45 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java#L45

Added line #L45 was not covered by tests
}
} catch (KeyStoreException e) {
String msg = "Error getting certificate aliases from trust store";
log.error(msg, e);
throw new APIManagementException(msg, e);
}
return null;

Check warning on line 52 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java#L47-L52

Added lines #L47 - L52 were not covered by tests
}

/**
* Fetches certificate for given certificate alias from listener trust store.
*
* @param certAlias Certificate alias
* @return Certificate
* @throws APIManagementException
*/
public static Certificate getCertificateFromListenerTrustStore(String certAlias) throws APIManagementException {

Certificate publicCert = null;

Check warning on line 64 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java#L64

Added line #L64 was not covered by tests
try {
KeyStore trustStore = ServiceReferenceHolder.getInstance().getListenerTrustStore();

Check warning on line 66 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java#L66

Added line #L66 was not covered by tests
if (trustStore != null) {
// Read public certificate from trust store
publicCert = trustStore.getCertificate(certAlias);

Check warning on line 69 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java#L69

Added line #L69 was not covered by tests
}
} catch (KeyStoreException e) {
String msg = "Error while retrieving public certificate with alias : " + certAlias;
log.error(msg, e);
throw new APIManagementException(msg, e);
}
return publicCert;

Check warning on line 76 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/GatewayCertificateMgtUtil.java#L71-L76

Added lines #L71 - L76 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,9 @@
<MutualSSL>
<ClientCertificateHeader>{{apimgt.mutual_ssl.certificate_header}}</ClientCertificateHeader>
<EnableClientCertificateValidation>{{apimgt.mutual_ssl.enable_client_validation}}</EnableClientCertificateValidation>
{% if apimgt.mutual_ssl.enable_certificate_chain_validation is defined %}
<EnableCertificateChainValidation>{{apimgt.mutual_ssl.enable_certificate_chain_validation}}</EnableCertificateChainValidation>
{% endif %}
{% if apimgt.mutual_ssl.client_certificate_encode is defined %}
<ClientCertificateEncode>{{apimgt.mutual_ssl.client_certificate_encode}}</ClientCertificateEncode>
{% endif %}
Expand Down

0 comments on commit 60f0dab

Please sign in to comment.