You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
9.0 KiB

9 years ago
  1. # flake8: noqa
  2. # -*- coding: utf-8 -*-
  3. import argparse, subprocess, json, os, sys, base64, binascii, time, hashlib, re, copy, textwrap, logging
  4. try:
  5. from urllib.request import urlopen # Python 3
  6. except ImportError:
  7. from urllib2 import urlopen # Python 2
  8. #DEFAULT_CA = "https://acme-staging.api.letsencrypt.org"
  9. DEFAULT_CA = "https://acme-v01.api.letsencrypt.org"
  10. LOGGER = logging.getLogger(__name__)
  11. LOGGER.addHandler(logging.StreamHandler())
  12. LOGGER.setLevel(logging.INFO)
  13. def get_crt(account_key, csr, acme_dir, log=LOGGER, CA=DEFAULT_CA):
  14. # helper function base64 encode for jose spec
  15. def _b64(b):
  16. return base64.urlsafe_b64encode(b).decode('utf8').replace("=", "")
  17. # parse account key to get public key
  18. log.info("Parsing account key...")
  19. proc = subprocess.Popen(["openssl", "rsa", "-in", account_key, "-noout", "-text"],
  20. stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  21. out, err = proc.communicate()
  22. if proc.returncode != 0:
  23. raise IOError("OpenSSL Error: {0}".format(err))
  24. pub_hex, pub_exp = re.search(
  25. r"modulus:\n\s+00:([a-f0-9\:\s]+?)\npublicExponent: ([0-9]+)",
  26. out.decode('utf8'), re.MULTILINE|re.DOTALL).groups()
  27. pub_exp = "{0:x}".format(int(pub_exp))
  28. pub_exp = "0{0}".format(pub_exp) if len(pub_exp) % 2 else pub_exp
  29. header = {
  30. "alg": "RS256",
  31. "jwk": {
  32. "e": _b64(binascii.unhexlify(pub_exp.encode("utf-8"))),
  33. "kty": "RSA",
  34. "n": _b64(binascii.unhexlify(re.sub(r"(\s|:)", "", pub_hex).encode("utf-8"))),
  35. },
  36. }
  37. accountkey_json = json.dumps(header['jwk'], sort_keys=True, separators=(',', ':'))
  38. thumbprint = _b64(hashlib.sha256(accountkey_json.encode('utf8')).digest())
  39. # helper function make signed requests
  40. def _send_signed_request(url, payload):
  41. payload64 = _b64(json.dumps(payload).encode('utf8'))
  42. protected = copy.deepcopy(header)
  43. protected["nonce"] = urlopen(CA + "/directory").headers['Replay-Nonce']
  44. protected64 = _b64(json.dumps(protected).encode('utf8'))
  45. proc = subprocess.Popen(["openssl", "dgst", "-sha256", "-sign", account_key],
  46. stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  47. out, err = proc.communicate("{0}.{1}".format(protected64, payload64).encode('utf8'))
  48. if proc.returncode != 0:
  49. raise IOError("OpenSSL Error: {0}".format(err))
  50. data = json.dumps({
  51. "header": header, "protected": protected64,
  52. "payload": payload64, "signature": _b64(out),
  53. })
  54. try:
  55. resp = urlopen(url, data.encode('utf8'))
  56. return resp.getcode(), resp.read()
  57. except IOError as e:
  58. return getattr(e, "code", None), getattr(e, "read", e.__str__)()
  59. # find domains
  60. log.info("Parsing CSR...")
  61. proc = subprocess.Popen(["openssl", "req", "-in", csr, "-noout", "-text"],
  62. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  63. out, err = proc.communicate()
  64. if proc.returncode != 0:
  65. raise IOError("Error loading {0}: {1}".format(csr, err))
  66. domains = set([])
  67. common_name = re.search(r"Subject:.*? CN=([^\s,;/]+)", out.decode('utf8'))
  68. if common_name is not None:
  69. domains.add(common_name.group(1))
  70. subject_alt_names = re.search(r"X509v3 Subject Alternative Name: \n +([^\n]+)\n", out.decode('utf8'), re.MULTILINE|re.DOTALL)
  71. if subject_alt_names is not None:
  72. for san in subject_alt_names.group(1).split(", "):
  73. if san.startswith("DNS:"):
  74. domains.add(san[4:])
  75. # get the certificate domains and expiration
  76. log.info("Registering account...")
  77. code, result = _send_signed_request(CA + "/acme/new-reg", {
  78. "resource": "new-reg",
  79. "agreement": "https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf",
  80. })
  81. if code == 201:
  82. log.info("Registered!")
  83. elif code == 409:
  84. log.info("Already registered!")
  85. else:
  86. raise ValueError("Error registering: {0} {1}".format(code, result))
  87. # verify each domain
  88. for domain in domains:
  89. log.info("Verifying {0}...".format(domain))
  90. # get new challenge
  91. code, result = _send_signed_request(CA + "/acme/new-authz", {
  92. "resource": "new-authz",
  93. "identifier": {"type": "dns", "value": domain},
  94. })
  95. if code != 201:
  96. raise ValueError("Error requesting challenges: {0} {1}".format(code, result))
  97. # make the challenge file
  98. challenge = [c for c in json.loads(result.decode('utf8'))['challenges'] if c['type'] == "http-01"][0]
  99. token = re.sub(r"[^A-Za-z0-9_\-]", "_", challenge['token'])
  100. keyauthorization = "{0}.{1}".format(token, thumbprint)
  101. wellknown_path = os.path.join(acme_dir, token)
  102. with open(wellknown_path, "w") as wellknown_file:
  103. wellknown_file.write(keyauthorization)
  104. # check that the file is in place
  105. wellknown_url = "http://{0}/.well-known/acme-challenge/{1}".format(domain, token)
  106. try:
  107. resp = urlopen(wellknown_url)
  108. resp_data = resp.read().decode('utf8').strip()
  109. assert resp_data == keyauthorization
  110. except (IOError, AssertionError):
  111. os.remove(wellknown_path)
  112. raise ValueError("Wrote file to {0}, but couldn't download {1}".format(
  113. wellknown_path, wellknown_url))
  114. # notify challenge are met
  115. code, result = _send_signed_request(challenge['uri'], {
  116. "resource": "challenge",
  117. "keyAuthorization": keyauthorization,
  118. })
  119. if code != 202:
  120. raise ValueError("Error triggering challenge: {0} {1}".format(code, result))
  121. # wait for challenge to be verified
  122. while True:
  123. try:
  124. resp = urlopen(challenge['uri'])
  125. challenge_status = json.loads(resp.read().decode('utf8'))
  126. except IOError as e:
  127. raise ValueError("Error checking challenge: {0} {1}".format(
  128. e.code, json.loads(e.read().decode('utf8'))))
  129. if challenge_status['status'] == "pending":
  130. time.sleep(2)
  131. elif challenge_status['status'] == "valid":
  132. log.info("{0} verified!".format(domain))
  133. os.remove(wellknown_path)
  134. break
  135. else:
  136. raise ValueError("{0} challenge did not pass: {1}".format(
  137. domain, challenge_status))
  138. # get the new certificate
  139. log.info("Signing certificate...")
  140. proc = subprocess.Popen(["openssl", "req", "-in", csr, "-outform", "DER"],
  141. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  142. csr_der, err = proc.communicate()
  143. code, result = _send_signed_request(CA + "/acme/new-cert", {
  144. "resource": "new-cert",
  145. "csr": _b64(csr_der),
  146. })
  147. if code != 201:
  148. raise ValueError("Error signing certificate: {0} {1}".format(code, result))
  149. # return signed certificate!
  150. log.info("Certificate signed!")
  151. return """-----BEGIN CERTIFICATE-----\n{0}\n-----END CERTIFICATE-----\n""".format(
  152. "\n".join(textwrap.wrap(base64.b64encode(result).decode('utf8'), 64)))
  153. def main(argv):
  154. parser = argparse.ArgumentParser(
  155. formatter_class=argparse.RawDescriptionHelpFormatter,
  156. description=textwrap.dedent("""\
  157. This script automates the process of getting a signed TLS certificate from
  158. Let's Encrypt using the ACME protocol. It will need to be run on your server
  159. and have access to your private account key, so PLEASE READ THROUGH IT! It's
  160. only ~200 lines, so it won't take long.
  161. ===Example Usage===
  162. python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir /usr/share/nginx/html/.well-known/acme-challenge/ > signed.crt
  163. ===================
  164. ===Example Crontab Renewal (once per month)===
  165. 0 0 1 * * python /path/to/acme_tiny.py --account-key /path/to/account.key --csr /path/to/domain.csr --acme-dir /usr/share/nginx/html/.well-known/acme-challenge/ > /path/to/signed.crt 2>> /var/log/acme_tiny.log
  166. ==============================================
  167. """)
  168. )
  169. parser.add_argument("--account-key", required=True, help="path to your Let's Encrypt account private key")
  170. parser.add_argument("--csr", required=True, help="path to your certificate signing request")
  171. parser.add_argument("--acme-dir", required=True, help="path to the .well-known/acme-challenge/ directory")
  172. parser.add_argument("--quiet", action="store_const", const=logging.ERROR, help="suppress output except for errors")
  173. parser.add_argument("--ca", default=DEFAULT_CA, help="certificate authority, default is Let's Encrypt")
  174. args = parser.parse_args(argv)
  175. LOGGER.setLevel(args.quiet or LOGGER.level)
  176. signed_crt = get_crt(args.account_key, args.csr, args.acme_dir, log=LOGGER, CA=args.ca)
  177. sys.stdout.write(signed_crt)
  178. if __name__ == "__main__": # pragma: no cover
  179. main(sys.argv[1:])