Skip to content

Commit

Permalink
added getFeatures(), fixed C40/MRZ parsing, fixed lengths in SealCodings
Browse files Browse the repository at this point in the history
  • Loading branch information
tsenger committed Nov 1, 2024
1 parent aa8310f commit 7019eeb
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 77 deletions.
21 changes: 18 additions & 3 deletions src/main/java/de/tsenger/vdstools/FeatureConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Set<String> getAvailableVdsFeatures() {
return vdsFeatures;
}

public String getFeature(String vdsType, DerTlv derTlv) {
public String getFeatureName(String vdsType, DerTlv derTlv) {
if (!vdsTypes.containsKey(vdsType)) {
Logger.warn("No seal type with name '" + vdsType + "' was found.");
return null;
Expand Down Expand Up @@ -156,10 +156,16 @@ private <T> DerTlv encodeFeature(SealDto sealDto, String feature, T inputValue)

@SuppressWarnings("unchecked")
private <T> T decodeFeature(SealDto sealDto, DerTlv derTlv) {
String coding = getCoding(sealDto, derTlv.getTag());
byte tag = derTlv.getTag();
String coding = getCoding(sealDto, tag);
switch (coding) {
case "C40":
return (T) DataParser.decodeC40(derTlv.getValue()).replace(' ', '<');
String featureValue = DataParser.decodeC40(derTlv.getValue());
String featureName = getFeatureName(sealDto, tag);
if (featureName.startsWith("MRZ")) {
featureValue = featureValue.replace(' ', '<');
}
return (T) featureValue;
case "ByteArray":
return (T) derTlv.getValue();
case "Utf8String":
Expand Down Expand Up @@ -205,6 +211,15 @@ private String getCoding(SealDto sealDto, byte tag) {
return null;
}

private FeaturesDto getFeatureDto(SealDto sealDto, byte tag) {
for (FeaturesDto featureDto : sealDto.features) {
if (featureDto.tag == tag) {
return featureDto;
}
}
return null;
}

private SealDto getSealDto(String vdsType) {
for (SealDto sealDto : sealDtoList) {
if (sealDto.documentType.equals(vdsType)) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/de/tsenger/vdstools/vds/DigitalSeal.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;
Expand Down Expand Up @@ -106,8 +107,12 @@ public String getRawString() throws IOException {
return DataEncoder.encodeBase256(getEncoded());
}

public <T> Map<String, T> getFeatures() {
return vdsMessage.getFeatures();
}

public <T> T getFeature(String feature) {
return vdsMessage.getDocumentFeature(feature);
return vdsMessage.getFeature(feature);
}

public static DigitalSeal fromRawString(String rawString) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/de/tsenger/vdstools/vds/VdsMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.tinylog.Logger;

Expand Down Expand Up @@ -51,7 +53,17 @@ public List<DerTlv> getDerTlvList() {
return this.derTlvList;
}

public <T> T getDocumentFeature(String feature) {
public <T> Map<String, T> getFeatures() {
Map<String, T> featureMap = new HashMap<String, T>();
for (DerTlv derTlv : derTlvList) {
T value = DataEncoder.getFeatureEncoder().decodeFeature(vdsType, derTlv);
String key = DataEncoder.getFeatureEncoder().getFeatureName(vdsType, derTlv);
featureMap.put(key, value);
}
return featureMap;
}

public <T> T getFeature(String feature) {
T value = null;
byte tag = DataEncoder.getFeatureEncoder().getTag(vdsType, feature);
for (DerTlv derTlv : derTlvList) {
Expand Down
24 changes: 12 additions & 12 deletions src/main/resources/SealCodings.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
"tag": 2,
"coding": "C40",
"required": true,
"minLength": 44,
"maxLength": 44
"minLength": 48,
"maxLength": 48
},
{
"name": "PASSPORT_NUMBER",
Expand Down Expand Up @@ -141,8 +141,8 @@
"tag": 2,
"coding": "C40",
"required": true,
"minLength": 44,
"maxLength": 44
"minLength": 48,
"maxLength": 48
},
{
"name": "PASSPORT_NUMBER",
Expand Down Expand Up @@ -172,8 +172,8 @@
"tag": 2,
"coding": "C40",
"required": true,
"minLength": 44,
"maxLength": 44
"minLength": 48,
"maxLength": 48
}
]
},
Expand Down Expand Up @@ -327,7 +327,7 @@
},
{
"documentType": "TEMP_PASSPORT",
"documentRef": "F60D",
"documentRef": "f60d",
"version": 4,
"features": [
{
Expand All @@ -343,14 +343,14 @@
"tag": 2,
"coding": "C40",
"required": true,
"minLength": 48,
"maxLength": 48
"minLength": 60,
"maxLength": 60
}
]
},
{
"documentType": "TEMP_PERSO",
"documentRef": "F70B",
"documentRef": "f70b",
"version": 4,
"features": [
{
Expand All @@ -366,8 +366,8 @@
"tag": 2,
"coding": "C40",
"required": true,
"minLength": 44,
"maxLength": 44
"minLength": 48,
"maxLength": 48
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testFeatureConverterString_notFound() throws FileNotFoundException {
@Test
public void testGetFeature_String() throws IOException {
FeatureConverter featureConverter = new FeatureConverter();
String feature = featureConverter.getFeature("FICTION_CERT",
String feature = featureConverter.getFeatureName("FICTION_CERT",
DerTlv.fromByteArray(Hex.decode("0306d79519a65306")));
assertEquals("PASSPORT_NUMBER", feature);
}
Expand Down
Loading

0 comments on commit 7019eeb

Please sign in to comment.