Files
@ c3e3f7ebf69d
Branch filter:
Location: gimmecert/functional_tests/test_key_specification.py
c3e3f7ebf69d
20.2 KiB
text/x-python
GC-37: Added ECDSA support for issuing server certificates via server command:
- Added functional test.
- Added unit tests.
- Updated existing functional test that checks for avertising of curve
support for key specification in the init command to be a bit less
fragile in case the output gets broken-up into different lines in a
slightly different location.
- Implement ability to get public key specification out of ECDSA
public key.
- Expose ECDSA key specification in the server command.
- Updated inline documentation.
- Added functional test.
- Added unit tests.
- Updated existing functional test that checks for avertising of curve
support for key specification in the init command to be a bit less
fragile in case the output gets broken-up into different lines in a
slightly different location.
- Implement ability to get public key specification out of ECDSA
public key.
- Expose ECDSA key specification in the server command.
- Updated inline documentation.
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 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2018 Branko Majic
#
# This file is part of Gimmecert.
#
# Gimmecert is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Gimmecert is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Gimmecert. If not, see <http://www.gnu.org/licenses/>.
#
from .base import run_command
def test_initialisation_with_rsa_private_key_specification(tmpdir):
# John is looking into improving the security of one of his
# projects. Amongst other things, John is interested in using
# stronger private keys for his TLS services - which he wants to
# try out in his test envioronment first.
# John knows that the Gimmecert tool uses 2048-bit RSA keys for
# the CA hierarchy, but what he would really like to do is specify
# himself what kind of private key should be generated
# instead. He checks-out the help for the init command first.
stdout, _, _ = run_command('gimmecert', 'init', '-h')
# John noticies there is an option to provide a custom key
# specification to the tool, that he can specify the length of
# the RSA private keys, and that the default is "rsa:2048".
assert "--key-specification" in stdout
assert " -k" in stdout
assert "rsa:BIT_LENGTH" in stdout
assert "Default is rsa:2048" in stdout
# John switches to his project directory.
tmpdir.chdir()
# He initalises the CA hierarchy, requesting to use 4096-bit RSA
# keys.
stdout, stderr, exit_code = run_command('gimmecert', 'init', '--key-specification', 'rsa:4096')
# Command finishes execution with success, and John notices that
# the tool has informed him of what the private key algorithm is
# in use for the CA hierarchy.
assert exit_code == 0
assert stderr == ""
assert "CA hierarchy initialised using 4096-bit RSA keys." in stdout
# John goes ahead and inspects the CA private key to ensure his
# private key specification has been accepted.
stdout, stderr, exit_code = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/ca/level1.key.pem')
assert exit_code == 0
assert stderr == ""
assert "Private-Key: (4096 bit)" in stdout
# John also does a quick check on the generated certificate's
# signing and public key algorithm.
stdout, stderr, exit_code = run_command('openssl', 'x509', '-noout', '-text', '-in', '.gimmecert/ca/level1.cert.pem')
assert exit_code == 0
assert stderr == ""
assert "Signature Algorithm: sha256WithRSAEncryption" in stdout
assert "Public-Key: (4096 bit)" in stdout
def test_server_command_default_key_specification_with_rsa(tmpdir):
# John is setting-up a project to test some functionality
# revolving around X.509 certificates. Since he does not care much
# about the strength of private keys for it, he wants to use
# 1024-bit RSA keys for both CA hierarchy and server certificates
# to speed-up the key generation process.
# He switches to his project directory, and initialises the CA
# hierarchy, requesting that 1024-bit RSA keys should be used.
tmpdir.chdir()
run_command("gimmecert", "init", "--key-specification", "rsa:1024")
# John issues a server certificate.
stdout, stderr, exit_code = run_command('gimmecert', 'server', 'myserver1')
# John observes that the process was completed successfully.
assert exit_code == 0
assert stderr == ""
# He runs a command to see details about the generated private
# key.
stdout, _, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/server/myserver1.key.pem')
# And indeed, the generated private key uses the same size as the
# one he specified for the CA hierarchy.
assert "Private-Key: (1024 bit)" in stdout
def test_server_command_key_specification_with_rsa(tmpdir):
# John is setting-up a project where he needs to test performance
# when using different RSA private key sizes.
# He switches to his project directory, and initialises the CA
# hierarchy, requesting that 3072-bit RSA keys should be used.
tmpdir.chdir()
run_command("gimmecert", "init", "--key-specification", "rsa:3072")
# Very soon he realizes that he needs to test performance using
# smaller RSA key sizes for proper comparison. He starts off by
# having a look at the help for the server command to see if there
# is an option that will satisfy his needs.
stdout, stderr, exit_code = run_command("gimmecert", "server", "-h")
# John notices the option for passing-in a key specification.
assert " --key-specification" in stdout
assert " -k" in stdout
# John goes ahead and tries to issue a server certificate using
# key specification option.
stdout, stderr, exit_code = run_command("gimmecert", "server", "--key-specification", "rsas:2048", "myserver1")
# Unfortunately, the command fails due to John's typo.
assert exit_code != 0
assert "invalid key_specification" in stderr
# John tries again, fixing his typo.
stdout, stderr, exit_code = run_command("gimmecert", "server", "--key-specification", "rsa:2048", "myserver1")
# This time around he succeeds.
assert exit_code == 0
assert stderr == ""
# He runs a command to see details about the generated private
# key.
stdout, _, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/server/myserver1.key.pem')
# He nods with his head, observing that the generated private key
# uses the same key size as he has specified.
assert "Private-Key: (2048 bit)" in stdout
def test_client_command_default_key_specification_with_rsa(tmpdir):
# John is setting-up a project to test some functionality
# revolving around X.509 certificates. Since he does not care much
# about the strength of private keys for it, he wants to use
# 1024-bit RSA keys for both CA hierarchy and client certificates
# to speed-up the key generation process.
# He switches to his project directory, and initialises the CA
# hierarchy, requesting that 1024-bit RSA keys should be used.
tmpdir.chdir()
run_command("gimmecert", "init", "--key-specification", "rsa:1024")
# John issues a client certificate.
stdout, stderr, exit_code = run_command('gimmecert', 'client', 'myclient1')
# John observes that the process was completed successfully.
assert exit_code == 0
assert stderr == ""
# He runs a command to see details about the generated private
# key.
stdout, _, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/client/myclient1.key.pem')
# And indeed, the generated private key uses the same size as the
# one he specified for the CA hierarchy.
assert "Private-Key: (1024 bit)" in stdout
def test_client_command_key_specification_with_rsa(tmpdir):
# John is setting-up a project where he needs to test performance
# when using different RSA private key sizes.
# He switches to his project directory, and initialises the CA
# hierarchy, requesting that 1024-bit RSA keys should be used.
tmpdir.chdir()
run_command("gimmecert", "init", "--key-specification", "rsa:1024")
# Very soon he realizes that he needs to test performance using
# smaller RSA key sizes for proper comparison. He starts off by
# having a look at the help for the client command to see if there
# is an option that will satisfy his needs.
stdout, stderr, exit_code = run_command("gimmecert", "client", "-h")
# John notices the option for passing-in a key specification.
assert " --key-specification" in stdout
assert " -k" in stdout
# John goes ahead and tries to issue a client certificate using
# key specification option.
stdout, stderr, exit_code = run_command("gimmecert", "client", "--key-specification", "rsas:2048", "myclient1")
# Unfortunately, the command fails due to John's typo.
assert exit_code != 0
assert "invalid key_specification" in stderr
# John tries again, fixing his typo.
stdout, stderr, exit_code = run_command("gimmecert", "client", "--key-specification", "rsa:2048", "myclient1")
# This time around he succeeds.
assert exit_code == 0
assert stderr == ""
# He runs a command to see details about the generated private
# key.
stdout, _, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/client/myclient1.key.pem')
# He nods with his head, observing that the generated private key
# uses the same key size as he has specified.
assert "Private-Key: (2048 bit)" in stdout
def test_renew_command_key_specification_with_rsa(tmpdir):
# John has set-up a project where he has issued a couple of
# certificates. For some of them he has used externally-generated
# private keys.
tmpdir.chdir()
run_command("openssl", "req", "-newkey", "rsa:3072", "-nodes", "-keyout", "myserver2.key.pem",
"-new", "-subj", "/CN=myserver2", "-out", "myserver2.csr.pem")
run_command("openssl", "req", "-newkey", "rsa:3072", "-nodes", "-keyout", "myclient2.key.pem",
"-new", "-subj", "/CN=myclient2", "-out", "myclient2.csr.pem")
run_command("gimmecert", "init")
run_command('gimmecert', 'server', 'myserver1')
run_command('gimmecert', 'client', 'myclient1')
run_command("gimmecert", "server", "--csr", "myserver2.csr.pem", "myserver2")
run_command("gimmecert", "client", "--csr", "myclient2.csr.pem", "myclient2")
# After some testing he realises that he needs to perform some
# tests using a different RSA key size. John has a look at the
# renew command options to see if he can request new private keys
# to be generated with different key sizes.
stdout, stderr, exit_code = run_command("gimmecert", "renew", "-h")
# John notices the option for passing-in custom key specification.
assert " --key-specification" in stdout
assert " -k" in stdout
# He tries to renew the server certificate, specifying the desired
# RSA key size.
stdout, stderr, exit_code = run_command("gimmecert", "renew", "server", "--key-specification", "rsa:1024", "myserver1")
# Gimmecert informs him that the key specification option can only
# be used when requesting a new private key to be generated as
# well.
assert exit_code != 0
assert "argument --key-specification/-k: must be used with --new-private-key/-p" in stderr
# John updates his command to include the additional option.
stdout, stderr, exit_code = run_command("gimmecert", "renew", "server", "--new-private-key", "--key-specification", "rsa:1024", "-p", "myserver1")
# Command suceeds.
assert exit_code == 0
assert stderr == ""
# He checks the details about the generated private key, and
# disovers that Gimmecert generated the key according to his
# wishes.
stdout, _, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/server/myserver1.key.pem')
assert "Private-Key: (1024 bit)" in stdout
# John goes ahead and performs a similar operation for his client
# entity.
stdout, stderr, exit_code = run_command("gimmecert", "renew", "client", "-k", "rsa:1024", "-p", "myclient1")
assert exit_code == 0
assert stderr == ""
# And once again, Gimmecert has created the key with correct size.
stdout, _, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/client/myclient1.key.pem')
assert "Private-Key: (1024 bit)" in stdout
# After some further testing, John decides to renew the
# certificates that have been issued using a CSR. He requests new
# private keys to be generated as well.
stdout, stderr, exit_code = run_command("gimmecert", "renew", "server", "-p", "myserver1")
assert exit_code == 0
assert stderr == ""
stdout, stderr, exit_code = run_command("gimmecert", "renew", "client", "-p", "myclient1")
assert exit_code == 0
assert stderr == ""
# John is unsure if the same key specification has been used,
# however. So he goes ahead and has a look at the server key.
stdout, _, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/server/myserver1.key.pem')
# The renew command has used the same key specification for the
# new private key as for the old private key.
assert "Private-Key: (1024 bit)" in stdout
# He performs the same check on the client key.
stdout, _, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/client/myclient1.key.pem')
# The renew command has used the same key specification for the
# new private key as for the old private key.
assert "Private-Key: (1024 bit)" in stdout
# After using his manually generated private keys for a while,
# John accidentally deletes them from his managed machine. Instead
# of redoing the whole process with CSRs, he decides to simply
# regenerate the private keys and certificates and copy them over.
run_command('gimmecert', 'renew', 'server', '--new-private-key', 'myserver2')
run_command('gimmecert', 'renew', 'client', '--new-private-key', 'myclient2')
# John realizes that the original private keys he generated used
# 3072-bit RSA, while the CA hierarchy uses 2048-bit RSA. He
# decides to check if the generated key ended-up using CA
# hierarchy defaults, or the same key size he used when generating
# the keys manually.
#
# He checks the server private key, and everything seems right -
# same key size is used as in case of the old private key.
stdout, stderr, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/server/myserver2.key.pem')
assert "Private-Key: (3072 bit)" in stdout
# Then he has a look at the client private key, and that one is
# also using the same key size as the old private key.
stdout, _, _ = run_command('openssl', 'rsa', '-noout', '-text', '-in', '.gimmecert/client/myclient2.key.pem')
assert "Private-Key: (3072 bit)" in stdout
def test_initialisation_with_ecdsa_key_specification(tmpdir):
# John is looking into using ECDSA keys in his latest project. He
# is already aware that Gimmecert supports use of RSA keys, but he
# hasn't tried using it with ECDSA yet.
# He checks the help for the init command first to see if he can
# somehow request ECDSA keys to be used instead of RSA.
stdout, _, _ = run_command('gimmecert', 'init', '-h')
# John noticies there is an option to provide a custom key
# specification to the tool, and that he can request ECDSA keys to
# be used with a specific curve.
assert "--key-specification" in stdout
assert " -k" in stdout
assert "rsa:BIT_LENGTH" in stdout
assert "ecdsa:CURVE_NAME" in stdout
# John can see a number of curves listed as supported.
assert "curves: " in stdout
assert "secp192r1" in stdout
assert "secp224r1" in stdout
assert "secp256k1" in stdout
assert "secp256r1" in stdout
assert "secp384r1" in stdout
assert "secp521r1" in stdout
# John switches to his project directory.
tmpdir.chdir()
# After a short deliberation, he opts to use the secp256r1 curve,
# and initialises his CA hierarchy.
stdout, stderr, exit_code = run_command('gimmecert', 'init', '--key-specification', 'ecdsa:secp256r1')
# Command finishes execution with success, and John notices that
# the tool has informed him of what the private key algorithm is
# in use for the CA hierarchy.
assert exit_code == 0
assert stderr == ""
assert "CA hierarchy initialised using secp256r1 ECDSA keys." in stdout
# John goes ahead and inspects the CA private key to ensure his
# private key specification has been accepted.
stdout, stderr, exit_code = run_command('openssl', 'ec', '-noout', '-text', '-in', '.gimmecert/ca/level1.key.pem')
assert exit_code == 0
assert stderr == "read EC key\n" # OpenSSL print this out to stderr no matter what.
# He notices that although he requested secp256r1, the output from
# OpenSSL tool uses its older name from RFC3279 -
# prime256v1. However, he understands this is just an alternate
# name for the curve.
assert "ASN1 OID: prime256v1" in stdout
# John also does a quick check on the generated certificate's
# signing and public key algorithm.
stdout, stderr, exit_code = run_command('openssl', 'x509', '-noout', '-text', '-in', '.gimmecert/ca/level1.cert.pem')
assert exit_code == 0
assert stderr == ""
assert "Signature Algorithm: ecdsa-with-SHA256" in stdout
assert "Public Key Algorithm: id-ecPublicKey" in stdout
assert "ASN1 OID: prime256v1" in stdout
def test_server_command_default_key_specification_with_ecdsa(tmpdir):
# John is setting-up a project to test some functionality
# revolving around X.509 certificates. He has used RSA extensively
# before, but now he wants to switch to using ECDSA private keys
# instead.
# He switches to his project directory, and initialises the CA
# hierarchy, requesting that secp256r1 ECDSA keys should be used.
tmpdir.chdir()
run_command("gimmecert", "init", "--key-specification", "ecdsa:secp384r1")
# John issues a server certificate.
stdout, stderr, exit_code = run_command('gimmecert', 'server', 'myserver1')
# John observes that the process was completed successfully.
assert exit_code == 0
assert stderr == ""
# He runs a command to see details about the generated private
# key.
stdout, _, _ = run_command('openssl', 'ec', '-noout', '-text', '-in', '.gimmecert/server/myserver1.key.pem')
# And indeed, the generated private key uses the same algorithm as
# the one he specified for the CA hierarchy.
assert "ASN1 OID: secp384r1" in stdout
def test_server_command_key_specification_with_ecdsa(tmpdir):
# John is setting-up a project where he needs to test performance
# when using different ECDSA private key sizes.
# He switches to his project directory, and initialises the CA
# hierarchy, requesting that secp192r1 ECDSA keys should be used.
tmpdir.chdir()
run_command("gimmecert", "init", "--key-specification", "ecdsa:secp192r1")
# Very soon he realizes that he needs to test performance using
# different elliptic curve algorithms for proper comparison. He
# starts off by having a look at the help for the server command
# to see if there is an option that will satisfy his needs.
stdout, stderr, exit_code = run_command("gimmecert", "server", "-h")
# John notices the option for passing-in a key specification, and
# that he can request ECDSA keys to be used with a specific curve.
assert " --key-specification" in stdout
assert " -k" in stdout
assert "rsa:BIT_LENGTH" in stdout
assert "ecdsa:CURVE_NAME" in stdout
# John can see a number of curves listed as supported.
assert "curves: " in stdout
assert "secp192r1" in stdout
assert "secp224r1" in stdout
assert "secp256k1" in stdout
assert "secp256r1" in stdout
assert "secp384r1" in stdout
assert "secp521r1" in stdout
# John goes ahead and tries to issue a server certificate using
# key specification option.
stdout, stderr, exit_code = run_command("gimmecert", "server", "--key-specification", "ecdsa:secp224r11", "myserver1")
# Unfortunately, the command fails due to John's typo.
assert exit_code != 0
assert "invalid key_specification" in stderr
# John tries again, fixing his typo.
stdout, stderr, exit_code = run_command("gimmecert", "server", "--key-specification", "ecdsa:secp224r1", "myserver1")
# This time around he succeeds.
assert exit_code == 0
assert stderr == ""
# He runs a command to see details about the generated private
# key.
stdout, _, _ = run_command('openssl', 'ec', '-noout', '-text', '-in', '.gimmecert/server/myserver1.key.pem')
# He nods with his head, observing that the generated private key
# uses the same algorithm as he has specified.
assert "ASN1 OID: secp224r1" in stdout
|