authorgravatar for lacc97@protonmail.chLuis Cáceres <lacc97@protonmail.ch> 2023-07-23 20:48:45+00:00
committergravatar for lacc97@protonmail.chLuis Cáceres <lacc97@protonmail.ch> 2023-07-23 20:48:45+00:00
log05bad1f42d06ab77967ce14ac47253f5dd6d170e
tree55a642ff7f261190a1c9d10773bb2927001cca45
parentb35874a4292526b3d6ab33b24b8fbd0d1596e216

std.crypto.Certificate: fix timedate parsing

This commit fixes parsing in parseYear4 and parseTimeDigits by using a wider vector data type such that the intermediate result cannot overflow and the error check remains correct.

1 files changed, 11 insertions(+), 8 deletions(-)

lib/std/crypto/Certificate.zig+11-8
......@@ -613,13 +613,14 @@ const Date = struct {
613613 }
614614};
615615
616pub fn parseTimeDigits(nn: @Vector(2, u8), min: u8, max: u8) !u8 {
617 const zero: @Vector(2, u8) = .{ '0', '0' };
618 const mm: @Vector(2, u8) = .{ 10, 1 };
616pub fn parseTimeDigits(nn_u8: @Vector(2, u8), min: u8, max: u8) !u8 {
617 const nn: @Vector(2, u16) = .{ nn_u8[0], nn_u8[1] };
618 const zero: @Vector(2, u16) = .{ '0', '0' };
619 const mm: @Vector(2, u16) = .{ 10, 1 };
619620 const result = @reduce(.Add, (nn -% zero) *% mm);
620621 if (result < min) return error.CertificateTimeInvalid;
621622 if (result > max) return error.CertificateTimeInvalid;
622 return result;
623 return @truncate(result);
623624}
624625
625626test parseTimeDigits {
......@@ -631,15 +632,16 @@ test parseTimeDigits {
631632 const expectError = std.testing.expectError;
632633 try expectError(error.CertificateTimeInvalid, parseTimeDigits("13".*, 1, 12));
633634 try expectError(error.CertificateTimeInvalid, parseTimeDigits("00".*, 1, 12));
635 try expectError(error.CertificateTimeInvalid, parseTimeDigits("Di".*, 0, 99));
634636}
635637
636638pub fn parseYear4(text: *const [4]u8) !u16 {
637 const nnnn: @Vector(4, u16) = .{ text[0], text[1], text[2], text[3] };
638 const zero: @Vector(4, u16) = .{ '0', '0', '0', '0' };
639 const mmmm: @Vector(4, u16) = .{ 1000, 100, 10, 1 };
639 const nnnn: @Vector(4, u32) = .{ text[0], text[1], text[2], text[3] };
640 const zero: @Vector(4, u32) = .{ '0', '0', '0', '0' };
641 const mmmm: @Vector(4, u32) = .{ 1000, 100, 10, 1 };
640642 const result = @reduce(.Add, (nnnn -% zero) *% mmmm);
641643 if (result > 9999) return error.CertificateTimeInvalid;
642 return result;
644 return @truncate(result);
643645}
644646
645647test parseYear4 {
......@@ -651,6 +653,7 @@ test parseYear4 {
651653 const expectError = std.testing.expectError;
652654 try expectError(error.CertificateTimeInvalid, parseYear4("999b"));
653655 try expectError(error.CertificateTimeInvalid, parseYear4("crap"));
656 try expectError(error.CertificateTimeInvalid, parseYear4("r:bQ"));
654657}
655658
656659pub fn parseAlgorithm(bytes: []const u8, element: der.Element) ParseEnumError!Algorithm {