mirror of
https://github.com/ziglang/zig.git
synced 2026-01-04 12:33:19 +00:00
Add module for mapping ASN1 types to Zig types. See
`asn1.Tag.fromZig` for the mapping. Add DER encoder and decoder.
See `asn1/test.zig` for example usage of every ASN1 type.
This implementation allows ASN1 tags to be overriden with `asn1_tag`
and `asn1_tags`:
```zig
const MyContainer = (enum | union | struct) {
field: u32,
pub const asn1_tag = asn1.Tag.init(...);
// This specifies a tag's class, and if explicit, additional encoding
// rules.
pub const asn1_tags = .{
.field = asn1.FieldTag.explicit(0, .context_specific),
};
};
```
Despite having an enum tag type, ASN1 frequently uses OIDs as enum
values. This is supported via an `pub const oids` field.
```zig
const MyEnum = enum {
a,
pub const oids = asn1.Oid.StaticMap(MyEnum).initComptime(.{
.a = "1.2.3.4",
});
};
```
Futhermore, a container may choose to implement encoding and decoding
however it deems fit. This allows for derived fields since Zig has a far
more powerful type system than ASN1.
```zig
// ASN1 has no standard way of tagging unions.
const MyContainer = union(enum) {
derived: PowerfulZigType,
const WeakAsn1Type = ...;
pub fn encodeDer(self: MyContainer, encoder: *der.Encoder) !void {
try encoder.any(WeakAsn1Type{...});
}
pub fn decodeDer(decoder: *der.Decoder) !MyContainer {
const weak_asn1_type = try decoder.any(WeakAsn1Type);
return .{ .derived = PowerfulZigType{...} };
}
};
```
An unfortunate side-effect is that decoding and encoding cannot have
complete complete error sets unless we limit what errors users may
return. Luckily, PKI ASN1 types are NOT recursive so the inferred
error set should be sufficient.
Finally, other encodings are possible, but this patch only implements
a buffered DER encoder and decoder.
In an effort to keep the changeset minimal this PR does not actually
use the DER parser for stdlib PKI, but a tested example of how it may
be used for Certificate is available
[here.](https://github.com/clickingbuttons/asn1/blob/69c5709d/src/Certificate.zig)
Closes #19775.
80 lines
2.3 KiB
Zig
80 lines
2.3 KiB
Zig
const std = @import("std");
|
|
const asn1 = @import("../asn1.zig");
|
|
|
|
const der = asn1.der;
|
|
const Tag = asn1.Tag;
|
|
const FieldTag = asn1.FieldTag;
|
|
|
|
/// An example that uses all ASN1 types and available implementation features.
|
|
const AllTypes = struct {
|
|
a: u8 = 0,
|
|
b: asn1.BitString,
|
|
c: C,
|
|
d: asn1.Opaque(Tag.universal(.string_utf8, false)),
|
|
e: asn1.Opaque(Tag.universal(.octetstring, false)),
|
|
f: ?u16,
|
|
g: ?Nested,
|
|
h: asn1.Any,
|
|
|
|
pub const asn1_tags = .{
|
|
.a = FieldTag.explicit(0, .context_specific),
|
|
.b = FieldTag.explicit(1, .context_specific),
|
|
.c = FieldTag.implicit(2, .context_specific),
|
|
.g = FieldTag.implicit(3, .context_specific),
|
|
};
|
|
|
|
const C = enum {
|
|
a,
|
|
b,
|
|
|
|
pub const oids = asn1.Oid.StaticMap(@This()).initComptime(.{
|
|
.a = "1.2.3.4",
|
|
.b = "1.2.3.5",
|
|
});
|
|
};
|
|
|
|
const Nested = struct {
|
|
inner: Asn1T,
|
|
sum: i16,
|
|
|
|
const Asn1T = struct { a: u8, b: i16 };
|
|
|
|
pub fn decodeDer(decoder: *der.Decoder) !Nested {
|
|
const inner = try decoder.any(Asn1T);
|
|
return Nested{ .inner = inner, .sum = inner.a + inner.b };
|
|
}
|
|
|
|
pub fn encodeDer(self: Nested, encoder: *der.Encoder) !void {
|
|
try encoder.any(self.inner);
|
|
}
|
|
};
|
|
};
|
|
|
|
test AllTypes {
|
|
const expected = AllTypes{
|
|
.a = 2,
|
|
.b = asn1.BitString{ .bytes = &[_]u8{ 0x04, 0xa0 } },
|
|
.c = .a,
|
|
.d = .{ .bytes = "asdf" },
|
|
.e = .{ .bytes = "fdsa" },
|
|
.f = (1 << 8) + 1,
|
|
.g = .{ .inner = .{ .a = 4, .b = 5 }, .sum = 9 },
|
|
.h = .{ .tag = Tag.init(.string_ia5, false, .universal), .bytes = "asdf" },
|
|
};
|
|
const path = "./der/testdata/all_types.der";
|
|
const encoded = @embedFile(path);
|
|
const actual = try asn1.der.decode(AllTypes, encoded);
|
|
try std.testing.expectEqualDeep(expected, actual);
|
|
|
|
const allocator = std.testing.allocator;
|
|
const buf = try asn1.der.encode(allocator, expected);
|
|
defer allocator.free(buf);
|
|
try std.testing.expectEqualSlices(u8, encoded, buf);
|
|
|
|
// Use this to update test file.
|
|
// const dir = try std.fs.cwd().openDir("lib/std/crypto/asn1", .{});
|
|
// var file = try dir.createFile(path, .{});
|
|
// defer file.close();
|
|
// try file.writeAll(buf);
|
|
}
|