| 1 | #!/usr/bin/env perl | ||
| 2 | package sbtool::keygen; | ||
| 3 | |||
| 4 | use utf8; | ||
| 5 | use v5.40; | ||
| 6 | |||
| 7 | use lib 'lib'; | ||
| 8 | |||
| 9 | use Const::Fast; | ||
| 10 | use Path::Tiny; | ||
| 11 | use File::chdir; | ||
| 12 | use List::Util qw'all first'; | ||
| 13 | use Time::HiRes 'gettimeofday'; | ||
| 14 | use Sys::Hostname 'hostname'; | ||
| 15 | use Net::SSLeay::CA::Base 'dmsg'; | ||
| 16 | use Net::SSLeay::CA::Util::Cmd; | ||
| 17 | |||
| 18 | const our $SBTOOL_ROOT => path( $ENV{SBTOOL_ROOT} // '/etc/sbtool' ); | ||
| 19 | const our $CERTFILE_EXT_PTN => qr/(pem|crt)$/; | ||
| 20 | |||
| 21 | sub cmd ( $cmd_aref, $inh = \undef, $outh = undef, $errh = undef, %opts ) { | ||
| 22 | |||
| 23 | my ( $ret, $status, $err ) = ( run3( $cmd_aref, \undef ), $?, $! ); | ||
| 24 | |||
| 25 | die "❌️: $err ($status)" | ||
| 26 | || "Encountered unknonw error attempting to run:\n▷ " . join " ", | ||
| 27 | @$cmd_aref; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub subj_common ( $argv = [@ARGV] ) { | ||
| 31 | dmsg( | ||
| 32 | { | ||
| 33 | argv => $argv, | ||
| 34 | NAME => $ENV{NAME}, | ||
| 35 | SBINIT_SUBJCN => $ENV{SBINIT_SUBJCN}, | ||
| 36 | hostname => hostname | ||
| 37 | } | ||
| 38 | ); | ||
| 39 | |||
| 40 | my $cn = ( $ENV{NAME} || $ENV{SBINIT_SUBJCN} || shift @ARGV || hostname ); | ||
| 41 | my $subj = "/CN=$cn"; | ||
| 42 | my $o = ( $ENV{SBINIT_SUBJO} // shift @ARGV ); | ||
| 43 | $o and $subj .= "O=$o/"; | ||
| 44 | my $ou = ( $ENV{SBINIT_OU} // shift @ARGV ); | ||
| 45 | $ou and $subj .= "OU=$ou"; | ||
| 46 | |||
| 47 | $subj; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub noPK ( $file = "noPK.esl" ) { | ||
| 51 | if ( -f $file ) { | ||
| 52 | rename "$file", | ||
| 53 | $file =~ s/\.esl$//r . ( join "", gettimeofday ) . '.esl'; | ||
| 54 | } | ||
| 55 | |||
| 56 | # Ugly touch | ||
| 57 | open my $fh, '>', $file; | ||
| 58 | printf $fh ""; | ||
| 59 | close $fh; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub newreq ( $certfile, $keyfile, $level, %opts ) { | ||
| 63 | my @req = ( qw'openssl req -new -x509 -nodes -sha256 -out', $certfile ); | ||
| 64 | push @req, ( '-days', $ENV{DAYSVALID} // 3650 ); | ||
| 65 | push @req, ( '-newkey', 'rsa:4096', '-keyout', $keyfile ) | ||
| 66 | unless -f $keyfile; | ||
| 67 | push @req, | ||
| 68 | ( '-subj', $ENV{ uc $level . 'SUBJBASE' } // "$ENV{SUBJBASE} $level" ); | ||
| 69 | |||
| 70 | cmd( \@req ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub to_DER ($certfile) { | ||
| 74 | cmd( | ||
| 75 | [ | ||
| 76 | qw(openssl x509 -in), | ||
| 77 | $certfile, '-out', | ||
| 78 | ( $certfile =~ s/$CERTFILE_EXT_PTN/cer/r ), | ||
| 79 | qw'-outform DER' | ||
| 80 | ] | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub siglist_add ( $certfile, $guid = `uuidgen` ) { | ||
| 85 | cmd( | ||
| 86 | [ | ||
| 87 | 'sign-efi-sig-list', '-g', $guid, $certfile, | ||
| 88 | ( $certfile =~ s/$CERTFILE_EXT_PTN/esl/r ) | ||
| 89 | ] | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub siglist_sign ( $certfile, $keyfile, $guid ) { | ||
| 94 | $certfile = path($certfile); | ||
| 95 | my $certbase = ( $certfile->basename =~ s/$CERTFILE_EXT_PTN//r ); | ||
| 96 | |||
| 97 | cmd( | ||
| 98 | [ | ||
| 99 | qw(sign-efi-sig-lsit -g), | ||
| 100 | $guid, '-k', $keyfile, '-c', $certfile, $certbase, "$certbase.esl", | ||
| 101 | "$certbase.auth" | ||
| 102 | ] | ||
| 103 | ); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub outpath ($subjbase) { | ||
| 107 | |||
| 108 | # : OUTDIR: = | ||
| 109 | # "$SBTOOL_ROOT/${SUBJBASE:=${NAME:-${SUBJBASE:-$(hostname)}}}_$(date +%s)" | ||
| 110 | |||
| 111 | # mkdirout = "$( | ||
| 112 | # perl -Mv54.40 -MCwd=abs_path -MData::Dumber -MPath::Tiny \ | ||
| 113 | # -e 'my $path = path($ARGV[0] =~ s/\//_/rg)->mkdir; warn Dumper(\@ARGV, $?, $!, $path) or $?; say $path;' \ | ||
| 114 | # "$OUTDIR " || exit $? | ||
| 115 | # )" | ||
| 116 | |||
| 117 | # echo "$mkdirout" | ||
| 118 | my $path = path( $subjbase =~ s/\//_/rg )->mkdir; | ||
| 119 | |||
| 120 | my $outdir = | ||
| 121 | first { $_ } | ||
| 122 | map { "$SBTOOL_ROOT/$_\_" . join "", gettimeofday } | ||
| 123 | ( $subjbase, hostname ); | ||
| 124 | |||
| 125 | $outdir; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub sbinit { | ||
| 129 | my $name; | ||
| 130 | |||
| 131 | my $subjbase = subj_common( | ||
| 132 | scalar @ARGV | ||
| 133 | ? sub { $name = $ARGV[0]; \@ARGV } | ||
| 134 | ->($name) | ||
| 135 | : sub { | ||
| 136 | say "▶ Enter a value for "; | ||
| 137 | join "", grep { $_ } map { chomp $_; $_ } (<>); | ||
| 138 | } | ||
| 139 | ->() | ||
| 140 | ); | ||
| 141 | |||
| 142 | my $outdir = outpath($subjbase); | ||
| 143 | mkdir $outdir unless -d $outdir; | ||
| 144 | mkdir "$outdir/priv" unless -d "$outdir/priv"; | ||
| 145 | $CWD = $outdir; | ||
| 146 | |||
| 147 | my $guid = `uuidgen`; | ||
| 148 | |||
| 149 | open my $fh, '>', ( $ENV{GUIDFILE} // 'myGUID.txt' ); | ||
| 150 | say $fh $guid; | ||
| 151 | close $fh; | ||
| 152 | |||
| 153 | foreach my $type (qw'PK KEK db') { | ||
| 154 | say "▶ Checking for existing $type Secure Boot material..."; | ||
| 155 | |||
| 156 | my $cert = $ENV{"${type}_CERTFILE"} // "$type.crt"; | ||
| 157 | my $key = $ENV{"${type}_KEYFILE"} // "priv/$type.key"; | ||
| 158 | |||
| 159 | if ( all { -f $_ } ( $cert, $key ) ) { | ||
| 160 | say STDERR "▶ Using existing keypair ($cert:$key)"; | ||
| 161 | next; | ||
| 162 | } | ||
| 163 | elsif ( -f $cert && !-f $key ) { | ||
| 164 | die "❌️: No valid key found at '$key'!"; | ||
| 165 | } | ||
| 166 | |||
| 167 | newreq( $cert, $key, $type, subjbasesubj_common => $subjbase ); | ||
| 168 | to_DER($cert); | ||
| 169 | siglist_add( $cert, $guid ); | ||
| 170 | siglist_sign( $cert, $key, $guid ); | ||
| 171 | } | ||
| 172 | |||
| 173 | my $noPK = $ENV{NOPKFILE} // 'noPK.esl'; | ||
| 174 | noPK($noPK) if -f $noPK; | ||
| 175 | } | ||
| 176 | |||
| 177 | sbinit() |