Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cookie] Introduce of context keystore for GetCookie, SetCookie Fn for signing #198

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.central.log.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.security.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.crypto</groupId>
<artifactId>org.wso2.carbon.crypto.impl</artifactId>
Expand Down Expand Up @@ -183,6 +187,8 @@
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.authentication.framework.config.model.graph;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.security.keystore.service.*;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.core.util; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.central.log.mgt.*; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.user.core; version="${carbon.kernel.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsServletRequest;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsServletResponse;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.conditional.auth.functions.http.internal.HTTPFunctionsServiceHolder;
import org.wso2.carbon.identity.conditional.auth.functions.http.util.HTTPConstants;
import org.wso2.carbon.identity.core.util.IdentityUtil;

Expand All @@ -43,6 +44,8 @@
import java.util.Optional;
import javax.servlet.http.Cookie;

import static org.wso2.carbon.identity.conditional.auth.functions.http.util.HTTPConstants.KEY_STORE_CONTEXT;

/**
* Implementation of the setCookie and getCookieValue functions.
*/
Expand Down Expand Up @@ -74,7 +77,11 @@ public void setCookie(JsServletResponse response, String name, Object... params)
if (sign) {
try {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
signature = Base64.encode(IdentityUtil.signWithTenantKey(value, tenantDomain));
// getCookie, setCookie functionalities uses a functionality specific keystore.
// The below code will create the keystore for this context on-demand if it does not exist.
HTTPFunctionsServiceHolder.getInstance().getIdentityKeyStoreGenerator()
.generateKeyStore(tenantDomain, KEY_STORE_CONTEXT);
signature = Base64.encode(IdentityUtil.signWithTenantKey(value, tenantDomain, KEY_STORE_CONTEXT));
} catch (Exception e) {
log.error("Error occurred when signing the cookie value.", e);
return;
Expand Down Expand Up @@ -186,11 +193,7 @@ public String getCookieValue(JsServletRequest request, Object... params) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getTenantDomain();
boolean isValid = IdentityUtil.validateSignatureFromTenant(valueString, signature,
tenantDomain);
// Fallback mechanism for already signed cookies.
if (!isValid) {
isValid = SignatureUtil.validateSignature(valueString, signature);
}
tenantDomain, KEY_STORE_CONTEXT);
if (!isValid) {
log.error("Cookie signature didn't matched with the cookie value.");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.wso2.carbon.core.util.CryptoUtil;
import org.wso2.carbon.core.util.SignatureUtil;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsServletRequest;
import org.wso2.carbon.identity.conditional.auth.functions.http.internal.HTTPFunctionsServiceHolder;
import org.wso2.carbon.identity.conditional.auth.functions.http.util.HTTPConstants;
import org.wso2.carbon.identity.core.util.IdentityUtil;

Expand All @@ -40,6 +41,8 @@

import javax.servlet.http.Cookie;

import static org.wso2.carbon.identity.conditional.auth.functions.http.util.HTTPConstants.KEY_STORE_CONTEXT;

/**
* Implementation of GetCookieFunction.
*/
Expand Down Expand Up @@ -103,11 +106,8 @@ public String getCookieValue(JsServletRequest request, Object... params) {
try {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getTenantDomain();
boolean isValid = IdentityUtil.validateSignatureFromTenant(valueString, signature, tenantDomain);
// Fallback mechanism for already signed cookies.
if (!isValid) {
isValid = SignatureUtil.validateSignature(valueString, signature);
}
boolean isValid = IdentityUtil.validateSignatureFromTenant(valueString, signature,
tenantDomain, KEY_STORE_CONTEXT);
if (!isValid) {
log.error("Cookie signature didn't matched with the cookie value.");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
import org.wso2.carbon.core.util.CryptoUtil;
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsServletResponse;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.conditional.auth.functions.http.internal.HTTPFunctionsServiceHolder;
import org.wso2.carbon.identity.conditional.auth.functions.http.util.HTTPConstants;
import org.wso2.carbon.identity.core.util.IdentityUtil;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;

import static org.wso2.carbon.identity.conditional.auth.functions.http.util.HTTPConstants.KEY_STORE_CONTEXT;

/**
* Implementation of SetCookieFunction.
*/
Expand Down Expand Up @@ -68,7 +71,11 @@ public void setCookie(JsServletResponse response, String name, Object... params)
if (sign) {
try {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
signature = Base64.encode(IdentityUtil.signWithTenantKey(value, tenantDomain));
// getCookie, setCookie functionalities uses a functionality specific keystore.
// The below code will create the keystore for this context on-demand if it does not exist.
HTTPFunctionsServiceHolder.getInstance().getIdentityKeyStoreGenerator()
Thumimku marked this conversation as resolved.
Show resolved Hide resolved
.generateKeyStore(tenantDomain, KEY_STORE_CONTEXT);
signature = Base64.encode(IdentityUtil.signWithTenantKey(value, tenantDomain, KEY_STORE_CONTEXT));
Thumimku marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
log.error("Error occurred when signing the cookie value.", e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.wso2.carbon.identity.conditional.auth.functions.http.HTTPPostFunctionImpl;
import org.wso2.carbon.identity.conditional.auth.functions.http.SetCookieFunctionImpl;
import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent;
import org.wso2.carbon.security.keystore.service.IdentityKeyStoreGenerator;

/**
* OSGi declarative services component which handle cookie related conditional auth functions.
Expand Down Expand Up @@ -112,4 +113,20 @@ protected void unsetIdentityCoreInitializedEventService(IdentityCoreInitializedE
/* reference IdentityCoreInitializedEvent service to guarantee that this component will wait until identity core
is started */
}

@Reference(
service = IdentityKeyStoreGenerator.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetIdentityKeyStoreGenerator"
)
public void setIdentityKeyStoreGenerator(IdentityKeyStoreGenerator identityKeyStoreGenerator) {

HTTPFunctionsServiceHolder.getInstance().setIdentityKeyStoreGenerator(identityKeyStoreGenerator);
}

public void unsetIdentityKeyStoreGenerator(IdentityKeyStoreGenerator identityKeyStoreGenerator) {

HTTPFunctionsServiceHolder.getInstance().setIdentityKeyStoreGenerator(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
package org.wso2.carbon.identity.conditional.auth.functions.http.internal;

import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry;
import org.wso2.carbon.security.keystore.service.IdentityKeyStoreGenerator;

public class HTTPFunctionsServiceHolder {

private static HTTPFunctionsServiceHolder instance = new HTTPFunctionsServiceHolder();

private JsFunctionRegistry jsFunctionRegistry;
private IdentityKeyStoreGenerator identityKeyStoreGenerator;

public static HTTPFunctionsServiceHolder getInstance() {

Expand All @@ -44,4 +46,14 @@ public void setJsFunctionRegistry(JsFunctionRegistry jsFunctionRegistry) {

this.jsFunctionRegistry = jsFunctionRegistry;
}

public IdentityKeyStoreGenerator getIdentityKeyStoreGenerator() {

return identityKeyStoreGenerator;
}

public void setIdentityKeyStoreGenerator(IdentityKeyStoreGenerator identityKeyStoreGenerator) {

this.identityKeyStoreGenerator = identityKeyStoreGenerator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ public class HTTPConstants {
public static final String DECRYPT = "decrypt";
public static final String VALUE = "value";
public static final String SIGNATURE = "signature";
public static final String KEY_STORE_CONTEXT = "cookie";
}
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@
<artifactId>org.wso2.carbon.identity.core</artifactId>
<version>${carbon.identity.framework.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.security.mgt</artifactId>
<version>${carbon.identity.framework.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.nashorn</groupId>
<artifactId>nashorn-core</artifactId>
Expand Down Expand Up @@ -523,7 +528,7 @@
<carbon.kernel.version>4.10.22</carbon.kernel.version>
<carbon.kernel.package.import.version.range>[4.6.0, 5.0.0)</carbon.kernel.package.import.version.range>
<carbon.user.package.import.version.range>[1.0.1, 2.0.0)</carbon.user.package.import.version.range>
<carbon.identity.framework.version>7.7.22</carbon.identity.framework.version>
<carbon.identity.framework.version>7.7.34</carbon.identity.framework.version>
<identity.organization.management.core.version>1.0.89</identity.organization.management.core.version>
<carbon.identity.framework.testutils.version>5.20.447</carbon.identity.framework.testutils.version>
<carbon.identity.package.import.version.range>[5.14.0, 8.0.0)</carbon.identity.package.import.version.range>
Expand Down
Loading