mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
Merge pull request #16516 from lacc97/std-crypto-certificate-parse-datetime
std.crypto.Certificate: fix timedate parsing
This commit is contained in:
commit
f8666d748d
@ -538,12 +538,12 @@ pub fn parseTime(cert: Certificate, elem: der.Element) ParseTimeError!u64 {
|
||||
return error.CertificateTimeInvalid;
|
||||
|
||||
return Date.toSeconds(.{
|
||||
.year = @as(u16, 2000) + try parseTimeDigits(bytes[0..2].*, 0, 99),
|
||||
.month = try parseTimeDigits(bytes[2..4].*, 1, 12),
|
||||
.day = try parseTimeDigits(bytes[4..6].*, 1, 31),
|
||||
.hour = try parseTimeDigits(bytes[6..8].*, 0, 23),
|
||||
.minute = try parseTimeDigits(bytes[8..10].*, 0, 59),
|
||||
.second = try parseTimeDigits(bytes[10..12].*, 0, 59),
|
||||
.year = @as(u16, 2000) + try parseTimeDigits(bytes[0..2], 0, 99),
|
||||
.month = try parseTimeDigits(bytes[2..4], 1, 12),
|
||||
.day = try parseTimeDigits(bytes[4..6], 1, 31),
|
||||
.hour = try parseTimeDigits(bytes[6..8], 0, 23),
|
||||
.minute = try parseTimeDigits(bytes[8..10], 0, 59),
|
||||
.second = try parseTimeDigits(bytes[10..12], 0, 59),
|
||||
});
|
||||
},
|
||||
.generalized_time => {
|
||||
@ -555,11 +555,11 @@ pub fn parseTime(cert: Certificate, elem: der.Element) ParseTimeError!u64 {
|
||||
return error.CertificateTimeInvalid;
|
||||
return Date.toSeconds(.{
|
||||
.year = try parseYear4(bytes[0..4]),
|
||||
.month = try parseTimeDigits(bytes[4..6].*, 1, 12),
|
||||
.day = try parseTimeDigits(bytes[6..8].*, 1, 31),
|
||||
.hour = try parseTimeDigits(bytes[8..10].*, 0, 23),
|
||||
.minute = try parseTimeDigits(bytes[10..12].*, 0, 59),
|
||||
.second = try parseTimeDigits(bytes[12..14].*, 0, 59),
|
||||
.month = try parseTimeDigits(bytes[4..6], 1, 12),
|
||||
.day = try parseTimeDigits(bytes[6..8], 1, 31),
|
||||
.hour = try parseTimeDigits(bytes[8..10], 0, 23),
|
||||
.minute = try parseTimeDigits(bytes[10..12], 0, 59),
|
||||
.second = try parseTimeDigits(bytes[12..14], 0, 59),
|
||||
});
|
||||
},
|
||||
else => return error.CertificateFieldHasWrongDataType,
|
||||
@ -613,33 +613,35 @@ const Date = struct {
|
||||
}
|
||||
};
|
||||
|
||||
pub fn parseTimeDigits(nn: @Vector(2, u8), min: u8, max: u8) !u8 {
|
||||
const zero: @Vector(2, u8) = .{ '0', '0' };
|
||||
const mm: @Vector(2, u8) = .{ 10, 1 };
|
||||
pub fn parseTimeDigits(text: *const [2]u8, min: u8, max: u8) !u8 {
|
||||
const nn: @Vector(2, u16) = .{ text[0], text[1] };
|
||||
const zero: @Vector(2, u16) = .{ '0', '0' };
|
||||
const mm: @Vector(2, u16) = .{ 10, 1 };
|
||||
const result = @reduce(.Add, (nn -% zero) *% mm);
|
||||
if (result < min) return error.CertificateTimeInvalid;
|
||||
if (result > max) return error.CertificateTimeInvalid;
|
||||
return result;
|
||||
return @truncate(result);
|
||||
}
|
||||
|
||||
test parseTimeDigits {
|
||||
const expectEqual = std.testing.expectEqual;
|
||||
try expectEqual(@as(u8, 0), try parseTimeDigits("00".*, 0, 99));
|
||||
try expectEqual(@as(u8, 99), try parseTimeDigits("99".*, 0, 99));
|
||||
try expectEqual(@as(u8, 42), try parseTimeDigits("42".*, 0, 99));
|
||||
try expectEqual(@as(u8, 0), try parseTimeDigits("00", 0, 99));
|
||||
try expectEqual(@as(u8, 99), try parseTimeDigits("99", 0, 99));
|
||||
try expectEqual(@as(u8, 42), try parseTimeDigits("42", 0, 99));
|
||||
|
||||
const expectError = std.testing.expectError;
|
||||
try expectError(error.CertificateTimeInvalid, parseTimeDigits("13".*, 1, 12));
|
||||
try expectError(error.CertificateTimeInvalid, parseTimeDigits("00".*, 1, 12));
|
||||
try expectError(error.CertificateTimeInvalid, parseTimeDigits("13", 1, 12));
|
||||
try expectError(error.CertificateTimeInvalid, parseTimeDigits("00", 1, 12));
|
||||
try expectError(error.CertificateTimeInvalid, parseTimeDigits("Di", 0, 99));
|
||||
}
|
||||
|
||||
pub fn parseYear4(text: *const [4]u8) !u16 {
|
||||
const nnnn: @Vector(4, u16) = .{ text[0], text[1], text[2], text[3] };
|
||||
const zero: @Vector(4, u16) = .{ '0', '0', '0', '0' };
|
||||
const mmmm: @Vector(4, u16) = .{ 1000, 100, 10, 1 };
|
||||
const nnnn: @Vector(4, u32) = .{ text[0], text[1], text[2], text[3] };
|
||||
const zero: @Vector(4, u32) = .{ '0', '0', '0', '0' };
|
||||
const mmmm: @Vector(4, u32) = .{ 1000, 100, 10, 1 };
|
||||
const result = @reduce(.Add, (nnnn -% zero) *% mmmm);
|
||||
if (result > 9999) return error.CertificateTimeInvalid;
|
||||
return result;
|
||||
return @truncate(result);
|
||||
}
|
||||
|
||||
test parseYear4 {
|
||||
@ -651,6 +653,7 @@ test parseYear4 {
|
||||
const expectError = std.testing.expectError;
|
||||
try expectError(error.CertificateTimeInvalid, parseYear4("999b"));
|
||||
try expectError(error.CertificateTimeInvalid, parseYear4("crap"));
|
||||
try expectError(error.CertificateTimeInvalid, parseYear4("r:bQ"));
|
||||
}
|
||||
|
||||
pub fn parseAlgorithm(bytes: []const u8, element: der.Element) ParseEnumError!Algorithm {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user