Skip to content

Instantly share code, notes, and snippets.

@JanisErdmanis
Last active July 2, 2023 09:59
Show Gist options
  • Save JanisErdmanis/a55c592170d8a640d2a233b8e8ee44f6 to your computer and use it in GitHub Desktop.
Save JanisErdmanis/a55c592170d8a640d2a233b8e8ee44f6 to your computer and use it in GitHub Desktop.
JWT public key test with CryptoGroups
#https://www.rfc-editor.org/rfc/rfc7515#appendix-A.3
using CryptoGroups
using CryptoSignatures
using Base64
base64url_decode(x) = replace(x, "_" => "/", "-" => "+") |> base64decode
base64url_encode(x) = replace(base64encode(x), "/" => "_", "+" => "-")
# Reading in from JSON Web Key string
curve = CryptoGroups.curve("secp256r1")
P_256 = specialize(ECPoint, curve)
ctx = ECDSAContext(curve, "sha256")
x = "f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU" |> base64url_decode
y = "x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0" |> base64url_decode
x_int = parse(BigInt, bytes2hex(x), base=16)
y_int = parse(BigInt, bytes2hex(y), base=16)
pbkey = CryptoGroups.octet(x_int, y_int, curve)
point = convert(P_256, pbkey)
@assert CryptoGroups.oncurve(point)
# Parsing and veryfing JWS
jws = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8ISlSApmWQxfKTUJqPP3-Kg6NU1Q"
header, payload, signature_str = split(jws, ".")
r = base64url_decode(signature_str)[1:32]
s = base64url_decode(signature_str)[33:64]
sig = DSA(parse(BigInt, bytes2hex(r), base=16), parse(BigInt, bytes2hex(s), base=16))
input = Vector{UInt8}(header * "." * payload)
@assert CryptoSignatures.verify(ctx, input, pbkey, sig)
@assert CryptoGroups.oncurve(point)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment