Search in sources :

Example 66 with ASN1BitString

use of com.unboundid.asn1.ASN1BitString in project LinLong-Java by zhenwei1108.

the class BCDSTU4145PublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    ASN1BitString bits = info.getPublicKeyData();
    ASN1OctetString key;
    this.algorithm = "DSTU4145";
    try {
        key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
    } catch (IOException ex) {
        throw new IllegalArgumentException("error recovering public key");
    }
    byte[] keyEnc = key.getOctets();
    if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
        reverseBytes(keyEnc);
    }
    ASN1Sequence seq = ASN1Sequence.getInstance(info.getAlgorithm().getParameters());
    com.github.zhenwei.provider.jce.spec.ECParameterSpec spec = null;
    X9ECParameters x9Params = null;
    if (seq.getObjectAt(0) instanceof ASN1Integer) {
        x9Params = X9ECParameters.getInstance(seq);
        spec = new com.github.zhenwei.provider.jce.spec.ECParameterSpec(x9Params.getCurve(), x9Params.getG(), x9Params.getN(), x9Params.getH(), x9Params.getSeed());
    } else {
        dstuParams = DSTU4145Params.getInstance(seq);
        if (dstuParams.isNamedCurve()) {
            ASN1ObjectIdentifier curveOid = dstuParams.getNamedCurve();
            ECDomainParameters ecP = DSTU4145NamedCurves.getByOID(curveOid);
            spec = new ECNamedCurveParameterSpec(curveOid.getId(), ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
        } else {
            DSTU4145ECBinary binary = dstuParams.getECBinary();
            byte[] b_bytes = binary.getB();
            if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
                reverseBytes(b_bytes);
            }
            DSTU4145BinaryField field = binary.getField();
            ECCurve curve = new ECCurve.F2m(field.getM(), field.getK1(), field.getK2(), field.getK3(), binary.getA(), new BigInteger(1, b_bytes));
            byte[] g_bytes = binary.getG();
            if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le)) {
                reverseBytes(g_bytes);
            }
            spec = new com.github.zhenwei.provider.jce.spec.ECParameterSpec(curve, DSTU4145PointEncoder.decodePoint(curve, g_bytes), binary.getN());
        }
    }
    ECCurve curve = spec.getCurve();
    EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
    if (dstuParams != null) {
        ECPoint g = EC5Util.convertPoint(spec.getG());
        if (dstuParams.isNamedCurve()) {
            String name = dstuParams.getNamedCurve().getId();
            ecSpec = new ECNamedCurveSpec(name, ellipticCurve, g, spec.getN(), spec.getH());
        } else {
            ecSpec = new ECParameterSpec(ellipticCurve, g, spec.getN(), spec.getH().intValue());
        }
    } else {
        ecSpec = EC5Util.convertToSpec(x9Params);
    }
    // this.q = curve.createPoint(new BigInteger(1, x), new BigInteger(1, y), false);
    this.ecPublicKey = new ECPublicKeyParameters(DSTU4145PointEncoder.decodePoint(curve, keyEnc), EC5Util.getDomainParameters(null, ecSpec));
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) DSTU4145ECBinary(com.github.zhenwei.core.asn1.ua.DSTU4145ECBinary) IOException(java.io.IOException) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) DSTU4145BinaryField(com.github.zhenwei.core.asn1.ua.DSTU4145BinaryField) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 67 with ASN1BitString

use of com.unboundid.asn1.ASN1BitString in project LinLong-Java by zhenwei1108.

the class BCECPublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    X962Parameters params = X962Parameters.getInstance(info.getAlgorithm().getParameters());
    ECCurve curve = EC5Util.getCurve(configuration, params);
    ecSpec = EC5Util.convertToSpec(params, curve);
    ASN1BitString bits = info.getPublicKeyData();
    byte[] data = bits.getBytes();
    ASN1OctetString key = new DEROctetString(data);
    // 
    if (data[0] == 0x04 && data[1] == data.length - 2 && (data[2] == 0x02 || data[2] == 0x03)) {
        int qLength = new X9IntegerConverter().getByteLength(curve);
        if (qLength >= data.length - 3) {
            try {
                key = (ASN1OctetString) ASN1Primitive.fromByteArray(data);
            } catch (IOException ex) {
                throw new IllegalArgumentException("error recovering public key");
            }
        }
    }
    X9ECPoint derQ = new X9ECPoint(curve, key);
    this.ecPublicKey = new ECPublicKeyParameters(derQ.getPoint(), ECUtil.getDomainParameters(configuration, params));
}
Also used : X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) X9IntegerConverter(com.github.zhenwei.core.asn1.x9.X9IntegerConverter) IOException(java.io.IOException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint)

Example 68 with ASN1BitString

use of com.unboundid.asn1.ASN1BitString in project LinLong-Java by zhenwei1108.

the class X509V2AttributeCertificate method getIssuerUniqueID.

