Search in sources :

Example 81 with PrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.

the class EncKeyWithID method toASN1Primitive.

/**
 * <pre>
 * EncKeyWithID ::= SEQUENCE {
 *      privateKey           PrivateKeyInfo,
 *      identifier CHOICE {
 *         string               UTF8String,
 *         generalName          GeneralName
 *     } OPTIONAL
 * }
 * </pre>
 *
 * @return an ASN.1 primitive composition of this EncKeyWithID.
 */
public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector v = new ASN1EncodableVector(2);
    v.add(privKeyInfo);
    if (identifier != null) {
        v.add(identifier);
    }
    return new DERSequence(v);
}
Also used : DERSequence(com.github.zhenwei.core.asn1.DERSequence) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector)

Example 82 with PrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.

the class BCECPrivateKey method populateFromPrivKeyInfo.

private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException {
    X962Parameters params = X962Parameters.getInstance(info.getPrivateKeyAlgorithm().getParameters());
    ECCurve curve = EC5Util.getCurve(configuration, params);
    ecSpec = EC5Util.convertToSpec(params, curve);
    ASN1Encodable privKey = info.parsePrivateKey();
    if (privKey instanceof ASN1Integer) {
        ASN1Integer derD = ASN1Integer.getInstance(privKey);
        this.d = derD.getValue();
    } else {
        com.github.zhenwei.core.asn1.sec.ECPrivateKey ec = com.github.zhenwei.core.asn1.sec.ECPrivateKey.getInstance(privKey);
        this.d = ec.getKey();
        this.publicKey = ec.getPublicKey();
    }
}
Also used : X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer)

Example 83 with PrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.

the class BCDSTU4145PrivateKey method getEncoded.

/**
 * Return a PKCS8 representation of the key. The sequence returned represents a full
 * PrivateKeyInfo object.
 *
 * @return a PKCS8 representation of the key.
 */
public byte[] getEncoded() {
    X962Parameters params;
    int orderBitLength;
    if (ecSpec instanceof ECNamedCurveSpec) {
        ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveSpec) ecSpec).getName());
        if (// guess it's the OID
        curveOid == null) {
            curveOid = new ASN1ObjectIdentifier(((ECNamedCurveSpec) ecSpec).getName());
        }
        params = new X962Parameters(curveOid);
        orderBitLength = ECUtil.getOrderBitLength(WeGooProvider.CONFIGURATION, ecSpec.getOrder(), this.getS());
    } else if (ecSpec == null) {
        params = new X962Parameters(DERNull.INSTANCE);
        orderBitLength = ECUtil.getOrderBitLength(WeGooProvider.CONFIGURATION, null, this.getS());
    } else {
        ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
        X9ECParameters ecP = new X9ECParameters(curve, new X9ECPoint(EC5Util.convertPoint(curve, ecSpec.getGenerator()), withCompression), ecSpec.getOrder(), BigInteger.valueOf(ecSpec.getCofactor()), ecSpec.getCurve().getSeed());
        params = new X962Parameters(ecP);
        orderBitLength = ECUtil.getOrderBitLength(WeGooProvider.CONFIGURATION, ecSpec.getOrder(), this.getS());
    }
    PrivateKeyInfo info;
    com.github.zhenwei.core.asn1.sec.ECPrivateKey keyStructure;
    if (publicKey != null) {
        keyStructure = new com.github.zhenwei.core.asn1.sec.ECPrivateKey(orderBitLength, this.getS(), publicKey, params);
    } else {
        keyStructure = new com.github.zhenwei.core.asn1.sec.ECPrivateKey(orderBitLength, this.getS(), params);
    }
    try {
        if (algorithm.equals("DSTU4145")) {
            info = new PrivateKeyInfo(new AlgorithmIdentifier(UAObjectIdentifiers.dstu4145be, params.toASN1Primitive()), keyStructure.toASN1Primitive());
        } else {
            info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params.toASN1Primitive()), keyStructure.toASN1Primitive());
        }
        return info.getEncoded(ASN1Encoding.DER);
    } catch (IOException e) {
        return null;
    }
}
Also used : X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) IOException(java.io.IOException) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 84 with PrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.

the class BcFKSKeyStoreSpi method engineGetKey.

