|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +######################################### |
| 4 | +# Author: [DonFreed](https://github.com/DonFreed) |
| 5 | +# File: license_message.py |
| 6 | +# Source: https://github.com/DonFreed/docker-actions-test/blob/main/.github/scripts/license_message.py |
| 7 | +# Source+commit: https://github.com/DonFreed/docker-actions-test/blob/aa1051a9f53b3a1e801953748d062cad74dca9a9/.github/scripts/license_message.py |
| 8 | +# Download Date: 2023-07-04, commit: aa1051a |
| 9 | +# This source code is licensed under the BSD 2-Clause license |
| 10 | +######################################### |
| 11 | + |
| 12 | +""" |
| 13 | +Functions for generating and sending license messages |
| 14 | +""" |
| 15 | + |
| 16 | +# Modified from - https://stackoverflow.com/a/59835994 |
| 17 | + |
| 18 | +import argparse |
| 19 | +import base64 |
| 20 | +import calendar |
| 21 | +import re |
| 22 | +import secrets |
| 23 | +import sys |
| 24 | + |
| 25 | +from cryptography.hazmat.primitives.ciphers.aead import AESGCM |
| 26 | +from datetime import datetime as dt |
| 27 | + |
| 28 | +MESSAGE_TIMEOUT = 60 * 60 * 24 # Messages are valid for 1 day |
| 29 | +NONCE_BYTES = 12 |
| 30 | + |
| 31 | + |
| 32 | +class DecryptionTimeout(Exception): |
| 33 | + # Decrypting a message that is too old |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +def generate_key(): |
| 38 | + key = secrets.token_bytes(32) |
| 39 | + return key |
| 40 | + |
| 41 | + |
| 42 | +def handle_generate_key(args): |
| 43 | + key = generate_key() |
| 44 | + key_b64 = base64.b64encode(key) |
| 45 | + print(key_b64.decode("utf-8"), file=args.outfile) |
| 46 | + |
| 47 | + |
| 48 | +def encrypt_message(key, message): |
| 49 | + nonce = secrets.token_bytes(NONCE_BYTES) |
| 50 | + timestamp = calendar.timegm(dt.now().utctimetuple()) |
| 51 | + data = timestamp.to_bytes(10, byteorder="big") + b"__" + message |
| 52 | + ciphertext = nonce + AESGCM(key).encrypt(nonce, data, b"") |
| 53 | + return ciphertext |
| 54 | + |
| 55 | + |
| 56 | +def handle_encrypt_message(args): |
| 57 | + key = base64.b64decode(args.key.encode("utf-8")) |
| 58 | + message = args.message.encode("utf-8") |
| 59 | + ciphertext = encrypt_message(key, message) |
| 60 | + ciphertext_b64 = base64.b64encode(ciphertext) |
| 61 | + print(ciphertext_b64.decode("utf-8"), file=args.outfile) |
| 62 | + |
| 63 | + |
| 64 | +def decrypt_message(key, ciphertext, timeout=MESSAGE_TIMEOUT): |
| 65 | + nonce, ciphertext = ciphertext[:NONCE_BYTES], ciphertext[NONCE_BYTES:] |
| 66 | + message = AESGCM(key).decrypt(nonce, ciphertext, b"") |
| 67 | + |
| 68 | + msg_timestamp, message = re.split(b"__", message, maxsplit=1) |
| 69 | + msg_timestamp = int.from_bytes(msg_timestamp, byteorder="big") |
| 70 | + timestamp = calendar.timegm(dt.now().utctimetuple()) |
| 71 | + if (timestamp - msg_timestamp) > timeout: |
| 72 | + raise DecryptionTimeout("The message has an expired timeout") |
| 73 | + return message.decode("utf-8") |
| 74 | + |
| 75 | + |
| 76 | +def handle_decrypt_message(args): |
| 77 | + key = base64.b64decode(args.key.encode("utf-8")) |
| 78 | + ciphertext = base64.b64decode(args.message.encode("utf-8")) |
| 79 | + message = decrypt_message(key, ciphertext, timeout=args.timeout) |
| 80 | + print(str(message), file=args.outfile) |
| 81 | + |
| 82 | + |
| 83 | +def parse_args(argv=None): |
| 84 | + parser = argparse.ArgumentParser(description=__doc__) |
| 85 | + parser.add_argument("--outfile", default=sys.stdout, type=argparse.FileType("w"), help="The output file") |
| 86 | + |
| 87 | + subparsers = parser.add_subparsers(help="Available sub-commands") |
| 88 | + |
| 89 | + gen_parser = subparsers.add_parser("generate_key", help="Generate a random key string") |
| 90 | + gen_parser.set_defaults(func=handle_generate_key) |
| 91 | + |
| 92 | + encrypt_parser = subparsers.add_parser("encrypt", help="Encrypt a message") |
| 93 | + encrypt_parser.add_argument("--key", required=True, help="The encryption key") |
| 94 | + encrypt_parser.add_argument("--message", required=True, help="Message to encrypt") |
| 95 | + encrypt_parser.set_defaults(func=handle_encrypt_message) |
| 96 | + |
| 97 | + decrypt_parser = subparsers.add_parser("decrypt", help="Decyrpt a message") |
| 98 | + decrypt_parser.add_argument("--key", required=True, help="The encryption key") |
| 99 | + decrypt_parser.add_argument("--message", required=True, help="Message to decrypt") |
| 100 | + decrypt_parser.add_argument( |
| 101 | + "--timeout", |
| 102 | + default=MESSAGE_TIMEOUT, |
| 103 | + type=int, |
| 104 | + help="A message timeout. Decryption will fail for older messages", |
| 105 | + ) |
| 106 | + decrypt_parser.set_defaults(func=handle_decrypt_message) |
| 107 | + |
| 108 | + return parser.parse_args(argv) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + args = parse_args() |
| 113 | + args.func(args) |
0 commit comments