From 813db2bd54603b5f4abe5c057bcbbfd9dadeae3e Mon Sep 17 00:00:00 2001 From: Nandhukumar Date: Tue, 11 Jun 2024 11:57:05 +0530 Subject: [PATCH] MOSIP-33327 Signed-off-by: Nandhukumar --- .../src/main/java/UnitTests.java | 5 +- .../auth/dto/CertificateChainResponseDto.java | 2 +- .../auth/dto/CryptomanagerRequestDto.java | 2 +- .../auth/dto/EncryptionRequestDto.java | 2 +- .../auth/dto/EncryptionResponseDto.java | 2 +- .../io/mosip/testrig/auth/util/AuthUtil.java | 21 +- .../testrig/auth/util/CertificateTypes.java | 2 +- .../testrig/auth/util/CryptoCoreUtil.java | 2 +- .../mosip/testrig/auth/util/CryptoUtil.java | 8 +- .../io/mosip/testrig/auth/util/Encrypt.java | 77 ++--- .../auth/util/JWSSignAndVerifyController.java | 7 +- .../testrig/auth/util/KeyMgrUtility.java | 83 +++-- .../mosip/testrig/auth/util/PartnerTypes.java | 2 +- .../testrig/auth/util/PropertiesReader.java | 2 +- .../apirig/admin/fw/util/BioDataUtility.java | 43 ++- .../fw/util/EncryptionDecrptionUtil.java | 293 +++++++++--------- .../testrig/apirig/testscripts/BioAuth.java | 4 + .../testNgXmlFiles/authenticationApi.xml | 16 +- 18 files changed, 298 insertions(+), 275 deletions(-) diff --git a/AuthenticationUtil/src/main/java/UnitTests.java b/AuthenticationUtil/src/main/java/UnitTests.java index 61d886b6aa..cc41971567 100644 --- a/AuthenticationUtil/src/main/java/UnitTests.java +++ b/AuthenticationUtil/src/main/java/UnitTests.java @@ -1,5 +1,6 @@ -import Util.AuthUtil; -import helper.PartnerTypes; +import io.mosip.testrig.auth.util.AuthUtil; +import io.mosip.testrig.auth.util.PartnerTypes; + import org.bouncycastle.operator.OperatorCreationException; import java.io.IOException; diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/CertificateChainResponseDto.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/CertificateChainResponseDto.java index 1cc1d8d8fc..055b6f1277 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/CertificateChainResponseDto.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/CertificateChainResponseDto.java @@ -1,4 +1,4 @@ -package dto; +package io.mosip.testrig.auth.dto; import lombok.Data; @Data diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/CryptomanagerRequestDto.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/CryptomanagerRequestDto.java index a2561181eb..b44f71c96d 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/CryptomanagerRequestDto.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/CryptomanagerRequestDto.java @@ -1,4 +1,4 @@ -package dto; +package io.mosip.testrig.auth.dto; import lombok.Data; diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/EncryptionRequestDto.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/EncryptionRequestDto.java index 08bb6c4f79..ed2b7c887a 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/EncryptionRequestDto.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/EncryptionRequestDto.java @@ -1,4 +1,4 @@ -package dto; +package io.mosip.testrig.auth.dto; import lombok.Data; diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/EncryptionResponseDto.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/EncryptionResponseDto.java index 6db332fb48..a85b72e14f 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/EncryptionResponseDto.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/dto/EncryptionResponseDto.java @@ -1,4 +1,4 @@ -package dto; +package io.mosip.testrig.auth.dto; import lombok.Data; diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/AuthUtil.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/AuthUtil.java index 4484e5dac8..3ce07ed304 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/AuthUtil.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/AuthUtil.java @@ -1,10 +1,7 @@ -package Util; +package io.mosip.testrig.auth.util; import com.fasterxml.jackson.databind.ObjectMapper; -import dto.EncryptionRequestDto; -import dto.EncryptionResponseDto; -import helper.*; -import dto.CertificateChainResponseDto; + import io.mosip.authentication.core.constant.IdAuthenticationErrorConstants; import io.mosip.authentication.core.exception.IdAuthenticationAppException; import io.mosip.authentication.core.exception.IdAuthenticationBusinessException; @@ -15,6 +12,10 @@ import io.mosip.kernel.core.templatemanager.spi.TemplateManager; import io.mosip.kernel.core.util.CryptoUtil; import io.mosip.kernel.core.util.DateUtils; +import io.mosip.testrig.auth.dto.CertificateChainResponseDto; +import io.mosip.testrig.auth.dto.EncryptionRequestDto; +import io.mosip.testrig.auth.dto.EncryptionResponseDto; + import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.bouncycastle.operator.OperatorCreationException; @@ -100,13 +101,13 @@ public AuthUtil() { public void clearKeys(String certsDir, String moduleName, String targetEnv) throws IOException { - KeyMgrUtil keyMgrUtil = new KeyMgrUtil(); + KeyMgrUtility keyMgrUtil = new KeyMgrUtility(); keyMgrUtil.deleteFile(new File(keyMgrUtil.getKeysDirPath(certsDir, moduleName, targetEnv).toString())); } public CertificateChainResponseDto generatePartnerKeys( PartnerTypes partnerType, String partnerName, boolean keyFileNameByPartnerName, String certsDir, String moduleName, String targetEnv) throws UnrecoverableEntryException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, OperatorCreationException { - KeyMgrUtil keyMgrUtil = new KeyMgrUtil(); + KeyMgrUtility keyMgrUtil = new KeyMgrUtility(); return keyMgrUtil.getPartnerCertificates(partnerType, keyMgrUtil.getKeysDirPath(certsDir, moduleName, targetEnv), partnerName, keyFileNameByPartnerName); } @@ -115,7 +116,7 @@ public String updatePartnerCertificate( PartnerTypes partnerType, String partnerName, boolean keyFileNameByPartnerName, Map requestData, String certsDir, String moduleName, String targetEnv) throws CertificateException, IOException, NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException { - KeyMgrUtil keyMgrUtil = new KeyMgrUtil(); + KeyMgrUtility keyMgrUtil = new KeyMgrUtility(); String certificateData = requestData.get("certData"); String filePrepend = partnerType.getFilePrepend(); @@ -134,7 +135,7 @@ public String uploadIDACertificate( String moduleName, String targetEnv) throws CertificateException, IOException { - KeyMgrUtil keyMgrUtil = new KeyMgrUtil(); + KeyMgrUtility keyMgrUtil = new KeyMgrUtility(); String certificateData = requestData.get("certData"); String fileName = certificateType.getFileName(); @@ -266,7 +267,7 @@ public String signRequest( String targetEnv) throws JoseException, NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException, CertificateException, IOException, OperatorCreationException { - KeyMgrUtil keyMgrUtil = new KeyMgrUtil(); + KeyMgrUtility keyMgrUtil = new KeyMgrUtility(); JWSSignAndVerifyController jWSSignAndVerifyController = new JWSSignAndVerifyController(); return jWSSignAndVerifyController.sign(request, false, true, false, null, keyMgrUtil.getKeysDirPath(certsDir, moduleName, targetEnv), partnerType, partnerName, keyFileNameByPartnerName); diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CertificateTypes.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CertificateTypes.java index 47258fcce9..1303611415 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CertificateTypes.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CertificateTypes.java @@ -1,4 +1,4 @@ -package helper; +package io.mosip.testrig.auth.util; public enum CertificateTypes { diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CryptoCoreUtil.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CryptoCoreUtil.java index 157d6b97e5..02bd4f4fd3 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CryptoCoreUtil.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CryptoCoreUtil.java @@ -1,4 +1,4 @@ -package helper; +package io.mosip.testrig.auth.util; import io.mosip.kernel.core.crypto.exception.InvalidParamSpecException; import io.mosip.kernel.core.util.CryptoUtil; diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CryptoUtil.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CryptoUtil.java index 4ee0cc4315..686f8c5422 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CryptoUtil.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/CryptoUtil.java @@ -1,4 +1,4 @@ -package helper; +package io.mosip.testrig.auth.util; import io.mosip.kernel.core.crypto.spi.CryptoCoreSpec; import org.bouncycastle.jce.provider.BouncyCastleProvider; @@ -16,7 +16,7 @@ * The Class CryptoUtil. */ @Component -public class CryptoUtility { +public class CryptoUtil { /** The Constant SYM_ALGORITHM. */ private static final String SYM_ALGORITHM = "AES"; @@ -98,8 +98,8 @@ private static BouncyCastleProvider addProvider() { public SecretKey genSecKey() throws NoSuchAlgorithmException { KeyGenerator keyGen; SecretKey secretKey = null; - keyGen = KeyGenerator.getInstance(CryptoUtility.SYM_ALGORITHM, bouncyCastleProvider); - keyGen.init(CryptoUtility.SYM_ALGORITHM_LENGTH, new SecureRandom()); + keyGen = KeyGenerator.getInstance(CryptoUtil.SYM_ALGORITHM, bouncyCastleProvider); + keyGen.init(CryptoUtil.SYM_ALGORITHM_LENGTH, new SecureRandom()); secretKey = keyGen.generateKey(); return secretKey; diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/Encrypt.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/Encrypt.java index 7b8a3ad5bc..d2d7044f23 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/Encrypt.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/Encrypt.java @@ -1,22 +1,14 @@ -package Util; +package io.mosip.testrig.auth.util; import java.io.ByteArrayInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.GeneralSecurityException; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; -import java.security.spec.InvalidKeySpecException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -24,70 +16,39 @@ import java.util.Map; import java.util.stream.IntStream; +import javax.crypto.SecretKey; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import javax.xml.bind.DatatypeConverter; + +import org.apache.commons.codec.binary.Base64; +import org.json.JSONException; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.env.Environment; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpRequest; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseCookie; import org.springframework.http.ResponseEntity; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; -import org.springframework.lang.Nullable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestClientException; +import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.util.UriComponentsBuilder; -import javax.crypto.BadPaddingException; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.SecretKey; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; -import javax.xml.bind.DatatypeConverter; - -import dto.EncryptionRequestDto; -import dto.EncryptionResponseDto; -import helper.CryptoUtility; -import helper.PropertiesReader; -import org.apache.commons.codec.binary.Base64; -import org.apache.commons.lang.ArrayUtils; -import org.bouncycastle.operator.OperatorCreationException; -import org.json.JSONException; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.mosip.authentication.core.constant.IdAuthConfigKeyConstants; -import io.mosip.authentication.core.logger.IdaLogger; -import io.mosip.authentication.core.util.BytesUtil; import io.mosip.kernel.core.http.RequestWrapper; -import io.mosip.kernel.core.logger.spi.Logger; -import io.mosip.kernel.core.util.CryptoUtil; import io.mosip.kernel.core.util.DateUtils; import io.mosip.kernel.core.util.HMACUtils2; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.web.client.RestTemplate; - +import io.mosip.testrig.auth.dto.EncryptionRequestDto; +import io.mosip.testrig.auth.dto.EncryptionResponseDto; +@Component public class Encrypt { private static final String SSL = "SSL"; @@ -95,6 +56,9 @@ public class Encrypt { String appID = "${application.id}"; String keySplitter = "#KEY_SPLITTER#"; + + @Autowired + CryptoUtil cryptoUtil; public EncryptionResponseDto encrypt(EncryptionRequestDto encryptionRequestDto, String refId, @@ -109,7 +73,6 @@ public EncryptionResponseDto encrypt(EncryptionRequestDto encryptionRequestDto, private EncryptionResponseDto kernelEncrypt(EncryptionRequestDto encryptionRequestDto, String refId) throws Exception { ObjectMapper objMapper = new ObjectMapper(); - CryptoUtility cryptoUtil = new CryptoUtility(); String identityBlock = objMapper.writeValueAsString(encryptionRequestDto.getIdentityRequest()); SecretKey secretKey = cryptoUtil.genSecKey(); @@ -147,7 +110,7 @@ private String getRefId(boolean isInternal, boolean isBiometrics) { } @SuppressWarnings({"unchecked", "rawtypes"}) - private X509Certificate getCertificate(String refId) throws IOException, KeyManagementException, + public X509Certificate getCertificate(String refId) throws IOException, KeyManagementException, NoSuchAlgorithmException, JSONException, CertificateException { turnOffSslChecking(); RestTemplate restTemplate = new RestTemplate(); @@ -230,7 +193,7 @@ private String generateAuthToken() { public SplittedEncryptedData splitEncryptedData(String data) throws Exception { //boolean encryptedDataHasVersion = env.getProperty("encryptedDataHasVersion", boolean.class, false); boolean encryptedDataHasVersion = false; - byte[] dataBytes = CryptoUtil.decodeURLSafeBase64(data); + byte[] dataBytes = io.mosip.kernel.core.util.CryptoUtil.decodeURLSafeBase64(data); byte[][] splits = splitAtFirstOccurance(dataBytes, keySplitter.getBytes()); byte[] thumbPrintAndSessionKey = splits[0]; byte[] sessionKey; @@ -246,7 +209,7 @@ public SplittedEncryptedData splitEncryptedData(String data) throws Exception { } byte[] encryptedData = splits[1]; - return new SplittedEncryptedData(CryptoUtil.encodeToURLSafeBase64(sessionKey), CryptoUtil.encodeToURLSafeBase64(encryptedData), digestAsPlainText(thumbPrint)); + return new SplittedEncryptedData(io.mosip.kernel.core.util.CryptoUtil.encodeToURLSafeBase64(sessionKey), io.mosip.kernel.core.util.CryptoUtil.encodeToURLSafeBase64(encryptedData), digestAsPlainText(thumbPrint)); } public static class SplittedEncryptedData { diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/JWSSignAndVerifyController.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/JWSSignAndVerifyController.java index b9c8370ac0..cc02ea2062 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/JWSSignAndVerifyController.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/JWSSignAndVerifyController.java @@ -1,7 +1,5 @@ -package Util; +package io.mosip.testrig.auth.util; -import helper.KeyMgrUtil; -import helper.PartnerTypes; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; @@ -21,6 +19,7 @@ import java.util.Objects; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; + import io.swagger.annotations.Api; import lombok.Data; @@ -45,7 +44,7 @@ public String sign(String dataToSign, boolean keyFileNameByPartnerName) throws JoseException, NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException, CertificateException, IOException, OperatorCreationException { - KeyMgrUtil keyMgrUtil = new KeyMgrUtil(); + KeyMgrUtility keyMgrUtil = new KeyMgrUtility(); JsonWebSignature jwSign = new JsonWebSignature(); PrivateKeyEntry keyEntry = keyMgrUtil.getKeyEntry(dirPath, partnerType, organizationName, keyFileNameByPartnerName); diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/KeyMgrUtility.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/KeyMgrUtility.java index f32f97d725..c7d8e74f17 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/KeyMgrUtility.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/KeyMgrUtility.java @@ -1,8 +1,45 @@ -package helper; +package io.mosip.testrig.auth.util; + + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringReader; +import java.io.StringWriter; +import java.math.BigInteger; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.security.KeyFactory; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.SecureRandom; +import java.security.UnrecoverableEntryException; +import java.security.cert.Certificate; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.X509EncodedKeySpec; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Arrays; +import java.util.Date; +import java.util.Objects; +import javax.security.auth.x500.X500Principal; -import dto.CertificateChainResponseDto; -import io.mosip.authentication.core.util.CryptoUtil; import org.bouncycastle.asn1.x500.RDN; import org.bouncycastle.asn1.x500.X500Name; import org.bouncycastle.asn1.x500.X500NameBuilder; @@ -18,42 +55,24 @@ import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils; import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; -import org.bouncycastle.crypto.InvalidCipherTextException; import org.bouncycastle.openssl.jcajce.JcaPEMWriter; import org.bouncycastle.operator.ContentSigner; import org.bouncycastle.operator.OperatorCreationException; import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; import org.bouncycastle.util.io.pem.PemObject; import org.bouncycastle.util.io.pem.PemReader; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; - -import javax.security.auth.x500.X500Principal; -import java.io.*; -import java.math.BigInteger; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.*; -import java.security.cert.Certificate; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; -import java.security.interfaces.RSAPublicKey; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.X509EncodedKeySpec; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.util.Arrays; -import java.util.Date; -import java.util.Objects; - - -public class KeyMgrUtil { - public KeyMgrUtil() { - cryptoCoreUtil = new CryptoCoreUtil(); - } - - CryptoCoreUtil cryptoCoreUtil; +import io.mosip.authentication.core.util.CryptoUtil; +import io.mosip.testrig.auth.dto.CertificateChainResponseDto; + +@Component +public class KeyMgrUtility { + + +@Autowired + private CryptoCoreUtil cryptoCoreUtil; private static final String DOMAIN_URL = "mosip.base.url"; private static final String CA_P12_FILE_NAME = "-ca.p12"; diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/PartnerTypes.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/PartnerTypes.java index e428378914..2cf5287c53 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/PartnerTypes.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/PartnerTypes.java @@ -1,4 +1,4 @@ -package helper; +package io.mosip.testrig.auth.util; public enum PartnerTypes { diff --git a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/PropertiesReader.java b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/PropertiesReader.java index ceef63dcb6..968a57c22f 100644 --- a/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/PropertiesReader.java +++ b/AuthenticationUtil/src/main/java/io/mosip/testrig/auth/util/PropertiesReader.java @@ -1,4 +1,4 @@ -package helper; +package io.mosip.testrig.auth.util; import java.io.FileInputStream; import java.io.IOException; diff --git a/automationtests/src/main/java/io/mosip/testrig/apirig/admin/fw/util/BioDataUtility.java b/automationtests/src/main/java/io/mosip/testrig/apirig/admin/fw/util/BioDataUtility.java index 99fd2c1198..f23e9f6f5f 100644 --- a/automationtests/src/main/java/io/mosip/testrig/apirig/admin/fw/util/BioDataUtility.java +++ b/automationtests/src/main/java/io/mosip/testrig/apirig/admin/fw/util/BioDataUtility.java @@ -25,6 +25,8 @@ import io.mosip.testrig.apirig.global.utils.GlobalConstants; import io.mosip.testrig.apirig.service.BaseTestCase; import io.mosip.testrig.auth.util.AuthUtil; +import io.mosip.testrig.auth.util.Encrypt; +import io.mosip.testrig.auth.util.Encrypt.SplittedEncryptedData; import io.mosip.testrig.auth.util.PartnerTypes; /** @@ -41,6 +43,8 @@ public class BioDataUtility extends AdminTestUtil { private static final Logger logger = Logger.getLogger(BioDataUtility.class); @Autowired private EncryptionDecrptionUtil encryptDecryptUtil; + @Autowired + private Encrypt encrypt; private String encryptIsoBioValue(String isoBiovalue, String timestamp, String bioValueEncryptionTemplateJson, @@ -67,20 +71,41 @@ private String encryptIsoBioValue(String isoBiovalue, String timestamp, String b residentCookie = kernelAuthLib.getTokenByRole(GlobalConstants.RESIDENT); - try { - String json = encryptDecryptUtil.encrypt(jsonContent); - logger.info("json is" + json); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } +// try { +// String json = encryptDecryptUtil.encrypt(jsonContent); +// logger.info("json is" + json); +// } catch (Exception e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } String content = RestClient.postRequestWithCookie(cryptoEncryptUrl, jsonContent, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, COOKIENAME, residentCookie).asString(); String data = JsonPrecondtion.getValueFromJson(content, "response.data"); logger.info("data is" + data); - return EncryptionDecrptionUtil.splitEncryptedData(data); + + SplittedEncryptedData splittedEncryptedData = null; + JSONObject splittedEncryptedDataJson = new JSONObject(); + + + try { + splittedEncryptedData = encrypt.splitEncryptedData(data); + logger.info("EncryptedSessionKey is " + splittedEncryptedData.getEncryptedSessionKey()); + logger.info("EncryptedData is " + splittedEncryptedData.getEncryptedData()); + logger.info("Thumbprint is " + splittedEncryptedData.getThumbprint()); + splittedEncryptedDataJson.put("encryptedSessionKey", splittedEncryptedData.getEncryptedSessionKey()); + splittedEncryptedDataJson.put("encryptedData", splittedEncryptedData.getEncryptedData()); + splittedEncryptedDataJson.put("thumbprint", splittedEncryptedData.getThumbprint()); + + } catch (Exception e) { + logger.error(e.getMessage()); + } + +// return EncryptionDecrptionUtil.splitEncryptedData(data); + return splittedEncryptedDataJson.toString(); } + + private String getHash(String content) { return HMACUtils.digestAsPlainText(HMACUtils.generateHash(content.getBytes())); @@ -156,7 +181,7 @@ public String constractBioIdentityRequest(String identityRequest, String bioValu - identityRequest = JsonPrecondtion.parseAndReturnJsonContent(identityRequest, "https://api-internal.qa-inji.mosip.net", + identityRequest = JsonPrecondtion.parseAndReturnJsonContent(identityRequest, BaseTestCase.ApplnURI, biometricsMapper + ".data.domainUri"); diff --git a/automationtests/src/main/java/io/mosip/testrig/apirig/admin/fw/util/EncryptionDecrptionUtil.java b/automationtests/src/main/java/io/mosip/testrig/apirig/admin/fw/util/EncryptionDecrptionUtil.java index 010cc8d5fe..c567ffc013 100644 --- a/automationtests/src/main/java/io/mosip/testrig/apirig/admin/fw/util/EncryptionDecrptionUtil.java +++ b/automationtests/src/main/java/io/mosip/testrig/apirig/admin/fw/util/EncryptionDecrptionUtil.java @@ -41,6 +41,7 @@ import io.mosip.testrig.apirig.kernel.util.ConfigManager; import io.mosip.testrig.auth.dto.EncryptionResponseDto; import io.mosip.testrig.auth.util.CryptoUtil; +import io.mosip.testrig.auth.util.Encrypt; import io.restassured.response.Response; /** @@ -67,6 +68,8 @@ public class EncryptionDecrptionUtil extends AdminTestUtil{ private CryptoUtil cryptoUtil; @Autowired private KeyMgrUtil keymgrUtil; + @Autowired + private Encrypt encrypt; static { if(EncryptUtilBaseUrl==null) @@ -97,14 +100,20 @@ public static String getEncryptUtilBaseUrl() { */ public Map getEncryptSessionKeyValue(String jsonString) { Map ecryptData = new HashMap<>(); + EncryptionResponseDto encryptionResponseDto = new EncryptionResponseDto(); try { - String json = encrypt(jsonString); - JSONObject jsonobj = new JSONObject(json); + encryptionResponseDto = encrypt(jsonString); +// JSONObject jsonobj = new JSONObject(json); Reporter.log(" Encryption of identity request "); - GlobalMethods.reportRequest(null, json); - ecryptData.put("key", jsonobj.get(key).toString()); - ecryptData.put("data", jsonobj.get(data).toString()); - ecryptData.put("hmac", jsonobj.get(hmac).toString()); + GlobalMethods.reportRequest(null, encryptionResponseDto.toString()); + ecryptData.put("key", encryptionResponseDto.getEncryptedSessionKey()); + ecryptData.put("data", encryptionResponseDto.getEncryptedIdentity()); + ecryptData.put("hmac", encryptionResponseDto.getRequestHMAC()); + + +// ecryptData.put("key", jsonobj.get(key).toString()); +// ecryptData.put("data", jsonobj.get(data).toString()); +// ecryptData.put("hmac", jsonobj.get(hmac).toString()); ecryptData.put("thumbprint", partnerThumbPrint); return ecryptData; } catch (Exception e) { @@ -146,7 +155,7 @@ public Map getInternalEncryptSessionKeyValue(String jsonString) */ - public String encrypt(String jsonString) throws Exception { + public EncryptionResponseDto encrypt(String jsonString) throws Exception { String refId= null; boolean isInternal = false; @@ -176,7 +185,7 @@ private static String getRefId(boolean isInternal, boolean isBiometrics) { return refId; } - private String kernelEncrypt(String jsonString, String refId) throws Exception { + private EncryptionResponseDto kernelEncrypt(String jsonString, String refId) throws Exception { String identityBlock = objMapper.writeValueAsString(jsonString); SecretKey secretKey = cryptoUtil.genSecKey(); EncryptionResponseDto encryptionResponseDto = new EncryptionResponseDto(); @@ -204,7 +213,9 @@ private String kernelEncrypt(String jsonString, String refId) throws Exception digestAsPlainText(HMACUtils2.generateHash(identityBlock.getBytes(StandardCharsets.UTF_8))).getBytes(), secretKey); encryptionResponseDto.setRequestHMAC(Base64.getUrlEncoder().encodeToString(byteArr)); - return encryptionResponseDto.toString(); + lOGGER.info("encryptionResponseDto is " + encryptionResponseDto.toString()); + + return encryptionResponseDto; } public static String digestAsPlainText(byte[] data) { @@ -272,48 +283,48 @@ private String getIntenalEncryption(String jsonString) { * @param filename * @return String, Encoded data */ - public String getEncode(String jsonString) { - try { - JSONObject objectData = new JSONObject(jsonString); - return RestClient.postRequest(EncryptUtilBaseUrl+properties.get(GlobalConstants.ENCODEPATH), objectData.toString(), MediaType.TEXT_PLAIN, - MediaType.TEXT_PLAIN).asString(); - } catch (Exception e) { - lOGGER.error(GlobalConstants.EXCEPTION + e); - return e.toString(); - } - } +// public String getEncode(String jsonString) { +// try { +// JSONObject objectData = new JSONObject(jsonString); +// return RestClient.postRequest(EncryptUtilBaseUrl+properties.get(GlobalConstants.ENCODEPATH), objectData.toString(), MediaType.TEXT_PLAIN, +// MediaType.TEXT_PLAIN).asString(); +// } catch (Exception e) { +// lOGGER.error(GlobalConstants.EXCEPTION + e); +// return e.toString(); +// } +// } /** * The method will get encoded data from cbeff file * * @param filename * @return String, Encoded data */ - public String getCbeffEncode(String filename) { - try { - String objectData = FileUtil.readInput(filename); - objectData=objectData.replaceAll(" xmlns=\"\"", ""); - return RestClient.postRequest(EncryptUtilBaseUrl+properties.get(GlobalConstants.ENCODEPATH), objectData, MediaType.TEXT_PLAIN, - MediaType.TEXT_PLAIN).asString(); - } catch (Exception e) { - lOGGER.error(GlobalConstants.EXCEPTION + e); - return e.toString(); - } - } +// public String getCbeffEncode(String filename) { +// try { +// String objectData = FileUtil.readInput(filename); +// objectData=objectData.replaceAll(" xmlns=\"\"", ""); +// return RestClient.postRequest(EncryptUtilBaseUrl+properties.get(GlobalConstants.ENCODEPATH), objectData, MediaType.TEXT_PLAIN, +// MediaType.TEXT_PLAIN).asString(); +// } catch (Exception e) { +// lOGGER.error(GlobalConstants.EXCEPTION + e); +// return e.toString(); +// } +// } /** * The method get decoded content in file * * @param content, String to decode * @return String, decoded content */ - public String getDecodeFile(String content) { - try { - return RestClient.postRequest(EncryptUtilBaseUrl + properties.get("decodeFilePath"), content, - MediaType.TEXT_PLAIN, MediaType.APPLICATION_OCTET_STREAM).asString(); - } catch (Exception e) { - lOGGER.error(GlobalConstants.EXCEPTION + e); - return e.toString(); - } - } +// public String getDecodeFile(String content) { +// try { +// return RestClient.postRequest(EncryptUtilBaseUrl + properties.get("decodeFilePath"), content, +// MediaType.TEXT_PLAIN, MediaType.APPLICATION_OCTET_STREAM).asString(); +// } catch (Exception e) { +// lOGGER.error(GlobalConstants.EXCEPTION + e); +// return e.toString(); +// } +// } /** * The method get encoded data from file @@ -321,15 +332,15 @@ public String getDecodeFile(String content) { * @param file, file to be encoded * @return String, encoded data */ - public String getEncodeFile(File file) { - try { - return RestClient.postRequest(EncryptUtilBaseUrl + properties.get("encodeFilePath"), file, - MediaType.MULTIPART_FORM_DATA, MediaType.TEXT_PLAIN).asString(); - } catch (Exception e) { - lOGGER.error(GlobalConstants.EXCEPTION + e); - return e.toString(); - } - } +// public String getEncodeFile(File file) { +// try { +// return RestClient.postRequest(EncryptUtilBaseUrl + properties.get("encodeFilePath"), file, +// MediaType.MULTIPART_FORM_DATA, MediaType.TEXT_PLAIN).asString(); +// } catch (Exception e) { +// lOGGER.error(GlobalConstants.EXCEPTION + e); +// return e.toString(); +// } +// } /** * The method get decoded data from file @@ -337,16 +348,16 @@ public String getEncodeFile(File file) { * @param filename, file to to be decoded * @return String, decoded data */ - public String getDecodeFromFile(String filename) { - try (FileReader fr = new FileReader(filename)) { - JSONObject objectData = (JSONObject) new JSONParser().parse(fr); - return RestClient.postRequest(EncryptUtilBaseUrl + properties.get("decodePath"), objectData.toString(), - MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).asString(); - } catch (Exception e) { - lOGGER.error(GlobalConstants.EXCEPTION + e); - return e.toString(); - } - } +// public String getDecodeFromFile(String filename) { +// try (FileReader fr = new FileReader(filename)) { +// JSONObject objectData = (JSONObject) new JSONParser().parse(fr); +// return RestClient.postRequest(EncryptUtilBaseUrl + properties.get("decodePath"), objectData.toString(), +// MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).asString(); +// } catch (Exception e) { +// lOGGER.error(GlobalConstants.EXCEPTION + e); +// return e.toString(); +// } +// } /** * The method get decoded data from string @@ -354,31 +365,31 @@ public String getDecodeFromFile(String filename) { * @param content, String to be decoded * @return String, decoded data */ - public String getDecodeFromStr(String content) { - try { - return RestClient.postRequest(EncryptUtilBaseUrl + properties.get("decodePath"), content, - MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).asString(); - } catch (Exception e) { - lOGGER.error(GlobalConstants.EXCEPTION + e); - return e.toString(); - } - } +// public String getDecodeFromStr(String content) { +// try { +// return RestClient.postRequest(EncryptUtilBaseUrl + properties.get("decodePath"), content, +// MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).asString(); +// } catch (Exception e) { +// lOGGER.error(GlobalConstants.EXCEPTION + e); +// return e.toString(); +// } +// } /** * The method get decrypt data from file * * @param filename, file to to be decoded * @return String, decoded data */ - public String getDecryptFromFile(String filename) { - try (FileReader fr = new FileReader(filename)) { - JSONObject objectData = (JSONObject) new JSONParser().parse(fr); - return RestClient.postRequest(EncryptUtilBaseUrl + properties.get(GlobalConstants.DECRYPTPATH), - objectData.toString(), MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).asString(); - } catch (Exception e) { - lOGGER.error(GlobalConstants.EXCEPTION + e); - return e.toString(); - } - } +// public String getDecryptFromFile(String filename) { +// try (FileReader fr = new FileReader(filename)) { +// JSONObject objectData = (JSONObject) new JSONParser().parse(fr); +// return RestClient.postRequest(EncryptUtilBaseUrl + properties.get(GlobalConstants.DECRYPTPATH), +// objectData.toString(), MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).asString(); +// } catch (Exception e) { +// lOGGER.error(GlobalConstants.EXCEPTION + e); +// return e.toString(); +// } +// } /** * The method get decrypt data from string @@ -386,18 +397,18 @@ public String getDecryptFromFile(String filename) { * @param content, String to be decoded * @return String, decoded data */ - public String getDecyptFromStr(String content, String referenceId, boolean isInternal) { - try { - HashMap queryParams = new HashMap<>(); - queryParams.put("refId", referenceId); - queryParams.put("isInternal", isInternal); - return RestClient.postRequestWithQueryParamsAndBody(EncryptUtilBaseUrl + properties.get(GlobalConstants.DECRYPTPATH), content, queryParams, - MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).asString(); - } catch (Exception e) { - lOGGER.error(GlobalConstants.EXCEPTION + e); - return e.toString(); - } - } +// public String getDecyptFromStr(String content, String referenceId, boolean isInternal) { +// try { +// HashMap queryParams = new HashMap<>(); +// queryParams.put("refId", referenceId); +// queryParams.put("isInternal", isInternal); +// return RestClient.postRequestWithQueryParamsAndBody(EncryptUtilBaseUrl + properties.get(GlobalConstants.DECRYPTPATH), content, queryParams, +// MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).asString(); +// } catch (Exception e) { +// lOGGER.error(GlobalConstants.EXCEPTION + e); +// return e.toString(); +// } +// } /** * The method will get encoded data from json content in file @@ -415,18 +426,18 @@ public static String getBase64EncodedString(String content) { } } - public static String splitEncryptedData(String content) - { - try { - return RestClient.postRequest(EncryptUtilBaseUrl+properties.get("splitEncryptedData"), content, MediaType.APPLICATION_JSON, - MediaType.APPLICATION_JSON).asString(); - - } catch (Exception e) { - lOGGER.error(GlobalConstants.EXCEPTION + e); - return e.toString(); - } - - } +// public static String splitEncryptedData(String content) +// { +// try { +// return RestClient.postRequest(EncryptUtilBaseUrl+properties.get("splitEncryptedData"), content, MediaType.APPLICATION_JSON, +// MediaType.APPLICATION_JSON).asString(); +// +// } catch (Exception e) { +// lOGGER.error(GlobalConstants.EXCEPTION + e); +// return e.toString(); +// } +// +// } public static String getCertificateThumbprint(Certificate cert){ try { @@ -521,47 +532,47 @@ public boolean validateThumbPrint( String thumbPrint, String partnerId) { return expectedThumbPrint.equals(thumbPrint); } - public boolean validateEkycResponseIdentity(String identity, String partnerId, boolean isInternal) { - String decryptedKycIdentity = getDecyptFromStr(identity, partnerId, isInternal); - boolean bReturn = true; - Reporter.log( - "Decrypted Kyc Response: (EndPointUrl: " + EncryptUtilBaseUrl + properties.get(GlobalConstants.DECRYPTPATH) - + ")
" + ReportUtil.getTextAreaJsonMsgHtml(decryptedKycIdentity) + "
"); - String[] keysToValidateInKYC = properties.getProperty("keysToValidateInKYC").split(","); - JSONObject decryptedKycJson = new JSONObject(decryptedKycIdentity); - if (decryptedKycJson.length() != 0) { - for (String key : keysToValidateInKYC) - if (!decryptedKycJson.has(key)) - bReturn = false; - } else - bReturn = false; - return bReturn; - } +// public boolean validateEkycResponseIdentity(String identity, String partnerId, boolean isInternal) { +// String decryptedKycIdentity = getDecyptFromStr(identity, partnerId, isInternal); +// boolean bReturn = true; +// Reporter.log( +// "Decrypted Kyc Response: (EndPointUrl: " + EncryptUtilBaseUrl + properties.get(GlobalConstants.DECRYPTPATH) +// + ")
" + ReportUtil.getTextAreaJsonMsgHtml(decryptedKycIdentity) + "
"); +// String[] keysToValidateInKYC = properties.getProperty("keysToValidateInKYC").split(","); +// JSONObject decryptedKycJson = new JSONObject(decryptedKycIdentity); +// if (decryptedKycJson.length() != 0) { +// for (String key : keysToValidateInKYC) +// if (!decryptedKycJson.has(key)) +// bReturn = false; +// } else +// bReturn = false; +// return bReturn; +// } - public boolean validateThumbPrintAndIdentity(Response response, String ekycUri) throws AdminTestException { - String thumbPrint = JsonPrecondtion.getValueFromJson(response.asString(), "response.thumbprint"); - String[] uriParts = ekycUri.split("/"); - String partnerId = uriParts[uriParts.length-2]; - boolean thumprintValid = validateThumbPrint(thumbPrint, partnerId); - if(!thumprintValid) throw new AdminTestException("Failed in Thumbprint validation"); - - String identity = JsonPrecondtion.getValueFromJson(response.asString(), "response.identity"); - boolean ekycResponseValid = validateEkycResponseIdentity(identity, partnerId, false); - if(!ekycResponseValid) throw new AdminTestException("Failed in KYC Response validation"); - - return thumprintValid&&ekycResponseValid; - } +// public boolean validateThumbPrintAndIdentity(Response response, String ekycUri) throws AdminTestException { +// String thumbPrint = JsonPrecondtion.getValueFromJson(response.asString(), "response.thumbprint"); +// String[] uriParts = ekycUri.split("/"); +// String partnerId = uriParts[uriParts.length-2]; +// boolean thumprintValid = validateThumbPrint(thumbPrint, partnerId); +// if(!thumprintValid) throw new AdminTestException("Failed in Thumbprint validation"); +// +// String identity = JsonPrecondtion.getValueFromJson(response.asString(), "response.identity"); +// boolean ekycResponseValid = validateEkycResponseIdentity(identity, partnerId, false); +// if(!ekycResponseValid) throw new AdminTestException("Failed in KYC Response validation"); +// +// return thumprintValid&&ekycResponseValid; +// } - public boolean verifyResponseUsingDigitalSignature(String resonseContent, String digitalSignature) { - HashMap queryparams = new HashMap<>(); - queryparams.put("signature", digitalSignature); - String signatureApiPath = EncryptUtilBaseUrl + properties.getProperty("validateSignatureUrl"); - Response response = RestClient.postRequestWithQueryParamAndBody(signatureApiPath, resonseContent,queryparams, MediaType.APPLICATION_JSON, - MediaType.APPLICATION_JSON); - if (response.asString().contains("success")) - return true; - else - return false; - } +// public boolean verifyResponseUsingDigitalSignature(String resonseContent, String digitalSignature) { +// HashMap queryparams = new HashMap<>(); +// queryparams.put("signature", digitalSignature); +// String signatureApiPath = EncryptUtilBaseUrl + properties.getProperty("validateSignatureUrl"); +// Response response = RestClient.postRequestWithQueryParamAndBody(signatureApiPath, resonseContent,queryparams, MediaType.APPLICATION_JSON, +// MediaType.APPLICATION_JSON); +// if (response.asString().contains("success")) +// return true; +// else +// return false; +// } } diff --git a/automationtests/src/main/java/io/mosip/testrig/apirig/testscripts/BioAuth.java b/automationtests/src/main/java/io/mosip/testrig/apirig/testscripts/BioAuth.java index 302077a439..0fbb83eb8a 100644 --- a/automationtests/src/main/java/io/mosip/testrig/apirig/testscripts/BioAuth.java +++ b/automationtests/src/main/java/io/mosip/testrig/apirig/testscripts/BioAuth.java @@ -129,8 +129,12 @@ public void test01(TestCaseDTO testCaseDTO) throws AuthenticationTestException, String authRequest = getJsonFromTemplate(request.toString(), testCaseDTO.getInputTemplate()); logger.info("************* Modification of bio auth request ******************"); Reporter.log("Modification of bio auth request"); + + logger.info("authRequest is = " + authRequest); + logger.info("bioAuthTempMap is = " + bioAuthTempMap); authRequest = modifyRequest(authRequest, bioAuthTempMap, getResourcePath() + props.getProperty("idaMappingPath")); + logger.info("authRequestTemp is = " + authRequest); JSONObject authRequestTemp = new JSONObject(authRequest); authRequestTemp.remove("env"); authRequestTemp.put("env", "Staging"); diff --git a/automationtests/testNgXmlFiles/authenticationApi.xml b/automationtests/testNgXmlFiles/authenticationApi.xml index fd73a01898..3872750f4a 100644 --- a/automationtests/testNgXmlFiles/authenticationApi.xml +++ b/automationtests/testNgXmlFiles/authenticationApi.xml @@ -124,22 +124,22 @@ - -