Skip to content

Commit 995cdb2

Browse files
committed
Merge branch 'TinCanTech-init-pki-ec-ed-v2-auto-vars'
Signed-off-by: Richard T Bonhomme <[email protected]>
2 parents 84a19b7 + f3da7dc commit 995cdb2

File tree

2 files changed

+144
-37
lines changed

2 files changed

+144
-37
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Easy-RSA 3 ChangeLog
22

33
3.2.5 (TBD)
44

5+
* peer-fingerprint: Allow 'show-cert' to be used (7cf55e0) (#1397)
6+
* init-pki: Introduce configurable cryptography (a8da392) (#1397)
7+
58
* Replace "local" openssl-easyrsa.cnf (80702d6..b31443d) (#1394)
69

710
Original bug report: #1390 'OpenBSD/LibreSSL failure'

easyrsa3/easyrsa

Lines changed: 141 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Certificate & Request options: (these impact cert/req field values)
6464
--use-algo=ALG : Crypto alg to use: choose rsa (default), ec or ed
6565
--curve=NAME : For elliptic curve, sets the named curve
6666
(Default: algo ec: secp384r1, algo ed: ed25519)
67+
(--use-algo and --curve can be used to configure 'init-pki')
6768

6869
--subca-len=# : Path length of signed intermediate CA certificates
6970
--copy-ext : Copy included request X509 extensions (namely subjAltName)
@@ -110,7 +111,7 @@ Deprecated features:
110111

111112
Command list:
112113

113-
init-pki
114+
init-pki [ cmd-opts ]
114115
self-sign-server <file_name_base> [ cmd-opts ]
115116
self-sign-client <file_name_base> [ cmd-opts ]
116117
build-ca [ cmd-opts ]
@@ -169,7 +170,19 @@ Usage: easyrsa [ OPTIONS.. ] <COMMAND> <TARGET> [ cmd-opts.. ]"
169170
text="
170171
* init-pki [ cmd-opts ]
171172

172-
Removes & re-initializes the PKI directory for a new PKI"
173+
Removes & re-initializes the PKI directory for a new PKI
174+
175+
The new PKI can be auto-configured to use alternative cryptography.
176+
177+
The following command line examples are equivalent:
178+
$ easyrsa init-pki ed448
179+
$ easyrsa init-pki ed ed448
180+
$ easyrsa --use-algo=ed --curve=ed448 init-pki
181+
182+
Note: cmd-opts take priority over '--' global options"
183+
opts="
184+
* Optional algorithm 'ec' or 'ed' (Default: rsa)
185+
* Optional curve name (Default: None, secp384r1 or ed25519)"
173186
;;
174187
self-sign*)
175188
text="
@@ -901,8 +914,8 @@ secure_session - Missing temporary directory:
901914
remove_secure_session() {
902915
[ -d "$secured_session" ] || return 0
903916
if rm -rf "$secured_session"; then
904-
unset -v secured_session EASYRSA_SSL_CONF OPENSSL_CONF
905917
verbose "remove_secure_session; DELETED $secured_session"
918+
unset -v secured_session EASYRSA_SSL_CONF OPENSSL_CONF
906919
return
907920
fi
908921
die "remove_secure_session Failed: $secured_session"
@@ -1380,12 +1393,66 @@ $verify_ca_help_note"
13801393

13811394
# init-pki backend:
13821395
init_pki() {
1396+
verbose "BEGIN: algo: '$EASYRSA_ALGO' | curve: '$EASYRSA_CURVE'"
1397+
1398+
# Parse options for algo/curve - overwrite defaults
1399+
while [ "$1" ]; do
1400+
case "$1" in
1401+
rsa)
1402+
export EASYRSA_ALGO="$1"
1403+
unset -v EASYRSA_CURVE
1404+
;;
1405+
ec)
1406+
export EASYRSA_ALGO="$1"
1407+
export EASYRSA_CURVE=secp384r1
1408+
;;
1409+
ed)
1410+
export EASYRSA_ALGO="$1"
1411+
export EASYRSA_CURVE=ed25519
1412+
;;
1413+
*)
1414+
export EASYRSA_CURVE="$1"
1415+
case "$EASYRSA_CURVE" in
1416+
ed*) export EASYRSA_ALGO=ed ;;
1417+
*) export EASYRSA_ALGO=ec
1418+
esac
1419+
esac
1420+
shift
1421+
done
1422+
1423+
# Set default curve based on algo
1424+
case "$EASYRSA_ALGO" in
1425+
rsa) : ;; # ok
1426+
ec) set_var EASYRSA_CURVE secp384r1 ;;
1427+
ed) set_var EASYRSA_CURVE ed25519 ;;
1428+
*) die "Unknown EASYRSA_ALGO: '$EASYRSA_ALGO'"
1429+
esac
1430+
1431+
# Set default algo based on curve
1432+
case "$EASYRSA_CURVE" in
1433+
'') : ;; # ok
1434+
ed*) set_var EASYRSA_ALGO ed ;;
1435+
*) set_var EASYRSA_ALGO ec
1436+
esac
1437+
1438+
# Verify user settings
1439+
verbose "TRY: Algo: '$EASYRSA_ALGO' - Curve: '$EASYRSA_CURVE'"
1440+
verify_algo_params
1441+
13831442
# EasyRSA will NOT do 'rm -rf /'
13841443
case "$EASYRSA_PKI" in
13851444
.|..|./|../|.//*|..//*|/|//*|\\|?:|'')
13861445
user_error "Invalid PKI: $EASYRSA_PKI"
13871446
esac
13881447

