Search in sources :

Example 1 with PasswordResolverException

use of org.xipki.password.PasswordResolverException in project xipki by xipki.

the class CaManagerImpl method init.

private void init() throws CaMgmtException {
    if (securityFactory == null) {
        throw new IllegalStateException("securityFactory is not set");
    }
    if (datasourceFactory == null) {
        throw new IllegalStateException("datasourceFactory is not set");
    }
    if (x509CertProfileFactoryRegister == null) {
        throw new IllegalStateException("x509CertProfileFactoryRegister is not set");
    }
    if (x509CertPublisherFactoryRegister == null) {
        throw new IllegalStateException("x509CertPublisherFactoryRegister is not set");
    }
    if (caConfFile == null) {
        throw new IllegalStateException("caConfFile is not set");
    }
    Properties caConfProps = new Properties();
    try {
        caConfProps.load(new FileInputStream(IoUtil.expandFilepath(caConfFile)));
    } catch (IOException ex) {
        throw new CaMgmtException("could not parse CA configuration" + caConfFile, ex);
    }
    String caModeStr = caConfProps.getProperty("ca.mode");
    if (caModeStr != null) {
        if ("slave".equalsIgnoreCase(caModeStr)) {
            masterMode = false;
        } else if ("master".equalsIgnoreCase(caModeStr)) {
            masterMode = true;
        } else {
            throw new CaMgmtException(concat("invalid ca.mode '", caModeStr, "'"));
        }
    } else {
        masterMode = true;
    }
    int shardId;
    String shardIdStr = caConfProps.getProperty("ca.shardId");
    if (StringUtil.isBlank(shardIdStr)) {
        throw new CaMgmtException("ca.shardId is not set");
    }
    LOG.info("ca.shardId: {}", shardIdStr);
    try {
        shardId = Integer.parseInt(shardIdStr);
    } catch (NumberFormatException ex) {
        throw new CaMgmtException(concat("invalid ca.shardId '", shardIdStr, "'"));
    }
    if (shardId < 0 || shardId > 127) {
        throw new CaMgmtException("ca.shardId is not in [0, 127]");
    }
    if (this.datasources == null) {
        this.datasources = new ConcurrentHashMap<>();
        for (Object objKey : caConfProps.keySet()) {
            String key = (String) objKey;
            if (!StringUtil.startsWithIgnoreCase(key, "datasource.")) {
                continue;
            }
            String datasourceFile = caConfProps.getProperty(key);
            try {
                String datasourceName = key.substring("datasource.".length());
                DataSourceWrapper datasource = datasourceFactory.createDataSourceForFile(datasourceName, datasourceFile, securityFactory.getPasswordResolver());
                Connection conn = datasource.getConnection();
                datasource.returnConnection(conn);
                this.datasources.put(datasourceName, datasource);
            } catch (DataAccessException | PasswordResolverException | IOException | RuntimeException ex) {
                throw new CaMgmtException(concat(ex.getClass().getName(), " while parsing datasource ", datasourceFile, ": ", ex.getMessage()), ex);
            }
        }
        this.datasource = this.datasources.get("ca");
    }
    if (this.datasource == null) {
        throw new CaMgmtException("no datasource named 'ca' configured");
    }
    this.queryExecutor = new CaManagerQueryExecutor(this.datasource);
    initEnvironmentParamters();
    String envEpoch = envParameterResolver.getParameter(ENV_EPOCH);
    if (masterMode) {
        lockCa(true);
        if (envEpoch == null) {
            final long day = 24L * 60 * 60 * 1000;
            envEpoch = queryExecutor.setEpoch(new Date(System.currentTimeMillis() - day));
            LOG.info("set environment {} to {}", ENV_EPOCH, envEpoch);
        }
        queryExecutor.addRequestorIfNeeded(RequestorInfo.NAME_BY_CA);
        queryExecutor.addRequestorIfNeeded(RequestorInfo.NAME_BY_USER);
    } else {
        if (envEpoch == null) {
            throw new CaMgmtException("The CA system must be started first with ca.mode = master");
        }
    }
    LOG.info("use EPOCH: {}", envEpoch);
    long epoch = DateUtil.parseUtcTimeyyyyMMdd(envEpoch).getTime();
    UniqueIdGenerator idGen = new UniqueIdGenerator(epoch, shardId);
    try {
        this.certstore = new CertificateStore(datasource, idGen);
    } catch (DataAccessException ex) {
        throw new CaMgmtException(ex.getMessage(), ex);
    }
    initCaAliases();
    initCertprofiles();
    initPublishers();
    initCmpControls();
    initRequestors();
    initResponders();
    initCrlSigners();
    initCas();
    initSceps();
}
Also used : Connection(java.sql.Connection) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Date(java.util.Date) CaMgmtException(org.xipki.ca.server.mgmt.api.CaMgmtException) CertificateStore(org.xipki.ca.server.impl.store.CertificateStore) PasswordResolverException(org.xipki.password.PasswordResolverException) DataSourceWrapper(org.xipki.datasource.DataSourceWrapper) DataAccessException(org.xipki.datasource.DataAccessException)

