Skip to content

Commit

Permalink
[INJIMOB-2471] remove id field from mock VCs (#86)
Browse files Browse the repository at this point in the history
* [INJIMOB-2471] modify MdocGenerator kotlin to Java class

Signed-off-by: KiruthikaJeyashankar <[email protected]>

* [INJIMOB-2471] modify CBORConvertor kotlin to Java class

Signed-off-by: KiruthikaJeyashankar <[email protected]>

* [INJIMOB-2471] remove id field from mock VCs

id field is removed from both ldp_vc and mso_mdoc format VCs. For ldp_vcs id field is marked as optional

Signed-off-by: KiruthikaJeyashankar <[email protected]>

* [INJIMOB-2471] update version of mock-certify-plugin to 0.3.1-SNAPSHOT

Signed-off-by: KiruthikaJeyashankar <[email protected]>

* [INJIMOB-2471] rm unused kotlin dependencies

Signed-off-by: KiruthikaJeyashankar <[email protected]>

---------

Signed-off-by: KiruthikaJeyashankar <[email protected]>
Signed-off-by: Vishwa <[email protected]>
  • Loading branch information
KiruthikaJeyashankar authored Dec 16, 2024
1 parent cb220c0 commit 3a6d62f
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 267 deletions.
66 changes: 1 addition & 65 deletions mock-certify-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>io.mosip.certify</groupId>
<artifactId>mock-certify-plugin</artifactId>
<version>0.3.0-SNAPSHOT</version>
<version>0.3.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mock-certify-integration-impl</name>
Expand Down Expand Up @@ -401,70 +401,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>target/generated-sources/annotations</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>target/generated-test-sources/test-annotations</source>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<jvmTarget>${maven.compiler.target}</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.mosip.certify.api.spi.VCIssuancePlugin;
import io.mosip.certify.api.util.ErrorConstants;
import io.mosip.certify.core.exception.CertifyException;
import io.mosip.certify.mock.integration.mocks.MdocGenerator;
import io.mosip.certify.util.UUIDGenerator;
import io.mosip.esignet.core.dto.OIDCTransaction;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -133,7 +134,6 @@ private JsonLDObject buildJsonLDWithLDProof(String accessTokenHash)
Map<String, Object> verCredJsonObject = new HashMap<>();
verCredJsonObject.put("@context", vcCredentialContexts);
verCredJsonObject.put("type", Arrays.asList("VerifiableCredential", "MockVerifiableCredential"));
verCredJsonObject.put("id", uuid);
verCredJsonObject.put("issuer", "did:example:123456789");
verCredJsonObject.put("issuanceDate", getUTCDateTime());
verCredJsonObject.put("credentialSubject", formattedMap);
Expand Down Expand Up @@ -252,7 +252,7 @@ public VCResult<String> getVerifiableCredential(VCRequestDto vcRequestDto, Strin
VCResult<String> vcResult = new VCResult<>();
String mdocVc = null;
try {
mdocVc = new io.mosip.certify.mock.integration.mocks.MdocGenerator().generate(mockDataForMsoMdoc(documentNumber),holderId, issuerKeyAndCertificate);
mdocVc = new MdocGenerator().generate(mockDataForMsoMdoc(documentNumber),holderId, issuerKeyAndCertificate);
} catch (Exception e) {
log.error("Exception on mdoc creation", e);
throw new VCIExchangeException(ErrorConstants.VCI_EXCHANGE_FAILED);
Expand Down Expand Up @@ -282,4 +282,4 @@ private Map<String, Object> mockDataForMsoMdoc(String documentNumber) {
public OIDCTransaction getUserInfoTransaction(String accessTokenHash) {
return cacheManager.getCache(USERINFO_CACHE).get(accessTokenHash, OIDCTransaction.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package io.mosip.certify.mock.integration.mocks;

import co.nstant.in.cbor.CborBuilder;
import co.nstant.in.cbor.CborEncoder;
import co.nstant.in.cbor.CborException;
import co.nstant.in.cbor.model.DataItem;
import com.android.identity.credential.NameSpacedData;
import com.android.identity.internal.Util;
import com.android.identity.mdoc.mso.MobileSecurityObjectGenerator;
import com.android.identity.mdoc.util.MdocUtil;
import com.android.identity.util.Timestamp;
import io.mosip.certify.util.*;

import java.io.ByteArrayOutputStream;
import java.security.KeyPair;
import java.security.PublicKey;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class MdocGenerator {

public static final String NAMESPACE = "org.iso.18013.5.1";
public static final String DOCTYPE = NAMESPACE + ".mDL";
public static final String DIGEST_ALGORITHM = "SHA-256";
public static final String ECDSA_ALGORITHM = "SHA256withECDSA";
public static final long SEED = 42L;
public static final DateTimeFormatter FULL_DATE_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE;

public String generate(Map<String, Object> data, String holderId, String issuerKeyAndCertificate) throws Exception {
PKCS12Reader pkcs12Reader = new PKCS12Reader();
KeyPairAndCertificate issuerDetails = pkcs12Reader.extract(issuerKeyAndCertificate);

if (issuerDetails.keyPair() == null) {
throw new RuntimeException("Unable to load Crypto details");
}

JwkToKeyConverter jwkToKeyConverter = new JwkToKeyConverter();
PublicKey devicePublicKey = jwkToKeyConverter.convertToPublicKey(holderId.replace("did:jwk:", ""));
KeyPair issuerKeypair = issuerDetails.keyPair();

LocalDate issueDate = LocalDate.now();
String formattedIssueDate = issueDate.format(FULL_DATE_FORMATTER);
LocalDate expiryDate = issueDate.plusYears(5);
String formattedExpiryDate = expiryDate.format(FULL_DATE_FORMATTER);

NameSpacedData.Builder nameSpacedDataBuilder = new NameSpacedData.Builder();
nameSpacedDataBuilder.putEntryString(NAMESPACE, "issue_date", formattedIssueDate);
nameSpacedDataBuilder.putEntryString(NAMESPACE, "expiry_date", formattedExpiryDate);

Map<String, String> drivingPrivileges = (Map<String, String>) data.get("driving_privileges");
drivingPrivileges.put("issue_date", formattedIssueDate);
drivingPrivileges.put("expiry_date", formattedExpiryDate);

for (String key : data.keySet()) {
nameSpacedDataBuilder.putEntryString(NAMESPACE, key, data.get(key).toString());
}

NameSpacedData nameSpacedData = nameSpacedDataBuilder.build();
Map<String, List<byte[]>> generatedIssuerNameSpaces = MdocUtil.generateIssuerNameSpaces(nameSpacedData, new Random(SEED), 16);
Map<Long, byte[]> calculateDigestsForNameSpace = MdocUtil.calculateDigestsForNameSpace(NAMESPACE, generatedIssuerNameSpaces, DIGEST_ALGORITHM);

MobileSecurityObjectGenerator mobileSecurityObjectGenerator = new MobileSecurityObjectGenerator(DIGEST_ALGORITHM, DOCTYPE, devicePublicKey);
mobileSecurityObjectGenerator.addDigestIdsForNamespace(NAMESPACE, calculateDigestsForNameSpace);

Timestamp currentTimestamp = Timestamp.now();
Timestamp validUntil = Timestamp.ofEpochMilli(addYearsToDate(currentTimestamp.toEpochMilli(), 2));
mobileSecurityObjectGenerator.setValidityInfo(currentTimestamp, currentTimestamp, validUntil, null);

byte[] mso = mobileSecurityObjectGenerator.generate();

DataItem coseSign1Sign = Util.coseSign1Sign(
issuerKeypair.getPrivate(),
ECDSA_ALGORITHM,
Util.cborEncode(Util.cborBuildTaggedByteString(mso)),
null,
Collections.singletonList(issuerDetails.certificate())
);

return construct(generatedIssuerNameSpaces, coseSign1Sign);
}

private String construct(Map<String, List<byte[]>> nameSpaces, DataItem issuerAuth) throws CborException {
MDoc mDoc = new MDoc(DOCTYPE, new IssuerSigned(nameSpaces, issuerAuth));
byte[] cbor = mDoc.toCBOR();
return Base64.getUrlEncoder().encodeToString(cbor);
}

private long addYearsToDate(long dateInEpochMillis, int years) {
Instant instant = Instant.ofEpochMilli(dateInEpochMillis);
Instant futureInstant = instant.plus(years * 365L, ChronoUnit.DAYS);
return futureInstant.toEpochMilli();
}
}


class MDoc {
private final String docType;
private final IssuerSigned issuerSigned;

public MDoc(String docType, IssuerSigned issuerSigned) {
this.docType = docType;
this.issuerSigned = issuerSigned;
}

public byte[] toCBOR() throws CborException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CborEncoder cborEncoder = new CborEncoder(byteArrayOutputStream);
cborEncoder.encode(
new CborBuilder().addMap()
.put("docType", docType)
.put(CBORConverter.toDataItem("issuerSigned"), CBORConverter.toDataItem(issuerSigned.toMap()))
.end()
.build()
);
return byteArrayOutputStream.toByteArray();
}
}

class IssuerSigned {
private final Map<String, List<byte[]>> nameSpaces;
private final DataItem issuerAuth;

public IssuerSigned(Map<String, List<byte[]>> nameSpaces, DataItem issuerAuth) {
this.nameSpaces = nameSpaces;
this.issuerAuth = issuerAuth;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("nameSpaces", CBORConverter.toDataItem(nameSpaces));
map.put("issuerAuth", issuerAuth);
return map;
}
}

This file was deleted.

Loading

0 comments on commit 3a6d62f

Please sign in to comment.