1448+
# Auto-configuration $pki/vars for ec/ed
1449+
case "$EASYRSA_ALGO" in
1450+
rsa) auto_algo= ;; # ok
1451+
ec) auto_algo="Auto-configured for Elliptic curve '$EASYRSA_CURVE'" ;;
1452+
ed) auto_algo="Auto-configured for Edwards curve '$EASYRSA_CURVE'" ;;
1453+
*) die "Auto-configuration, Unknown EASYRSA_ALGO: '$EASYRSA_ALGO'"
1454+
esac
1455+
13891456
# If EASYRSA_PKI exists, confirm before deletion
13901457
if [ -d "$EASYRSA_PKI" ]; then
13911458
confirm "Confirm removal: " "yes" "
@@ -1394,12 +1461,12 @@ WARNING!!!
13941461
You are about to remove the EASYRSA_PKI at:
13951462
* $EASYRSA_PKI
13961463

1397-
and initialize a fresh PKI here."
1398-
fi
1464+
and initialize a fresh PKI here. $auto_algo"
13991465

1400-
# # # shellcheck disable=SC2115 # Use "${var:?}"
1401-
rm -rf "$EASYRSA_PKI" || \
1402-
die "init-pki hard reset failed."
1466+
# Remove existing PKI
1467+
rm -rf "$EASYRSA_PKI" || \
1468+
die "Failed to remove existing PKI: '$EASYRSA_PKI'"
1469+
fi
14031470

14041471
# new dirs:
14051472
for i in issued private reqs; do
@@ -1409,7 +1476,23 @@ and initialize a fresh PKI here."
14091476

14101477
# write pki/vars.example - no temp-file because no session
14111478
write_legacy_file_v2 vars "$EASYRSA_PKI"/vars.example || \
1412-
warn "init_pki() - Failed to create vars.example"
1479+
die "init_pki() - Failed to create vars.example"
1480+
1481+
# Auto-configuration $pki/vars for ec/ed
1482+
case "$EASYRSA_ALGO" in
1483+
ec|ed)
1484+
# sed search and replace regex
1485+
s_alg='#set_var[[:blank:]]*EASYRSA_ALGO[[:blank:]]*rsa'
1486+
r_alg="set_var EASYRSA_ALGO $EASYRSA_ALGO # --> $auto_algo"
1487+
s_crv='#set_var[[:blank:]]*EASYRSA_CURVE[[:blank:]]*secp384r1'
1488+
r_crv="set_var EASYRSA_CURVE $EASYRSA_CURVE # --> $auto_algo"
1489+
1490+
# Create Auto-configured vars file
1491+
# Note: pki/vars.example is Always created by Easy-RSA above
1492+
sed -e s/"$s_alg"/"$r_alg"/ -e s/"$s_crv"/"$r_crv"/ \
1493+
"$EASYRSA_PKI"/vars.example > "$EASYRSA_PKI"/vars || \
1494+
die "sed auto-vars"
1495+
esac
14131496

