blob: 54d1ed8532b40bfa4de51c6254e75a25559e2add [file] [log] [blame] [view]
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +02001This document lists current limitations of the PSA Crypto API (as of version
21.1) that may impact our ability to (1) use it for all crypto operations in
3TLS and X.509 and (2) support isolation of all long-term secrets in TLS (that
4is, goals G1 and G2 in [strategy.md][] in the same directory).
5
6This is supposed to be a complete list, based on a exhaustive review of crypto
7operations done in TLS and X.509 code, but of course it's still possible that
8subtle-but-important issues have been missed. The only way to be really sure
9is, of course, to actually do the migration work.
10
11Limitations relevant for G1 (performing crypto operations)
12==========================================================
13
14Restartable ECC operations
15--------------------------
16
17There is currently no support for that in PSA at all. API design, as well as
18implementation, would be non-trivial.
19
20Currently, `MBEDTLS_USE_PSA_CRYPTO` is simply incompatible with
21`MBEDTLS_ECP_RESTARTABLE`.
22
23Arbitrary parameters for FFDH
24-----------------------------
25
26Currently, the PSA Crypto API can only perform FFDH with a limited set of
27well-know parameters (some of them defined in the spec, but implementations
28are free to extend that set).
29
30TLS 1.2 (and earlier) on the other hand have the server send explicit
31parameters (P and G) in is ServerKeyExchange message. This has been found to
32be suboptimal for security, as it is prohibitively hard for the client to
33verify the strength of these parameters. This led to the development of RFC
347919 which allows use of named groups in TLS 1.2 - however as this is only an
35extension, servers can still send custom parameters if they don't support the
36extension.
37
38In TLS 1.3 the situation will be simpler: named groups are the only
39option, so the current PSA Crypto API is a good match for that. (Not
40coincidentally, the groups used by RFC 7919 and TLS 1.3 are part those defined
41in the specification.)
42
43There are several options here:
44
451. Implement support for custom FFDH parameters in PSA Crypto: this would pose
46 non-trivial API design problem, but most importantly seems backwards, as
47the crypto community is moving away from custom FFDH parameters.
482. Drop the DHE-RSA and DHE-PSK key exchanges in TLS 1.2 when moving to PSA.
493. Implement RFC 7919, support DHE-RSA and DHE-PSK only in conjunction with it
50 when moving to PSA. We can modify our server so that it only selects a DHE
51 ciphersuite if the client offered name FFDH groups; unfortunately
52client-side the only option is to offer named groups and break the handshake
53if the server didn't take on our offer. This is not fully satisfying, but is
54perhaps the least unsatisfying option in terms of result; it's also probably
55the one that requires the most work, but it would deliver value beyond PSA
56migration by implementing RFC 7919.
57
58RSA-PSS parameters
59------------------
60
61RSA-PSS signatures are defined by PKCS#1 v2, re-published as RFC 8017
62(previously RFC 3447).
63
64As standardized, the signature scheme takes several parameters, in addition to
65the hash algorithm potentially used to hash the message being signed:
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +020066- a hash algorithm used for the encoding function
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +020067- a mask generation function
68 - most commonly MGF1, which in turn is parametrized by a hash algorithm
69- a salt length
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +020070- a trailer field - this is universally 0xBC as far as I've seen
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +020071
72Both the existing `mbedtls_` API and the PSA API support only MGF1 as the
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +020073generation function (and only 0xBC as the trailer field), but there are
74discrepancies in handling the salt length and which of the various hash
75algorithms can differ from each other.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +020076
77### API comparison
78
79- RSA:
80 - signature: `mbedtls_rsa_rsassa_pss_sign()`
81 - message hashed externally
82 - encoding hash = MGF1 hash (from context, or argument = message hash)
83 - salt length: always using the maximum legal value
84 - signature: `mbedtls_rsa_rsassa_pss_sign_ext()`
85 - message hashed externally
86 - encoding hash = MGF1 hash (from context, or argument = message hash)
87 - salt length: specified explicitly
88 - verification: `mbedtls_rsassa_pss_verify()`
89 - message hashed externally
90 - encoding hash = MGF1 hash (from context, or argument = message hash)
91 - salt length: any valid length accepted
92 - verification: `mbedtls_rsassa_pss_verify_ext()`
93 - message hashed externally
94 - encoding hash = MGF1 hash from dedicated argument
95 - expected salt length: specified explicitly, can specify "ANY"
96- PK:
97 - signature: not supported
98 - verification: `mbedtls_pk_verify_ext()`
99 - message hashed externally
100 - encoding hash = MGF1 hash, specified explicitly
101 - expected salt length: specified explicitly, can specify "ANY"
102- PSA:
103 - algorithm specification:
104 - hash alg used for message hashing, encoding and MGF1
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200105 - salt length can be either "standard" (== hashlen) or "any"
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200106 - signature generation:
107 - salt length: always using the maximum legal value
108 - verification:
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200109 - salt length: either == hashlen, or any depending on algorithm
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200110
111The RSA/PK API is in principle more flexible than the PSA Crypto API. The
112following sub-sections study whether and how this matters in practice.
113
114### Use in X.509
115
116RFC 4055 Section 3.1 defines the encoding of RSA-PSS that's used in X.509.
117It allows independently specifying the message hash (also used for encoding
118hash), the MGF (and its hash if MGF1 is used), and the salt length (plus an
119extra parameter "trailer field" that doesn't vary in practice"). These can be
120encoded as part of the key, and of the signature. If both encoding are
121presents, all values must match except possibly for the salt length, where the
122value from the signature parameters is used.
123
124In Mbed TLS, RSA-PSS parameters can be parsed and displayed for various
125objects (certificates, CRLs, CSRs). During parsing, the following properties
126are enforced:
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200127- the extra "trailer field" parameter must have its default value
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200128- the mask generation function is MGF1
129- encoding hash = message hashing algorithm (may differ from MGF1 hash)
130
131When it comes to cryptographic operations, only two things are supported:
132- verifying the signature on a certificate from its parent;
133- verifying the signature on a CRL from the issuing CA.
134
135The verification is done using `mbedtls_pk_verify_ext()`.
136
137Note: since X.509 parsing ensures that message hash = encoding hash, and
138`mbedtls_pk_verify_ext()` use encoding hash = mgf1 hash, it looks like all
139three hash algorithms must be equal, which would be good news as it would
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200140match a limitation of the PSA API.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200141
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200142It is unclear what parameters people use in practice. It looks like by default
143OpenSSL picks saltlen = keylen - hashlen - 2 (tested with openssl 1.1.1f).
144The `certool` command provided by GnuTLS seems to be picking saltlen = hashlen
145by default (tested with GnuTLS 3.6.13).
Manuel Pégourié-Gonnardf5ee4b32021-10-21 13:04:01 +0200146
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200147### Use in TLS
148
149In TLS 1.2 (or lower), RSA-PSS signatures are never used, except via X.509.
150
151In TLS 1.3, RSA-PSS signatures can be used directly in the protocol (in
152addition to indirect use via X.509). It has two sets of three signature
153algorithm identifiers (for SHA-256, SHA-384 and SHA-512), depending of what
154the OID of the public key is (rsaEncryption or RSASSA-PSS).
155
156In both cases, it specifies that:
157- the mask generation function is MGF1
158- all three hashes are equal
159- the length of the salt MUST be equal to the length of the digest algorithm
160
161When signing, the salt length picked by PSA is the one required by TLS 1.3
162(unless the key is unreasonably small).
163
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200164When verifying signatures, PSA will by default enforce the salt len is the one
165required by TLS 1.3.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200166
167### Current testing - X509
168
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200169All test files use the default trailer field of 0xBC, as enforced by our
170parser. (There's a negative test for that using the
171`x509_parse_rsassa_pss_params` test function and hex data.)
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200172
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200173Files with "bad" in the name are expected to be invalid and rejected in tests.
Manuel Pégourié-Gonnardf5ee4b32021-10-21 13:04:01 +0200174
175**Test certificates:**
176
177server9-bad-mgfhash.crt (announcing mgf1(sha224), signed with another mgf)
178 Hash Algorithm: sha256
179 Mask Algorithm: mgf1 with sha224
180 Salt Length: 0xDE
181server9-bad-saltlen.crt (announcing saltlen = 0xDE, signed with another len)
182 Hash Algorithm: sha256
183 Mask Algorithm: mgf1 with sha256
184 Salt Length: 0xDE
185server9-badsign.crt (one bit flipped in the signature)
186 Hash Algorithm: sha1 (default)
187 Mask Algorithm: mgf1 with sha1 (default)
188 Salt Length: 0xEA
189server9-defaults.crt
190 Hash Algorithm: sha1 (default)
191 Mask Algorithm: mgf1 with sha1 (default)
192 Salt Length: 0x14 (default)
193server9-sha224.crt
194 Hash Algorithm: sha224
195 Mask Algorithm: mgf1 with sha224
196 Salt Length: 0xE2
197server9-sha256.crt
198 Hash Algorithm: sha256
199 Mask Algorithm: mgf1 with sha256
200 Salt Length: 0xDE
201server9-sha384.crt
202 Hash Algorithm: sha384
203 Mask Algorithm: mgf1 with sha384
204 Salt Length: 0xCE
205server9-sha512.crt
206 Hash Algorithm: sha512
207 Mask Algorithm: mgf1 with sha512
208 Salt Length: 0xBE
209server9-with-ca.crt
210 Hash Algorithm: sha1 (default)
211 Mask Algorithm: mgf1 with sha1 (default)
212 Salt Length: 0xEA
213server9.crt
214 Hash Algorithm: sha1 (default)
215 Mask Algorithm: mgf1 with sha1 (default)
216 Salt Length: 0xEA
217
218These certificates are signed with a 2048-bit key. It appears that they are
219all using saltlen = keylen - hashlen - 2, except for server9-defaults which is
220using saltlen = hashlen.
221
222**Test CRLs:**
223
224crl-rsa-pss-sha1-badsign.pem
225 Hash Algorithm: sha1 (default)
226 Mask Algorithm: mgf1 with sha1 (default)
227 Salt Length: 0xEA
228crl-rsa-pss-sha1.pem
229 Hash Algorithm: sha1 (default)
230 Mask Algorithm: mgf1 with sha1 (default)
231 Salt Length: 0xEA
232crl-rsa-pss-sha224.pem
233 Hash Algorithm: sha224
234 Mask Algorithm: mgf1 with sha224
235 Salt Length: 0xE2
236crl-rsa-pss-sha256.pem
237 Hash Algorithm: sha256
238 Mask Algorithm: mgf1 with sha256
239 Salt Length: 0xDE
240crl-rsa-pss-sha384.pem
241 Hash Algorithm: sha384
242 Mask Algorithm: mgf1 with sha384
243 Salt Length: 0xCE
244crl-rsa-pss-sha512.pem
245 Hash Algorithm: sha512
246 Mask Algorithm: mgf1 with sha512
247 Salt Length: 0xBE
248
249These CRLs are signed with a 2048-bit key. It appears that they are
250all using saltlen = keylen - hashlen - 2.
251
252**Test CSRs:**
253
254server9.req.sha1
255 Hash Algorithm: sha1 (default)
256 Mask Algorithm: mgf1 with sha1 (default)
257 Salt Length: 0x6A
258server9.req.sha224
259 Hash Algorithm: sha224
260 Mask Algorithm: mgf1 with sha224
261 Salt Length: 0x62
262server9.req.sha256
263 Hash Algorithm: sha256
264 Mask Algorithm: mgf1 with sha256
265 Salt Length: 0x5E
266server9.req.sha384
267 Hash Algorithm: sha384
268 Mask Algorithm: mgf1 with sha384
269 Salt Length: 0x4E
270server9.req.sha512
271 Hash Algorithm: sha512
272 Mask Algorithm: mgf1 with sha512
273 Salt Length: 0x3E
274
275These CSRss are signed with a 2048-bit key. It appears that they are
276all using saltlen = keylen - hashlen - 2.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200277
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200278### Possible courses of action
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200279
Manuel Pégourié-Gonnarde459be22021-10-27 13:25:49 +0200280There's no question about what to do with TLS (any version); the only question
281is about X.509 signature verification. Options include:
282
2831. Doing all verifications with `PSA_ALG_RSA_PSS_ANY_SALT` - while this
284 wouldn't cause a concrete security issue, this would be non-compliant.
2852. Doing verifications with `PSA_ALG_RSA_PSS` when we're lucky and the encoded
286 saltlen happens to match hashlen, and falling back to `ANY_SALT` otherwise.
287Same issue as with the previous point, except more contained.
2883. Reject all certificates with saltlen != hashlen. This includes all
289 certificates generate with OpenSSL using the default parameters, so it's
290probably not acceptable.
2914. Request an extension to the PSA Crypto API and use one of the above options
292 in the meantime. Such an extension seems inconvenient and not motivated by
293strong security arguments, so it's unclear whether it would be accepted.
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200294
Manuel Pégourié-Gonnard52187742022-01-17 11:02:31 +0100295HKDF: Expand not exposed on its own (TLS 1.3)
296---------------------------------------------
297
298The HKDF function uses and Extract-then-Expand approch, that is:
299
300 HKDF(x, ...) = HKDF-Expand(HKDF-Extract(x, ...), ...)
301
302Only the full HKDF function is safe in general, however there are cases when
303one case safely use the individual Extract and Expand; the TLS 1.3 key
304schedule does so. Specifically, looking at the [hierarchy of secrets][13hs]
305is seems that Expand and Extract are always chained, so that this hierarchy
306can be implemented using only the full HKDF. However, looking at the
307derivation of traffic keys (7.3) and the update mechanism (7.2) it appears
308that calls to HKDF-Expand are iterated without any intermediated call to
309HKDF-Extract : that is, the traffic keys are computed as
310
311 HKDF-Expand(HKDF-Expand(HKDF-Extract(...)))
312
313(with possibly more than two Expands in a row with update).
314
315[13hs]: https://datatracker.ietf.org/doc/html/rfc8446#page-93
316
317In the short term (early 2022), we'll work around that by re-implementing HKDF
318in `ssl_tls13_keys.c` based on the `psa_mac_` APIs (for HMAC).
319
320In the long term, it is desirable to extend the PSA API.
321
Manuel Pégourié-Gonnardd9edd562021-09-30 15:05:01 +0200322Limitations relevant for G2 (isolation of long-term secrets)
323============================================================
324
325Custom key derivations for mixed-PSK handshake
326----------------------------------------------
327
328Currently, `MBEDTLS_USE_PSA_CRYPTO` enables the new configuration function
329`mbedtls_ssl_conf_psk_opaque()` which allows a PSA-held key to be used for the
330(pure) `PSK` key exchange in TLS 1.2. This requires that the derivation of the
331Master Secret (MS) be done on the PSA side. To support this, an algorithm
332family `PSA_ALG_TLS12_PSK_TO_MS(hash_alg)` was added to PSA Crypto.
333
334If we want to support key isolation for the "mixed PSK" key exchanges:
335DHE-PSK, RSA-PSK, ECDHE-PSK, where the PSK is concatenated with the result of
336a DH key agreement (resp. RSA decryption) to form the pre-master secret (PMS)
337from which the MS is derived. If the value of the PSK is to remain hidden, we
338need the derivation PSK + secondary secret -> MS to be implemented as an
339ad-hoc PSA key derivation algorithm.
340
341Adding this new, TLS-specific, key derivation algorithm to PSA Crypto should
342be no harder than it was to add `PSA_ALG_TLS12_PSK_TO_MS()` but still requires
343an extension to PSA Crypto.
344
345Note: looking at RFCs 4279 and 5489, it appears that the structure of the PMS
346is always the same: 2-byte length of the secondary secret, secondary secret,
3472-byte length of the PSK, PSK. So, a single key derivation algorithm should be
348able to cover the 3 key exchanges DHE-PSK, RSA-PSK and ECDHE-PSK. (That's a
349minor gain: adding 3 algorithms would not be a blocker anyway.)
350
351Note: if later we want to also isolate short-term secret (G3), the "secondary
352secret" (output of DHE/ECDHE key agreement or RSA decryption) could be a
353candidate. This wouldn't be a problem as the PSA key derivation API always
354allows inputs from key slots. (Tangent: the hard part in isolating the result
355of RSA decryption would be still checking that is has the correct format:
35648 bytes, the first two matching the TLS version - note that this is timing
357sensitive.)
358
Manuel Pégourié-Gonnard52187742022-01-17 11:02:31 +0100359HKDF: Expand not exposed on its own (TLS 1.3)
360---------------------------------------------
361
362See the section with the same namw in the G1 part above for background.
363
364The work-around mentioned there works well enough just for acceleration, but
365is not sufficient for key isolation or generally proper key management (it
366requires marking keys are usable for HMAC while they should only be used for
367key derivation).
368
369The obvious long-term solution is to make HKDF-Expand available as a new KDF
370(in addition to the full HKDF) in PSA (with appropriate warnings in the
371documentation).