zig/lib/std/crypto
Igor Anić aa73bb6bc9
tls.Client: implement record padding (#20558)
On decryption tls client should remove zero byte padding after the
content type field. This padding is rarely used, the only site (from the
list of top domains) that I found using it is `tutanota.com`.

From [RFC](https://datatracker.ietf.org/doc/html/rfc8446#section-5.4):
> All encrypted TLS records can be padded.
> Padding is a string of zero-valued bytes appended to the ContentType
field before encryption.
> the receiving implementation scans the field from the end toward the
beginning until it finds a non-zero octet. This non-zero octet is the
content type of the message.

Currently we can't connect to that site:
```
$ zig run main.zig -- tutanota.com
error: TlsInitializationFailed
/usr/local/zig/zig-linux-x86_64-0.14.0-dev.208+854e86c56/lib/std/crypto/tls/Client.zig:476:45: 0x121fbed in init__anon_10331 (http_get_std)
                if (inner_ct != .handshake) return error.TlsUnexpectedMessage;
                                            ^
/usr/local/zig/zig-linux-x86_64-0.14.0-dev.208+854e86c56/lib/std/http/Client.zig:1357:99: 0x1161f0b in connectTcp (http_get_std)
        conn.data.tls_client.* = std.crypto.tls.Client.init(stream, client.ca_bundle, host) catch return error.TlsInitializationFailed;
                                                                                                  ^
/usr/local/zig/zig-linux-x86_64-0.14.0-dev.208+854e86c56/lib/std/http/Client.zig:1492:14: 0x11271e1 in connect (http_get_std)
    } orelse return client.connectTcp(host, port, protocol);
             ^
/usr/local/zig/zig-linux-x86_64-0.14.0-dev.208+854e86c56/lib/std/http/Client.zig:1640:9: 0x111a24e in open (http_get_std)
        try client.connect(valid_uri.host.?.raw, uriPort(valid_uri, protocol), protocol);
        ^
/home/ianic/Code/tls.zig/example/http_get_std.zig:28:19: 0x1118f8c in main (http_get_std)
        var req = try client.open(.GET, uri, .{ .server_header_buffer = &server_header_buffer });
                  ^
```
using this example:

```zig
const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();

    const args = try std.process.argsAlloc(allocator);
    defer std.process.argsFree(allocator, args);

    if (args.len > 1) {
        const domain = args[1];

        var client: std.http.Client = .{ .allocator = allocator };
        defer client.deinit();

        // Add https:// prefix if needed
        const url = brk: {
            const scheme = "https://";
            if (domain.len >= scheme.len and std.mem.eql(u8, domain[0..scheme.len], scheme))
                break :brk domain;

            var url_buf: [128]u8 = undefined;
            break :brk try std.fmt.bufPrint(&url_buf, "https://{s}", .{domain});
        };

        const uri = try std.Uri.parse(url);
        var server_header_buffer: [16 * 1024]u8 = undefined;
        var req = try client.open(.GET, uri, .{ .server_header_buffer = &server_header_buffer });
        defer req.deinit();

        try req.send();
        try req.wait();
    }
}
```
2024-07-21 01:19:36 -07:00
..
25519 crypto.edwards25519: add the ability to check for group membership (#20175) 2024-06-04 10:11:05 +02:00
aes std.builtin.Endian: make the tags lower case 2023-10-31 21:37:35 -04:00
Certificate std.c reorganization 2024-07-19 00:30:32 -07:00
pcurves std.crypto.pcurves.*: simpler, smaller, faster u64 addition with carry (#19644) 2024-04-14 01:13:22 +02:00
tls tls.Client: implement record padding (#20558) 2024-07-21 01:19:36 -07:00
aegis.zig {aegis,aes_gcm}: fix overflow with large inputs on 32-bit systems (#19270) 2024-03-12 22:56:28 +00:00
aes.zig x86_64: fix incorrect mnemonic selection 2024-02-25 11:22:10 +01:00
aes_gcm.zig {aegis,aes_gcm}: fix overflow with large inputs on 32-bit systems (#19270) 2024-03-12 22:56:28 +00:00
aes_ocb.zig x86_64: implement shifts of big integers 2024-02-12 05:25:07 +01:00
argon2.zig std.crypto.pwhash: Add recommended parameters (#20527) 2024-07-07 20:18:33 +00:00
ascon.zig lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
bcrypt.zig std.crypto.pwhash: Add recommended parameters (#20527) 2024-07-07 20:18:33 +00:00
benchmark.zig std.crypto.pcurves.*: simpler, smaller, faster u64 addition with carry (#19644) 2024-04-14 01:13:22 +02:00
blake2.zig std.builtin.Endian: make the tags lower case 2023-10-31 21:37:35 -04:00
blake3.zig x86_64: implement more shuffles 2024-02-25 11:22:10 +01:00
Certificate.zig std: fix typos (#20560) 2024-07-09 14:25:42 -07:00
chacha20.zig std.crypto.chacha: fix typo in XChaChaIETF.stream() (#20399) 2024-06-23 13:20:18 +00:00
cmac.zig x86_64: fix std test failures 2023-11-03 23:18:21 -04:00
ecdsa.zig Rename der_encoded_max_length to der_encoded_length_max 2024-04-20 16:27:56 -07:00
errors.zig crypto.edwards25519: add the ability to check for group membership (#20175) 2024-06-04 10:11:05 +02:00
ff.zig std.crypto.ff: fix typo in montgomery boolean documentation (#20624) 2024-07-14 15:34:02 +02:00
ghash_polyval.zig Deprecate suggestVectorSize in favor of suggestVectorLength 2024-01-01 16:18:57 +01:00
hash_composition.zig x86_64: implement enough to pass unicode tests 2023-10-23 22:42:18 -04:00
hkdf.zig x86_64: implement enough to pass unicode tests 2023-10-23 22:42:18 -04:00
hmac.zig Remove redundant test name prefixes now that test names are fully qualified 2024-02-26 15:18:31 -08:00
isap.zig std.builtin.Endian: make the tags lower case 2023-10-31 21:37:35 -04:00
keccak_p.zig sha3.keccak: allow Keccak[f=200] (#20181) 2024-06-04 10:10:46 +02:00
md5.zig Remove redundant test name prefixes now that test names are fully qualified 2024-02-26 15:18:31 -08:00
ml_kem.zig std: fix typos (#20560) 2024-07-09 14:25:42 -07:00
modes.zig update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
pbkdf2.zig lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
phc_encoding.zig x86_64: fix std test failures 2023-11-03 23:18:21 -04:00
poly1305.zig Remove redundant test name prefixes now that test names are fully qualified 2024-02-26 15:18:31 -08:00
salsa20.zig std: fix typos (#20560) 2024-07-09 14:25:42 -07:00
scrypt.zig std.crypto.pwhash: Add recommended parameters (#20527) 2024-07-07 20:18:33 +00:00
sha1.zig std.builtin.Endian: make the tags lower case 2023-10-31 21:37:35 -04:00
sha2.zig std: fix typos (#20560) 2024-07-09 14:25:42 -07:00
sha3.zig Delete compile errors for deprecated decls 2024-05-03 13:27:30 -07:00
siphash.zig std.builtin.Endian: make the tags lower case 2023-10-31 21:37:35 -04:00
test.zig update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
tlcsprng.zig std.c reorganization 2024-07-19 00:30:32 -07:00
tls.zig std: fix typos (#20560) 2024-07-09 14:25:42 -07:00
utils.zig std: promote tests to doctests 2024-03-21 14:11:46 -07:00