Commit Graph

66 Commits

Author SHA1 Message Date
Jacob Young
eaa6218f09 x86_64: fix errors compiling the compiler
This fixes issues targetting both `x86_64-linux` and `x86_64-macos` with
the self-hosted backend.
2024-02-04 22:58:38 -05:00
Jacob Young
5e791e8e07 tls: support ed25519 signatures
Which were claimed to be supported during the handshake but were not
actually implemented.
2024-02-02 17:27:26 -08:00
melonedo
9b0da5ccef Fix TLS record overflow by limiting inner record length to 2^14
Per last paragraph of RFC 8446, Section 5.2, the length of the inner content of an encrypted record must not exceed 2^14 + 1, while that of the whole encrypted record must not exceed 2^14 + 256.
2024-01-16 14:58:56 -08:00
Purrie
c4a1b54ebe tls client interface consistency fix
Client for tls was using a function that wasn't declared on the
interface for it. The issue wasn't apparent because net stream
implemented that function.

I changed it to keep the interface promise of what's required to be
compatible with the tls client functionality.
2024-01-16 13:02:00 -08:00
Frank Denis
f276bb107e verify_buffer is not expected to be sentinel-terminated 2023-12-01 20:04:52 +01:00
Frank Denis
9831dc9e0c TLS: The 0x1306 TLS identifier was updated to TLS_AEGIS_256_SHA512
Following the recommendations from [1], the AEGIS specification
and the TLS registry [2] were updated to recommend SHA512 for the
traffic secrets.

