Search in sources :

Example 61 with ASN1BitString

use of com.unboundid.asn1.ASN1BitString in project jmulticard by ctt-gob-es.

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(final PrivateKeyInfo keyInfo) throws IOException {
    final AlgorithmIdentifier algId = keyInfo.getPrivateKeyAlgorithm();
    final ASN1ObjectIdentifier algOID = algId.getAlgorithm();
    if (algOID.on(BCObjectIdentifiers.qTESLA)) {
        final ASN1OctetString qTESLAPriv = ASN1OctetString.getInstance(keyInfo.parsePrivateKey());
        return new QTESLAPrivateKeyParameters(Utils.qTeslaLookupSecurityCategory(keyInfo.getPrivateKeyAlgorithm()), qTESLAPriv.getOctets());
    }
    if (algOID.equals(BCObjectIdentifiers.sphincs256)) {
        return new SPHINCSPrivateKeyParameters(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets(), Utils.sphincs256LookupTreeAlgName(SPHINCS256KeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters())));
    }
    if (algOID.equals(BCObjectIdentifiers.newHope)) {
        return new NHPrivateKeyParameters(convert(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets()));
    }
    if (algOID.equals(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final ASN1BitString pubKey = keyInfo.getPublicKeyData();
        if (Pack.bigEndianToInt(keyEnc, 0) == 1) {
            if (pubKey != null) {
                final 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));
        }
        if (pubKey != null) {
            final byte[] pubEnc = pubKey.getOctets();
            return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), pubEnc);
        }
        return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
    }
    if (algOID.on(BCObjectIdentifiers.sphincsPlus)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final SPHINCSPlusParameters spParams = SPHINCSPlusParameters.getParams(Integers.valueOf(Pack.bigEndianToInt(keyEnc, 0)));
        return new SPHINCSPlusPrivateKeyParameters(spParams, Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
    }
    if (algOID.on(BCObjectIdentifiers.pqc_kem_mceliece)) {
        final CMCEPrivateKey cmceKey = CMCEPrivateKey.getInstance(keyInfo.parsePrivateKey());
        final CMCEParameters spParams = Utils.mcElieceParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
        return new CMCEPrivateKeyParameters(spParams, cmceKey.getDelta(), cmceKey.getC(), cmceKey.getG(), cmceKey.getAlpha(), cmceKey.getS());
    } else if (algOID.on(BCObjectIdentifiers.pqc_kem_frodo)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final FrodoParameters spParams = Utils.frodoParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
        return new FrodoPrivateKeyParameters(spParams, keyEnc);
    } else if (algOID.on(BCObjectIdentifiers.pqc_kem_saber)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final SABERParameters spParams = Utils.saberParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
        return new SABERPrivateKeyParameters(spParams, keyEnc);
    } else if (algOID.equals(BCObjectIdentifiers.xmss) || algOID.equals(PQCObjectIdentifiers.xmss_mt)) {
        // $NON-NLS-1$
        throw new IOException("Modificacion para JMultiCard");
    } else if (algOID.equals(PQCObjectIdentifiers.mcElieceCca2)) {
        final 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 {
        // $NON-NLS-1$
        throw new RuntimeException("algorithm identifier in private key not recognised");
    }
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) McElieceCCA2PrivateKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters) QTESLAPrivateKeyParameters(org.bouncycastle.pqc.crypto.qtesla.QTESLAPrivateKeyParameters) SABERPrivateKeyParameters(org.bouncycastle.pqc.crypto.saber.SABERPrivateKeyParameters) NHPrivateKeyParameters(org.bouncycastle.pqc.crypto.newhope.NHPrivateKeyParameters) McElieceCCA2PrivateKey(org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey) SPHINCSPrivateKeyParameters(org.bouncycastle.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters) SPHINCSPlusParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters) IOException(java.io.IOException) SABERParameters(org.bouncycastle.pqc.crypto.saber.SABERParameters) FrodoPrivateKeyParameters(org.bouncycastle.pqc.crypto.frodo.FrodoPrivateKeyParameters) ASN1BitString(org.bouncycastle.asn1.ASN1BitString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) FrodoParameters(org.bouncycastle.pqc.crypto.frodo.FrodoParameters) CMCEParameters(org.bouncycastle.pqc.crypto.cmce.CMCEParameters) CMCEPrivateKeyParameters(org.bouncycastle.pqc.crypto.cmce.CMCEPrivateKeyParameters) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) SPHINCSPlusPrivateKeyParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPrivateKeyParameters) CMCEPrivateKey(org.bouncycastle.pqc.asn1.CMCEPrivateKey)

