Update BoringSSL to 8462a367bb57e9524c3d8eca9c62733c63a63cf4 (#399)

* Update BoringSSL umbrella header

* Update BoringSSL to 8462a367bb57e9524c3d8eca9c62733c63a63cf4
This commit is contained in:
Cory Benfield authored and GitHub committed 2022-09-02 04:38:36 -07:00
1 parent 024bbd111e
commit ba7c0d7f82
156 files changed
+3916 -3770

No files matched your search

+1 -1
View File
@@ -26,7 +26,7 @@ import class Foundation.ProcessInfo
// Sources/CNIOBoringSSL directory. The source repository is at
// https://boringssl.googlesource.com/boringssl.
//
// BoringSSL Commit: c239ffd0552179f358de31517391679e9b62ccd3
// BoringSSL Commit: 8462a367bb57e9524c3d8eca9c62733c63a63cf4
/// This function generates the dependencies we want to express.
///
+24 -39
View File
@@ -107,57 +107,42 @@ ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
time_t t, int offset_day,
long offset_sec) {
char *p;
struct tm *ts;
struct tm data;
size_t len = 20;
ASN1_GENERALIZEDTIME *tmps = NULL;
if (s == NULL) {
tmps = ASN1_GENERALIZEDTIME_new();
} else {
tmps = s;
}
if (tmps == NULL) {
if (!OPENSSL_gmtime(&t, &data)) {
return NULL;
}
ts = OPENSSL_gmtime(&t, &data);
if (ts == NULL) {
goto err;
}
if (offset_day || offset_sec) {
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) {
goto err;
if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
return NULL;
}
}
if (ts->tm_year < 0 - 1900 || ts->tm_year > 9999 - 1900) {
if (data.tm_year < 0 - 1900 || data.tm_year > 9999 - 1900) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
goto err;
return NULL;
}
p = (char *)tmps->data;
if ((p == NULL) || ((size_t)tmps->length < len)) {
p = OPENSSL_malloc(len);
if (p == NULL) {
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
goto err;
}
OPENSSL_free(tmps->data);
tmps->data = (unsigned char *)p;
}
char buf[16];
BIO_snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
data.tm_year + 1900, data.tm_mon + 1, data.tm_mday, data.tm_hour,
data.tm_min, data.tm_sec);
BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900,
ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
ts->tm_sec);
tmps->length = strlen(p);
tmps->type = V_ASN1_GENERALIZEDTIME;
return tmps;
err:
int free_s = 0;
if (s == NULL) {
ASN1_GENERALIZEDTIME_free(tmps);
free_s = 1;
s = ASN1_UTCTIME_new();
if (s == NULL) {
return NULL;
}
}
return NULL;
if (!ASN1_STRING_set(s, buf, strlen(buf))) {
if (free_s) {
ASN1_UTCTIME_free(s);
}
return NULL;
}
s->type = V_ASN1_GENERALIZEDTIME;
return s;
}
+2 -2
View File
@@ -56,13 +56,13 @@
#include <CNIOBoringSSL_asn1.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <CNIOBoringSSL_bytestring.h>
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_type_check.h>
#include "../internal.h"
@@ -353,7 +353,7 @@ static long asn1_string_get_long(const ASN1_STRING *a, int type) {
i64 = (int64_t)v;
fits_in_i64 = i64 >= 0;
}
OPENSSL_STATIC_ASSERT(sizeof(long) <= sizeof(int64_t), "long is too big");
static_assert(sizeof(long) <= sizeof(int64_t), "long is too big");
if (fits_in_i64 && LONG_MIN <= i64 && i64 <= LONG_MAX) {
return (long)i64;
+31 -70
View File
@@ -66,6 +66,7 @@
#include <CNIOBoringSSL_bytestring.h>
#include <CNIOBoringSSL_mem.h>
#include "../bytestring/internal.h"
#include "internal.h"
@@ -129,70 +130,46 @@ static int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes,
// appropriate.
static int do_buf(const unsigned char *buf, int buflen, int encoding,
int utf8_convert, unsigned long flags, char *quotes,
BIO *out) {
// Reject invalid UCS-4 and UCS-2 lengths without parsing.
unsigned long flags, char *quotes, BIO *out) {
int (*get_char)(CBS *cbs, uint32_t *out);
int get_char_error;
switch (encoding) {
case MBSTRING_UNIV:
if (buflen & 3) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UNIVERSALSTRING);
return -1;
}
get_char = cbs_get_utf32_be;
get_char_error = ASN1_R_INVALID_UNIVERSALSTRING;
break;
case MBSTRING_BMP:
if (buflen & 1) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BMPSTRING);
return -1;
}
get_char = cbs_get_ucs2_be;
get_char_error = ASN1_R_INVALID_BMPSTRING;
break;
case MBSTRING_ASC:
get_char = cbs_get_latin1;
get_char_error = ERR_R_INTERNAL_ERROR; // Should not be possible.
break;
case MBSTRING_UTF8:
get_char = cbs_get_utf8;
get_char_error = ASN1_R_INVALID_UTF8STRING;
break;
default:
assert(0);
return -1;
}
const unsigned char *p = buf;
const unsigned char *q = buf + buflen;
CBS cbs;
CBS_init(&cbs, buf, buflen);
int outlen = 0;
while (p != q) {
const int is_first = p == buf;
// TODO(davidben): Replace this with |cbs_get_ucs2_be|, etc., to check
// for invalid codepoints. Before doing that, enforce it in the parser,
// https://crbug.com/boringssl/427, so these error cases are not
// reachable from parsed objects.
while (CBS_len(&cbs) != 0) {
const int is_first = CBS_data(&cbs) == buf;
uint32_t c;
switch (encoding) {
case MBSTRING_UNIV:
c = ((uint32_t)*p++) << 24;
c |= ((uint32_t)*p++) << 16;
c |= ((uint32_t)*p++) << 8;
c |= *p++;
break;
case MBSTRING_BMP:
c = ((uint32_t)*p++) << 8;
c |= *p++;
break;
case MBSTRING_ASC:
c = *p++;
break;
case MBSTRING_UTF8: {
int consumed = UTF8_getc(p, buflen, &c);
if (consumed < 0) {
return -1; // Invalid UTF8String
}
buflen -= consumed;
p += consumed;
break;
}
default:
assert(0);
return -1;
if (!get_char(&cbs, &c)) {
OPENSSL_PUT_ERROR(ASN1, get_char_error);
return -1;
}
const int is_last = p == q;
if (utf8_convert) {
const int is_last = CBS_len(&cbs) == 0;
if (flags & ASN1_STRFLGS_UTF8_CONVERT) {
unsigned char utfbuf[6];
int utflen;
utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
utflen = UTF8_putc(utfbuf, sizeof(utfbuf), c);
for (int i = 0; i < utflen; i++) {
int len = do_esc_char(utfbuf[i], flags, quotes, out, is_first && i == 0,
is_last && i == utflen - 1);
@@ -350,24 +327,9 @@ int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str,
return outlen;
}
int utf8_convert = 0;
if (flags & ASN1_STRFLGS_UTF8_CONVERT) {
// If the string is UTF-8, skip decoding and just interpret it as 1 byte
// per character to avoid converting twice.
//
// TODO(davidben): This is not quite a valid optimization if the input
// was invalid UTF-8.
if (encoding == MBSTRING_UTF8) {
encoding = MBSTRING_ASC;
} else {
utf8_convert = 1;
}
}
// Measure the length.
char quotes = 0;
int len = do_buf(str->data, str->length, encoding, utf8_convert, flags,
&quotes, NULL);
int len = do_buf(str->data, str->length, encoding, flags, &quotes, NULL);
if (len < 0) {
return -1;
}
@@ -381,8 +343,7 @@ int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str,
// Encode the value.
if ((quotes && !maybe_write(out, "\"", 1)) ||
do_buf(str->data, str->length, encoding, utf8_convert, flags, NULL, out) <
0 ||
do_buf(str->data, str->length, encoding, flags, NULL, out) < 0 ||
(quotes && !maybe_write(out, "\"", 1))) {
return -1;
}
+29 -4
View File
@@ -187,7 +187,8 @@ int ASN1_TIME_set_string(ASN1_TIME *s, const char *str) {
return 1;
}
static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t) {
static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t,
int allow_timezone_offset) {
if (t == NULL) {
time_t now_t;
time(&now_t);
@@ -198,7 +199,7 @@ static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t) {
}
if (t->type == V_ASN1_UTCTIME) {
return asn1_utctime_to_tm(tm, t);
return asn1_utctime_to_tm(tm, t, allow_timezone_offset);
} else if (t->type == V_ASN1_GENERALIZEDTIME) {
return asn1_generalizedtime_to_tm(tm, t);
}
@@ -209,11 +210,35 @@ static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t) {
int ASN1_TIME_diff(int *out_days, int *out_seconds, const ASN1_TIME *from,
const ASN1_TIME *to) {
struct tm tm_from, tm_to;
if (!asn1_time_to_tm(&tm_from, from)) {
if (!asn1_time_to_tm(&tm_from, from, /*allow_timezone_offset=*/1)) {
return 0;
}
if (!asn1_time_to_tm(&tm_to, to)) {
if (!asn1_time_to_tm(&tm_to, to, /*allow_timezone_offset=*/1)) {
return 0;
}
return OPENSSL_gmtime_diff(out_days, out_seconds, &tm_from, &tm_to);
}
// The functions below do *not* permissively allow the use of four digit
// timezone offsets in UTC times, as is done elsewhere in the code. They are
// both new API, and used internally to X509_cmp_time. This is to discourage the
// use of nonstandard times in new code, and to ensure that this code behaves
// correctly in X509_cmp_time which historically did its own time validations
// slightly different than the many other copies of X.509 time validation
// sprinkled through the codebase. The custom checks in X509_cmp_time meant that
// it did not allow four digit timezone offsets in UTC times.
int ASN1_TIME_to_time_t(const ASN1_TIME *t, time_t *out_time) {
struct tm tm;
if (!asn1_time_to_tm(&tm, t, /*allow_timezone_offset=*/0)) {
return 0;
}
return OPENSSL_timegm(&tm, out_time);
}
int ASN1_TIME_to_posix(const ASN1_TIME *t, int64_t *out_time) {
struct tm tm;
if (!asn1_time_to_tm(&tm, t, /*allow_timezone_offset=*/0)) {
return 0;
}
return OPENSSL_tm_to_posix(&tm, out_time);
}
+28 -43
View File
@@ -64,20 +64,21 @@
#include "internal.h"
int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d) {
int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
int allow_timezone_offset) {
if (d->type != V_ASN1_UTCTIME) {
return 0;
}
CBS cbs;
CBS_init(&cbs, d->data, (size_t)d->length);
if (!CBS_parse_utc_time(&cbs, tm, /*allow_timezone_offset=*/1)) {
if (!CBS_parse_utc_time(&cbs, tm, allow_timezone_offset)) {
return 0;
}
return 1;
}
int ASN1_UTCTIME_check(const ASN1_UTCTIME *d) {
return asn1_utctime_to_tm(NULL, d);
return asn1_utctime_to_tm(NULL, d, /*allow_timezone_offset=*/1);
}
int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) {
@@ -105,66 +106,50 @@ ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t) {
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, int offset_day,
long offset_sec) {
char *p;
struct tm *ts;
struct tm data;
size_t len = 20;
int free_s = 0;
if (s == NULL) {
free_s = 1;
s = ASN1_UTCTIME_new();
}
if (s == NULL) {
goto err;
}
ts = OPENSSL_gmtime(&t, &data);
if (ts == NULL) {
goto err;
if (!OPENSSL_gmtime(&t, &data)) {
return NULL;
}
if (offset_day || offset_sec) {
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) {
goto err;
if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
return NULL;
}
}
if ((ts->tm_year < 50) || (ts->tm_year >= 150)) {
goto err;
if (data.tm_year < 50 || data.tm_year >= 150) {
return NULL;
}
p = (char *)s->data;
if ((p == NULL) || ((size_t)s->length < len)) {
p = OPENSSL_malloc(len);
if (p == NULL) {
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
goto err;
char buf[14];
BIO_snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ",
data.tm_year % 100, data.tm_mon + 1, data.tm_mday, data.tm_hour,
data.tm_min, data.tm_sec);
int free_s = 0;
if (s == NULL) {
free_s = 1;
s = ASN1_UTCTIME_new();
if (s == NULL) {
return NULL;
}
if (s->data != NULL) {
OPENSSL_free(s->data);
}
s->data = (unsigned char *)p;
}
BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ", ts->tm_year % 100,
ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
ts->tm_sec);
s->length = strlen(p);
if (!ASN1_STRING_set(s, buf, strlen(buf))) {
if (free_s) {
ASN1_UTCTIME_free(s);
}
return NULL;
}
s->type = V_ASN1_UTCTIME;
return s;
err:
if (free_s) {
ASN1_UTCTIME_free(s);
}
return NULL;
}
int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t) {
struct tm stm, ttm;
int day, sec;
if (!asn1_utctime_to_tm(&stm, s)) {
if (!asn1_utctime_to_tm(&stm, s, /*allow_timezone_offset=*/1)) {
return -2;
}
-106
View File
@@ -63,112 +63,6 @@
// UTF8 utilities
// This parses a UTF8 string one character at a time. It is passed a pointer
// to the string and the length of the string. It sets 'value' to the value
// of the current character. It returns the number of characters read or a
// negative error code: -1 = string too short -2 = illegal character -3 =
// subsequent characters not of the form 10xxxxxx -4 = character encoded
// incorrectly (not minimal length).
int UTF8_getc(const unsigned char *str, int len, uint32_t *val) {
const unsigned char *p;
uint32_t value;
int ret;
if (len <= 0) {
return 0;
}
p = str;
// Check syntax and work out the encoded value (if correct)
if ((*p & 0x80) == 0) {
value = *p++ & 0x7f;
ret = 1;
} else if ((*p & 0xe0) == 0xc0) {
if (len < 2) {
return -1;
}
if ((p[1] & 0xc0) != 0x80) {
return -3;
}
value = (*p++ & 0x1f) << 6;
value |= *p++ & 0x3f;
if (value < 0x80) {
return -4;
}
ret = 2;
} else if ((*p & 0xf0) == 0xe0) {
if (len < 3) {
return -1;
}
if (((p[1] & 0xc0) != 0x80) || ((p[2] & 0xc0) != 0x80)) {
return -3;
}
value = (*p++ & 0xf) << 12;
value |= (*p++ & 0x3f) << 6;
value |= *p++ & 0x3f;
if (value < 0x800) {
return -4;
}
ret = 3;
} else if ((*p & 0xf8) == 0xf0) {
if (len < 4) {
return -1;
}
if (((p[1] & 0xc0) != 0x80) || ((p[2] & 0xc0) != 0x80) ||
((p[3] & 0xc0) != 0x80)) {
return -3;
}
value = ((uint32_t)(*p++ & 0x7)) << 18;
value |= (*p++ & 0x3f) << 12;
value |= (*p++ & 0x3f) << 6;
value |= *p++ & 0x3f;
if (value < 0x10000) {
return -4;
}
ret = 4;
} else if ((*p & 0xfc) == 0xf8) {
if (len < 5) {
return -1;
}
if (((p[1] & 0xc0) != 0x80) || ((p[2] & 0xc0) != 0x80) ||
((p[3] & 0xc0) != 0x80) || ((p[4] & 0xc0) != 0x80)) {
return -3;
}
value = ((uint32_t)(*p++ & 0x3)) << 24;
value |= ((uint32_t)(*p++ & 0x3f)) << 18;
value |= ((uint32_t)(*p++ & 0x3f)) << 12;
value |= (*p++ & 0x3f) << 6;
value |= *p++ & 0x3f;
if (value < 0x200000) {
return -4;
}
ret = 5;
} else if ((*p & 0xfe) == 0xfc) {
if (len < 6) {
return -1;
}
if (((p[1] & 0xc0) != 0x80) || ((p[2] & 0xc0) != 0x80) ||
((p[3] & 0xc0) != 0x80) || ((p[4] & 0xc0) != 0x80) ||
((p[5] & 0xc0) != 0x80)) {
return -3;
}
value = ((uint32_t)(*p++ & 0x1)) << 30;
value |= ((uint32_t)(*p++ & 0x3f)) << 24;
value |= ((uint32_t)(*p++ & 0x3f)) << 18;
value |= ((uint32_t)(*p++ & 0x3f)) << 12;
value |= (*p++ & 0x3f) << 6;
value |= *p++ & 0x3f;
if (value < 0x4000000) {
return -4;
}
ret = 6;
} else {
return -2;
}
*val = value;
return ret;
}
// This takes a character 'value' and writes the UTF8 encoded value in 'str'
// where 'str' is a buffer containing 'len' characters. Returns the number of
// characters written or -1 if 'len' is too small. 'str' can be set to NULL
+30 -8
View File
@@ -71,16 +71,38 @@ extern "C" {
// Wrapper functions for time functions.
// OPENSSL_gmtime wraps |gmtime_r|. See the manual page for that function.
// OPENSSL_posix_to_tm converts a int64_t POSIX time value in |time| whuch must
// be in the range of year 0000 to 9999 to a broken out time value in |tm|. It
// returns one on success and zero on error.
OPENSSL_EXPORT int OPENSSL_posix_to_tm(int64_t time, struct tm *out_tm);
// OPENSSL_tm_to_posix converts a time value between the years 0 and 9999 in
// |tm| to a POSIX time value in |out|. One is returned on success, zero is
// returned on failure. It is a failure if the tm contains out of range values.
OPENSSL_EXPORT int OPENSSL_tm_to_posix(const struct tm *tm, int64_t *out);
// OPENSSL_gmtime converts a time_t value in |time| which must be in the range
// of year 0000 to 9999 to a broken out time value in |tm|. On success |tm| is
// returned. On failure NULL is returned.
OPENSSL_EXPORT struct tm *OPENSSL_gmtime(const time_t *time, struct tm *result);
// OPENSSL_gmtime_adj updates |tm| by adding |offset_day| days and |offset_sec|
// seconds.
// OPENSSL_timegm converts a time value between the years 0 and 9999 in |tm| to
// a time_t value in |out|. One is returned on success, zero is returned on
// failure. It is a failure if the converted time can not be represented in a
// time_t, or if the tm contains out of range values.
OPENSSL_EXPORT int OPENSSL_timegm(const struct tm *tm, time_t *out);
// OPENSSL_gmtime_adj returns one on success, and updates |tm| by adding
// |offset_day| days and |offset_sec| seconds. It returns zero on failure. |tm|
// must be in the range of year 0000 to 9999 both before and after the update or
// a failure will be returned.
int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec);
// OPENSSL_gmtime_diff calculates the difference between |from| and |to| and
// outputs the difference as a number of days and seconds in |*out_days| and
// |*out_secs|.
// OPENSSL_gmtime_diff calculates the difference between |from| and |to|. It
// returns one, and outputs the difference as a number of days and seconds in
// |*out_days| and |*out_secs| on success. It returns zero on failure. Both
// |from| and |to| must be in the range of year 0000 to 9999 or a failure will
// be returned.
OPENSSL_EXPORT int OPENSSL_gmtime_diff(int *out_days, int *out_secs,
const struct tm *from,
const struct tm *to);
@@ -126,14 +148,14 @@ typedef struct ASN1_ENCODING_st {
unsigned alias_only_on_next_parse : 1;
} ASN1_ENCODING;
OPENSSL_EXPORT int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d);
OPENSSL_EXPORT int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
int allow_timezone_offset);
OPENSSL_EXPORT int asn1_generalizedtime_to_tm(struct tm *tm,
const ASN1_GENERALIZEDTIME *d);
void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
int combine);
int UTF8_getc(const unsigned char *str, int len, uint32_t *val);
int UTF8_putc(unsigned char *str, int len, uint32_t value);
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
@@ -0,0 +1,227 @@
/* Copyright (c) 2022, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
// Time conversion to/from POSIX time_t and struct tm, with no support
// for time zones other than UTC
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include "internal.h"
#define SECS_PER_HOUR (60 * 60)
#define SECS_PER_DAY (24 * SECS_PER_HOUR)
// Is a year/month/day combination valid, in the range from year 0000
// to 9999?
static int is_valid_date(int year, int month, int day) {
if (day < 1 || month < 1 || year < 0 || year > 9999) {
return 0;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return day > 0 && day <= 31;
case 4:
case 6:
case 9:
case 11:
return day > 0 && day <= 30;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return day > 0 && day <= 29;
} else {
return day > 0 && day <= 28;
}
default:
return 0;
}
}
// Is a time valid? Leap seconds of 60 are not considered valid, as
// the POSIX time in seconds does not include them.
static int is_valid_time(int hours, int minutes, int seconds) {
if (hours < 0 || minutes < 0 || seconds < 0 || hours > 23 || minutes > 59 ||
seconds > 59) {
return 0;
}
return 1;
}
// Is a int64 time representing a time within our expected range?
static int is_valid_epoch_time(int64_t time) {
// 0000-01-01 00:00:00 UTC to 9999-12-31 23:59:59 UTC
return (int64_t)-62167219200 <= time && time <= (int64_t)253402300799;
}
// Inspired by algorithms presented in
// https://howardhinnant.github.io/date_algorithms.html
// (Public Domain)
static int posix_time_from_utc(int year, int month, int day, int hours,
int minutes, int seconds, int64_t *out_time) {
if (!is_valid_date(year, month, day) ||
!is_valid_time(hours, minutes, seconds)) {
return 0;
}
if (month <= 2) {
year--; // Start years on Mar 1, so leap days always finish a year.
}
// At this point year will be in the range -1 and 9999.
assert(-1 <= year && year <= 9999);
int64_t era = (year >= 0 ? year : year - 399) / 400;
int64_t year_of_era = year - era * 400;
int64_t day_of_year =
(153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1;
int64_t day_of_era =
year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
int64_t posix_days = era * 146097 + day_of_era - 719468;
*out_time = posix_days * SECS_PER_DAY + hours * SECS_PER_HOUR + minutes * 60 +
seconds;
return 1;
}
// Inspired by algorithms presented in
// https://howardhinnant.github.io/date_algorithms.html
// (Public Domain)
static int utc_from_posix_time(int64_t time, int *out_year, int *out_month,
int *out_day, int *out_hours, int *out_minutes,
int *out_seconds) {
if (!is_valid_epoch_time(time)) {
return 0;
}
int64_t days = time / SECS_PER_DAY;
int64_t leftover_seconds = time % SECS_PER_DAY;
if (leftover_seconds < 0) {
days--;
leftover_seconds += SECS_PER_DAY;
}
days += 719468; // Shift to starting epoch of Mar 1 0000.
// At this point, days will be in the range -61 and 3652364.
assert(-61 <= days && days <= 3652364);
int64_t era = (days > 0 ? days : days - 146096) / 146097;
int64_t day_of_era = days - era * 146097;
int64_t year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36524 -
day_of_era / 146096) /
365;
*out_year = (int)(year_of_era + era * 400); // Year starting on Mar 1.
int64_t day_of_year =
day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
int64_t month_of_year = (5 * day_of_year + 2) / 153;
*out_month =
(int)(month_of_year < 10 ? month_of_year + 3 : month_of_year - 9);
if (*out_month <= 2) {
(*out_year)++; // Adjust year back to Jan 1 start of year.
}
*out_day = (int)(day_of_year - (153 * month_of_year + 2) / 5 + 1);
*out_hours = (int)(leftover_seconds / SECS_PER_HOUR);
leftover_seconds %= SECS_PER_HOUR;
*out_minutes = (int)(leftover_seconds / 60);
*out_seconds = (int)(leftover_seconds % 60);
return 1;
}
int OPENSSL_tm_to_posix(const struct tm *tm, int64_t *out) {
return posix_time_from_utc(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, out);
}
int OPENSSL_posix_to_tm(int64_t time, struct tm *out_tm) {
memset(out_tm, 0, sizeof(struct tm));
if (!utc_from_posix_time(time, &out_tm->tm_year, &out_tm->tm_mon,
&out_tm->tm_mday, &out_tm->tm_hour, &out_tm->tm_min,
&out_tm->tm_sec)) {
return 0;
}
out_tm->tm_year -= 1900;
out_tm->tm_mon -= 1;
return 1;
}
int OPENSSL_timegm(const struct tm *tm, time_t *out) {
static_assert(
sizeof(time_t) == sizeof(int32_t) || sizeof(time_t) == sizeof(int64_t),
"time_t is broken");
int64_t posix_time;
if (!OPENSSL_tm_to_posix(tm, &posix_time)) {
return 0;
}
if (sizeof(time_t) == sizeof(int32_t) &&
(posix_time > INT32_MAX || posix_time < INT32_MIN)) {
return 0;
}
*out = (time_t)posix_time;
return 1;
}
struct tm *OPENSSL_gmtime(const time_t *time, struct tm *out_tm) {
static_assert(
sizeof(time_t) == sizeof(int32_t) || sizeof(time_t) == sizeof(int64_t),
"time_t is broken");
int64_t posix_time = *time;
if (!OPENSSL_posix_to_tm(posix_time, out_tm)) {
return NULL;
}
return out_tm;
}
int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec) {
int64_t posix_time;
if (!posix_time_from_utc(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, &posix_time)) {
return 0;
}
if (!utc_from_posix_time(posix_time + off_day * SECS_PER_DAY + offset_sec,
&tm->tm_year, &tm->tm_mon, &tm->tm_mday,
&tm->tm_hour, &tm->tm_min, &tm->tm_sec)) {
return 0;
}
tm->tm_year -= 1900;
tm->tm_mon -= 1;
return 1;
}
int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from,
const struct tm *to) {
int64_t time_to;
if (!posix_time_from_utc(to->tm_year + 1900, to->tm_mon + 1, to->tm_mday,
to->tm_hour, to->tm_min, to->tm_sec, &time_to)) {
return 0;
}
int64_t time_from;
if (!posix_time_from_utc(from->tm_year + 1900, from->tm_mon + 1,
from->tm_mday, from->tm_hour, from->tm_min,
from->tm_sec, &time_from)) {
return 0;
}
int64_t timediff = time_to - time_from;
int64_t daydiff = timediff / SECS_PER_DAY;
timediff %= SECS_PER_DAY;
if (daydiff > INT_MAX || daydiff < INT_MIN) {
return 0;
}
*out_secs = (int)timediff;
*out_days = (int)daydiff;
return 1;
}
+32 -11
View File
@@ -63,6 +63,7 @@
#include <limits.h>
#include <string.h>
#include "../bytestring/internal.h"
#include "../internal.h"
#include "internal.h"
@@ -797,32 +798,51 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
case V_ASN1_OTHER:
case V_ASN1_SET:
case V_ASN1_SEQUENCE:
default:
if (utype == V_ASN1_BMPSTRING && (len & 1)) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
goto err;
default: {
CBS cbs;
CBS_init(&cbs, cont, (size_t)len);
if (utype == V_ASN1_BMPSTRING) {
while (CBS_len(&cbs) != 0) {
uint32_t c;
if (!cbs_get_ucs2_be(&cbs, &c)) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BMPSTRING);
goto err;
}
}
}
if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
goto err;
if (utype == V_ASN1_UNIVERSALSTRING) {
while (CBS_len(&cbs) != 0) {
uint32_t c;
if (!cbs_get_utf32_be(&cbs, &c)) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UNIVERSALSTRING);
goto err;
}
}
}
if (utype == V_ASN1_UTF8STRING) {
while (CBS_len(&cbs) != 0) {
uint32_t c;
if (!cbs_get_utf8(&cbs, &c)) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UTF8STRING);
goto err;
}
}
}
if (utype == V_ASN1_UTCTIME) {
CBS cbs;
CBS_init(&cbs, cont, (size_t)len);
if (!CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/1)) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_TIME_FORMAT);
goto err;
}
}
if (utype == V_ASN1_GENERALIZEDTIME) {
CBS cbs;
CBS_init(&cbs, cont, (size_t)len);
if (!CBS_parse_generalized_time(&cbs, NULL,
/*allow_timezone_offset=*/0)) {
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_TIME_FORMAT);
goto err;
}
}
// TODO(https://crbug.com/boringssl/427): Check other string types.
// All based on ASN1_STRING and handled the same
if (!*pval) {
stmp = ASN1_STRING_type_new(utype);
@@ -842,6 +862,7 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
goto err;
}
break;
}
}
// If ASN1_ANY and NULL type fix up value
if (typ && (utype == V_ASN1_NULL)) {
@@ -111,8 +111,6 @@ IMPLEMENT_ASN1_TYPE_ex(ASN1_BOOLEAN, ASN1_BOOLEAN, -1)
IMPLEMENT_ASN1_TYPE_ex(ASN1_TBOOLEAN, ASN1_BOOLEAN, 1)
IMPLEMENT_ASN1_TYPE_ex(ASN1_FBOOLEAN, ASN1_BOOLEAN, 0)
// Special, OCTET STRING with indefinite length constructed support
ASN1_ITEM_TEMPLATE(ASN1_SEQUENCE_ANY) =
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY)
ASN1_ITEM_TEMPLATE_END(ASN1_SEQUENCE_ANY)
@@ -1,206 +0,0 @@
/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
* project 2001.
* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
* project 2008.
*/
/* ====================================================================
* Copyright (c) 2001 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com). */
#if defined(__linux__) && !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 201410L // for gmtime_r
#endif
#include "internal.h"
#include <time.h>
#define SECS_PER_DAY (24 * 60 * 60)
struct tm *OPENSSL_gmtime(const time_t *time, struct tm *result) {
#if defined(OPENSSL_WINDOWS)
if (gmtime_s(result, time)) {
return NULL;
}
return result;
#else
return gmtime_r(time, result);
#endif
}
// Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
static long date_to_julian(int y, int m, int d) {
return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
(367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
(3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
}
static void julian_to_date(long jd, int *y, int *m, int *d) {
long L = jd + 68569;
long n = (4 * L) / 146097;
long i, j;
L = L - (146097 * n + 3) / 4;
i = (4000 * (L + 1)) / 1461001;
L = L - (1461 * i) / 4 + 31;
j = (80 * L) / 2447;
*d = L - (2447 * j) / 80;
L = j / 11;
*m = j + 2 - (12 * L);
*y = 100 * (n - 49) + i + L;
}
// Convert tm structure and offset into julian day and seconds
static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
long *pday, int *psec) {
int offset_hms, offset_day;
long time_jd;
int time_year, time_month, time_day;
// split offset into days and day seconds
offset_day = offset_sec / SECS_PER_DAY;
// Avoid sign issues with % operator
offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
offset_day += off_day;
// Add current time seconds to offset
offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
// Adjust day seconds if overflow
if (offset_hms >= SECS_PER_DAY) {
offset_day++;
offset_hms -= SECS_PER_DAY;
} else if (offset_hms < 0) {
offset_day--;
offset_hms += SECS_PER_DAY;
}
// Convert date of time structure into a Julian day number.
time_year = tm->tm_year + 1900;
time_month = tm->tm_mon + 1;
time_day = tm->tm_mday;
time_jd = date_to_julian(time_year, time_month, time_day);
// Work out Julian day of new date
time_jd += offset_day;
if (time_jd < 0) {
return 0;
}
*pday = time_jd;
*psec = offset_hms;
return 1;
}
int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec) {
int time_sec, time_year, time_month, time_day;
long time_jd;
// Convert time and offset into julian day and seconds
if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec)) {
return 0;
}
// Convert Julian day back to date
julian_to_date(time_jd, &time_year, &time_month, &time_day);
if (time_year < 1900 || time_year > 9999) {
return 0;
}
// Update tm structure
tm->tm_year = time_year - 1900;
tm->tm_mon = time_month - 1;
tm->tm_mday = time_day;
tm->tm_hour = time_sec / 3600;
tm->tm_min = (time_sec / 60) % 60;
tm->tm_sec = time_sec % 60;
return 1;
}
int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from,
const struct tm *to) {
int from_sec, to_sec, diff_sec;
long from_jd, to_jd, diff_day;
if (!julian_adj(from, 0, 0, &from_jd, &from_sec)) {
return 0;
}
if (!julian_adj(to, 0, 0, &to_jd, &to_sec)) {
return 0;
}
diff_day = to_jd - from_jd;
diff_sec = to_sec - from_sec;
// Adjust differences so both positive or both negative
if (diff_day > 0 && diff_sec < 0) {
diff_day--;
diff_sec += SECS_PER_DAY;
}
if (diff_day < 0 && diff_sec > 0) {
diff_day++;
diff_sec -= SECS_PER_DAY;
}
if (out_days) {
*out_days = (int)diff_day;
}
if (out_secs) {
*out_secs = diff_sec;
}
return 1;
}
+2 -4
View File
@@ -60,8 +60,6 @@
#include <limits.h>
#include <string.h>
#include <CNIOBoringSSL_type_check.h>
#include "../internal.h"
@@ -98,8 +96,8 @@ static uint8_t conv_bin2ascii(uint8_t a) {
return ret;
}
OPENSSL_STATIC_ASSERT(sizeof(((EVP_ENCODE_CTX *)(NULL))->data) % 3 == 0,
"data length must be a multiple of base64 chunk size");
static_assert(sizeof(((EVP_ENCODE_CTX *)(NULL))->data) % 3 == 0,
"data length must be a multiple of base64 chunk size");
int EVP_EncodedLength(size_t *out_len, size_t len) {
if (len + 2 < len) {
+4 -4
View File
@@ -14,7 +14,7 @@
#include <CNIOBoringSSL_blake2.h>
#include <CNIOBoringSSL_type_check.h>
#include <assert.h>
#include "../internal.h"
@@ -61,7 +61,7 @@ static void blake2b_transform(
size_t num_bytes, int is_final_block) {
// https://tools.ietf.org/html/rfc7693#section-3.2
uint64_t v[16];
OPENSSL_STATIC_ASSERT(sizeof(v) == sizeof(b2b->h) + sizeof(kIV), "");
static_assert(sizeof(v) == sizeof(b2b->h) + sizeof(kIV), "");
OPENSSL_memcpy(v, b2b->h, sizeof(b2b->h));
OPENSSL_memcpy(&v[8], kIV, sizeof(kIV));
@@ -97,7 +97,7 @@ static void blake2b_transform(
void BLAKE2B256_Init(BLAKE2B_CTX *b2b) {
OPENSSL_memset(b2b, 0, sizeof(BLAKE2B_CTX));
OPENSSL_STATIC_ASSERT(sizeof(kIV) == sizeof(b2b->h), "");
static_assert(sizeof(kIV) == sizeof(b2b->h), "");
OPENSSL_memcpy(&b2b->h, kIV, sizeof(kIV));
// https://tools.ietf.org/html/rfc7693#section-2.5
@@ -143,7 +143,7 @@ void BLAKE2B256_Final(uint8_t out[BLAKE2B256_DIGEST_LENGTH], BLAKE2B_CTX *b2b) {
sizeof(b2b->block.bytes) - b2b->block_used);
blake2b_transform(b2b, b2b->block.words, b2b->block_used,
/*is_final_block=*/1);
OPENSSL_STATIC_ASSERT(BLAKE2B256_DIGEST_LENGTH <= sizeof(b2b->h), "");
static_assert(BLAKE2B256_DIGEST_LENGTH <= sizeof(b2b->h), "");
memcpy(out, b2b->h, BLAKE2B256_DIGEST_LENGTH);
}
@@ -13,6 +13,9 @@
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <CNIOBoringSSL_aead.h>
#include <assert.h>
#include <CNIOBoringSSL_cipher.h>
#include <CNIOBoringSSL_crypto.h>
#include <CNIOBoringSSL_err.h>
@@ -35,12 +38,12 @@ struct aead_aes_ctr_hmac_sha256_ctx {
SHA256_CTX outer_init_state;
};
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_ctr_hmac_sha256_ctx),
"AEAD state is too small");
OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_ctr_hmac_sha256_ctx),
"AEAD state has insufficient alignment");
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_ctr_hmac_sha256_ctx),
"AEAD state is too small");
static_assert(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_ctr_hmac_sha256_ctx),
"AEAD state has insufficient alignment");
static void hmac_init(SHA256_CTX *out_inner, SHA256_CTX *out_outer,
const uint8_t hmac_key[32]) {
@@ -42,11 +42,11 @@ struct aead_aes_gcm_siv_asm_ctx {
// The assembly code assumes 8-byte alignment of the EVP_AEAD_CTX's state, and
// aligns to 16 bytes itself.
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) + 8 >=
sizeof(struct aead_aes_gcm_siv_asm_ctx),
"AEAD state is too small");
OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >= 8,
"AEAD state has insufficient alignment");
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) + 8 >=
sizeof(struct aead_aes_gcm_siv_asm_ctx),
"AEAD state is too small");
static_assert(alignof(union evp_aead_ctx_st_state) >= 8,
"AEAD state has insufficient alignment");
// asm_ctx_from_ctx returns a 16-byte aligned context pointer from |ctx|.
static struct aead_aes_gcm_siv_asm_ctx *asm_ctx_from_ctx(
@@ -550,12 +550,12 @@ struct aead_aes_gcm_siv_ctx {
unsigned is_256:1;
};
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_gcm_siv_ctx),
"AEAD state is too small");
OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_gcm_siv_ctx),
"AEAD state has insufficient alignment");
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_gcm_siv_ctx),
"AEAD state is too small");
static_assert(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_gcm_siv_ctx),
"AEAD state has insufficient alignment");
static int aead_aes_gcm_siv_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
size_t key_len, size_t tag_len) {
@@ -14,6 +14,7 @@
#include <CNIOBoringSSL_aead.h>
#include <assert.h>
#include <string.h>
#include <CNIOBoringSSL_chacha.h>
@@ -21,7 +22,6 @@
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_poly1305.h>
#include <CNIOBoringSSL_type_check.h>
#include "internal.h"
#include "../chacha/internal.h"
@@ -32,12 +32,12 @@ struct aead_chacha20_poly1305_ctx {
uint8_t key[32];
};
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_chacha20_poly1305_ctx),
"AEAD state is too small");
OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_chacha20_poly1305_ctx),
"AEAD state has insufficient alignment");
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_chacha20_poly1305_ctx),
"AEAD state is too small");
static_assert(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_chacha20_poly1305_ctx),
"AEAD state has insufficient alignment");
static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
size_t key_len, size_t tag_len) {
@@ -23,7 +23,6 @@
#include <CNIOBoringSSL_md5.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_sha.h>
#include <CNIOBoringSSL_type_check.h>
#include "../fipsmodule/cipher/internal.h"
#include "../internal.h"
@@ -42,15 +41,12 @@ typedef struct {
char implicit_iv;
} AEAD_TLS_CTX;
OPENSSL_STATIC_ASSERT(EVP_MAX_MD_SIZE < 256,
"mac_key_len does not fit in uint8_t");
static_assert(EVP_MAX_MD_SIZE < 256, "mac_key_len does not fit in uint8_t");
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(AEAD_TLS_CTX),
"AEAD state is too small");
OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
alignof(AEAD_TLS_CTX),
"AEAD state has insufficient alignment");
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >= sizeof(AEAD_TLS_CTX),
"AEAD state is too small");
static_assert(alignof(union evp_aead_ctx_st_state) >= alignof(AEAD_TLS_CTX),
"AEAD state has insufficient alignment");
static void aead_tls_cleanup(EVP_AEAD_CTX *ctx) {
AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
@@ -57,10 +57,10 @@
#ifndef OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H
#define OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H
#include <assert.h>
#include <stdlib.h>
#include <CNIOBoringSSL_base.h>
#include <CNIOBoringSSL_type_check.h>
#include "../internal.h"
@@ -166,10 +166,10 @@ union chacha20_poly1305_seal_data {
#if (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
!defined(OPENSSL_NO_ASM)
OPENSSL_STATIC_ASSERT(sizeof(union chacha20_poly1305_open_data) == 48,
"wrong chacha20_poly1305_open_data size");
OPENSSL_STATIC_ASSERT(sizeof(union chacha20_poly1305_seal_data) == 48 + 8 + 8,
"wrong chacha20_poly1305_seal_data size");
static_assert(sizeof(union chacha20_poly1305_open_data) == 48,
"wrong chacha20_poly1305_open_data size");
static_assert(sizeof(union chacha20_poly1305_seal_data) == 48 + 8 + 8,
"wrong chacha20_poly1305_seal_data size");
OPENSSL_INLINE int chacha20_poly1305_asm_capable(void) {
#if defined(OPENSSL_X86_64)
@@ -27,7 +27,6 @@
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_rand.h>
#include <CNIOBoringSSL_sha.h>
#include <CNIOBoringSSL_type_check.h>
#include "internal.h"
#include "../internal.h"
@@ -151,8 +150,8 @@ typedef uint32_t fe_limb_t;
#endif // BORINGSSL_CURVE25519_64BIT
OPENSSL_STATIC_ASSERT(sizeof(fe) == sizeof(fe_limb_t) * FE_NUM_LIMBS,
"fe_limb_t[FE_NUM_LIMBS] is inconsistent with fe");
static_assert(sizeof(fe) == sizeof(fe_limb_t) * FE_NUM_LIMBS,
"fe_limb_t[FE_NUM_LIMBS] is inconsistent with fe");
static void fe_frombytes_strict(fe *h, const uint8_t s[32]) {
// |fiat_25519_from_bytes| requires the top-most bit be clear.
@@ -315,8 +314,7 @@ static void fe_copy(fe *h, const fe *f) {
}
static void fe_copy_lt(fe_loose *h, const fe *f) {
OPENSSL_STATIC_ASSERT(sizeof(fe_loose) == sizeof(fe),
"fe and fe_loose mismatch");
static_assert(sizeof(fe_loose) == sizeof(fe), "fe and fe_loose mismatch");
OPENSSL_memmove(h, f, sizeof(fe));
}
#if !defined(OPENSSL_SMALL)
@@ -22,6 +22,7 @@
#include <CNIOBoringSSL_rand.h>
#include <CNIOBoringSSL_sha.h>
#include "../fipsmodule/bn/internal.h"
#include "../internal.h"
#include "./internal.h"
@@ -314,46 +315,30 @@ static void left_shift_3(uint8_t n[32]) {
}
}
typedef union {
uint8_t bytes[32];
uint32_t words[8];
typedef struct {
BN_ULONG words[32 / sizeof(BN_ULONG)];
} scalar;
// kOrder is the order of the prime-order subgroup of curve25519 in
// little-endian order.
static const scalar kOrder = {{0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10}};
// kOrder is the order of the prime-order subgroup of curve25519.
static const scalar kOrder = {
{TOBN(0x5812631a, 0x5cf5d3ed), TOBN(0x14def9de, 0xa2f79cd6),
TOBN(0x00000000, 0x00000000), TOBN(0x10000000, 0x00000000)}};
// scalar_cmov copies |src| to |dest| if |mask| is all ones.
static void scalar_cmov(scalar *dest, const scalar *src, crypto_word_t mask) {
for (size_t i = 0; i < 8; i++) {
dest->words[i] =
constant_time_select_w(mask, src->words[i], dest->words[i]);
}
bn_select_words(dest->words, mask, src->words, dest->words,
OPENSSL_ARRAY_SIZE(dest->words));
}
// scalar_double sets |s| to |2×s|.
static void scalar_double(scalar *s) {
uint32_t carry = 0;
for (size_t i = 0; i < 8; i++) {
const uint32_t carry_out = s->words[i] >> 31;
s->words[i] = (s->words[i] << 1) | carry;
carry = carry_out;
}
bn_add_words(s->words, s->words, s->words, OPENSSL_ARRAY_SIZE(s->words));
}
// scalar_add sets |dest| to |dest| plus |src|.
static void scalar_add(scalar *dest, const scalar *src) {
uint32_t carry = 0;
for (size_t i = 0; i < 8; i++) {
uint64_t tmp = ((uint64_t)dest->words[i] + src->words[i]) + carry;
dest->words[i] = (uint32_t)tmp;
carry = (uint32_t)(tmp >> 32);
}
bn_add_words(dest->words, dest->words, src->words,
OPENSSL_ARRAY_SIZE(dest->words));
}
int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, size_t *out_len,
@@ -412,25 +397,25 @@ int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, size_t *out_len,
OPENSSL_memset(&tmp, 0, sizeof(tmp));
scalar_cmov(&tmp, &order,
constant_time_eq_w(password_scalar.bytes[0] & 1, 1));
constant_time_eq_w(password_scalar.words[0] & 1, 1));
scalar_add(&password_scalar, &tmp);
scalar_double(&order);
OPENSSL_memset(&tmp, 0, sizeof(tmp));
scalar_cmov(&tmp, &order,
constant_time_eq_w(password_scalar.bytes[0] & 2, 2));
constant_time_eq_w(password_scalar.words[0] & 2, 2));
scalar_add(&password_scalar, &tmp);
scalar_double(&order);
OPENSSL_memset(&tmp, 0, sizeof(tmp));
scalar_cmov(&tmp, &order,
constant_time_eq_w(password_scalar.bytes[0] & 4, 4));
constant_time_eq_w(password_scalar.words[0] & 4, 4));
scalar_add(&password_scalar, &tmp);
assert((password_scalar.bytes[0] & 7) == 0);
assert((password_scalar.words[0] & 7) == 0);
}
OPENSSL_memcpy(ctx->password_scalar, password_scalar.bytes,
OPENSSL_memcpy(ctx->password_scalar, password_scalar.words,
sizeof(ctx->password_scalar));
ge_p3 mask;
+2
View File
@@ -130,6 +130,8 @@ int DSA_up_ref(DSA *dsa) {
return 1;
}
unsigned DSA_bits(const DSA *dsa) { return BN_num_bits(dsa->p); }
const BIGNUM *DSA_get0_pub_key(const DSA *dsa) { return dsa->pub_key; }
const BIGNUM *DSA_get0_priv_key(const DSA *dsa) { return dsa->priv_key; }
@@ -17,7 +17,6 @@
#include <CNIOBoringSSL_digest.h>
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_nid.h>
#include <CNIOBoringSSL_type_check.h>
#include <assert.h>
@@ -61,7 +60,7 @@ static int expand_message_xmd(const EVP_MD *md, uint8_t *out, size_t out_len,
EVP_MD_CTX_init(&ctx);
// Long DSTs are hashed down to size. See section 5.3.3.
OPENSSL_STATIC_ASSERT(EVP_MAX_MD_SIZE < 256, "hashed DST still too large");
static_assert(EVP_MAX_MD_SIZE < 256, "hashed DST still too large");
uint8_t dst_buf[EVP_MAX_MD_SIZE];
if (dst_len >= 256) {
static const char kPrefix[] = "H2C-OVERSIZE-DST-";
+35 -35
View File
@@ -16,43 +16,43 @@
#include <CNIOBoringSSL_base.h>
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_type_check.h>
#include <assert.h>
OPENSSL_STATIC_ASSERT(ERR_LIB_NONE == 1, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_SYS == 2, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_BN == 3, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_RSA == 4, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_DH == 5, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_EVP == 6, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_BUF == 7, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_OBJ == 8, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_PEM == 9, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_DSA == 10, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_X509 == 11, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_ASN1 == 12, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_CONF == 13, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_CRYPTO == 14, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_EC == 15, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_SSL == 16, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_BIO == 17, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_PKCS7 == 18, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_PKCS8 == 19, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_X509V3 == 20, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_RAND == 21, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_ENGINE == 22, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_OCSP == 23, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_UI == 24, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_COMP == 25, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_ECDSA == 26, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_ECDH == 27, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_HMAC == 28, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_DIGEST == 29, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_CIPHER == 30, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_HKDF == 31, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_TRUST_TOKEN == 32, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_LIB_USER == 33, "library value changed");
OPENSSL_STATIC_ASSERT(ERR_NUM_LIBS == 34, "number of libraries changed");
static_assert(ERR_LIB_NONE == 1, "library value changed");
static_assert(ERR_LIB_SYS == 2, "library value changed");
static_assert(ERR_LIB_BN == 3, "library value changed");
static_assert(ERR_LIB_RSA == 4, "library value changed");
static_assert(ERR_LIB_DH == 5, "library value changed");
static_assert(ERR_LIB_EVP == 6, "library value changed");
static_assert(ERR_LIB_BUF == 7, "library value changed");
static_assert(ERR_LIB_OBJ == 8, "library value changed");
static_assert(ERR_LIB_PEM == 9, "library value changed");
static_assert(ERR_LIB_DSA == 10, "library value changed");
static_assert(ERR_LIB_X509 == 11, "library value changed");
static_assert(ERR_LIB_ASN1 == 12, "library value changed");
static_assert(ERR_LIB_CONF == 13, "library value changed");
static_assert(ERR_LIB_CRYPTO == 14, "library value changed");
static_assert(ERR_LIB_EC == 15, "library value changed");
static_assert(ERR_LIB_SSL == 16, "library value changed");
static_assert(ERR_LIB_BIO == 17, "library value changed");
static_assert(ERR_LIB_PKCS7 == 18, "library value changed");
static_assert(ERR_LIB_PKCS8 == 19, "library value changed");
static_assert(ERR_LIB_X509V3 == 20, "library value changed");
static_assert(ERR_LIB_RAND == 21, "library value changed");
static_assert(ERR_LIB_ENGINE == 22, "library value changed");
static_assert(ERR_LIB_OCSP == 23, "library value changed");
static_assert(ERR_LIB_UI == 24, "library value changed");
static_assert(ERR_LIB_COMP == 25, "library value changed");
static_assert(ERR_LIB_ECDSA == 26, "library value changed");
static_assert(ERR_LIB_ECDH == 27, "library value changed");
static_assert(ERR_LIB_HMAC == 28, "library value changed");
static_assert(ERR_LIB_DIGEST == 29, "library value changed");
static_assert(ERR_LIB_CIPHER == 30, "library value changed");
static_assert(ERR_LIB_HKDF == 31, "library value changed");
static_assert(ERR_LIB_TRUST_TOKEN == 32, "library value changed");
static_assert(ERR_LIB_USER == 33, "library value changed");
static_assert(ERR_NUM_LIBS == 34, "number of libraries changed");
const uint32_t kOpenSSLReasonValues[] = {
0xc320885,
+2 -3
View File
@@ -13,7 +13,6 @@
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_type_check.h>
#include "../internal.h"
@@ -30,7 +29,7 @@
// A block_t is a Salsa20 block.
typedef struct { uint32_t words[16]; } block_t;
OPENSSL_STATIC_ASSERT(sizeof(block_t) == 64, "block_t has padding");
static_assert(sizeof(block_t) == 64, "block_t has padding");
// salsa208_word_specification implements the Salsa20/8 core function, also
// described in RFC 7914, section 3. It modifies the block at |inout|
@@ -171,7 +170,7 @@ int EVP_PBE_scrypt(const char *password, size_t password_len,
// Allocate and divide up the scratch space. |max_mem| fits in a size_t, which
// is no bigger than uint64_t, so none of these operations may overflow.
OPENSSL_STATIC_ASSERT(UINT64_MAX >= ((size_t)-1), "size_t exceeds uint64_t");
static_assert(UINT64_MAX >= ((size_t)-1), "size_t exceeds uint64_t");
size_t B_blocks = p * 2 * r;
size_t B_bytes = B_blocks * sizeof(block_t);
size_t T_blocks = 2 * r;
@@ -152,10 +152,10 @@ static inline aes_word_t aes_nohw_shift_right(aes_word_t a, aes_word_t i) {
}
#endif // OPENSSL_SSE2
OPENSSL_STATIC_ASSERT(AES_NOHW_BATCH_SIZE * 128 == 8 * 8 * sizeof(aes_word_t),
"batch size does not match word size");
OPENSSL_STATIC_ASSERT(AES_NOHW_WORD_SIZE == sizeof(aes_word_t),
"AES_NOHW_WORD_SIZE is incorrect");
static_assert(AES_NOHW_BATCH_SIZE * 128 == 8 * 8 * sizeof(aes_word_t),
"batch size does not match word size");
static_assert(AES_NOHW_WORD_SIZE == sizeof(aes_word_t),
"AES_NOHW_WORD_SIZE is incorrect");
// Block representations.
@@ -56,6 +56,7 @@
#include <CNIOBoringSSL_bn.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
@@ -416,8 +417,8 @@ int bn_resize_words(BIGNUM *bn, size_t words) {
void bn_select_words(BN_ULONG *r, BN_ULONG mask, const BN_ULONG *a,
const BN_ULONG *b, size_t num) {
for (size_t i = 0; i < num; i++) {
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
r[i] = constant_time_select_w(mask, a[i], b[i]);
}
}
@@ -56,8 +56,9 @@
#include <CNIOBoringSSL_bn.h>
#include <assert.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_type_check.h>
#include "internal.h"
#include "../../internal.h"
@@ -65,8 +66,8 @@
static int bn_cmp_words_consttime(const BN_ULONG *a, size_t a_len,
const BN_ULONG *b, size_t b_len) {
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
int ret = 0;
// Process the common words in little-endian order.
size_t min = a_len < b_len ? a_len : b_len;
@@ -116,7 +116,6 @@
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_thread.h>
#include <CNIOBoringSSL_type_check.h>
#include "internal.h"
#include "../../internal.h"
@@ -190,11 +189,10 @@ static int bn_mont_ctx_set_N_and_n0(BN_MONT_CTX *mont, const BIGNUM *mod) {
// others, we could use a shorter R value and use faster |BN_ULONG|-based
// math instead of |uint64_t|-based math, which would be double-precision.
// However, currently only the assembler files know which is which.
OPENSSL_STATIC_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
"BN_MONT_CTX_N0_LIMBS value is invalid");
OPENSSL_STATIC_ASSERT(
sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS == sizeof(uint64_t),
"uint64_t is insufficient precision for n0");
static_assert(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
"BN_MONT_CTX_N0_LIMBS value is invalid");
static_assert(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS == sizeof(uint64_t),
"uint64_t is insufficient precision for n0");
uint64_t n0 = bn_mont_n0(&mont->N);
mont->n0[0] = (BN_ULONG)n0;
#if BN_MONT_CTX_N0_LIMBS == 2
@@ -22,11 +22,10 @@
static uint64_t bn_neg_inv_mod_r_u64(uint64_t n);
OPENSSL_STATIC_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
"BN_MONT_CTX_N0_LIMBS value is invalid");
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS ==
sizeof(uint64_t),
"uint64_t is insufficient precision for n0");
static_assert(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
"BN_MONT_CTX_N0_LIMBS value is invalid");
static_assert(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS == sizeof(uint64_t),
"uint64_t is insufficient precision for n0");
// LG_LITTLE_R is log_2(r).
#define LG_LITTLE_R (BN_MONT_CTX_N0_LIMBS * BN_BITS2)
@@ -62,7 +62,6 @@
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_type_check.h>
#include "internal.h"
#include "../../internal.h"
@@ -281,8 +280,8 @@ static void bn_mul_recursive(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2);
BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2);
bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2);
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
c = constant_time_select_w(neg, c_neg, c_pos);
// We now have our three components. Add them together.
@@ -395,8 +394,8 @@ static void bn_mul_part_recursive(BN_ULONG *r, const BN_ULONG *a,
BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2);
BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2);
bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2);
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
c = constant_time_select_w(neg, c_neg, c_pos);
// We now have our three components. Add them together.
@@ -108,12 +108,12 @@
#include <CNIOBoringSSL_bn.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_rand.h>
#include <CNIOBoringSSL_type_check.h>
#include "../../internal.h"
#include "../rand/internal.h"
@@ -199,8 +199,8 @@ static crypto_word_t bn_less_than_word_mask(const BN_ULONG *a, size_t len,
}
// |a| < |b| iff a[1..len-1] are all zero and a[0] < b.
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
crypto_word_t mask = 0;
for (size_t i = 1; i < len; i++) {
mask |= a[i];
@@ -18,6 +18,8 @@
#include <CNIOBoringSSL_mem.h>
#include <assert.h>
#include "internal.h"
#include "../../internal.h"
@@ -38,8 +40,8 @@ void RSAZ_1024_mod_exp_avx2(BN_ULONG result_norm[16],
const BN_ULONG m_norm[16], const BN_ULONG RR[16],
BN_ULONG k0,
BN_ULONG storage[MOD_EXP_CTIME_STORAGE_LEN]) {
OPENSSL_STATIC_ASSERT(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH % 64 == 0,
"MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH is too small");
static_assert(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH % 64 == 0,
"MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH is too small");
assert((uintptr_t)storage % 64 == 0);
BN_ULONG *a_inv, *m, *result, *table_s = storage + 40 * 3, *R2 = table_s;
@@ -56,10 +56,10 @@
#include <CNIOBoringSSL_bn.h>
#include <assert.h>
#include <string.h>
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_type_check.h>
#include "internal.h"
@@ -296,15 +296,14 @@ int BN_mask_bits(BIGNUM *a, int n) {
}
static int bn_count_low_zero_bits_word(BN_ULONG l) {
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
OPENSSL_STATIC_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
OPENSSL_STATIC_ASSERT(BN_BITS2 == sizeof(BN_ULONG) * 8,
"BN_ULONG has padding bits");
static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
static_assert(sizeof(int) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
static_assert(BN_BITS2 == sizeof(BN_ULONG) * 8, "BN_ULONG has padding bits");
// C has very bizarre rules for types smaller than an int.
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) >= sizeof(int),
"BN_ULONG gets promoted to int");
static_assert(sizeof(BN_ULONG) >= sizeof(int),
"BN_ULONG gets promoted to int");
crypto_word_t mask;
int bits = 0;
@@ -342,10 +341,10 @@ static int bn_count_low_zero_bits_word(BN_ULONG l) {
}
int BN_count_low_zero_bits(const BIGNUM *bn) {
OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
OPENSSL_STATIC_ASSERT(sizeof(int) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
static_assert(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
static_assert(sizeof(int) <= sizeof(crypto_word_t),
"crypto_word_t is too small");
int ret = 0;
crypto_word_t saw_nonzero = 0;
@@ -335,7 +335,7 @@ ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key,
#endif
static EVP_AES_GCM_CTX *aes_gcm_from_cipher_ctx(EVP_CIPHER_CTX *ctx) {
OPENSSL_STATIC_ASSERT(
static_assert(
alignof(EVP_AES_GCM_CTX) <= 16,
"EVP_AES_GCM_CTX needs more alignment than this function provides");
@@ -921,12 +921,12 @@ static int aead_aes_gcm_init_impl(struct aead_aes_gcm_ctx *gcm_ctx,
return 1;
}
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_gcm_ctx),
"AEAD state is too small");
OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_gcm_ctx),
"AEAD state has insufficient alignment");
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_gcm_ctx),
"AEAD state is too small");
static_assert(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_gcm_ctx),
"AEAD state has insufficient alignment");
static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
size_t key_len, size_t requested_tag_len) {
@@ -1259,12 +1259,12 @@ struct aead_aes_gcm_tls12_ctx {
uint64_t min_next_nonce;
};
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_gcm_tls12_ctx),
"AEAD state is too small");
OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_gcm_tls12_ctx),
"AEAD state has insufficient alignment");
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_gcm_tls12_ctx),
"AEAD state is too small");
static_assert(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_gcm_tls12_ctx),
"AEAD state has insufficient alignment");
static int aead_aes_gcm_tls12_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
size_t key_len, size_t requested_tag_len) {
@@ -1353,12 +1353,12 @@ struct aead_aes_gcm_tls13_ctx {
uint8_t first;
};
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_gcm_tls13_ctx),
"AEAD state is too small");
OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_gcm_tls13_ctx),
"AEAD state has insufficient alignment");
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_gcm_tls13_ctx),
"AEAD state is too small");
static_assert(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_gcm_tls13_ctx),
"AEAD state has insufficient alignment");
static int aead_aes_gcm_tls13_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
size_t key_len, size_t requested_tag_len) {
@@ -276,12 +276,12 @@ struct aead_aes_ccm_ctx {
struct ccm128_context ccm;
};
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_ccm_ctx),
"AEAD state is too small");
OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_ccm_ctx),
"AEAD state has insufficient alignment");
static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_ccm_ctx),
"AEAD state is too small");
static_assert(alignof(union evp_aead_ctx_st_state) >=
alignof(struct aead_aes_ccm_ctx),
"AEAD state has insufficient alignment");
static int aead_aes_ccm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
size_t key_len, size_t tag_len, unsigned M,
@@ -432,3 +432,22 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_bluetooth_8) {
out->seal_scatter = aead_aes_ccm_seal_scatter;
out->open_gather = aead_aes_ccm_open_gather;
}
static int aead_aes_ccm_matter_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
size_t key_len, size_t tag_len) {
return aead_aes_ccm_init(ctx, key, key_len, tag_len, 16, 2);
}
DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_ccm_matter) {
memset(out, 0, sizeof(EVP_AEAD));
out->key_len = 16;
out->nonce_len = 13;
out->overhead = 16;
out->max_tag_len = 16;
out->init = aead_aes_ccm_matter_init;
out->cleanup = aead_aes_ccm_cleanup;
out->seal_scatter = aead_aes_ccm_seal_scatter;
out->open_gather = aead_aes_ccm_open_gather;
}
@@ -111,6 +111,8 @@ void DH_free(DH *dh) {
OPENSSL_free(dh);
}
unsigned DH_bits(const DH *dh) { return BN_num_bits(dh->p); }
const BIGNUM *DH_get0_pub_key(const DH *dh) { return dh->pub_key; }
const BIGNUM *DH_get0_priv_key(const DH *dh) { return dh->priv_key; }
@@ -1155,8 +1155,8 @@ void ec_affine_select(const EC_GROUP *group, EC_AFFINE *out, BN_ULONG mask,
void ec_precomp_select(const EC_GROUP *group, EC_PRECOMP *out, BN_ULONG mask,
const EC_PRECOMP *a, const EC_PRECOMP *b) {
OPENSSL_STATIC_ASSERT(sizeof(out->comb) == sizeof(*out),
"out->comb does not span the entire structure");
static_assert(sizeof(out->comb) == sizeof(*out),
"out->comb does not span the entire structure");
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(out->comb); i++) {
ec_affine_select(group, &out->comb[i], mask, &a->comb[i], &b->comb[i]);
}
@@ -70,10 +70,11 @@
#include <CNIOBoringSSL_base.h>
#include <assert.h>
#include <CNIOBoringSSL_bn.h>
#include <CNIOBoringSSL_ec.h>
#include <CNIOBoringSSL_ex_data.h>
#include <CNIOBoringSSL_type_check.h>
#include "../bn/internal.h"
@@ -91,8 +92,8 @@ extern "C" {
#define EC_MAX_BYTES 66
#define EC_MAX_WORDS ((EC_MAX_BYTES + BN_BYTES - 1) / BN_BYTES)
OPENSSL_STATIC_ASSERT(EC_MAX_WORDS <= BN_SMALL_MAX_WORDS,
"bn_*_small functions not usable");
static_assert(EC_MAX_WORDS <= BN_SMALL_MAX_WORDS,
"bn_*_small functions not usable");
// Scalars.
@@ -64,16 +64,6 @@ static inline void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
ecp_nistz256_mul_mont(res, in, ONE);
}
// ecp_nistz256_to_mont sets |res| to |in|, converted to Montgomery domain
// by multiplying with RR = 2^512 mod P precomputed for NIST P256 curve.
static inline void ecp_nistz256_to_mont(BN_ULONG res[P256_LIMBS],
const BN_ULONG in[P256_LIMBS]) {
static const BN_ULONG RR[P256_LIMBS] = {
TOBN(0x00000000, 0x00000003), TOBN(0xfffffffb, 0xffffffff),
TOBN(0xffffffff, 0xfffffffe), TOBN(0x00000004, 0xfffffffd)};
ecp_nistz256_mul_mont(res, in, RR);
}
// P-256 scalar operations.
//
@@ -22,7 +22,6 @@
#include <CNIOBoringSSL_ec.h>
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_type_check.h>
#include <assert.h>
#include <string.h>
@@ -202,9 +202,8 @@ int ec_GFp_mont_init_precomp(const EC_GROUP *group, EC_PRECOMP *out,
// Store the comb in affine coordinates to shrink the table. (This reduces
// cache pressure and makes the constant-time selects faster.)
OPENSSL_STATIC_ASSERT(
OPENSSL_ARRAY_SIZE(comb) == OPENSSL_ARRAY_SIZE(out->comb),
"comb sizes did not match");
static_assert(OPENSSL_ARRAY_SIZE(comb) == OPENSSL_ARRAY_SIZE(out->comb),
"comb sizes did not match");
return ec_jacobian_to_affine_batch(group, out->comb, comb,
OPENSSL_ARRAY_SIZE(comb));
}
@@ -59,7 +59,6 @@
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_sha.h>
#include <CNIOBoringSSL_type_check.h>
#include "../../internal.h"
#include "../bn/internal.h"
@@ -322,8 +321,8 @@ ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
// Pass a SHA512 hash of the private key and digest as additional data
// into the RBG. This is a hardening measure against entropy failure.
OPENSSL_STATIC_ASSERT(SHA512_DIGEST_LENGTH >= 32,
"additional_data is too large for SHA-512");
static_assert(SHA512_DIGEST_LENGTH >= 32,
"additional_data is too large for SHA-512");
FIPS_service_indicator_lock_state();
@@ -224,9 +224,9 @@ int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, unsigned int *out_len) {
return ret;
}
size_t HMAC_size(const HMAC_CTX *ctx) {
return EVP_MD_size(ctx->md);
}
size_t HMAC_size(const HMAC_CTX *ctx) { return EVP_MD_size(ctx->md); }
const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx) { return ctx->md; }
int HMAC_CTX_copy_ex(HMAC_CTX *dest, const HMAC_CTX *src) {
if (!EVP_MD_CTX_copy_ex(&dest->i_ctx, &src->i_ctx) ||
@@ -49,8 +49,6 @@
#include <assert.h>
#include <string.h>
#include <CNIOBoringSSL_type_check.h>
#include "internal.h"
#include "../../internal.h"
@@ -120,8 +118,8 @@ void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
if ((inptr >= 32 && outptr <= inptr - 32) || inptr < outptr) {
// If |out| is at least two blocks behind |in| or completely disjoint, there
// is no need to decrypt to a temporary block.
OPENSSL_STATIC_ASSERT(16 % sizeof(crypto_word_t) == 0,
"block cannot be evenly divided into words");
static_assert(16 % sizeof(crypto_word_t) == 0,
"block cannot be evenly divided into words");
const uint8_t *iv = ivec;
while (len >= 16) {
(*block)(in, out, key);
@@ -136,8 +134,8 @@ void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
}
OPENSSL_memcpy(ivec, iv, 16);
} else {
OPENSSL_STATIC_ASSERT(16 % sizeof(crypto_word_t) == 0,
"block cannot be evenly divided into words");
static_assert(16 % sizeof(crypto_word_t) == 0,
"block cannot be evenly divided into words");
while (len >= 16) {
(*block)(in, tmp, key);
@@ -46,16 +46,13 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ==================================================================== */
#include <CNIOBoringSSL_type_check.h>
#include <assert.h>
#include <string.h>
#include "internal.h"
OPENSSL_STATIC_ASSERT(16 % sizeof(size_t) == 0,
"block cannot be divided into size_t");
static_assert(16 % sizeof(size_t) == 0, "block cannot be divided into size_t");
void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
const AES_KEY *key, uint8_t ivec[16], unsigned *num,
@@ -46,8 +46,6 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ==================================================================== */
#include <CNIOBoringSSL_type_check.h>
#include <assert.h>
#include <string.h>
@@ -70,8 +68,8 @@ static void ctr128_inc(uint8_t *counter) {
} while (n);
}
OPENSSL_STATIC_ASSERT(16 % sizeof(crypto_word_t) == 0,
"block cannot be divided into crypto_word_t");
static_assert(16 % sizeof(crypto_word_t) == 0,
"block cannot be divided into crypto_word_t");
// The input encrypted as though 128bit counter mode is being used. The extra
// state information to record how much of the 128bit block we have used is
@@ -46,16 +46,13 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ==================================================================== */
#include <CNIOBoringSSL_type_check.h>
#include <assert.h>
#include <string.h>
#include "internal.h"
OPENSSL_STATIC_ASSERT(16 % sizeof(size_t) == 0,
"block cannot be divided into size_t");
static_assert(16 % sizeof(size_t) == 0, "block cannot be divided into size_t");
void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
const AES_KEY *key, uint8_t ivec[16], unsigned *num,
@@ -33,58 +33,6 @@ LordK:
.byte 69,67,80,95,78,73,83,84,90,50,53,54,32,102,111,114,32,65,82,77,118,56,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
.align 2
// void ecp_nistz256_to_mont(BN_ULONG x0[4],const BN_ULONG x1[4]);
.globl _ecp_nistz256_to_mont
.private_extern _ecp_nistz256_to_mont
.align 6
_ecp_nistz256_to_mont:
AARCH64_SIGN_LINK_REGISTER
stp x29,x30,[sp,#-32]!
add x29,sp,#0
stp x19,x20,[sp,#16]
ldr x3,LRR // bp[0]
ldp x4,x5,[x1]
ldp x6,x7,[x1,#16]
ldr x12,Lpoly+8
ldr x13,Lpoly+24
adr x2,LRR // &bp[0]
bl __ecp_nistz256_mul_mont
ldp x19,x20,[sp,#16]
ldp x29,x30,[sp],#32
AARCH64_VALIDATE_LINK_REGISTER
ret
// void ecp_nistz256_from_mont(BN_ULONG x0[4],const BN_ULONG x1[4]);
.globl _ecp_nistz256_from_mont
.private_extern _ecp_nistz256_from_mont
.align 4
_ecp_nistz256_from_mont:
AARCH64_SIGN_LINK_REGISTER
stp x29,x30,[sp,#-32]!
add x29,sp,#0
stp x19,x20,[sp,#16]
mov x3,#1 // bp[0]
ldp x4,x5,[x1]
ldp x6,x7,[x1,#16]
ldr x12,Lpoly+8
ldr x13,Lpoly+24
adr x2,Lone // &bp[0]
bl __ecp_nistz256_mul_mont
ldp x19,x20,[sp,#16]
ldp x29,x30,[sp],#32
AARCH64_VALIDATE_LINK_REGISTER
ret
// void ecp_nistz256_mul_mont(BN_ULONG x0[4],const BN_ULONG x1[4],
// const BN_ULONG x2[4]);
.globl _ecp_nistz256_mul_mont
@@ -34,58 +34,6 @@
.byte 69,67,80,95,78,73,83,84,90,50,53,54,32,102,111,114,32,65,82,77,118,56,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
.align 2
// void ecp_nistz256_to_mont(BN_ULONG x0[4],const BN_ULONG x1[4]);
.globl ecp_nistz256_to_mont
.hidden ecp_nistz256_to_mont
.type ecp_nistz256_to_mont,%function
.align 6
ecp_nistz256_to_mont:
AARCH64_SIGN_LINK_REGISTER
stp x29,x30,[sp,#-32]!
add x29,sp,#0
stp x19,x20,[sp,#16]
ldr x3,.LRR // bp[0]
ldp x4,x5,[x1]
ldp x6,x7,[x1,#16]
ldr x12,.Lpoly+8
ldr x13,.Lpoly+24
adr x2,.LRR // &bp[0]
bl __ecp_nistz256_mul_mont
ldp x19,x20,[sp,#16]
ldp x29,x30,[sp],#32
AARCH64_VALIDATE_LINK_REGISTER
ret
.size ecp_nistz256_to_mont,.-ecp_nistz256_to_mont
// void ecp_nistz256_from_mont(BN_ULONG x0[4],const BN_ULONG x1[4]);
.globl ecp_nistz256_from_mont
.hidden ecp_nistz256_from_mont
.type ecp_nistz256_from_mont,%function
.align 4
ecp_nistz256_from_mont:
AARCH64_SIGN_LINK_REGISTER
stp x29,x30,[sp,#-32]!
add x29,sp,#0
stp x19,x20,[sp,#16]
mov x3,#1 // bp[0]
ldp x4,x5,[x1]
ldp x6,x7,[x1,#16]
ldr x12,.Lpoly+8
ldr x13,.Lpoly+24
adr x2,.Lone // &bp[0]
bl __ecp_nistz256_mul_mont
ldp x19,x20,[sp,#16]
ldp x29,x30,[sp],#32
AARCH64_VALIDATE_LINK_REGISTER
ret
.size ecp_nistz256_from_mont,.-ecp_nistz256_from_mont
// void ecp_nistz256_mul_mont(BN_ULONG x0[4],const BN_ULONG x1[4],
// const BN_ULONG x2[4]);
.globl ecp_nistz256_mul_mont
@@ -12,9 +12,10 @@
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <CNIOBoringSSL_rand.h>
#include <CNIOBoringSSL_ctrdrbg.h>
#include <assert.h>
#include <CNIOBoringSSL_type_check.h>
#include <CNIOBoringSSL_mem.h>
#include "internal.h"
@@ -28,6 +29,21 @@
// See table 3.
static const uint64_t kMaxReseedCount = UINT64_C(1) << 48;
CTR_DRBG_STATE *CTR_DRBG_new(const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
const uint8_t *personalization,
size_t personalization_len) {
CTR_DRBG_STATE *drbg = OPENSSL_malloc(sizeof(CTR_DRBG_STATE));
if (drbg == NULL ||
!CTR_DRBG_init(drbg, entropy, personalization, personalization_len)) {
CTR_DRBG_free(drbg);
return NULL;
}
return drbg;
}
void CTR_DRBG_free(CTR_DRBG_STATE *state) { OPENSSL_free(state); }
int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
const uint8_t *personalization, size_t personalization_len) {
@@ -65,8 +81,8 @@ int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
return 1;
}
OPENSSL_STATIC_ASSERT(CTR_DRBG_ENTROPY_LEN % AES_BLOCK_SIZE == 0,
"not a multiple of AES block size");
static_assert(CTR_DRBG_ENTROPY_LEN % AES_BLOCK_SIZE == 0,
"not a multiple of AES block size");
// ctr_inc adds |n| to the last four bytes of |drbg->counter|, treated as a
// big-endian number.
@@ -21,18 +21,17 @@
#include "fork_detect.h"
#if defined(OPENSSL_LINUX)
#include <assert.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <CNIOBoringSSL_type_check.h>
#include "../delocate.h"
#include "../../internal.h"
#if defined(MADV_WIPEONFORK)
OPENSSL_STATIC_ASSERT(MADV_WIPEONFORK == 18, "MADV_WIPEONFORK is not 18");
static_assert(MADV_WIPEONFORK == 18, "MADV_WIPEONFORK is not 18");
#else
#define MADV_WIPEONFORK 18
#endif
@@ -32,6 +32,8 @@
#define EXPECTED_NR_getrandom 384
#elif defined(OPENSSL_PPC64LE)
#define EXPECTED_NR_getrandom 359
#elif defined(OPENSSL_RISCV64)
#define EXPECTED_NR_getrandom 278
#endif
#if defined(EXPECTED_NR_getrandom)
@@ -16,6 +16,7 @@
#define OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
#include <CNIOBoringSSL_aes.h>
#include <CNIOBoringSSL_ctrdrbg.h>
#include "../../internal.h"
#include "../modes/internal.h"
@@ -49,10 +50,10 @@ void CRYPTO_get_seed_entropy(uint8_t *out_entropy, size_t out_entropy_len,
int *out_used_cpu);
// RAND_load_entropy supplies |entropy_len| bytes of entropy to the module. The
// |from_cpu| parameter is true iff the entropy was obtained directly from the
// CPU.
// |want_additional_input| parameter is true iff the entropy was obtained from
// a source other than the system, e.g. directly from the CPU.
void RAND_load_entropy(const uint8_t *entropy, size_t entropy_len,
int from_cpu);
int want_additional_input);
// RAND_need_entropy is implemented outside of the FIPS module and is called
// when the module has stopped because it has run out of entropy.
@@ -95,17 +96,13 @@ int rand_fork_unsafe_buffering_enabled(void);
// CTR_DRBG_STATE contains the state of a CTR_DRBG based on AES-256. See SP
// 800-90Ar1.
typedef struct {
struct ctr_drbg_state_st {
AES_KEY ks;
block128_f block;
ctr128_f ctr;
uint8_t counter[16];
uint64_t reseed_counter;
} CTR_DRBG_STATE;
// See SP 800-90Ar1, table 3.
#define CTR_DRBG_ENTROPY_LEN 48
#define CTR_DRBG_MAX_GENERATE_LENGTH 65536
};
// CTR_DRBG_init initialises |*drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of
// entropy in |entropy| and, optionally, a personalization string up to
@@ -116,27 +113,6 @@ OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
const uint8_t *personalization,
size_t personalization_len);
// CTR_DRBG_reseed reseeds |drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of entropy
// in |entropy| and, optionally, up to |CTR_DRBG_ENTROPY_LEN| bytes of
// additional data. It returns one on success or zero on error.
OPENSSL_EXPORT int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
const uint8_t *additional_data,
size_t additional_data_len);
// CTR_DRBG_generate processes to up |CTR_DRBG_ENTROPY_LEN| bytes of additional
// data (if any) and then writes |out_len| random bytes to |out|, where
// |out_len| <= |CTR_DRBG_MAX_GENERATE_LENGTH|. It returns one on success or
// zero on error.
OPENSSL_EXPORT int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out,
size_t out_len,
const uint8_t *additional_data,
size_t additional_data_len);
// CTR_DRBG_clear zeroises the state of |drbg|.
OPENSSL_EXPORT void CTR_DRBG_clear(CTR_DRBG_STATE *drbg);
#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM)
OPENSSL_INLINE int have_rdrand(void) {
@@ -23,8 +23,8 @@
#endif
#include <CNIOBoringSSL_chacha.h>
#include <CNIOBoringSSL_ctrdrbg.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_type_check.h>
#include "internal.h"
#include "fork_detect.h"
@@ -162,10 +162,10 @@ static int rdrand(uint8_t *buf, size_t len) {
#if defined(BORINGSSL_FIPS)
void CRYPTO_get_seed_entropy(uint8_t *out_entropy, size_t out_entropy_len,
int *out_used_cpu) {
*out_used_cpu = 0;
int *out_want_additional_input) {
*out_want_additional_input = 0;
if (have_rdrand() && rdrand(out_entropy, out_entropy_len)) {
*out_used_cpu = 1;
*out_want_additional_input = 1;
} else {
CRYPTO_sysrand_for_seed(out_entropy, out_entropy_len);
}
@@ -183,20 +183,22 @@ void CRYPTO_get_seed_entropy(uint8_t *out_entropy, size_t out_entropy_len,
struct entropy_buffer {
// bytes contains entropy suitable for seeding a DRBG.
uint8_t bytes[CTR_DRBG_ENTROPY_LEN * BORINGSSL_FIPS_OVERREAD];
uint8_t
bytes[CRNGT_BLOCK_SIZE + CTR_DRBG_ENTROPY_LEN * BORINGSSL_FIPS_OVERREAD];
// bytes_valid indicates the number of bytes of |bytes| that contain valid
// data.
size_t bytes_valid;
// from_cpu is true if any of the contents of |bytes| were obtained directly
// from the CPU.
int from_cpu;
// want_additional_input is true if any of the contents of |bytes| were
// obtained via a method other than from the kernel. In these cases entropy
// from the kernel is also provided via an additional input to the DRBG.
int want_additional_input;
};
DEFINE_BSS_GET(struct entropy_buffer, entropy_buffer);
DEFINE_STATIC_MUTEX(entropy_buffer_lock);
void RAND_load_entropy(const uint8_t *entropy, size_t entropy_len,
int from_cpu) {
int want_additional_input) {
struct entropy_buffer *const buffer = entropy_buffer_bss_get();
CRYPTO_STATIC_MUTEX_lock_write(entropy_buffer_lock_bss_get());
@@ -207,14 +209,15 @@ void RAND_load_entropy(const uint8_t *entropy, size_t entropy_len,
OPENSSL_memcpy(&buffer->bytes[buffer->bytes_valid], entropy, entropy_len);
buffer->bytes_valid += entropy_len;
buffer->from_cpu |= from_cpu && (entropy_len != 0);
buffer->want_additional_input |=
want_additional_input && (entropy_len != 0);
CRYPTO_STATIC_MUTEX_unlock_write(entropy_buffer_lock_bss_get());
}
// get_seed_entropy fills |out_entropy_len| bytes of |out_entropy| from the
// global |entropy_buffer|.
static void get_seed_entropy(uint8_t *out_entropy, size_t out_entropy_len,
int *out_used_cpu) {
int *out_want_additional_input) {
struct entropy_buffer *const buffer = entropy_buffer_bss_get();
if (out_entropy_len > sizeof(buffer->bytes)) {
abort();
@@ -227,53 +230,66 @@ static void get_seed_entropy(uint8_t *out_entropy, size_t out_entropy_len,
CRYPTO_STATIC_MUTEX_lock_write(entropy_buffer_lock_bss_get());
}
*out_used_cpu = buffer->from_cpu;
*out_want_additional_input = buffer->want_additional_input;
OPENSSL_memcpy(out_entropy, buffer->bytes, out_entropy_len);
OPENSSL_memmove(buffer->bytes, &buffer->bytes[out_entropy_len],
buffer->bytes_valid - out_entropy_len);
buffer->bytes_valid -= out_entropy_len;
if (buffer->bytes_valid == 0) {
buffer->from_cpu = 0;
buffer->want_additional_input = 0;
}
CRYPTO_STATIC_MUTEX_unlock_write(entropy_buffer_lock_bss_get());
}
// rand_get_seed fills |seed| with entropy and sets |*out_used_cpu| to one if
// that entropy came directly from the CPU and zero otherwise.
// rand_get_seed fills |seed| with entropy. In some cases, it will additionally
// fill |additional_input| with entropy to supplement |seed|. It sets
// |*out_additional_input_len| to the number of extra bytes.
static void rand_get_seed(struct rand_thread_state *state,
uint8_t seed[CTR_DRBG_ENTROPY_LEN],
int *out_used_cpu) {
if (!state->last_block_valid) {
int unused;
get_seed_entropy(state->last_block, sizeof(state->last_block), &unused);
state->last_block_valid = 1;
uint8_t additional_input[CTR_DRBG_ENTROPY_LEN],
size_t *out_additional_input_len) {
uint8_t entropy_bytes[sizeof(state->last_block) +
CTR_DRBG_ENTROPY_LEN * BORINGSSL_FIPS_OVERREAD];
uint8_t *entropy = entropy_bytes;
size_t entropy_len = sizeof(entropy_bytes);
if (state->last_block_valid) {
// No need to fill |state->last_block| with entropy from the read.
entropy += sizeof(state->last_block);
entropy_len -= sizeof(state->last_block);
}
uint8_t entropy[CTR_DRBG_ENTROPY_LEN * BORINGSSL_FIPS_OVERREAD];
get_seed_entropy(entropy, sizeof(entropy), out_used_cpu);
int want_additional_input;
get_seed_entropy(entropy, entropy_len, &want_additional_input);
if (!state->last_block_valid) {
OPENSSL_memcpy(state->last_block, entropy, sizeof(state->last_block));
entropy += sizeof(state->last_block);
entropy_len -= sizeof(state->last_block);
}
// See FIPS 140-2, section 4.9.2. This is the “continuous random number
// generator test” which causes the program to randomly abort. Hopefully the
// rate of failure is small enough not to be a problem in practice.
if (CRYPTO_memcmp(state->last_block, entropy, CRNGT_BLOCK_SIZE) == 0) {
if (CRYPTO_memcmp(state->last_block, entropy, sizeof(state->last_block)) ==
0) {
fprintf(stderr, "CRNGT failed.\n");
BORINGSSL_FIPS_abort();
}
OPENSSL_STATIC_ASSERT(sizeof(entropy) % CRNGT_BLOCK_SIZE == 0, "");
for (size_t i = CRNGT_BLOCK_SIZE; i < sizeof(entropy);
i += CRNGT_BLOCK_SIZE) {
assert(entropy_len % CRNGT_BLOCK_SIZE == 0);
for (size_t i = CRNGT_BLOCK_SIZE; i < entropy_len; i += CRNGT_BLOCK_SIZE) {
if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i,
CRNGT_BLOCK_SIZE) == 0) {
fprintf(stderr, "CRNGT failed.\n");
BORINGSSL_FIPS_abort();
}
}
OPENSSL_memcpy(state->last_block,
entropy + sizeof(entropy) - CRNGT_BLOCK_SIZE,
OPENSSL_memcpy(state->last_block, entropy + entropy_len - CRNGT_BLOCK_SIZE,
CRNGT_BLOCK_SIZE);
assert(entropy_len == BORINGSSL_FIPS_OVERREAD * CTR_DRBG_ENTROPY_LEN);
OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN);
for (size_t i = 1; i < BORINGSSL_FIPS_OVERREAD; i++) {
@@ -281,19 +297,30 @@ static void rand_get_seed(struct rand_thread_state *state,
seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j];
}
}
// If we used something other than system entropy then also
// opportunistically read from the system. This avoids solely relying on the
// hardware once the entropy pool has been initialized.
*out_additional_input_len = 0;
if (want_additional_input &&
CRYPTO_sysrand_if_available(additional_input, CTR_DRBG_ENTROPY_LEN)) {
*out_additional_input_len = CTR_DRBG_ENTROPY_LEN;
}
}
#else
// rand_get_seed fills |seed| with entropy and sets |*out_used_cpu| to one if
// that entropy came directly from the CPU and zero otherwise.
// rand_get_seed fills |seed| with entropy. In some cases, it will additionally
// fill |additional_input| with entropy to supplement |seed|. It sets
// |*out_additional_input_len| to the number of extra bytes.
static void rand_get_seed(struct rand_thread_state *state,
uint8_t seed[CTR_DRBG_ENTROPY_LEN],
int *out_used_cpu) {
uint8_t additional_input[CTR_DRBG_ENTROPY_LEN],
size_t *out_additional_input_len) {
// If not in FIPS mode, we don't overread from the system entropy source and
// we don't depend only on the hardware RDRAND.
CRYPTO_sysrand_for_seed(seed, CTR_DRBG_ENTROPY_LEN);
*out_used_cpu = 0;
*out_additional_input_len = 0;
}
#endif
@@ -352,20 +379,9 @@ void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
state->last_block_valid = 0;
uint8_t seed[CTR_DRBG_ENTROPY_LEN];
int used_cpu;
rand_get_seed(state, seed, &used_cpu);
uint8_t personalization[CTR_DRBG_ENTROPY_LEN] = {0};
size_t personalization_len = 0;
#if defined(OPENSSL_URANDOM)
// If we used RDRAND, also opportunistically read from the system. This
// avoids solely relying on the hardware once the entropy pool has been
// initialized.
if (used_cpu &&
CRYPTO_sysrand_if_available(personalization, sizeof(personalization))) {
personalization_len = sizeof(personalization);
}
#endif
rand_get_seed(state, seed, personalization, &personalization_len);
if (!CTR_DRBG_init(&state->drbg, seed, personalization,
personalization_len)) {
@@ -392,8 +408,10 @@ void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
if (state->calls >= kReseedInterval ||
state->fork_generation != fork_generation) {
uint8_t seed[CTR_DRBG_ENTROPY_LEN];
int used_cpu;
rand_get_seed(state, seed, &used_cpu);
uint8_t reseed_additional_data[CTR_DRBG_ENTROPY_LEN] = {0};
size_t reseed_additional_data_len = 0;
rand_get_seed(state, seed, reseed_additional_data,
&reseed_additional_data_len);
#if defined(BORINGSSL_FIPS)
// Take a read lock around accesses to |state->drbg|. This is needed to
// avoid returning bad entropy if we race with
@@ -405,7 +423,8 @@ void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
// kernel, syscalls made with |syscall| did not abort the transaction.
CRYPTO_STATIC_MUTEX_lock_read(state_clear_all_lock_bss_get());
#endif
if (!CTR_DRBG_reseed(&state->drbg, seed, NULL, 0)) {
if (!CTR_DRBG_reseed(&state->drbg, seed, reseed_additional_data,
reseed_additional_data_len)) {
abort();
}
state->calls = 0;
@@ -454,3 +473,10 @@ int RAND_bytes(uint8_t *out, size_t out_len) {
int RAND_pseudo_bytes(uint8_t *buf, size_t len) {
return RAND_bytes(buf, len);
}
void RAND_get_system_entropy_for_custom_prng(uint8_t *buf, size_t len) {
if (len > 256) {
abort();
}
CRYPTO_sysrand_for_seed(buf, len);
}
@@ -62,14 +62,11 @@
#include <sys/random.h>
#endif
#if defined(OPENSSL_FREEBSD)
#define URANDOM_BLOCKS_FOR_ENTROPY
#if __FreeBSD__ >= 12
#if defined(OPENSSL_FREEBSD) && __FreeBSD__ >= 12
// getrandom is supported in FreeBSD 12 and up.
#define FREEBSD_GETRANDOM
#include <sys/random.h>
#endif
#endif
#include <CNIOBoringSSL_thread.h>
#include <CNIOBoringSSL_mem.h>
@@ -190,15 +187,21 @@ static void init_once(void) {
return;
#endif
// Android FIPS builds must support getrandom.
#if defined(BORINGSSL_FIPS) && defined(OPENSSL_ANDROID)
// FIPS builds must support getrandom.
//
// Historically, only Android FIPS builds required getrandom, while Linux FIPS
// builds had a /dev/urandom fallback which used RNDGETENTCNT as a poor
// approximation for getrandom's blocking behavior. This is now removed, but
// avoid making assumptions on this removal until March 2023, in case it needs
// to be restored. This comment can be deleted after March 2023.
#if defined(BORINGSSL_FIPS)
perror("getrandom not found");
abort();
#endif
int fd;
do {
fd = open("/dev/urandom", O_RDONLY);
fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
} while (fd == -1 && errno == EINTR);
if (fd < 0) {
@@ -206,20 +209,6 @@ static void init_once(void) {
abort();
}
int flags = fcntl(fd, F_GETFD);
if (flags == -1) {
// Native Client doesn't implement |fcntl|.
if (errno != ENOSYS) {
perror("failed to get flags from urandom fd");
abort();
}
} else {
flags |= FD_CLOEXEC;
if (fcntl(fd, F_SETFD, flags) == -1) {
perror("failed to set FD_CLOEXEC on urandom fd");
abort();
}
}
*urandom_fd_bss_get() = fd;
}
@@ -269,29 +258,6 @@ static void wait_for_entropy(void) {
#endif // USE_NR_getrandom
return;
}
#if defined(BORINGSSL_FIPS) && !defined(URANDOM_BLOCKS_FOR_ENTROPY)
// In FIPS mode on platforms where urandom doesn't block at startup, we ensure
// that the kernel has sufficient entropy before continuing. This is
// automatically handled by getrandom, which requires that the entropy pool
// has been initialised, but for urandom we have to poll.
for (;;) {
int entropy_bits;
if (ioctl(fd, RNDGETENTCNT, &entropy_bits)) {
fprintf(stderr,
"RNDGETENTCNT on /dev/urandom failed. We cannot continue in this "
"case when in FIPS mode.\n");
abort();
}
static const int kBitsNeeded = 256;
if (entropy_bits >= kBitsNeeded) {
break;
}
usleep(250000);
}
#endif // BORINGSSL_FIPS && !URANDOM_BLOCKS_FOR_ENTROPY
}
// fill_with_entropy writes |len| bytes of entropy into |out|. It returns one
@@ -489,29 +489,23 @@ static const uint8_t kPSSZeroes[] = {0, 0, 0, 0, 0, 0, 0, 0};
int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash,
const EVP_MD *Hash, const EVP_MD *mgf1Hash,
const uint8_t *EM, int sLen) {
int i;
int ret = 0;
int maskedDBLen, MSBits, emLen;
size_t hLen;
const uint8_t *H;
uint8_t *DB = NULL;
EVP_MD_CTX ctx;
uint8_t H_[EVP_MAX_MD_SIZE];
EVP_MD_CTX_init(&ctx);
if (mgf1Hash == NULL) {
mgf1Hash = Hash;
}
hLen = EVP_MD_size(Hash);
int ret = 0;
uint8_t *DB = NULL;
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&ctx);
FIPS_service_indicator_lock_state();
// Negative sLen has special meanings:
// -1 sLen == hLen
// -2 salt length is autorecovered from signature
// -N reserved
size_t hLen = EVP_MD_size(Hash);
if (sLen == -1) {
sLen = hLen;
sLen = (int)hLen;
} else if (sLen == -2) {
sLen = -2;
} else if (sLen < -2) {
@@ -519,8 +513,8 @@ int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash,
goto err;
}
MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
emLen = RSA_size(rsa);
unsigned MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
size_t emLen = RSA_size(rsa);
if (EM[0] & (0xFF << MSBits)) {
OPENSSL_PUT_ERROR(RSA, RSA_R_FIRST_OCTET_INVALID);
goto err;
@@ -529,8 +523,9 @@ int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash,
EM++;
emLen--;
}
if (emLen < (int)hLen + 2 || emLen < ((int)hLen + sLen + 2)) {
// sLen can be small negative
// |sLen| may be -2 for the non-standard salt length recovery mode.
if (emLen < hLen + 2 ||
(sLen >= 0 && emLen < hLen + (size_t)sLen + 2)) {
OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
goto err;
}
@@ -538,8 +533,8 @@ int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash,
OPENSSL_PUT_ERROR(RSA, RSA_R_LAST_OCTET_INVALID);
goto err;
}
maskedDBLen = emLen - hLen - 1;
H = EM + maskedDBLen;
size_t maskedDBLen = emLen - hLen - 1;
const uint8_t *H = EM + maskedDBLen;
DB = OPENSSL_malloc(maskedDBLen);
if (!DB) {
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
@@ -548,42 +543,49 @@ int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash,
if (!PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash)) {
goto err;
}
for (i = 0; i < maskedDBLen; i++) {
for (size_t i = 0; i < maskedDBLen; i++) {
DB[i] ^= EM[i];
}
if (MSBits) {
DB[0] &= 0xFF >> (8 - MSBits);
}
for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) {
// This step differs slightly from EMSA-PSS-VERIFY (RFC 8017) step 10 because
// it accepts a non-standard salt recovery flow. DB should be some number of
// zeros, a one, then the salt.
size_t salt_start;
for (salt_start = 0; DB[salt_start] == 0 && salt_start < maskedDBLen - 1;
salt_start++) {
;
}
if (DB[i++] != 0x1) {
if (DB[salt_start] != 0x1) {
OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_RECOVERY_FAILED);
goto err;
}
if (sLen >= 0 && (maskedDBLen - i) != sLen) {
salt_start++;
// If a salt length was specified, check it matches.
if (sLen >= 0 && maskedDBLen - salt_start != (size_t)sLen) {
OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
goto err;
}
uint8_t H_[EVP_MAX_MD_SIZE];
if (!EVP_DigestInit_ex(&ctx, Hash, NULL) ||
!EVP_DigestUpdate(&ctx, kPSSZeroes, sizeof(kPSSZeroes)) ||
!EVP_DigestUpdate(&ctx, mHash, hLen) ||
!EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i) ||
!EVP_DigestUpdate(&ctx, DB + salt_start, maskedDBLen - salt_start) ||
!EVP_DigestFinal_ex(&ctx, H_, NULL)) {
goto err;
}
if (OPENSSL_memcmp(H_, H, hLen)) {
if (OPENSSL_memcmp(H_, H, hLen) != 0) {
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
ret = 0;
} else {
ret = 1;
goto err;
}
ret = 1;
err:
OPENSSL_free(DB);
EVP_MD_CTX_cleanup(&ctx);
FIPS_service_indicator_unlock_state();
return ret;
}
@@ -64,7 +64,6 @@
#include <CNIOBoringSSL_err.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_thread.h>
#include <CNIOBoringSSL_type_check.h>
#include "../../internal.h"
#include "../bn/internal.h"
@@ -406,8 +405,8 @@ static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
}
// Double the length of the cache.
OPENSSL_STATIC_ASSERT(MAX_BLINDINGS_PER_RSA < UINT_MAX / 2,
"MAX_BLINDINGS_PER_RSA too large");
static_assert(MAX_BLINDINGS_PER_RSA < UINT_MAX / 2,
"MAX_BLINDINGS_PER_RSA too large");
unsigned new_num_blindings = rsa->num_blindings * 2;
if (new_num_blindings == 0) {
new_num_blindings = 1;
@@ -417,9 +416,8 @@ static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
}
assert(new_num_blindings > rsa->num_blindings);
OPENSSL_STATIC_ASSERT(
MAX_BLINDINGS_PER_RSA < UINT_MAX / sizeof(BN_BLINDING *),
"MAX_BLINDINGS_PER_RSA too large");
static_assert(MAX_BLINDINGS_PER_RSA < UINT_MAX / sizeof(BN_BLINDING *),
"MAX_BLINDINGS_PER_RSA too large");
BN_BLINDING **new_blindings =
OPENSSL_malloc(sizeof(BN_BLINDING *) * new_num_blindings);
uint8_t *new_blindings_inuse = OPENSSL_malloc(new_num_blindings);
@@ -20,6 +20,7 @@
#include <CNIOBoringSSL_aead.h>
#include <CNIOBoringSSL_aes.h>
#include <CNIOBoringSSL_bn.h>
#include <CNIOBoringSSL_ctrdrbg.h>
#include <CNIOBoringSSL_dh.h>
#include <CNIOBoringSSL_digest.h>
#include <CNIOBoringSSL_ec.h>
@@ -133,7 +133,7 @@ int SHA224_Update(SHA256_CTX *ctx, const void *data, size_t len) {
return SHA256_Update(ctx, data, len);
}
static int sha256_final_impl(uint8_t *out, SHA256_CTX *c) {
static int sha256_final_impl(uint8_t *out, size_t md_len, SHA256_CTX *c) {
crypto_md32_final(&sha256_block_data_order, c->h, c->data, SHA256_CBLOCK,
&c->num, c->Nh, c->Nl, /*is_big_endian=*/1);
@@ -141,12 +141,12 @@ static int sha256_final_impl(uint8_t *out, SHA256_CTX *c) {
// 'final' function can fail. SHA-512 does not have a corresponding check.
// These functions already misbehave if the caller arbitrarily mutates |c|, so
// can we assume one of |SHA256_Init| or |SHA224_Init| was used?
if (c->md_len > SHA256_DIGEST_LENGTH) {
if (md_len > SHA256_DIGEST_LENGTH) {
return 0;
}
assert(c->md_len % 4 == 0);
const size_t out_words = c->md_len / 4;
assert(md_len % 4 == 0);
const size_t out_words = md_len / 4;
for (size_t i = 0; i < out_words; i++) {
CRYPTO_store_u32_be(out, c->h[i]);
out += 4;
@@ -162,14 +162,14 @@ int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *c) {
// |SHA256_Final| and expects |sha->md_len| to carry the size over.
//
// TODO(davidben): Add an assert and fix code to match them up.
return sha256_final_impl(out, c);
return sha256_final_impl(out, c->md_len, c);
}
int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *ctx) {
// SHA224_Init sets |ctx->md_len| to |SHA224_DIGEST_LENGTH|, so this has a
// smaller output.
// This function must be paired with |SHA224_Init|, which sets |ctx->md_len|
// to |SHA224_DIGEST_LENGTH|.
assert(ctx->md_len == SHA224_DIGEST_LENGTH);
return sha256_final_impl(out, ctx);
return sha256_final_impl(out, SHA224_DIGEST_LENGTH, ctx);
}
#ifndef SHA256_ASM
@@ -71,7 +71,7 @@
// this writing, so there is no need for a common collector/padding
// implementation yet.
static int sha512_final_impl(uint8_t *out, SHA512_CTX *sha);
static int sha512_final_impl(uint8_t *out, size_t md_len, SHA512_CTX *sha);
int SHA384_Init(SHA512_CTX *sha) {
sha->h[0] = UINT64_C(0xcbbb9d5dc1059ed8);
@@ -162,10 +162,10 @@ static void sha512_block_data_order(uint64_t *state, const uint8_t *in,
int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH], SHA512_CTX *sha) {
// |SHA384_Init| sets |sha->md_len| to |SHA384_DIGEST_LENGTH|, so this has a
// smaller output.
// This function must be paired with |SHA384_Init|, which sets |sha->md_len|
// to |SHA384_DIGEST_LENGTH|.
assert(sha->md_len == SHA384_DIGEST_LENGTH);
return sha512_final_impl(out, sha);
return sha512_final_impl(out, SHA384_DIGEST_LENGTH, sha);
}
int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len) {
@@ -177,10 +177,10 @@ int SHA512_256_Update(SHA512_CTX *sha, const void *data, size_t len) {
}
int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH], SHA512_CTX *sha) {
// |SHA512_256_Init| sets |sha->md_len| to |SHA512_256_DIGEST_LENGTH|, so this
// has a |smaller output.
// This function must be paired with |SHA512_256_Init|, which sets
// |sha->md_len| to |SHA512_256_DIGEST_LENGTH|.
assert(sha->md_len == SHA512_256_DIGEST_LENGTH);
return sha512_final_impl(out, sha);
return sha512_final_impl(out, SHA512_256_DIGEST_LENGTH, sha);
}
void SHA512_Transform(SHA512_CTX *c, const uint8_t block[SHA512_CBLOCK]) {
@@ -241,10 +241,10 @@ int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH], SHA512_CTX *sha) {
// |SHA512_Final| and expects |sha->md_len| to carry the size over.
//
// TODO(davidben): Add an assert and fix code to match them up.
return sha512_final_impl(out, sha);
return sha512_final_impl(out, sha->md_len, sha);
}
static int sha512_final_impl(uint8_t *out, SHA512_CTX *sha) {
static int sha512_final_impl(uint8_t *out, size_t md_len, SHA512_CTX *sha) {
uint8_t *p = sha->p;
size_t n = sha->num;
@@ -268,8 +268,8 @@ static int sha512_final_impl(uint8_t *out, SHA512_CTX *sha) {
return 0;
}
assert(sha->md_len % 8 == 0);
const size_t out_words = sha->md_len / 8;
assert(md_len % 8 == 0);
const size_t out_words = md_len / 8;
for (size_t i = 0; i < out_words; i++) {
CRYPTO_store_u64_be(out, sha->h[i]);
out += 8;
+18 -4
View File
@@ -40,6 +40,7 @@ struct evp_hpke_kem_st {
size_t public_key_len;
size_t private_key_len;
size_t seed_len;
size_t enc_len;
int (*init_key)(EVP_HPKE_KEY *key, const uint8_t *priv_key,
size_t priv_key_len);
int (*generate_key)(EVP_HPKE_KEY *key);
@@ -216,6 +217,7 @@ const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void) {
/*public_key_len=*/X25519_PUBLIC_VALUE_LEN,
/*private_key_len=*/X25519_PRIVATE_KEY_LEN,
/*seed_len=*/X25519_PRIVATE_KEY_LEN,
/*enc_len=*/X25519_PUBLIC_VALUE_LEN,
x25519_init_key,
x25519_generate_key,
x25519_encap_with_seed,
@@ -226,6 +228,8 @@ const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void) {
uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem) { return kem->id; }
size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem) { return kem->enc_len; }
void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key) {
OPENSSL_memset(key, 0, sizeof(EVP_HPKE_KEY));
}
@@ -315,6 +319,10 @@ const EVP_HPKE_KDF *EVP_hpke_hkdf_sha256(void) {
uint16_t EVP_HPKE_KDF_id(const EVP_HPKE_KDF *kdf) { return kdf->id; }
const EVP_MD *EVP_HPKE_KDF_hkdf_md(const EVP_HPKE_KDF *kdf) {
return kdf->hkdf_md_func();
}
const EVP_HPKE_AEAD *EVP_hpke_aes_128_gcm(void) {
static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_AES_128_GCM,
&EVP_aead_aes_128_gcm};
@@ -351,9 +359,9 @@ static int hpke_build_suite_id(const EVP_HPKE_CTX *ctx,
uint8_t out[HPKE_SUITE_ID_LEN]) {
CBB cbb;
int ret = CBB_init_fixed(&cbb, out, HPKE_SUITE_ID_LEN) &&
add_label_string(&cbb, "HPKE") &&
CBB_add_u16(&cbb, EVP_HPKE_DHKEM_X25519_HKDF_SHA256) &&
CBB_add_u16(&cbb, ctx->kdf->id) &&
add_label_string(&cbb, "HPKE") && //
CBB_add_u16(&cbb, ctx->kem->id) && //
CBB_add_u16(&cbb, ctx->kdf->id) && //
CBB_add_u16(&cbb, ctx->aead->id);
CBB_cleanup(&cbb);
return ret;
@@ -487,6 +495,7 @@ int EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
size_t seed_len) {
EVP_HPKE_CTX_zero(ctx);
ctx->is_sender = 1;
ctx->kem = kem;
ctx->kdf = kdf;
ctx->aead = aead;
uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
@@ -509,12 +518,13 @@ int EVP_HPKE_CTX_setup_recipient(EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key,
size_t info_len) {
EVP_HPKE_CTX_zero(ctx);
ctx->is_sender = 0;
ctx->kem = key->kem;
ctx->kdf = kdf;
ctx->aead = aead;
uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
size_t shared_secret_len;
if (!key->kem->decap(key, shared_secret, &shared_secret_len, enc, enc_len) ||
!hpke_key_schedule(ctx, shared_secret, sizeof(shared_secret), info,
!hpke_key_schedule(ctx, shared_secret, shared_secret_len, info,
info_len)) {
EVP_HPKE_CTX_cleanup(ctx);
return 0;
@@ -609,6 +619,10 @@ size_t EVP_HPKE_CTX_max_overhead(const EVP_HPKE_CTX *ctx) {
return EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(&ctx->aead_ctx));
}
const EVP_HPKE_KEM *EVP_HPKE_CTX_kem(const EVP_HPKE_CTX *ctx) {
return ctx->kem;
}
const EVP_HPKE_AEAD *EVP_HPKE_CTX_aead(const EVP_HPKE_CTX *ctx) {
return ctx->aead;
}
+13 -14
View File
@@ -1227,10 +1227,10 @@ static void poly_mul_vec_aux(vec_t *restrict out, vec_t *restrict scratch,
// poly_mul_vec sets |*out| to |x|×|y| mod (𝑥^n - 1).
static void poly_mul_vec(struct POLY_MUL_SCRATCH *scratch, struct poly *out,
const struct poly *x, const struct poly *y) {
OPENSSL_STATIC_ASSERT(sizeof(out->v) == sizeof(vec_t) * VECS_PER_POLY,
"struct poly is the wrong size");
OPENSSL_STATIC_ASSERT(alignof(struct poly) == alignof(vec_t),
"struct poly has incorrect alignment");
static_assert(sizeof(out->v) == sizeof(vec_t) * VECS_PER_POLY,
"struct poly is the wrong size");
static_assert(alignof(struct poly) == alignof(vec_t),
"struct poly has incorrect alignment");
poly_assert_normalized(x);
poly_assert_normalized(y);
@@ -1752,8 +1752,7 @@ static void poly_marshal_mod3(uint8_t out[HRSS_POLY3_BYTES],
// function uses that freedom to implement a flatter distribution of values.
static void poly_short_sample(struct poly *out,
const uint8_t in[HRSS_SAMPLE_BYTES]) {
OPENSSL_STATIC_ASSERT(HRSS_SAMPLE_BYTES == N - 1,
"HRSS_SAMPLE_BYTES incorrect");
static_assert(HRSS_SAMPLE_BYTES == N - 1, "HRSS_SAMPLE_BYTES incorrect");
for (size_t i = 0; i < N - 1; i++) {
uint16_t v = mod3(in[i]);
// Map {0, 1, 2} -> {0, 1, 0xffff}
@@ -1922,7 +1921,7 @@ struct private_key {
// that up.)
static struct public_key *public_key_from_external(
struct HRSS_public_key *ext) {
OPENSSL_STATIC_ASSERT(
static_assert(
sizeof(struct HRSS_public_key) >= sizeof(struct public_key) + 15,
"HRSS public key too small");
@@ -1934,7 +1933,7 @@ static struct public_key *public_key_from_external(
// issues.
static struct private_key *private_key_from_external(
struct HRSS_private_key *ext) {
OPENSSL_STATIC_ASSERT(
static_assert(
sizeof(struct HRSS_private_key) >= sizeof(struct private_key) + 15,
"HRSS private key too small");
@@ -2111,8 +2110,8 @@ int HRSS_decap(uint8_t out_shared_key[HRSS_KEY_BYTES],
// This is HMAC, expanded inline rather than using the |HMAC| function so that
// we can avoid dealing with possible allocation failures and so keep this
// function infallible.
OPENSSL_STATIC_ASSERT(sizeof(priv->hmac_key) <= sizeof(vars->masked_key),
"HRSS HMAC key larger than SHA-256 block size");
static_assert(sizeof(priv->hmac_key) <= sizeof(vars->masked_key),
"HRSS HMAC key larger than SHA-256 block size");
for (size_t i = 0; i < sizeof(priv->hmac_key); i++) {
vars->masked_key[i] = priv->hmac_key[i] ^ 0x36;
}
@@ -2134,8 +2133,8 @@ int HRSS_decap(uint8_t out_shared_key[HRSS_KEY_BYTES],
SHA256_Init(&vars->hash_ctx);
SHA256_Update(&vars->hash_ctx, vars->masked_key, sizeof(vars->masked_key));
SHA256_Update(&vars->hash_ctx, inner_digest, sizeof(inner_digest));
OPENSSL_STATIC_ASSERT(HRSS_KEY_BYTES == SHA256_DIGEST_LENGTH,
"HRSS shared key length incorrect");
static_assert(HRSS_KEY_BYTES == SHA256_DIGEST_LENGTH,
"HRSS shared key length incorrect");
SHA256_Final(out_shared_key, &vars->hash_ctx);
// If the ciphertext is publicly invalid then a random shared key is still
@@ -2188,8 +2187,8 @@ int HRSS_decap(uint8_t out_shared_key[HRSS_KEY_BYTES],
// The |poly_marshal| here then is just confirming that |poly_unmarshal| is
// strict and could be omitted.
OPENSSL_STATIC_ASSERT(HRSS_CIPHERTEXT_BYTES == POLY_BYTES,
"ciphertext is the wrong size");
static_assert(HRSS_CIPHERTEXT_BYTES == POLY_BYTES,
"ciphertext is the wrong size");
assert(ciphertext_len == sizeof(vars->expected_ciphertext));
poly_marshal(vars->expected_ciphertext, &vars->c);
+2 -2
View File
@@ -72,8 +72,7 @@ OPENSSL_MSVC_PRAGMA(warning(pop))
#define OPENSSL_MALLOC_PREFIX 8
OPENSSL_STATIC_ASSERT(OPENSSL_MALLOC_PREFIX >= sizeof(size_t),
"size_t too large");
static_assert(OPENSSL_MALLOC_PREFIX >= sizeof(size_t), "size_t too large");
#if defined(OPENSSL_ASAN)
void __asan_poison_memory_region(const volatile void *addr, size_t size);
@@ -183,6 +182,7 @@ void OPENSSL_free(void *orig_ptr) {
// ASan knows to intercept malloc and free, but not sdallocx.
#if defined(OPENSSL_ASAN)
(void)sdallocx;
free(ptr);
#else
if (sdallocx) {
@@ -263,84 +263,3 @@ err:
OPENSSL_free(data);
return ret;
}
// A TJH addition
int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
unsigned char *kstr, int klen, pem_password_cb *cb,
void *u) {
int i, ret = 0;
unsigned char *data = NULL;
const char *objstr = NULL;
char buf[PEM_BUFSIZE];
unsigned char *iv = NULL;
unsigned iv_len = 0;
if (enc != NULL) {
iv_len = EVP_CIPHER_iv_length(enc);
objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
if (objstr == NULL) {
OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_CIPHER);
goto err;
}
}
// now for the fun part ... if we have a private key then we have to be
// able to handle a not-yet-decrypted key being written out correctly ...
// if it is decrypted or it is non-encrypted then we use the base code
if (xi->x_pkey != NULL) {
if ((xi->enc_data != NULL) && (xi->enc_len > 0)) {
if (enc == NULL) {
OPENSSL_PUT_ERROR(PEM, PEM_R_CIPHER_IS_NULL);
goto err;
}
// copy from weirdo names into more normal things
iv = xi->enc_cipher.iv;
data = (unsigned char *)xi->enc_data;
i = xi->enc_len;
// we take the encryption data from the internal stuff rather
// than what the user has passed us ... as we have to match
// exactly for some strange reason
objstr = OBJ_nid2sn(EVP_CIPHER_nid(xi->enc_cipher.cipher));
if (objstr == NULL) {
OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_CIPHER);
goto err;
}
// create the right magic header stuff
assert(strlen(objstr) + 23 + 2 * iv_len + 13 <= sizeof buf);
buf[0] = '\0';
PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
PEM_dek_info(buf, objstr, iv_len, (char *)iv);
// use the normal code to write things out
i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
if (i <= 0) {
goto err;
}
} else {
// Add DSA/DH
// normal optionally encrypted stuff
if (PEM_write_bio_RSAPrivateKey(bp, xi->x_pkey->dec_pkey->pkey.rsa, enc,
kstr, klen, cb, u) <= 0) {
goto err;
}
}
}
// if we have a certificate then write it out now
if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0)) {
goto err;
}
// we are ignoring anything else that is loaded into the X509_INFO
// structure for the moment ... as I don't need it so I'm not coding it
// here and Eric can do it when this makes it into the base library --tjh
ret = 1;
err:
OPENSSL_cleanse(buf, PEM_BUFSIZE);
return ret;
}
@@ -90,62 +90,16 @@ int pkcs12_iterations_acceptable(uint64_t iterations) {
return 0 < iterations && iterations <= kIterationsLimit;
}
// Minor tweak to operation: zero private key data
static int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
void *exarg) {
// Since the structure must still be valid use ASN1_OP_FREE_PRE
if (operation == ASN1_OP_FREE_PRE) {
PKCS8_PRIV_KEY_INFO *key = (PKCS8_PRIV_KEY_INFO *)*pval;
if (key->pkey) {
OPENSSL_cleanse(key->pkey->data, key->pkey->length);
}
}
return 1;
}
ASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = {
ASN1_SEQUENCE(PKCS8_PRIV_KEY_INFO) = {
ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER),
ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR),
ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_OCTET_STRING),
ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0),
} ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
} ASN1_SEQUENCE_END(PKCS8_PRIV_KEY_INFO)
IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
IMPLEMENT_ASN1_FUNCTIONS_const(PKCS8_PRIV_KEY_INFO)
int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj, int version,
int ptype, void *pval, uint8_t *penc, int penclen) {
if (version >= 0 &&
!ASN1_INTEGER_set(priv->version, version)) {
return 0;
}
if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval)) {
return 0;
}
if (penc != NULL) {
ASN1_STRING_set0(priv->pkey, penc, penclen);
}
return 1;
}
int PKCS8_pkey_get0(ASN1_OBJECT **ppkalg, const uint8_t **pk, int *ppklen,
X509_ALGOR **pa, PKCS8_PRIV_KEY_INFO *p8) {
if (ppkalg) {
*ppkalg = p8->pkeyalg->algorithm;
}
if (pk) {
*pk = ASN1_STRING_data(p8->pkey);
*ppklen = ASN1_STRING_length(p8->pkey);
}
if (pa) {
*pa = p8->pkeyalg;
}
return 1;
}
EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8) {
EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8) {
uint8_t *der = NULL;
int der_len = i2d_PKCS8_PRIV_KEY_INFO(p8, &der);
if (der_len < 0) {
@@ -166,7 +120,7 @@ EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8) {
return ret;
}
PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey) {
PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey) {
CBB cbb;
uint8_t *der = NULL;
size_t der_len;
@@ -18,6 +18,7 @@
#include <CNIOBoringSSL_poly1305.h>
#include <assert.h>
#include <string.h>
#include "internal.h"
@@ -48,7 +49,7 @@ struct poly1305_state_st {
uint8_t key[16];
};
OPENSSL_STATIC_ASSERT(
static_assert(
sizeof(struct poly1305_state_st) + 63 <= sizeof(poly1305_state),
"poly1305_state isn't large enough to hold aligned poly1305_state_st");
@@ -17,6 +17,7 @@
#include <CNIOBoringSSL_poly1305.h>
#include <assert.h>
#include <string.h>
#include "../internal.h"
@@ -183,7 +184,7 @@ struct poly1305_state_st {
uint8_t key[16];
};
OPENSSL_STATIC_ASSERT(
static_assert(
sizeof(struct poly1305_state_st) + 63 <= sizeof(poly1305_state),
"poly1305_state isn't large enough to hold aligned poly1305_state_st.");
@@ -20,6 +20,8 @@
#include <CNIOBoringSSL_poly1305.h>
#include <assert.h>
#include "../internal.h"
@@ -76,9 +78,10 @@ typedef struct poly1305_state_internal_t {
} poly1305_state_internal; /* 448 bytes total + 63 bytes for
alignment = 511 bytes raw */
OPENSSL_STATIC_ASSERT(
sizeof(struct poly1305_state_internal_t) + 63 <= sizeof(poly1305_state),
"poly1305_state isn't large enough to hold aligned poly1305_state_internal_t");
static_assert(sizeof(struct poly1305_state_internal_t) + 63 <=
sizeof(poly1305_state),
"poly1305_state isn't large enough to hold aligned "
"poly1305_state_internal_t");
static inline poly1305_state_internal *poly1305_aligned_state(
poly1305_state *state) {
@@ -12,7 +12,8 @@
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <CNIOBoringSSL_base.h>
#include <CNIOBoringSSL_ctrdrbg.h>
#include "../fipsmodule/rand/internal.h"
#if defined(BORINGSSL_FIPS)
@@ -20,15 +21,16 @@
// RAND_need_entropy is called by the FIPS module when it has blocked because of
// a lack of entropy. This signal is used as an indication to feed it more.
void RAND_need_entropy(size_t bytes_needed) {
uint8_t buf[CTR_DRBG_ENTROPY_LEN * BORINGSSL_FIPS_OVERREAD];
uint8_t buf[/* last_block size */ 16 +
CTR_DRBG_ENTROPY_LEN * BORINGSSL_FIPS_OVERREAD];
size_t todo = sizeof(buf);
if (todo > bytes_needed) {
todo = bytes_needed;
}
int used_cpu;
CRYPTO_get_seed_entropy(buf, todo, &used_cpu);
RAND_load_entropy(buf, todo, used_cpu);
int want_additional_input;
CRYPTO_get_seed_entropy(buf, todo, &want_additional_input);
RAND_load_entropy(buf, todo, want_additional_input);
}
#endif // FIPS
@@ -22,8 +22,6 @@
#include <stdatomic.h>
#include <stdlib.h>
#include <CNIOBoringSSL_type_check.h>
// See comment above the typedef of CRYPTO_refcount_t about these tests.
static_assert(alignof(CRYPTO_refcount_t) == alignof(_Atomic CRYPTO_refcount_t),
+3 -4
View File
@@ -14,15 +14,14 @@
#include "internal.h"
#include <assert.h>
#include <stdlib.h>
#include <CNIOBoringSSL_type_check.h>
#if !defined(OPENSSL_C11_ATOMIC)
OPENSSL_STATIC_ASSERT((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX,
"CRYPTO_REFCOUNT_MAX is incorrect");
static_assert((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX,
"CRYPTO_REFCOUNT_MAX is incorrect");
static struct CRYPTO_STATIC_MUTEX g_refcount_lock = CRYPTO_STATIC_MUTEX_INIT;
+16 -16
View File
@@ -67,7 +67,7 @@
// stack.
static const size_t kMinSize = 4;
_STACK *sk_new(stack_cmp_func comp) {
_STACK *sk_new(OPENSSL_sk_cmp_func comp) {
_STACK *ret = OPENSSL_malloc(sizeof(_STACK));
if (ret == NULL) {
return NULL;
@@ -131,8 +131,8 @@ void sk_free(_STACK *sk) {
OPENSSL_free(sk);
}
void sk_pop_free_ex(_STACK *sk, stack_call_free_func call_free_func,
stack_free_func free_func) {
void sk_pop_free_ex(_STACK *sk, OPENSSL_sk_call_free_func call_free_func,
OPENSSL_sk_free_func free_func) {
if (sk == NULL) {
return;
}
@@ -145,14 +145,14 @@ void sk_pop_free_ex(_STACK *sk, stack_call_free_func call_free_func,
sk_free(sk);
}
// Historically, |sk_pop_free| called the function as |stack_free_func|
// Historically, |sk_pop_free| called the function as |OPENSSL_sk_free_func|
// directly. This is undefined in C. Some callers called |sk_pop_free| directly,
// so we must maintain a compatibility version for now.
static void call_free_func_legacy(stack_free_func func, void *ptr) {
static void call_free_func_legacy(OPENSSL_sk_free_func func, void *ptr) {
func(ptr);
}
void sk_pop_free(_STACK *sk, stack_free_func free_func) {
void sk_pop_free(_STACK *sk, OPENSSL_sk_free_func free_func) {
sk_pop_free_ex(sk, call_free_func_legacy, free_func);
}
@@ -234,7 +234,7 @@ void *sk_delete_ptr(_STACK *sk, const void *p) {
}
int sk_find(const _STACK *sk, size_t *out_index, const void *p,
stack_call_cmp_func call_cmp_func) {
OPENSSL_sk_call_cmp_func call_cmp_func) {
if (sk == NULL) {
return 0;
}
@@ -356,8 +356,8 @@ err:
#if defined(_MSC_VER)
struct sort_compare_ctx {
stack_call_cmp_func call_cmp_func;
stack_cmp_func cmp_func;
OPENSSL_sk_call_cmp_func call_cmp_func;
OPENSSL_sk_cmp_func cmp_func;
};
static int sort_compare(void *ctx_v, const void *a, const void *b) {
@@ -366,7 +366,7 @@ static int sort_compare(void *ctx_v, const void *a, const void *b) {
}
#endif
void sk_sort(_STACK *sk, stack_call_cmp_func call_cmp_func) {
void sk_sort(_STACK *sk, OPENSSL_sk_call_cmp_func call_cmp_func) {
if (sk == NULL || sk->comp == NULL || sk->sorted) {
return;
}
@@ -402,8 +402,8 @@ int sk_is_sorted(const _STACK *sk) {
return sk->sorted;
}
stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp) {
stack_cmp_func old = sk->comp;
OPENSSL_sk_cmp_func sk_set_cmp_func(_STACK *sk, OPENSSL_sk_cmp_func comp) {
OPENSSL_sk_cmp_func old = sk->comp;
if (sk->comp != comp) {
sk->sorted = 0;
@@ -413,10 +413,10 @@ stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp) {
return old;
}
_STACK *sk_deep_copy(const _STACK *sk, stack_call_copy_func call_copy_func,
stack_copy_func copy_func,
stack_call_free_func call_free_func,
stack_free_func free_func) {
_STACK *sk_deep_copy(const _STACK *sk, OPENSSL_sk_call_copy_func call_copy_func,
OPENSSL_sk_copy_func copy_func,
OPENSSL_sk_call_free_func call_free_func,
OPENSSL_sk_free_func free_func) {
_STACK *ret = sk_dup(sk);
if (ret == NULL) {
return NULL;
@@ -16,18 +16,18 @@
#if defined(OPENSSL_PTHREADS)
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_type_check.h>
OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
"CRYPTO_MUTEX is too small");
OPENSSL_STATIC_ASSERT(alignof(CRYPTO_MUTEX) >= alignof(pthread_rwlock_t),
"CRYPTO_MUTEX has insufficient alignment");
static_assert(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
"CRYPTO_MUTEX is too small");
static_assert(alignof(CRYPTO_MUTEX) >= alignof(pthread_rwlock_t),
"CRYPTO_MUTEX has insufficient alignment");
void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
+5 -5
View File
@@ -20,17 +20,17 @@ OPENSSL_MSVC_PRAGMA(warning(push, 3))
#include <windows.h>
OPENSSL_MSVC_PRAGMA(warning(pop))
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <CNIOBoringSSL_mem.h>
#include <CNIOBoringSSL_type_check.h>
OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(SRWLOCK),
"CRYPTO_MUTEX is too small");
OPENSSL_STATIC_ASSERT(alignof(CRYPTO_MUTEX) >= alignof(SRWLOCK),
"CRYPTO_MUTEX has insufficient alignment");
static_assert(sizeof(CRYPTO_MUTEX) >= sizeof(SRWLOCK),
"CRYPTO_MUTEX is too small");
static_assert(alignof(CRYPTO_MUTEX) >= alignof(SRWLOCK),
"CRYPTO_MUTEX has insufficient alignment");
static BOOL CALLBACK call_once_init(INIT_ONCE *once, void *arg, void **out) {
void (**init)(void) = (void (**)(void))arg;
+11 -3
View File
@@ -72,12 +72,15 @@ extern "C" {
// Internal structures.
typedef struct X509_POLICY_CACHE_st X509_POLICY_CACHE;
typedef struct X509_POLICY_TREE_st X509_POLICY_TREE;
typedef struct X509_val_st {
ASN1_TIME *notBefore;
ASN1_TIME *notAfter;
} X509_VAL;
DECLARE_ASN1_FUNCTIONS(X509_VAL)
DECLARE_ASN1_FUNCTIONS_const(X509_VAL)
struct X509_pubkey_st {
X509_ALGOR *algor;
@@ -113,7 +116,7 @@ typedef struct x509_cert_aux_st {
ASN1_OCTET_STRING *keyid; // key id of private key
} X509_CERT_AUX;
DECLARE_ASN1_FUNCTIONS(X509_CERT_AUX)
DECLARE_ASN1_FUNCTIONS_const(X509_CERT_AUX)
struct X509_extension_st {
ASN1_OBJECT *object;
@@ -135,6 +138,8 @@ typedef struct {
ASN1_ENCODING enc;
} X509_CINF;
// TODO(https://crbug.com/boringssl/407): This is not const because it contains
// an |X509_NAME|.
DECLARE_ASN1_FUNCTIONS(X509_CINF)
struct x509_st {
@@ -171,13 +176,14 @@ typedef struct {
STACK_OF(X509_ATTRIBUTE) *attributes; // [ 0 ]
} X509_REQ_INFO;
// TODO(https://crbug.com/boringssl/407): This is not const because it contains
// an |X509_NAME|.
DECLARE_ASN1_FUNCTIONS(X509_REQ_INFO)
struct X509_req_st {
X509_REQ_INFO *req_info;
X509_ALGOR *sig_alg;
ASN1_BIT_STRING *signature;
CRYPTO_refcount_t references;
} /* X509_REQ */;
struct x509_revoked_st {
@@ -201,6 +207,8 @@ typedef struct {
ASN1_ENCODING enc;
} X509_CRL_INFO;
// TODO(https://crbug.com/boringssl/407): This is not const because it contains
// an |X509_NAME|.
DECLARE_ASN1_FUNCTIONS(X509_CRL_INFO)
struct X509_crl_st {
@@ -86,9 +86,6 @@ static int do_name_ex(BIO *out, const X509_NAME *n, int indent,
unsigned long flags) {
int i, prev = -1, orflags, cnt;
int fn_opt, fn_nid;
ASN1_OBJECT *fn;
ASN1_STRING *val;
X509_NAME_ENTRY *ent;
char objtmp[80];
const char *objbuf;
int outlen, len;
@@ -149,6 +146,7 @@ static int do_name_ex(BIO *out, const X509_NAME *n, int indent,
cnt = X509_NAME_entry_count(n);
for (i = 0; i < cnt; i++) {
const X509_NAME_ENTRY *ent;
if (flags & XN_FLAG_DN_REV) {
ent = X509_NAME_get_entry(n, cnt - i - 1);
} else {
@@ -172,8 +170,8 @@ static int do_name_ex(BIO *out, const X509_NAME *n, int indent,
}
}
prev = X509_NAME_ENTRY_set(ent);
fn = X509_NAME_ENTRY_get_object(ent);
val = X509_NAME_ENTRY_get_data(ent);
const ASN1_OBJECT *fn = X509_NAME_ENTRY_get_object(ent);
const ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent);
fn_nid = OBJ_obj2nid(fn);
if (fn_opt != XN_FLAG_FN_NONE) {
int objlen, fld_len;
+82 -87
View File
@@ -56,6 +56,7 @@
#include <CNIOBoringSSL_x509.h>
#include <assert.h>
#include <limits.h>
#include <CNIOBoringSSL_asn1.h>
#include <CNIOBoringSSL_asn1t.h>
@@ -83,13 +84,13 @@ ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = {
ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER, 3),
} ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS)
IMPLEMENT_ASN1_FUNCTIONS(RSA_PSS_PARAMS)
IMPLEMENT_ASN1_FUNCTIONS_const(RSA_PSS_PARAMS)
// Given an MGF1 Algorithm ID decode to an Algorithm Identifier
static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg) {
if (alg == NULL || alg->parameter == NULL ||
OBJ_obj2nid(alg->algorithm) != NID_mgf1 ||
static X509_ALGOR *rsa_mgf1_decode(const X509_ALGOR *alg) {
if (OBJ_obj2nid(alg->algorithm) != NID_mgf1 ||
alg->parameter == NULL ||
alg->parameter->type != V_ASN1_SEQUENCE) {
return NULL;
}
@@ -99,30 +100,27 @@ static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg) {
return d2i_X509_ALGOR(NULL, &p, plen);
}
static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg,
X509_ALGOR **pmaskHash) {
*pmaskHash = NULL;
static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg) {
if (alg->parameter == NULL || alg->parameter->type != V_ASN1_SEQUENCE) {
return NULL;
}
const uint8_t *p = alg->parameter->value.sequence->data;
int plen = alg->parameter->value.sequence->length;
RSA_PSS_PARAMS *pss = d2i_RSA_PSS_PARAMS(NULL, &p, plen);
if (pss == NULL) {
return NULL;
}
*pmaskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
return pss;
return d2i_RSA_PSS_PARAMS(NULL, &p, plen);
}
// allocate and set algorithm ID from EVP_MD, default SHA1
static int is_allowed_pss_md(const EVP_MD *md) {
int md_type = EVP_MD_type(md);
return md_type == NID_sha256 || md_type == NID_sha384 ||
md_type == NID_sha512;
}
// rsa_md_to_algor sets |*palg| to an |X509_ALGOR| describing the digest |md|,
// which must be an allowed PSS digest.
static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md) {
if (EVP_MD_type(md) == NID_sha1) {
return 1;
}
// SHA-1 should be omitted (DEFAULT), but we do not allow SHA-1.
assert(is_allowed_pss_md(md));
*palg = X509_ALGOR_new();
if (*palg == NULL) {
return 0;
@@ -131,15 +129,13 @@ static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md) {
return 1;
}
// Allocate and set MGF1 algorithm ID from EVP_MD
// rsa_md_to_mgf1 sets |*palg| to an |X509_ALGOR| describing MGF-1 with the
// digest |mgf1md|, which must be an allowed PSS digest.
static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md) {
// SHA-1 should be omitted (DEFAULT), but we do not allow SHA-1.
assert(is_allowed_pss_md(mgf1md));
X509_ALGOR *algtmp = NULL;
ASN1_STRING *stmp = NULL;
*palg = NULL;
if (EVP_MD_type(mgf1md) == NID_sha1) {
return 1;
}
// need to embed algorithm ID inside another
if (!rsa_md_to_algor(&algtmp, mgf1md) ||
!ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp)) {
@@ -162,37 +158,35 @@ err:
return 0;
}
// convert algorithm ID to EVP_MD, default SHA1
static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg) {
const EVP_MD *md;
static const EVP_MD *rsa_algor_to_md(const X509_ALGOR *alg) {
if (!alg) {
return EVP_sha1();
}
md = EVP_get_digestbyobj(alg->algorithm);
if (md == NULL) {
// If omitted, PSS defaults to SHA-1, which we do not allow.
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
return NULL;
}
const EVP_MD *md = EVP_get_digestbyobj(alg->algorithm);
if (md == NULL || !is_allowed_pss_md(md)) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
return NULL;
}
return md;
}
// convert MGF1 algorithm ID to EVP_MD, default SHA1
static const EVP_MD *rsa_mgf1_to_md(const X509_ALGOR *alg,
X509_ALGOR *maskHash) {
const EVP_MD *md;
static const EVP_MD *rsa_mgf1_to_md(const X509_ALGOR *alg) {
if (!alg) {
return EVP_sha1();
}
// Check mask and lookup mask hash algorithm
if (OBJ_obj2nid(alg->algorithm) != NID_mgf1 || maskHash == NULL) {
// If omitted, PSS defaults to MGF-1 with SHA-1, which we do not allow.
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
return NULL;
}
md = EVP_get_digestbyobj(maskHash->algorithm);
if (md == NULL) {
// Check mask and lookup mask hash algorithm.
X509_ALGOR *maskHash = rsa_mgf1_decode(alg);
if (maskHash == NULL) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
return NULL;
}
return md;
const EVP_MD *ret = rsa_algor_to_md(maskHash);
X509_ALGOR_free(maskHash);
return ret;
}
int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) {
@@ -204,18 +198,14 @@ int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) {
return 0;
}
EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(ctx->pctx);
if (sigmd != mgf1md || !is_allowed_pss_md(sigmd)) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
return 0;
}
int md_len = EVP_MD_size(sigmd);
if (saltlen == -1) {
saltlen = EVP_MD_size(sigmd);
} else if (saltlen == -2) {
// TODO(davidben): Forbid this mode. The world has largely standardized on
// salt length matching hash length.
saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0) {
saltlen--;
}
} else if (saltlen != (int)EVP_MD_size(sigmd)) {
// We only allow salt length matching hash length and, for now, the -2 case.
saltlen = md_len;
} else if (saltlen != md_len) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
return 0;
}
@@ -227,11 +217,12 @@ int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) {
goto err;
}
if (saltlen != 20) {
pss->saltLength = ASN1_INTEGER_new();
if (!pss->saltLength || !ASN1_INTEGER_set(pss->saltLength, saltlen)) {
goto err;
}
// The DEFAULT value is 20, but this does not match any supported digest.
assert(saltlen != 20);
pss->saltLength = ASN1_INTEGER_new();
if (!pss->saltLength || //
!ASN1_INTEGER_set(pss->saltLength, saltlen)) {
goto err;
}
if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd) ||
@@ -260,33 +251,38 @@ int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg,
// Decode PSS parameters
int ret = 0;
X509_ALGOR *maskHash;
RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg, &maskHash);
RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
if (pss == NULL) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
goto err;
}
const EVP_MD *mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm, maskHash);
const EVP_MD *mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm);
const EVP_MD *md = rsa_algor_to_md(pss->hashAlgorithm);
if (mgf1md == NULL || md == NULL) {
goto err;
}
int saltlen = 20;
if (pss->saltLength != NULL) {
saltlen = ASN1_INTEGER_get(pss->saltLength);
// Could perform more salt length sanity checks but the main
// RSA routines will trap other invalid values anyway.
if (saltlen < 0) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
goto err;
}
// We require the MGF-1 and signing hashes to match.
if (mgf1md != md) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
goto err;
}
// low-level routines support only trailer field 0xbc (value 1)
// and PKCS#1 says we should reject any other value anyway.
// We require the salt length be the hash length. The DEFAULT value is 20, but
// this does not match any supported salt length.
uint64_t salt_len = 0;
if (pss->saltLength == NULL ||
!ASN1_INTEGER_get_uint64(&salt_len, pss->saltLength) ||
salt_len != EVP_MD_size(md)) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
goto err;
}
assert(salt_len <= INT_MAX);
// The trailer field must be 1 (0xbc). This value is DEFAULT, so the structure
// is required to omit it in DER. Although a syntax error, we also tolerate an
// explicitly-encoded value. See the certificates in cl/362617931.
if (pss->trailerField != NULL && ASN1_INTEGER_get(pss->trailerField) != 1) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
goto err;
@@ -295,7 +291,7 @@ int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg,
EVP_PKEY_CTX *pctx;
if (!EVP_DigestVerifyInit(ctx, &pctx, md, NULL, pkey) ||
!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
!EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, saltlen) ||
!EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, (int)salt_len) ||
!EVP_PKEY_CTX_set_rsa_mgf1_md(pctx, mgf1md)) {
goto err;
}
@@ -304,7 +300,6 @@ int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg,
err:
RSA_PSS_PARAMS_free(pss);
X509_ALGOR_free(maskHash);
return ret;
}
@@ -313,8 +308,8 @@ int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent,
assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss);
int rv = 0;
X509_ALGOR *maskHash;
RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg, &maskHash);
X509_ALGOR *maskHash = NULL;
RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
if (!pss) {
if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0) {
goto err;
@@ -344,17 +339,17 @@ int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent,
}
if (pss->maskGenAlgorithm) {
if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0 ||
BIO_puts(bp, " with ") <= 0) {
goto err;
}
if (maskHash) {
if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0) {
maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
if (maskHash == NULL) {
if (BIO_puts(bp, "INVALID") <= 0) {
goto err;
}
} else {
if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0 ||
BIO_puts(bp, " with ") <= 0 ||
i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0) {
goto err;
}
} else if (BIO_puts(bp, "INVALID") <= 0) {
goto err;
}
} else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
goto err;
+6 -5
View File
@@ -105,7 +105,9 @@ int X509_REQ_print_ex(BIO *bio, X509_REQ *x, unsigned long nmflags,
}
if (!(cflag & X509_FLAG_NO_VERSION)) {
l = X509_REQ_get_version(x);
assert(l == X509_REQ_VERSION_1);
// Only zero, |X509_REQ_VERSION_1|, is valid but our parser accepts some
// invalid values for compatibility.
assert(0 <= l && l <= 2);
if (BIO_printf(bio, "%8sVersion: %ld (0x%lx)\n", "", l + 1,
(unsigned long)l) <= 0) {
goto err;
@@ -206,13 +208,12 @@ int X509_REQ_print_ex(BIO *bio, X509_REQ *x, unsigned long nmflags,
if (exts) {
BIO_printf(bio, "%8sRequested Extensions:\n", "");
size_t i;
for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
X509_EXTENSION *ex = sk_X509_EXTENSION_value(exts, i);
for (size_t i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
const X509_EXTENSION *ex = sk_X509_EXTENSION_value(exts, i);
if (BIO_printf(bio, "%12s", "") <= 0) {
goto err;
}
ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex);
const ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex);
i2a_ASN1_OBJECT(bio, obj);
const int is_critical = X509_EXTENSION_get_critical(ex);
if (BIO_printf(bio, ": %s\n", is_critical ? "critical" : "") <= 0) {
@@ -270,59 +270,6 @@ err:
return ret;
}
int X509_ocspid_print(BIO *bp, X509 *x) {
unsigned char *der = NULL;
unsigned char *dertmp;
int derlen;
int i;
unsigned char SHA1md[SHA_DIGEST_LENGTH];
// display the hash of the subject as it would appear in OCSP requests
if (BIO_printf(bp, " Subject OCSP hash: ") <= 0) {
goto err;
}
derlen = i2d_X509_NAME(x->cert_info->subject, NULL);
if ((der = dertmp = (unsigned char *)OPENSSL_malloc(derlen)) == NULL) {
goto err;
}
i2d_X509_NAME(x->cert_info->subject, &dertmp);
if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL)) {
goto err;
}
for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0) {
goto err;
}
}
OPENSSL_free(der);
der = NULL;
// display the hash of the public key as it would appear in OCSP requests
if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0) {
goto err;
}
if (!EVP_Digest(x->cert_info->key->public_key->data,
x->cert_info->key->public_key->length, SHA1md, NULL,
EVP_sha1(), NULL)) {
goto err;
}
for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0) {
goto err;
}
}
BIO_printf(bp, "\n");
return 1;
err:
if (der != NULL) {
OPENSSL_free(der);
}
return 0;
}
int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
const ASN1_STRING *sig) {
if (BIO_puts(bp, " Signature Algorithm: ") <= 0) {
@@ -305,127 +305,6 @@ int X509_check_private_key(X509 *x, const EVP_PKEY *k) {
return 0;
}
// Check a suite B algorithm is permitted: pass in a public key and the NID
// of its signature (or 0 if no signature). The pflags is a pointer to a
// flags field which must contain the suite B verification flags.
static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags) {
const EC_GROUP *grp = NULL;
int curve_nid;
if (pkey && pkey->type == EVP_PKEY_EC) {
grp = EC_KEY_get0_group(pkey->pkey.ec);
}
if (!grp) {
return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
}
curve_nid = EC_GROUP_get_curve_name(grp);
// Check curve is consistent with LOS
if (curve_nid == NID_secp384r1) { // P-384
// Check signature algorithm is consistent with curve.
if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384) {
return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
}
if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS)) {
return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
}
// If we encounter P-384 we cannot use P-256 later
*pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
} else if (curve_nid == NID_X9_62_prime256v1) { // P-256
if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256) {
return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
}
if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY)) {
return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
}
} else {
return X509_V_ERR_SUITE_B_INVALID_CURVE;
}
return X509_V_OK;
}
int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
unsigned long flags) {
int rv, sign_nid;
size_t i;
EVP_PKEY *pk = NULL;
unsigned long tflags;
if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) {
return X509_V_OK;
}
tflags = flags;
// If no EE certificate passed in must be first in chain
if (x == NULL) {
x = sk_X509_value(chain, 0);
i = 1;
} else {
i = 0;
}
if (X509_get_version(x) != X509_VERSION_3) {
rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
// Correct error depth
i = 0;
goto end;
}
pk = X509_get_pubkey(x);
// Check EE key only
rv = check_suite_b(pk, -1, &tflags);
if (rv != X509_V_OK) {
// Correct error depth
i = 0;
goto end;
}
for (; i < sk_X509_num(chain); i++) {
sign_nid = X509_get_signature_nid(x);
x = sk_X509_value(chain, i);
if (X509_get_version(x) != X509_VERSION_3) {
rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
goto end;
}
EVP_PKEY_free(pk);
pk = X509_get_pubkey(x);
rv = check_suite_b(pk, sign_nid, &tflags);
if (rv != X509_V_OK) {
goto end;
}
}
// Final check: root CA signature
rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
end:
if (pk) {
EVP_PKEY_free(pk);
}
if (rv != X509_V_OK) {
// Invalid signature or LOS errors are for previous cert
if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM ||
rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) &&
i) {
i--;
}
// If we have LOS error and flags changed then we are signing P-384
// with P-256. Use more meaninggul error.
if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags) {
rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
}
if (perror_depth) {
*perror_depth = i;
}
}
return rv;
}
int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) {
int sign_nid;
if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) {
return X509_V_OK;
}
sign_nid = OBJ_obj2nid(crl->crl->sig_alg->algorithm);
return check_suite_b(pk, sign_nid, &flags);
}
// Not strictly speaking an "up_ref" as a STACK doesn't have a reference
// count but it has the same effect by duping the STACK and upping the ref of
// each X509 structure.
+3 -3
View File
@@ -99,7 +99,7 @@ int X509_CRL_add1_ext_i2d(X509_CRL *x, int nid, void *value, int crit,
return X509V3_add1_i2d(&x->crl->extensions, nid, value, crit, flags);
}
int X509_CRL_add_ext(X509_CRL *x, X509_EXTENSION *ex, int loc) {
int X509_CRL_add_ext(X509_CRL *x, const X509_EXTENSION *ex, int loc) {
return (X509v3_add_ext(&(x->crl->extensions), ex, loc) != NULL);
}
@@ -127,7 +127,7 @@ X509_EXTENSION *X509_delete_ext(X509 *x, int loc) {
return (X509v3_delete_ext(x->cert_info->extensions, loc));
}
int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc) {
int X509_add_ext(X509 *x, const X509_EXTENSION *ex, int loc) {
return (X509v3_add_ext(&(x->cert_info->extensions), ex, loc) != NULL);
}
@@ -168,7 +168,7 @@ X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x, int loc) {
return (X509v3_delete_ext(x->extensions, loc));
}
int X509_REVOKED_add_ext(X509_REVOKED *x, X509_EXTENSION *ex, int loc) {
int X509_REVOKED_add_ext(X509_REVOKED *x, const X509_EXTENSION *ex, int loc) {
return (X509v3_add_ext(&(x->extensions), ex, loc) != NULL);
}
@@ -168,19 +168,6 @@ const char *X509_verify_cert_error_string(long err) {
case X509_V_ERR_CRL_PATH_VALIDATION_ERROR:
return "CRL path validation error";
case X509_V_ERR_SUITE_B_INVALID_VERSION:
return "Suite B: certificate version invalid";
case X509_V_ERR_SUITE_B_INVALID_ALGORITHM:
return "Suite B: invalid public key algorithm";
case X509_V_ERR_SUITE_B_INVALID_CURVE:
return "Suite B: invalid ECC curve";
case X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM:
return "Suite B: invalid signature algorithm";
case X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED:
return "Suite B: curve not allowed for this LOS";
case X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256:
return "Suite B: cannot sign P-384 with P-256";
case X509_V_ERR_HOSTNAME_MISMATCH:
return "Hostname mismatch";
case X509_V_ERR_EMAIL_MISMATCH:
+3 -3
View File
@@ -144,7 +144,7 @@ X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc) {
}
STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
X509_EXTENSION *ex, int loc) {
const X509_EXTENSION *ex, int loc) {
X509_EXTENSION *new_ex = NULL;
int n;
STACK_OF(X509_EXTENSION) *sk = NULL;
@@ -267,14 +267,14 @@ int X509_EXTENSION_set_data(X509_EXTENSION *ex, const ASN1_OCTET_STRING *data) {
return 1;
}
ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex) {
ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex) {
if (ex == NULL) {
return NULL;
}
return ex->object;
}
ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex) {
ASN1_OCTET_STRING *X509_EXTENSION_get_data(const X509_EXTENSION *ex) {
if (ex == NULL) {
return NULL;
}
+29 -98
View File
@@ -457,17 +457,6 @@ int X509_verify_cert(X509_STORE_CTX *ctx) {
goto end;
}
int err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
ctx->param->flags);
if (err != X509_V_OK) {
ctx->error = err;
ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
ok = ctx->verify_cb(0, ctx);
if (!ok) {
goto end;
}
}
// At this point, we have a chain and need to verify it
if (ctx->verify != NULL) {
ok = ctx->verify(ctx);
@@ -689,7 +678,7 @@ end:
}
static int reject_dns_name_in_common_name(X509 *x509) {
X509_NAME *name = X509_get_subject_name(x509);
const X509_NAME *name = X509_get_subject_name(x509);
int i = -1;
for (;;) {
i = X509_NAME_get_index_by_NID(name, NID_commonName, i);
@@ -697,8 +686,8 @@ static int reject_dns_name_in_common_name(X509 *x509) {
return X509_V_OK;
}
X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i);
ASN1_STRING *common_name = X509_NAME_ENTRY_get_data(entry);
const X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i);
const ASN1_STRING *common_name = X509_NAME_ENTRY_get_data(entry);
unsigned char *idval;
int idlen = ASN1_STRING_to_UTF8(&idval, common_name);
if (idlen < 0) {
@@ -982,18 +971,21 @@ err:
// Check CRL times against values in X509_STORE_CTX
static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) {
time_t *ptime;
int i;
if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) {
return 1;
}
if (notify) {
ctx->current_crl = crl;
}
time_t *ptime;
if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) {
ptime = &ctx->param->check_time;
} else {
ptime = NULL;
}
i = X509_cmp_time(X509_CRL_get0_lastUpdate(crl), ptime);
int i = X509_cmp_time(X509_CRL_get0_lastUpdate(crl), ptime);
if (i == 0) {
if (!notify) {
return 0;
@@ -1108,7 +1100,7 @@ static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
// both present or both absent. If both present all fields must be identical.
static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid) {
ASN1_OCTET_STRING *exta, *extb;
const ASN1_OCTET_STRING *exta, *extb;
int i;
i = X509_CRL_get_ext_by_NID(a, nid, -1);
if (i >= 0) {
@@ -1646,15 +1638,6 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) {
goto err;
}
} else {
int rv;
rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
if (rv != X509_V_OK) {
ctx->error = rv;
ok = ctx->verify_cb(0, ctx);
if (!ok) {
goto err;
}
}
// Verify CRL signature
if (X509_CRL_verify(crl, ikey) <= 0) {
ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
@@ -1710,6 +1693,9 @@ static int check_policy(X509_STORE_CTX *ctx) {
if (ctx->parent) {
return 1;
}
// TODO(davidben): Historically, outputs of the |X509_policy_check| were saved
// on |ctx| and accessible via the public API. This has since been removed, so
// remove the fields from |X509_STORE_CTX|.
ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
ctx->param->policies, ctx->param->flags);
if (ret == 0) {
@@ -1756,16 +1742,18 @@ static int check_policy(X509_STORE_CTX *ctx) {
}
static int check_cert_time(X509_STORE_CTX *ctx, X509 *x) {
time_t *ptime;
int i;
if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) {
return 1;
}
time_t *ptime;
if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) {
ptime = &ctx->param->check_time;
} else {
ptime = NULL;
}
i = X509_cmp_time(X509_get_notBefore(x), ptime);
int i = X509_cmp_time(X509_get_notBefore(x), ptime);
if (i == 0) {
ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
ctx->current_cert = x;
@@ -1889,66 +1877,13 @@ int X509_cmp_current_time(const ASN1_TIME *ctm) {
}
int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time) {
static const size_t utctime_length = sizeof("YYMMDDHHMMSSZ") - 1;
static const size_t generalizedtime_length = sizeof("YYYYMMDDHHMMSSZ") - 1;
ASN1_TIME *asn1_cmp_time = NULL;
int i, day, sec, ret = 0;
// Note that ASN.1 allows much more slack in the time format than RFC 5280.
// In RFC 5280, the representation is fixed:
// UTCTime: YYMMDDHHMMSSZ
// GeneralizedTime: YYYYMMDDHHMMSSZ
//
// We do NOT currently enforce the following RFC 5280 requirement:
// "CAs conforming to this profile MUST always encode certificate
// validity dates through the year 2049 as UTCTime; certificate validity
// dates in 2050 or later MUST be encoded as GeneralizedTime."
switch (ctm->type) {
case V_ASN1_UTCTIME:
if (ctm->length != (int)(utctime_length)) {
return 0;
}
break;
case V_ASN1_GENERALIZEDTIME:
if (ctm->length != (int)(generalizedtime_length)) {
return 0;
}
break;
default:
return 0;
}
//*
// Verify the format: the ASN.1 functions we use below allow a more
// flexible format than what's mandated by RFC 5280.
// Digit and date ranges will be verified in the conversion methods.
for (i = 0; i < ctm->length - 1; i++) {
if (!isdigit(ctm->data[i])) {
return 0;
}
}
if (ctm->data[ctm->length - 1] != 'Z') {
int64_t ctm_time;
if (!ASN1_TIME_to_posix(ctm, &ctm_time)) {
return 0;
}
// There is ASN1_UTCTIME_cmp_time_t but no
// ASN1_GENERALIZEDTIME_cmp_time_t or ASN1_TIME_cmp_time_t,
// so we go through ASN.1
asn1_cmp_time = X509_time_adj(NULL, 0, cmp_time);
if (asn1_cmp_time == NULL) {
goto err;
}
if (!ASN1_TIME_diff(&day, &sec, ctm, asn1_cmp_time)) {
goto err;
}
// X509_cmp_time comparison is <=.
int64_t compare_time = (cmp_time == NULL) ? time(NULL) : *cmp_time;
// The return value 0 is reserved for errors.
ret = (day >= 0 && sec >= 0) ? -1 : 1;
err:
ASN1_TIME_free(asn1_cmp_time);
return ret;
return (ctm_time - compare_time <= 0) ? -1 : 1;
}
ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec) {
@@ -2042,8 +1977,7 @@ X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer, EVP_PKEY *skey,
// number to correct value too.
for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
X509_EXTENSION *ext;
ext = X509_CRL_get_ext(newer, i);
const X509_EXTENSION *ext = X509_CRL_get_ext(newer, i);
if (!X509_CRL_add_ext(crl, ext, -1)) {
goto memerr;
}
@@ -2356,11 +2290,16 @@ err:
// Set alternative lookup method: just a STACK of trusted certificates. This
// avoids X509_STORE nastiness where it isn't needed.
void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) {
void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx,
STACK_OF(X509) *sk) {
ctx->other_ctx = sk;
ctx->get_issuer = get_issuer_sk;
}
void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) {
X509_STORE_CTX_set0_trusted_stack(ctx, sk);
}
void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) {
// We need to be idempotent because, unfortunately, |X509_STORE_CTX_free|
// also calls this function.
@@ -2406,14 +2345,6 @@ void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
ctx->verify_cb = verify_cb;
}
X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx) {
return ctx->tree;
}
int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx) {
return ctx->explicit_policy;
}
int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name) {
const X509_VERIFY_PARAM *param;
param = X509_VERIFY_PARAM_lookup(name);
+2 -5
View File
@@ -245,11 +245,8 @@ int i2d_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp) {
}
int X509_CRL_set1_signature_algo(X509_CRL *crl, const X509_ALGOR *algo) {
// TODO(https://crbug.com/boringssl/407): Generated ASN.1 dup functions
// should be const. Alternatively, when we can embed required fields
// directly in structs, import |X509_ALGOR_copy| from upstream.
X509_ALGOR *copy1 = X509_ALGOR_dup((X509_ALGOR *)algo);
X509_ALGOR *copy2 = X509_ALGOR_dup((X509_ALGOR *)algo);
X509_ALGOR *copy1 = X509_ALGOR_dup(algo);
X509_ALGOR *copy2 = X509_ALGOR_dup(algo);
if (copy1 == NULL || copy2 == NULL) {
X509_ALGOR_free(copy1);
X509_ALGOR_free(copy2);
+27 -36
View File
@@ -80,14 +80,12 @@ int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid, char *buf,
int X509_NAME_get_text_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
char *buf, int len) {
int i;
ASN1_STRING *data;
i = X509_NAME_get_index_by_OBJ(name, obj, -1);
int i = X509_NAME_get_index_by_OBJ(name, obj, -1);
if (i < 0) {
return -1;
}
data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
const ASN1_STRING *data =
X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
i = (data->length > (len - 1)) ? (len - 1) : data->length;
if (buf == NULL) {
return data->length;
@@ -148,51 +146,46 @@ X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc) {
}
X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc) {
X509_NAME_ENTRY *ret;
int i, n, set_prev, set_next;
STACK_OF(X509_NAME_ENTRY) *sk;
if (name == NULL || loc < 0 ||
sk_X509_NAME_ENTRY_num(name->entries) <= (size_t)loc) {
return NULL;
}
sk = name->entries;
ret = sk_X509_NAME_ENTRY_delete(sk, loc);
n = sk_X509_NAME_ENTRY_num(sk);
STACK_OF(X509_NAME_ENTRY) *sk = name->entries;
X509_NAME_ENTRY *ret = sk_X509_NAME_ENTRY_delete(sk, loc);
int n = sk_X509_NAME_ENTRY_num(sk);
name->modified = 1;
if (loc == n) {
return ret;
}
// else we need to fixup the set field
int set_prev;
if (loc != 0) {
set_prev = (sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
set_prev = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set;
} else {
set_prev = ret->set - 1;
}
set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set;
int set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set;
// set_prev is the previous set set is the current set set_next is the
// following prev 1 1 1 1 1 1 1 1 set 1 1 2 2 next 1 1 2 2 2 2 3 2 so
// basically only if prev and next differ by 2, then re-number down by 1
// If we removed a singleton RDN, update the RDN indices so they are
// consecutive again.
if (set_prev + 1 < set_next) {
for (i = loc; i < n; i++) {
for (int i = loc; i < n; i++) {
sk_X509_NAME_ENTRY_value(sk, i)->set--;
}
}
return ret;
}
int X509_NAME_add_entry_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int type,
const unsigned char *bytes, int len, int loc,
int set) {
X509_NAME_ENTRY *ne;
int ret;
ne = X509_NAME_ENTRY_create_by_OBJ(NULL, obj, type, bytes, len);
int X509_NAME_add_entry_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj,
int type, const unsigned char *bytes, int len,
int loc, int set) {
X509_NAME_ENTRY *ne =
X509_NAME_ENTRY_create_by_OBJ(NULL, obj, type, bytes, len);
if (!ne) {
return 0;
}
ret = X509_NAME_add_entry(name, ne, loc, set);
int ret = X509_NAME_add_entry(name, ne, loc, set);
X509_NAME_ENTRY_free(ne);
return ret;
}
@@ -200,13 +193,12 @@ int X509_NAME_add_entry_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int type,
int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
const unsigned char *bytes, int len, int loc,
int set) {
X509_NAME_ENTRY *ne;
int ret;
ne = X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
X509_NAME_ENTRY *ne =
X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
if (!ne) {
return 0;
}
ret = X509_NAME_add_entry(name, ne, loc, set);
int ret = X509_NAME_add_entry(name, ne, loc, set);
X509_NAME_ENTRY_free(ne);
return ret;
}
@@ -214,20 +206,19 @@ int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
const unsigned char *bytes, int len, int loc,
int set) {
X509_NAME_ENTRY *ne;
int ret;
ne = X509_NAME_ENTRY_create_by_txt(NULL, field, type, bytes, len);
X509_NAME_ENTRY *ne =
X509_NAME_ENTRY_create_by_txt(NULL, field, type, bytes, len);
if (!ne) {
return 0;
}
ret = X509_NAME_add_entry(name, ne, loc, set);
int ret = X509_NAME_add_entry(name, ne, loc, set);
X509_NAME_ENTRY_free(ne);
return ret;
}
// if set is -1, append to previous set, 0 'a new one', and 1, prepend to the
// guy we are about to stomp on.
int X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc,
int X509_NAME_add_entry(X509_NAME *name, const X509_NAME_ENTRY *entry, int loc,
int set) {
X509_NAME_ENTRY *new_name = NULL;
int n, i, inc;
@@ -267,7 +258,7 @@ int X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc,
}
}
if ((new_name = X509_NAME_ENTRY_dup(ne)) == NULL) {
if ((new_name = X509_NAME_ENTRY_dup(entry)) == NULL) {
goto err;
}
new_name->set = set;
+1 -4
View File
@@ -88,10 +88,7 @@ int X509_REQ_set_pubkey(X509_REQ *x, EVP_PKEY *pkey) {
}
int X509_REQ_set1_signature_algo(X509_REQ *req, const X509_ALGOR *algo) {
// TODO(https://crbug.com/boringssl/407): Generated ASN.1 dup functions
// should be const. Alternatively, when we can embed required fields
// directly in structs, import |X509_ALGOR_copy| from upstream.
X509_ALGOR *copy = X509_ALGOR_dup((X509_ALGOR *)algo);
X509_ALGOR *copy = X509_ALGOR_dup(algo);
if (copy == NULL) {
return 0;
}
+2 -8
View File
@@ -69,14 +69,8 @@ ASN1_SEQUENCE(X509_ALGOR) = {
ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY),
} ASN1_SEQUENCE_END(X509_ALGOR)
ASN1_ITEM_TEMPLATE(X509_ALGORS) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF,
0, algorithms,
X509_ALGOR)
ASN1_ITEM_TEMPLATE_END(X509_ALGORS)
IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR)
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS)
IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR)
IMPLEMENT_ASN1_FUNCTIONS_const(X509_ALGOR)
IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_ALGOR)
int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval) {
if (!alg) {
@@ -241,7 +241,6 @@ IMPLEMENT_I2D_BIO(RSA, i2d_RSAPublicKey_bio, i2d_RSAPublicKey)
IMPLEMENT_D2I_BIO(RSA, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY)
IMPLEMENT_I2D_BIO(RSA, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY)
#ifndef OPENSSL_NO_DSA
IMPLEMENT_D2I_FP(DSA, d2i_DSAPrivateKey_fp, d2i_DSAPrivateKey_bio)
IMPLEMENT_I2D_FP(DSA, i2d_DSAPrivateKey_fp, i2d_DSAPrivateKey_bio)
@@ -253,7 +252,6 @@ IMPLEMENT_I2D_BIO(DSA, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey)
IMPLEMENT_D2I_BIO(DSA, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY)
IMPLEMENT_I2D_BIO(DSA, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY)
#endif
IMPLEMENT_D2I_FP(EC_KEY, d2i_ECPrivateKey_fp, d2i_ECPrivateKey_bio)
IMPLEMENT_I2D_FP(EC_KEY, i2d_ECPrivateKey_fp, i2d_ECPrivateKey_bio)
+2 -2
View File
@@ -67,8 +67,8 @@ ASN1_SEQUENCE(X509_ATTRIBUTE) = {
ASN1_SET_OF(X509_ATTRIBUTE, set, ASN1_ANY),
} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
IMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
IMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
IMPLEMENT_ASN1_FUNCTIONS_const(X509_ATTRIBUTE)
IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_ATTRIBUTE)
X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int attrtype, void *value) {
ASN1_OBJECT *obj = OBJ_nid2obj(nid);
+5 -3
View File
@@ -348,9 +348,11 @@ ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING),
} ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
// Although |X509_REVOKED| contains an |X509_NAME|, it can be const. It is not
// affected by https://crbug.com/boringssl/407 because the |X509_NAME| does
// not participate in serialization.
IMPLEMENT_ASN1_FUNCTIONS_const(X509_REVOKED)
IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_REVOKED)
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
+4 -4
View File
@@ -72,7 +72,7 @@ ASN1_ITEM_TEMPLATE(X509_EXTENSIONS) =
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Extension, X509_EXTENSION)
ASN1_ITEM_TEMPLATE_END(X509_EXTENSIONS)
IMPLEMENT_ASN1_FUNCTIONS(X509_EXTENSION)
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_EXTENSIONS, X509_EXTENSIONS,
X509_EXTENSIONS)
IMPLEMENT_ASN1_DUP_FUNCTION(X509_EXTENSION)
IMPLEMENT_ASN1_FUNCTIONS_const(X509_EXTENSION)
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(X509_EXTENSIONS, X509_EXTENSIONS,
X509_EXTENSIONS)
IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_EXTENSION)
+2 -2
View File
@@ -99,8 +99,8 @@ ASN1_SEQUENCE(X509_NAME_ENTRY) = {
ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE),
} ASN1_SEQUENCE_END(X509_NAME_ENTRY)
IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
IMPLEMENT_ASN1_FUNCTIONS_const(X509_NAME_ENTRY)
IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_NAME_ENTRY)
// For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so
// declare two template wrappers for this
+1 -1
View File
@@ -85,7 +85,7 @@ ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING),
} ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
IMPLEMENT_ASN1_FUNCTIONS_const(X509_PUBKEY)
int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) {
X509_PUBKEY *pk = NULL;
+7 -3
View File
@@ -82,7 +82,11 @@ static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
}
if (operation == ASN1_OP_D2I_POST) {
if (ASN1_INTEGER_get(rinf->version) != X509_REQ_VERSION_1) {
// The only defined CSR version is v1(0). For compatibility, we also accept
// a hypothetical v3(2). Although not defined, older versions of certbot
// use it. See https://github.com/certbot/certbot/pull/9334.
long version = ASN1_INTEGER_get(rinf->version);
if (version != X509_REQ_VERSION_1 && version != 2) {
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
return 0;
}
@@ -101,11 +105,11 @@ ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = {
IMPLEMENT_ASN1_FUNCTIONS(X509_REQ_INFO)
ASN1_SEQUENCE_ref(X509_REQ, 0) = {
ASN1_SEQUENCE(X509_REQ) = {
ASN1_SIMPLE(X509_REQ, req_info, X509_REQ_INFO),
ASN1_SIMPLE(X509_REQ, sig_alg, X509_ALGOR),
ASN1_SIMPLE(X509_REQ, signature, ASN1_BIT_STRING),
} ASN1_SEQUENCE_END_ref(X509_REQ, X509_REQ)
} ASN1_SEQUENCE_END(X509_REQ)
IMPLEMENT_ASN1_FUNCTIONS(X509_REQ)
+1 -1
View File
@@ -71,7 +71,7 @@ ASN1_SEQUENCE(X509_SIG) = {
ASN1_SIMPLE(X509_SIG, digest, ASN1_OCTET_STRING),
} ASN1_SEQUENCE_END(X509_SIG)
IMPLEMENT_ASN1_FUNCTIONS(X509_SIG)
IMPLEMENT_ASN1_FUNCTIONS_const(X509_SIG)
void X509_SIG_get0(const X509_SIG *sig, const X509_ALGOR **out_alg,
const ASN1_OCTET_STRING **out_digest) {
+2 -2
View File
@@ -67,7 +67,7 @@ ASN1_SEQUENCE(NETSCAPE_SPKAC) = {
ASN1_SIMPLE(NETSCAPE_SPKAC, challenge, ASN1_IA5STRING),
} ASN1_SEQUENCE_END(NETSCAPE_SPKAC)
IMPLEMENT_ASN1_FUNCTIONS(NETSCAPE_SPKAC)
IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_SPKAC)
ASN1_SEQUENCE(NETSCAPE_SPKI) = {
ASN1_SIMPLE(NETSCAPE_SPKI, spkac, NETSCAPE_SPKAC),
@@ -75,4 +75,4 @@ ASN1_SEQUENCE(NETSCAPE_SPKI) = {
ASN1_SIMPLE(NETSCAPE_SPKI, signature, ASN1_BIT_STRING),
} ASN1_SEQUENCE_END(NETSCAPE_SPKI)
IMPLEMENT_ASN1_FUNCTIONS(NETSCAPE_SPKI)
IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_SPKI)
+1 -1
View File
@@ -68,4 +68,4 @@ ASN1_SEQUENCE(X509_VAL) = {
ASN1_SIMPLE(X509_VAL, notAfter, ASN1_TIME),
} ASN1_SEQUENCE_END(X509_VAL)
IMPLEMENT_ASN1_FUNCTIONS(X509_VAL)
IMPLEMENT_ASN1_FUNCTIONS_const(X509_VAL)
+2 -5
View File
@@ -338,11 +338,8 @@ int i2d_X509_tbs(X509 *x509, unsigned char **outp) {
}
int X509_set1_signature_algo(X509 *x509, const X509_ALGOR *algo) {
// TODO(https://crbug.com/boringssl/407): Generated ASN.1 dup functions
// should be const. Alternatively, when we can embed required fields
// directly in structs, import |X509_ALGOR_copy| from upstream.
X509_ALGOR *copy1 = X509_ALGOR_dup((X509_ALGOR *)algo);
X509_ALGOR *copy2 = X509_ALGOR_dup((X509_ALGOR *)algo);
X509_ALGOR *copy1 = X509_ALGOR_dup(algo);
X509_ALGOR *copy2 = X509_ALGOR_dup(algo);
if (copy1 == NULL || copy2 == NULL) {
X509_ALGOR_free(copy1);
X509_ALGOR_free(copy2);
Loaded 100 of 156 files, more files were not shown because too many files have changed in this diff. Show more