Example 2 with PasswordResolverException

use of org.xipki.password.PasswordResolverException in project xipki by xipki.

the class DataSourceFactory method createDataSource.

public DataSourceWrapper createDataSource(String name, InputStream conf, PasswordResolver passwordResolver) throws PasswordResolverException, IOException {
    ParamUtil.requireNonNull("conf", conf);
    Properties config = new Properties();
    try {
        config.load(conf);
    } finally {
        try {
            conf.close();
        } catch (Exception ex) {
            LOG.error("could not close stream: {}", ex.getMessage());
        }
    }
    return createDataSource(name, config, passwordResolver);
}
Also used : Properties(java.util.Properties) IOException(java.io.IOException) PasswordResolverException(org.xipki.password.PasswordResolverException)

Example 3 with PasswordResolverException

use of org.xipki.password.PasswordResolverException in project xipki by xipki.

the class FilePasswordCallback method init.

// method getPassword
@Override
public void init(String conf) throws PasswordResolverException {
    ParamUtil.requireNonBlank("conf", conf);
    ConfPairs pairs = new ConfPairs(conf);
    passwordFile = pairs.value("file");
    if (StringUtil.isBlank(passwordFile)) {
        throw new PasswordResolverException("invalid configuration " + conf + ", no file is specified");
    }
    passwordFile = IoUtil.expandFilepath(passwordFile);
}
Also used : PasswordResolverException(org.xipki.password.PasswordResolverException) ConfPairs(org.xipki.common.ConfPairs)

Example 4 with PasswordResolverException

use of org.xipki.password.PasswordResolverException in project xipki by xipki.

the class FilePasswordCallback method getPassword.

@Override
public char[] getPassword(String prompt, String testToken) throws PasswordResolverException {
    if (passwordFile == null) {
        throw new PasswordResolverException("please initialize me first");
    }
    String passwordHint = null;
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(IoUtil.expandFilepath(passwordFile)));
        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (StringUtil.isNotBlank(line) && !line.startsWith("#")) {
                passwordHint = line;
                break;
            }
        }
    } catch (IOException ex) {
        throw new PasswordResolverException("could not read file " + passwordFile, ex);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                LOG.error("could not close reader: {}", ex.getMessage());
            }
        }
    }
    if (passwordHint == null) {
        throw new PasswordResolverException("no password is specified in file " + passwordFile);
    }
    if (StringUtil.startsWithIgnoreCase(passwordHint, OBFPasswordService.OBFUSCATE)) {
        return OBFPasswordService.deobfuscate(passwordHint).toCharArray();
    } else {
        return passwordHint.toCharArray();
    }
}
Also used : PasswordResolverException(org.xipki.password.PasswordResolverException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException)

Example 5 with PasswordResolverException

use of org.xipki.password.PasswordResolverException in project xipki by xipki.

the class GuiPasswordCallback method init.

@Override
public void init(String conf) throws PasswordResolverException {
    if (StringUtil.isBlank(conf)) {
        quorum = 1;
        return;
    }
    ConfPairs pairs = new ConfPairs(conf);
    String str = pairs.value("quorum");
    quorum = Integer.valueOf(str);
    if (quorum < 1 || quorum > 10) {
        throw new PasswordResolverException("quorum " + quorum + " is not in [1,10]");
    }
    str = pairs.value("tries");
    if (StringUtil.isNotBlank(str)) {
        int intValue = Integer.parseInt(str);
        if (intValue > 0) {
            this.tries = intValue;
        }
    }
}
Also used : PasswordResolverException(org.xipki.password.PasswordResolverException) ConfPairs(org.xipki.common.ConfPairs)

Aggregations

PasswordResolverException (org.xipki.password.PasswordResolverException)9 IOException (java.io.IOException)4 ConfPairs (org.xipki.common.ConfPairs)4 FileInputStream (java.io.FileInputStream)2 Properties (java.util.Properties)2 CaMgmtException (org.xipki.ca.server.mgmt.api.CaMgmtException)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PublicKey (java.security.PublicKey)1 Connection (java.sql.Connection)1 Date (java.util.Date)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)1 CertificateStore (org.xipki.ca.server.impl.store.CertificateStore)1 ObjectCreationException (org.xipki.common.ObjectCreationException)1