[1] https://eprint.iacr.org/2023/913.pdf
[2] https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4
2023-12-01 18:00:15 +01:00
mlugg
51595d6b75
lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
Andrew Kelley
3fc6fc6812 std.builtin.Endian: make the tags lower case
Let's take this breaking change opportunity to fix the style of this
enum.
2023-10-31 21:37:35 -04:00
Jacob Young
d890e81761 mem: fix ub in writeInt
Use inline to vastly simplify the exposed API.  This allows a
comptime-known endian parameter to be propogated, making extra functions
for a specific endianness completely unnecessary.
2023-10-31 21:37:35 -04:00
Nameless
e1c37f70d4
std.http.Client: store *Connection instead of a pool node, buffer writes 2023-10-21 20:52:58 -05:00
dec05eba
2e424e019f Client.zig: support rsa_pss_rsae_sha384 and rsa_pss_rsae_sha512
This fixes HTTP GET to https://www.iana.org/domains/reserved
for example
2023-07-06 18:36:15 -07:00
Jacob Young
8239d3b358 crypto: recoup storage in tls.Client.partially_read_buffer 2023-06-26 21:05:08 -04:00
Jacob Young
eb8881a538 crypto: cleanup unneeded uses of @as in tls.Client 2023-06-26 21:05:08 -04:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
Eric Joldasov
50339f595a all: zig fmt and rename "@XToY" to "@YFromX"
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:42 -07:00
mlugg
9e61ba19e9 std.crypto.tls.Client: fix @memcpy crash in limitedOverlapCopy
Resolves: #15928
2023-06-12 14:22:09 -07:00
Nameless
0e5e6cb10c
std.http: add TlsAlert descriptions so that they can at least be viewed in err return traces 2023-06-01 13:43:55 -05:00
Frank Denis
1ab008d89d
RSA: remove usage of allocators (#15901)
Individual max buffer sizes are well known, now that arithmetic doesn't
require allocations any more.

Also bump `main_cert_pub_key_buf`, so that e.g. `nodejs.org` public
keys can fit.
2023-05-30 10:06:44 +00:00
Frank Denis
89f622fc68
std.crypto.ff - Alloc-free, constant-time field arithmetic for crypto (#15795)
A minimal set of simple, safe functions for Montgomery arithmetic,
designed for cryptographic primitives.

Also update the current RSA cert validation to use it, getting rid
of the FixedBuffer hack and the previous limitations.

Make the check of the RSA public key a little bit more strict by
the way.
2023-05-22 16:11:06 +02:00
Andrew Kelley
7cf2cbb33e std.crypto.tls.Client.readvAdvanced: fix bugs
* When there is buffered cleartext, return it without calling the
   underlying read function. This prevents buffer overflow due to space
   used up by cleartext.
 * Avoid clearing the buffer when the buffered cleartext could not be
   completely given to the result read buffer, and there is some
   buffered ciphertext left.
 * Instead of rounding up the amount of bytes to ask for to the nearest
   TLS record size, round down, with a minimum of 1. This prevents the
   code path from being taken which requires extra memory copies.
 * Avoid calling `@memcpy` with overlapping arguments.

closes #15590
2023-05-18 03:26:16 -07:00
Pyry Kovanen
ffccd70671 tls: update finishRead2 for new @memcpy semantics 2023-05-09 14:47:04 -07:00
Jonathan Marler
5def162391 fix bug and simplify std.crypto.tls.Client.limitVecs 2023-05-03 20:52:18 -07:00
Andrew Kelley
6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
Andrew Kelley
a5c910adb6 change semantics of @memcpy and @memset
Now they use slices or array pointers with any element type instead of
requiring byte pointers.

This is a breaking enhancement to the language.

The safety check for overlapping pointers will be implemented in a
future commit.

closes #14040
2023-04-25 11:23:40 -07:00
Nameless
fde05b10b3 tls.Client: don't read if we don't need more data 2023-04-08 03:37:05 -04:00
Frank Denis
d61ac0db8c
TLS: Favor ChaCha over AES-based ciphers on CPUs without AES support (#15034)
On CPUs without AES support, ChaCha is always faster and safer than
software AES.

Add `crypto.core.aes.has_hardware_support` to represent whether
AES acceleration is available or not, and in `tls.Client`, favor
AES-based ciphers only if hardware support is available.

This matches what BoringSSL is doing.
2023-03-22 17:58:24 +01:00
Bas Westerbaan
2089b3f193
tls: use post-quantum secure key exchange (#14920) 2023-03-17 17:51:24 +01:00
Nameless
0a4130f364
std.http: handle relative redirects 2023-03-09 14:55:13 -06:00
Nameless
8d86194b6e
add error sets to tcpConnect* and tls.Client.init 2023-03-09 14:54:26 -06:00
Andrew Kelley
d711f459ae std.crypto.tls.Client: fix crash in read()
An abstraction for giving bytes to the read buffer didn't check for an
out-of-space condition.
2023-01-05 19:57:00 -07:00
Andrew Kelley
7178451d62 std.crypto.tls.Client: make close_notify optional
Although RFC 8446 states:

> Each party MUST send a "close_notify" alert before closing its write
> side of the connection

In practice many servers do not do this. Also in practice, truncation
attacks are thwarted at the application layer by comparing the amount of
bytes received with the amount expected via the HTTP headers.
2023-01-02 18:27:38 -07:00
Andrew Kelley
9ca6d67345 std.crypto.tls.Certificate: make the current time a parameter 2023-01-02 16:57:16 -07:00
Andrew Kelley
97acdeeca8 std.crypto.tls: verify via Subject Alt Name
Previously, the code only checked Common Name, leading to unable to
validate valid certificates which relied on the subject_alt_name
extension for host name verification.

This commit also adds rsa_pss_rsae_* back to the signature algorithms
list in the ClientHello.
2023-01-02 16:57:16 -07:00
Andrew Kelley
611a1fdd6d std.crypto.tls: add API for sending close_notify
This commit adds `writeEnd` and `writeAllEnd` in order to send data and
also notify the server that there will be no more data written.

Unfortunately, it seems most TLS implementations in the wild get this
wrong and immediately close the socket when they see a close_notify,
rather than only ending the data stream on the application layer.
2023-01-02 16:57:16 -07:00
Andrew Kelley
0fb78b15aa std.crypto.tls: use a Decoder abstraction
This commit introduces tls.Decoder and then uses it in tls.Client. The
purpose is to make it difficult to introduce vulnerabilities in the
parsing code. With this abstraction in place, bugs in the TLS
implementation will trip checks in the decoder, regardless of the actual
length of packets sent by the other party, so that we can have
confidence when using ReleaseFast builds.
2023-01-02 16:57:16 -07:00
Andrew Kelley
341e68ff8f std.crypto.tls.Client: remove debug prints 2023-01-02 16:57:16 -07:00
Andrew Kelley
79b41dbdbf std.crypto.tls: avoid heap allocation
The code we are borrowing from https://github.com/shiguredo/tls13-zig
requires an Allocator for doing RSA certificate verification. As a
stopgap measure, this commit uses a FixedBufferAllocator to avoid heap
allocation for these functions.

Thank you to @naoki9911 for providing this great resource which has been
extremely helpful for me when working on this standard library TLS
implementation. Until Zig has std.crypto.rsa officially, we will borrow
this implementation of RSA. 🙏
2023-01-02 16:57:16 -07:00
Andrew Kelley
05fee3b22b std.crypto.tls.Client: fix eof logic
Before this, it incorrectly returned true when there was still cleartext
to be read.
2023-01-02 16:57:16 -07:00
Andrew Kelley
22e2aaa283 crypto.tls: support rsa_pss_rsae_sha256 and fixes
* fix eof logic
 * fix read logic
 * fix VecPut logic
 * add some debug prints to remove later
2023-01-02 16:57:16 -07:00
Andrew Kelley
e4a9b19a14 std.crypto.tls.Client: rework the read function
Here's what I landed on for the TLS client. It's 16896 bytes
(max_ciphertext_record_len is 16640). I believe this is the theoretical
minimum size, give or take a few bytes.

These constraints are satisfied:
 * a call to the readvAdvanced() function makes at most one call to the
   underlying readv function
 * iovecs are provided by the API, and used by the implementation for
   underlying readv() calls to the socket
 * the theoretical minimum number of memcpy() calls are issued in all
   circumstances
 * decryption is only performed once for any given TLS record
 * large read buffers are fully exploited

This is accomplished by using the partial read buffer to storing both
cleartext and ciphertext.
2023-01-02 16:57:16 -07:00
Andrew Kelley
1d20ada366 std.crypto.tls.Client: refactor to reduce namespace bloat 2023-01-02 16:57:16 -07:00
Andrew Kelley
16af6286c8 std.crypto.tls.Client: support SignatureScheme.ecdsa_secp384r1_sha384 2023-01-02 16:57:16 -07:00
Andrew Kelley
940d368e7e std.crypto.tls.Client: fix the read function
The read function has been renamed to readAdvanced since it has slightly
different semantics than typical read functions, specifically regarding
the end-of-file. A higher level read function is implemented on top.

Now, API users may pass small buffers to the read function and
everything will work fine. This is done by re-decrypting the same
ciphertext record with each call to read() until the record is finished
being transmitted.

If the buffer supplied to read() is large enough, then any given
ciphertext record will only be decrypted once, since it decrypts
directly to the read() buffer and therefore does not need any memcpy. On
the other hand, if the buffer supplied to read() is small, then the
ciphertext is decrypted into a stack buffer, a subset is copied to the
read() buffer, and then the entire ciphertext record is saved for the
next call to read().
2023-01-02 16:57:16 -07:00
Andrew Kelley
21ab99174e std.crypto.tls.Client: use enums more 2023-01-02 16:57:16 -07:00
Andrew Kelley
477864dca5 std.crypto.tls.Client: fix truncation attack vulnerability 2023-01-02 16:57:16 -07:00
Andrew Kelley
ceb211e65f std.crypto.tls.Client: handle key_update message 2023-01-02 16:57:15 -07:00
Andrew Kelley
b24f178029 std.crypto.tls.Certificate: fix parsing missing subsequent fields
Instead of seeing all the attributed types and values, the code was only
seeing the first one.
2023-01-02 16:57:15 -07:00
Andrew Kelley
642a8b05c3 std.crypto.tls.Certificate: explicit error set for verify 2023-01-02 16:57:15 -07:00
Andrew Kelley
862ecf2344 std.crypto.tls.Client: handle extra data after handshake 2023-01-02 16:57:15 -07:00
Andrew Kelley
16f936b420 std.crypto.tls: handle the certificate_verify message 2023-01-02 16:57:15 -07:00