Search in sources :

Example 16 with IllegalCmdParamException

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;
}
Also used : IllegalCmdParamException(org.xipki.console.karaf.IllegalCmdParamException) File(java.io.File) LinkedList(java.util.LinkedList)

Example 17 with IllegalCmdParamException

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;
}
Also used : BigIntegerRange(org.xipki.common.util.BigIntegerRange) RequestOptions(org.xipki.ocsp.client.api.RequestOptions) OcspBenchmark(org.xipki.ocsp.qa.benchmark.OcspBenchmark) FileBigIntegerIterator(org.xipki.common.util.FileBigIntegerIterator) LinkedList(java.util.LinkedList) X509Certificate(java.security.cert.X509Certificate) IllegalCmdParamException(org.xipki.console.karaf.IllegalCmdParamException) StringTokenizer(java.util.StringTokenizer) IllegalCmdParamException(org.xipki.console.karaf.IllegalCmdParamException) BigInteger(java.math.BigInteger) RangeBigIntegerIterator(org.xipki.common.util.RangeBigIntegerIterator) X509Certificate(java.security.cert.X509Certificate) Certificate(org.bouncycastle.asn1.x509.Certificate)

Example 18 with IllegalCmdParamException

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;
}
Also used : CertListOrderBy(org.xipki.ca.server.mgmt.api.CertListOrderBy) IllegalCmdParamException(org.xipki.console.karaf.IllegalCmdParamException) CertListInfo(org.xipki.ca.server.mgmt.api.CertListInfo) X500Name(org.bouncycastle.asn1.x500.X500Name) Date(java.util.Date)

Example 19 with IllegalCmdParamException

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;
}
Also used : IllegalCmdParamException(org.xipki.console.karaf.IllegalCmdParamException) File(java.io.File)

Example 20 with IllegalCmdParamException

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;
}
Also used : X509CertprofileQa(org.xipki.ca.qa.X509CertprofileQa) CertificationRequestInfo(org.bouncycastle.asn1.pkcs.CertificationRequestInfo) Attribute(org.bouncycastle.asn1.pkcs.Attribute) X509IssuerInfo(org.xipki.ca.qa.X509IssuerInfo) Extensions(org.bouncycastle.asn1.x509.Extensions) ValidationResult(org.xipki.common.qa.ValidationResult) ValidationIssue(org.xipki.common.qa.ValidationIssue) ASN1Set(org.bouncycastle.asn1.ASN1Set) CmdFailure(org.xipki.console.karaf.CmdFailure) IllegalCmdParamException(org.xipki.console.karaf.IllegalCmdParamException) CertificationRequest(org.bouncycastle.asn1.pkcs.CertificationRequest)

Aggregations

IllegalCmdParamException (org.xipki.console.karaf.IllegalCmdParamException)42 CmdFailure (org.xipki.console.karaf.CmdFailure)15 File (java.io.File)8 X509Certificate (java.security.cert.X509Certificate)6 CaMgmtException (org.xipki.ca.server.mgmt.api.CaMgmtException)6 P11Slot (org.xipki.security.pkcs11.P11Slot)6 BigInteger (java.math.BigInteger)5 RequestResponseDebug (org.xipki.common.RequestResponseDebug)5 P11ObjectIdentifier (org.xipki.security.pkcs11.P11ObjectIdentifier)5 FileInputStream (java.io.FileInputStream)4 NameId (org.xipki.ca.api.NameId)4 X509CRL (java.security.cert.X509CRL)3 Date (java.util.Date)3 LinkedList (java.util.LinkedList)3 Certificate (org.bouncycastle.asn1.x509.Certificate)3 CertIdOrError (org.xipki.ca.client.api.CertIdOrError)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Key (java.security.Key)2