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
|
import os
import sys
import json
import time
import shutil
import logging
import unittest
import tempfile
from subprocess import Popen, PIPE
try:
from urllib.request import urlopen, Request # Python 3
except ImportError: # pragma: no cover
from urllib2 import urlopen, Request # Python 2
try:
from StringIO import StringIO # Python 2
except ImportError: # pragma: no cover
from io import StringIO # Python 3
import acme_tiny
from . import utils
# test settings based on environmental variables
PEBBLE_BIN = os.getenv("ACME_TINY_PEBBLE_BIN") or "{}/go/bin/pebble".format(os.getenv("HOME")) # default pebble install path
DOMAIN = os.getenv("ACME_TINY_DOMAIN") or "local.gethttpsforfree.com" # default to domain that resolves to 127.0.0.1
USE_STAGING = bool(os.getenv("ACME_TINY_USE_STAGING")) # default to false
SSHFS_CHALLENGE_DIR = os.getenv("ACME_TINY_SSHFS_CHALLENGE_DIR") # default to None (only used if USE_STAGING is True)
class TestModule(unittest.TestCase):
"""
Tests for acme_tiny.py functionality itself
"""
def setUp(self):
"""
Set up ACME server for each test (or use Let's Encrypt's staging server)
"""
# create new account keys every test
self.KEYS = utils.gen_keys(DOMAIN)
# use Let's Encrypt staging server
if USE_STAGING: # pragma: no cover
os.unsetenv("SSL_CERT_FILE") # use the default ssl trust store
# config references
self.tempdir = SSHFS_CHALLENGE_DIR
self.check_port = "80"
self.DIR_URL = "https://acme-staging-v02.api.letsencrypt.org/directory"
# staging server errors
self.account_key_error = "certificate public key must be different than account key"
self.ca_issued_string = "(STAGING) Let's Encrypt"
self.bad_character_error = "Domain name contains an invalid character"
# default to using pebble server
else:
# config references
self.tempdir = None # generated below
self.DIR_URL = "https://localhost:14000/dir"
self._pebble_server, self._pebble_config = utils.setup_pebble(PEBBLE_BIN)
self.check_port = str(self._pebble_config['pebble']['httpPort'])
self._challenge_file_server, self._base_tempdir, self.tempdir = utils.setup_local_fileserver(self.check_port, pebble_proc=self._pebble_server)
# pebble server errors
self.account_key_error = "CSR contains a public key for a known account"
self.ca_issued_string = "Pebble Intermediate CA"
self.bad_character_error = "Order included DNS identifier with a value containing an illegal character"
def tearDown(self):
"""
Shut down sub processes (pebble, etc.)
"""
# only need to shut down stuff if using local servers (pebble)
if not USE_STAGING:
self._pebble_server.terminate()
self._pebble_server.wait()
os.remove(self._pebble_config['pebble']['certificate'])
os.remove(self._pebble_config['pebble']['privateKey'])
self._challenge_file_server.terminate()
self._challenge_file_server.wait()
shutil.rmtree(self._base_tempdir)
def test_module_linecount(self):
""" This project is supposed to remain under 200 lines """
test_dir = os.path.dirname(os.path.realpath(__file__))
module_path = os.path.abspath(os.path.join(test_dir, os.pardir, "acme_tiny.py"))
out, err = Popen(["wc", "-l", module_path], stdout=PIPE, stderr=PIPE).communicate()
num_lines = int(out.decode("utf8").split(" ", 1)[0])
self.assertTrue(num_lines <= 200)
def test_success_domain(self):
""" Successfully issue a certificate via subject alt name """
old_stdout = sys.stdout
sys.stdout = StringIO()
result = acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['domain_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
sys.stdout.seek(0)
crt = sys.stdout.read().encode("utf8")
sys.stdout = old_stdout
out, err = Popen(["openssl", "x509", "-text", "-noout"], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate(crt)
self.assertIn(self.ca_issued_string, out.decode("utf8"))
def test_skip_valid_authorizations(self):
""" Authorizations that are already valid should be skipped """
# issue a valid cert
self.test_success_domain()
# add a logging handler that captures the info log output
log_output = StringIO()
debug_handler = logging.StreamHandler(log_output)
acme_tiny.LOGGER.addHandler(debug_handler)
# issue the cert again, where challenges should already be valid
old_stdout = sys.stdout
sys.stdout = StringIO()
result = acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['domain_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
sys.stdout.seek(0)
crt = sys.stdout.read().encode("utf8")
sys.stdout = old_stdout
log_output.seek(0)
log_string = log_output.read().encode("utf8")
# remove logging capture
acme_tiny.LOGGER.removeHandler(debug_handler)
# should say the domain is already verified
self.assertIn("Already verified: {0}, skipping...".format(DOMAIN), log_string.decode("utf8"))
def test_success_cli(self):
""" Successfully issue a certificate via command line interface """
crt, err = Popen([
"python", "acme_tiny.py",
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['domain_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
], stdout=PIPE, stderr=PIPE).communicate()
out, err = Popen(["openssl", "x509", "-text", "-noout"], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate(crt)
self.assertIn(self.ca_issued_string, out.decode("utf8"))
def test_missing_account_key(self):
""" OpenSSL throws an error when the account key is missing """
try:
result = acme_tiny.main([
"--account-key", "/foo/bar",
"--csr", self.KEYS['domain_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
except Exception as e:
result = e
self.assertIsInstance(result, IOError)
err_msgs = ["unable to load Private Key", "Could not open file or uri for loading private key"]
self.assertTrue(any(msg in result.args[0] for msg in err_msgs))
def test_missing_csr(self):
""" OpenSSL throws an error when the CSR is missing """
try:
result = acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", "/foo/bar",
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
except Exception as e:
result = e
self.assertIsInstance(result, IOError)
self.assertIn("Error loading /foo/bar", result.args[0])
def test_invalid_domain(self):
""" Let's Encrypt rejects invalid domains """
try:
result = acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['invalid_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
except Exception as e:
result = e
self.assertIsInstance(result, ValueError)
self.assertIn(self.bad_character_error, result.args[0])
def test_nonexistent_domain(self):
""" Should be unable verify a nonexistent domain """
try:
result = acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['nonexistent_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
except Exception as e:
result = e
self.assertIsInstance(result, ValueError)
self.assertIn("but couldn't download", result.args[0])
def test_account_key_domain(self):
""" Can't use the account key for the CSR """
try:
result = acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['account_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
except Exception as e:
result = e
self.assertIsInstance(result, ValueError)
self.assertIn(self.account_key_error, result.args[0])
def test_contact(self):
""" Make sure optional contact details can be set """
# add a logging handler that captures the info log output
log_output = StringIO()
debug_handler = logging.StreamHandler(log_output)
acme_tiny.LOGGER.addHandler(debug_handler)
# call acme_tiny with new contact details
old_stdout = sys.stdout
sys.stdout = StringIO()
result = acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['domain_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
"--contact", "mailto:devteam@gethttpsforfree.com", "mailto:boss@gethttpsforfree.com",
])
sys.stdout.seek(0)
crt = sys.stdout.read().encode("utf8")
sys.stdout = old_stdout
log_output.seek(0)
log_string = log_output.read().encode("utf8")
# make sure the certificate was issued and the contact details were updated
out, err = Popen(["openssl", "x509", "-text", "-noout"], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate(crt)
self.assertIn(self.ca_issued_string, out.decode("utf8"))
self.assertTrue(( # can be in either order
"Updated contact details:\nmailto:devteam@gethttpsforfree.com\nmailto:boss@gethttpsforfree.com" in log_string.decode("utf8")
or "Updated contact details:\nmailto:boss@gethttpsforfree.com\nmailto:devteam@gethttpsforfree.com" in log_string.decode("utf8")
))
# remove logging capture
acme_tiny.LOGGER.removeHandler(debug_handler)
def test_challenge_failure(self):
""" Raises error if challenge doesn't pass """
# man-in-the-middle ACME requests to modify valid challenges so we raise that exception
def urlopenMITM(*args, **kwargs):
resp = urlopenOriginal(*args, **kwargs)
resp._orig_read = resp.read()
# modify valid challenges and authorizations to invalid
try:
resp_json = json.loads(resp._orig_read.decode("utf8"))
if (
len(resp_json.get("challenges", [])) == 1
and resp_json['challenges'][0]['status'] == "valid"
and resp_json['status'] == "valid"
):
resp_json['challenges'][0]['status'] = "invalid"
resp_json['status'] = "invalid"
resp._orig_read = json.dumps(resp_json).encode("utf8")
except ValueError:
pass
# serve up modified response when read
def multi_read():
return resp._orig_read
resp.read = multi_read
return resp
# call acme-tiny with MITM'd urlopen
urlopenOriginal = acme_tiny.urlopen
acme_tiny.urlopen = urlopenMITM
try:
acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['domain_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
except ValueError as e:
result = e
acme_tiny.urlopen = urlopenOriginal
# should raise error that challenge didn't pass
self.assertIn("Challenge did not pass for", result.args[0])
def test_malicious_challenge_token(self):
""" Raises error if malicious challenge token is provided by the CA """
# assume the CA wants to try to fool you into serving up your password file
malicious_token = "../../../../etc/passwd"
cleaned_token = "____________etc_passwd"
# man-in-the-middle ACME requests to modify the challenge token to something malicious
def urlopenMITM(*args, **kwargs):
resp = urlopenOriginal(*args, **kwargs)
resp._orig_read = resp.read()
try:
resp_json = json.loads(resp._orig_read.decode("utf8"))
if len([c for c in resp_json.get("challenges", []) if c['type'] == "http-01"]) == 1:
challenge = [c for c in resp_json['challenges'] if c['type'] == "http-01"][0]
challenge['token'] = malicious_token
resp._orig_read = json.dumps(resp_json).encode("utf8")
except ValueError:
pass
# serve up modified response when read
def multi_read():
return resp._orig_read
resp.read = multi_read
return resp
# call acme-tiny with MITM'd urlopen
urlopenOriginal = acme_tiny.urlopen
acme_tiny.urlopen = urlopenMITM
try:
acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['domain_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
except ValueError as e:
result = e
acme_tiny.urlopen = urlopenOriginal
# should raise error that challenge didn't pass
self.assertIn("Challenge did not pass for", result.args[0])
# challenge file actually saved as a cleaned version
resp = urlopen(Request("http://{0}:{1}/.well-known/acme-challenge/{2}".format(DOMAIN, self.check_port, cleaned_token)))
token_data = resp.read().decode("utf8")
self.assertIn(cleaned_token, token_data)
def test_order_failure(self):
""" Raises error if order doesn't complete """
# man-in-the-middle ACME requests to modify valid orders so we raise that exception
def urlopenMITM(*args, **kwargs):
resp = urlopenOriginal(*args, **kwargs)
resp._orig_read = resp.read()
# modify valid orders to invalid
try:
resp_json = json.loads(resp._orig_read.decode("utf8"))
if (
resp_json.get("finalize", None) is not None
and resp_json.get("status", None) == "valid"
):
resp_json['status'] = "invalid"
resp._orig_read = json.dumps(resp_json).encode("utf8")
except ValueError:
pass
# serve up modified response when read
def multi_read():
return resp._orig_read
resp.read = multi_read
return resp
# call acme-tiny with MITM'd urlopen
urlopenOriginal = acme_tiny.urlopen
acme_tiny.urlopen = urlopenMITM
try:
acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['domain_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
except ValueError as e:
result = e
acme_tiny.urlopen = urlopenOriginal
# should raise error that challenge didn't pass
self.assertIn("Order failed", result.args[0])
###########################
## Pebble-specific tests ##
###########################
@unittest.skipIf(USE_STAGING, "only checked on pebble server since staging can't have nonce retries set")
def test_nonce_retry(self):
""" Still works when lots of nonce retries """
# kill current pebble server
self._pebble_server.terminate()
self._pebble_server.wait()
os.remove(self._pebble_config['pebble']['certificate'])
os.remove(self._pebble_config['pebble']['privateKey'])
# restart with new bad nonce rate
self._pebble_server, self._pebble_config = utils.setup_pebble(PEBBLE_BIN, bad_nonces=90)
# normal success test
self.test_success_domain()
@unittest.skipIf(USE_STAGING, "only checked on pebble server since ")
def test_pebble_doesnt_support_cn_domains(self):
""" Test that pebble server doesn't support CN subject domains """
try:
result = acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['cn_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
"--check-port", self.check_port,
])
except Exception as e:
result = e
self.assertIsInstance(result, ValueError)
self.assertIn("Order includes different number of DNSnames identifiers than CSR specifies", result.args[0])
############################
## Staging-specific tests ##
############################
@unittest.skipIf((not USE_STAGING), "only checked on staging since pebble doesn't support CN names")
def test_success_cn(self): # pragma: no cover
""" Successfully issue a certificate via common name """
old_stdout = sys.stdout
sys.stdout = StringIO()
result = acme_tiny.main([
"--account-key", self.KEYS['account_key'].name,
"--csr", self.KEYS['cn_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
#"--check-port", self.check_port, # defaults to port 80 anyway, so test that the default works
])
sys.stdout.seek(0)
crt = sys.stdout.read().encode("utf8")
sys.stdout = old_stdout
out, err = Popen(["openssl", "x509", "-text", "-noout"], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate(crt)
self.assertIn(self.ca_issued_string, out.decode("utf8"))
@unittest.skipIf((not USE_STAGING), "only checked on staging since pebble doesn't check for weak keys")
def test_weak_key(self): # pragma: no cover
""" Let's Encrypt rejects weak keys """
try:
result = acme_tiny.main([
"--account-key", self.KEYS['weak_key'].name,
"--csr", self.KEYS['domain_csr'].name,
"--acme-dir", self.tempdir,
"--directory-url", self.DIR_URL,
#"--check-port", self.check_port, # defaults to port 80 anyway, so test that the default works
])
except Exception as e:
result = e
self.assertIsInstance(result, ValueError)
err_msgs = ["key too small", "key size not supported"]
self.assertTrue(any(msg in result.args[0] for msg in err_msgs))
|