use of org.bouncycastle.bcpg.ArmoredInputStream in project jPOS by jpos.
the class PGPHelper method checkLicense.
public static int checkLicense() {
int rc = 0x90000;
boolean newl = false;
int ch;
try (InputStream in = getLicenseeStream()) {
InputStream ks = Q2.class.getClassLoader().getResourceAsStream(PUBRING);
PGPPublicKey pk = readPublicKey(ks, SIGNER);
ArmoredInputStream ain = new ArmoredInputStream(in, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(pk.getFingerprint(), "HmacSHA256"));
while ((ch = ain.read()) >= 0 && ain.isClearText()) {
if (newl) {
out.write((byte) '\n');
newl = false;
}
if (ch == '\n') {
newl = true;
continue;
}
out.write((byte) ch);
}
PGPObjectFactory pgpf = new PGPObjectFactory(ain, fingerPrintCalculator);
Object o = pgpf.nextObject();
if (o instanceof PGPSignatureList) {
PGPSignatureList list = (PGPSignatureList) o;
if (list.size() > 0) {
PGPSignature sig = list.get(0);
sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pk);
while ((ch = ain.read()) >= 0 && ain.isClearText()) {
if (newl) {
out.write((byte) '\n');
newl = false;
}
if (ch == '\n') {
newl = true;
continue;
}
out.write((byte) ch);
}
sig.update(out.toByteArray());
if (sig.verify()) {
rc &= 0x7FFFF;
ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
BufferedReader reader = new BufferedReader(new InputStreamReader(bais, StandardCharsets.UTF_8));
String s;
Pattern p1 = Pattern.compile("\\s(valid through:)\\s(\\d\\d\\d\\d-\\d\\d-\\d\\d)?", Pattern.CASE_INSENSITIVE);
Pattern p2 = Pattern.compile("\\s(instances:)\\s([\\d]{0,4})?", Pattern.CASE_INSENSITIVE);
String h = ModuleUtils.getSystemHash();
while ((s = reader.readLine()) != null) {
Matcher matcher = p1.matcher(s);
if (matcher.find() && matcher.groupCount() == 2) {
String lDate = matcher.group(2);
if (lDate.compareTo(Q2.getBuildTimestamp().substring(0, 10)) < 0) {
rc |= 0x40000;
}
}
matcher = p2.matcher(s);
if (matcher.find() && matcher.groupCount() == 2) {
rc |= Integer.parseInt(matcher.group(2));
}
if (s.contains(h)) {
rc &= 0xEFFFF;
}
}
}
}
if (!Arrays.equals(Q2.PUBKEYHASH, mac.doFinal(pk.getEncoded())))
rc |= 0x20000;
if (ModuleUtils.getRKeys().contains(PGPHelper.getLicenseeHash()))
rc |= 0x80000;
}
} catch (Exception ignored) {
// NOPMD: signature isn't good
}
return rc;
}
Aggregations