14141497
# User notice
14151498
notice "\
@@ -1418,12 +1501,15 @@ and initialize a fresh PKI here."
14181501
Your newly created PKI dir is:
14191502
* $EASYRSA_PKI"
14201503

1421-
# Select and show vars file
1422-
unset -v EASYRSA_VARS_FILE
1504+
# Select and show Auto-configured vars file
1505+
unset -v ignore_vars EASYRSA_VARS_FILE
14231506
select_vars
1424-
information "\
1425-
Using Easy-RSA configuration:
1426-
* ${EASYRSA_VARS_FILE:-undefined}"
1507+
if [ "$EASYRSA_VARS_FILE" ]; then
1508+
information "\
1509+
IMPORTANT: PKI algorithm is $auto_algo${NL}
1510+
Using Easy-RSA Auto-configured 'vars' file:
1511+
* ${EASYRSA_VARS_FILE}${NL}"
1512+
fi
14271513
} # => init_pki()
14281514

14291515
# Find support files from various sources
@@ -2166,13 +2252,16 @@ $EASYRSA_EXTRA_EXTS"
21662252
# Set algorithm options
21672253
algo_opts=""
21682254
case "$EASYRSA_ALGO" in
2169-
rsa|ec)
2170-
# Set elliptic curve parameters-file
2171-
# or RSA bit-length
2255+
rsa)
2256+
# RSA bit-length
2257+
algo_opts="$EASYRSA_ALGO:$EASYRSA_ALGO_PARAMS"
2258+
;;
2259+
ec)
2260+
# Elliptic curve parameters-file
21722261
algo_opts="$EASYRSA_ALGO:$EASYRSA_ALGO_PARAMS"
21732262
;;
21742263
ed)
2175-
# Set Edwards curve name
2264+
# Edwards curve name
21762265
algo_opts="$EASYRSA_CURVE"
21772266
;;
21782267
*)
@@ -5322,34 +5411,40 @@ show_host() {
53225411
verify_algo_params() {
53235412
case "$EASYRSA_ALGO" in
53245413
rsa)
5414+
[ "$EASYRSA_CURVE" ] && user_error "\
5415+
Elliptic curve cryptography cannot be use with algo '$EASYRSA_ALGO'"
53255416
# Set RSA key size
53265417
EASYRSA_ALGO_PARAMS="$EASYRSA_KEY_SIZE"
53275418
;;
53285419
ec)
5329-
# Verify Elliptic curve
5330-
EASYRSA_ALGO_PARAMS=""
5331-
easyrsa_mktemp EASYRSA_ALGO_PARAMS
5332-
5333-
# Create the required ecparams file, temp-file
5334-
# call openssl directly because error is expected
5335-
"$EASYRSA_OPENSSL" ecparam \
5336-
-name "$EASYRSA_CURVE" \
5337-
-out "$EASYRSA_ALGO_PARAMS" \
5338-
>/dev/null 2>&1 || user_error "\
5339-
Failed to generate ecparam file for curve '$EASYRSA_CURVE'"
5420+
if [ -f "$EASYRSA_ALGO_PARAMS" ]; then
5421+
# User supplied file
5422+
verbose "External ecparams file '$EASYRSA_ALGO_PARAMS'"
5423+
elif [ -d "$EASYRSA_TEMP_DIR" ]; then
5424+
# generate file
5425+
unset -v EASYRSA_ALGO_PARAMS
5426+
easyrsa_mktemp EASYRSA_ALGO_PARAMS
5427+
"$EASYRSA_OPENSSL" ecparam -name "$EASYRSA_CURVE" \
5428+
-out "$EASYRSA_ALGO_PARAMS" >/dev/null 2>&1 || user_error \
5429+
"Failed to generate ecparams for curve '$EASYRSA_CURVE'"
5430+
else
5431+
# Verify only
5432+
"$EASYRSA_OPENSSL" ecparam -name "$EASYRSA_CURVE" \
5433+
>/dev/null 2>&1 || user_error \
5434+
"Failed to verify ecparams for curve '$EASYRSA_CURVE'"
5435+
fi
53405436
;;
53415437
ed)
53425438
# Verify Edwards curve
5343-
# call openssl directly because error is expected
5344-
"$EASYRSA_OPENSSL" genpkey \
5345-
-algorithm "$EASYRSA_CURVE" \
5346-
>/dev/null 2>&1 || user_error "\
5347-
Edwards Curve '$EASYRSA_CURVE' not found."
5439+
"$EASYRSA_OPENSSL" genpkey -algorithm "$EASYRSA_CURVE" \
5440+
>/dev/null 2>&1 || user_error \
5441+
"Edwards Curve '$EASYRSA_CURVE' not found."
53485442
;;
53495443
*) user_error "\
53505444
Unknown algorithm '$EASYRSA_ALGO': Must be 'rsa', 'ec' or 'ed'"
53515445
esac
5352-
verbose "verify_algo_params; OK: algo '$EASYRSA_ALGO'"
5446+
verbose "\
5447+
verify_algo_params; OK: Algo '$EASYRSA_ALGO' - Curve '${EASYRSA_CURVE:-None}'"
53535448
} # => verify_algo_params()
53545449

