Skip to content

Commit

Permalink
Merge pull request mosip#1480 from nandhu-kumar/MOSIP-33327
Browse files Browse the repository at this point in the history
Mosip 33327
  • Loading branch information
lsivanand authored Jun 11, 2024
2 parents 571c0b9 + d3abd8b commit 3bfedb4
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 275 deletions.
5 changes: 3 additions & 2 deletions AuthenticationUtil/src/main/java/UnitTests.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dto;
package io.mosip.testrig.auth.dto;
import lombok.Data;

@Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dto;
package io.mosip.testrig.auth.dto;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dto;
package io.mosip.testrig.auth.dto;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dto;
package io.mosip.testrig.auth.dto;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -115,7 +116,7 @@ public String updatePartnerCertificate(
PartnerTypes partnerType, String partnerName, boolean keyFileNameByPartnerName, Map<String, String> 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();

Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helper;
package io.mosip.testrig.auth.util;

public enum CertificateTypes {

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,100 +1,64 @@
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;
import java.util.List;
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";
String publicKeyURL = "${mosip.ida.publicKey-url}";
String appID = "${application.id}";

String keySplitter = "#KEY_SPLITTER#";

@Autowired
CryptoUtil cryptoUtil;

public EncryptionResponseDto encrypt(EncryptionRequestDto encryptionRequestDto,
String refId,
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand Down
Loading

0 comments on commit 3bfedb4

Please sign in to comment.