Search in sources :

Example 41 with PrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project fabric-sdk-java by hyperledger.

the class NetworkConfig method getPrivateKeyFromString.

private static PrivateKey getPrivateKeyFromString(String data) throws IOException {
    final Reader pemReader = new StringReader(data);
    final PrivateKeyInfo pemPair;
    try (PEMParser pemParser = new PEMParser(pemReader)) {
        pemPair = (PrivateKeyInfo) pemParser.readObject();
    }
    return new JcaPEMKeyConverter().getPrivateKey(pemPair);
}
Also used : PEMParser(org.bouncycastle.openssl.PEMParser) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) Reader(java.io.Reader) StringReader(java.io.StringReader) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 42 with PrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project vcert-java by Venafi.

the class PEMCollection method derPrivateKey.

public RawPrivateKey derPrivateKey() {
    if (Objects.isNull(this.privateKey)) {
        return null;
    }
    try {
        RawPrivateKey result = new RawPrivateKey();
        if (KeyType.from(this.privateKey.getAlgorithm()) == KeyType.RSA) {
            PrivateKeyInfo pkInfo = PrivateKeyInfo.getInstance(this.privateKey.getEncoded());
            ASN1Primitive privateKeyPKCS1ASN1 = pkInfo.parsePrivateKey().toASN1Primitive();
            result.data = privateKeyPKCS1ASN1.getEncoded();
        } else {
            result.data = this.privateKey.getEncoded();
        }
        if (privateKeyPassword == null) {
            return result;
        } else {
            result.iv = new byte[SECRET_KEY_LENGTH_BITS / 8];
            new SecureRandom().nextBytes(result.iv);
            SecretKeySpec secretKey = passwordToCipherSecretKey(privateKeyPassword.toCharArray(), result.iv);
            Cipher c = Cipher.getInstance(CIPHER_TRANSFORMATION);
            c.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(result.iv));
            result.data = c.doFinal(result.data);
            return result;
        }
    } catch (IOException | GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) SecureRandom(java.security.SecureRandom) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) PKCS8EncryptedPrivateKeyInfo(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo)

Example 43 with PrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project vcert-java by Venafi.

the class PEMCollection method decryptPKCS8PrivateKey.

public static PrivateKey decryptPKCS8PrivateKey(PKCS8EncryptedPrivateKeyInfo encryptedPrivateKeyInfo, String keyPassword) throws PEMException, OperatorCreationException, PKCSException {
    InputDecryptorProvider pkcs8Prov = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(keyPassword.toCharArray());
    JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
    PrivateKeyInfo decryptedPrivateKeyInfo = encryptedPrivateKeyInfo.decryptPrivateKeyInfo(pkcs8Prov);
    return converter.getPrivateKey(decryptedPrivateKeyInfo);
}
Also used : InputDecryptorProvider(org.bouncycastle.operator.InputDecryptorProvider) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) JceOpenSSLPKCS8DecryptorProviderBuilder(org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) PKCS8EncryptedPrivateKeyInfo(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo)

Example 44 with PrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project radixdlt by radixdlt.

the class RadixKeyStore method encodePrivKey.

/**
 * Encodes the specified raw secp256k1 private key into an ASN.1 encoded <a
 * href="https://tools.ietf.org/html/rfc5208#section-5">Private Key Info</a> structure.
 *
 * <p>Note that the encoded key will not include a public key.
 *
 * @param rawkey The raw secp256k1 private key.
 * @return The ASN.1 encoded private key.
 * @throws IOException If an error occurs encoding the ASN.1 key.
 */
private static byte[] encodePrivKey(byte[] rawkey) throws IOException {
    AlgorithmIdentifier ecSecp256k1 = new AlgorithmIdentifier(OID_EC_ENCRYPTION, OID_SECP256K1_CURVE);
    BigInteger key = new BigInteger(1, rawkey);
    ECPrivateKey privateKey = new ECPrivateKey(ECKeyPair.BYTES * Byte.SIZE, key, OID_SECP256K1_CURVE);
    PrivateKeyInfo pki = new PrivateKeyInfo(ecSecp256k1, privateKey);
    return pki.getEncoded();
}
Also used : ECPrivateKey(org.bouncycastle.asn1.sec.ECPrivateKey) BigInteger(java.math.BigInteger) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 45 with PrivateKeyInfo

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

the class EncryptedValueBuilder method build.

/**
 * Build an EncryptedValue structure containing the private key contained in the passed info
 * structure.
 *
 * @param privateKeyInfo a PKCS#8 private key info structure.
 * @return an EncryptedValue containing an EncryptedPrivateKeyInfo structure.
 * @throws CRMFException on a failure to encrypt the data, or wrap the symmetric key for this
 *                       value.
 */
public EncryptedValue build(PrivateKeyInfo privateKeyInfo) throws CRMFException {
    PKCS8EncryptedPrivateKeyInfoBuilder encInfoBldr = new PKCS8EncryptedPrivateKeyInfoBuilder(privateKeyInfo);
    AlgorithmIdentifier intendedAlg = privateKeyInfo.getPrivateKeyAlgorithm();
    AlgorithmIdentifier symmAlg = encryptor.getAlgorithmIdentifier();
    DERBitString encSymmKey;
    try {
        PKCS8EncryptedPrivateKeyInfo encInfo = encInfoBldr.build(encryptor);
        encSymmKey = new DERBitString(wrapper.generateWrappedKey(encryptor.getKey()));
        AlgorithmIdentifier keyAlg = wrapper.getAlgorithmIdentifier();
        ASN1OctetString valueHint = null;
        return new EncryptedValue(intendedAlg, symmAlg, encSymmKey, keyAlg, valueHint, new DERBitString(encInfo.getEncryptedData()));
    } catch (IllegalStateException e) {
        throw new CRMFException("cannot encode key: " + e.getMessage(), e);
    } catch (OperatorException e) {
        throw new CRMFException("cannot wrap key: " + e.getMessage(), e);
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) PKCS8EncryptedPrivateKeyInfoBuilder(com.github.zhenwei.pkix.pkcs.PKCS8EncryptedPrivateKeyInfoBuilder) DERBitString(com.github.zhenwei.core.asn1.DERBitString) PKCS8EncryptedPrivateKeyInfo(com.github.zhenwei.pkix.pkcs.PKCS8EncryptedPrivateKeyInfo) EncryptedValue(com.github.zhenwei.pkix.util.asn1.crmf.EncryptedValue) OperatorException(com.github.zhenwei.pkix.operator.OperatorException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)100 IOException (java.io.IOException)69 JcaPEMKeyConverter (org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter)53 PEMParser (org.bouncycastle.openssl.PEMParser)49 PrivateKey (java.security.PrivateKey)37 PEMKeyPair (org.bouncycastle.openssl.PEMKeyPair)35 PKCS8EncryptedPrivateKeyInfo (org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo)33 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)25 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)25 InputDecryptorProvider (org.bouncycastle.operator.InputDecryptorProvider)25 JceOpenSSLPKCS8DecryptorProviderBuilder (org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder)20 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)18 BigInteger (java.math.BigInteger)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)16 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)16 KeyPair (java.security.KeyPair)15 PEMEncryptedKeyPair (org.bouncycastle.openssl.PEMEncryptedKeyPair)15 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)14 InputStreamReader (java.io.InputStreamReader)14