private static byte[] readData(final FileOrValueType fileOrValue) throws IOException { byte[] data = fileOrValue.getValue(); if (data == null) { data = IoUtil.read(fileOrValue.getFile()); } return data; }
protected static String expandFilepath(final String path) { return IoUtil.expandFilepath(path); }
public DbDigestReporter(final String reportDirname, final byte[] caCertBytes) throws IOException { this.reportDirname = ParamUtil.requireNonBlank("reportDirname", reportDirname); File dir = new File(reportDirname); dir.mkdirs(); IoUtil.save(new File(dir, "ca.der"), caCertBytes); this.missingWriter = new BufferedWriter( new FileWriter(reportDirname + File.separator + "missing")); this.unexpectedWriter = new BufferedWriter( new FileWriter(reportDirname + File.separator + "unexpected")); this.diffWriter = new BufferedWriter( new FileWriter(reportDirname + File.separator + "diff")); this.goodWriter = new BufferedWriter( new FileWriter(reportDirname + File.separator + "good")); this.errorWriter = new BufferedWriter( new FileWriter(reportDirname + File.separator + "error")); start(); }
public void close() throws IOException { // write the account StringBuilder sb = new StringBuilder(50); sb.append(PROPKEY_ACCOUNT).append("=").append(numProcessed).append("\n"); sb.append(PROPKEY_ACCOUNT_REVOKED).append("=").append(numProcessedRevoked).append("\n"); IoUtil.save(new File(caDir, FILENAME_OVERVIEW), sb.toString().getBytes()); closeCurrentCsvFile(); IoUtil.closeStream(certsManifestOs); }
@Override protected Object execute0() throws Exception { String base64Cert = null; if (!"CA".equalsIgnoreCase(signerType)) { if (signerCertFile != null) { byte[] encodedCert = IoUtil.read(signerCertFile); base64Cert = IoUtil.base64Encode(encodedCert, false); X509Util.parseCert(encodedCert); } if (signerConf != null) { if ("PKCS12".equalsIgnoreCase(signerType) || "JKS".equalsIgnoreCase(signerType)) { signerConf = ShellUtil.canonicalizeSignerConf(signerType, signerConf, passwordResolver, securityFactory); } } } X509CrlSignerEntry entry = new X509CrlSignerEntry(name, signerType, signerConf, base64Cert, crlControl); boolean bo = caManager.addCrlSigner(entry); output(bo, "added", "could not add", "CRL signer " + name); return null; }
dataBytes = postData.getBytes(postDataCharSet); } else if (postDataFile != null) { dataBytes = IoUtil.read(postDataFile); HttpURLConnection httpConn = IoUtil.openHttpConn(newUrl); respContentBytes = IoUtil.read(inputStream); } else if (errorStream != null) { respContentBytes = IoUtil.read(errorStream); } else { respContentBytes = null;
protected byte[] send(final byte[] request) throws IOException { ParamUtil.requireNonNull("request", request); HttpURLConnection httpUrlConnection = IoUtil.openHttpConn(serverUrl); httpUrlConnection.setDoOutput(true); httpUrlConnection.setUseCaches(false);
private Exception exportCert(final CertStoreType certstore, final File processLogFile) { final File entriesDir = new File(baseDir, OcspDbEntryType.CERT.dirName()); entriesDir.mkdirs(); FileOutputStream certsFileOs = null; try { certsFileOs = new FileOutputStream( new File(baseDir, OcspDbEntryType.CERT.dirName() + ".mf"), true); exportCert0(certstore, processLogFile, certsFileOs); return null; } catch (Exception ex) { // delete the temporary files deleteTmpFiles(baseDir, "tmp-certs-"); System.err.println("\nexporting table CERT and CRAW has been cancelled due to error,\n" + "please continue with the option '--resume'"); LOG.error("Exception", ex); return ex; } finally { IoUtil.closeStream(certsFileOs); } } // method exportCert
@Override protected Object execute0() throws Exception { String base64Cert = null; X509Certificate signerCert = null; if (certFile != null) { signerCert = X509Util.parseCert(certFile); base64Cert = IoUtil.base64Encode(signerCert.getEncoded(), false); } if ("PKCS12".equalsIgnoreCase(signerType) || "JKS".equalsIgnoreCase(signerType)) { signerConf = ShellUtil.canonicalizeSignerConf(signerType, signerConf, passwordResolver, securityFactory); } CmpResponderEntry entry = new CmpResponderEntry(name, signerType, signerConf, base64Cert); boolean bo = caManager.addResponder(entry); output(bo, "added", "could not add", "CMP responder " + name); return null; }
@Override protected Object execute0() throws Exception { String base64Cert = IoUtil.base64Encode(IoUtil.read(certFile), false); CmpRequestorEntry entry = new CmpRequestorEntry(new NameId(null, name), base64Cert); boolean bo = (entry.cert() == null) ? false : caManager.addRequestor(entry); output(bo, "added", "could not add", "CMP requestor " + name); return null; }
private Exception exportEntries(final CaDbEntryType type, final CertStoreType certstore, final File processLogFile, final Long idProcessedInLastProcess) { String tablesText = (CaDbEntryType.CERT == type) ? "tables CERT and CRAW" : "table " + type.tableName(); File dir = new File(baseDir, type.dirName()); dir.mkdirs(); FileOutputStream entriesFileOs = null; try { entriesFileOs = new FileOutputStream( new File(baseDir, type.dirName() + ".mf"), true); exportEntries(type, certstore, processLogFile, entriesFileOs, idProcessedInLastProcess); return null; } catch (Exception ex) { // delete the temporary files deleteTmpFiles(baseDir, "tmp-"); System.err.println("\nexporting " + tablesText + " has been cancelled due to error,\n" + "please continue with the option '--resume'"); LOG.error("Exception", ex); return ex; } finally { if (entriesFileOs != null) { IoUtil.closeStream(entriesFileOs); } } }
protected String value(final FileOrValueType fileOrValue) throws IOException { if (fileOrValue == null) { return null; } if (fileOrValue.getValue() != null) { return fileOrValue.getValue(); } File file = new File(baseDir, fileOrValue.getFile()); return new String(IoUtil.read(file), "UTF-8"); }
@Override protected Object execute0() throws Exception { String base64Cert = null; if (certFile != null) { base64Cert = IoUtil.base64Encode(IoUtil.read(certFile), false); } if ("PKCS12".equalsIgnoreCase(responderType) || "JKS".equalsIgnoreCase(responderType)) { responderConf = ShellUtil.canonicalizeSignerConf(responderType, responderConf, passwordResolver, securityFactory); } ScepEntry entry = new ScepEntry(name, new NameId(null, caName), !inactive, responderType, responderConf, base64Cert, profiles, scepControl); if (entry.isFaulty()) { throw new InvalidConfException("certificate is invalid"); } boolean bo = caManager.addScep(entry); output(bo, "added", "could not add", "SCEP responder " + name); return null; }
protected static File expandFilepath(final File file) { return IoUtil.expandFilepath(file); }
private void replaceFile(final File file, final String oldText, final String newText) throws Exception { BufferedReader reader = new BufferedReader(new FileReader(file)); ByteArrayOutputStream writer = new ByteArrayOutputStream(); boolean changed = false; try { String line; while ((line = reader.readLine()) != null) { if (line.contains(oldText)) { changed = true; writer.write(line.replace(oldText, newText).getBytes()); } else { writer.write(line.getBytes()); } writer.write('\n'); } } finally { writer.close(); reader.close(); } if (changed) { File newFile = new File(file.getPath() + "-new"); byte[] newBytes = writer.toByteArray(); IoUtil.save(file, newBytes); newFile.renameTo(file); } }
@Override protected Object execute0() throws Exception { if (type == null && conf == null && confFile == null) { throw new IllegalCmdParamException("nothing to update"); } if (conf == null && confFile != null) { conf = new String(IoUtil.read(confFile)); } boolean bo = caManager.changePublisher(name, type, conf); output(bo, "updated", "could not update", "publisher " + name); return null; }
public static X509Certificate parseCert(final File file) throws IOException, CertificateException { ParamUtil.requireNonNull("file", file); FileInputStream in = new FileInputStream(IoUtil.expandFilepath(file)); try { return parseCert(in); } finally { in.close(); } }
private static void savePkcs11Entry(final File dir, final byte[] id, final String label, final byte[] value) throws P11TokenException { ParamUtil.requireNonNull("dir", dir); ParamUtil.requireNonNull("id", id); ParamUtil.requireNonBlank("label", label); ParamUtil.requireNonNull("value", value); String hexId = Hex.toHexString(id).toLowerCase(); StringBuilder sb = new StringBuilder(200); sb.append(PROP_ID).append('=').append(hexId).append('\n'); sb.append(PROP_LABEL).append('=').append(label).append('\n'); sb.append(PROP_SHA1SUM).append('=').append(HashAlgoType.SHA1.hexHash(value)).append('\n'); try { IoUtil.save(new File(dir, hexId + INFO_FILE_SUFFIX), sb.toString().getBytes()); IoUtil.save(new File(dir, hexId + VALUE_FILE_SUFFIX), value); } catch (IOException ex) { throw new P11TokenException("could not save certificate"); } }
private X509Cert readCertificate(final byte[] keyId) throws CertificateException, IOException { byte[] encoded = IoUtil.read(new File(certDir, Hex.toHexString(keyId) + VALUE_FILE_SUFFIX)); X509Certificate cert = X509Util.parseCert(encoded); return new X509Cert(cert, encoded); }
public static X509CRL parseCrl(final String file) throws IOException, CertificateException, CRLException { ParamUtil.requireNonBlank("file", file); return parseCrl(new FileInputStream(IoUtil.expandFilepath(file))); }