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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.2 -->
<HTML>
<HEAD>
<TITLE>Creating Certificates</TITLE>
<SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="4"><!-- Empty --></A>
<H2>4 Creating Certificates</H2>
<P>Here we consider the creation of example certificates.
<A NAME="4.1"><!-- Empty --></A>
<H3>4.1 The openssl Command</H3>
<P>The <CODE>openssl</CODE> command is a utility that comes with the
OpenSSL distribution. It provides a variety of subcommands. Each
subcommand is invoked as
<PRE>
openssl subcmd <options and arguments>
</PRE>
<P>where <CODE>subcmd</CODE> denotes the subcommand in question.
<P>We shall use the following subcommands to create certificates for
the purpose of testing Erlang/OTP SSL:
<P>
<UL>
<LI>
<STRONG>req</STRONG> to create certificate requests and a
self-signed certificates,
</LI>
<LI>
<STRONG>ca</STRONG> to create certificates from certificate requests.
</LI>
</UL>
<P>We create the following certificates:
<P>
<UL>
<LI>
the <STRONG>erlangCA</STRONG> root certificate (a self-signed
certificate),
</LI>
<LI>
the <STRONG>otpCA</STRONG> certificate signed by the <STRONG>erlangCA</STRONG>,
</LI>
<LI>
a client certificate signed by the <STRONG>otpCA</STRONG>, and
</LI>
<LI>
a server certificate signed by the <STRONG>otpCA</STRONG>.
</LI>
</UL>
<A NAME="4.1.1"><!-- Empty --></A>
<H4>4.1.1 The openssl configuration file</H4>
<P>An <CODE>openssl</CODE> configuration file consist of a number of
sections, where each section starts with one line containing
<CODE>[ section_name ]</CODE>, where <CODE>section_name</CODE> is the name
of the section. The first section of the file is either
unnamed, or is named <CODE>[ default ]</CODE>. For further details
see the OpenSSL config(5) manual page.
<P>The required sections for the subcommands we are going to
use are as follows:
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
<CAPTION ALIGN=BOTTOM><EM>openssl subcommands to use</EM></CAPTION>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
subcommand
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
required/default section
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
override command line option
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
configuration file option
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
req
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
[req]
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
-
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>-config FILE</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
ca
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
[ca]
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>-name section</CODE>
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>-config FILE</CODE>
</TD>
</TR>
</TABLE>
</CENTER>
<A NAME="4.1.2"><!-- Empty --></A>
<H4>4.1.2 Creating the Erlang root CA</H4>
<P>The Erlang root CA is created with the command
<PRE>
openssl req -new -x509 -config /some/path/req.cnf \
-keyout /some/path/key.pem -out /some/path/cert.pem
</PRE>
<P>where the option <CODE>-new</CODE> indicates that we want to create
a new certificate request and the option <CODE>-x509</CODE> implies
that a self-signed certificate is created.
<A NAME="4.1.3"><!-- Empty --></A>
<H4>4.1.3 Creating the OTP CA</H4>
<P>The OTP CA is created by first creating a certificate request
with the command
<PRE>
openssl req -new -config /some/path/req.cnf \
-keyout /some/path/key.pem -out /some/path/req.pem
</PRE>
<P>and the ask the Erlang CA to sign it:
<PRE>
openssl ca -batch -notext -config /some/path/req.cnf \
-extensions ca_cert -in /some/path/req.pem -out /some/path/cert.pem
</PRE>
<P>where the option <CODE>-extensions</CODE> refers to a section in the
configuration file saying that it should create a CA certificate,
and not a plain user certificate.
<P>The <CODE>client</CODE> and <CODE>server</CODE> certificates are created
similarly, except that the option <CODE>-extensions</CODE> then has the
value <CODE>user_cert</CODE>.
<A NAME="4.2"><!-- Empty --></A>
<H3>4.2 An Example</H3>
<P>The following module <CODE>create_certs</CODE> is used by the Erlang/OTP
SSL application for generating certificates to be used in tests. The
source code is also found in <CODE>ssl-X.Y.Z/examples/certs/src</CODE>.
<P>The purpose of the <CODE>create_certs:all/1</CODE> function is to make
it possible to provide from the <CODE>erl</CODE> command line, the
full path name of the <CODE>openssl</CODE> command.
<P>Note that the module creates temporary OpenSSL configuration files
for the <CODE>req</CODE> and <CODE>ca</CODE> subcommands.
<PRE>
%% The purpose of this module is to create example certificates for
%% testing.
%% Run it as:
%%
%% erl -noinput -run make_certs all "/path/to/openssl" -s erlang halt
%%
-module(make_certs).
-export([all/0, all/1]).
-record(dn, {commonName,
organizationalUnitName = "Erlang OTP",
organizationName = "Ericsson AB",
localityName = "Stockholm",
countryName = "SE",
emailAddress = "peter@erix.ericsson.se"}).
all() ->
all(["openssl"]).
all([OpenSSLCmd]) ->
Root = filename:dirname(filename:dirname((code:which(?MODULE)))),
%% io:fwrite("Root : ~s~n", [Root]),
NRoot = filename:join([Root, "etc"]),
file:make_dir(NRoot),
create_rnd(Root, "etc"), % For all requests
rootCA(NRoot, OpenSSLCmd, "erlangCA"),
intermediateCA(NRoot, OpenSSLCmd, "otpCA", "erlangCA"),
endusers(NRoot, OpenSSLCmd, "otpCA", ["client", "server"]),
collect_certs(NRoot, ["erlangCA", "otpCA"], ["client", "server"]),
remove_rnd(Root, "etc").
rootCA(Root, OpenSSLCmd, Name) ->
create_ca_dir(Root, Name, ca_cnf(Name)),
DN = #dn{commonName = Name},
create_self_signed_cert(Root, OpenSSLCmd, Name, req_cnf(DN)),
ok.
intermediateCA(Root, OpenSSLCmd, CA, ParentCA) ->
CA = "otpCA",
create_ca_dir(Root, CA, ca_cnf(CA)),
CARoot = filename:join([Root, CA]),
DN = #dn{commonName = CA},
CnfFile = filename:join([CARoot, "req.cnf"]),
file:write_file(CnfFile, req_cnf(DN)),
KeyFile = filename:join([CARoot, "private", "key.pem"]),
ReqFile = filename:join([CARoot, "req.pem"]),
create_req(Root, OpenSSLCmd, CnfFile, KeyFile, ReqFile),
CertFile = filename:join([CARoot, "cert.pem"]),
sign_req(Root, OpenSSLCmd, ParentCA, "ca_cert", ReqFile, CertFile).
endusers(Root, OpenSSLCmd, CA, Users) ->
lists:foreach(fun(User) -> enduser(Root, OpenSSLCmd, CA, User) end, Users).
enduser(Root, OpenSSLCmd, CA, User) ->
UsrRoot = filename:join([Root, User]),
file:make_dir(UsrRoot),
CnfFile = filename:join([UsrRoot, "req.cnf"]),
DN = #dn{commonName = User},
file:write_file(CnfFile, req_cnf(DN)),
KeyFile = filename:join([UsrRoot, "key.pem"]),
ReqFile = filename:join([UsrRoot, "req.pem"]),
create_req(Root, OpenSSLCmd, CnfFile, KeyFile, ReqFile),
CertFile = filename:join([UsrRoot, "cert.pem"]),
sign_req(Root, OpenSSLCmd, CA, "user_cert", ReqFile, CertFile).
collect_certs(Root, CAs, Users) ->
Bins = lists:foldr(
fun(CA, Acc) ->
File = filename:join([Root, CA, "cert.pem"]),
{ok, Bin} = file:read_file(File),
[Bin, "\n" | Acc]
end, [], CAs),
lists:foreach(
fun(User) ->
File = filename:join([Root, User, "cacerts.pem"]),
file:write_file(File, Bins)
end, Users).
create_self_signed_cert(Root, OpenSSLCmd, CAName, Cnf) ->
CARoot = filename:join([Root, CAName]),
CnfFile = filename:join([CARoot, "req.cnf"]),
file:write_file(CnfFile, Cnf),
KeyFile = filename:join([CARoot, "private", "key.pem"]),
CertFile = filename:join([CARoot, "cert.pem"]),
Cmd = [OpenSSLCmd, " req"
" -new"
" -x509"
" -config ", CnfFile,
" -keyout ", KeyFile,
" -out ", CertFile],
Env = [{"ROOTDIR", Root}],
cmd(Cmd, Env).
create_ca_dir(Root, CAName, Cnf) ->
CARoot = filename:join([Root, CAName]),
file:make_dir(CARoot),
create_dirs(CARoot, ["certs", "crl", "newcerts", "private"]),
create_rnd(Root, filename:join([CAName, "private"])),
create_files(CARoot, [{"serial", "01\n"},
{"index.txt", ""},
{"ca.cnf", Cnf}]).
create_req(Root, OpenSSLCmd, CnfFile, KeyFile, ReqFile) ->
Cmd = [OpenSSLCmd, " req"
" -new"
" -config ", CnfFile,
" -keyout ", KeyFile,
" -out ", ReqFile],
Env = [{"ROOTDIR", Root}],
cmd(Cmd, Env).
sign_req(Root, OpenSSLCmd, CA, CertType, ReqFile, CertFile) ->
CACnfFile = filename:join([Root, CA, "ca.cnf"]),
Cmd = [OpenSSLCmd, " ca"
" -batch"
" -notext"
" -config ", CACnfFile,
" -extensions ", CertType,
" -in ", ReqFile,
" -out ", CertFile],
Env = [{"ROOTDIR", Root}],
cmd(Cmd, Env).
%%
%% Misc
%%
create_dirs(Root, Dirs) ->
lists:foreach(fun(Dir) ->
file:make_dir(filename:join([Root, Dir])) end,
Dirs).
create_files(Root, NameContents) ->
lists:foreach(
fun({Name, Contents}) ->
file:write_file(filename:join([Root, Name]), Contents) end,
NameContents).
create_rnd(Root, Dir) ->
From = filename:join([Root, "rnd", "RAND"]),
To = filename:join([Root, Dir, "RAND"]),
file:copy(From, To).
remove_rnd(Root, Dir) ->
File = filename:join([Root, Dir, "RAND"]),
file:delete(File).
cmd(Cmd, Env) ->
FCmd = lists:flatten(Cmd),
Port = open_port({spawn, FCmd}, [stream, eof, exit_status,
{env, Env}]),
eval_cmd(Port).
eval_cmd(Port) ->
receive
{Port, {data, _}} ->
eval_cmd(Port);
{Port, eof} ->
ok
end,
receive
{Port, {exit_status, Status}} when Status /= 0 ->
%% io:fwrite("exit status: ~w~n", [Status]),
erlang:halt(Status)
after 0 ->
ok
end.
%%
%% Contents of configuration files
%%
req_cnf(DN) ->
["# Purpose: Configuration for requests (end users and CAs)."
"\n"
"ROOTDIR = $ENV::ROOTDIR\n"
"\n"
"[req]\n"
"input_password = secret\n"
"output_password = secret\n"
"default_bits = 1024\n"
"RANDFILE = $ROOTDIR/RAND\n"
"encrypt_key = no\n"
"default_md = sha1\n"
"#string_mask = pkix\n"
"x509_extensions = ca_ext\n"
"prompt = no\n"
"distinguished_name= name\n"
"\n"
"[name]\n"
"commonName = ", DN#dn.commonName, "\n"
"organizationalUnitName = ", DN#dn.organizationalUnitName, "\n"
"organizationName = ", DN#dn.organizationName, "\n"
"localityName = ", DN#dn.localityName, "\n"
"countryName = ", DN#dn.countryName, "\n"
"emailAddress = ", DN#dn.emailAddress, "\n"
"\n"
"[ca_ext]\n"
"basicConstraints = critical, CA:true\n"
"keyUsage = cRLSign, keyCertSign\n"
"subjectKeyIdentifier = hash\n"
"subjectAltName = email:copy\n"].
ca_cnf(CA) ->
["# Purpose: Configuration for CAs.\n"
"\n"
"ROOTDIR = $ENV::ROOTDIR\n"
"default_ca = ca\n"
"\n"
"[ca]\n"
"dir = $ROOTDIR/", CA, "\n"
"certs = $dir/certs\n"
"crl_dir = $dir/crl\n"
"database = $dir/index.txt\n"
"new_certs_dir = $dir/newcerts\n"
"certificate = $dir/cert.pem\n"
"serial = $dir/serial\n"
"crl = $dir/crl.pem\n"
"private_key = $dir/private/key.pem\n"
"RANDFILE = $dir/private/RAND\n"
"\n"
"x509_extensions = user_cert\n"
"default_days = 3600\n"
"default_md = sha1\n"
"preserve = no\n"
"policy = policy_match\n"
"\n"
"[policy_match]\n"
"commonName = supplied\n"
"organizationalUnitName = optional\n"
"organizationName = match\n"
"countryName = match\n"
"localityName = match\n"
"emailAddress = supplied\n"
"\n"
"[user_cert]\n"
"basicConstraints = CA:false\n"
"keyUsage = nonRepudiation, digitalSignature, keyEncipherment\n"
"subjectKeyIdentifier = hash\n"
"authorityKeyIdentifier = keyid,issuer:always\n"
"subjectAltName = email:copy\n"
"issuerAltName = issuer:copy\n"
"\n"
"[ca_cert]\n"
"basicConstraints = critical,CA:true\n"
"keyUsage = cRLSign, keyCertSign\n"
"subjectKeyIdentifier = hash\n"
"authorityKeyIdentifier = keyid:always,issuer:always\n"
"subjectAltName = email:copy\n"
"issuerAltName = issuer:copy\n"].
</PRE>
<CENTER>
<HR>
<SMALL>
Copyright © 1991-2004
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|