/usr/bin/opendkim-genkey is in opendkim-tools 2.11.0~alpha-10+deb9u1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | #!/usr/bin/perl
##
## Copyright (c) 2005-2008 Sendmail, Inc. and its suppliers.
## All rights reserved.
##
## Copyright (c) 2009-2013, The Trusted Domain Project. All rights reserved.
##
## opendkim-genkey -- generate a key and/or TXT record for DKIM service
##
## The output of this script includes two files based on the
## selector name:
##
## <selector>.private PEM-formatted private key, for use by
## opendkim when signing messages
##
## <selector>.txt A DNS TXT record suitable for insertion
## into or inclusion by a DNS zone file
## in order to publish the public key
## for retrieval by verifiers
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
##
## Set up defaults
##
# Debian patch to set default to 2048 based on 2016 NIST recommendations:
my $bits = 2048;
my $domain = "localhost";
my $version = "localhost";
my $outdir = ".";
my $nosubdomains = 0;
my $subdomains = 1;
# Debian patch to use sha256 only based on 2016 NIST recommendations:
my $hashalgs = "sha256";
my $note;
my $flags = "";
my $selector = "default";
my $restricted = 0;
my $testmode = 0;
my $verbose = 0;
my $append = 0;
my $showversion = 0;
my $keyin;
my $status;
my $txtout;
my $keydata;
my $helponly;
my $hashout;
my $noteout;
my $comment;
my $len;
my $cur;
my $domstr;
my $progname = basename($0);
##
## Usage message
##
sub usage
{
print STDERR "$progname: usage: $progname [options]\n";
print STDERR "\t--append-domain include domain name in zone file stub\n";
print STDERR "\t--bits=n use n bits to generate the key\n";
print STDERR "\t--directory=path leave output in the named directory\n";
print STDERR "\t--domain=name generate data for the named domain [$domain]\n";
print STDERR "\t--hash-algorithms=list limit to use of the named algorithm(s)\n";
print STDERR "\t--help print help and exit\n";
print STDERR "\t--note=string include specified note in zone data\n";
print STDERR "\t--restrict restrict key to email use only\n";
print STDERR "\t--selector=name selector name [$selector]\n";
print STDERR "\t--subdomains allow signing of subdomains\n";
print STDERR "\t--testmode indicate key is in test mode\n";
print STDERR "\t--verbose increased output\n";
print STDERR "\t--version print version and exit\n";
}
##
## Argument processing
##
# parse command line arguments
Getopt::Long::Configure("bundling");
my $opt_retval = &Getopt::Long::GetOptions('a|append-domain!' => \$append,
'b|bits=i' => \$bits,
'D|directory=s' => \$outdir,
'd|domain=s' => \$domain,
'h|hash-algorithms=s' => \$hashalgs,
'help!' => \$helponly,
'n|note=s' => \$note,
'r|restrict!' => \$restricted,
's|selector=s' => \$selector,
'S!' => \$nosubdomains,
'subdomains!' => \$subdomains,
't|testmode!' => \$testmode,
'v|verbose+' => \$verbose,
'V|version!' => \$showversion,
);
if (!$opt_retval || $helponly)
{
usage();
if ($helponly)
{
exit(0);
}
else
{
exit(1);
}
}
if ($showversion)
{
print STDOUT "$progname v2.11.0\n";
exit 0;
}
## do this securely and in the right place
chdir($outdir) or die "$progname: $outdir: chdir(): $!";;
umask(077);
## generate a private key
if ($verbose >= 1)
{
print STDERR "$progname: generating private key\n";
}
if ($bits < 1024)
{
print STDERR "$progname: WARNING: RFC6376 advises minimum 1024-bit keys\n";
}
$status = system("openssl genrsa -out " . $selector . ".private " . $bits . " > /dev/null 2>&1");
if ($status != 0)
{
if ($? & 127)
{
print STDERR "$progname: openssl died with signal %d\n",
($? & 127);
exit(1);
}
else
{
print STDERR "$progname: openssl exited with status %d\n",
($? >> 8);
exit(1);
}
}
if ($verbose)
{
print STDERR "$progname: private key written to " . $selector . ".private\n";
}
## generate a public key based on the private key
if ($verbose)
{
print STDERR "$progname: extracting public key\n";
}
$status = system("openssl rsa -in " . $selector . ".private -pubout -out " . $selector . ".public -outform PEM > /dev/null 2>&1");
if ($status != 0)
{
if ($? & 127)
{
print STDERR "$progname: openssl died with signal %d\n",
($? & 127);
exit(1);
}
else
{
print STDERR "$progname: openssl exited with status %d\n",
($? >> 8);
exit(1);
}
}
if (!open($keyin, "<", $selector . ".public"))
{
print STDERR "$progname: unable to read from " . $selector . ".public: $!\n";
exit(1);
}
while (<$keyin>)
{
chomp;
if ($_ =~ /^-/)
{
next;
}
$keydata .= $_;
}
close($keyin);
## output the record
if ($testmode)
{
$flags = "t=y;";
}
if ($nosubdomains)
{
$subdomains = 0;
}
if (!$subdomains)
{
if ($flags eq "t=y;")
{
$flags = "t=y:s;";
}
else
{
$flags = "t=s;";
}
}
if ($restricted)
{
if ($flags ne "")
{
$flags .= " ";
}
$flags .= "s=email;";
}
if ($flags ne "")
{
$flags .= " ";
}
$hashout = "";
if (defined($hashalgs))
{
$hashout = " h=$hashalgs;";
}
$noteout = "";
if (defined($note))
{
$noteout = " n=\\\"$note\\\";";
}
$domstr = "";
if ($append)
{
$domstr = "." . $domain . ".";
}
if ($domain ne "")
{
$comment = " ; ----- DKIM key $selector for $domain"
}
else
{
$comment = "";
}
if (!open($txtout, ">", $selector . ".txt"))
{
print STDERR "$progname: unable to write from " . $selector . ".txt: $!\n";
exit(1);
}
print $txtout $selector . "._domainkey" . ${domstr} . "\tIN\tTXT\t( \"v=DKIM1;" . $noteout . $hashout . " k=rsa; " . $flags . "\"\n\t \"p=";
$len = length($keydata);
$cur = 0;
while ($len > 0)
{
if ($len < 250)
{
print $txtout substr($keydata, $cur);
$len = 0;
}
else
{
print $txtout substr($keydata, $cur, 250);
print $txtout "\"\n\t \"";
$cur += 250;
$len -= 250;
}
}
print $txtout "\" ) " . $comment . "\n";
close($txtout);
if ($verbose)
{
print STDERR "$progname: DNS TXT record written to " . $selector . ".txt\n";
}
## all done!
unlink($selector . ".public");
exit(0);
|