use of com.github.zhenwei.core.asn1.x500.X500Name in project fabric-gateway by hyperledger.
the class X509Credentials method generateCertificate.
private X509Certificate generateCertificate(KeyPair keyPair) {
X500Name dnName = new X500Name("CN=John Doe");
// Yesterday
Date validityBeginDate = new Date(System.currentTimeMillis() - 24L * 60 * 60 * 1000);
// 2 years from now
Date validityEndDate = new Date(System.currentTimeMillis() + 2L * 365 * 24 * 60 * 60 * 1000);
SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded());
X509v3CertificateBuilder builder = new X509v3CertificateBuilder(dnName, BigInteger.valueOf(System.currentTimeMillis()), validityBeginDate, validityEndDate, Locale.getDefault(), dnName, subPubKeyInfo);
AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WithRSAEncryption");
AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
try {
ContentSigner contentSigner = new BcECContentSignerBuilder(sigAlgId, digAlgId).build(PrivateKeyFactory.createKey(keyPair.getPrivate().getEncoded()));
X509CertificateHolder holder = builder.build(contentSigner);
return new JcaX509CertificateConverter().getCertificate(holder);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (OperatorCreationException | CertificateException e) {
throw new RuntimeException(e);
}
}
use of com.github.zhenwei.core.asn1.x500.X500Name in project attestation by TokenScript.
the class IdentifierAttestationTest method testOtherSubject.
@Test
public void testOtherSubject() throws Exception {
IdentifierAttestation initial = HelperTest.makeUnsignedStandardAtt(subjectKeys.getPublic(), BigInteger.ONE, mail);
Field field = initial.getClass().getSuperclass().getDeclaredField("subject");
field.setAccessible(true);
// Change the subject
field.set(initial, new X500Name("CN=John Doe"));
// Common Names are allowed
assertTrue(initial.checkValidity());
}
use of com.github.zhenwei.core.asn1.x500.X500Name in project attestation by TokenScript.
the class ParserTest method testSunshine.
@Test
public void testSunshine() throws Exception {
String request = Files.readString(Path.of("src/test/data/verification_request.json"));
String response = Files.readString(Path.of("src/test/data/verification_response.json"));
Parser parser = new Parser(new JSONObject(request), (new JSONObject(response)).getJSONObject("Record"));
Map<String, X500Name> names = parser.getX500Names();
Map<String, Extensions> extensions = parser.getExtensions();
Assertions.assertEquals(names.size(), 2);
Assertions.assertEquals(extensions.size(), 2);
Assertions.assertTrue(names.containsKey("National Change of Address"));
Assertions.assertTrue(names.containsKey("NZ Driver Licence"));
Assertions.assertTrue(extensions.containsKey("National Change of Address"));
Assertions.assertTrue(extensions.containsKey("NZ Driver Licence"));
Set<String> expectedNameFields = new HashSet<String>(Arrays.asList(Parser.OID_COUNTRY_NAME, Parser.OID_GIVEN_NAME, Parser.OID_SUR_NAME, Parser.OID_STATE_OR_PROVINCE_NAME));
for (X500Name name : names.values()) {
Set<String> oids = Arrays.stream(name.getAttributeTypes()).map(c -> c.toString()).collect(Collectors.toSet());
Assertions.assertEquals(oids.size(), expectedNameFields.size());
Assertions.assertEquals(oids, expectedNameFields);
Set<String> encs = Arrays.stream(name.getRDNs()).map(c -> c.getTypesAndValues()[0].getValue().toString()).collect(Collectors.toSet());
Assertions.assertEquals(encs.size(), 4);
Assertions.assertTrue(encs.contains("NZ"));
Assertions.assertTrue(encs.contains("JaneKone"));
Assertions.assertTrue(encs.contains("Doe"));
Assertions.assertTrue(encs.contains("Queensland"));
}
Set<String> expectedDLExtensions = new HashSet<>(Arrays.asList(Parser.OID_STREET_ADDRESS, Parser.OID_SUBURB, Parser.OID_POSTAL_CODE, Parser.OID_DATE_OF_BIRTH));
Set<String> oids = Arrays.stream(extensions.get("NZ Driver Licence").getExtensionOIDs()).map(c -> c.toString()).collect(Collectors.toSet());
Assertions.assertEquals(expectedDLExtensions.size(), oids.size());
Assertions.assertEquals(expectedDLExtensions, oids);
Set<String> encs = Arrays.stream(extensions.get("NZ Driver Licence").getExtensionOIDs()).map(c -> new String(extensions.get("NZ Driver Licence").getExtension(c).getExtnValue().getOctets())).collect(Collectors.toSet());
Assertions.assertEquals(encs.size(), 4);
Assertions.assertTrue(encs.contains("1973111100"));
Assertions.assertTrue(encs.contains("13 Markeri Street"));
Assertions.assertTrue(encs.contains("4218"));
Assertions.assertTrue(encs.contains("Mermaid Beach"));
Set<String> expectedCAExtensions = new HashSet<>(Arrays.asList(Parser.OID_STREET_ADDRESS, Parser.OID_SUBURB, Parser.OID_POSTAL_CODE));
Set<String> caOids = Arrays.stream(extensions.get("National Change of Address").getExtensionOIDs()).map(c -> c.toString()).collect(Collectors.toSet());
Assertions.assertEquals(expectedCAExtensions.size(), caOids.size());
Assertions.assertEquals(expectedCAExtensions, caOids);
Set<String> caEncs = Arrays.stream(extensions.get("National Change of Address").getExtensionOIDs()).map(c -> new String(extensions.get("National Change of Address").getExtension(c).getExtnValue().getOctets())).collect(Collectors.toSet());
Assertions.assertEquals(caEncs.size(), 3);
Assertions.assertTrue(caEncs.contains("13 Markeri Street"));
Assertions.assertTrue(caEncs.contains("4218"));
Assertions.assertTrue(caEncs.contains("Mermaid Beach"));
}
use of com.github.zhenwei.core.asn1.x500.X500Name in project attestation by TokenScript.
the class Attestor method constructAttestations.
/**
* Constructs a list of X509 attestations to each of the relevant DatasourceName lists of elements
* in the response json.
*
* @param request Json request in a Sring - verification request that was sent to Trulioo Global Gateway†
* @param verifyRecord Json object of the Record in verifyResponse, from Trulioo Global Gateway‡
* @param signature DER encoded signature of exactly the json request string encoded as UTF-8 using a Secp256k1 key with Keccak
* @param userPK user's public key (SubjectPublicKeyInfo object)
* @return List of DER encoded x509 attestations
*
* † An example can be found https://developer.trulioo.com/docs/identity-verification-step-6-verify
* ‡ Observe the "Record" in https://developer.trulioo.com/docs/identity-verification-verify-response
*/
public List<X509CertificateHolder> constructAttestations(String request, JSONObject verifyRecord, byte[] signature, AsymmetricKeyParameter userPK) {
if (!SignatureUtil.verifySha256(request.getBytes(StandardCharsets.UTF_8), signature, userPK)) {
throw ExceptionUtil.throwException(logger, new IllegalArgumentException("Request signature verification failed. " + "Make sure that your message is unaltered, signature is created by hashing the message with SHA256" + "and using a key of secp256k1 type."));
}
List<X509CertificateHolder> res = new ArrayList<>();
Parser parser = new Parser(new JSONObject(request), verifyRecord);
Map<String, X500Name> subjectNames = parser.getX500Names();
Map<String, Extensions> subjectExtensions = parser.getExtensions();
for (String currentAttName : subjectNames.keySet()) {
try {
long time = System.currentTimeMillis();
V3TBSCertificateGenerator certBuilder = new V3TBSCertificateGenerator();
certBuilder.setSignature(serverSigningAlgo);
certBuilder.setIssuer(serverInfo);
certBuilder.setSerialNumber(new ASN1Integer(time));
certBuilder.setStartDate(new Time(new Date(time)));
certBuilder.setEndDate(new Time(new Date(time + lifeTime)));
SubjectPublicKeyInfo spki = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(userPK);
// // todo hack to create a valid spki without ECNamedParameters
// spki = new SubjectPublicKeyInfo(new AlgorithmIdentifier(new ASN1ObjectIdentifier(OID_ECDSA)),
// spki.getPublicKeyData());
certBuilder.setSubjectPublicKeyInfo(spki);
certBuilder.setSubject(subjectNames.get(currentAttName));
certBuilder.setExtensions(subjectExtensions.get(currentAttName));
TBSCertificate tbsCert = certBuilder.generateTBSCertificate();
res.add(new X509CertificateHolder(constructSignedAttestation(tbsCert)));
// To ensure that we get a new serial number for every cert
Thread.sleep(1);
} catch (IOException e) {
throw ExceptionUtil.makeRuntimeException(logger, "Could not parse server key", e);
} catch (InterruptedException e) {
throw ExceptionUtil.makeRuntimeException(logger, "Could not sleep", e);
}
}
return res;
}
use of com.github.zhenwei.core.asn1.x500.X500Name in project attestation by TokenScript.
the class IdentifierAttestation method makeLabeledURI.
/**
* @param label the label of the URL, similar to what is inside <a>...</a>
* @param URL the URL itself, similar to what is in <a href="...">, note that
* it should already be URLencoded therefore not containing space
*/
private X500Name makeLabeledURI(String label, String URL) {
DERUTF8String labeledURLValue = new DERUTF8String(URL + " " + label);
RDN rdn = new RDN(LABELED_URI, labeledURLValue);
return new X500Name(new RDN[] { rdn });
}
Aggregations