在openvpn中传递SSL密钥的密码

时间:2013-11-26 14:10:42

标签: bash shell openssl openvpn

目前我即将更改一个名为“pkitool”的脚本(如果有人不使用openvpn,但也想帮助我,这就是pkitool的样子:https://joinup.ec.europa.eu/svn/cube/trunk/cube/cube-integration/src/main/scripts/openvpn/pkitool)。我的目标是,我能够传递变量$ 1(Keyname)和我在相同脚本中导出的密码。它看起来像这样:

export KEY_PASSWORD=$2
./pkitool --pass $1

目前我被要求输入密码并进行验证。我想改变它,只是传递密码和脚本,我希望脚本要求我输入一个密码短语...(我导出变量KEY_PASSWORD的原因是因为我想稍后使用它。)这是我修改过的pkitool的摘录:

# Process options while [ $# -gt 0 ]; do
    case "$1" in
        --keysize  ) KEY_SIZE=$2
                     shift;;
        --server   ) REQ_EXT="$REQ_EXT -extensions server"
                     CA_EXT="$CA_EXT -extensions server" ;;
        --batch    ) BATCH="-batch" ;;
        --interact ) BATCH="" ;;
        --inter    ) CA_EXT="$CA_EXT -extensions v3_ca" ;;
        --initca   ) DO_ROOT="1" ;;
        --pass     ) NODES_REQ="-passin env:KEY_PASSWORD" ;;
        --csr      ) DO_CA="0" ;;
        --sign     ) DO_REQ="0" ;;
        --pkcs12   ) DO_P12="1" ;;
        --pkcs11   ) DO_P11="1"
                     PKCS11_MODULE_PATH="$2"
                     PKCS11_SLOT="$3"
                     PKCS11_ID="$4"
                     PKCS11_LABEL="$5"
                     shift 4;;

我明显使用变量参数“--pass”。我使用“-passin env:KEY_PASSWORD”的原因是这个手册页我多次被误解......

PASS PHRASE ARGUMENTS
       Several commands accept password arguments, typically using -passin and -passout for
       input and output passwords respectively. These allow the password to be obtained from a
       variety of sources. Both of these options take a single argument whose format is
       described below. If no password argument is given and a password is required then the
       user is prompted to enter one: this will typically be read from the current terminal with

env:var   obtain the password from the environment variable var. Since the environment of
                 other processes is visible on certain platforms (e.g. ps under certain Unix
                 OSes) this option should be used with caution.

这是pkitool的一部分,再次使用NODES_REQ:

# Build cert/key
        ( [ $DO_REQ -eq 0 ] || $OPENSSL req $BATCH -days $KEY_EXPIRE $NODES_REQ -new -newkey rsa:$KEY_SIZE \
                -keyout "$FN.key" -out "$FN.csr" $REQ_EXT -config "$KEY_CONFIG" $PKCS11_ARGS ) && \
            ( [ $DO_CA -eq 0 ]  || $OPENSSL ca $BATCH -days $KEY_EXPIRE -out "$FN.crt" \
                -in "$FN.csr" $CA_EXT -md sha1 -config "$KEY_CONFIG" ) && \
            ( [ $DO_P12 -eq 0 ] || $OPENSSL pkcs12 -export -inkey "$FN.key" \
                -in "$FN.crt" -certfile "$CA.crt" -out "$FN.p12" $NODES_P12 ) && \
            ( [ $DO_CA -eq 0 -o $DO_P11 -eq 1 ]  || chmod 0600 "$FN.key" ) && \
            ( [ $DO_P12 -eq 0 ] || chmod 0600 "$FN.p12" )

未修改pkitool的其余部分,您可以在说明中查看链接。希望你们明白我的问题。 HALP PLS,无法弄明白:(

编辑:当NODES_REQ处于默认状态时,它看起来像这样:

NODES_REQ = "-nodes"

这两个重要部分(也就是我使用-passin的原因)看起来像这样:

-nodes
           if this option is specified then if a private key is created it will not be
           encrypted.
-passin arg
           the input file password source. For more information about the format of arg see the
           PASS PHRASE ARGUMENTS section in openssl(1).

1 个答案:

答案 0 :(得分:2)

我不得不使用-passout而不是-passin ...我必须仔细阅读手册页以了解细微之处。有两个选项-passin和-passout的原因是当输入文件受密码保护并且需要提供密码来解锁时使用passin,并且在密码保护输出文件时使用passout。由于“req”只是生成输出,我需要的是-passout,而不是-passin。 :)