use of com.mindbright.asn1.ASN1Object in project jruby-openssl by jruby.
the class PEMInputOutput method readPrivateKey.
/**
* c: PEM_read_PrivateKey + PEM_read_bio_PrivateKey
* CAUTION: KeyPair#getPublic() may be null.
*/
public static KeyPair readPrivateKey(final Reader in, char[] passwd) throws PasswordRequiredException, IOException {
final String BEG_STRING_ECPRIVATEKEY = BEF_G + PEM_STRING_ECPRIVATEKEY;
final String BEG_STRING_PKCS8INF = BEF_G + PEM_STRING_PKCS8INF;
final String BEG_STRING_PKCS8 = BEF_G + PEM_STRING_PKCS8;
final BufferedReader reader = makeBuffered(in);
String line;
while ((line = reader.readLine()) != null) {
if (line.indexOf(BEG_STRING_RSA) != -1) {
try {
return readKeyPair(reader, passwd, "RSA", BEF_E + PEM_STRING_RSA);
} catch (Exception e) {
throw mapReadException("problem creating RSA private key: ", e);
}
} else if (line.indexOf(BEG_STRING_DSA) != -1) {
try {
return readKeyPair(reader, passwd, "DSA", BEF_E + PEM_STRING_DSA);
} catch (Exception e) {
throw mapReadException("problem creating DSA private key: ", e);
}
} else if (line.indexOf(BEG_STRING_ECPRIVATEKEY) != -1) {
try {
return readKeyPair(reader, passwd, "ECDSA", BEF_E + PEM_STRING_ECPRIVATEKEY);
} catch (Exception e) {
throw mapReadException("problem creating DSA private key: ", e);
}
} else if (line.indexOf(BEG_STRING_PKCS8INF) != -1) {
try {
byte[] bytes = readBase64Bytes(reader, BEF_E + PEM_STRING_PKCS8INF);
PrivateKeyInfo info = PrivateKeyInfo.getInstance(bytes);
String type = getPrivateKeyTypeFromObjectId(info.getPrivateKeyAlgorithm().getAlgorithm());
return org.jruby.ext.openssl.impl.PKey.readPrivateKey(((ASN1Object) info.parsePrivateKey()).getEncoded(ASN1Encoding.DER), type);
} catch (Exception e) {
throw mapReadException("problem creating private key: ", e);
}
} else if (line.indexOf(BEG_STRING_PKCS8) != -1) {
try {
byte[] bytes = readBase64Bytes(reader, BEF_E + PEM_STRING_PKCS8);
EncryptedPrivateKeyInfo eIn = EncryptedPrivateKeyInfo.getInstance(bytes);
AlgorithmIdentifier algId = eIn.getEncryptionAlgorithm();
PrivateKey privKey;
if (algId.getAlgorithm().toString().equals("1.2.840.113549.1.5.13")) {
// PBES2
privKey = derivePrivateKeyPBES2(eIn, algId, passwd);
} else {
privKey = derivePrivateKeyPBES1(eIn, algId, passwd);
}
return new KeyPair(null, privKey);
} catch (Exception e) {
throw mapReadException("problem creating private key: ", e);
}
}
}
return null;
}
use of com.mindbright.asn1.ASN1Object in project tbd-studio-se by Talend.
the class MongoDBConnectionUtil method buildSSLContext_PEM.
private static SSLContext buildSSLContext_PEM(String _keystore, String _keystorePass, String _truststore, String _truststorePass, SSLContext sslContext) {
SSLContext context = null;
final String pubCertEndDelimiter = "-----END CERTIFICATE-----";
final String privateKeyStartDelimiter = "-----BEGIN PRIVATE KEY-----";
final String privateKeyEndDelimiter = "-----END PRIVATE KEY-----";
final String privateKeyRSAStartDelimiter = "-----BEGIN RSA PRIVATE KEY-----";
final String privateKeyRSAEndDelimiter = "-----END RSA PRIVATE KEY-----";
try {
context = SSLContext.getInstance("TLS");
byte[] certAndKey = Files.readAllBytes(Paths.get(new File(_keystore).toURI()));
String[] tokens = new String(certAndKey).split(pubCertEndDelimiter);
byte[] certBytes = tokens[0].concat(pubCertEndDelimiter).getBytes();
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
java.security.cert.Certificate certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(certBytes));
//
X509Certificate cert = (X509Certificate) certificate;
PrivateKey key = null;
KeyFactory kf = KeyFactory.getInstance("RSA");
if (tokens[1].contains(privateKeyStartDelimiter)) {
byte[] privatekeyBytes = tokens[1].replace(privateKeyStartDelimiter, "").replace(privateKeyEndDelimiter, "").replace("\r\n", "").replace("\n", "").getBytes();
byte[] decode = Base64.getDecoder().decode(privatekeyBytes);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decode);
key = kf.generatePrivate(spec);
} else if (tokens[1].contains(privateKeyRSAStartDelimiter)) {
byte[] privateKeyBytes = tokens[1].replace(privateKeyRSAStartDelimiter, "").replace(privateKeyRSAEndDelimiter, "").replace("\r\n", "").replace("\n", "").getBytes("UTF-8");
byte[] decodeBytes = Base64.getDecoder().decode(privateKeyBytes);
ASN1Object fromByteArray = ASN1Sequence.fromByteArray(decodeBytes);
RSAPrivateKeyStructure asn1PrivKey = new RSAPrivateKeyStructure((ASN1Sequence) fromByteArray);
RSAPrivateKeySpec rsaPrivKeySpec = new RSAPrivateKeySpec(asn1PrivKey.getModulus(), asn1PrivKey.getPrivateExponent());
key = kf.generatePrivate(rsaPrivKeySpec);
}
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(null);
keystore.setCertificateEntry("cert-alias", cert);
keystore.setKeyEntry("key-alias", key, "changeit".toCharArray(), new Certificate[] { cert });
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keystore, "changeit".toCharArray());
KeyManager[] km = kmf.getKeyManagers();
context.init(km, null, null);
} catch (Exception e) {
e.printStackTrace();
}
return context;
}
use of com.mindbright.asn1.ASN1Object in project laverca by laverca.
the class CmsSignature method bytesToPkcs7SignedData.
/**
* Convert a byte array to a PKCS7 SignedData object
* @param bytes byte array
* @return PKCS7 SignedData object
*/
public static SignedData bytesToPkcs7SignedData(byte[] bytes) {
if (bytes == null) {
throw new IllegalArgumentException("null bytes");
}
ASN1InputStream ais = new ASN1InputStream(bytes);
ASN1Object asn1 = null;
try {
asn1 = ais.readObject();
} catch (IOException ioe) {
throw new IllegalArgumentException("not a pkcs7 signature");
} finally {
try {
ais.close();
} catch (IOException e) {
// Ignore
}
}
ContentInfo ci = ContentInfo.getInstance(asn1);
ASN1ObjectIdentifier typeId = ci.getContentType();
if (!typeId.equals(PKCSObjectIdentifiers.signedData)) {
throw new IllegalArgumentException("not a pkcs7 signature");
}
return SignedData.getInstance(ci.getContent());
}
use of com.mindbright.asn1.ASN1Object in project documentproduction by qld-gov-au.
the class CertInformationCollector method addTimestampCerts.
/**
* Processes an embedded signed timestamp, that has been placed into a signature. The
* certificates and its chain(s) will be processed the same way as the signature itself.
*
* @param signerInformation of the signature, to get unsigned attributes from it.
* @throws IOException
* @throws CertificateProccessingException
*/
private void addTimestampCerts(SignerInformation signerInformation) throws IOException, CertificateProccessingException {
AttributeTable unsignedAttributes = signerInformation.getUnsignedAttributes();
if (unsignedAttributes == null) {
return;
}
Attribute tsAttribute = unsignedAttributes.get(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken);
if (tsAttribute == null) {
return;
}
ASN1Encodable obj0 = tsAttribute.getAttrValues().getObjectAt(0);
if (!(obj0 instanceof ASN1Object)) {
return;
}
ASN1Object tsSeq = (ASN1Object) obj0;
try {
CMSSignedData signedData = new CMSSignedData(tsSeq.getEncoded("DER"));
rootCertInfo.setTsaCerts(new CertSignatureInformation());
processSignerStore(signedData, rootCertInfo.getTsaCerts());
} catch (CMSException e) {
throw new IOException("Error parsing timestamp token", e);
}
}
use of com.mindbright.asn1.ASN1Object in project OpenUnison by TremoloSecurity.
the class AndroidKeyStoreAttestation method getKeyDescriptionSequence.
private static ASN1Sequence getKeyDescriptionSequence(ASN1OctetString octet) throws CertificateParsingException {
// Read out the Sequence
ASN1Object asn1Object = X509ExtensionParsingUtil.getAsn1Object(octet.getOctets());
if (asn1Object == null || !(asn1Object instanceof ASN1Sequence)) {
throw new CertificateParsingException("Expected KeyDescription Sequence.");
}
ASN1Sequence sequence = (ASN1Sequence) asn1Object;
if (sequence.size() != DESCRIPTION_LENGTH) {
throw new CertificateParsingException("KeyDescription Sequence has " + sequence.size() + " elements. Expected " + DESCRIPTION_LENGTH + " elements ");
}
return sequence;
}
Aggregations