use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project cas by apereo.
the class PrivateKeyFactoryBean method readPemPrivateKey.
private PrivateKey readPemPrivateKey() {
LOGGER.trace("Attempting to read as PEM [{}]", this.location);
try (val in = new InputStreamReader(this.location.getInputStream(), StandardCharsets.UTF_8);
val br = new BufferedReader(in);
val pp = new PEMParser(br)) {
val object = pp.readObject();
if (object instanceof PrivateKeyInfo) {
return new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) object);
}
if (object instanceof PEMKeyPair) {
val pemKeyPair = (PEMKeyPair) object;
val kp = new JcaPEMKeyConverter().getKeyPair(pemKeyPair);
return kp.getPrivate();
}
} catch (final Exception e) {
LOGGER.debug("Unable to read key", e);
}
return null;
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project keystore-explorer by kaikramer.
the class OpenSslPvkUtil method load.
/**
* Load an unencrypted OpenSSL private key from the stream. The encoding of
* the private key may be PEM or DER.
*
* @param pvkData Stream to load the unencrypted private key from
* @return The private key
* @throws PrivateKeyEncryptedException If private key is encrypted
* @throws CryptoException Problem encountered while loading the private key
* @throws IOException An I/O error occurred
*/
public static PrivateKey load(byte[] pvkData) throws CryptoException, IOException {
EncryptionType encType = getEncryptionType(pvkData);
if (encType == null) {
throw new CryptoException(res.getString("NotValidOpenSsl.exception.message"));
}
if (encType == ENCRYPTED) {
throw new PrivateKeyEncryptedException(res.getString("OpenSslIsEncrypted.exception.message"));
}
// Check if stream is PEM encoded
PemInfo pemInfo = PemUtil.decode(pvkData);
if (pemInfo != null) {
// It is - get DER from PEM
pvkData = pemInfo.getContent();
}
try (ASN1InputStream asn1InputStream = new ASN1InputStream(pvkData)) {
// Read OpenSSL DER structure
ASN1Primitive openSsl = asn1InputStream.readObject();
asn1InputStream.close();
if (openSsl instanceof ASN1Sequence) {
ASN1Sequence seq = (ASN1Sequence) openSsl;
if (seq.size() == 9) {
// RSA private key
BigInteger version = ((ASN1Integer) seq.getObjectAt(0)).getValue();
BigInteger modulus = ((ASN1Integer) seq.getObjectAt(1)).getValue();
BigInteger publicExponent = ((ASN1Integer) seq.getObjectAt(2)).getValue();
BigInteger privateExponent = ((ASN1Integer) seq.getObjectAt(3)).getValue();
BigInteger primeP = ((ASN1Integer) seq.getObjectAt(4)).getValue();
BigInteger primeQ = ((ASN1Integer) seq.getObjectAt(5)).getValue();
BigInteger primeExponentP = ((ASN1Integer) seq.getObjectAt(6)).getValue();
BigInteger primeExponenetQ = ((ASN1Integer) seq.getObjectAt(7)).getValue();
BigInteger crtCoefficient = ((ASN1Integer) seq.getObjectAt(8)).getValue();
if (!version.equals(VERSION)) {
throw new CryptoException(MessageFormat.format(res.getString("OpenSslVersionIncorrect.exception.message"), "" + VERSION.intValue(), "" + version.intValue()));
}
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponenetQ, crtCoefficient);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} else if (seq.size() == 6) {
// DSA private key
BigInteger version = ((ASN1Integer) seq.getObjectAt(0)).getValue();
BigInteger primeModulusP = ((ASN1Integer) seq.getObjectAt(1)).getValue();
BigInteger primeQ = ((ASN1Integer) seq.getObjectAt(2)).getValue();
BigInteger generatorG = ((ASN1Integer) seq.getObjectAt(3)).getValue();
// publicExponentY not req for pvk: sequence.getObjectAt(4);
BigInteger secretExponentX = ((ASN1Integer) seq.getObjectAt(5)).getValue();
if (!version.equals(VERSION)) {
throw new CryptoException(MessageFormat.format(res.getString("OpenSslVersionIncorrect.exception.message"), "" + VERSION.intValue(), "" + version.intValue()));
}
DSAPrivateKeySpec dsaPrivateKeySpec = new DSAPrivateKeySpec(secretExponentX, primeModulusP, primeQ, generatorG);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
return keyFactory.generatePrivate(dsaPrivateKeySpec);
} else if (seq.size() >= 2) {
// EC private key (RFC 5915)
org.bouncycastle.asn1.sec.ECPrivateKey pKey = org.bouncycastle.asn1.sec.ECPrivateKey.getInstance(seq);
AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, pKey.getParametersObject());
PrivateKeyInfo privInfo = new PrivateKeyInfo(algId, pKey);
return new JcaPEMKeyConverter().getPrivateKey(privInfo);
} else {
throw new CryptoException(MessageFormat.format(res.getString("OpenSslSequenceIncorrectSize.exception.message"), "" + seq.size()));
}
} else {
throw new CryptoException(res.getString("OpenSslSequenceNotFound.exception.message"));
}
} catch (Exception ex) {
throw new CryptoException(res.getString("NoLoadOpenSslPrivateKey.exception.message"), ex);
}
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project keystore-explorer by kaikramer.
the class EccUtil method convertToECPrivateKeyStructure.
/**
* Converts PKCS#8 EC private key (RFC 5208/5958 ASN.1 PrivateKeyInfo structure) to "traditional" OpenSSL
* ASN.1 structure ECPrivateKey from RFC 5915. As ECPrivateKey is already in the PrivateKey field of PrivateKeyInfo,
* this must only be extracted:
* <p>
* SEQUENCE {
* INTEGER 0
* SEQUENCE {
* OBJECT IDENTIFIER ecPublicKey (1 2 840 10045 2 1)
* OBJECT IDENTIFIER prime256v1 (1 2 840 10045 3 1 7)
* }
* OCTET STRING, encapsulates {
* SEQUENCE {
* INTEGER 1
* OCTET STRING
* 17 12 CA 42 16 79 1B 45 ...B.y.E
* ...
* C8 B2 66 0A E5 60 50 0B
* [0] {
* OBJECT IDENTIFIER prime256v1 (1 2 840 10045 3 1 7)
* }
* [1] {
* BIT STRING
* 04 61 C0 08 B4 89 A0 50 .a.....P
* ...
* AE D5 ED C3 4D 0E 47 91 ....M.G.
* 89 .
* }
* }
* }
* }
*
* @param ecPrivateKey An EC key
* @return Object holding ASN1 ECPrivateKey structure
* @throws IOException When ECPrivateKey structure in PrivateKeyInfo's PrivateKey field cannot be parsed
*/
public static org.bouncycastle.asn1.sec.ECPrivateKey convertToECPrivateKeyStructure(ECPrivateKey ecPrivateKey) throws IOException {
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(ecPrivateKey.getEncoded());
ASN1Encodable privateKey = privateKeyInfo.parsePrivateKey();
return org.bouncycastle.asn1.sec.ECPrivateKey.getInstance(privateKey);
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project keystore-explorer by kaikramer.
the class EccUtil method detectEdDSACurve.
/**
* Detect which one of the two EdDSA curves (Ed25519 or Ed448) the given privateKey is.
*
* @param privateKey An EdDSA private key
* @return Ed25519 or Ed448
* @throws InvalidParameterException if privateKey is not a EdDSA key
*/
public static EdDSACurves detectEdDSACurve(PrivateKey privateKey) {
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(privateKey.getEncoded());
AlgorithmIdentifier algorithm = privateKeyInfo.getPrivateKeyAlgorithm();
ASN1ObjectIdentifier algOid = algorithm.getAlgorithm();
return EdDSACurves.resolve(algOid);
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project incubator-pulsar by apache.
the class MessageCryptoBc method loadPrivateKey.
private PrivateKey loadPrivateKey(byte[] keyBytes) throws Exception {
Reader keyReader = new StringReader(new String(keyBytes));
PrivateKey privateKey = null;
try (PEMParser pemReader = new PEMParser(keyReader)) {
X9ECParameters ecParam = null;
Object pemObj = pemReader.readObject();
if (pemObj instanceof ASN1ObjectIdentifier) {
// make sure this is EC Parameter we're handling. In which case
// we'll store it and read the next object which should be our
// EC Private Key
ASN1ObjectIdentifier ecOID = (ASN1ObjectIdentifier) pemObj;
ecParam = ECNamedCurveTable.getByOID(ecOID);
if (ecParam == null) {
throw new PEMException("Unable to find EC Parameter for the given curve oid: " + ecOID.getId());
}
pemObj = pemReader.readObject();
} else if (pemObj instanceof X9ECParameters) {
ecParam = (X9ECParameters) pemObj;
pemObj = pemReader.readObject();
}
if (pemObj instanceof PEMKeyPair) {
PrivateKeyInfo pKeyInfo = ((PEMKeyPair) pemObj).getPrivateKeyInfo();
JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
privateKey = pemConverter.getPrivateKey(pKeyInfo);
}
if (ecParam != null && ECDSA.equals(privateKey.getAlgorithm())) {
ECParameterSpec ecSpec = new ECParameterSpec(ecParam.getCurve(), ecParam.getG(), ecParam.getN(), ecParam.getH(), ecParam.getSeed());
KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BouncyCastleProvider.PROVIDER_NAME);
ECPrivateKeySpec keySpec = new ECPrivateKeySpec(((BCECPrivateKey) privateKey).getS(), ecSpec);
privateKey = keyFactory.generatePrivate(keySpec);
}
} catch (IOException e) {
throw new Exception(e);
}
return privateKey;
}
Aggregations