Example 62 with ASN1BitString

use of com.unboundid.asn1.ASN1BitString in project openkeystore by cyberphone.

the class XMLSignatureWrapper method readPublicKey.

public static PublicKey readPublicKey(DOMReaderHelper rd) throws IOException {
    PublicKey publicKey = null;
    try {
        if (rd.hasNext(RSA_KEY_VALUE_ELEM)) {
            rd.getNext(RSA_KEY_VALUE_ELEM);
            rd.getChild();
            publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(readCryptoBinary(rd, MODULUS_ELEM), readCryptoBinary(rd, EXPONENT_ELEM)));
        } else {
            rd.getNext(EC_KEY_VALUE_ELEM);
            rd.getChild();
            rd.getNext(NAMED_CURVE_ELEM);
            final String named_curve = rd.getAttributeHelper().getString(URI_ATTR);
            if (!named_curve.startsWith(KeyAlgorithms.XML_DSIG_CURVE_PREFIX)) {
                throw new IOException("Syntax error: " + named_curve);
            }
            publicKey = KeyFactory.getInstance("EC").generatePublic(new X509EncodedKeySpec(new ASN1Sequence(new BaseASN1Object[] { new ASN1Sequence(new BaseASN1Object[] { new ASN1ObjectID(EC_NAMED_CURVES_OID), new ASN1ObjectID(named_curve.substring(KeyAlgorithms.XML_DSIG_CURVE_PREFIX.length())) }), new ASN1BitString(rd.getBinary(PUBLIC_KEY_ELEM)) }).encode()));
        }
    } catch (GeneralSecurityException e) {
        throw new IOException(e);
    }
    rd.getParent();
    // Verify that it is one of the supported
    KeyAlgorithms.getKeyAlgorithm(publicKey);
    return publicKey;
}
Also used : ASN1Sequence(org.webpki.asn1.ASN1Sequence) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) BaseASN1Object(org.webpki.asn1.BaseASN1Object) ASN1ObjectID(org.webpki.asn1.ASN1ObjectID) GeneralSecurityException(java.security.GeneralSecurityException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ASN1BitString(org.webpki.asn1.ASN1BitString) IOException(java.io.IOException) ASN1BitString(org.webpki.asn1.ASN1BitString)

Example 63 with ASN1BitString

use of com.unboundid.asn1.ASN1BitString in project openkeystore by cyberphone.

the class KeyStore2PEMConverter method writePrivateKey.

public void writePrivateKey(PrivateKey privateKey, PublicKey publicKey) throws Exception {
    byte[] encoded;
    KeyAlgorithms keyAlgorithm = KeyAlgorithms.getKeyAlgorithm(privateKey);
    if (privateKey instanceof RSAKey) {
        encoded = privateKey.getEncoded();
    } else if (privateKey instanceof ECKey) {
        JSONObjectReader jwk = JSONParser.parse(new KeyStore2JWKConverter().writePrivateKey(privateKey, publicKey));
        encoded = new ASN1Sequence(new BaseASN1Object[] { new ASN1Integer(0), new ASN1Sequence(new BaseASN1Object[] { new ASN1ObjectID(EC_PUBLIC_KEY_OID), new ASN1ObjectID(keyAlgorithm.getECDomainOID()) }), new ASN1OctetString(new ASN1Sequence(new BaseASN1Object[] { new ASN1Integer(1), new ASN1OctetString(jwk.getBinary("d")), new CompositeContextSpecific(1, new ASN1BitString(ArrayUtil.add(new byte[] { 4 }, ArrayUtil.add(jwk.getBinary("x"), jwk.getBinary("y"))))) }).encode()) }).encode();
    } else {
        encoded = new ASN1Sequence(new BaseASN1Object[] { new ASN1Integer(1), new ASN1Sequence(new ASN1ObjectID(keyAlgorithm.getECDomainOID())), new ASN1OctetString(new ASN1OctetString(OkpSupport.private2RawOkpKey(privateKey, keyAlgorithm)).encode()), new SimpleContextSpecific(1, // BITSTRING unused bits
        ArrayUtil.add(// BITSTRING unused bits
        new byte[] { 0 }, OkpSupport.public2RawOkpKey(publicKey, keyAlgorithm))) }).encode();
    }
    writeObject("PRIVATE KEY", encoded);
}
Also used : ASN1OctetString(org.webpki.asn1.ASN1OctetString) RSAKey(java.security.interfaces.RSAKey) BaseASN1Object(org.webpki.asn1.BaseASN1Object) CompositeContextSpecific(org.webpki.asn1.CompositeContextSpecific) JSONObjectReader(org.webpki.json.JSONObjectReader) KeyAlgorithms(org.webpki.crypto.KeyAlgorithms) ECKey(java.security.interfaces.ECKey) ASN1Integer(org.webpki.asn1.ASN1Integer) ASN1BitString(org.webpki.asn1.ASN1BitString) ASN1Sequence(org.webpki.asn1.ASN1Sequence) ASN1ObjectID(org.webpki.asn1.ASN1ObjectID) SimpleContextSpecific(org.webpki.asn1.SimpleContextSpecific)

Example 64 with ASN1BitString

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

the class BCECGOST3410PublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    ASN1BitString bits = info.getPublicKeyData();
    ASN1OctetString key;
    this.algorithm = "ECGOST3410";
    try {
        key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
    } catch (IOException ex) {
        throw new IllegalArgumentException("error recovering public key");
    }
    byte[] keyEnc = key.getOctets();
    byte[] x9Encoding = new byte[65];
    x9Encoding[0] = 0x04;
    for (int i = 1; i <= 32; ++i) {
        x9Encoding[i] = keyEnc[32 - i];
        x9Encoding[i + 32] = keyEnc[64 - i];
    }
    ASN1ObjectIdentifier paramOID;
    if (info.getAlgorithm().getParameters() instanceof ASN1ObjectIdentifier) {
        paramOID = ASN1ObjectIdentifier.getInstance(info.getAlgorithm().getParameters());
        gostParams = paramOID;
    } else {
        GOST3410PublicKeyAlgParameters params = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
        gostParams = params;
        paramOID = params.getPublicKeyParamSet();
    }
    ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(paramOID));
    ECCurve curve = spec.getCurve();
    EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
    this.ecPublicKey = new ECPublicKeyParameters(curve.decodePoint(x9Encoding), ECUtil.getDomainParameters(null, spec));
    this.ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(paramOID), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) IOException(java.io.IOException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) EllipticCurve(java.security.spec.EllipticCurve) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 65 with ASN1BitString

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

