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

MOSIP-24522 #234

Merged
merged 2 commits into from
Jan 30, 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
18 changes: 15 additions & 3 deletions hub/hub_service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import kafkaHub.util;
import kafkaHub.health_check as healthcheck;
import ballerina/jballerina.java;
import ballerina/crypto;
import ballerina/random;

http:Service healthCheckService = service object {

Expand Down Expand Up @@ -173,6 +174,8 @@ service object {
if config:SECURITY_ON {
check security:authorizeSubscriber(headers, message.hubTopic);
}
byte[] hash = crypto:hashSha256((<string> message.hubSecret).toBytes());
message.hubSecret = hash.toBase64();
log:printInfo("Subscription request received", payload = message);
return websubhub:SUBSCRIPTION_ACCEPTED;
}
Expand All @@ -198,6 +201,8 @@ service object {
log:printError("Subscriber has already registered with the Hub", topic = topicName, callback = message.hubCallback);
return error websubhub:SubscriptionDeniedError("Subscriber has already registered with the Hub");
} else {
byte[] hash = crypto:hashSha256((<string> message.hubSecret).toBytes());
message.hubSecret = hash.toBase64();
log:printInfo("Validation done before sending intent verification", payload = message);
}
}
Expand All @@ -213,10 +218,17 @@ service object {

if (message.hubSecret is string) {
string hubSecret = <string> message.hubSecret;
byte[] data = hubSecret.toBytes();
log:printInfo("Secret before Encryption", secret = hubSecret);
string encryptionKey = config:HUB_SECRET_ENCRYPTION_KEY;
byte[] cipherText = check crypto:encryptAesEcb(data, encryptionKey.toBytes());
message.hubSecret = check string:fromBytes(cipherText);
log:printInfo("Encryption of the hubsecret with configured key", encryptionKey = encryptionKey);
byte[16] initialVector = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
foreach int i in 0...15 {
initialVector[i] = <byte>(check random:createIntInRange(0, 255));
}
log:printInfo("Random generated iv value", iv = initialVector);
byte[] cipherText = check crypto:encryptAesGcm(hubSecret.toBytes(), encryptionKey.toBytes(), initialVector);
cipherText.push(...initialVector);
message.hubSecret = cipherText.toBase64();
}

error? persistingResult = persist:addSubscription(message.cloneReadOnly());
Expand Down
11 changes: 9 additions & 2 deletions hub/start_hub.bal
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import ballerina/mime;
import kafkaHub.config;
import kafkaHub.internal_topic_helper as internalTopicHelper;
import ballerina/crypto;
import ballerina/lang.array;

isolated map<websubhub:TopicRegistration> registeredTopicsCache = {};
isolated map<websubhub:VerifiedSubscription> subscribersCache = {};
Expand Down Expand Up @@ -180,10 +181,16 @@ function startMissingSubscribers(websubhub:VerifiedSubscription[] persistedSubsc

if (subscriber.hubSecret is string) {
string hubSecret = <string>subscriber.hubSecret;
byte[] cipherText = hubSecret.toBytes();
byte[] ivAppendedCipherText = check array:fromBase64(hubSecret);
int cipherLength = ivAppendedCipherText.length();
byte[] cipher = ivAppendedCipherText.slice(0, cipherLength-16);
byte[] iv = ivAppendedCipherText.slice(cipherLength-16, cipherLength);
log:printInfo("Extracted iv before decryption", iv = iv);
string encryptionKey = config:HUB_SECRET_ENCRYPTION_KEY;
byte[] plainText = check crypto:decryptAesEcb(cipherText, encryptionKey.toBytes());
log:printInfo("Key used for decryption", key = encryptionKey);
byte[] plainText = check crypto:decryptAesGcm(cipher, encryptionKey.toBytes(), iv);
subscriber.hubSecret = check string:fromBytes(plainText);
log:printInfo("secret after decryption", secret = subscriber.hubSecret);
}

websubhub:HubClient hubClientEp = check new (subscriber, {
Expand Down
Loading