Extract certificate from signature

revert-70aa11f8
Dilshan Edirisuriya 9 years ago
parent aeae7a09f0
commit 454c459172

@ -64,7 +64,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigInteger;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.KeyFactory; import java.security.KeyFactory;
import java.security.KeyPair; import java.security.KeyPair;
@ -158,10 +157,9 @@ public class CertificateGenerator {
keyPairGenerator.initialize(ConfigurationUtil.RSA_KEY_LENGTH, new SecureRandom()); keyPairGenerator.initialize(ConfigurationUtil.RSA_KEY_LENGTH, new SecureRandom());
KeyPair pair = keyPairGenerator.generateKeyPair(); KeyPair pair = keyPairGenerator.generateKeyPair();
X500Principal principal = new X500Principal(ConfigurationUtil.DEFAULT_PRINCIPAL); X500Principal principal = new X500Principal(ConfigurationUtil.DEFAULT_PRINCIPAL);
BigInteger serial = BigInteger.valueOf(System.currentTimeMillis());
X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder( X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(
principal, serial, validityBeginDate, validityEndDate, principal, CommonUtil.generateSerialNumber(), validityBeginDate, validityEndDate,
principal, pair.getPublic()); principal, pair.getPublic());
ContentSigner contentSigner = new JcaContentSignerBuilder(ConfigurationUtil.SHA256_RSA) ContentSigner contentSigner = new JcaContentSignerBuilder(ConfigurationUtil.SHA256_RSA)
.setProvider(ConfigurationUtil.PROVIDER).build( .setProvider(ConfigurationUtil.PROVIDER).build(
@ -285,9 +283,14 @@ public class CertificateGenerator {
} }
public boolean verifySignature(String headerSignature) throws KeystoreException { public boolean verifySignature(String headerSignature) throws KeystoreException {
Certificate certificate = extractCertificateFromSignature(headerSignature);
return (certificate != null);
}
public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException {
if (headerSignature == null || headerSignature.isEmpty()) { if (headerSignature == null || headerSignature.isEmpty()) {
return false; return null;
} }
try { try {
@ -308,8 +311,8 @@ public class CertificateGenerator {
Certificate lookUpCertificate = keyStoreReader.getCertificateByAlias( Certificate lookUpCertificate = keyStoreReader.getCertificateByAlias(
reqCert.getSerialNumber().toString()); reqCert.getSerialNumber().toString());
if (lookUpCertificate != null) { if (lookUpCertificate != null && (lookUpCertificate instanceof X509Certificate)) {
return true; return (X509Certificate)lookUpCertificate;
} }
} }
@ -328,7 +331,7 @@ public class CertificateGenerator {
throw new KeystoreException(errorMsg, e); throw new KeystoreException(errorMsg, e);
} }
return false; return null;
} }
public X509Certificate generateCertificateFromCSR(PrivateKey privateKey, public X509Certificate generateCertificateFromCSR(PrivateKey privateKey,
@ -353,7 +356,7 @@ public class CertificateGenerator {
} }
X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder( X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder(
new X500Name(issueSubject), BigInteger.valueOf(System.currentTimeMillis()), new X500Name(issueSubject), CommonUtil.generateSerialNumber(),
validityBeginDate, validityEndDate, certSubject, request.getSubjectPublicKeyInfo()); validityBeginDate, validityEndDate, certSubject, request.getSubjectPublicKeyInfo());
ContentSigner sigGen; ContentSigner sigGen;

@ -49,4 +49,6 @@ public interface CertificateManagementService {
Certificate getCertificateByAlias(String alias) throws KeystoreException; Certificate getCertificateByAlias(String alias) throws KeystoreException;
boolean verifySignature(String headerSignature) throws KeystoreException; boolean verifySignature(String headerSignature) throws KeystoreException;
public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException;
} }

@ -92,4 +92,8 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
public boolean verifySignature(String headerSignature) throws KeystoreException { public boolean verifySignature(String headerSignature) throws KeystoreException {
return certificateGenerator.verifySignature(headerSignature); return certificateGenerator.verifySignature(headerSignature);
} }
public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException {
return certificateGenerator.extractCertificateFromSignature(headerSignature);
}
} }

@ -17,6 +17,7 @@
*/ */
package org.wso2.carbon.certificate.mgt.core.util; package org.wso2.carbon.certificate.mgt.core.util;
import java.math.BigInteger;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -40,4 +41,8 @@ public class CommonUtil {
return calendar.getTime(); return calendar.getTime();
} }
public static synchronized BigInteger generateSerialNumber() {
return BigInteger.valueOf(System.currentTimeMillis());
}
} }

@ -88,7 +88,11 @@
org.wso2.carbon.user.core.tenant, org.wso2.carbon.user.core.tenant,
org.wso2.carbon.utils, org.wso2.carbon.utils,
org.wso2.carbon.utils.multitenancy, org.wso2.carbon.utils.multitenancy,
org.xml.sax org.xml.sax,
javax.servlet.http,
javax.xml,
org.apache.axis2.transport.http,
org.wso2.carbon.apimgt.impl
</Import-Package> </Import-Package>
<!--<Fragment-Host>tomcat</Fragment-Host>--> <!--<Fragment-Host>tomcat</Fragment-Host>-->
</instructions> </instructions>

Loading…
Cancel
Save