use of com.android.apksig.internal.x509.SubjectPublicKeyInfo in project axelor-open-suite by axelor.
the class X509Generator method getSubjectKeyIdentifier.
/**
* Returns the <code>SubjectKeyIdentifier</code> corresponding to a given <code>PublicKey</code>
*
* @param publicKey the given public key
* @return the subject key identifier
* @throws IOException
* @throws NoSuchAlgorithmException
*/
private SubjectKeyIdentifier getSubjectKeyIdentifier(PublicKey publicKey) throws IOException, NoSuchAlgorithmException {
InputStream input;
SubjectPublicKeyInfo keyInfo;
input = new ByteArrayInputStream(publicKey.getEncoded());
try (final ASN1InputStream is = new ASN1InputStream(input)) {
keyInfo = SubjectPublicKeyInfo.getInstance((ASN1Sequence) is.readObject());
}
final JcaX509ExtensionUtils jcaX509ExtensionUtils = new JcaX509ExtensionUtils();
return jcaX509ExtensionUtils.createSubjectKeyIdentifier(keyInfo);
}
use of com.android.apksig.internal.x509.SubjectPublicKeyInfo in project documentproduction by qld-gov-au.
the class AwsContentSignerFactory method getPublicKey.
@Override
public PublicKey getPublicKey(SignatureKey key) {
if ("stub".equals(this.region)) {
return null;
}
AWSKMS kmsClient = AWSKMSClientBuilder.standard().withRegion(region).build();
GetPublicKeyResult response = kmsClient.getPublicKey(new GetPublicKeyRequest().withKeyId(key.getKmsId()));
SubjectPublicKeyInfo spki = SubjectPublicKeyInfo.getInstance(response.getPublicKey().array());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
try {
return converter.getPublicKey(spki);
} catch (PEMException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
use of com.android.apksig.internal.x509.SubjectPublicKeyInfo in project documentproduction by qld-gov-au.
the class SigningServiceTest method setUpKeys.
private static void setUpKeys() throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
KeyPair keyPair = keyGen.generateKeyPair();
X500Name x500Name = new X500Name("CN=test");
SubjectPublicKeyInfo pubKeyInfo = SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded());
final X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder(x500Name, new BigInteger(10, new SecureRandom()), new Date(), new LocalDateTime().plusDays(1).toDate(), x500Name, pubKeyInfo);
contentSigner = new JcaContentSignerBuilder("SHA256WithRSA").build(keyPair.getPrivate());
certificate = new JcaX509CertificateConverter().setProvider(new BouncyCastleProvider()).getCertificate(certificateBuilder.build(contentSigner));
}
use of com.android.apksig.internal.x509.SubjectPublicKeyInfo 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.android.apksig.internal.x509.SubjectPublicKeyInfo in project attestation by TokenScript.
the class UseTicketTest method testNegativeUnmatchingKeys.
// Test that the key used to sign the Attested Ticket is the same as attested to
@Test
public void testNegativeUnmatchingKeys() throws Exception {
Attestation att = attestedTicket.getAtt().getUnsignedAttestation();
Field field = att.getClass().getSuperclass().getDeclaredField("subjectPublicKeyInfo");
field.setAccessible(true);
SubjectPublicKeyInfo spki = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(attestorKeys.getPublic());
assertFalse(Arrays.equals(spki.getEncoded(), att.getSubjectPublicKeyInfo().getEncoded()));
// Change public key
field.set(att, spki);
// Validation of attestation should not fail
assertTrue(attestedTicket.getAtt().checkValidity());
// But validation of ticket should since the keys used are not consistent
assertFalse(attestedTicket.checkValidity());
// Verification should fail
assertFalse(attestedTicket.getAtt().verify());
assertFalse(attestedTicket.verify());
}
Aggregations