Search in sources :

Example 26 with RSAPrivateKey

use of org.bouncycastle.asn1.pkcs.RSAPrivateKey in project faf-java-server by FAForever.

the class RsaHelper method readPkcs1.

/**
 * Reads a specified PKCS#1 formatted key (without any headers or footers). Having the key in PKCS#8 format would be
 * easier as bouncy castle provides a one-liner to read it but since the original FAF server had its key in PKCS#1,
 * this method allows to just use the same key string instead of having to convert it.
 */
@SneakyThrows
RSAPrivateCrtKey readPkcs1(String content) {
    ASN1Sequence seq = ASN1Sequence.getInstance(Base64.getDecoder().decode(content.getBytes(StandardCharsets.UTF_8)));
    Assert.notNull(seq, "RSA private key has not been specified properly. Value is '" + content + "'.");
    Assert.isTrue(seq.size() == 9, "Invalid RSA Private Key ASN1 sequence.");
    RSAPrivateKey key = RSAPrivateKey.getInstance(seq);
    RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec(key.getModulus(), key.getPublicExponent(), key.getPrivateExponent(), key.getPrime1(), key.getPrime2(), key.getExponent1(), key.getExponent2(), key.getCoefficient());
    return (RSAPrivateCrtKey) KeyFactory.getInstance("RSA").generatePrivate(privSpec);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPrivateKey(org.bouncycastle.asn1.pkcs.RSAPrivateKey) SneakyThrows(lombok.SneakyThrows)

Aggregations

BigInteger (java.math.BigInteger)11 PrivateKey (java.security.PrivateKey)10 BufferedOutputStream (java.io.BufferedOutputStream)8 File (java.io.File)8 FileOutputStream (java.io.FileOutputStream)8 OutputStream (java.io.OutputStream)8 X509CRL (java.security.cert.X509CRL)8 DERIA5String (org.bouncycastle.asn1.DERIA5String)8 Test (org.testng.annotations.Test)8 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)7 Test (org.junit.Test)7 PublicKey (java.security.PublicKey)6 X509CRLEntry (java.security.cert.X509CRLEntry)6 Date (java.util.Date)6 HashSet (java.util.HashSet)6 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)5 DERSequence (org.bouncycastle.asn1.DERSequence)5 X509CRLHolder (org.bouncycastle.cert.X509CRLHolder)5 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)4 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)4