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 {...@@ -613,13 +613,14 @@ const Date = struct {
613 }613 }
614};614};
615615
616pub fn parseTimeDigits(nn: @Vector(2, u8), min: u8, max: u8) !u8 {616pub fn parseTimeDigits(nn_u8: @Vector(2, u8), min: u8, max: u8) !u8 {
617 const zero: @Vector(2, u8) = .{ '0', '0' };617 const nn: @Vector(2, u16) = .{ nn_u8[0], nn_u8[1] };
618 const mm: @Vector(2, u8) = .{ 10, 1 };618 const zero: @Vector(2, u16) = .{ '0', '0' };
619 const mm: @Vector(2, u16) = .{ 10, 1 };
619 const result = @reduce(.Add, (nn -% zero) *% mm);620 const result = @reduce(.Add, (nn -% zero) *% mm);
620 if (result < min) return error.CertificateTimeInvalid;621 if (result < min) return error.CertificateTimeInvalid;
621 if (result > max) return error.CertificateTimeInvalid;622 if (result > max) return error.CertificateTimeInvalid;
622 return result;623 return @truncate(result);
623}624}
624625
625test parseTimeDigits {626test parseTimeDigits {
...@@ -631,15 +632,16 @@ test parseTimeDigits {...@@ -631,15 +632,16 @@ test parseTimeDigits {
631 const expectError = std.testing.expectError;632 const expectError = std.testing.expectError;
632 try expectError(error.CertificateTimeInvalid, parseTimeDigits("13".*, 1, 12));633 try expectError(error.CertificateTimeInvalid, parseTimeDigits("13".*, 1, 12));
633 try expectError(error.CertificateTimeInvalid, parseTimeDigits("00".*, 1, 12));634 try expectError(error.CertificateTimeInvalid, parseTimeDigits("00".*, 1, 12));
635 try expectError(error.CertificateTimeInvalid, parseTimeDigits("Di".*, 0, 99));
634}636}
635637
636pub fn parseYear4(text: *const [4]u8) !u16 {638pub fn parseYear4(text: *const [4]u8) !u16 {
637 const nnnn: @Vector(4, u16) = .{ text[0], text[1], text[2], text[3] };639 const nnnn: @Vector(4, u32) = .{ text[0], text[1], text[2], text[3] };
638 const zero: @Vector(4, u16) = .{ '0', '0', '0', '0' };640 const zero: @Vector(4, u32) = .{ '0', '0', '0', '0' };
639 const mmmm: @Vector(4, u16) = .{ 1000, 100, 10, 1 };641 const mmmm: @Vector(4, u32) = .{ 1000, 100, 10, 1 };
640 const result = @reduce(.Add, (nnnn -% zero) *% mmmm);642 const result = @reduce(.Add, (nnnn -% zero) *% mmmm);
641 if (result > 9999) return error.CertificateTimeInvalid;643 if (result > 9999) return error.CertificateTimeInvalid;
642 return result;644 return @truncate(result);
643}645}
644646
645test parseYear4 {647test parseYear4 {
...@@ -651,6 +653,7 @@ test parseYear4 {...@@ -651,6 +653,7 @@ test parseYear4 {
651 const expectError = std.testing.expectError;653 const expectError = std.testing.expectError;
652 try expectError(error.CertificateTimeInvalid, parseYear4("999b"));654 try expectError(error.CertificateTimeInvalid, parseYear4("999b"));
653 try expectError(error.CertificateTimeInvalid, parseYear4("crap"));655 try expectError(error.CertificateTimeInvalid, parseYear4("crap"));
656 try expectError(error.CertificateTimeInvalid, parseYear4("r:bQ"));
654}657}
655658
656pub fn parseAlgorithm(bytes: []const u8, element: der.Element) ParseEnumError!Algorithm {659pub fn parseAlgorithm(bytes: []const u8, element: der.Element) ParseEnumError!Algorithm {