3 #### ACME client (only http-01 challenges supported thus far)
5 import sys, os, getopt, binascii, json, pprint, signal, time, calendar, threading
10 class msgerror(Exception):
11 def report(self, out):
12 out.write("acmecert: undefined error\n")
15 return binascii.b2a_base64(dat).decode("us-ascii").translate({43: 45, 47: 95, 61: None}).strip()
19 if len(h) % 2 == 1: h = "0" + h
20 return base64url(binascii.a2b_hex(h))
22 class maybeopen(object):
23 def __init__(self, name, mode):
31 raise ValueError(mode)
34 self.fp = open(name, mode)
39 def __exit__(self, *excinfo):
49 if _cryptobke is None:
50 from cryptography.hazmat import backends
51 _cryptobke = backends.default_backend()
54 class dererror(Exception):
57 class pemerror(Exception):
60 def pemdec(pem, ptypes):
61 if isinstance(ptypes, str):
65 p = pem.find("-----BEGIN ", p)
67 raise pemerror("could not find any %s in PEM-encoded data" % (ptypes,))
68 p2 = pem.find("-----", p + 11)
70 raise pemerror("incomplete PEM header")
71 ptype = pem[p + 11 : p2]
72 if ptype not in ptypes:
75 p3 = pem.find("-----END " + ptype + "-----", p2 + 5)
77 raise pemerror("incomplete PEM data")
78 pem = pem[p2 + 5 : p3]
79 return binascii.a2b_base64(pem)
81 class derdecoder(object):
82 def __init__(self, data, offset=0, size=None):
85 self.size = len(data) if size is None else size
88 return self.offset >= self.size
91 if self.offset >= self.size:
92 raise dererror("unexpected end-of-data")
93 ret = self.data[self.offset]
98 if self.offset + ln > self.size:
99 raise dererror("unexpected end-of-data")
100 ret = self.data[self.offset : self.offset + ln]
107 cons = (h & 0x20) != 0
110 raise dererror("extended type tags not supported")
118 raise dererror("indefinite lengths not supported in DER")
120 raise dererror("invalid length byte")
124 ret = (ret << 8) + self.byte()
128 cl, cons, tag = self.dectag()
130 return cons, cl, tag, self.splice(ln)
132 def getcons(self, ckcl, cktag):
133 cons, cl, tag, data = self.get()
135 raise dererror("expected constructed value")
136 if (ckcl != None and ckcl != cl) or (cktag != None and cktag != tag):
137 raise dererror("unexpected value tag: got (%d, %d), expected (%d, %d)" % (cl, tag, ckcl, cktag))
138 return derdecoder(data)
141 cons, cl, tag, data = self.get()
142 if (cons, cl, tag) == (False, 0, 2):
147 raise dererror("unexpected integer type: (%s, %d, %d)" % (cons, cl, tag))
150 cons, cl, tag, data = self.get()
151 if (cons, cl, tag) == (False, 0, 12):
152 return data.decode("utf-8")
153 if (cons, cl, tag) == (False, 0, 13):
154 return data.decode("us-ascii")
155 if (cons, cl, tag) == (False, 0, 22):
156 return data.decode("us-ascii")
157 if (cons, cl, tag) == (False, 0, 30):
158 return data.decode("utf-16-be")
159 raise dererror("unexpected string type: (%s, %d, %d)" % (cons, cl, tag))
162 cons, cl, tag, data = self.get()
163 if (cons, cl, tag) == (False, 0, 4):
165 raise dererror("unexpected byte-string type: (%s, %d, %d)" % (cons, cl, tag))
168 cons, cl, tag, data = self.get()
169 if (cons, cl, tag) == (False, 0, 6):
171 ret.append(data[0] // 40)
172 ret.append(data[0] % 40)
179 n = (n + (v & 0x7f)) * 128
185 raise dererror("unexpected object-id type: (%s, %d, %d)" % (cons, cl, tag))
188 def parsetime(data, c):
194 y += 1900 if y > 50 else 2000
200 if data[:1].isdigit():
205 if data[:1].isdigit():
212 while len(data) < p and data[p].isdigit():
214 S += float("0." + data[1:p])
217 raise dererror("unspecified local time not supported for decoding")
221 tz = (int(data[1:3]) * 60) + int(data[3:5])
223 tz = -((int(data[1:3]) * 60) + int(data[3:5]))
225 raise dererror("cannot parse X.690 timestamp")
226 return calendar.timegm((y, m, d, H, M, S)) - (tz * 60)
229 cons, cl, tag, data = self.get()
230 if (cons, cl, tag) == (False, 0, 23):
231 return self.parsetime(data.decode("us-ascii"), False)
232 if (cons, cl, tag) == (False, 0, 24):
233 return self.parsetime(data.decode("us-ascii"), True)
234 raise dererror("unexpected time type: (%s, %d, %d)" % (cons, cl, tag))
237 def frompem(cls, pem, ptypes):
238 return cls(pemdec(pem, ptypes))
240 class certificate(object):
241 def __init__(self, der):
242 ci = der.getcons(0, 16).getcons(0, 16)
243 self.ver = ci.getcons(2, 0).getint()
244 self.serial = ci.getint()
245 ci.getcons(0, 16) # Signature algorithm
246 ci.getcons(0, 16) # Issuer
247 vl = ci.getcons(0, 16)
248 self.startdate = vl.gettime()
249 self.enddate = vl.gettime()
251 def expiring(self, timespec):
252 if timespec.endswith("y"):
253 timespec = int(timespec[:-1]) * 365 * 86400
254 elif timespec.endswith("m"):
255 timespec = int(timespec[:-1]) * 30 * 86400
256 elif timespec.endswith("w"):
257 timespec = int(timespec[:-1]) * 7 * 86400
258 elif timespec.endswith("d"):
259 timespec = int(timespec[:-1]) * 86400
260 elif timespec.endswith("h"):
261 timespec = int(timespec[:-1]) * 3600
263 timespec = int(timespec)
264 return (self.enddate - time.time()) < timespec
268 return cls(derdecoder.frompem(fp.read(), {"CERTIFICATE", "X509 CERTIFICATE"}))
270 class signreq(object):
271 def __init__(self, der):
273 req = derdecoder(der).getcons(0, 16).getcons(0, 16)
274 self.ver = req.getint()
275 req.getcons(0, 16) # Subject
276 req.getcons(0, 16) # Public key
279 attrs = req.getcons(2, 0)
280 while not attrs.end():
281 attr = attrs.getcons(0, 16)
283 if anm == (1, 2, 840, 113549, 1, 9, 14):
284 # Certificate extension request
285 exts = attr.getcons(0, 17).getcons(0, 16)
286 while not exts.end():
287 ext = exts.getcons(0, 16)
289 if extnm == (2, 5, 29, 17):
290 # Subject alternative names
291 names = derdecoder(ext.getbytes()).getcons(0, 16)
292 while not names.end():
293 cons, cl, tag, data = names.get()
294 if (cons, cl, tag) == (False, 2, 2):
295 self.altnames.append(("DNS", data.decode("us-ascii")))
298 return [nm[1] for nm in self.altnames if nm[0] == "DNS"]
305 return cls(pemdec(fp.read(), {"CERTIFICATE REQUEST"}))
307 ### Somewhat general request utilities
310 with urllib.request.urlopen(directory()["newNonce"]) as resp:
312 return resp.headers["Replay-Nonce"]
314 def req(url, data=None, ctype=None, headers={}, method=None, **kws):
315 if data is not None and not isinstance(data, bytes):
316 data = json.dumps(data).encode("utf-8")
317 ctype = "application/jose+json"
318 req = urllib.request.Request(url, data=data, method=method)
319 for hnam, hval in headers.items():
320 req.add_header(hnam, hval)
321 if ctype is not None:
322 req.add_header("Content-Type", ctype)
323 return urllib.request.urlopen(req)
325 class problem(msgerror):
326 def __init__(self, code, data, *args, url=None, **kw):
327 super().__init__(*args, **kw)
331 if not isinstance(data, dict):
332 raise ValueError("unexpected problem object type: %r" % (data,))
336 return self.data.get("type", "about:blank")
339 return self.data.get("title")
342 return self.data.get("detail")
344 def report(self, out):
346 if self.title is None:
349 extra, msg = msg, None
354 msg = self.data.get("type")
356 out.write("acemcert: %s: %s\n" % (
357 ("remote service error" if self.url is None else self.url),
358 ("unspecified error" if msg is None else msg)))
359 if extra is not None:
360 out.write("%s\n" % (extra,))
363 def read(cls, err, **kw):
364 self = cls(err.code, json.loads(err.read().decode("utf-8")), **kw)
367 def jreq(url, data, auth):
368 authdata = {"alg": "RS256", "url": url, "nonce": getnonce()}
369 authdata.update(auth.authdata())
370 authdata = base64url(json.dumps(authdata).encode("us-ascii"))
374 data = base64url(json.dumps(data).encode("us-ascii"))
375 seal = base64url(auth.sign(("%s.%s" % (authdata, data)).encode("us-ascii")))
376 enc = {"protected": authdata, "payload": data, "signature": seal}
378 with req(url, data=enc) as resp:
379 return json.loads(resp.read().decode("utf-8")), resp.headers
380 except urllib.error.HTTPError as exc:
381 if exc.headers["Content-Type"] == "application/problem+json":
382 raise problem.read(exc, url=url)
387 class jwkauth(object):
388 def __init__(self, key):
392 pub = self.key.public_key().public_numbers()
393 return {"jwk": {"kty": "RSA", "e": ebignum(pub.e), "n": ebignum(pub.n)}}
395 def sign(self, data):
396 from cryptography.hazmat.primitives import hashes
397 from cryptography.hazmat.primitives.asymmetric import padding
398 return self.key.sign(data, padding.PKCS1v15(), hashes.SHA256())
400 class account(object):
401 def __init__(self, uri, key):
406 return {"kid": self.uri}
408 def sign(self, data):
409 from cryptography.hazmat.primitives import hashes
410 from cryptography.hazmat.primitives.asymmetric import padding
411 return self.key.sign(data, padding.PKCS1v15(), hashes.SHA256())
414 data, headers = jreq(self.uri, None, self)
418 data = self.getinfo()
419 if data.get("status", "") != "valid":
420 raise Exception("account is not valid: %s" % (data.get("status", "\"\"")))
422 def write(self, out):
423 from cryptography.hazmat.primitives import serialization
424 out.write("%s\n" % (self.uri,))
425 out.write("%s\n" % (self.key.private_bytes(
426 encoding=serialization.Encoding.PEM,
427 format=serialization.PrivateFormat.TraditionalOpenSSL,
428 encryption_algorithm=serialization.NoEncryption()
429 ).decode("us-ascii"),))
433 from cryptography.hazmat.primitives import serialization
436 raise Exception("missing account URI")
438 key = serialization.load_pem_private_key(fp.read().encode("us-ascii"), password=None, backend=cryptobke())
443 service = "https://acme-v02.api.letsencrypt.org/directory"
447 if _directory is None:
448 with req(service) as resp:
449 _directory = json.loads(resp.read().decode("utf-8"))
452 def register(keysize=4096):
453 from cryptography.hazmat.primitives.asymmetric import rsa
454 key = rsa.generate_private_key(public_exponent=65537, key_size=keysize, backend=cryptobke())
455 data, headers = jreq(directory()["newAccount"], {"termsOfServiceAgreed": True}, jwkauth(key))
456 return account(headers["Location"], key)
458 def mkorder(acct, csr):
459 data, headers = jreq(directory()["newOrder"], {"identifiers": [{"type": "dns", "value": dn} for dn in csr.domains()]}, acct)
460 data["acmecert.location"] = headers["Location"]
463 def httptoken(acct, ch):
464 from cryptography.hazmat.primitives import hashes
465 pub = acct.key.public_key().public_numbers()
466 jwk = {"kty": "RSA", "e": ebignum(pub.e), "n": ebignum(pub.n)}
467 dig = hashes.Hash(hashes.SHA256(), backend=cryptobke())
468 dig.update(json.dumps(jwk, separators=(',', ':'), sort_keys=True).encode("us-ascii"))
469 khash = base64url(dig.finalize())
470 return ch["token"], ("%s.%s" % (ch["token"], khash))
472 def finalize(acct, csr, orderid):
473 order, headers = jreq(orderid, None, acct)
474 if order["status"] == "valid":
476 elif order["status"] == "ready":
477 jreq(order["finalize"], {"csr": base64url(csr.der())}, acct)
479 resp, headers = jreq(orderid, None, acct)
480 if resp["status"] == "processing":
482 elif resp["status"] == "valid":
486 raise Exception("unexpected order status when finalizing: %s" % resp["status"])
488 raise Exception("order finalization timed out")
490 raise Exception("unexpected order state when finalizing: %s" % (order["status"],))
491 with req(order["certificate"]) as resp:
492 return resp.read().decode("us-ascii")
496 class htconfig(object):
505 if len(words) < 1 or ln[0] == '#':
507 if words[0] == "root":
508 self.roots[words[1]] = words[2]
510 sys.stderr.write("acmecert: warning: unknown htconfig directive: %s\n" % (words[0]))
513 def authorder(acct, htconf, orderid):
514 order, headers = jreq(orderid, None, acct)
521 raise Exception("challenges refuse to become valid even after 5 retries")
522 for authuri in order["authorizations"]:
523 auth, headers = jreq(authuri, None, acct)
524 if auth["status"] == "valid":
526 elif auth["status"] == "pending":
529 raise Exception("unknown authorization status: %s" % (auth["status"],))
531 if auth["identifier"]["type"] != "dns":
532 raise Exception("unknown authorization type: %s" % (auth["identifier"]["type"],))
533 dn = auth["identifier"]["value"]
534 if dn not in htconf.roots:
535 raise Exception("no configured ht-root for domain name %s" % (dn,))
536 for ch in auth["challenges"]:
537 if ch["type"] == "http-01":
540 raise Exception("no http-01 challenge for %s" % (dn,))
541 root = htconf.roots[dn]
542 tokid, tokval = httptoken(acct, ch)
543 tokpath = os.path.join(root, tokid);
544 fp = open(tokpath, "w")
548 with req("http://%s/.well-known/acme-challenge/%s" % (dn, tokid)) as resp:
549 if resp.read().decode("utf-8") != tokval:
550 raise Exception("challenge from %s does not match written value" % (dn,))
552 resp, headers = jreq(ch["url"], {}, acct)
553 if resp["status"] == "processing":
555 elif resp["status"] == "pending":
556 # I don't think this should happen, but it
557 # does. LE bug? Anyway, just retry.
562 elif resp["status"] == "valid":
565 raise Exception("unexpected challenge status for %s when validating: %s" % (dn, resp["status"]))
567 raise Exception("challenge processing timed out for %s" % (dn,))
571 ### Invocation and commands
573 invdata = threading.local()
576 class usageerr(msgerror):
578 self.cmd = invdata.cmd
580 def report(self, out):
581 out.write("%s\n" % (self.cmd.__doc__,))
586 "usage: acmecert reg [OUTPUT-FILE]"
589 with maybeopen(args[1] if len(args) > 1 else "-", "w") as fp:
591 commands["reg"] = cmd_reg
593 def cmd_validate_acct(args):
594 "usage: acmecert validate-acct ACCOUNT-FILE"
595 if len(args) < 2: raise usageerr()
596 with maybeopen(args[1], "r") as fp:
597 account.read(fp).validate()
598 commands["validate-acct"] = cmd_validate_acct
600 def cmd_acct_info(args):
601 "usage: acmecert acct-info ACCOUNT-FILE"
602 if len(args) < 2: raise usageerr()
603 with maybeopen(args[1], "r") as fp:
604 pprint.pprint(account.read(fp).getinfo())
605 commands["acct-info"] = cmd_acct_info
608 "usage: acmecert order ACCOUNT-FILE CSR [OUTPUT-FILE]"
609 if len(args) < 3: raise usageerr()
610 with maybeopen(args[1], "r") as fp:
611 acct = account.read(fp)
612 with maybeopen(args[2], "r") as fp:
613 csr = signreq.read(fp)
614 order = mkorder(acct, csr)
615 with maybeopen(args[3] if len(args) > 3 else "-", "w") as fp:
616 fp.write("%s\n" % (order["acmecert.location"]))
617 commands["order"] = cmd_order
619 def cmd_http_auth(args):
620 "usage: acmecert http-auth ACCOUNT-FILE HTTP-CONFIG {ORDER-ID|ORDER-FILE}"
621 if len(args) < 4: raise usageerr()
622 with maybeopen(args[1], "r") as fp:
623 acct = account.read(fp)
624 with maybeopen(args[2], "r") as fp:
625 htconf = htconfig.read(fp)
629 with maybeopen(args[3], "r") as fp:
630 orderid = fp.readline().strip()
631 authorder(acct, htconf, orderid)
632 commands["http-auth"] = cmd_http_auth
635 "usage: acmecert get ACCOUNT-FILE CSR {ORDER-ID|ORDER-FILE}"
636 if len(args) < 4: raise usageerr()
637 with maybeopen(args[1], "r") as fp:
638 acct = account.read(fp)
639 with maybeopen(args[2], "r") as fp:
640 csr = signreq.read(fp)
644 with maybeopen(args[3], "r") as fp:
645 orderid = fp.readline().strip()
646 sys.stdout.write(finalize(acct, csr, orderid))
647 commands["get"] = cmd_get
649 def cmd_http_order(args):
650 "usage: acmecert http-order ACCOUNT-FILE CSR HTTP-CONFIG [OUTPUT-FILE]"
651 if len(args) < 4: raise usageerr()
652 with maybeopen(args[1], "r") as fp:
653 acct = account.read(fp)
654 with maybeopen(args[2], "r") as fp:
655 csr = signreq.read(fp)
656 with maybeopen(args[3], "r") as fp:
657 htconf = htconfig.read(fp)
658 orderid = mkorder(acct, csr)["acmecert.location"]
659 authorder(acct, htconf, orderid)
660 with maybeopen(args[4] if len(args) > 4 else "-", "w") as fp:
661 fp.write(finalize(acct, csr, orderid))
662 commands["http-order"] = cmd_http_order
664 def cmd_check_cert(args):
665 "usage: acmecert check-cert CERT-FILE TIME-SPEC"
666 if len(args) < 3: raise usageerr()
667 with maybeopen(args[1], "r") as fp:
668 crt = certificate.read(fp)
669 sys.exit(1 if crt.expiring(args[2]) else 0)
670 commands["check-cert"] = cmd_check_cert
672 def cmd_directory(args):
673 "usage: acmecert directory"
674 pprint.pprint(directory())
675 commands["directory"] = cmd_directory
680 out.write("usage: acmecert [-D SERVICE] COMMAND [ARGS...]\n")
681 out.write(" acmecert -h [COMMAND]\n")
682 buf = " COMMAND is any of: "
685 if len(buf) + len(cmd) > 70:
686 out.write("%s\n" % (buf,))
694 out.write("%s\n" % (buf,))
698 opts, args = getopt.getopt(argv[1:], "hD:")
702 cmd = commands.get(args[0])
704 sys.stderr.write("acmecert: unknown command: %s\n" % (args[0],))
706 sys.stdout.write("%s\n" % (cmd.__doc__,))
715 cmd = commands.get(args[0])
717 sys.stderr.write("acmecert: unknown command: %s\n" % (args[0],))
726 except msgerror as exc:
727 exc.report(sys.stderr)
730 if __name__ == "__main__":
733 except KeyboardInterrupt:
734 signal.signal(signal.SIGINT, signal.SIG_DFL)
735 os.kill(os.getpid(), signal.SIGINT)