-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencode.zig
More file actions
133 lines (119 loc) · 4.84 KB
/
encode.zig
File metadata and controls
133 lines (119 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const std = @import("std");
const Algorithm = @import("root.zig").Algorithm;
const Header = @import("root.zig").Header;
/// Key used for encoding JWT token components
pub const EncodingKey = union(enum) {
secret: []const u8,
edsa: std.crypto.sign.Ed25519.SecretKey,
es256: std.crypto.sign.ecdsa.EcdsaP256Sha256.SecretKey,
es384: std.crypto.sign.ecdsa.EcdsaP384Sha384.SecretKey,
/// create a new edsa encoding key from edsa secret key bytes
pub fn fromEdsaBytes(bytes: [std.crypto.sign.Ed25519.SecretKey.encoded_length]u8) !@This() {
return .{ .edsa = try std.crypto.sign.Ed25519.SecretKey.fromBytes(bytes) };
}
pub fn fromEs256Bytes(bytes: [std.crypto.ecdsa.EcdsaP256Sha256.SecretKey.encoded_length]u8) !@This() {
return .{ .es256 = try std.crypto.sign.ecdsa.EcdsaP256Sha256.SecretKey.fromBytes(bytes) };
}
pub fn fromEs384Bytes(bytes: [std.crypto.ecdsa.EcdsaP384Sha384.SecretKey.encoded_length]u8) !@This() {
return .{ .es384 = try std.crypto.sign.ecdsa.EcdsaP384Sha384.SecretKey.fromBytes(bytes) };
}
};
fn encodePart(
allocator: std.mem.Allocator,
part: anytype,
) ![]const u8 {
const encoder = std.base64.url_safe_no_pad.Encoder;
const json = try std.json.Stringify.valueAlloc(allocator, part, .{ .emit_null_optional_fields = false });
defer allocator.free(json);
const enc = try allocator.alloc(u8, encoder.calcSize(json.len));
_ = encoder.encode(enc, json);
return enc;
}
fn sign(
allocator: std.mem.Allocator,
msg: []const u8,
algo: Algorithm,
key: EncodingKey,
) ![]const u8 {
return switch (algo) {
.HS256 => blk: {
var dest: [std.crypto.auth.hmac.sha2.HmacSha256.mac_length]u8 = undefined;
std.crypto.auth.hmac.sha2.HmacSha256.create(&dest, msg, switch (key) {
.secret => |v| v,
else => return error.InvalidEncodingKey,
});
break :blk allocator.dupe(u8, &dest);
},
.HS384 => blk: {
var dest: [std.crypto.auth.hmac.sha2.HmacSha384.mac_length]u8 = undefined;
std.crypto.auth.hmac.sha2.HmacSha384.create(&dest, msg, switch (key) {
.secret => |v| v,
else => return error.InvalidEncodingKey,
});
break :blk allocator.dupe(u8, &dest);
},
.HS512 => blk: {
var dest: [std.crypto.auth.hmac.sha2.HmacSha512.mac_length]u8 = undefined;
std.crypto.auth.hmac.sha2.HmacSha512.create(&dest, msg, switch (key) {
.secret => |v| v,
else => return error.InvalidEncodingKey,
});
break :blk allocator.dupe(u8, &dest);
},
.ES256 => blk: {
const pair = try std.crypto.sign.ecdsa.EcdsaP256Sha256.KeyPair.fromSecretKey(switch (key) {
.es256 => |v| v,
else => return error.InvalidEncodingKey,
});
const dest = (try pair.sign(msg, null)).toBytes();
break :blk allocator.dupe(u8, &dest);
},
.ES384 => blk: {
const pair = try std.crypto.sign.ecdsa.EcdsaP384Sha384.KeyPair.fromSecretKey(switch (key) {
.es384 => |v| v,
else => return error.InvalidEncodingKey,
});
const dest = (try pair.sign(msg, null)).toBytes();
break :blk allocator.dupe(u8, &dest);
},
.EdDSA => blk: {
const pair = try std.crypto.sign.Ed25519.KeyPair.fromSecretKey(switch (key) {
.edsa => |v| v,
else => return error.InvalidEncodingKey,
});
const dest = (try pair.sign(msg, null)).toBytes();
break :blk allocator.dupe(u8, &dest);
},
else => return error.TODO,
};
}
pub fn encode(
allocator: std.mem.Allocator,
header: Header,
claims: anytype,
key: EncodingKey,
) ![]const u8 {
comptime {
if (@typeInfo(@TypeOf(claims)) != .@"struct") {
@compileError("expected claims to be a struct but was a " ++ @typeName(@TypeOf(claims)));
}
}
const encoder = std.base64.url_safe_no_pad.Encoder;
const header_enc = try encodePart(allocator, header);
defer allocator.free(header_enc);
const claims_enc = try encodePart(allocator, claims);
defer allocator.free(claims_enc);
const msg = try std.mem.join(allocator, ".", &.{ header_enc, claims_enc });
defer allocator.free(msg);
const sig = try sign(allocator, msg, header.alg, key);
defer allocator.free(sig);
const sig_enc = try allocator.alloc(u8, encoder.calcSize(sig.len));
defer allocator.free(sig_enc);
_ = encoder.encode(sig_enc, sig);
var buf = std.array_list.Managed(u8).init(allocator);
defer buf.deinit();
try buf.appendSlice(msg);
try buf.append('.');
try buf.appendSlice(sig_enc);
return try buf.toOwnedSlice();
}