use of org.xipki.console.karaf.IllegalCmdParamException in project xipki by xipki.
the class FileListCmd method execute0.
@Override
protected Object execute0() throws Exception {
File target = new File(expandFilepath(targetPath));
if (!target.exists()) {
throw new IllegalCmdParamException("could not access " + targetPath + ": no such file or directory");
}
if (!target.isDirectory()) {
print(targetPath);
return null;
}
List<String> list = new LinkedList<>();
File[] children = target.listFiles();
int maxLen = -1;
if (children != null) {
for (File child : children) {
String name = child.getName();
if (child.isDirectory()) {
name += File.separator;
}
list.add(name);
maxLen = Math.max(maxLen, name.length());
}
}
if (isEmpty(list)) {
return null;
}
Collections.sort(list);
List<String> l2 = new LinkedList<>();
for (String s : list) {
String tmpS = s;
int diffLen = maxLen - tmpS.length();
if (diffLen > 0) {
for (int i = 0; i < diffLen; i++) {
tmpS += " ";
}
}
l2.add(tmpS);
}
int width = session.getTerminal().getWidth();
final int n = width / (maxLen + 1);
if (n == 0) {
for (String s : l2) {
print(s);
}
} else {
for (int i = 0; i < l2.size(); i += n) {
StringBuilder sb = new StringBuilder();
for (int j = i; j < Math.min(l2.size(), i + n); j++) {
sb.append(l2.get(j)).append(" ");
}
print(sb.toString());
}
}
println("");
return null;
}
use of org.xipki.console.karaf.IllegalCmdParamException in project xipki by xipki.
the class BenchmarkOcspStatusCmd method execute0.
@Override
protected Object execute0() throws Exception {
int ii = 0;
if (serialNumberList != null) {
ii++;
}
if (serialNumberFile != null) {
ii++;
}
if (CollectionUtil.isNonEmpty(certFiles)) {
ii++;
}
if (ii != 1) {
throw new IllegalCmdParamException("exactly one of serial, serial-file and cert must be specified");
}
if (numThreads < 1) {
throw new IllegalCmdParamException("invalid number of threads " + numThreads);
}
Iterator<BigInteger> serialNumberIterator;
if (serialNumberFile != null) {
serialNumberIterator = new FileBigIntegerIterator(IoUtil.expandFilepath(serialNumberFile), hex, true);
} else {
List<BigIntegerRange> serialNumbers = new LinkedList<>();
if (serialNumberList != null) {
StringTokenizer st = new StringTokenizer(serialNumberList, ", ");
while (st.hasMoreTokens()) {
String token = st.nextToken();
StringTokenizer st2 = new StringTokenizer(token, "-");
BigInteger from = toBigInt(st2.nextToken(), hex);
BigInteger to = st2.hasMoreTokens() ? toBigInt(st2.nextToken(), hex) : from;
serialNumbers.add(new BigIntegerRange(from, to));
}
} else if (certFiles != null) {
for (String certFile : certFiles) {
X509Certificate cert;
try {
cert = X509Util.parseCert(certFile);
} catch (Exception ex) {
throw new IllegalCmdParamException("invalid certificate file '" + certFile + "'", ex);
}
BigInteger serial = cert.getSerialNumber();
serialNumbers.add(new BigIntegerRange(serial, serial));
}
}
serialNumberIterator = new RangeBigIntegerIterator(serialNumbers, true);
}
try {
String description = StringUtil.concatObjects("issuer cert: ", issuerCertFile, "\nserver URL: ", serverUrl, "\nmaxRequest: ", maxRequests, "\nhash: ", hashAlgo);
Certificate issuerCert = Certificate.getInstance(IoUtil.read(issuerCertFile));
RequestOptions options = getRequestOptions();
OcspBenchmark loadTest = new OcspBenchmark(issuerCert, serverUrl, options, serialNumberIterator, maxRequests, analyzeResponse, queueSize, description.toString());
loadTest.setDuration(duration);
loadTest.setThreads(numThreads);
loadTest.test();
} finally {
if (serialNumberIterator instanceof FileBigIntegerIterator) {
((FileBigIntegerIterator) serialNumberIterator).close();
}
}
return null;
}
use of org.xipki.console.karaf.IllegalCmdParamException in project xipki by xipki.
the class ListCertCmd method execute0.
/**
* TODO.
* @return comma-separated serial numbers (in hex).
*/
@Override
protected Object execute0() throws Exception {
Date validFrom = getDate(validFromS);
Date validTo = getDate(validToS);
X500Name subjectPattern = null;
if (StringUtil.isNotBlank(subjectPatternS)) {
subjectPattern = new X500Name(subjectPatternS);
}
CertListOrderBy orderBy = null;
if (orderByS != null) {
orderBy = CertListOrderBy.forValue(orderByS);
if (orderBy == null) {
throw new IllegalCmdParamException("invalid order '" + orderByS + "'");
}
}
List<CertListInfo> certInfos = caManager.listCertificates(caName, subjectPattern, validFrom, validTo, orderBy, num);
final int n = certInfos.size();
if (n == 0) {
println("found no certificate");
return null;
}
println(" | serial | notBefore | notAfter | subject");
println("-----+----------------------+----------------+----------------+-----------------");
for (int i = 0; i < n; i++) {
CertListInfo info = certInfos.get(i);
println(format(i + 1, info));
}
return null;
}
use of org.xipki.console.karaf.IllegalCmdParamException in project xipki by xipki.
the class PBEDecryptCmd method execute0.
@Override
protected Object execute0() throws Exception {
ParamUtil.requireRange("mk", mquorum, 1, 10);
if (!(passwordHint == null ^ passwordFile == null)) {
throw new IllegalCmdParamException("exactly one of password and password-file must be specified");
}
if (passwordHint == null) {
passwordHint = new String(IoUtil.read(passwordFile));
}
if (!StringUtil.startsWithIgnoreCase(passwordHint, "PBE:")) {
throw new IllegalCmdParamException("encrypted password '" + passwordHint + "' does not start with PBE:");
}
char[] masterPassword;
if (masterPasswordFile != null) {
String str = new String(IoUtil.read(masterPasswordFile));
if (str.startsWith("OBF:") || str.startsWith("obf:")) {
str = OBFPasswordService.deobfuscate(str);
}
masterPassword = str.toCharArray();
} else {
if (mquorum == 1) {
masterPassword = readPassword("Master password");
} else {
char[][] parts = new char[mquorum][];
for (int i = 0; i < mquorum; i++) {
parts[i] = readPassword("Master password (part " + (i + 1) + "/" + mquorum + ")");
}
masterPassword = StringUtil.merge(parts);
}
}
char[] password = PBEPasswordService.decryptPassword(masterPassword, passwordHint);
if (outFile != null) {
saveVerbose("saved the password to file", new File(outFile), new String(password).getBytes());
} else {
println("the password is: '" + new String(password) + "'");
}
return null;
}
use of org.xipki.console.karaf.IllegalCmdParamException in project xipki by xipki.
the class CheckCertCmd method execute0.
@Override
protected Object execute0() throws Exception {
Set<String> issuerNames = qaSystemManager.getIssuerNames();
if (isEmpty(issuerNames)) {
throw new IllegalCmdParamException("no issuer is configured");
}
if (issuerName == null) {
if (issuerNames.size() != 1) {
throw new IllegalCmdParamException("no issuer is specified");
}
issuerName = issuerNames.iterator().next();
}
if (!issuerNames.contains(issuerName)) {
throw new IllegalCmdParamException("issuer " + issuerName + " is not within the configured issuers " + issuerNames);
}
X509IssuerInfo issuerInfo = qaSystemManager.getIssuer(issuerName);
X509CertprofileQa qa = qaSystemManager.getCertprofile(profileName);
if (qa == null) {
throw new IllegalCmdParamException("found no certificate profile named '" + profileName + "'");
}
CertificationRequest csr = CertificationRequest.getInstance(IoUtil.read(csrFile));
Extensions extensions = null;
CertificationRequestInfo reqInfo = csr.getCertificationRequestInfo();
ASN1Set attrs = reqInfo.getAttributes();
for (int i = 0; i < attrs.size(); i++) {
Attribute attr = Attribute.getInstance(attrs.getObjectAt(i));
if (PKCSObjectIdentifiers.pkcs_9_at_extensionRequest.equals(attr.getAttrType())) {
extensions = Extensions.getInstance(attr.getAttributeValues()[0]);
}
}
byte[] certBytes = IoUtil.read(certFile);
ValidationResult result = qa.checkCert(certBytes, issuerInfo, reqInfo.getSubject(), reqInfo.getSubjectPublicKeyInfo(), extensions);
StringBuilder sb = new StringBuilder();
sb.append(certFile).append(" (certprofile ").append(profileName).append(")\n");
sb.append("\tcertificate is ");
sb.append(result.isAllSuccessful() ? "valid" : "invalid");
if (verbose.booleanValue()) {
for (ValidationIssue issue : result.getValidationIssues()) {
sb.append("\n");
format(issue, " ", sb);
}
}
println(sb.toString());
if (!result.isAllSuccessful()) {
throw new CmdFailure("certificate is invalid");
}
return null;
}
Aggregations