aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/s2n/utils/s2n_safety.h
blob: 67a0328901ffdbfb1f0e7fafe20646b73d108386 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#pragma once

#include <string.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>

#include "error/s2n_errno.h"
#include "utils/s2n_ensure.h"
#include "utils/s2n_result.h"

/* Success signal value for OpenSSL functions */
#define _OSSL_SUCCESS 1

/**
 * The goal of s2n_safety is to provide helpers to perform common
 * checks, which help with code readability.
 */

/**
 * Sets the global `errno` and returns with a `S2N_RESULT_ERROR`
 */
#define BAIL( x )                                    do { _S2N_ERROR( ( x ) ); return S2N_RESULT_ERROR; } while (0)

/**
 * Sets the global `errno` and returns with a POSIX error (`-1`)
 */
#define BAIL_POSIX( x )                              do { _S2N_ERROR( ( x ) ); return S2N_FAILURE; } while (0)

/**
 * Sets the global `errno` and returns with a `NULL` pointer value
 */
#define BAIL_PTR( x )                                do { _S2N_ERROR( ( x ) ); return NULL; } while (0)

/**
 * Ensures the `condition` is `true`, otherwise the function will `BAIL` with an `error`
 */
#define ENSURE( condition , error )                  __S2N_ENSURE((condition), BAIL(error))

/**
 * Ensures the `result` is OK, otherwise the function will `BAIL` with an `error`
 */
#define ENSURE_OK( result , error )                  __S2N_ENSURE(s2n_result_is_ok(result), BAIL(error))

/**
 * Ensures `n` is greater than or equal to `min`, otherwise the function will `BAIL` with a `S2N_ERR_SAFETY` error
 */
#define ENSURE_GTE( n , min )                        ENSURE((n) >= (min), S2N_ERR_SAFETY)

/**
 * Ensures `n` is less than or equal to `max`, otherwise the function will `BAIL` with a `S2N_ERR_SAFETY` error
 */
#define ENSURE_LTE( n , max )                        ENSURE((n) <= (max), S2N_ERR_SAFETY)

/**
 * Ensures `n` is greater than `min`, otherwise the function will `BAIL` with a `S2N_ERR_SAFETY` error
 */
#define ENSURE_GT( n , min )                         ENSURE((n) > (min), S2N_ERR_SAFETY)

/**
 * Ensures `n` is less than `min`, otherwise the function will `BAIL` with a `S2N_ERR_SAFETY` error
 */
#define ENSURE_LT( n , max )                         ENSURE((n) < (max), S2N_ERR_SAFETY)

/**
 * Ensures `a` is equal to `b`, otherwise the function will `BAIL` with a `S2N_ERR_SAFETY` error
 */
#define ENSURE_EQ( a , b )                           ENSURE((a) == (b), S2N_ERR_SAFETY)

/**
 * Ensures `a` is not equal to `b`, otherwise the function will `BAIL` with a `S2N_ERR_SAFETY` error
 */
#define ENSURE_NE( a , b )                           ENSURE((a) != (b), S2N_ERR_SAFETY)

/**
 * Ensures the `condition` is `true`, otherwise the function will `BAIL_POSIX` with an `error`
 */
#define ENSURE_POSIX( condition , error )           __S2N_ENSURE((condition), BAIL_POSIX(error))

/**
 * Ensures the `condition` is `true`, otherwise the function will `BAIL_PTR` with an `error`
 */
#define ENSURE_PTR( condition , error )             __S2N_ENSURE((condition), BAIL_PTR(error))

/**
 * Ensures `x` is not `NULL`, otherwise the function will `BAIL_PTR` with an `error`
 */
#define ENSURE_REF_PTR( x )                         ENSURE_PTR(S2N_OBJECT_PTR_IS_READABLE(x), S2N_ERR_NULL)

/**
 * Ensures `x` is a readable reference, otherwise the function will `BAIL` with `S2N_ERR_NULL`
 */
#define ENSURE_REF( x )                             ENSURE(S2N_OBJECT_PTR_IS_READABLE(x), S2N_ERR_NULL)

/**
 * Ensures `x` is a readable reference, otherwise the function will `BAIL_POSIX` with `S2N_ERR_NULL`
 */
#define ENSURE_POSIX_REF( x )                       ENSURE_POSIX(S2N_OBJECT_PTR_IS_READABLE(x), S2N_ERR_NULL)