53555450
# Check for conflicting input options
@@ -5439,6 +5534,9 @@ To correct this problem, it is recommended that you either:
54395534
# If not present, defaults are used to support
54405535
# running without a sourced config format.
54415536
select_vars() {
5537+
# Deliberately ignore vars
5538+
[ "$ignore_vars" ] && return 1
5539+
54425540
# User specified vars file will be used ONLY
54435541
if [ "$EASYRSA_VARS_FILE" ]; then
54445542
: # Takes priority, nothing to do
@@ -5770,7 +5868,7 @@ Using Easy-RSA 'vars' configuration:
57705868
# Create temp-session and global safe ssl config tmp-file
57715869
# if required, openssl-easyrsa.cnf tmp-file
57725870
if [ -d "$EASYRSA_TEMP_DIR" ]; then
5773-
verbose "temp-dir: Found: $EASYRSA_TEMP_DIR"
5871+
verbose "temp-dir: FOUND: $EASYRSA_TEMP_DIR"
57745872
# Temp dir session
57755873
secure_session
57765874

@@ -6543,6 +6641,7 @@ unset -v \
65436641
secured_session \
65446642
alias_days text \
65456643
prohibit_no_pass \
6644+
ignore_vars \
65466645
invalid_vars \
65476646
local_request error_build_full_cleanup \
65486647
selfsign_eku \
@@ -6618,6 +6717,10 @@ while :; do
66186717
;;
66196718
--curve)
66206719
export EASYRSA_CURVE="$val"
6720+
case "$EASYRSA_CURVE" in
6721+
ed*) set_var EASYRSA_ALGO ed ;;
6722+
*) set_var EASYRSA_ALGO ec
6723+
esac
66216724
;;
66226725
--dn-mode)
66236726
export EASYRSA_DN="$val"
@@ -6818,6 +6921,7 @@ cmd="$1"
68186921
# ONLY verify_working_env() for valid commands
68196922
case "$cmd" in
68206923
init-pki|clean-all)
6924+
ignore_vars=1 # Deliberately ignore vars
68216925
require_pki=""; require_ca=""; verify_working_env
68226926
init_pki "$@"
68236927
;;
@@ -6939,7 +7043,7 @@ case "$cmd" in
69397043
show req "$@"
69407044
;;
69417045
show-cert)
6942-
require_pki=1; require_ca=1; verify_working_env
7046+
require_pki=1; require_ca=""; verify_working_env
69437047
show cert "$@"
69447048
;;
69457049
show-crl)

0 commit comments

Comments
 (0)