Skip to content

Commit 2e278f6

Browse files
committed
remove std.crypto.der
Only a little bit of generalized logic for DER encoding is needed and so it can live inside the Certificate namespace. This commit removes the generic "parse object id" function which is no longer used in favor of more specific, smaller sets of object ids used with ComptimeStringMap.
1 parent 99c9087 commit 2e278f6

File tree

4 files changed

+84
-169
lines changed

4 files changed

+84
-169
lines changed

lib/std/crypto.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ const std = @import("std.zig");
177177
pub const errors = @import("crypto/errors.zig");
178178

179179
pub const tls = @import("crypto/tls.zig");
180-
pub const der = @import("crypto/der.zig");
181180
pub const Certificate = @import("crypto/Certificate.zig");
182181

183182
test {
@@ -269,7 +268,6 @@ test {
269268
_ = random;
270269
_ = errors;
271270
_ = tls;
272-
_ = der;
273271
_ = Certificate;
274272
}
275273

lib/std/crypto/Certificate.zig

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,91 @@ pub fn checkVersion(bytes: []const u8, version: der.Element) !void {
499499
const std = @import("../std.zig");
500500
const crypto = std.crypto;
501501
const mem = std.mem;
502-
const der = std.crypto.der;
503502
const Certificate = @This();
504503

504+
pub const der = struct {
505+
pub const Class = enum(u2) {
506+
universal,
507+
application,
508+
context_specific,
509+
private,
510+
};
511+
512+
pub const PC = enum(u1) {
513+
primitive,
514+
constructed,
515+
};
516+
517+
pub const Identifier = packed struct(u8) {
518+
tag: Tag,
519+
pc: PC,
520+
class: Class,
521+
};
522+
523+
pub const Tag = enum(u5) {
524+
boolean = 1,
525+
integer = 2,
526+
bitstring = 3,
527+
null = 5,
528+
object_identifier = 6,
529+
sequence = 16,
530+
sequence_of = 17,
531+
utc_time = 23,
532+
generalized_time = 24,
533+
_,
534+
};
535+
536+
pub const Element = struct {
537+
identifier: Identifier,
538+
slice: Slice,
539+
540+
pub const Slice = struct {
541+
start: u32,
542+
end: u32,
543+
544+
pub const empty: Slice = .{ .start = 0, .end = 0 };
545+
};
546+
};
547+
548+
pub const ParseElementError = error{CertificateFieldHasInvalidLength};
549+
550+
pub fn parseElement(bytes: []const u8, index: u32) ParseElementError!Element {
551+
var i = index;
552+
const identifier = @bitCast(Identifier, bytes[i]);
553+
i += 1;
554+
const size_byte = bytes[i];
555+
i += 1;
556+
if ((size_byte >> 7) == 0) {
557+
return .{
558+
.identifier = identifier,
559+
.slice = .{
560+
.start = i,
561+
.end = i + size_byte,
562+
},
563+
};
564+
}
565+
566+
const len_size = @truncate(u7, size_byte);
567+
if (len_size > @sizeOf(u32)) {
568+
return error.CertificateFieldHasInvalidLength;
569+
}
570+
571+
const end_i = i + len_size;
572+
var long_form_size: u32 = 0;
573+
while (i < end_i) : (i += 1) {
574+
long_form_size = (long_form_size << 8) | bytes[i];
575+
}
576+
577+
return .{
578+
.identifier = identifier,
579+
.slice = .{
580+
.start = i,
581+
.end = i + long_form_size,
582+
},
583+
};
584+
}
585+
};
586+
505587
test {
506588
_ = Bundle;
507589
}

lib/std/crypto/Certificate/Bundle.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ const fs = std.fs;
154154
const mem = std.mem;
155155
const crypto = std.crypto;
156156
const Allocator = std.mem.Allocator;
157-
const der = std.crypto.der;
158157
const Certificate = std.crypto.Certificate;
158+
const der = Certificate.der;
159159
const Bundle = @This();
160160

161161
const base64 = std.base64.standard.decoderWithIgnore(" \t\r\n");

lib/std/crypto/der.zig

Lines changed: 0 additions & 165 deletions
This file was deleted.

0 commit comments

Comments
 (0)