/**
 * Ensures `x` is a mutable reference, otherwise the function will `BAIL` with `S2N_ERR_NULL`
 */
#define ENSURE_MUT( x )                             ENSURE(S2N_OBJECT_PTR_IS_WRITABLE(x), S2N_ERR_NULL)

/**
 * Ensures `x` is a mutable reference, otherwise the function will `BAIL_POSIX` with `S2N_ERR_NULL`
 */
#define ENSURE_POSIX_MUT( x )                       ENSURE_POSIX(S2N_OBJECT_PTR_IS_WRITABLE(x), S2N_ERR_NULL)

/**
 * Ensures `min <= n <= max`
 */
#define ENSURE_INCLUSIVE_RANGE( min , n , max )      \
  do {                                               \
    __typeof( n ) __tmp_n = ( n );                   \
    ENSURE_GTE(__tmp_n, min);                        \
    ENSURE_LTE(__tmp_n, max);                        \
  } while(0)

/**
 * Ensures `min < n < max`
 */
#define ENSURE_EXCLUSIVE_RANGE( min , n , max )      \
  do {                                               \
    __typeof( n ) __tmp_n = ( n );                   \
    ENSURE_GT(__tmp_n, min);                         \
    ENSURE_LT(__tmp_n, max);                         \
  } while(0)

/**
 * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
 */
#define PRECONDITION( result )                      GUARD_RESULT(__S2N_ENSURE_PRECONDITION(result))

/**
 * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
 */
#define POSTCONDITION( result )                     GUARD_RESULT(__S2N_ENSURE_POSTCONDITION(result))

/**
 * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
 */
#define PRECONDITION_POSIX( result )                GUARD_AS_POSIX(__S2N_ENSURE_PRECONDITION(result))

/**
 * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
 */
#define POSTCONDITION_POSIX( result )               GUARD_AS_POSIX(__S2N_ENSURE_POSTCONDITION(result))

/**
 * Ensures the `condition` is `true`, otherwise the function will `BAIL` with an `error`.
 * When the code is built in debug mode, they are checked.
 * When the code is built in production mode, they are ignored.
 */
#define DEBUG_ENSURE( condition, error )            __S2N_ENSURE_DEBUG((condition), BAIL(error))

/**
 * Ensures the `condition` is `true`, otherwise the function will `BAIL_POSIX` with an `error`.
 * When the code is built in debug mode, they are checked.
 * When the code is built in production mode, they are ignored.
 */
#define DEBUG_ENSURE_POSIX( condition, error )      __S2N_ENSURE_DEBUG((condition), BAIL_POSIX(error))

/**
 * Ensures `x` is not an error, otherwise the function will return an error signal
 *
 * Note: this currently accepts POSIX error signals but will transition to accept s2n_result
 */
#define GUARD( x )                                  GUARD_POSIX(x)

/**
 * Ensures `x` is not an error, otherwise goto `label`
 *
 * Note: this currently accepts POSIX error signals but will transition to accept s2n_result
 */
#define GUARD_GOTO( x , label )                     GUARD_POSIX_GOTO((x), (label))

/**
 * Ensures `x` is not an error, otherwise the function will return `NULL`
 *
 * Note: this currently accepts POSIX error signals but will transition to accept s2n_result
 */
#define GUARD_PTR( x )                              GUARD_POSIX_PTR(x)

/**
 * Ensures `x` is not `NULL`, otherwise the function will return an error signal
 *
 * Note: this currently accepts POSIX error signals but will transition to accept s2n_result
 */
#define GUARD_NONNULL( x )                          GUARD_POSIX_NONNULL(x)

/**
 * Ensures `x` is not `NULL`, otherwise goto `label`
 *
 * Note: this currently accepts POSIX error signals but will transition to accept s2n_result
 */
#define GUARD_NONNULL_GOTO( x , label )             __S2N_ENSURE((x) != NULL, goto label)

/**
 * Ensures `x` is not `NULL`, otherwise the function will return `NULL`
 *
 * Note: this currently accepts POSIX error signals but will transition to accept s2n_result
 */
#define GUARD_NONNULL_PTR( x )                      __S2N_ENSURE((x) != NULL, return NULL)

/**
 * Ensures `x` is not a OpenSSL error, otherwise the function will return an error signal
 *
 * Note: this currently accepts POSIX error signals but will transition to accept s2n_result
 */
#define GUARD_OSSL( x, error )                      GUARD_POSIX_OSSL((x), (error))

