-
Notifications
You must be signed in to change notification settings - Fork 9
/
DataConnection.java
63 lines (53 loc) · 2.33 KB
/
DataConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.github.tkaczenko.cardreader.service;
import lombok.RequiredArgsConstructor;
import net.sf.scuba.smartcards.CardServiceException;
import org.jmrtd.PACEKeySpec;
import org.jmrtd.PassportService;
import org.jmrtd.lds.CardAccessFile;
import org.jmrtd.lds.PACEInfo;
import org.jmrtd.lds.SecurityInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Andrii Tkachenko
*/
@RequiredArgsConstructor
@Validated
@Service
public class DataConnection {
private static final Logger LOGGER = LoggerFactory.getLogger(DataConnection.class);
private final PassportService ps;
public boolean initConnection(@NotNull String can) throws CardServiceException, IOException {
ps.open();
CardAccessFile cardAccessFile = new CardAccessFile(ps.getInputStream(PassportService.EF_CARD_ACCESS));
Collection<SecurityInfo> securityInfos = cardAccessFile.getSecurityInfos();
SecurityInfo securityInfo = securityInfos.iterator().next();
LOGGER.info("ProtocolOIDString: " + securityInfo.getProtocolOIDString());
LOGGER.info("ObjectIdentifier: " + securityInfo.getObjectIdentifier());
List<PACEInfo> paceInfos = getPACEInfos(securityInfos);
LOGGER.debug("Found a card access file: paceInfos (" + (paceInfos == null ? 0 : paceInfos.size()) + ") = " + paceInfos);
if (paceInfos != null && paceInfos.size() > 0) {
PACEInfo paceInfo = paceInfos.get(0);
PACEKeySpec paceKey = PACEKeySpec.createCANKey(can);
ps.doPACE(paceKey, paceInfo.getObjectIdentifier(), PACEInfo.toParameterSpec(paceInfo.getParameterId()), paceInfo.getParameterId());
ps.sendSelectApplet(true);
return true;
} else {
ps.close();
return false;
}
}
private List<PACEInfo> getPACEInfos(Collection<SecurityInfo> securityInfos) {
return securityInfos.stream()
.filter(securityInfo -> securityInfo instanceof PACEInfo)
.map(securityInfo -> (PACEInfo) securityInfo)
.collect(Collectors.toList());
}
}