public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
    ObjectData ent = (ObjectData) entries.get(alias);
    if (ent != null) {
        if (ent.getType().equals(PRIVATE_KEY) || ent.getType().equals(PROTECTED_PRIVATE_KEY)) {
            PrivateKey cachedKey = (PrivateKey) privateKeyCache.get(alias);
            if (cachedKey != null) {
                return cachedKey;
            }
            EncryptedPrivateKeyData encPrivData = EncryptedPrivateKeyData.getInstance(ent.getData());
            EncryptedPrivateKeyInfo encInfo = EncryptedPrivateKeyInfo.getInstance(encPrivData.getEncryptedPrivateKeyInfo());
            try {
                PrivateKeyInfo pInfo = PrivateKeyInfo.getInstance(decryptData("PRIVATE_KEY_ENCRYPTION", encInfo.getEncryptionAlgorithm(), password, encInfo.getEncryptedData()));
                KeyFactory kFact = helper.createKeyFactory(getPublicKeyAlg(pInfo.getPrivateKeyAlgorithm().getAlgorithm()));
                PrivateKey privateKey = kFact.generatePrivate(new PKCS8EncodedKeySpec(pInfo.getEncoded()));
                // check that the key pair and the certificate public key are consistent
                // TODO: new ConsistentKeyPair(engineGetCertificate(alias).getPublicKey(), privateKey);
                privateKeyCache.put(alias, privateKey);
                return privateKey;
            } catch (Exception e) {
                throw new UnrecoverableKeyException("BCFKS KeyStore unable to recover private key (" + alias + "): " + e.getMessage());
            }
        } else if (ent.getType().equals(SECRET_KEY) || ent.getType().equals(PROTECTED_SECRET_KEY)) {
            EncryptedSecretKeyData encKeyData = EncryptedSecretKeyData.getInstance(ent.getData());
            try {
                SecretKeyData keyData = SecretKeyData.getInstance(decryptData("SECRET_KEY_ENCRYPTION", encKeyData.getKeyEncryptionAlgorithm(), password, encKeyData.getEncryptedKeyData()));
                SecretKeyFactory kFact = helper.createSecretKeyFactory(keyData.getKeyAlgorithm().getId());
                return kFact.generateSecret(new SecretKeySpec(keyData.getKeyBytes(), keyData.getKeyAlgorithm().getId()));
            } catch (Exception e) {
                throw new UnrecoverableKeyException("BCFKS KeyStore unable to recover secret key (" + alias + "): " + e.getMessage());
            }
        } else {
            throw new UnrecoverableKeyException("BCFKS KeyStore unable to recover secret key (" + alias + "): type not recognized");
        }
    }
    return null;
}
Also used : PrivateKey(java.security.PrivateKey) ObjectData(com.github.zhenwei.core.asn1.bc.ObjectData) SecretKeyData(com.github.zhenwei.core.asn1.bc.SecretKeyData) EncryptedSecretKeyData(com.github.zhenwei.core.asn1.bc.EncryptedSecretKeyData) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) ParseException(java.text.ParseException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchProviderException(java.security.NoSuchProviderException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) EncryptedSecretKeyData(com.github.zhenwei.core.asn1.bc.EncryptedSecretKeyData) SecretKeySpec(javax.crypto.spec.SecretKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo) SecretKeyFactory(javax.crypto.SecretKeyFactory) EncryptedPrivateKeyData(com.github.zhenwei.core.asn1.bc.EncryptedPrivateKeyData) EncryptedPrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 85 with PrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.

the class WeGooKeyProtector method recover.

public Key recover(EncryptedPrivateKeyInfo var1) throws UnrecoverableKeyException {
    AlgorithmId var7 = var1.getAlgorithm();
    if (!var7.getOID().toString().equals("1.3.6.1.4.1.42.2.17.1.1")) {
        throw new UnrecoverableKeyException("Unsupported key protection algorithm");
    } else {
        byte[] var8 = var1.getEncryptedData();
        byte[] var9 = new byte[20];
        System.arraycopy(var8, 0, var9, 0, 20);
        int var6 = var8.length - 20 - 20;
        int var4 = var6 / 20;
        if (var6 % 20 != 0) {
            ++var4;
        }
        byte[] var10 = new byte[var6];
        System.arraycopy(var8, 20, var10, 0, var6);
        byte[] var11 = new byte[var10.length];
        int var2 = 0;
        int var5 = 0;
        byte[] var3;
        for (var3 = var9; var2 < var4; var5 += 20) {
            this.md.update(this.passwdBytes);
            this.md.update(var3);
            var3 = this.md.digest();
            this.md.reset();
            if (var2 < var4 - 1) {
                System.arraycopy(var3, 0, var11, var5, var3.length);
            } else {
                System.arraycopy(var3, 0, var11, var5, var11.length - var5);
            }
            ++var2;
        }
        byte[] var12 = new byte[var10.length];
        for (var2 = 0; var2 < var12.length; ++var2) {
            var12[var2] = (byte) (var10[var2] ^ var11[var2]);
        }
        this.md.update(this.passwdBytes);
        Arrays.fill(this.passwdBytes, (byte) 0);
        this.passwdBytes = null;
        this.md.update(var12);
        var3 = this.md.digest();
        this.md.reset();
        for (var2 = 0; var2 < var3.length; ++var2) {
            if (var3[var2] != var8[20 + var6 + var2]) {
                throw new UnrecoverableKeyException("Cannot recover key");
            }
        }
        try {
            // return PKCS8Key.parseKey(new DerValue(var12));
            PrivateKeyInfo info = PrivateKeyInfo.getInstance(var12);
            if (info == null) {
                throw new UnrecoverableKeyException("Recover key can not null");
            }
            KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
            PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(var12);
            KeyFactory factory = KeyFactory.getInstance(algEnum.getAlg(), new WeGooProvider());
            return factory.generatePrivate(spec);
        } catch (Exception var14) {
            throw new UnrecoverableKeyException(var14.getMessage());
        }
    }
}
Also used : AlgorithmId(sun.security.x509.AlgorithmId) KeyPairAlgEnum(com.github.zhenwei.core.enums.KeyPairAlgEnum) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) WeGooProvider(com.github.zhenwei.provider.jce.provider.WeGooProvider) EncryptedPrivateKeyInfo(sun.security.pkcs.EncryptedPrivateKeyInfo) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) IOException(java.io.IOException)

Aggregations

PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)138 IOException (java.io.IOException)95 JcaPEMKeyConverter (org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter)66 PEMParser (org.bouncycastle.openssl.PEMParser)62 PrivateKey (java.security.PrivateKey)48 PEMKeyPair (org.bouncycastle.openssl.PEMKeyPair)41 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)35 PKCS8EncryptedPrivateKeyInfo (org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo)35 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)33 InputDecryptorProvider (org.bouncycastle.operator.InputDecryptorProvider)27 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)26 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)26 KeyFactory (java.security.KeyFactory)23 JceOpenSSLPKCS8DecryptorProviderBuilder (org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder)21 StringReader (java.io.StringReader)20 BigInteger (java.math.BigInteger)20 KeyPair (java.security.KeyPair)20 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)18 ByteArrayInputStream (java.io.ByteArrayInputStream)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18