the class BCECGOST3410_2012PublicKey method populateFromPubKeyInfo.

private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
    ASN1ObjectIdentifier algOid = info.getAlgorithm().getAlgorithm();
    ASN1BitString bits = info.getPublicKeyData();
    ASN1OctetString key;
    this.algorithm = "ECGOST3410-2012";
    try {
        key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
    } catch (IOException ex) {
        throw new IllegalArgumentException("error recovering public key");
    }
    byte[] keyEnc = key.getOctets();
    int fieldSize = 32;
    if (algOid.equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512)) {
        fieldSize = 64;
    }
    int keySize = 2 * fieldSize;
    byte[] x9Encoding = new byte[1 + keySize];
    x9Encoding[0] = 0x04;
    for (int i = 1; i <= fieldSize; ++i) {
        x9Encoding[i] = keyEnc[fieldSize - i];
        x9Encoding[i + fieldSize] = keyEnc[keySize - i];
    }
    this.gostParams = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
    ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
    ECCurve curve = spec.getCurve();
    EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
    this.ecPublicKey = new ECPublicKeyParameters(curve.decodePoint(x9Encoding), ECUtil.getDomainParameters(null, spec));
    this.ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) EllipticCurve(java.security.spec.EllipticCurve) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) IOException(java.io.IOException) ECPublicKeyParameters(com.github.zhenwei.core.crypto.params.ECPublicKeyParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

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