2021-03-08 16:49:10 +00:00
|
|
|
""" signs activitypub activities """
|
2020-05-18 18:42:22 +00:00
|
|
|
import hashlib
|
2020-05-14 13:24:37 +00:00
|
|
|
from urllib.parse import urlparse
|
2020-05-20 14:26:01 +00:00
|
|
|
import datetime
|
2020-05-14 13:24:37 +00:00
|
|
|
from base64 import b64encode, b64decode
|
|
|
|
|
|
|
|
from Crypto import Random
|
|
|
|
from Crypto.PublicKey import RSA
|
2021-03-08 16:49:10 +00:00
|
|
|
from Crypto.Signature import pkcs1_15 # pylint: disable=no-name-in-module
|
2020-05-14 13:24:37 +00:00
|
|
|
from Crypto.Hash import SHA256
|
|
|
|
|
2020-05-20 14:26:01 +00:00
|
|
|
MAX_SIGNATURE_AGE = 300
|
2020-05-14 13:24:37 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-05-14 13:24:37 +00:00
|
|
|
def create_key_pair():
|
2021-04-26 16:15:42 +00:00
|
|
|
"""a new public/private key pair, used for creating new users"""
|
2020-05-14 13:24:37 +00:00
|
|
|
random_generator = Random.new().read
|
|
|
|
key = RSA.generate(1024, random_generator)
|
2021-03-08 16:49:10 +00:00
|
|
|
private_key = key.export_key().decode("utf8")
|
|
|
|
public_key = key.publickey().export_key().decode("utf8")
|
2020-05-14 13:24:37 +00:00
|
|
|
|
|
|
|
return private_key, public_key
|
|
|
|
|
|
|
|
|
2020-05-19 20:33:47 +00:00
|
|
|
def make_signature(sender, destination, date, digest):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""uses a private key to sign an outgoing message"""
|
2020-05-14 13:24:37 +00:00
|
|
|
inbox_parts = urlparse(destination)
|
|
|
|
signature_headers = [
|
2021-03-08 16:49:10 +00:00
|
|
|
"(request-target): post %s" % inbox_parts.path,
|
|
|
|
"host: %s" % inbox_parts.netloc,
|
|
|
|
"date: %s" % date,
|
|
|
|
"digest: %s" % digest,
|
2020-05-14 13:24:37 +00:00
|
|
|
]
|
2021-03-08 16:49:10 +00:00
|
|
|
message_to_sign = "\n".join(signature_headers)
|
2020-12-01 03:53:42 +00:00
|
|
|
signer = pkcs1_15.new(RSA.import_key(sender.key_pair.private_key))
|
2021-03-08 16:49:10 +00:00
|
|
|
signed_message = signer.sign(SHA256.new(message_to_sign.encode("utf8")))
|
2020-05-14 13:24:37 +00:00
|
|
|
signature = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"keyId": "%s#main-key" % sender.remote_id,
|
|
|
|
"algorithm": "rsa-sha256",
|
|
|
|
"headers": "(request-target) host date digest",
|
|
|
|
"signature": b64encode(signed_message).decode("utf8"),
|
2020-05-14 13:24:37 +00:00
|
|
|
}
|
2021-03-08 16:49:10 +00:00
|
|
|
return ",".join('%s="%s"' % (k, v) for (k, v) in signature.items())
|
2020-05-14 13:24:37 +00:00
|
|
|
|
2020-09-21 17:25:26 +00:00
|
|
|
|
2020-05-19 20:33:47 +00:00
|
|
|
def make_digest(data):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""creates a message digest for signing"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return "SHA-256=" + b64encode(hashlib.sha256(data.encode("utf-8")).digest()).decode(
|
|
|
|
"utf-8"
|
|
|
|
)
|
2020-09-21 17:25:26 +00:00
|
|
|
|
2020-05-19 20:33:47 +00:00
|
|
|
|
2020-05-18 18:42:22 +00:00
|
|
|
def verify_digest(request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""checks if a digest is syntactically valid and matches the message"""
|
2021-03-08 16:49:10 +00:00
|
|
|
algorithm, digest = request.headers["digest"].split("=", 1)
|
|
|
|
if algorithm == "SHA-256":
|
2020-05-18 18:42:22 +00:00
|
|
|
hash_function = hashlib.sha256
|
2021-03-08 16:49:10 +00:00
|
|
|
elif algorithm == "SHA-512":
|
2020-05-18 18:42:22 +00:00
|
|
|
hash_function = hashlib.sha512
|
|
|
|
else:
|
2020-05-19 20:33:47 +00:00
|
|
|
raise ValueError("Unsupported hash function: {}".format(algorithm))
|
2020-05-18 18:42:22 +00:00
|
|
|
|
|
|
|
expected = hash_function(request.body).digest()
|
|
|
|
if b64decode(digest) != expected:
|
2020-08-19 12:53:17 +00:00
|
|
|
raise ValueError("Invalid HTTP Digest header")
|
2020-05-14 13:24:37 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-05-14 13:24:37 +00:00
|
|
|
class Signature:
|
2021-04-26 16:15:42 +00:00
|
|
|
"""read and validate incoming signatures"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-05-14 13:24:37 +00:00
|
|
|
def __init__(self, key_id, headers, signature):
|
|
|
|
self.key_id = key_id
|
|
|
|
self.headers = headers
|
|
|
|
self.signature = signature
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def parse(cls, request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""extract and parse a signature from an http request"""
|
2020-05-14 13:24:37 +00:00
|
|
|
signature_dict = {}
|
2021-03-08 16:49:10 +00:00
|
|
|
for pair in request.headers["Signature"].split(","):
|
|
|
|
k, v = pair.split("=", 1)
|
|
|
|
v = v.replace('"', "")
|
2020-05-14 13:24:37 +00:00
|
|
|
signature_dict[k] = v
|
|
|
|
|
|
|
|
try:
|
2021-03-08 16:49:10 +00:00
|
|
|
key_id = signature_dict["keyId"]
|
|
|
|
headers = signature_dict["headers"]
|
|
|
|
signature = b64decode(signature_dict["signature"])
|
2020-05-14 13:24:37 +00:00
|
|
|
except KeyError:
|
2021-03-08 16:49:10 +00:00
|
|
|
raise ValueError("Invalid auth header")
|
2020-05-14 13:24:37 +00:00
|
|
|
|
|
|
|
return cls(key_id, headers, signature)
|
|
|
|
|
|
|
|
def verify(self, public_key, request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""verify rsa signature"""
|
2021-03-08 16:49:10 +00:00
|
|
|
if http_date_age(request.headers["date"]) > MAX_SIGNATURE_AGE:
|
|
|
|
raise ValueError("Request too old: %s" % (request.headers["date"],))
|
2020-05-14 13:24:37 +00:00
|
|
|
public_key = RSA.import_key(public_key)
|
|
|
|
|
|
|
|
comparison_string = []
|
2021-03-08 16:49:10 +00:00
|
|
|
for signed_header_name in self.headers.split(" "):
|
|
|
|
if signed_header_name == "(request-target)":
|
|
|
|
comparison_string.append("(request-target): post %s" % request.path)
|
2020-05-14 13:24:37 +00:00
|
|
|
else:
|
2021-03-08 16:49:10 +00:00
|
|
|
if signed_header_name == "digest":
|
2020-05-18 18:42:22 +00:00
|
|
|
verify_digest(request)
|
2021-03-08 16:49:10 +00:00
|
|
|
comparison_string.append(
|
|
|
|
"%s: %s" % (signed_header_name, request.headers[signed_header_name])
|
|
|
|
)
|
|
|
|
comparison_string = "\n".join(comparison_string)
|
2020-05-14 13:24:37 +00:00
|
|
|
|
|
|
|
signer = pkcs1_15.new(public_key)
|
|
|
|
digest = SHA256.new()
|
|
|
|
digest.update(comparison_string.encode())
|
|
|
|
|
|
|
|
# raises a ValueError if it fails
|
|
|
|
signer.verify(digest, self.signature)
|
2020-05-20 14:26:01 +00:00
|
|
|
|
2020-09-21 17:25:26 +00:00
|
|
|
|
2020-05-20 14:26:01 +00:00
|
|
|
def http_date_age(datestr):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""age of a signature in seconds"""
|
2021-03-08 16:49:10 +00:00
|
|
|
parsed = datetime.datetime.strptime(datestr, "%a, %d %b %Y %H:%M:%S GMT")
|
2020-05-20 14:26:01 +00:00
|
|
|
delta = datetime.datetime.utcnow() - parsed
|
|
|
|
return delta.total_seconds()
|