utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import base64
  2. import binascii
  3. import re
  4. from typing import Optional, Union
  5. try:
  6. from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurve
  7. from cryptography.hazmat.primitives.asymmetric.utils import (
  8. decode_dss_signature,
  9. encode_dss_signature,
  10. )
  11. except ModuleNotFoundError:
  12. pass
  13. def force_bytes(value: Union[bytes, str]) -> bytes:
  14. if isinstance(value, str):
  15. return value.encode("utf-8")
  16. elif isinstance(value, bytes):
  17. return value
  18. else:
  19. raise TypeError("Expected a string value")
  20. def base64url_decode(input: Union[bytes, str]) -> bytes:
  21. input_bytes = force_bytes(input)
  22. rem = len(input_bytes) % 4
  23. if rem > 0:
  24. input_bytes += b"=" * (4 - rem)
  25. return base64.urlsafe_b64decode(input_bytes)
  26. def base64url_encode(input: bytes) -> bytes:
  27. return base64.urlsafe_b64encode(input).replace(b"=", b"")
  28. def to_base64url_uint(val: int, *, bit_length: Optional[int] = None) -> bytes:
  29. if val < 0:
  30. raise ValueError("Must be a positive integer")
  31. int_bytes = bytes_from_int(val, bit_length=bit_length)
  32. if len(int_bytes) == 0:
  33. int_bytes = b"\x00"
  34. return base64url_encode(int_bytes)
  35. def from_base64url_uint(val: Union[bytes, str]) -> int:
  36. data = base64url_decode(force_bytes(val))
  37. return int.from_bytes(data, byteorder="big")
  38. def number_to_bytes(num: int, num_bytes: int) -> bytes:
  39. padded_hex = "%0*x" % (2 * num_bytes, num)
  40. return binascii.a2b_hex(padded_hex.encode("ascii"))
  41. def bytes_to_number(string: bytes) -> int:
  42. return int(binascii.b2a_hex(string), 16)
  43. def bytes_from_int(val: int, *, bit_length: Optional[int] = None) -> bytes:
  44. if bit_length is None:
  45. bit_length = val.bit_length()
  46. byte_length = (bit_length + 7) // 8
  47. return val.to_bytes(byte_length, "big", signed=False)
  48. def der_to_raw_signature(der_sig: bytes, curve: "EllipticCurve") -> bytes:
  49. num_bits = curve.key_size
  50. num_bytes = (num_bits + 7) // 8
  51. r, s = decode_dss_signature(der_sig)
  52. return number_to_bytes(r, num_bytes) + number_to_bytes(s, num_bytes)
  53. def raw_to_der_signature(raw_sig: bytes, curve: "EllipticCurve") -> bytes:
  54. num_bits = curve.key_size
  55. num_bytes = (num_bits + 7) // 8
  56. if len(raw_sig) != 2 * num_bytes:
  57. raise ValueError("Invalid signature")
  58. r = bytes_to_number(raw_sig[:num_bytes])
  59. s = bytes_to_number(raw_sig[num_bytes:])
  60. return bytes(encode_dss_signature(r, s))
  61. # Based on https://github.com/hynek/pem/blob/7ad94db26b0bc21d10953f5dbad3acfdfacf57aa/src/pem/_core.py#L224-L252
  62. _PEMS = {
  63. b"CERTIFICATE",
  64. b"TRUSTED CERTIFICATE",
  65. b"PRIVATE KEY",
  66. b"PUBLIC KEY",
  67. b"ENCRYPTED PRIVATE KEY",
  68. b"OPENSSH PRIVATE KEY",
  69. b"DSA PRIVATE KEY",
  70. b"RSA PRIVATE KEY",
  71. b"RSA PUBLIC KEY",
  72. b"EC PRIVATE KEY",
  73. b"DH PARAMETERS",
  74. b"NEW CERTIFICATE REQUEST",
  75. b"CERTIFICATE REQUEST",
  76. b"SSH2 PUBLIC KEY",
  77. b"SSH2 ENCRYPTED PRIVATE KEY",
  78. b"X509 CRL",
  79. }
  80. _PEM_RE = re.compile(
  81. b"----[- ]BEGIN ("
  82. + b"|".join(_PEMS)
  83. + b""")[- ]----\r?
  84. .+?\r?
  85. ----[- ]END \\1[- ]----\r?\n?""",
  86. re.DOTALL,
  87. )
  88. def is_pem_format(key: bytes) -> bool:
  89. return bool(_PEM_RE.search(key))
  90. # Based on https://github.com/pyca/cryptography/blob/bcb70852d577b3f490f015378c75cba74986297b/src/cryptography/hazmat/primitives/serialization/ssh.py#L40-L46
  91. _SSH_KEY_FORMATS = (
  92. b"ssh-ed25519",
  93. b"ssh-rsa",
  94. b"ssh-dss",
  95. b"ecdsa-sha2-nistp256",
  96. b"ecdsa-sha2-nistp384",
  97. b"ecdsa-sha2-nistp521",
  98. )
  99. def is_ssh_key(key: bytes) -> bool:
  100. return key.startswith(_SSH_KEY_FORMATS)