-
Notifications
You must be signed in to change notification settings - Fork 180
Add ECDSA support #326
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
Open
Zeta201
wants to merge
2
commits into
wso2:master
Choose a base branch
from
Zeta201:ecdsa-keypair
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add ECDSA support #326
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
....keystore.mgt/src/main/java/org/wso2/carbon/keystore/mgt/util/TenantKeyPairConstants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * 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.keystore.mgt.util; | ||
|
|
||
| /* | ||
| * Constants required for Tenant KeyPair Utility. | ||
| */ | ||
| public class TenantKeyPairConstants { | ||
|
|
||
| // Constants required for EC key pair generation | ||
| public static final String EC_KEY_ALG = "EC"; | ||
| public static final String EC_SHA256 = "SHA256withECDSA"; | ||
| public static final String EC_CURVE = "secp256r1"; | ||
|
|
||
| // Supported signature algorithms for public certificate generation. | ||
| public static final String RSA_MD5 = "MD5withRSA"; | ||
| public static final String RSA_SHA1 = "SHA1withRSA"; | ||
| public static final String RSA_SHA256 = "SHA256withRSA"; | ||
| public static final String RSA_SHA384 = "SHA384withRSA"; | ||
| public static final String RSA_SHA512 = "SHA512withRSA"; | ||
|
|
||
| public static final String RSA_KEY_ALG = "RSA"; | ||
| } | ||
127 changes: 127 additions & 0 deletions
127
...enant.keystore.mgt/src/main/java/org/wso2/carbon/keystore/mgt/util/TenantKeyPairUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * 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.keystore.mgt.util; | ||
|
|
||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.bouncycastle.asn1.x500.X500Name; | ||
| import org.bouncycastle.asn1.x509.Extension; | ||
| import org.bouncycastle.asn1.x509.GeneralName; | ||
| import org.bouncycastle.asn1.x509.GeneralNames; | ||
| import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; | ||
| import org.bouncycastle.cert.X509v3CertificateBuilder; | ||
| import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; | ||
| import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; | ||
| import org.wso2.carbon.core.util.CryptoUtil; | ||
| import org.wso2.carbon.keystore.mgt.KeyStoreMgtException; | ||
|
|
||
| import java.math.BigInteger; | ||
| import java.security.KeyPair; | ||
| import java.security.KeyPairGenerator; | ||
| import java.security.KeyStore; | ||
| import java.security.PublicKey; | ||
| import java.security.SecureRandom; | ||
| import java.security.cert.Certificate; | ||
| import java.security.cert.X509Certificate; | ||
| import java.security.spec.ECGenParameterSpec; | ||
| import java.util.Date; | ||
|
|
||
| import static org.wso2.carbon.core.util.CryptoUtil.getJCEProvider; | ||
| import static org.wso2.carbon.keystore.mgt.util.TenantKeyPairConstants.EC_CURVE; | ||
| import static org.wso2.carbon.keystore.mgt.util.TenantKeyPairConstants.EC_KEY_ALG; | ||
| import static org.wso2.carbon.keystore.mgt.util.TenantKeyPairConstants.RSA_KEY_ALG; | ||
|
|
||
| /** | ||
| * Utility for provisioning and validating tenant specific key pairs required. | ||
| */ | ||
| public class TenantKeyPairUtil { | ||
|
|
||
| private static final Log LOG = LogFactory.getLog(TenantKeyPairUtil.class); | ||
|
|
||
| private TenantKeyPairUtil() { | ||
| } | ||
|
|
||
| /** | ||
| * Generate and store key pair with self-signed certificate in tenant keystore. | ||
| * | ||
| * @param tenantDomain tenant domain | ||
| * @param ksPassword keystore password | ||
| * @param keyStore tenant keystore | ||
| * @param alias key alias | ||
| * @param keyType key type | ||
| * @param sigAlgId signature algorithm ID | ||
| * @return generated certificate | ||
| * @throws KeyStoreMgtException keystore exception | ||
| */ | ||
| public static X509Certificate addKeyEntry(String tenantDomain, String ksPassword, KeyStore keyStore, | ||
| String alias, String keyType, String sigAlgId) | ||
| throws KeyStoreMgtException { | ||
|
|
||
| try { | ||
| CryptoUtil.getDefaultCryptoUtil(); | ||
| KeyPairGenerator kpg; | ||
|
|
||
| if (RSA_KEY_ALG.equals(keyType)) { | ||
| kpg = KeyPairGenerator.getInstance(RSA_KEY_ALG); | ||
| kpg.initialize(2048); | ||
| } else if (EC_KEY_ALG.equals(keyType)) { | ||
| kpg = KeyPairGenerator.getInstance(EC_KEY_ALG); | ||
| kpg.initialize(new ECGenParameterSpec(EC_CURVE)); | ||
| } else { | ||
| throw new IllegalArgumentException("Unsupported key type: " + keyType); | ||
| } | ||
|
|
||
| KeyPair keyPair = kpg.generateKeyPair(); | ||
| X509Certificate cert = generateCertificate(tenantDomain, keyPair, sigAlgId); | ||
|
|
||
| keyStore.setKeyEntry(alias, keyPair.getPrivate(), ksPassword.toCharArray(), | ||
| new Certificate[]{cert}); | ||
| return cert; | ||
| } catch (Exception e) { | ||
| String errorMsg = "Error while generating the certificate for tenant :" + tenantDomain + "."; | ||
| LOG.error(errorMsg, e); | ||
| throw new KeyStoreMgtException(errorMsg, e); | ||
| } | ||
| } | ||
|
|
||
| private static X509Certificate generateCertificate(String tenantDomain, KeyPair keyPair, String algorithmId) | ||
| throws Exception { | ||
|
|
||
| String commonName = "CN=" + tenantDomain + ", OU=None, O=None, L=None, C=None"; | ||
| X500Name dn = new X500Name(commonName); | ||
|
|
||
| Date notBefore = new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30); | ||
| Date notAfter = new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * 10)); | ||
| BigInteger serialNumber = new BigInteger(32, new SecureRandom()); | ||
|
|
||
| PublicKey publicKey = keyPair.getPublic(); | ||
| SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()); | ||
|
|
||
| X509v3CertificateBuilder certBuilder = new X509v3CertificateBuilder( | ||
| dn, serialNumber, notBefore, notAfter, dn, publicKeyInfo); | ||
| certBuilder.addExtension(Extension.subjectAlternativeName, false, | ||
| new GeneralNames(new GeneralName(GeneralName.dNSName, tenantDomain))); | ||
|
|
||
| JcaContentSignerBuilder signerBuilder = new JcaContentSignerBuilder(algorithmId); | ||
| signerBuilder.setProvider(getJCEProvider()); | ||
|
|
||
| return new JcaX509CertificateConverter() | ||
| .setProvider(getJCEProvider()) | ||
| .getCertificate(certBuilder.build(signerBuilder.build(keyPair.getPrivate()))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider removing or deprecating weak signature algorithm constants.
MD5withRSAandSHA1withRSAuse hash functions that are no longer recommended for new cryptographic applications. Including them as constants may encourage continued use. If backward compatibility is required, consider annotating these with@Deprecatedand adding documentation discouraging their use.🤖 Prompt for AI Agents
Source: Linters/SAST tools