Search in sources :

Example 6 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project gdmatrix by gdmatrix.

the class P7MUtils method printAttribute.

public static void printAttribute(Attribute attribute) throws Exception {
    ASN1Set set = attribute.getAttrValues();
    ASN1Primitive der = set.getObjectAt(0).toASN1Primitive();
    System.out.println(der.getClass());
    if (der instanceof DEROctetString) {
        DEROctetString octet = (DEROctetString) der;
        byte[] data = octet.getOctets();
        System.out.println(new String(data, "UTF-16LE"));
    } else if (der instanceof ASN1UTCTime) {
        ASN1UTCTime utcTime = (ASN1UTCTime) der;
        String time = utcTime.getAdjustedTime();
        System.out.println(time);
    } else if (der instanceof ASN1ObjectIdentifier) {
        ASN1ObjectIdentifier id = (ASN1ObjectIdentifier) der;
        System.out.println(id.getId());
    }
}
Also used : ASN1Set(org.bouncycastle.asn1.ASN1Set) ASN1UTCTime(org.bouncycastle.asn1.ASN1UTCTime) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 7 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project jiguang-java-client-common by jpush.

the class BCECUtil method convertECPrivateKeyToSEC1.

/**
 * 将ECC私钥转换为SEC1标准的字节流
 * openssl d2i_ECPrivateKey函数要求的DER编码的私钥也是SEC1标准的,
 * 这个工具函数的主要目的就是为了能生成一个openssl可以直接“识别”的ECC私钥.
 * 相对RSA私钥的PKCS1标准,ECC私钥的标准为SEC1
 *
 * @param priKey
 * @param pubKey
 * @return
 * @throws IOException
 */
public static byte[] convertECPrivateKeyToSEC1(ECPrivateKeyParameters priKey, ECPublicKeyParameters pubKey) throws IOException {
    byte[] pkcs8Bytes = convertECPrivateKeyToPKCS8(priKey, pubKey);
    PrivateKeyInfo pki = PrivateKeyInfo.getInstance(pkcs8Bytes);
    ASN1Encodable encodable = pki.parsePrivateKey();
    ASN1Primitive primitive = encodable.toASN1Primitive();
    byte[] sec1Bytes = primitive.getEncoded();
    return sec1Bytes;
}
Also used : ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 8 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project java-webauthn-server by Yubico.

the class ExtensionMatcher method matches.

@Override
public boolean matches(X509Certificate attestationCertificate, JsonNode parameters) {
    String matchKey = parameters.get(EXTENSION_KEY).asText();
    JsonNode matchValue = parameters.get(EXTENSION_VALUE);
    byte[] extensionValue = attestationCertificate.getExtensionValue(matchKey);
    if (extensionValue != null) {
        if (matchValue == null) {
            return true;
        } else {
            try {
                final ASN1Primitive value = ASN1Primitive.fromByteArray(extensionValue);
                if (matchValue.isObject()) {
                    if (matchTypedValue(matchKey, matchValue, value)) {
                        return true;
                    }
                } else if (matchValue.isTextual()) {
                    if (matchStringValue(matchKey, matchValue, value))
                        return true;
                }
            } catch (IOException e) {
                log.error("Failed to parse extension value as ASN1: {}", new ByteArray(extensionValue).getHex(), e);
            }
        }
    }
    return false;
}
Also used : ByteArray(com.yubico.webauthn.data.ByteArray) JsonNode(com.fasterxml.jackson.databind.JsonNode) DEROctetString(org.bouncycastle.asn1.DEROctetString) IOException(java.io.IOException) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Example 9 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project SAMLRaider by CompassSecurity.

the class BurpCertificate method getAuthorityKeyIdentifier.

public String getAuthorityKeyIdentifier() {
    byte[] e = certificate.getExtensionValue(Extension.authorityKeyIdentifier.getId());
    if (e == null) {
        return "";
    }
    ASN1Primitive ap;
    byte[] k = {};
    try {
        ap = JcaX509ExtensionUtils.parseExtensionValue(e);
        k = ASN1Sequence.getInstance(ap.getEncoded()).getEncoded();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // Extension :(
    return CertificateHelper.addHexColons(CertificateHelper.byteArrayToHex(k)).substring(12, k.length * 3 - 1);
}
Also used : IOException(java.io.IOException) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Example 10 with ASN1Primitive

use of com.github.zhenwei.core.asn1.ASN1Primitive in project CipherTrust_Application_Protection by thalescpl-io.

the class ByokSample method readAWSPublicKeyFromFile.

private static byte[] readAWSPublicKeyFromFile(String publicKeyPath) throws Exception {
    File file = new File(publicKeyPath);
    FileInputStream ios = new FileInputStream(file);
    byte[] buffer = new byte[(int) file.length()];
    try {
        ios.read(buffer);
    } finally {
        ios.close();
    }
    // format conversion to PEM encoded PKCS#1 to allow import on to
    // keysecure
    X509EncodedKeySpec spec1 = new X509EncodedKeySpec(buffer);
    KeyFactory kf1 = KeyFactory.getInstance("RSA");
    RSAPublicKey pubKey = (RSAPublicKey) kf1.generatePublic(spec1);
    byte[] pubBytes = pubKey.getEncoded();
    SubjectPublicKeyInfo spkInfo = SubjectPublicKeyInfo.getInstance(pubBytes);
    ASN1Primitive primitive = spkInfo.parsePublicKey();
    if (primitive != null)
        return primitive.getEncoded();
    return null;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) File(java.io.File) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) FileInputStream(java.io.FileInputStream) KeyFactory(java.security.KeyFactory)

Aggregations

ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)253 DERSequence (com.github.zhenwei.core.asn1.DERSequence)231 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)177 IOException (java.io.IOException)107 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)62 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)57 DERTaggedObject (com.github.zhenwei.core.asn1.DERTaggedObject)55 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)42 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)38 ByteArrayInputStream (java.io.ByteArrayInputStream)38 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)32 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)31 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)31 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)30 DEROctetString (org.bouncycastle.asn1.DEROctetString)28 BigInteger (java.math.BigInteger)24 GeneralSecurityException (java.security.GeneralSecurityException)24 X509Certificate (java.security.cert.X509Certificate)24 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)23 DERIA5String (org.bouncycastle.asn1.DERIA5String)22