Ovirt程序员

【Ovirt 笔记】管理员登录密码的加解密实现分析与整理

2018-02-26  本文已影响57人  58bc06151329

文前说明

作为码农中的一员,需要不断的学习,我工作之余将一些分析总结和学习笔记写成博客与大家一起交流,也希望采用这种方式记录自己的学习之旅。

本文仅供学习交流使用,侵权必删。
不用于商业目的,转载请注明出处。

分析整理的版本为 Ovirt 3.4.5 版本。

1. 登录密码的加密

public static String encrypt(String source) throws GeneralSecurityException {
        if (source == null || source.trim().length() == 0) {
            return source;
        }
        else {
            Cipher rsa = Cipher.getInstance("RSA");
            rsa.init(Cipher.ENCRYPT_MODE, getCertificate().getPublicKey());
            return new Base64(0).encodeToString(
                rsa.doFinal(source.trim().getBytes(Charset.forName("UTF-8")))
            );
        }
}

2. 登录密码的解密

public static String decrypt(String source) throws GeneralSecurityException {
        if (source == null || source.trim().length() == 0) {
            return source;
        }
        else {
            Cipher rsa = Cipher.getInstance("RSA");
            rsa.init(Cipher.DECRYPT_MODE, getPrivateKeyEntry().getPrivateKey());
            return new String(
                rsa.doFinal(new Base64().decode(source)),
                Charset.forName("UTF-8")
            );
        }
}

3. 证书的生成

[root@localhost ~]# cat /etc/ovirt-engine/engine.conf.d/10-setup-pki.conf
ENGINE_PKI="/etc/pki/ovirt-engine"
ENGINE_PKI_CA="/etc/pki/ovirt-engine/ca.pem"
ENGINE_PKI_ENGINE_CERT="/etc/pki/ovirt-engine/certs/engine.cer"
ENGINE_PKI_TRUST_STORE="/etc/pki/ovirt-engine/.truststore"
ENGINE_PKI_TRUST_STORE_PASSWORD="mypass"
ENGINE_PKI_ENGINE_STORE="/etc/pki/ovirt-engine/keys/engine.p12"
ENGINE_PKI_ENGINE_STORE_PASSWORD="mypass"
ENGINE_PKI_ENGINE_STORE_ALIAS="1
public static Certificate getCertificate() {
    return getPrivateKeyEntry().getCertificate();
}

public static KeyStore.PrivateKeyEntry getPrivateKeyEntry() {
        try {
            KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry)getKeyStore().getEntry(
                keystoreAlias,
                keystorePassword
            );
            if (entry == null) {
                throw new RuntimeException("Alias was not found");
            }
            return entry;
        }
        catch (Exception e) {
            throw new RuntimeException(
                String.format(
                    "Failed to locate key '%1$s'",
                    keystoreAlias
                ),
                e
            );
        }
}

public static KeyStore getKeyStore() {
     return _getKeyStore("PKCS12", keystoreFile, keystorePassword.getPassword());
}

keystoreFile = config.getPKIEngineStore().getAbsoluteFile();

public File getPKIEngineStore() {
   return getFile("ENGINE_PKI_ENGINE_STORE");
}
3.1 engine.p12 密钥库的生成
3.1.1 CA 证书的生成
self.execute(
            args=(
                osetupcons.FileLocations.OVIRT_ENGINE_PKI_CA_CREATE,
                '--subject=/C=%s/O=%s/CN=%s.%s' % (
                    self._subjectComponentEscape(
                        self.environment[osetupcons.PKIEnv.COUNTRY],
                    ),
                    self._subjectComponentEscape(
                        self.environment[osetupcons.PKIEnv.ORG],
                    ),
                    self._subjectComponentEscape(
                        self.environment[
                            osetupcons.ConfigEnv.FQDN
                        ][:MAX_HOST_FQDN_LEN],
                    ),
                    random.randint(10000, 99999),
                ),
                '--keystore-password=%s' % (
                    self.environment[osetupcons.PKIEnv.STORE_PASS],
                ),
            ),
            envAppend={
                'JAVA_HOME': self.environment[
                    osetupcons.ConfigEnv.JAVA_HOME
                ],
            },
        )
