|
|
@ -1,3 +1,21 @@
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* WSO2 Inc. 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.certificate.mgt.core.impl;
|
|
|
|
package org.wso2.carbon.certificate.mgt.core.impl;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
@ -20,43 +38,38 @@ import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.exception.CertificateManagementException;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.exception.CertificateManagementException;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementServiceImpl;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementServiceImpl;
|
|
|
|
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.service.PaginationResult;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.util.CSRGenerator;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.util.CSRGenerator;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.util.CertificateManagementConstants;
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.util.CertificateManagementConstants;
|
|
|
|
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.util.DummyCertificate;
|
|
|
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
|
|
|
import sun.misc.BASE64Encoder;
|
|
|
|
import sun.misc.BASE64Encoder;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.math.BigInteger;
|
|
|
|
|
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.security.PrivateKey;
|
|
|
|
import java.security.PrivateKey;
|
|
|
|
import java.security.Security;
|
|
|
|
import java.security.Security;
|
|
|
|
import java.security.cert.Certificate;
|
|
|
|
import java.security.cert.*;
|
|
|
|
import java.security.cert.CertificateEncodingException;
|
|
|
|
|
|
|
|
import java.security.cert.X509Certificate;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
//@RunWith(PowerMockRunner.class)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//@PowerMockIgnore({"javax.xml.*","java.sql.*", "org.xml.sax.*", "org.w3c.dom.*", "org.springframework.context.*", "org.apache.log4j.*"})
|
|
|
|
|
|
|
|
//@PrepareForTest(org.wso2.carbon.certificate.mgt.core.util.CommonUtil.class)
|
|
|
|
|
|
|
|
public class CertificateManagementServiceImplTests extends BaseDeviceManagementCertificateTest {
|
|
|
|
public class CertificateManagementServiceImplTests extends BaseDeviceManagementCertificateTest {
|
|
|
|
|
|
|
|
|
|
|
|
private static Log log = LogFactory.getLog(CertificateManagementServiceImplTests.class);
|
|
|
|
private static Log log = LogFactory.getLog(CertificateManagementServiceImplTests.class);
|
|
|
|
private static final String CA_CERT_PEM = "src/test/resources/ca_cert.pem";
|
|
|
|
private static final String CA_CERT_PEM = "src/test/resources/ca_cert.pem";
|
|
|
|
private static final String RA_CERT_PEM = "src/test/resources/ra_cert.pem";
|
|
|
|
private static final String RA_CERT_PEM = "src/test/resources/ra_cert.pem";
|
|
|
|
private static final String CA_CERT_DER = "src/test/resources/ca_cert.der";
|
|
|
|
|
|
|
|
CertificateManagementServiceImpl managementService = null;
|
|
|
|
CertificateManagementServiceImpl managementService = null;
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests initialization of CertificateManagementServiceImpl instance")
|
|
|
|
public void testGetInstance() {
|
|
|
|
public void testGetInstance() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
CertificateManagementServiceImpl instance = CertificateManagementServiceImpl.getInstance();
|
|
|
|
CertificateManagementServiceImpl instance = CertificateManagementServiceImpl.getInstance();
|
|
|
|
Assert.assertNotNull(instance);
|
|
|
|
Assert.assertNotNull(instance);
|
|
|
|
log.info("Successfully created instance");
|
|
|
|
log.info("getInstance Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
log.error("Error while initializing CertificateManagementService", e);
|
|
|
|
log.error("Error while initializing CertificateManagementService", e);
|
|
|
@ -68,19 +81,21 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
@BeforeClass
|
|
|
|
public void initCertificateManagementService() throws DeviceManagementException {
|
|
|
|
public void initCertificateManagementService() throws DeviceManagementException {
|
|
|
|
|
|
|
|
//save certificatemanagementservice instance as class variable
|
|
|
|
managementService = CertificateManagementServiceImpl.getInstance();
|
|
|
|
managementService = CertificateManagementServiceImpl.getInstance();
|
|
|
|
|
|
|
|
//set Bouncycastle as a provider for testing
|
|
|
|
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
|
|
|
|
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests retrieval of CA Certificate from the keystore")
|
|
|
|
public void testGetCACertificate() {
|
|
|
|
public void testGetCACertificate() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
CertificateManagementServiceImpl instance = CertificateManagementServiceImpl.getInstance();
|
|
|
|
CertificateManagementServiceImpl instance = CertificateManagementServiceImpl.getInstance();
|
|
|
|
Certificate caCertificate = instance.getCACertificate();
|
|
|
|
Certificate caCertificate = instance.getCACertificate();
|
|
|
|
Assert.assertNotNull(caCertificate);
|
|
|
|
Assert.assertNotNull(caCertificate);
|
|
|
|
Assert.assertEquals(caCertificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
Assert.assertEquals(caCertificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
log.info("Successfully returned CA Certificate");
|
|
|
|
log.info("GetCACertificate Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
String msg = "Error while getting the CA Certificate";
|
|
|
|
String msg = "Error while getting the CA Certificate";
|
|
|
@ -89,13 +104,13 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests retrieval of RA Certificate from the keystore")
|
|
|
|
public void testGetRACertificate() {
|
|
|
|
public void testGetRACertificate() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Certificate raCertificate = managementService.getRACertificate();
|
|
|
|
Certificate raCertificate = managementService.getRACertificate();
|
|
|
|
Assert.assertNotNull(raCertificate);
|
|
|
|
Assert.assertNotNull(raCertificate);
|
|
|
|
Assert.assertEquals(raCertificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
Assert.assertEquals(raCertificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
log.info("Successfully returned RA Certificate");
|
|
|
|
log.info("GetRACertificate Test Successful");
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
String msg = "Error while getting the RA Certificate";
|
|
|
|
String msg = "Error while getting the RA Certificate";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
@ -103,12 +118,13 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case test generation of root certificates")
|
|
|
|
public void testGetRootCertificate() {
|
|
|
|
public void testGetRootCertificate() {
|
|
|
|
File caCert = new File(CA_CERT_PEM);
|
|
|
|
File caCert = new File(CA_CERT_PEM);
|
|
|
|
File raCert = new File(RA_CERT_PEM);
|
|
|
|
File raCert = new File(RA_CERT_PEM);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
//read file to byte arrays
|
|
|
|
byte[] caBytes = FileUtils.readFileToByteArray(caCert);
|
|
|
|
byte[] caBytes = FileUtils.readFileToByteArray(caCert);
|
|
|
|
byte[] raBytes = FileUtils.readFileToByteArray(raCert);
|
|
|
|
byte[] raBytes = FileUtils.readFileToByteArray(raCert);
|
|
|
|
|
|
|
|
|
|
|
@ -116,7 +132,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
Assert.assertNotNull(rootCertificates);
|
|
|
|
Assert.assertNotNull(rootCertificates);
|
|
|
|
Assert.assertEquals(rootCertificates.get(0).getType(), CertificateManagementConstants.X_509);
|
|
|
|
Assert.assertEquals(rootCertificates.get(0).getType(), CertificateManagementConstants.X_509);
|
|
|
|
Assert.assertEquals(rootCertificates.get(1).getType(), CertificateManagementConstants.X_509);
|
|
|
|
Assert.assertEquals(rootCertificates.get(1).getType(), CertificateManagementConstants.X_509);
|
|
|
|
log.info("Successfully returned root Certificate");
|
|
|
|
log.info("GetRootCertificate Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
String msg = "Error reading byte streams";
|
|
|
|
String msg = "Error reading byte streams";
|
|
|
@ -130,13 +146,14 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests generation of X509Certificate")
|
|
|
|
public void testGenerateX509Certificate() {
|
|
|
|
public void testGenerateX509Certificate() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
|
X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
|
Assert.assertNotNull(x509Certificate);
|
|
|
|
Assert.assertNotNull(x509Certificate);
|
|
|
|
Assert.assertEquals(x509Certificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
Assert.assertEquals(x509Certificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
|
|
|
|
log.info("GenerateX509Certificate Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
String msg = "Error while generating X509 Certificate";
|
|
|
|
String msg = "Error while generating X509 Certificate";
|
|
|
@ -145,13 +162,14 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests retrieving SCEP CA Certificate")
|
|
|
|
public void testGetCACertSCEP() {
|
|
|
|
public void testGetCACertSCEP() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
SCEPResponse caCertSCEP = managementService.getCACertSCEP();
|
|
|
|
SCEPResponse caCertSCEP = managementService.getCACertSCEP();
|
|
|
|
Assert.assertNotNull(caCertSCEP);
|
|
|
|
Assert.assertNotNull(caCertSCEP);
|
|
|
|
Assert.assertEquals(caCertSCEP.getResultCriteria(), CAStatus.CA_RA_CERT_RECEIVED);
|
|
|
|
Assert.assertEquals(caCertSCEP.getResultCriteria(), CAStatus.CA_RA_CERT_RECEIVED);
|
|
|
|
|
|
|
|
log.info("GetCACertSCEP Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
String msg = "Error while Retrieving CA Certificate";
|
|
|
|
String msg = "Error while Retrieving CA Certificate";
|
|
|
@ -167,11 +185,12 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
byte[] caCapsSCEP = managementService.getCACapsSCEP();
|
|
|
|
byte[] caCapsSCEP = managementService.getCACapsSCEP();
|
|
|
|
Assert.assertNotNull(caCapsSCEP);
|
|
|
|
Assert.assertNotNull(caCapsSCEP);
|
|
|
|
Assert.assertEquals(caCapsSCEP, CertificateManagementConstants.POST_BODY_CA_CAPS.getBytes());
|
|
|
|
Assert.assertEquals(caCapsSCEP, CertificateManagementConstants.POST_BODY_CA_CAPS.getBytes());
|
|
|
|
|
|
|
|
log.info("GetCACapsSCEP Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests generation of a X509Certificate from a CSR")
|
|
|
|
public void testGenerateCertificateFromCSR() {
|
|
|
|
public void testGenerateCertificateFromCSR() {
|
|
|
|
CSRGenerator csrGeneration = new CSRGenerator();
|
|
|
|
CSRGenerator csrGeneration = new CSRGenerator();
|
|
|
|
KeyStoreReader keyStoreReader = new KeyStoreReader();
|
|
|
|
KeyStoreReader keyStoreReader = new KeyStoreReader();
|
|
|
@ -190,6 +209,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(x509Certificate);
|
|
|
|
Assert.assertNotNull(x509Certificate);
|
|
|
|
Assert.assertEquals(x509Certificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
Assert.assertEquals(x509Certificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
|
|
|
|
log.info("GenerateCertificateFromCSR Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
String msg = "Error while reading Certificates from the keystore";
|
|
|
|
String msg = "Error while reading Certificates from the keystore";
|
|
|
@ -204,7 +224,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests retrieval of a Certificate from the keystore from the Serial Number")
|
|
|
|
public void testGetCertificateBySerial() {
|
|
|
|
public void testGetCertificateBySerial() {
|
|
|
|
|
|
|
|
|
|
|
|
X509Certificate x509Certificate = null;
|
|
|
|
X509Certificate x509Certificate = null;
|
|
|
@ -217,6 +237,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(certificateBySerial);
|
|
|
|
Assert.assertNotNull(certificateBySerial);
|
|
|
|
Assert.assertEquals(certificateBySerial.getSerialNumber(), x509Certificate.getSerialNumber().toString());
|
|
|
|
Assert.assertEquals(certificateBySerial.getSerialNumber(), x509Certificate.getSerialNumber().toString());
|
|
|
|
|
|
|
|
log.info("GetCertificateBySerial Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
String msg = "Error while receiving the certificate";
|
|
|
|
String msg = "Error while receiving the certificate";
|
|
|
@ -231,7 +252,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests retrieval of a Certificate from the keystore from the Alias")
|
|
|
|
public void testGetCertificateByAlias() {
|
|
|
|
public void testGetCertificateByAlias() {
|
|
|
|
X509Certificate x509Certificate = null;
|
|
|
|
X509Certificate x509Certificate = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -245,6 +266,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(certificateByAlias);
|
|
|
|
Assert.assertNotNull(certificateByAlias);
|
|
|
|
Assert.assertEquals(certificateByAlias.getType(), CertificateManagementConstants.X_509);
|
|
|
|
Assert.assertEquals(certificateByAlias.getType(), CertificateManagementConstants.X_509);
|
|
|
|
|
|
|
|
log.info("GetCertificateByAlias Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
String msg = "Error while receiving the certificate";
|
|
|
|
String msg = "Error while receiving the certificate";
|
|
|
@ -257,7 +279,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests Signature verification of a Certificate against the keystore")
|
|
|
|
public void testVerifySignature() {
|
|
|
|
public void testVerifySignature() {
|
|
|
|
BASE64Encoder encoder = new BASE64Encoder();
|
|
|
|
BASE64Encoder encoder = new BASE64Encoder();
|
|
|
|
|
|
|
|
|
|
|
@ -278,6 +300,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(verifySignature);
|
|
|
|
Assert.assertNotNull(verifySignature);
|
|
|
|
Assert.assertTrue(verifySignature);
|
|
|
|
Assert.assertTrue(verifySignature);
|
|
|
|
|
|
|
|
log.info("VerifySignature Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (CertificateEncodingException e) {
|
|
|
|
} catch (CertificateEncodingException e) {
|
|
|
|
String msg = "Error in Certificate encoding";
|
|
|
|
String msg = "Error in Certificate encoding";
|
|
|
@ -300,22 +323,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(description = "This test case tests DN verification of a Certificate against the keystore")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public void testVerifyPEMSignature() throws KeystoreException, DeviceManagementException {
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// DeviceConfigurationManager.getInstance().initConfig();
|
|
|
|
|
|
|
|
// X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// PowerMockito.mockStatic(CommonUtil.class);
|
|
|
|
|
|
|
|
// PowerMockito.when(CommonUtil.generateSerialNumber()).thenReturn(new BigInteger("12345"));
|
|
|
|
|
|
|
|
// CertificateResponse certificateResponse = managementService.verifyPEMSignature(x509Certificate);
|
|
|
|
|
|
|
|
// Assert.assertNotNull(certificateResponse);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void testVerifySubjectDN() {
|
|
|
|
public void testVerifySubjectDN() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceConfigurationManager.getInstance().initConfig();
|
|
|
|
DeviceConfigurationManager.getInstance().initConfig();
|
|
|
@ -325,54 +333,75 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
managementService.verifySubjectDN(x509Certificate.getIssuerDN().getName());
|
|
|
|
managementService.verifySubjectDN(x509Certificate.getIssuerDN().getName());
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
String msg = "Error while initilizing DeviceConfigurationManager";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests retrieval of a Certificate from the keystore from the Serial")
|
|
|
|
public void testRetrieveCertificate(){
|
|
|
|
public void testRetrieveCertificate() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
|
X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
|
CertificateResponse certificateResponse = managementService.retrieveCertificate(x509Certificate.getSerialNumber().toString());
|
|
|
|
CertificateResponse certificateResponse = managementService.retrieveCertificate(x509Certificate.getSerialNumber().toString());
|
|
|
|
Assert.assertNotNull(certificateResponse);
|
|
|
|
Assert.assertNotNull(certificateResponse);
|
|
|
|
Assert.assertEquals(x509Certificate.getSerialNumber(),certificateResponse.getCertificateserial());
|
|
|
|
Assert.assertEquals(x509Certificate.getSerialNumber(), certificateResponse.getCertificateserial());
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
} catch (CertificateManagementException e) {
|
|
|
|
} catch (CertificateManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
String msg = " Error occurred while looking up for the certificate in the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests the retrieval of Certificates from keystore in desired pagination")
|
|
|
|
public void testGetAllCertificates() throws CertificateManagementException {
|
|
|
|
public void testGetAllCertificatesPaginated() throws CertificateManagementException {
|
|
|
|
managementService.getAllCertificates(1,1);
|
|
|
|
try {
|
|
|
|
|
|
|
|
managementService.generateX509Certificate();
|
|
|
|
|
|
|
|
managementService.generateX509Certificate();
|
|
|
|
|
|
|
|
PaginationResult allCertificates = managementService.getAllCertificates(0, 2);
|
|
|
|
|
|
|
|
Assert.assertEquals(allCertificates.getData().size(), 2);
|
|
|
|
|
|
|
|
log.info("GetAllCertificatesPaginated Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test casae tests retrieval of all Certificates from keystore")
|
|
|
|
public void testGetCertificates(){
|
|
|
|
public void testGetCertificates() throws CertificateManagementException {
|
|
|
|
try{
|
|
|
|
try {
|
|
|
|
List<CertificateResponse> certificatesBefore = managementService.getCertificates();
|
|
|
|
List<CertificateResponse> certificatesBefore = managementService.getCertificates();
|
|
|
|
X509Certificate x509Certificate1 = managementService.generateX509Certificate();
|
|
|
|
managementService.generateX509Certificate();
|
|
|
|
X509Certificate x509Certificate2 = managementService.generateX509Certificate();
|
|
|
|
managementService.generateX509Certificate();
|
|
|
|
List<CertificateResponse> certificatesAfter = managementService.getCertificates();
|
|
|
|
List<CertificateResponse> certificatesAfter = managementService.getCertificates();
|
|
|
|
Assert.assertNotNull(certificatesBefore);
|
|
|
|
Assert.assertNotNull(certificatesBefore);
|
|
|
|
Assert.assertNotNull(certificatesAfter);
|
|
|
|
Assert.assertNotNull(certificatesAfter);
|
|
|
|
Assert.assertEquals((certificatesBefore.size() + 2),certificatesAfter.size());
|
|
|
|
Assert.assertEquals((certificatesBefore.size() + 2), certificatesAfter.size());
|
|
|
|
|
|
|
|
log.info("GetCertificates Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (CertificateManagementException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests deleting Certificate from the keystore")
|
|
|
|
public void testGetCertificatesWithParams(){
|
|
|
|
public void testRemoveCertificate() throws CertificateManagementException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
|
X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
@ -385,37 +414,181 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(removed);
|
|
|
|
Assert.assertNotNull(removed);
|
|
|
|
Assert.assertTrue(removed);
|
|
|
|
Assert.assertTrue(removed);
|
|
|
|
Assert.assertEquals((size-1),sizeAfter);
|
|
|
|
Assert.assertEquals((size - 1), sizeAfter);
|
|
|
|
|
|
|
|
log.info("RemoveCertificate Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (CertificateManagementException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test(description = "This test case tests searching for a list of certificates by the serial number")
|
|
|
|
public void testSearchCertificates(){
|
|
|
|
public void testSearchCertificates() throws CertificateManagementException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
|
X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
|
List<CertificateResponse> certificateResponses = managementService.searchCertificates(x509Certificate.getSerialNumber().toString());
|
|
|
|
List<CertificateResponse> certificateResponses = managementService.searchCertificates(x509Certificate.getSerialNumber().toString());
|
|
|
|
Assert.assertNotNull(certificateResponses);
|
|
|
|
Assert.assertNotNull(certificateResponses);
|
|
|
|
Assert.assertEquals(1,certificateResponses.size());
|
|
|
|
Assert.assertEquals(1, certificateResponses.size());
|
|
|
|
Assert.assertEquals(certificateResponses.get(0).getSerialNumber(),x509Certificate.getSerialNumber().toString());
|
|
|
|
Assert.assertEquals(certificateResponses.get(0).getSerialNumber(), x509Certificate.getSerialNumber().toString());
|
|
|
|
|
|
|
|
log.info("SearchCertificates Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
} catch (CertificateManagementException e) {
|
|
|
|
log.error(msg, e);
|
|
|
|
e.printStackTrace();
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(description = "This test case tests generation of signed Certificate from a CSR")
|
|
|
|
|
|
|
|
public void testGetSignedCertificateFromCSR() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CSRGenerator csrGeneration = new CSRGenerator();
|
|
|
|
|
|
|
|
BASE64Encoder encoder = new BASE64Encoder();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate key pair
|
|
|
|
|
|
|
|
KeyPair keyPair = csrGeneration.generateKeyPair("RSA", 1024);
|
|
|
|
|
|
|
|
byte[] csrData = csrGeneration.generateCSR("SHA256WithRSA", keyPair);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
X509Certificate signedCertificateFromCSR = managementService.getSignedCertificateFromCSR(encoder.encode(csrData));
|
|
|
|
|
|
|
|
Assert.assertNotNull(signedCertificateFromCSR);
|
|
|
|
|
|
|
|
Assert.assertEquals(signedCertificateFromCSR.getType(), CertificateManagementConstants.X_509);
|
|
|
|
|
|
|
|
log.info("GetSignedCertificateFromCSR Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(description = "This test case tests the extraction of Challenge token from a Certificate")
|
|
|
|
|
|
|
|
public void testExtractChallengeToken() throws KeystoreException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
X509Certificate x509Certificate1 = new DummyCertificate();
|
|
|
|
|
|
|
|
String token = managementService.extractChallengeToken(x509Certificate1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(token);
|
|
|
|
|
|
|
|
Assert.assertEquals(token, DummyCertificate.EXT);
|
|
|
|
|
|
|
|
log.info("extractChallengeToken Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(description = "This test case tests saving a list of Certificates in the keystore")
|
|
|
|
|
|
|
|
public void testSaveCertificate() throws CertificateManagementException {
|
|
|
|
|
|
|
|
File caCert = new File(CA_CERT_PEM);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int before = managementService.getCertificates().size();
|
|
|
|
|
|
|
|
byte[] caBytes = FileUtils.readFileToByteArray(caCert);
|
|
|
|
|
|
|
|
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
|
|
|
|
|
|
|
X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(caBytes));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<org.wso2.carbon.certificate.mgt.core.bean.Certificate> certificates = new ArrayList<>();
|
|
|
|
|
|
|
|
org.wso2.carbon.certificate.mgt.core.bean.Certificate certificateToStore =
|
|
|
|
|
|
|
|
new org.wso2.carbon.certificate.mgt.core.bean.Certificate();
|
|
|
|
|
|
|
|
certificateToStore.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
|
|
|
|
|
|
|
|
certificateToStore.setCertificate(cert);
|
|
|
|
|
|
|
|
certificates.add(certificateToStore);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
managementService.saveCertificate(certificates);
|
|
|
|
|
|
|
|
int after = managementService.getCertificates().size();
|
|
|
|
|
|
|
|
Assert.assertEquals((before + 1), after);
|
|
|
|
|
|
|
|
log.info("SaveCertificate Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
String msg = "Error while reading Pem file from the file";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (CertificateException e) {
|
|
|
|
|
|
|
|
String msg = "Error while Converting Pem file to X509 Certificate";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(description = "This test case tests converting a pem file to X509 Certificate")
|
|
|
|
|
|
|
|
public void testPemToX509Certificate() {
|
|
|
|
|
|
|
|
File caCert = new File(CA_CERT_PEM);
|
|
|
|
|
|
|
|
BASE64Encoder encoder = new BASE64Encoder();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
byte[] caBytes = FileUtils.readFileToByteArray(caCert);
|
|
|
|
|
|
|
|
X509Certificate certificate = managementService.pemToX509Certificate(encoder.encode(caBytes));
|
|
|
|
|
|
|
|
Assert.assertNotNull(certificate);
|
|
|
|
|
|
|
|
Assert.assertEquals(certificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
|
|
|
|
log.info("PemToX509Certificate Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
String msg = "Error while reading Pem file from the file";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(description = "This test case tests extracting Certificate from the header Signature")
|
|
|
|
|
|
|
|
public void testExtractCertificateFromSignature() {
|
|
|
|
|
|
|
|
BASE64Encoder encoder = new BASE64Encoder();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
//generate and save a certificate in the keystore
|
|
|
|
|
|
|
|
X509Certificate x509Certificate = managementService.generateX509Certificate();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Generate CMSdata
|
|
|
|
|
|
|
|
CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
|
|
|
|
|
|
|
|
List<X509Certificate> list = new ArrayList<>();
|
|
|
|
|
|
|
|
list.add(x509Certificate);
|
|
|
|
|
|
|
|
JcaCertStore store = new JcaCertStore(list);
|
|
|
|
|
|
|
|
generator.addCertificates(store);
|
|
|
|
|
|
|
|
CMSSignedData degenerateSd = generator.generate(new CMSAbsentContent());
|
|
|
|
|
|
|
|
byte[] signature = degenerateSd.getEncoded();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
X509Certificate certificate = managementService.extractCertificateFromSignature(encoder.encode(signature));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertNotNull(certificate);
|
|
|
|
|
|
|
|
Assert.assertEquals(certificate.getType(), CertificateManagementConstants.X_509);
|
|
|
|
|
|
|
|
log.info("ExtractCertificateFromSignature Test Successful");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (CertificateEncodingException e) {
|
|
|
|
|
|
|
|
String msg = "Error in Certificate encoding";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
String msg = "Error reading encoded signature";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
} catch (CMSException e) {
|
|
|
|
|
|
|
|
String msg = "Error Adding certificates";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
} catch (KeystoreException e) {
|
|
|
|
|
|
|
|
String msg = "Error while accessing the keystore";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
Assert.fail(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
@BeforeClass
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void init() throws Exception {
|
|
|
|
public void init() throws Exception {
|
|
|
|
initDataSource();
|
|
|
|
initDataSource();
|
|
|
|
CertificateManagementDAOFactory.init(this.getDataSource());
|
|
|
|
CertificateManagementDAOFactory.init(this.getDataSource());
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|