Search in sources :

Example 26 with SubjectPublicKeyInfo

use of com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo in project attestation by TokenScript.

the class AttestationTest method testGetterSetter.

@Test
public void testGetterSetter() throws Exception {
    Attestation att = new Attestation();
    att.setVersion(19);
    assertEquals(att.getVersion(), 19);
    att.setSerialNumber(42);
    assertEquals(att.getSerialNumber(), 42);
    att.setIssuer("CN=ALX");
    assertEquals(att.getIssuer(), "CN=ALX");
    Date now = new Date();
    att.setNotValidBefore(now);
    assertEquals(att.getNotValidBefore().toString(), now.toString());
    Date later = new Date(Clock.systemUTC().millis() + 1000);
    att.setNotValidAfter(later);
    assertEquals(att.getNotValidAfter().toString(), later.toString());
    att.setSubject("CN=me");
    assertEquals(att.getSubject(), "CN=me");
    SubjectPublicKeyInfo newSpki = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(subjectKeys.getPublic());
    att.setSubjectPublicKeyInfo(newSpki);
    assertEquals(att.getSubjectPublicKeyInfo(), newSpki);
    att.setSmartcontracts(Arrays.asList(42L, 13L));
    assertEquals(att.getSmartcontracts(), Arrays.asList(42L, 13L));
    att.setExtensions(new DERSequence());
    assertEquals(att.getExtensions(), new DERSequence());
    Attestation att2 = new Attestation();
    att2.setDataObject(new DERSequence());
    assertEquals(att2.getDataObject(), new DERSequence());
}
Also used : SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 27 with SubjectPublicKeyInfo

use of com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo in project attestation by TokenScript.

the class HelperTest method makeUnsignedx509Att.

/* the unsigned x509 attestation will have a subject of "CN=0x2042424242424564648" */
public static Attestation makeUnsignedx509Att(AsymmetricKeyParameter key) throws IOException {
    Attestation att = new Attestation();
    // =v3 since counting starts from 0
    att.setVersion(2);
    att.setSerialNumber(42);
    // ECDSA with SHA256 which is needed for a proper x509
    att.setSigningAlgorithm(SignedIdentifierAttestation.ECDSA_WITH_SHA256);
    att.setIssuer("CN=ALX");
    Date now = new Date();
    att.setNotValidBefore(now);
    att.setNotValidAfter(new Date(System.currentTimeMillis() + VALIDITY));
    att.setSubject("CN=0x2042424242424564648");
    SubjectPublicKeyInfo spki = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(key);
    att.setSubjectPublicKeyInfo(spki);
    ASN1EncodableVector extensions = new ASN1EncodableVector();
    extensions.add(Attestation.OID_OCTETSTRING);
    extensions.add(ASN1Boolean.TRUE);
    extensions.add(new DEROctetString("hello world".getBytes()));
    // Double Sequence is needed to be compatible with X509V3
    att.setExtensions(new DERSequence(new DERSequence(extensions)));
    assertTrue(att.isValidX509());
    return att;
}
Also used : DERSequence(org.bouncycastle.asn1.DERSequence) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) Date(java.util.Date) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Example 28 with SubjectPublicKeyInfo

use of com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo in project attestation by TokenScript.

the class IdentifierAttestationTest method testInvalidPublicKey.

@Test
public void testInvalidPublicKey() throws Exception {
    IdentifierAttestation initial = HelperTest.makeUnsignedStandardAtt(subjectKeys.getPublic(), BigInteger.ONE, mail);
    Field field = initial.getClass().getSuperclass().getDeclaredField("subjectPublicKeyInfo");
    field.setAccessible(true);
    // Change the public key
    SubjectPublicKeyInfo spki = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(otherKeys.getPublic());
    field.set(initial, spki);
    // The key is only stored one place so it is allowed to change it as long as the attestation has not been signed
    assertTrue(initial.checkValidity());
}
Also used : Field(java.lang.reflect.Field) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) Test(org.junit.jupiter.api.Test)

Example 29 with SubjectPublicKeyInfo

use of com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo in project attestation by TokenScript.

the class TestRedeemCheque method testNegativeUnmatchingKeys.

// Test that the key used to sign the RedeemCheque is the same as attested to
@Test
public void testNegativeUnmatchingKeys() throws Exception {
    Attestation att = attestedCheque.getAtt().getUnsignedAttestation();
    Field field = att.getClass().getSuperclass().getDeclaredField("subjectPublicKeyInfo");
    field.setAccessible(true);
    SubjectPublicKeyInfo spki = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(issuerKeys.getPublic());
    assertFalse(Arrays.equals(spki.getEncoded(), att.getSubjectPublicKeyInfo().getEncoded()));
    // Change public key
    field.set(att, spki);
    // Validation should not fail for attestation
    assertTrue(attestedCheque.getAtt().checkValidity());
    // However it should for the whole object since the keys no longer match
    assertFalse(attestedCheque.checkValidity());
    // Verification should fail
    assertFalse(attestedCheque.getAtt().verify());
    assertFalse(attestedCheque.verify());
}
Also used : Field(java.lang.reflect.Field) SignedIdentifierAttestation(org.tokenscript.attestation.SignedIdentifierAttestation) IdentifierAttestation(org.tokenscript.attestation.IdentifierAttestation) Attestation(org.tokenscript.attestation.Attestation) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) Test(org.junit.jupiter.api.Test) HelperTest(org.tokenscript.attestation.HelperTest)

Example 30 with SubjectPublicKeyInfo

use of com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo in project attestation by TokenScript.

the class TicketTest method sunshine.

@Test
public void sunshine() throws Exception {
    Ticket ticket = new Ticket(MAIL, CONFERENCE_ID, TICKET_ID, TICKET_CLASS, senderKeys, SECRET);
    assertEquals(TICKET_ID, ticket.getTicketId());
    assertEquals(TICKET_CLASS, ticket.getTicketClass());
    assertEquals(CONFERENCE_ID, ticket.getDevconId());
    SubjectPublicKeyInfo ticketSpki = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(ticket.getPublicKey());
    SubjectPublicKeyInfo senderSpki = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(senderKeys.getPublic());
    assertArrayEquals(senderSpki.getEncoded(), ticketSpki.getEncoded());
}
Also used : SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) Test(org.junit.jupiter.api.Test)

Aggregations

SubjectPublicKeyInfo (org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)219 IOException (java.io.IOException)102 X500Name (org.bouncycastle.asn1.x500.X500Name)95 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)84 Date (java.util.Date)81 ContentSigner (org.bouncycastle.operator.ContentSigner)69 X509CertificateHolder (org.bouncycastle.cert.X509CertificateHolder)65 X509v3CertificateBuilder (org.bouncycastle.cert.X509v3CertificateBuilder)64 BigInteger (java.math.BigInteger)63 JcaX509CertificateConverter (org.bouncycastle.cert.jcajce.JcaX509CertificateConverter)53 JcaContentSignerBuilder (org.bouncycastle.operator.jcajce.JcaContentSignerBuilder)52 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)43 KeyPair (java.security.KeyPair)39 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)34 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)34 PublicKey (java.security.PublicKey)31 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)30 InvalidKeyException (java.security.InvalidKeyException)30 KeyPairGenerator (java.security.KeyPairGenerator)30