SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(PublicKeyFactory.createKey(keyPair.getPublic() .getEncoded())); X509ExtensionUtils extensionUtils = new BcX509ExtensionUtils();
/** * Create a public key from the passed in SubjectPublicKeyInfo * * @param keyInfo the SubjectPublicKeyInfo containing the key data * @return the appropriate key parameter * @throws IOException on an error decoding the key */ public static AsymmetricKeyParameter createKey(SubjectPublicKeyInfo keyInfo) throws IOException { return createKey(keyInfo, null); }
/** * Convenience method to obtain the public portion of the key. * * @return an AsymmetricKeyParameter that can be used for verification. */ public AsymmetricKeyParameter getPublic() throws IOException { if (this.kpr == null) return null; return PublicKeyFactory.createKey(this.kpr.getPublicKeyInfo()); }
@Override public PublicKeyParameters fromKey(PublicKey publicKey) { try { BcPublicKeyParameters key = new BcPublicKeyParameters(PublicKeyFactory.createKey(publicKey.getEncoded())); String keyType = checkKeyType(key); if (keyType != null) { throw new IllegalArgumentException(String.format(ALGORITHM_ERROR, keyType, PUBLIC, publicKey.getAlgorithm())); } return key; } catch (IOException e) { throw new IllegalArgumentException("Invalid public key: " + publicKey.getClass().getName()); } }
@Override public PublicKeyParameters fromKey(PublicKey publicKey) { try { BcPublicKeyParameters key = new BcPublicKeyParameters(PublicKeyFactory.createKey(publicKey.getEncoded())); String keyType = checkKeyType(key); if (keyType != null) { throw new IllegalArgumentException(String.format(ALGORITHM_ERROR, keyType, PUBLIC, publicKey.getAlgorithm())); } return key; } catch (IOException e) { throw new IllegalArgumentException("Invalid public key: " + publicKey.getClass().getName()); } }
@Override public PublicKeyParameters fromX509(byte[] encoded) throws IOException { BcPublicKeyParameters key = new BcPublicKeyParameters(PublicKeyFactory.createKey(encoded)); String keyType = checkKeyType(key); if (keyType != null) { throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PUBLIC, key.getParameters().getClass().getName())); } return key; }
@Override public PublicKeyParameters getPublicKeyParameters() { try { return new BcPublicKeyParameters(PublicKeyFactory.createKey(this.holder.getSubjectPublicKeyInfo())); } catch (IOException e) { // Very unlikely throw new UnsupportedOperationException("Unsupported public key encoding.", e); } }
@Override public PublicKeyParameters fromX509(byte[] encoded) throws IOException { BcPublicKeyParameters key = new BcPublicKeyParameters(PublicKeyFactory.createKey(encoded)); String keyType = checkKeyType(key); if (keyType != null) { throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PUBLIC, key.getParameters().getClass().getName())); } return key; }
/** * Create a public key from a SubjectPublicKeyInfo encoding * * @param keyInfoData the SubjectPublicKeyInfo encoding * @return the appropriate key parameter * @throws IOException on an error decoding the key */ public static AsymmetricKeyParameter createKey(byte[] keyInfoData) throws IOException { return createKey(SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(keyInfoData))); }
/** * Create a public key from a SubjectPublicKeyInfo encoding * * @param keyInfoData the SubjectPublicKeyInfo encoding * @return the appropriate key parameter * @throws IOException on an error decoding the key */ public static AsymmetricKeyParameter createKey(byte[] keyInfoData) throws IOException { return createKey(SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(keyInfoData))); }
/** * Convert public key parameters to asymmetric key parameter. * * @param publicKey the public key parameter to convert. * @return an asymmetric key parameter. */ public static AsymmetricKeyParameter getAsymmetricKeyParameter(PublicKeyParameters publicKey) { if (publicKey instanceof BcAsymmetricKeyParameters) { return ((BcAsymmetricKeyParameters) publicKey).getParameters(); } else { try { return PublicKeyFactory.createKey(publicKey.getEncoded()); } catch (IOException e) { // Very unlikely throw new IllegalArgumentException("Invalid public key, unable to encode."); } } }
/** * Create a public key from a SubjectPublicKeyInfo encoding read from a stream * * @param inStr the stream to read the SubjectPublicKeyInfo encoding from * @return the appropriate key parameter * @throws IOException on an error decoding the key */ public static AsymmetricKeyParameter createKey(InputStream inStr) throws IOException { return createKey(SubjectPublicKeyInfo.getInstance(new ASN1InputStream(inStr).readObject())); }
/** * Create a public key from a SubjectPublicKeyInfo encoding read from a stream * * @param inStr the stream to read the SubjectPublicKeyInfo encoding from * @return the appropriate key parameter * @throws IOException on an error decoding the key */ public static AsymmetricKeyParameter createKey(InputStream inStr) throws IOException { return createKey(SubjectPublicKeyInfo.getInstance(new ASN1InputStream(inStr).readObject())); }
@Override public AsymmetricKeyParameter decode(final byte[] encoded, final Object... args) { try { if (PemUtil.isPem(encoded)) { return PublicKeyFactory.createKey(PemUtil.decode(encoded)); } return PublicKeyFactory.createKey(new ASN1InputStream(encoded).readObject().getEncoded()); } catch (IOException e) { throw new EncodingException("ASN.1 decoding error", e); } } }
public static DHPublicKeyParameters extractDHPublicKeyParameters(Certificate cert) throws IOException { if (hasDHParameters(cert)) { if (cert.isEmpty()) { return null; } SubjectPublicKeyInfo keyInfo = cert.getCertificateAt(0).getSubjectPublicKeyInfo(); return (DHPublicKeyParameters) PublicKeyFactory.createKey(keyInfo); } else { return null; } }
public static ECPublicKeyParameters extractECPublicKeyParameters(Certificate cert) throws IOException { if (hasECParameters(cert)) { if (cert.isEmpty()) { return null; } SubjectPublicKeyInfo keyInfo = cert.getCertificateAt(0).getSubjectPublicKeyInfo(); if (keyInfo == null) { return null; } return (ECPublicKeyParameters) PublicKeyFactory.createKey(keyInfo); } else { return null; } }
private PKCS10CertificationRequest generateCertificationRequest(String dn, KeyPair kp) throws Exception{ X500Name subject=new X500Name(dn); PublicKey pubKey=kp.getPublic(); PrivateKey privKey=kp.getPrivate(); AsymmetricKeyParameter pubkeyParam = PublicKeyFactory.createKey(pubKey.getEncoded()); SubjectPublicKeyInfo publicKeyInfo=SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(pubkeyParam); PKCS10CertificationRequestBuilder builder=new PKCS10CertificationRequestBuilder(subject, publicKeyInfo); AlgorithmIdentifier signatureAi = new AlgorithmIdentifier(OIWObjectIdentifiers.sha1WithRSA); BcRSAContentSignerBuilder signerBuilder=new BcRSAContentSignerBuilder( signatureAi, AlgorithmIdentifier.getInstance(OIWObjectIdentifiers.idSHA1)); AsymmetricKeyParameter pkParam = PrivateKeyFactory.createKey(privKey.getEncoded()); ContentSigner signer=signerBuilder.build(pkParam); return builder.build(signer); } }
private PSSSigner pssSha384(boolean forSigning, byte[] key) { try { byte[] salt = new byte[SHA384_OUT_LEN]; new SecureRandom().nextBytes(salt); // RSA-PSS, SHA-384, MGF1(SHA-384), 48 byte salt length, 0xBC trailer PSSSigner pss = new PSSSigner(new RSABlindedEngine(), new SHA384Digest(), new SHA384Digest(), SHA384_OUT_LEN, (byte)0xBC); if (forSigning) { pss.init(true, PrivateKeyFactory.createKey(key)); } else { pss.init(false, PublicKeyFactory.createKey(key)); } return pss; } catch (IOException e) { throw new CryptoProviderException("IOException", e); } }
public void processServerCertificate(Certificate serverCertificate) throws IOException { if (serverCertificate.isEmpty()) { throw new TlsFatalAlert(AlertDescription.bad_certificate); } org.bouncycastle.asn1.x509.Certificate x509Cert = serverCertificate.getCertificateAt(0); SubjectPublicKeyInfo keyInfo = x509Cert.getSubjectPublicKeyInfo(); try { this.serverPublicKey = PublicKeyFactory.createKey(keyInfo); } catch (RuntimeException e) { throw new TlsFatalAlert(AlertDescription.unsupported_certificate, e); } // Sanity check the PublicKeyFactory if (this.serverPublicKey.isPrivate()) { throw new TlsFatalAlert(AlertDescription.internal_error); } this.rsaServerPublicKey = validateRSAPublicKey((RSAKeyParameters)this.serverPublicKey); TlsUtils.validateKeyUsage(x509Cert, KeyUsage.keyEncipherment); super.processServerCertificate(serverCertificate); }
public void processServerCertificate(Certificate serverCertificate) throws IOException { if (tlsSigner == null) { throw new TlsFatalAlert(AlertDescription.unexpected_message); } if (serverCertificate.isEmpty()) { throw new TlsFatalAlert(AlertDescription.bad_certificate); } org.bouncycastle.asn1.x509.Certificate x509Cert = serverCertificate.getCertificateAt(0); SubjectPublicKeyInfo keyInfo = x509Cert.getSubjectPublicKeyInfo(); try { this.serverPublicKey = PublicKeyFactory.createKey(keyInfo); } catch (RuntimeException e) { throw new TlsFatalAlert(AlertDescription.unsupported_certificate, e); } if (!tlsSigner.isValidPublicKey(this.serverPublicKey)) { throw new TlsFatalAlert(AlertDescription.certificate_unknown); } TlsUtils.validateKeyUsage(x509Cert, KeyUsage.digitalSignature); super.processServerCertificate(serverCertificate); }