public boolean[] getIssuerUniqueID() {
    ASN1BitString id = cert.getAcinfo().getIssuerUniqueID();
    if (id != null) {
        byte[] bytes = id.getBytes();
        boolean[] boolId = new boolean[bytes.length * 8 - id.getPadBits()];
        for (int i = 0; i != boolId.length; i++) {
            boolId[i] = (bytes[i / 8] & (0x80 >>> (i % 8))) != 0;
        }
        return boolId;
    }
    return null;
}
Also used : ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString)

Example 69 with ASN1BitString

use of com.unboundid.asn1.ASN1BitString in project LinLong-Java by zhenwei1108.

the class PrivateKeyFactory method createKey.

/**
 * Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
 *
 * @param keyInfo the PrivateKeyInfo object containing the key material
 * @return a suitable private key parameter
 * @throws IOException on an error decoding the key
 */
public static AsymmetricKeyParameter createKey(PrivateKeyInfo keyInfo) throws IOException {
    AlgorithmIdentifier algId = keyInfo.getPrivateKeyAlgorithm();
    ASN1ObjectIdentifier algOID = algId.getAlgorithm();
    if (algOID.on(BCObjectIdentifiers.qTESLA)) {
        ASN1OctetString qTESLAPriv = ASN1OctetString.getInstance(keyInfo.parsePrivateKey());
        return new QTESLAPrivateKeyParameters(Utils.qTeslaLookupSecurityCategory(keyInfo.getPrivateKeyAlgorithm()), qTESLAPriv.getOctets());
    } else if (algOID.equals(BCObjectIdentifiers.sphincs256)) {
        return new SPHINCSPrivateKeyParameters(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets(), Utils.sphincs256LookupTreeAlgName(SPHINCS256KeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters())));
    } else if (algOID.equals(BCObjectIdentifiers.newHope)) {
        return new NHPrivateKeyParameters(convert(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets()));
    } else if (algOID.equals(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig)) {
        byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        ASN1BitString pubKey = keyInfo.getPublicKeyData();
        if (Pack.bigEndianToInt(keyEnc, 0) == 1) {
            if (pubKey != null) {
                byte[] pubEnc = pubKey.getOctets();
                return LMSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), Arrays.copyOfRange(pubEnc, 4, pubEnc.length));
            }
            return LMSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
        } else {
            if (pubKey != null) {
                byte[] pubEnc = pubKey.getOctets();
                return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), pubEnc);
            }
            return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
        }
    } else if (algOID.equals(BCObjectIdentifiers.xmss)) {
        XMSSKeyParams keyParams = XMSSKeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters());
        ASN1ObjectIdentifier treeDigest = keyParams.getTreeDigest().getAlgorithm();
        XMSSPrivateKey xmssPrivateKey = XMSSPrivateKey.getInstance(keyInfo.parsePrivateKey());
        try {
            XMSSPrivateKeyParameters.Builder keyBuilder = new XMSSPrivateKeyParameters.Builder(new XMSSParameters(keyParams.getHeight(), Utils.getDigest(treeDigest))).withIndex(xmssPrivateKey.getIndex()).withSecretKeySeed(xmssPrivateKey.getSecretKeySeed()).withSecretKeyPRF(xmssPrivateKey.getSecretKeyPRF()).withPublicSeed(xmssPrivateKey.getPublicSeed()).withRoot(xmssPrivateKey.getRoot());
            if (xmssPrivateKey.getVersion() != 0) {
                keyBuilder.withMaxIndex(xmssPrivateKey.getMaxIndex());
            }
            if (xmssPrivateKey.getBdsState() != null) {
                BDS bds = (BDS) XMSSUtil.deserialize(xmssPrivateKey.getBdsState(), BDS.class);
                keyBuilder.withBDSState(bds.withWOTSDigest(treeDigest));
            }
            return keyBuilder.build();
        } catch (ClassNotFoundException e) {
            throw new IOException("ClassNotFoundException processing BDS state: " + e.getMessage());
        }
    } else if (algOID.equals(PQCObjectIdentifiers.xmss_mt)) {
        XMSSMTKeyParams keyParams = XMSSMTKeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters());
        ASN1ObjectIdentifier treeDigest = keyParams.getTreeDigest().getAlgorithm();
        try {
            XMSSMTPrivateKey xmssMtPrivateKey = XMSSMTPrivateKey.getInstance(keyInfo.parsePrivateKey());
            XMSSMTPrivateKeyParameters.Builder keyBuilder = new XMSSMTPrivateKeyParameters.Builder(new XMSSMTParameters(keyParams.getHeight(), keyParams.getLayers(), Utils.getDigest(treeDigest))).withIndex(xmssMtPrivateKey.getIndex()).withSecretKeySeed(xmssMtPrivateKey.getSecretKeySeed()).withSecretKeyPRF(xmssMtPrivateKey.getSecretKeyPRF()).withPublicSeed(xmssMtPrivateKey.getPublicSeed()).withRoot(xmssMtPrivateKey.getRoot());
            if (xmssMtPrivateKey.getVersion() != 0) {
                keyBuilder.withMaxIndex(xmssMtPrivateKey.getMaxIndex());
            }
            if (xmssMtPrivateKey.getBdsState() != null) {
                BDSStateMap bdsState = (BDSStateMap) XMSSUtil.deserialize(xmssMtPrivateKey.getBdsState(), BDSStateMap.class);
                keyBuilder.withBDSState(bdsState.withWOTSDigest(treeDigest));
            }
            return keyBuilder.build();
        } catch (ClassNotFoundException e) {
            throw new IOException("ClassNotFoundException processing BDS state: " + e.getMessage());
        }
    } else if (algOID.equals(PQCObjectIdentifiers.mcElieceCca2)) {
        McElieceCCA2PrivateKey mKey = McElieceCCA2PrivateKey.getInstance(keyInfo.parsePrivateKey());
        return new McElieceCCA2PrivateKeyParameters(mKey.getN(), mKey.getK(), mKey.getField(), mKey.getGoppaPoly(), mKey.getP(), Utils.getDigestName(mKey.getDigest().getAlgorithm()));
    } else {
        throw new RuntimeException("algorithm identifier in private key not recognised");
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) XMSSKeyParams(com.github.zhenwei.core.pqc.asn1.XMSSKeyParams) McElieceCCA2PrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters) QTESLAPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.qtesla.QTESLAPrivateKeyParameters) NHPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.newhope.NHPrivateKeyParameters) XMSSMTPrivateKey(com.github.zhenwei.core.pqc.asn1.XMSSMTPrivateKey) McElieceCCA2PrivateKey(com.github.zhenwei.core.pqc.asn1.McElieceCCA2PrivateKey) BDSStateMap(com.github.zhenwei.core.pqc.crypto.xmss.BDSStateMap) SPHINCSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters) IOException(java.io.IOException) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) XMSSMTParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTParameters) BDS(com.github.zhenwei.core.pqc.crypto.xmss.BDS) XMSSParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSParameters) XMSSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSPrivateKeyParameters) XMSSMTKeyParams(com.github.zhenwei.core.pqc.asn1.XMSSMTKeyParams) XMSSPrivateKey(com.github.zhenwei.core.pqc.asn1.XMSSPrivateKey) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Example 70 with ASN1BitString