/**
 * Ensures `x` is ok, otherwise the function will return an `S2N_RESULT_ERROR`
 */
#define GUARD_RESULT( x )                           __S2N_ENSURE(s2n_result_is_ok(x), return S2N_RESULT_ERROR)

/**
 * Ensures `x` is ok, otherwise goto `label`
 */
#define GUARD_RESULT_GOTO( x, label )               __S2N_ENSURE(s2n_result_is_ok(x), goto label)

/**
 * Ensures `x` is ok, otherwise the function will return `NULL`
 */
#define GUARD_RESULT_PTR( x )                       __S2N_ENSURE(s2n_result_is_ok(x), return NULL)

/**
 * Ensures `x` is not `NULL`, otherwise the function will return an `S2N_RESULT_ERROR`
 */
#define GUARD_RESULT_NONNULL( x )                   __S2N_ENSURE((x) != NULL, return S2N_RESULT_ERROR)

/**
 * Ensures `x` is not a OpenSSL error, otherwise the function will `BAIL` with `error`
 */
/* TODO: use the OSSL error code in error reporting https://github.com/awslabs/s2n/issues/705 */
#define GUARD_RESULT_OSSL( x , error )              ENSURE((x) == _OSSL_SUCCESS, error)

/**
 * Ensures `x` is not a POSIX error, otherwise return a POSIX error
 */
#define GUARD_POSIX( x )                            __S2N_ENSURE((x) >= S2N_SUCCESS, return S2N_FAILURE)

/**
 * Ensures `x` is strictly not a POSIX error (`-1`), otherwise goto `label`
 */
#define GUARD_POSIX_STRICT( x )                     __S2N_ENSURE((x) == S2N_SUCCESS, return S2N_FAILURE)

/**
 * Ensures `x` is not a POSIX error, otherwise goto `label`
 */
#define GUARD_POSIX_GOTO( x , label )               __S2N_ENSURE((x) >= S2N_SUCCESS, goto label)

/**
 * Ensures `x` is not a POSIX error, otherwise the function will return `NULL`
 */
#define GUARD_POSIX_PTR( x )                        __S2N_ENSURE((x) >= S2N_SUCCESS, return NULL)

/**
 * Ensures `x` is not `NULL`, otherwise the function will return a POSIX error (`-1`)
 */
#define GUARD_POSIX_NONNULL( x )                    __S2N_ENSURE((x) != NULL, return S2N_FAILURE)

/**
 * Ensures `x` is not a OpenSSL error, otherwise the function will `BAIL` with `error`
 */
/* TODO: use the OSSL error code in error reporting https://github.com/awslabs/s2n/issues/705 */
#define GUARD_POSIX_OSSL( x , error )               ENSURE_POSIX((x) == _OSSL_SUCCESS, error)

/**
 * Ensures `x` is not a POSIX error, otherwise the function will return a `S2N_RESULT_ERROR`
 */
#define GUARD_AS_RESULT( x )                        __S2N_ENSURE((x) >= S2N_SUCCESS, return S2N_RESULT_ERROR)

/**
 * Ensures `x` is OK (S2N_RESULT), otherwise the function will return a POSIX error (`-1`)
 */
#define GUARD_AS_POSIX( x )                         __S2N_ENSURE(s2n_result_is_ok(x), return S2N_FAILURE)

/**
 * Performs a safe memcpy
 */
#define CHECKED_MEMCPY( d , s , n )                 __S2N_ENSURE_SAFE_MEMCPY((d), (s), (n), GUARD_RESULT_NONNULL)

/**
 * Performs a safe memset
 */
#define CHECKED_MEMSET( d , c , n )                 __S2N_ENSURE_SAFE_MEMSET((d), (c), (n), ENSURE_REF)

/**
 * Marks a case of a switch statement as able to fall through to the next case
 */
#if (defined(__clang__) && __clang_major__ >= 10) || (defined(__GNUC__) && __GNUC__ >= 7)
#    define FALL_THROUGH __attribute__((fallthrough))
#else
#    define FALL_THROUGH ((void)0)
#endif

/* Returns `true` if s2n is in unit test mode, `false` otherwise */
bool s2n_in_unit_test();

/* Sets whether s2n is in unit test mode */
int s2n_in_unit_test_set(bool newval);

#define S2N_IN_INTEG_TEST ( getenv("S2N_INTEG_TEST") != NULL )
#define S2N_IN_TEST ( s2n_in_unit_test() || S2N_IN_INTEG_TEST )

