AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId); AsymmetricKeyParameter privateKeyAsymKeyParam = PrivateKeyFactory.createKey(keyPair.getPrivate().getEncoded()); SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded());
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(PublicKeyFactory.createKey(keyPair.getPublic() .getEncoded())); X509ExtensionUtils extensionUtils = new BcX509ExtensionUtils();
/** * Parse a public key. * <p> * This method accepts the bytes that are Base64 encoded in an OpenSSH public key file. * * @param encoded The key. * @return An AsymmetricKeyParameter instance. */ public static AsymmetricKeyParameter parsePublicKey(byte[] encoded) { SSHBuffer buffer = new SSHBuffer(encoded); return parsePublicKey(buffer); }
public static byte[] intToBigEndian(int n) { byte[] bs = new byte[4]; intToBigEndian(n, bs, 0); return bs; }
public static void bigEndianToLong(byte[] bs, int off, long[] ns) { for (int i = 0; i < ns.length; ++i) { ns[i] = bigEndianToLong(bs, off); off += 8; } }
public static void littleEndianToInt(byte[] bs, int bOff, int[] ns, int nOff, int count) { for (int i = 0; i < count; ++i) { ns[nOff + i] = littleEndianToInt(bs, bOff); bOff += 4; } }
public static void longToLittleEndian(long n, byte[] bs, int off) { intToLittleEndian((int)(n & 0xffffffffL), bs, off); intToLittleEndian((int)(n >>> 32), bs, off + 4); }
public static byte[] longToLittleEndian(long n) { byte[] bs = new byte[8]; longToLittleEndian(n, bs, 0); return bs; }
public static void bigEndianToInt(byte[] bs, int off, int[] ns) { for (int i = 0; i < ns.length; ++i) { ns[i] = bigEndianToInt(bs, off); off += 4; } }
public static void longToBigEndian(long[] ns, byte[] bs, int off) { for (int i = 0; i < ns.length; ++i) { longToBigEndian(ns[i], bs, off); off += 8; } }
public static void littleEndianToLong(byte[] bs, int off, long[] ns) { for (int i = 0; i < ns.length; ++i) { ns[i] = littleEndianToLong(bs, off); off += 8; } }
/** * Return the transcript so far, * * @return a copy of the randomness produced so far. */ public byte[] getTranscript() { return tOut.toByteArray(); }
public X509Certificate generate(String dn, KeyPair keyPair) throws CertificateException { try { Security.addProvider(new BouncyCastleProvider()); AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(algorithm); AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId); AsymmetricKeyParameter privateKeyAsymKeyParam = PrivateKeyFactory.createKey(keyPair.getPrivate().getEncoded()); SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()); ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(privateKeyAsymKeyParam); X500Name name = new X500Name(dn); Date from = new Date(); Date to = new Date(from.getTime() + days * 86400000L); BigInteger sn = new BigInteger(64, new SecureRandom()); X509v3CertificateBuilder v3CertGen = new X509v3CertificateBuilder(name, sn, from, to, name, subPubKeyInfo); if (subjectAltName != null) v3CertGen.addExtension(Extension.subjectAlternativeName, false, subjectAltName); X509CertificateHolder certificateHolder = v3CertGen.build(sigGen); return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certificateHolder); } catch (CertificateException ce) { throw ce; } catch (Exception e) { throw new CertificateException(e); } } }
public static byte[] intToBigEndian(int[] ns) { byte[] bs = new byte[4 * ns.length]; intToBigEndian(ns, bs, 0); return bs; }
public static void bigEndianToLong(byte[] bs, int off, long[] ns) { for (int i = 0; i < ns.length; ++i) { ns[i] = bigEndianToLong(bs, off); off += 8; } }
public static void littleEndianToInt(byte[] bs, int off, int[] ns) { for (int i = 0; i < ns.length; ++i) { ns[i] = littleEndianToInt(bs, off); off += 4; } }
public static byte[] intToLittleEndian(int[] ns) { byte[] bs = new byte[4 * ns.length]; intToLittleEndian(ns, bs, 0); return bs; }
public static byte[] longToLittleEndian(long[] ns) { byte[] bs = new byte[8 * ns.length]; longToLittleEndian(ns, bs, 0); return bs; }
/** * Signs the certificate being built by the given builder using the given private key and returns the certificate. * @param privateKey the private key to sign the certificate with. * @param builder the cert builder that contains the certificate data. * @return the signed certificate. * @throws IOException * @throws OperatorCreationException * @throws CertificateException */ private static X509Certificate buildAndSignCertificate( PrivateKey privateKey, X509v3CertificateBuilder builder) throws IOException, OperatorCreationException, CertificateException { BcContentSignerBuilder signerBuilder; if (privateKey.getAlgorithm().contains("RSA")) { // a little hacky way to detect key type, but it works AlgorithmIdentifier signatureAlgorithm = new DefaultSignatureAlgorithmIdentifierFinder().find( "SHA256WithRSAEncryption"); AlgorithmIdentifier digestAlgorithm = new DefaultDigestAlgorithmIdentifierFinder().find(signatureAlgorithm); signerBuilder = new BcRSAContentSignerBuilder(signatureAlgorithm, digestAlgorithm); } else { // if not RSA, assume EC AlgorithmIdentifier signatureAlgorithm = new DefaultSignatureAlgorithmIdentifierFinder().find( "SHA256withECDSA"); AlgorithmIdentifier digestAlgorithm = new DefaultDigestAlgorithmIdentifierFinder().find(signatureAlgorithm); signerBuilder = new BcECContentSignerBuilder(signatureAlgorithm, digestAlgorithm); } AsymmetricKeyParameter privateKeyParam = PrivateKeyFactory.createKey(privateKey.getEncoded()); ContentSigner signer = signerBuilder.build(privateKeyParam); return toX509Cert(builder.build(signer)); }
public static void longToBigEndian(long n, byte[] bs, int off) { intToBigEndian((int)(n >>> 32), bs, off); intToBigEndian((int)(n & 0xffffffffL), bs, off + 4); }