private ByteString pkcs1Bytes() { try { PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(keyPair.getPrivate().getEncoded()); return ByteString.of(privateKeyInfo.parsePrivateKey().toASN1Primitive().getEncoded()); } catch (IOException e) { throw new AssertionError(e); } }
/** * Returns a {@link PEMKeyPair} object with direct access to the public and private keys given a PKCS #8 private key. * * @param privateKeyInfo the PKCS #8 private key info * @return the PKCS #1 public and private key pair * @throws IOException if there is an error converting the key pair */ private static PEMKeyPair convertPrivateKeyFromPKCS8ToPKCS1(PrivateKeyInfo privateKeyInfo) throws IOException { // Parse the key wrapping to determine the internal key structure ASN1Encodable asn1PrivateKey = privateKeyInfo.parsePrivateKey(); // Convert the parsed key to an RSA private key RSAPrivateKey keyStruct = RSAPrivateKey.getInstance(asn1PrivateKey); // Create the RSA public key from the modulus and exponent RSAPublicKey pubSpec = new RSAPublicKey( keyStruct.getModulus(), keyStruct.getPublicExponent()); // Create an algorithm identifier for forming the key pair AlgorithmIdentifier algId = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE); if (isVerbose()) { logger.info("Converted private key from PKCS #8 to PKCS #1 RSA private key"); } // Create the key pair container return new PEMKeyPair(new SubjectPublicKeyInfo(algId, pubSpec), new PrivateKeyInfo(algId, keyStruct)); }
private static PrivateKey generatePrivateKey(final PrivateKeyInfo privateKeyInfo) throws IOException, InvalidKeySpecException { final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded()); return tryGeneratePrivateKey(spec, PRIVATE_KEY_ALGS); }
throw new IllegalArgumentException("Invalid RSA Private Key ASN1 sequence."); org.bouncycastle.asn1.pkcs.RSAPrivateKey key = org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec( key.getModulus(), key.getPublicExponent(), key.getPrivateExponent(), key.getPrime1(), key.getPrime2(), key.getExponent1(), key.getExponent2(), key.getCoefficient() ); publicKey = fact.generatePublic(pubSpec); } else if (type.equals("RSA PUBLIC KEY")) { ASN1Sequence seq = ASN1Sequence.getInstance(content); org.bouncycastle.asn1.pkcs.RSAPublicKey key = org.bouncycastle.asn1.pkcs.RSAPublicKey.getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); publicKey = fact.generatePublic(pubSpec); } else {
/** * construct an RSA key from a ASN.1 RSA private key object. */ BCRSAPrivateCrtKey( RSAPrivateKey key) { this.modulus = key.getModulus(); this.publicExponent = key.getPublicExponent(); this.privateExponent = key.getPrivateExponent(); this.primeP = key.getPrime1(); this.primeQ = key.getPrime2(); this.primeExponentP = key.getExponent1(); this.primeExponentQ = key.getExponent2(); this.crtCoefficient = key.getCoefficient(); }
public static X509Certificate generateSelfsignedCert(CertificationRequest csr, PrivateKey identityKey) throws CertificateException { requireNonNull("csr", csr); return generateSelfsignedCert(csr.getCertificationRequestInfo().getSubject(), csr.getCertificationRequestInfo().getSubjectPublicKeyInfo(), identityKey); }
/** * construct an RSA key from a private key info object. */ BCRSAPrivateCrtKey( PrivateKeyInfo info) throws IOException { this(RSAPrivateKey.getInstance(info.parsePrivateKey())); }
BCRSAPrivateKey(org.bouncycastle.asn1.pkcs.RSAPrivateKey key) { this.modulus = key.getModulus(); this.privateExponent = key.getPrivateExponent(); }
continue; if (attribute.getAttrType().equals(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest)) { final Extensions extensions = Extensions.getInstance(attribute.getAttrValues().getObjectAt(0)); final GeneralNames gns = GeneralNames.fromExtensions(extensions, Extension.subjectAlternativeName); if (gns != null && gns.getNames() != null && gns.getNames().length > 0) {
/** * Extract extensions from CSR object */ public static Extensions getExtensionsFromCSR(JcaPKCS10CertificationRequest csr) { Attribute[] attributess = csr.getAttributes(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest); for (Attribute attribute : attributess) { ASN1Set attValue = attribute.getAttrValues(); if (attValue != null) { ASN1Encodable extension = attValue.getObjectAt(0); if (extension instanceof Extensions) { return (Extensions) extension; } else if (extension instanceof DERSequence) { return Extensions.getInstance(extension); } } } return null; }
public AuthenticatedSafe( ContentInfo[] info) { this.info = copy(info); }
public ASN1Primitive toASN1Primitive() { if (oid != null) { return oid; } return attribute.toASN1Primitive(); } }
throw new IllegalArgumentException("Invalid RSA Private Key ASN1 sequence."); org.bouncycastle.asn1.pkcs.RSAPrivateKey key = org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec( key.getModulus(), key.getPublicExponent(), key.getPrivateExponent(), key.getPrime1(), key.getPrime2(), key.getExponent1(), key.getExponent2(), key.getCoefficient() ); publicKey = fact.generatePublic(pubSpec); } else if (type.equals("RSA PUBLIC KEY")) { ASN1Sequence seq = ASN1Sequence.getInstance(content); org.bouncycastle.asn1.pkcs.RSAPublicKey key = org.bouncycastle.asn1.pkcs.RSAPublicKey.getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); publicKey = fact.generatePublic(pubSpec); } else {
/** * construct an RSA key from a ASN.1 RSA private key object. */ JCERSAPrivateCrtKey( RSAPrivateKey key) { this.modulus = key.getModulus(); this.publicExponent = key.getPublicExponent(); this.privateExponent = key.getPrivateExponent(); this.primeP = key.getPrime1(); this.primeQ = key.getPrime2(); this.primeExponentP = key.getExponent1(); this.primeExponentQ = key.getExponent2(); this.crtCoefficient = key.getCoefficient(); }
public static byte[] signData(byte[] data) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // This is more or less code that has been just modified long enough that it works // Don't take it as good example how to get a Privatekey StringReader keyreader = new StringReader(SimpleSigner.certchain[0] + SimpleSigner.pemkey); PEMParser pemparser = new PEMParser(keyreader); X509CertificateHolder cert = (X509CertificateHolder) pemparser.readObject(); PrivateKeyInfo keyInfo = (PrivateKeyInfo) pemparser.readObject(); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyInfo.getEncoded()); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey key = kf.generatePrivate(keySpec); // The actual signing Cipher signer; signer = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); signer.init(Cipher.ENCRYPT_MODE, key); byte[] signed_bytes = signer.doFinal(data); return signed_bytes; } }
public Certificate generateCert(CertificationRequest csr) throws Exception { if (!verifyPopo(csr)) { throw new Exception("CSR invalid"); } CertificationRequestInfo reqInfo = csr.getCertificationRequestInfo(); return generateCert(reqInfo.getSubjectPublicKeyInfo(), reqInfo.getSubject()); }
/** * construct an RSA key from a private key info object. */ JCERSAPrivateCrtKey( PrivateKeyInfo info) throws IOException { this(org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(info.parsePrivateKey())); }
public ContentInfo[] getContentInfo() { return copy(info); }
public static byte[] getEncodedPrivateKeyInfo(PrivateKeyInfo info) { try { return info.getEncoded(ASN1Encoding.DER); } catch (Exception e) { return null; } } }
public static byte[] getEncodedPrivateKeyInfo(PrivateKeyInfo info) { try { return info.getEncoded(ASN1Encoding.DER); } catch (Exception e) { return null; } } }