/**
 * Get the process id
 *
 * Returns:
 *  The process ID of the current process
 */
extern pid_t s2n_actual_getpid();

/* Returns 1 if a and b are equal, in constant time */
extern bool s2n_constant_time_equals(const uint8_t * a, const uint8_t * b, const uint32_t len);

/* Copy src to dst, or don't copy it, in constant time */
extern int s2n_constant_time_copy_or_dont(uint8_t * dst, const uint8_t * src, uint32_t len, uint8_t dont);

/* If src contains valid PKCS#1 v1.5 padding of exactly expectlen bytes, decode
 * it into dst, otherwise leave dst alone, in constant time.
 * Always returns zero. */
extern int s2n_constant_time_pkcs1_unpad_or_dont(uint8_t * dst, const uint8_t * src, uint32_t srclen, uint32_t expectlen);

/**
 * Runs _thecleanup function on _thealloc once _thealloc went out of scope
 */
#define DEFER_CLEANUP(_thealloc, _thecleanup) \
   __attribute__((cleanup(_thecleanup))) _thealloc

/* Creates cleanup function for pointers from function func which accepts a pointer.
 * This is useful for DEFER_CLEANUP as it passes &_thealloc into _thecleanup function,
 * so if _thealloc is a pointer _thecleanup will receive a pointer to a pointer.*/
#define DEFINE_POINTER_CLEANUP_FUNC(type, func)             \
  static inline void func##_pointer(type *p) {              \
    if (p && *p)                                            \
      func(*p);                                             \
  }                                                         \
  struct __useless_struct_to_allow_trailing_semicolon__

#define s2n_array_len(array) ((array != NULL) ? (sizeof(array) / sizeof(array[0])) : 0)

extern int s2n_mul_overflow(uint32_t a, uint32_t b, uint32_t* out);

/**
 * Rounds "initial" up to a multiple of "alignment", and stores the result in "out".
 * Raises an error if overflow would occur.
 * NOT CONSTANT TIME.
 */
extern int s2n_align_to(uint32_t initial, uint32_t alignment, uint32_t* out);
extern int s2n_add_overflow(uint32_t a, uint32_t b, uint32_t* out);
extern int s2n_sub_overflow(uint32_t a, uint32_t b, uint32_t* out);
/* START COMPATIBILITY LAYER */

/**
 * NOTE: This will be removed once everything is using s2n_result
 */

/* `NULL` check a pointer */

/* Note: this macro is replaced by ENSURE_POSIX_REF */
#define notnull_check(ptr)                          ENSURE_POSIX_REF(ptr)
/* Note: this macro is replaced by ENSURE_REF_PTR */
#define notnull_check_ptr(ptr)                      ENSURE_REF_PTR(ptr)

/* Range check a number */
#define gte_check( n , min )                        ENSURE_POSIX((n) >= (min), S2N_ERR_SAFETY)
#define lte_check( n , max )                        ENSURE_POSIX((n) <= (max), S2N_ERR_SAFETY)
#define gt_check( n , min )                         ENSURE_POSIX((n) > (min), S2N_ERR_SAFETY)
#define lt_check( n , max )                         ENSURE_POSIX((n) < (max), S2N_ERR_SAFETY)
#define eq_check( a , b )                           ENSURE_POSIX((a) == (b), S2N_ERR_SAFETY)
#define ne_check( a , b )                           ENSURE_POSIX((a) != (b), S2N_ERR_SAFETY)
#define inclusive_range_check( low, n, high )       \
  do  {                                             \
    __typeof( n ) __tmp_n = ( n );                  \
    gte_check(__tmp_n, low);                        \
    lte_check(__tmp_n, high);                       \
  } while (0)
#define exclusive_range_check( low, n, high )       \
  do {                                              \
    __typeof( n ) __tmp_n = ( n );                  \
    gt_check(__tmp_n, low);                         \
    lt_check(__tmp_n, high);                        \
  } while (0)

#define memcpy_check( d , s , n )                   __S2N_ENSURE_SAFE_MEMCPY((d), (s), (n), GUARD_POSIX_NONNULL)
/* This will fail to build if d is an array. Cast the array to a pointer first! */
#define memset_check( d , c , n )                   __S2N_ENSURE_SAFE_MEMSET((d), (c), (n), ENSURE_POSIX_REF)

/* END COMPATIBILITY LAYER */