static ChannelBuffer[] readCertificates(File file) throws CertificateException { String content; try { content = readContent(file); } catch (IOException e) { throw new CertificateException("failed to read a file: " + file, e); } List<ChannelBuffer> certs = new ArrayList<ChannelBuffer>(); Matcher m = CERT_PATTERN.matcher(content); int start = 0; for (;;) { if (!m.find(start)) { break; } certs.add(Base64.decode(ChannelBuffers.copiedBuffer(m.group(1), CharsetUtil.US_ASCII))); start = m.end(); } if (certs.isEmpty()) { throw new CertificateException("found no certificates: " + file); } return certs.toArray(new ChannelBuffer[certs.size()]); }
private static String readContent(File file) throws IOException { InputStream in = new FileInputStream(file); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { byte[] buf = new byte[8192]; for (;;) { int ret = in.read(buf); if (ret < 0) { break; } out.write(buf, 0, ret); } return out.toString(CharsetUtil.US_ASCII.name()); } finally { safeClose(in); safeClose(out); } }
KeyFactory dsaKF = KeyFactory.getInstance("DSA"); ChannelBuffer encodedKeyBuf = PemReader.readPrivateKey(keyFile); byte[] encodedKey = new byte[encodedKeyBuf.readableBytes()]; encodedKeyBuf.readBytes(encodedKey); for (ChannelBuffer buf: PemReader.readCertificates(certChainFile)) { certChain.add(cf.generateCertificate(new ChannelBufferInputStream(buf)));
CertificateFactory cf = CertificateFactory.getInstance("X.509"); for (ChannelBuffer buf: PemReader.readCertificates(certChainFile)) { X509Certificate cert = (X509Certificate) cf.generateCertificate(new ChannelBufferInputStream(buf)); X500Principal principal = cert.getSubjectX500Principal();
static ChannelBuffer readPrivateKey(File file) throws KeyException { String content; try { content = readContent(file); } catch (IOException e) { throw new KeyException("failed to read a file: " + file, e); } Matcher m = KEY_PATTERN.matcher(content); if (!m.find()) { throw new KeyException("found no private key: " + file); } return Base64.decode(ChannelBuffers.copiedBuffer(m.group(1), CharsetUtil.US_ASCII)); }