use of com.unboundid.asn1.ASN1BitString in project LinLong-Java by zhenwei1108.

the class X509CertificateObject method createKeyUsage.

private static boolean[] createKeyUsage(com.github.zhenwei.core.asn1.x509.Certificate c) throws CertificateParsingException {
    try {
        byte[] extOctets = getExtensionOctets(c, "2.5.29.15");
        if (null == extOctets) {
            return null;
        }
        ASN1BitString bits = DERBitString.getInstance(ASN1Primitive.fromByteArray(extOctets));
        byte[] bytes = bits.getBytes();
        int length = (bytes.length * 8) - bits.getPadBits();
        boolean[] keyUsage = new boolean[(length < 9) ? 9 : length];
        for (int i = 0; i != length; i++) {
            keyUsage[i] = (bytes[i / 8] & (0x80 >>> (i % 8))) != 0;
        }
        return keyUsage;
    } catch (Exception e) {
        throw new CertificateParsingException("cannot construct KeyUsage: " + e);
    }
}
Also used : CertificateParsingException(java.security.cert.CertificateParsingException) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) CertificateParsingException(java.security.cert.CertificateParsingException) IOException(java.io.IOException) CertificateExpiredException(java.security.cert.CertificateExpiredException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Aggregations

ASN1BitString (com.unboundid.asn1.ASN1BitString)72 Test (org.testng.annotations.Test)62 DN (com.unboundid.ldap.sdk.DN)49 ASN1Null (com.unboundid.asn1.ASN1Null)36 OID (com.unboundid.util.OID)33 ASN1ObjectIdentifier (com.unboundid.asn1.ASN1ObjectIdentifier)26 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)25 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)24 ASN1Element (com.unboundid.asn1.ASN1Element)23 ASN1BigInteger (com.unboundid.asn1.ASN1BigInteger)22 ASN1Integer (com.unboundid.asn1.ASN1Integer)20 IOException (java.io.IOException)16 ASN1BitString (com.github.zhenwei.core.asn1.ASN1BitString)14 ASN1BitString (org.bouncycastle.asn1.ASN1BitString)11 BigInteger (java.math.BigInteger)10 ArrayList (java.util.ArrayList)10 ASN1GeneralizedTime (com.unboundid.asn1.ASN1GeneralizedTime)9 NotNull (com.unboundid.util.NotNull)9 Date (java.util.Date)8 KeyPair (java.security.KeyPair)7