OVIRT_ENGINE_PKI_CA_CREATE = os.path.join(
        OVIRT_ENGINE_BINDIR,
        'pki-create-ca.sh',
)
openssl genrsa \
    -out "${PKIDIR}/private/ca.pem" \
    2048 \
    || die "Cannot generate CA key"
openssl req \
    -batch \
    -config "${PKIDIR}/cacert.conf" \
    -new \
    -key "${PKIDIR}/private/ca.pem" \
    -out "${PKIDIR}/requests/ca.csr" \
    -subj "/" \
    || die "Cannot generate CA request"
openssl ca \
        -batch \
        -config openssl.conf \
        -extfile cacert.conf \
        -extensions v3_ca \
        -in requests/ca.csr \
        -out ca.pem \
        -keyfile private/ca.pem \
        -selfsign \
        -subj "${subject}" \
        -days 3650 \
        -startdate "$(date --utc --date "now -1 days" +"%y%m%d%H%M%S%z")"
3.1.2 engine 证书的生成
for name in ('engine', 'apache', 'jboss'):
            self.execute(
                (
                    osetupcons.FileLocations.OVIRT_ENGINE_PKI_CA_ENROLL,
                    '--name=%s' % name,
                    '--password=%s' % (
                        self.environment[osetupcons.PKIEnv.STORE_PASS],
                    ),
                    '--subject=/C=%s/O=%s/CN=%s' % (
                        self._subjectComponentEscape(
                            self.environment[osetupcons.PKIEnv.COUNTRY],
                        ),
                        self._subjectComponentEscape(
                            self.environment[osetupcons.PKIEnv.ORG],
                        ),
                        self._subjectComponentEscape(
                            self.environment[osetupcons.ConfigEnv.FQDN],
                        ),
                    ),
                ),
            )
OVIRT_ENGINE_PKI_CA_ENROLL = os.path.join(
        OVIRT_ENGINE_BINDIR,
        'pki-enroll-pkcs12.sh',
)
openssl \
        genrsa \
        -out "${TMPKEY}" \
        -passout "pass:${pass}" \
        -des3 \
        2048 \
        || die "Cannot create certificate request"
[root@localhost ~]# echo $(mktemp)
/tmp/tmp.cxiEzfmSJd
openssl \
    req \
    -new \
    -days 365 \
    -key "${TMPKEY}" \
    -out "${req}" \
    -passin "pass:${pass}" \
    -passout "pass:${pass}" \
    -batch \
    -subj "/" \
    || die "Cannot create certificate request"
"${BINDIR}/pki-enroll-request.sh" \
    --name="${name}" \
    --subject="${subj}" \
    || die "Cannot sign request"
openssl ca \
    -batch \
    -policy policy_match \
    -config openssl.conf \
    -cert ca.pem \
    -keyfile private/ca.pem \
    -days "${days}" \
    -in "${req}" \
    -out "${cert}" \
    -startdate "$(date --utc --date "now -1 days" +"%y%m%d%H%M%S%z")" \
    ${subject:+-subj "${subject}"} \
    ${EXTRA_COMMAND} \
    || die "Cannot sign certificate"
chmod a+r "${cert}" || die "Cannot set certificate permissions"
3.1.3 最终生成密钥库
touch "${pkcs12}"
chmod go-rwx "${pkcs12}" || die "Cannot set PKCS#12 permissions"
openssl \
    pkcs12 \
    -export \
    -in "${cert}" \
    -inkey "${TMPKEY}" \
    -out "${pkcs12}" \
    -passin "pass:${pass}" \
    -passout "pass:${pass}" \
    || die "Cannot create PKCS#12"
上一篇下一篇

猜你喜欢

热点阅读