Search in sources :

Example 31 with AlgorithmId

use of sun.security.x509.AlgorithmId in project jdk8u_jdk by JetBrains.

the class NonStandardNames method main.

public static void main(String[] args) throws Exception {
    byte[] data = "Hello".getBytes();
    X500Name n = new X500Name("cn=Me");
    CertAndKeyGen cakg = new CertAndKeyGen("RSA", "SHA256withRSA");
    cakg.generate(1024);
    X509Certificate cert = cakg.getSelfCertificate(n, 1000);
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    PKCS9Attributes authed = new PKCS9Attributes(new PKCS9Attribute[] { new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID), new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, md.digest(data)) });
    Signature s = Signature.getInstance("SHA256withRSA");
    s.initSign(cakg.getPrivateKey());
    s.update(authed.getDerEncoding());
    byte[] sig = s.sign();
    SignerInfo signerInfo = new SignerInfo(n, cert.getSerialNumber(), AlgorithmId.get("SHA-256"), authed, AlgorithmId.get("SHA256withRSA"), sig, null);
    PKCS7 pkcs7 = new PKCS7(new AlgorithmId[] { signerInfo.getDigestAlgorithmId() }, new ContentInfo(data), new X509Certificate[] { cert }, new SignerInfo[] { signerInfo });
    if (pkcs7.verify(signerInfo, data) == null) {
        throw new Exception("Not verified");
    }
}
Also used : SignerInfo(sun.security.pkcs.SignerInfo) PKCS9Attribute(sun.security.pkcs.PKCS9Attribute) ContentInfo(sun.security.pkcs.ContentInfo) PKCS7(sun.security.pkcs.PKCS7) CertAndKeyGen(sun.security.tools.keytool.CertAndKeyGen) Signature(java.security.Signature) X500Name(sun.security.x509.X500Name) MessageDigest(java.security.MessageDigest) PKCS9Attributes(sun.security.pkcs.PKCS9Attributes) X509Certificate(java.security.cert.X509Certificate)

Example 32 with AlgorithmId

use of sun.security.x509.AlgorithmId in project jdk8u_jdk by JetBrains.

the class TestProvider method main.

public static void main(String[] args) throws Exception {
    TestProvider p = new TestProvider();
    Security.addProvider(p);
    AlgorithmId algid = AlgorithmId.getAlgorithmId("XYZ");
    String alias = "Alg.Alias.Signature.OID." + algid.toString();
    String stdAlgName = p.getProperty(alias);
    if (stdAlgName == null || !stdAlgName.equalsIgnoreCase("XYZ")) {
        throw new Exception("Wrong OID");
    }
}
Also used : AlgorithmId(sun.security.x509.AlgorithmId)

Example 33 with AlgorithmId

use of sun.security.x509.AlgorithmId in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class CredentialStorage method isHardwareBackedKey.

private boolean isHardwareBackedKey(byte[] keyData) {
    try {
        ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(keyData));
        PrivateKeyInfo pki = PrivateKeyInfo.getInstance(bIn.readObject());
        String algOid = pki.getAlgorithmId().getAlgorithm().getId();
        String algName = new AlgorithmId(new ObjectIdentifier(algOid)).getName();
        return KeyChain.isBoundKeyAlgorithm(algName);
    } catch (IOException e) {
        Log.e(TAG, "Failed to parse key data");
        return false;
    }
}
Also used : ASN1InputStream(com.android.org.bouncycastle.asn1.ASN1InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AlgorithmId(sun.security.x509.AlgorithmId) IOException(java.io.IOException) PrivateKeyInfo(com.android.org.bouncycastle.asn1.pkcs.PrivateKeyInfo) ObjectIdentifier(sun.security.util.ObjectIdentifier)

Aggregations

AlgorithmId (sun.security.x509.AlgorithmId)24 CertificateException (java.security.cert.CertificateException)10 X500Name (sun.security.x509.X500Name)10 X509CertImpl (sun.security.x509.X509CertImpl)9 AlgorithmParameters (java.security.AlgorithmParameters)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 X509Certificate (java.security.cert.X509Certificate)7 SecretKey (javax.crypto.SecretKey)7 IOException (java.io.IOException)6 BigInteger (java.math.BigInteger)6 UnrecoverableKeyException (java.security.UnrecoverableKeyException)6 ObjectIdentifier (sun.security.util.ObjectIdentifier)6 CertificateAlgorithmId (sun.security.x509.CertificateAlgorithmId)6 KeyStoreException (java.security.KeyStoreException)5 CertificateFactory (java.security.cert.CertificateFactory)5 ContentInfo (sun.security.pkcs.ContentInfo)5 PKCS7 (sun.security.pkcs.PKCS7)5 SignerInfo (sun.security.pkcs.SignerInfo)5 PrivateKey (java.security.PrivateKey)4 UnrecoverableEntryException (java.security.UnrecoverableEntryException)4