mirror of
https://github.com/apple/swift-crypto
synced 2026-11-28 05:04:21 +00:00
Update BoringSSL to cab31f65f1ad6e6daca62e95b25dd6cd805fce0b (#132)
This commit is contained in:
1 parent
e5dabe7f3a
commit
d89caf3006
233 files changed
+23466
-23296
No files matched your search
+1
-1
@@ -20,7 +20,7 @@
|
||||
// Sources/CCryptoBoringSSL directory. The source repository is at
|
||||
// https://boringssl.googlesource.com/boringssl.
|
||||
//
|
||||
// BoringSSL Commit: 1f51cfc7d7f54d2bee30bb5793470ef9c36a5083
|
||||
// BoringSSL Commit: cab31f65f1ad6e6daca62e95b25dd6cd805fce0b
|
||||
|
||||
import PackageDescription
|
||||
|
||||
|
||||
+1
-2
@@ -35,13 +35,13 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/asn1/asn_pack.c"
|
||||
"crypto/asn1/f_int.c"
|
||||
"crypto/asn1/f_string.c"
|
||||
"crypto/asn1/posix_time.c"
|
||||
"crypto/asn1/tasn_dec.c"
|
||||
"crypto/asn1/tasn_enc.c"
|
||||
"crypto/asn1/tasn_fre.c"
|
||||
"crypto/asn1/tasn_new.c"
|
||||
"crypto/asn1/tasn_typ.c"
|
||||
"crypto/asn1/tasn_utl.c"
|
||||
"crypto/asn1/time_support.c"
|
||||
"crypto/base64/base64.c"
|
||||
"crypto/bio/bio.c"
|
||||
"crypto/bio/bio_mem.c"
|
||||
@@ -289,7 +289,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/x509/x_x509a.c"
|
||||
"crypto/x509v3/pcy_cache.c"
|
||||
"crypto/x509v3/pcy_data.c"
|
||||
"crypto/x509v3/pcy_lib.c"
|
||||
"crypto/x509v3/pcy_map.c"
|
||||
"crypto/x509v3/pcy_node.c"
|
||||
"crypto/x509v3/pcy_tree.c"
|
||||
|
||||
@@ -66,219 +66,216 @@
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, int len)
|
||||
{
|
||||
return ASN1_STRING_set(x, d, len);
|
||||
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, int len) {
|
||||
return ASN1_STRING_set(x, d, len);
|
||||
}
|
||||
|
||||
int asn1_bit_string_length(const ASN1_BIT_STRING *str,
|
||||
uint8_t *out_padding_bits) {
|
||||
int len = str->length;
|
||||
if (str->flags & ASN1_STRING_FLAG_BITS_LEFT) {
|
||||
// If the string is already empty, it cannot have padding bits.
|
||||
*out_padding_bits = len == 0 ? 0 : str->flags & 0x07;
|
||||
return len;
|
||||
}
|
||||
|
||||
// TODO(https://crbug.com/boringssl/447): If we move this logic to
|
||||
// |ASN1_BIT_STRING_set_bit|, can we remove this representation?
|
||||
while (len > 0 && str->data[len - 1] == 0) {
|
||||
len--;
|
||||
}
|
||||
uint8_t padding_bits = 0;
|
||||
if (len > 0) {
|
||||
uint8_t last = str->data[len - 1];
|
||||
assert(last != 0);
|
||||
for (; padding_bits < 7; padding_bits++) {
|
||||
if (last & (1 << padding_bits)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*out_padding_bits = padding_bits;
|
||||
int len = str->length;
|
||||
if (str->flags & ASN1_STRING_FLAG_BITS_LEFT) {
|
||||
// If the string is already empty, it cannot have padding bits.
|
||||
*out_padding_bits = len == 0 ? 0 : str->flags & 0x07;
|
||||
return len;
|
||||
}
|
||||
|
||||
// TODO(https://crbug.com/boringssl/447): If we move this logic to
|
||||
// |ASN1_BIT_STRING_set_bit|, can we remove this representation?
|
||||
while (len > 0 && str->data[len - 1] == 0) {
|
||||
len--;
|
||||
}
|
||||
uint8_t padding_bits = 0;
|
||||
if (len > 0) {
|
||||
uint8_t last = str->data[len - 1];
|
||||
assert(last != 0);
|
||||
for (; padding_bits < 7; padding_bits++) {
|
||||
if (last & (1 << padding_bits)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*out_padding_bits = padding_bits;
|
||||
return len;
|
||||
}
|
||||
|
||||
int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, size_t *out) {
|
||||
uint8_t padding_bits;
|
||||
int len = asn1_bit_string_length(str, &padding_bits);
|
||||
if (padding_bits != 0) {
|
||||
return 0;
|
||||
}
|
||||
*out = len;
|
||||
return 1;
|
||||
uint8_t padding_bits;
|
||||
int len = asn1_bit_string_length(str, &padding_bits);
|
||||
if (padding_bits != 0) {
|
||||
return 0;
|
||||
}
|
||||
*out = len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp)
|
||||
{
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp) {
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t bits;
|
||||
int len = asn1_bit_string_length(a, &bits);
|
||||
int ret = 1 + len;
|
||||
if (pp == NULL) {
|
||||
return ret;
|
||||
}
|
||||
uint8_t bits;
|
||||
int len = asn1_bit_string_length(a, &bits);
|
||||
int ret = 1 + len;
|
||||
if (pp == NULL) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8_t *p = *pp;
|
||||
*(p++) = bits;
|
||||
OPENSSL_memcpy(p, a->data, len);
|
||||
if (len > 0) {
|
||||
p[len - 1] &= (0xff << bits);
|
||||
}
|
||||
p += len;
|
||||
*pp = p;
|
||||
return (ret);
|
||||
uint8_t *p = *pp;
|
||||
*(p++) = bits;
|
||||
OPENSSL_memcpy(p, a->data, len);
|
||||
if (len > 0) {
|
||||
p[len - 1] &= (0xff << bits);
|
||||
}
|
||||
p += len;
|
||||
*pp = p;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
|
||||
const unsigned char **pp, long len)
|
||||
{
|
||||
ASN1_BIT_STRING *ret = NULL;
|
||||
const unsigned char *p;
|
||||
unsigned char *s;
|
||||
int padding;
|
||||
const unsigned char **pp, long len) {
|
||||
ASN1_BIT_STRING *ret = NULL;
|
||||
const unsigned char *p;
|
||||
unsigned char *s;
|
||||
int padding;
|
||||
|
||||
if (len < 1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
|
||||
goto err;
|
||||
if (len < 1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (len > INT_MAX) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if ((a == NULL) || ((*a) == NULL)) {
|
||||
if ((ret = ASN1_BIT_STRING_new()) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret = (*a);
|
||||
}
|
||||
|
||||
if (len > INT_MAX) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
p = *pp;
|
||||
padding = *(p++);
|
||||
len--;
|
||||
if (padding > 7) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Unused bits in a BIT STRING must be zero.
|
||||
uint8_t padding_mask = (1 << padding) - 1;
|
||||
if (padding != 0 && (len < 1 || (p[len - 1] & padding_mask) != 0)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_PADDING);
|
||||
goto err;
|
||||
}
|
||||
|
||||
// We do this to preserve the settings. If we modify the settings, via
|
||||
// the _set_bit function, we will recalculate on output
|
||||
ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); // clear
|
||||
ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | padding); // set
|
||||
|
||||
if (len > 0) {
|
||||
s = OPENSSL_memdup(p, len);
|
||||
if (s == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
p += len;
|
||||
} else {
|
||||
s = NULL;
|
||||
}
|
||||
|
||||
if ((a == NULL) || ((*a) == NULL)) {
|
||||
if ((ret = ASN1_BIT_STRING_new()) == NULL)
|
||||
return (NULL);
|
||||
} else
|
||||
ret = (*a);
|
||||
ret->length = (int)len;
|
||||
OPENSSL_free(ret->data);
|
||||
ret->data = s;
|
||||
ret->type = V_ASN1_BIT_STRING;
|
||||
if (a != NULL) {
|
||||
(*a) = ret;
|
||||
}
|
||||
*pp = p;
|
||||
return ret;
|
||||
err:
|
||||
if ((ret != NULL) && ((a == NULL) || (*a != ret))) {
|
||||
ASN1_BIT_STRING_free(ret);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p = *pp;
|
||||
padding = *(p++);
|
||||
len--;
|
||||
if (padding > 7) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
|
||||
goto err;
|
||||
// These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
|
||||
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) {
|
||||
int w, v, iv;
|
||||
unsigned char *c;
|
||||
|
||||
w = n / 8;
|
||||
v = 1 << (7 - (n & 0x07));
|
||||
iv = ~v;
|
||||
if (!value) {
|
||||
v = 0;
|
||||
}
|
||||
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); // clear, set on write
|
||||
|
||||
if ((a->length < (w + 1)) || (a->data == NULL)) {
|
||||
if (!value) {
|
||||
return 1; // Don't need to set
|
||||
}
|
||||
|
||||
/* Unused bits in a BIT STRING must be zero. */
|
||||
uint8_t padding_mask = (1 << padding) - 1;
|
||||
if (padding != 0 &&
|
||||
(len < 1 || (p[len - 1] & padding_mask) != 0)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_PADDING);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* We do this to preserve the settings. If we modify the settings, via
|
||||
* the _set_bit function, we will recalculate on output
|
||||
*/
|
||||
ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
|
||||
ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | padding); /* set */
|
||||
|
||||
if (len > 0) {
|
||||
s = OPENSSL_memdup(p, len);
|
||||
if (s == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
p += len;
|
||||
if (a->data == NULL) {
|
||||
c = (unsigned char *)OPENSSL_malloc(w + 1);
|
||||
} else {
|
||||
s = NULL;
|
||||
c = (unsigned char *)OPENSSL_realloc(a->data, w + 1);
|
||||
}
|
||||
|
||||
ret->length = (int)len;
|
||||
OPENSSL_free(ret->data);
|
||||
ret->data = s;
|
||||
ret->type = V_ASN1_BIT_STRING;
|
||||
if (a != NULL)
|
||||
(*a) = ret;
|
||||
*pp = p;
|
||||
return (ret);
|
||||
err:
|
||||
if ((ret != NULL) && ((a == NULL) || (*a != ret)))
|
||||
ASN1_BIT_STRING_free(ret);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
|
||||
*/
|
||||
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
|
||||
{
|
||||
int w, v, iv;
|
||||
unsigned char *c;
|
||||
|
||||
w = n / 8;
|
||||
v = 1 << (7 - (n & 0x07));
|
||||
iv = ~v;
|
||||
if (!value)
|
||||
v = 0;
|
||||
|
||||
if (a == NULL)
|
||||
return 0;
|
||||
|
||||
a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
|
||||
|
||||
if ((a->length < (w + 1)) || (a->data == NULL)) {
|
||||
if (!value)
|
||||
return (1); /* Don't need to set */
|
||||
if (a->data == NULL)
|
||||
c = (unsigned char *)OPENSSL_malloc(w + 1);
|
||||
else
|
||||
c = (unsigned char *)OPENSSL_realloc(a->data, w + 1);
|
||||
if (c == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
if (w + 1 - a->length > 0)
|
||||
OPENSSL_memset(c + a->length, 0, w + 1 - a->length);
|
||||
a->data = c;
|
||||
a->length = w + 1;
|
||||
if (c == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
a->data[w] = ((a->data[w]) & iv) | v;
|
||||
while ((a->length > 0) && (a->data[a->length - 1] == 0))
|
||||
a->length--;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
|
||||
{
|
||||
int w, v;
|
||||
|
||||
w = n / 8;
|
||||
v = 1 << (7 - (n & 0x07));
|
||||
if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
|
||||
return (0);
|
||||
return ((a->data[w] & v) != 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks if the given bit string contains only bits specified by
|
||||
* the flags vector. Returns 0 if there is at least one bit set in 'a'
|
||||
* which is not specified in 'flags', 1 otherwise.
|
||||
* 'len' is the length of 'flags'.
|
||||
*/
|
||||
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
|
||||
const unsigned char *flags, int flags_len)
|
||||
{
|
||||
int i, ok;
|
||||
/* Check if there is one bit set at all. */
|
||||
if (!a || !a->data)
|
||||
return 1;
|
||||
|
||||
/*
|
||||
* Check each byte of the internal representation of the bit string.
|
||||
*/
|
||||
ok = 1;
|
||||
for (i = 0; i < a->length && ok; ++i) {
|
||||
unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
|
||||
/* We are done if there is an unneeded bit set. */
|
||||
ok = (a->data[i] & mask) == 0;
|
||||
if (w + 1 - a->length > 0) {
|
||||
OPENSSL_memset(c + a->length, 0, w + 1 - a->length);
|
||||
}
|
||||
return ok;
|
||||
a->data = c;
|
||||
a->length = w + 1;
|
||||
}
|
||||
a->data[w] = ((a->data[w]) & iv) | v;
|
||||
while ((a->length > 0) && (a->data[a->length - 1] == 0)) {
|
||||
a->length--;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n) {
|
||||
int w, v;
|
||||
|
||||
w = n / 8;
|
||||
v = 1 << (7 - (n & 0x07));
|
||||
if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) {
|
||||
return 0;
|
||||
}
|
||||
return ((a->data[w] & v) != 0);
|
||||
}
|
||||
|
||||
// Checks if the given bit string contains only bits specified by
|
||||
// the flags vector. Returns 0 if there is at least one bit set in 'a'
|
||||
// which is not specified in 'flags', 1 otherwise.
|
||||
// 'len' is the length of 'flags'.
|
||||
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, const unsigned char *flags,
|
||||
int flags_len) {
|
||||
int i, ok;
|
||||
// Check if there is one bit set at all.
|
||||
if (!a || !a->data) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check each byte of the internal representation of the bit string.
|
||||
ok = 1;
|
||||
for (i = 0; i < a->length && ok; ++i) {
|
||||
unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
|
||||
// We are done if there is an unneeded bit set.
|
||||
ok = (a->data[i] & mask) == 0;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
@@ -59,64 +59,62 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **pp)
|
||||
{
|
||||
int r;
|
||||
unsigned char *p, *allocated = NULL;
|
||||
int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **pp) {
|
||||
int r;
|
||||
unsigned char *p, *allocated = NULL;
|
||||
|
||||
r = ASN1_object_size(0, 1, V_ASN1_BOOLEAN);
|
||||
if (pp == NULL)
|
||||
return (r);
|
||||
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(r)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, 1, V_ASN1_BOOLEAN, V_ASN1_UNIVERSAL);
|
||||
*p = a ? 0xff : 0x00;
|
||||
|
||||
/*
|
||||
* If a new buffer was allocated, just return it back.
|
||||
* If not, return the incremented buffer pointer.
|
||||
*/
|
||||
*pp = allocated != NULL ? allocated : p + 1;
|
||||
r = ASN1_object_size(0, 1, V_ASN1_BOOLEAN);
|
||||
if (pp == NULL) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(r)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, 1, V_ASN1_BOOLEAN, V_ASN1_UNIVERSAL);
|
||||
*p = a ? 0xff : 0x00;
|
||||
|
||||
// If a new buffer was allocated, just return it back.
|
||||
// If not, return the incremented buffer pointer.
|
||||
*pp = allocated != NULL ? allocated : p + 1;
|
||||
return r;
|
||||
}
|
||||
|
||||
ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *a, const unsigned char **pp,
|
||||
long length) {
|
||||
const unsigned char *p = *pp;
|
||||
long len;
|
||||
int inf, tag, xclass;
|
||||
inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
return -1;
|
||||
}
|
||||
const unsigned char *p = *pp;
|
||||
long len;
|
||||
int inf, tag, xclass;
|
||||
inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return -1;
|
||||
}
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tag != V_ASN1_BOOLEAN || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_A_BOOLEAN);
|
||||
return -1;
|
||||
}
|
||||
if (tag != V_ASN1_BOOLEAN || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_A_BOOLEAN);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (len != 1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
ASN1_BOOLEAN ret = (ASN1_BOOLEAN)*(p++);
|
||||
if (a != NULL) {
|
||||
(*a) = ret;
|
||||
}
|
||||
*pp = p;
|
||||
return ret;
|
||||
if (len != 1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
ASN1_BOOLEAN ret = (ASN1_BOOLEAN) * (p++);
|
||||
if (a != NULL) {
|
||||
(*a) = ret;
|
||||
}
|
||||
*pp = p;
|
||||
return ret;
|
||||
}
|
||||
@@ -63,29 +63,27 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
|
||||
void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
|
||||
{
|
||||
uint8_t *data;
|
||||
size_t len;
|
||||
// Historically, this function did not impose a limit in OpenSSL and is used
|
||||
// to read CRLs, so we leave this without an external bound.
|
||||
if (!BIO_read_asn1(in, &data, &len, INT_MAX)) {
|
||||
return NULL;
|
||||
}
|
||||
const uint8_t *ptr = data;
|
||||
void *ret = ASN1_item_d2i(x, &ptr, len, it);
|
||||
OPENSSL_free(data);
|
||||
return ret;
|
||||
void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x) {
|
||||
uint8_t *data;
|
||||
size_t len;
|
||||
// Historically, this function did not impose a limit in OpenSSL and is used
|
||||
// to read CRLs, so we leave this without an external bound.
|
||||
if (!BIO_read_asn1(in, &data, &len, INT_MAX)) {
|
||||
return NULL;
|
||||
}
|
||||
const uint8_t *ptr = data;
|
||||
void *ret = ASN1_item_d2i(x, &ptr, len, it);
|
||||
OPENSSL_free(data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
|
||||
{
|
||||
BIO *b = BIO_new_fp(in, BIO_NOCLOSE);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_BUF_LIB);
|
||||
return NULL;
|
||||
}
|
||||
void *ret = ASN1_item_d2i_bio(it, b, x);
|
||||
BIO_free(b);
|
||||
return ret;
|
||||
void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x) {
|
||||
BIO *b = BIO_new_fp(in, BIO_NOCLOSE);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_BUF_LIB);
|
||||
return NULL;
|
||||
}
|
||||
void *ret = ASN1_item_d2i_bio(it, b, x);
|
||||
BIO_free(b);
|
||||
return ret;
|
||||
}
|
||||
@@ -59,29 +59,27 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
/*
|
||||
* ASN1_ITEM version of dup: this follows the model above except we don't
|
||||
* need to allocate the buffer. At some point this could be rewritten to
|
||||
* directly dup the underlying structure instead of doing and encode and
|
||||
* decode.
|
||||
*/
|
||||
void *ASN1_item_dup(const ASN1_ITEM *it, void *x)
|
||||
{
|
||||
unsigned char *b = NULL;
|
||||
const unsigned char *p;
|
||||
long i;
|
||||
void *ret;
|
||||
// ASN1_ITEM version of dup: this follows the model above except we don't
|
||||
// need to allocate the buffer. At some point this could be rewritten to
|
||||
// directly dup the underlying structure instead of doing and encode and
|
||||
// decode.
|
||||
void *ASN1_item_dup(const ASN1_ITEM *it, void *x) {
|
||||
unsigned char *b = NULL;
|
||||
const unsigned char *p;
|
||||
long i;
|
||||
void *ret;
|
||||
|
||||
if (x == NULL)
|
||||
return (NULL);
|
||||
if (x == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
i = ASN1_item_i2d(x, &b, it);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return (NULL);
|
||||
}
|
||||
p = b;
|
||||
ret = ASN1_item_d2i(NULL, &p, i, it);
|
||||
OPENSSL_free(b);
|
||||
return (ret);
|
||||
i = ASN1_item_i2d(x, &b, it);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
p = b;
|
||||
ret = ASN1_item_d2i(NULL, &p, i, it);
|
||||
OPENSSL_free(b);
|
||||
return ret;
|
||||
}
|
||||
@@ -64,99 +64,85 @@
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
|
||||
{
|
||||
if (d->type != V_ASN1_GENERALIZEDTIME) {
|
||||
return 0;
|
||||
}
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, d->data, (size_t)d->length);
|
||||
if (!CBS_parse_generalized_time(&cbs, tm, /*allow_timezone_offset=*/0)) {
|
||||
int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d) {
|
||||
if (d->type != V_ASN1_GENERALIZEDTIME) {
|
||||
return 0;
|
||||
}
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, d->data, (size_t)d->length);
|
||||
if (!CBS_parse_generalized_time(&cbs, tm, /*allow_timezone_offset=*/0)) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) {
|
||||
return asn1_generalizedtime_to_tm(NULL, d);
|
||||
}
|
||||
|
||||
int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) {
|
||||
ASN1_GENERALIZEDTIME t;
|
||||
|
||||
t.type = V_ASN1_GENERALIZEDTIME;
|
||||
t.length = strlen(str);
|
||||
t.data = (unsigned char *)str;
|
||||
if (ASN1_GENERALIZEDTIME_check(&t)) {
|
||||
if (s != NULL) {
|
||||
if (!ASN1_STRING_set((ASN1_STRING *)s, (unsigned char *)str, t.length)) {
|
||||
return 0;
|
||||
}
|
||||
s->type = V_ASN1_GENERALIZEDTIME;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
|
||||
{
|
||||
return asn1_generalizedtime_to_tm(NULL, d);
|
||||
}
|
||||
|
||||
int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
|
||||
{
|
||||
ASN1_GENERALIZEDTIME t;
|
||||
|
||||
t.type = V_ASN1_GENERALIZEDTIME;
|
||||
t.length = strlen(str);
|
||||
t.data = (unsigned char *)str;
|
||||
if (ASN1_GENERALIZEDTIME_check(&t)) {
|
||||
if (s != NULL) {
|
||||
if (!ASN1_STRING_set((ASN1_STRING *)s,
|
||||
(unsigned char *)str, t.length))
|
||||
return 0;
|
||||
s->type = V_ASN1_GENERALIZEDTIME;
|
||||
}
|
||||
return (1);
|
||||
} else
|
||||
return (0);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
|
||||
time_t t)
|
||||
{
|
||||
return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
|
||||
time_t t) {
|
||||
return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
|
||||
}
|
||||
|
||||
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)
|
||||
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 (ts->tm_year < 0 - 1900 || ts->tm_year > 9999 - 1900) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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:
|
||||
if (s == NULL)
|
||||
ASN1_GENERALIZEDTIME_free(tmps);
|
||||
long offset_sec) {
|
||||
struct tm data;
|
||||
if (!OPENSSL_gmtime(&t, &data)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (offset_day || offset_sec) {
|
||||
if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (data.tm_year < 0 - 1900 || data.tm_year > 9999 - 1900) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
int free_s = 0;
|
||||
if (s == NULL) {
|
||||
free_s = 1;
|
||||
s = ASN1_UTCTIME_new();
|
||||
if (s == 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;
|
||||
}
|
||||
@@ -61,28 +61,26 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
|
||||
int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *x)
|
||||
{
|
||||
BIO *b = BIO_new_fp(out, BIO_NOCLOSE);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_BUF_LIB);
|
||||
return 0;
|
||||
}
|
||||
int ret = ASN1_item_i2d_bio(it, b, x);
|
||||
BIO_free(b);
|
||||
return ret;
|
||||
int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *x) {
|
||||
BIO *b = BIO_new_fp(out, BIO_NOCLOSE);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_BUF_LIB);
|
||||
return 0;
|
||||
}
|
||||
int ret = ASN1_item_i2d_bio(it, b, x);
|
||||
BIO_free(b);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x)
|
||||
{
|
||||
unsigned char *b = NULL;
|
||||
int n = ASN1_item_i2d(x, &b, it);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x) {
|
||||
unsigned char *b = NULL;
|
||||
int n = ASN1_item_i2d(x, &b, it);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = BIO_write_all(out, b, n);
|
||||
OPENSSL_free(b);
|
||||
return ret;
|
||||
int ret = BIO_write_all(out, b, n);
|
||||
OPENSSL_free(b);
|
||||
return ret;
|
||||
}
|
||||
@@ -56,407 +56,408 @@
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
|
||||
{
|
||||
return ASN1_STRING_dup(x);
|
||||
ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x) {
|
||||
return ASN1_STRING_dup(x);
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
|
||||
{
|
||||
/* Compare signs. */
|
||||
int neg = x->type & V_ASN1_NEG;
|
||||
if (neg != (y->type & V_ASN1_NEG)) {
|
||||
return neg ? -1 : 1;
|
||||
}
|
||||
int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) {
|
||||
// Compare signs.
|
||||
int neg = x->type & V_ASN1_NEG;
|
||||
if (neg != (y->type & V_ASN1_NEG)) {
|
||||
return neg ? -1 : 1;
|
||||
}
|
||||
|
||||
int ret = ASN1_STRING_cmp(x, y);
|
||||
if (neg) {
|
||||
/* This could be |-ret|, but |ASN1_STRING_cmp| is not forbidden from
|
||||
* returning |INT_MIN|. */
|
||||
if (ret < 0) {
|
||||
return 1;
|
||||
} else if (ret > 0) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
int ret = ASN1_STRING_cmp(x, y);
|
||||
if (neg) {
|
||||
// This could be |-ret|, but |ASN1_STRING_cmp| is not forbidden from
|
||||
// returning |INT_MIN|.
|
||||
if (ret < 0) {
|
||||
return 1;
|
||||
} else if (ret > 0) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* negate_twos_complement negates |len| bytes from |buf| in-place, interpreted
|
||||
* as a signed, big-endian two's complement value. */
|
||||
static void negate_twos_complement(uint8_t *buf, size_t len)
|
||||
{
|
||||
uint8_t borrow = 0;
|
||||
for (size_t i = len - 1; i < len; i--) {
|
||||
uint8_t t = buf[i];
|
||||
buf[i] = 0u - borrow - t;
|
||||
borrow |= t != 0;
|
||||
}
|
||||
// negate_twos_complement negates |len| bytes from |buf| in-place, interpreted
|
||||
// as a signed, big-endian two's complement value.
|
||||
static void negate_twos_complement(uint8_t *buf, size_t len) {
|
||||
uint8_t borrow = 0;
|
||||
for (size_t i = len - 1; i < len; i--) {
|
||||
uint8_t t = buf[i];
|
||||
buf[i] = 0u - borrow - t;
|
||||
borrow |= t != 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int is_all_zeros(const uint8_t *in, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (in[i] != 0) {
|
||||
return 0;
|
||||
}
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (in[i] != 0) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp)
|
||||
{
|
||||
if (in == NULL) {
|
||||
return 0;
|
||||
}
|
||||
int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) {
|
||||
if (in == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* |ASN1_INTEGER|s should be represented minimally, but it is possible to
|
||||
* construct invalid ones. Skip leading zeros so this does not produce an
|
||||
* invalid encoding or break invariants. */
|
||||
int start = 0;
|
||||
while (start < in->length && in->data[start] == 0) {
|
||||
start++;
|
||||
}
|
||||
// |ASN1_INTEGER|s should be represented minimally, but it is possible to
|
||||
// construct invalid ones. Skip leading zeros so this does not produce an
|
||||
// invalid encoding or break invariants.
|
||||
int start = 0;
|
||||
while (start < in->length && in->data[start] == 0) {
|
||||
start++;
|
||||
}
|
||||
|
||||
int is_negative = (in->type & V_ASN1_NEG) != 0;
|
||||
int pad;
|
||||
if (start >= in->length) {
|
||||
/* Zero is represented as a single byte. */
|
||||
is_negative = 0;
|
||||
pad = 1;
|
||||
} else if (is_negative) {
|
||||
/* 0x80...01 through 0xff...ff have a two's complement of 0x7f...ff
|
||||
* through 0x00...01 and need an extra byte to be negative.
|
||||
* 0x01...00 through 0x80...00 have a two's complement of 0xfe...ff
|
||||
* through 0x80...00 and can be negated as-is. */
|
||||
pad = in->data[start] > 0x80 ||
|
||||
(in->data[start] == 0x80 &&
|
||||
!is_all_zeros(in->data + start + 1, in->length - start - 1));
|
||||
} else {
|
||||
/* If the high bit is set, the signed representation needs an extra
|
||||
* byte to be positive. */
|
||||
pad = (in->data[start] & 0x80) != 0;
|
||||
}
|
||||
int is_negative = (in->type & V_ASN1_NEG) != 0;
|
||||
int pad;
|
||||
if (start >= in->length) {
|
||||
// Zero is represented as a single byte.
|
||||
is_negative = 0;
|
||||
pad = 1;
|
||||
} else if (is_negative) {
|
||||
// 0x80...01 through 0xff...ff have a two's complement of 0x7f...ff
|
||||
// through 0x00...01 and need an extra byte to be negative.
|
||||
// 0x01...00 through 0x80...00 have a two's complement of 0xfe...ff
|
||||
// through 0x80...00 and can be negated as-is.
|
||||
pad = in->data[start] > 0x80 ||
|
||||
(in->data[start] == 0x80 &&
|
||||
!is_all_zeros(in->data + start + 1, in->length - start - 1));
|
||||
} else {
|
||||
// If the high bit is set, the signed representation needs an extra
|
||||
// byte to be positive.
|
||||
pad = (in->data[start] & 0x80) != 0;
|
||||
}
|
||||
|
||||
if (in->length - start > INT_MAX - pad) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
int len = pad + in->length - start;
|
||||
assert(len > 0);
|
||||
if (outp == NULL) {
|
||||
return len;
|
||||
}
|
||||
|
||||
if (pad) {
|
||||
(*outp)[0] = 0;
|
||||
}
|
||||
OPENSSL_memcpy(*outp + pad, in->data + start, in->length - start);
|
||||
if (is_negative) {
|
||||
negate_twos_complement(*outp, len);
|
||||
assert((*outp)[0] >= 0x80);
|
||||
} else {
|
||||
assert((*outp)[0] < 0x80);
|
||||
}
|
||||
*outp += len;
|
||||
if (in->length - start > INT_MAX - pad) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
int len = pad + in->length - start;
|
||||
assert(len > 0);
|
||||
if (outp == NULL) {
|
||||
return len;
|
||||
}
|
||||
|
||||
if (pad) {
|
||||
(*outp)[0] = 0;
|
||||
}
|
||||
OPENSSL_memcpy(*outp + pad, in->data + start, in->length - start);
|
||||
if (is_negative) {
|
||||
negate_twos_complement(*outp, len);
|
||||
assert((*outp)[0] >= 0x80);
|
||||
} else {
|
||||
assert((*outp)[0] < 0x80);
|
||||
}
|
||||
*outp += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **out, const unsigned char **inp,
|
||||
long len)
|
||||
{
|
||||
/*
|
||||
* This function can handle lengths up to INT_MAX - 1, but the rest of the
|
||||
* legacy ASN.1 code mixes integer types, so avoid exposing it to
|
||||
* ASN1_INTEGERS with larger lengths.
|
||||
*/
|
||||
if (len < 0 || len > INT_MAX / 2) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
int is_negative;
|
||||
if (!CBS_is_valid_asn1_integer(&cbs, &is_negative)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ASN1_INTEGER *ret = NULL;
|
||||
if (out == NULL || *out == NULL) {
|
||||
ret = ASN1_INTEGER_new();
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret = *out;
|
||||
}
|
||||
|
||||
/* Convert to |ASN1_INTEGER|'s sign-and-magnitude representation. First,
|
||||
* determine the size needed for a minimal result. */
|
||||
if (is_negative) {
|
||||
/* 0xff00...01 through 0xff7f..ff have a two's complement of 0x00ff...ff
|
||||
* through 0x000100...001 and need one leading zero removed. 0x8000...00
|
||||
* through 0xff00...00 have a two's complement of 0x8000...00 through
|
||||
* 0x0100...00 and will be minimally-encoded as-is. */
|
||||
if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0xff &&
|
||||
!is_all_zeros(CBS_data(&cbs) + 1, CBS_len(&cbs) - 1)) {
|
||||
CBS_skip(&cbs, 1);
|
||||
}
|
||||
} else {
|
||||
/* Remove the leading zero byte, if any. */
|
||||
if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0x00) {
|
||||
CBS_skip(&cbs, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!ASN1_STRING_set(ret, CBS_data(&cbs), CBS_len(&cbs))) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (is_negative) {
|
||||
ret->type = V_ASN1_NEG_INTEGER;
|
||||
negate_twos_complement(ret->data, ret->length);
|
||||
} else {
|
||||
ret->type = V_ASN1_INTEGER;
|
||||
}
|
||||
|
||||
/* The value should be minimally-encoded. */
|
||||
assert(ret->length == 0 || ret->data[0] != 0);
|
||||
/* Zero is not negative. */
|
||||
assert(!is_negative || ret->length > 0);
|
||||
|
||||
*inp += len;
|
||||
if (out != NULL) {
|
||||
*out = ret;
|
||||
}
|
||||
return ret;
|
||||
|
||||
err:
|
||||
if (ret != NULL && (out == NULL || *out != ret)) {
|
||||
ASN1_INTEGER_free(ret);
|
||||
}
|
||||
long len) {
|
||||
// This function can handle lengths up to INT_MAX - 1, but the rest of the
|
||||
// legacy ASN.1 code mixes integer types, so avoid exposing it to
|
||||
// ASN1_INTEGERS with larger lengths.
|
||||
if (len < 0 || len > INT_MAX / 2) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
int is_negative;
|
||||
if (!CBS_is_valid_asn1_integer(&cbs, &is_negative)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ASN1_INTEGER *ret = NULL;
|
||||
if (out == NULL || *out == NULL) {
|
||||
ret = ASN1_INTEGER_new();
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret = *out;
|
||||
}
|
||||
|
||||
// Convert to |ASN1_INTEGER|'s sign-and-magnitude representation. First,
|
||||
// determine the size needed for a minimal result.
|
||||
if (is_negative) {
|
||||
// 0xff00...01 through 0xff7f..ff have a two's complement of 0x00ff...ff
|
||||
// through 0x000100...001 and need one leading zero removed. 0x8000...00
|
||||
// through 0xff00...00 have a two's complement of 0x8000...00 through
|
||||
// 0x0100...00 and will be minimally-encoded as-is.
|
||||
if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0xff &&
|
||||
!is_all_zeros(CBS_data(&cbs) + 1, CBS_len(&cbs) - 1)) {
|
||||
CBS_skip(&cbs, 1);
|
||||
}
|
||||
} else {
|
||||
// Remove the leading zero byte, if any.
|
||||
if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0x00) {
|
||||
CBS_skip(&cbs, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!ASN1_STRING_set(ret, CBS_data(&cbs), CBS_len(&cbs))) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (is_negative) {
|
||||
ret->type = V_ASN1_NEG_INTEGER;
|
||||
negate_twos_complement(ret->data, ret->length);
|
||||
} else {
|
||||
ret->type = V_ASN1_INTEGER;
|
||||
}
|
||||
|
||||
// The value should be minimally-encoded.
|
||||
assert(ret->length == 0 || ret->data[0] != 0);
|
||||
// Zero is not negative.
|
||||
assert(!is_negative || ret->length > 0);
|
||||
|
||||
*inp += len;
|
||||
if (out != NULL) {
|
||||
*out = ret;
|
||||
}
|
||||
return ret;
|
||||
|
||||
err:
|
||||
if (ret != NULL && (out == NULL || *out != ret)) {
|
||||
ASN1_INTEGER_free(ret);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
|
||||
{
|
||||
if (v >= 0) {
|
||||
return ASN1_INTEGER_set_uint64(a, (uint64_t) v);
|
||||
}
|
||||
int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t v) {
|
||||
if (v >= 0) {
|
||||
return ASN1_INTEGER_set_uint64(a, (uint64_t)v);
|
||||
}
|
||||
|
||||
if (!ASN1_INTEGER_set_uint64(a, 0 - (uint64_t) v)) {
|
||||
return 0;
|
||||
}
|
||||
if (!ASN1_INTEGER_set_uint64(a, 0 - (uint64_t)v)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
a->type = V_ASN1_NEG_INTEGER;
|
||||
return 1;
|
||||
a->type = V_ASN1_NEG_INTEGER;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
|
||||
{
|
||||
if (v >= 0) {
|
||||
return ASN1_ENUMERATED_set_uint64(a, (uint64_t) v);
|
||||
}
|
||||
int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t v) {
|
||||
if (v >= 0) {
|
||||
return ASN1_ENUMERATED_set_uint64(a, (uint64_t)v);
|
||||
}
|
||||
|
||||
if (!ASN1_ENUMERATED_set_uint64(a, 0 - (uint64_t) v)) {
|
||||
return 0;
|
||||
}
|
||||
if (!ASN1_ENUMERATED_set_uint64(a, 0 - (uint64_t)v)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
a->type = V_ASN1_NEG_ENUMERATED;
|
||||
return 1;
|
||||
a->type = V_ASN1_NEG_ENUMERATED;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int asn1_string_set_uint64(ASN1_STRING *out, uint64_t v, int type)
|
||||
{
|
||||
uint8_t buf[sizeof(uint64_t)];
|
||||
CRYPTO_store_u64_be(buf, v);
|
||||
size_t leading_zeros;
|
||||
for (leading_zeros = 0; leading_zeros < sizeof(buf); leading_zeros++) {
|
||||
if (buf[leading_zeros] != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ASN1_STRING_set(out, buf + leading_zeros,
|
||||
sizeof(buf) - leading_zeros)) {
|
||||
return 0;
|
||||
}
|
||||
out->type = type;
|
||||
return 1;
|
||||
int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) {
|
||||
static_assert(sizeof(long) <= sizeof(int64_t), "long fits in int64_t");
|
||||
return ASN1_INTEGER_set_int64(a, v);
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v)
|
||||
{
|
||||
return asn1_string_set_uint64(out, v, V_ASN1_INTEGER);
|
||||
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) {
|
||||
static_assert(sizeof(long) <= sizeof(int64_t), "long fits in int64_t");
|
||||
return ASN1_ENUMERATED_set_int64(a, v);
|
||||
}
|
||||
|
||||
int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v)
|
||||
{
|
||||
return asn1_string_set_uint64(out, v, V_ASN1_ENUMERATED);
|
||||
static int asn1_string_set_uint64(ASN1_STRING *out, uint64_t v, int type) {
|
||||
uint8_t buf[sizeof(uint64_t)];
|
||||
CRYPTO_store_u64_be(buf, v);
|
||||
size_t leading_zeros;
|
||||
for (leading_zeros = 0; leading_zeros < sizeof(buf); leading_zeros++) {
|
||||
if (buf[leading_zeros] != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ASN1_STRING_set(out, buf + leading_zeros, sizeof(buf) - leading_zeros)) {
|
||||
return 0;
|
||||
}
|
||||
out->type = type;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v) {
|
||||
return asn1_string_set_uint64(out, v, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v) {
|
||||
return asn1_string_set_uint64(out, v, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
static int asn1_string_get_abs_uint64(uint64_t *out, const ASN1_STRING *a,
|
||||
int type)
|
||||
{
|
||||
if ((a->type & ~V_ASN1_NEG) != type) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE);
|
||||
return 0;
|
||||
}
|
||||
uint8_t buf[sizeof(uint64_t)] = {0};
|
||||
if (a->length > (int)sizeof(buf)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_memcpy(buf + sizeof(buf) - a->length, a->data, a->length);
|
||||
*out = CRYPTO_load_u64_be(buf);
|
||||
return 1;
|
||||
int type) {
|
||||
if ((a->type & ~V_ASN1_NEG) != type) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE);
|
||||
return 0;
|
||||
}
|
||||
uint8_t buf[sizeof(uint64_t)] = {0};
|
||||
if (a->length > (int)sizeof(buf)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_memcpy(buf + sizeof(buf) - a->length, a->data, a->length);
|
||||
*out = CRYPTO_load_u64_be(buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int asn1_string_get_uint64(uint64_t *out, const ASN1_STRING *a, int type)
|
||||
{
|
||||
if (!asn1_string_get_abs_uint64(out, a, type)) {
|
||||
return 0;
|
||||
}
|
||||
if (a->type & V_ASN1_NEG) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
static int asn1_string_get_uint64(uint64_t *out, const ASN1_STRING *a,
|
||||
int type) {
|
||||
if (!asn1_string_get_abs_uint64(out, a, type)) {
|
||||
return 0;
|
||||
}
|
||||
if (a->type & V_ASN1_NEG) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_get_uint64(uint64_t *out, const ASN1_INTEGER *a)
|
||||
{
|
||||
return asn1_string_get_uint64(out, a, V_ASN1_INTEGER);
|
||||
int ASN1_INTEGER_get_uint64(uint64_t *out, const ASN1_INTEGER *a) {
|
||||
return asn1_string_get_uint64(out, a, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
int ASN1_ENUMERATED_get_uint64(uint64_t *out, const ASN1_ENUMERATED *a)
|
||||
{
|
||||
return asn1_string_get_uint64(out, a, V_ASN1_ENUMERATED);
|
||||
int ASN1_ENUMERATED_get_uint64(uint64_t *out, const ASN1_ENUMERATED *a) {
|
||||
return asn1_string_get_uint64(out, a, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
static long asn1_string_get_long(const ASN1_STRING *a, int type)
|
||||
{
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
static int asn1_string_get_int64(int64_t *out, const ASN1_STRING *a, int type) {
|
||||
uint64_t v;
|
||||
if (!asn1_string_get_abs_uint64(&v, a, type)) {
|
||||
return 0;
|
||||
}
|
||||
int64_t i64;
|
||||
int fits_in_i64;
|
||||
// Check |v != 0| to handle manually-constructed negative zeros.
|
||||
if ((a->type & V_ASN1_NEG) && v != 0) {
|
||||
i64 = (int64_t)(0u - v);
|
||||
fits_in_i64 = i64 < 0;
|
||||
} else {
|
||||
i64 = (int64_t)v;
|
||||
fits_in_i64 = i64 >= 0;
|
||||
}
|
||||
if (!fits_in_i64) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return 0;
|
||||
}
|
||||
*out = i64;
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint64_t v;
|
||||
if (!asn1_string_get_abs_uint64(&v, a, type)) {
|
||||
goto err;
|
||||
}
|
||||
int ASN1_INTEGER_get_int64(int64_t *out, const ASN1_INTEGER *a) {
|
||||
return asn1_string_get_int64(out, a, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
int64_t i64;
|
||||
int fits_in_i64;
|
||||
/* Check |v != 0| to handle manually-constructed negative zeros. */
|
||||
if ((a->type & V_ASN1_NEG) && v != 0) {
|
||||
i64 = (int64_t)(0u - v);
|
||||
fits_in_i64 = i64 < 0;
|
||||
} else {
|
||||
i64 = (int64_t)v;
|
||||
fits_in_i64 = i64 >= 0;
|
||||
}
|
||||
OPENSSL_STATIC_ASSERT(sizeof(long) <= sizeof(int64_t), "long is too big");
|
||||
int ASN1_ENUMERATED_get_int64(int64_t *out, const ASN1_ENUMERATED *a) {
|
||||
return asn1_string_get_int64(out, a, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
if (fits_in_i64 && LONG_MIN <= i64 && i64 <= LONG_MAX) {
|
||||
return (long)i64;
|
||||
}
|
||||
static long asn1_string_get_long(const ASN1_STRING *a, int type) {
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err:
|
||||
/* This function's return value does not distinguish overflow from -1. */
|
||||
int64_t v;
|
||||
if (!asn1_string_get_int64(&v, a, type) || //
|
||||
v < LONG_MIN || v > LONG_MAX) {
|
||||
// This function's return value does not distinguish overflow from -1.
|
||||
ERR_clear_error();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
long ASN1_INTEGER_get(const ASN1_INTEGER *a)
|
||||
{
|
||||
return asn1_string_get_long(a, V_ASN1_INTEGER);
|
||||
long ASN1_INTEGER_get(const ASN1_INTEGER *a) {
|
||||
return asn1_string_get_long(a, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
|
||||
{
|
||||
return asn1_string_get_long(a, V_ASN1_ENUMERATED);
|
||||
long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a) {
|
||||
return asn1_string_get_long(a, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
|
||||
int type)
|
||||
{
|
||||
ASN1_INTEGER *ret;
|
||||
if (ai == NULL) {
|
||||
ret = ASN1_STRING_type_new(type);
|
||||
} else {
|
||||
ret = ai;
|
||||
}
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
}
|
||||
int type) {
|
||||
ASN1_INTEGER *ret;
|
||||
if (ai == NULL) {
|
||||
ret = ASN1_STRING_type_new(type);
|
||||
} else {
|
||||
ret = ai;
|
||||
}
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (BN_is_negative(bn) && !BN_is_zero(bn)) {
|
||||
ret->type = type | V_ASN1_NEG;
|
||||
} else {
|
||||
ret->type = type;
|
||||
}
|
||||
if (BN_is_negative(bn) && !BN_is_zero(bn)) {
|
||||
ret->type = type | V_ASN1_NEG;
|
||||
} else {
|
||||
ret->type = type;
|
||||
}
|
||||
|
||||
int len = BN_num_bytes(bn);
|
||||
if (!ASN1_STRING_set(ret, NULL, len) ||
|
||||
!BN_bn2bin_padded(ret->data, len, bn)) {
|
||||
goto err;
|
||||
}
|
||||
return ret;
|
||||
int len = BN_num_bytes(bn);
|
||||
if (!ASN1_STRING_set(ret, NULL, len) ||
|
||||
!BN_bn2bin_padded(ret->data, len, bn)) {
|
||||
goto err;
|
||||
}
|
||||
return ret;
|
||||
|
||||
err:
|
||||
if (ret != ai) {
|
||||
ASN1_STRING_free(ret);
|
||||
}
|
||||
err:
|
||||
if (ret != ai) {
|
||||
ASN1_STRING_free(ret);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) {
|
||||
return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai) {
|
||||
return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
static BIGNUM *asn1_string_to_bn(const ASN1_STRING *ai, BIGNUM *bn, int type) {
|
||||
if ((ai->type & ~V_ASN1_NEG) != type) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BIGNUM *ret;
|
||||
if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB);
|
||||
} else if (ai->type & V_ASN1_NEG) {
|
||||
BN_set_negative(ret, 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
|
||||
{
|
||||
return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
|
||||
BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn) {
|
||||
return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
|
||||
{
|
||||
return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
static BIGNUM *asn1_string_to_bn(const ASN1_STRING *ai, BIGNUM *bn, int type)
|
||||
{
|
||||
if ((ai->type & ~V_ASN1_NEG) != type) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BIGNUM *ret;
|
||||
if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL)
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB);
|
||||
else if (ai->type & V_ASN1_NEG)
|
||||
BN_set_negative(ret, 1);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
|
||||
{
|
||||
return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
|
||||
{
|
||||
return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
|
||||
BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn) {
|
||||
return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
|
||||
}
|
||||
@@ -63,22 +63,19 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "internal.h"
|
||||
#include "../bytestring/internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
* These functions take a string in UTF8, ASCII or multibyte form and a mask
|
||||
* of permissible ASN1 string types. It then works out the minimal type
|
||||
* (using the order Printable < IA5 < T61 < BMP < Universal < UTF8) and
|
||||
* creates a string of the correct type with the supplied data. Yes this is
|
||||
* horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
|
||||
* size limits too.
|
||||
*/
|
||||
// These functions take a string in UTF8, ASCII or multibyte form and a mask
|
||||
// of permissible ASN1 string types. It then works out the minimal type
|
||||
// (using the order Printable < IA5 < T61 < BMP < Universal < UTF8) and
|
||||
// creates a string of the correct type with the supplied data. Yes this is
|
||||
// horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
|
||||
// size limits too.
|
||||
|
||||
int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
int inform, unsigned long mask)
|
||||
{
|
||||
return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
|
||||
int inform, unsigned long mask) {
|
||||
return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
|
||||
}
|
||||
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_BMPSTRING)
|
||||
@@ -86,213 +83,211 @@ OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UNIVERSALSTRING)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UTF8STRING)
|
||||
|
||||
int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
int inform, unsigned long mask,
|
||||
long minsize, long maxsize)
|
||||
{
|
||||
int str_type;
|
||||
char free_out;
|
||||
ASN1_STRING *dest;
|
||||
size_t nchar = 0;
|
||||
char strbuf[32];
|
||||
if (len == -1)
|
||||
len = strlen((const char *)in);
|
||||
if (!mask)
|
||||
mask = DIRSTRING_TYPE;
|
||||
int inform, unsigned long mask, long minsize,
|
||||
long maxsize) {
|
||||
int str_type;
|
||||
char free_out;
|
||||
ASN1_STRING *dest;
|
||||
size_t nchar = 0;
|
||||
char strbuf[32];
|
||||
if (len == -1) {
|
||||
len = strlen((const char *)in);
|
||||
}
|
||||
if (!mask) {
|
||||
mask = DIRSTRING_TYPE;
|
||||
}
|
||||
|
||||
int (*decode_func)(CBS *, uint32_t*);
|
||||
int error;
|
||||
switch (inform) {
|
||||
int (*decode_func)(CBS *, uint32_t *);
|
||||
int error;
|
||||
switch (inform) {
|
||||
case MBSTRING_BMP:
|
||||
decode_func = cbs_get_ucs2_be;
|
||||
error = ASN1_R_INVALID_BMPSTRING;
|
||||
break;
|
||||
decode_func = cbs_get_ucs2_be;
|
||||
error = ASN1_R_INVALID_BMPSTRING;
|
||||
break;
|
||||
|
||||
case MBSTRING_UNIV:
|
||||
decode_func = cbs_get_utf32_be;
|
||||
error = ASN1_R_INVALID_UNIVERSALSTRING;
|
||||
break;
|
||||
decode_func = cbs_get_utf32_be;
|
||||
error = ASN1_R_INVALID_UNIVERSALSTRING;
|
||||
break;
|
||||
|
||||
case MBSTRING_UTF8:
|
||||
decode_func = cbs_get_utf8;
|
||||
error = ASN1_R_INVALID_UTF8STRING;
|
||||
break;
|
||||
decode_func = cbs_get_utf8;
|
||||
error = ASN1_R_INVALID_UTF8STRING;
|
||||
break;
|
||||
|
||||
case MBSTRING_ASC:
|
||||
decode_func = cbs_get_latin1;
|
||||
error = ERR_R_INTERNAL_ERROR; // Latin-1 inputs are never invalid.
|
||||
break;
|
||||
decode_func = cbs_get_latin1;
|
||||
error = ERR_R_INTERNAL_ERROR; // Latin-1 inputs are never invalid.
|
||||
break;
|
||||
|
||||
default:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
|
||||
return -1;
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Check |minsize| and |maxsize| and work out the minimal type, if any.
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, in, len);
|
||||
size_t utf8_len = 0;
|
||||
while (CBS_len(&cbs) != 0) {
|
||||
uint32_t c;
|
||||
if (!decode_func(&cbs, &c)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, error);
|
||||
return -1;
|
||||
}
|
||||
if (nchar == 0 && (inform == MBSTRING_BMP || inform == MBSTRING_UNIV) &&
|
||||
c == 0xfeff) {
|
||||
// Reject byte-order mark. We could drop it but that would mean
|
||||
// adding ambiguity around whether a BOM was included or not when
|
||||
// matching strings.
|
||||
//
|
||||
// For a little-endian UCS-2 string, the BOM will appear as 0xfffe
|
||||
// and will be rejected as noncharacter, below.
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check |minsize| and |maxsize| and work out the minimal type, if any. */
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, in, len);
|
||||
size_t utf8_len = 0;
|
||||
while (CBS_len(&cbs) != 0) {
|
||||
uint32_t c;
|
||||
if (!decode_func(&cbs, &c)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, error);
|
||||
return -1;
|
||||
}
|
||||
if (nchar == 0 &&
|
||||
(inform == MBSTRING_BMP || inform == MBSTRING_UNIV) &&
|
||||
c == 0xfeff) {
|
||||
/* Reject byte-order mark. We could drop it but that would mean
|
||||
* adding ambiguity around whether a BOM was included or not when
|
||||
* matching strings.
|
||||
*
|
||||
* For a little-endian UCS-2 string, the BOM will appear as 0xfffe
|
||||
* and will be rejected as noncharacter, below. */
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Update which output formats are still possible. */
|
||||
if ((mask & B_ASN1_PRINTABLESTRING) && !asn1_is_printable(c)) {
|
||||
mask &= ~B_ASN1_PRINTABLESTRING;
|
||||
}
|
||||
if ((mask & B_ASN1_IA5STRING) && (c > 127)) {
|
||||
mask &= ~B_ASN1_IA5STRING;
|
||||
}
|
||||
if ((mask & B_ASN1_T61STRING) && (c > 0xff)) {
|
||||
mask &= ~B_ASN1_T61STRING;
|
||||
}
|
||||
if ((mask & B_ASN1_BMPSTRING) && (c > 0xffff)) {
|
||||
mask &= ~B_ASN1_BMPSTRING;
|
||||
}
|
||||
if (!mask) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
nchar++;
|
||||
utf8_len += cbb_get_utf8_len(c);
|
||||
// Update which output formats are still possible.
|
||||
if ((mask & B_ASN1_PRINTABLESTRING) && !asn1_is_printable(c)) {
|
||||
mask &= ~B_ASN1_PRINTABLESTRING;
|
||||
}
|
||||
if ((mask & B_ASN1_IA5STRING) && (c > 127)) {
|
||||
mask &= ~B_ASN1_IA5STRING;
|
||||
}
|
||||
if ((mask & B_ASN1_T61STRING) && (c > 0xff)) {
|
||||
mask &= ~B_ASN1_T61STRING;
|
||||
}
|
||||
if ((mask & B_ASN1_BMPSTRING) && (c > 0xffff)) {
|
||||
mask &= ~B_ASN1_BMPSTRING;
|
||||
}
|
||||
if (!mask) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (minsize > 0 && nchar < (size_t)minsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
|
||||
BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
|
||||
ERR_add_error_data(2, "minsize=", strbuf);
|
||||
return -1;
|
||||
}
|
||||
nchar++;
|
||||
utf8_len += cbb_get_utf8_len(c);
|
||||
}
|
||||
|
||||
if (maxsize > 0 && nchar > (size_t)maxsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
|
||||
ERR_add_error_data(2, "maxsize=", strbuf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Now work out output format and string type */
|
||||
int (*encode_func)(CBB *, uint32_t) = cbb_add_latin1;
|
||||
size_t size_estimate = nchar;
|
||||
int outform = MBSTRING_ASC;
|
||||
if (mask & B_ASN1_PRINTABLESTRING) {
|
||||
str_type = V_ASN1_PRINTABLESTRING;
|
||||
} else if (mask & B_ASN1_IA5STRING) {
|
||||
str_type = V_ASN1_IA5STRING;
|
||||
} else if (mask & B_ASN1_T61STRING) {
|
||||
str_type = V_ASN1_T61STRING;
|
||||
} else if (mask & B_ASN1_BMPSTRING) {
|
||||
str_type = V_ASN1_BMPSTRING;
|
||||
outform = MBSTRING_BMP;
|
||||
encode_func = cbb_add_ucs2_be;
|
||||
size_estimate = 2 * nchar;
|
||||
} else if (mask & B_ASN1_UNIVERSALSTRING) {
|
||||
str_type = V_ASN1_UNIVERSALSTRING;
|
||||
encode_func = cbb_add_utf32_be;
|
||||
size_estimate = 4 * nchar;
|
||||
outform = MBSTRING_UNIV;
|
||||
} else if (mask & B_ASN1_UTF8STRING) {
|
||||
str_type = V_ASN1_UTF8STRING;
|
||||
outform = MBSTRING_UTF8;
|
||||
encode_func = cbb_add_utf8;
|
||||
size_estimate = utf8_len;
|
||||
} else {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!out)
|
||||
return str_type;
|
||||
if (*out) {
|
||||
free_out = 0;
|
||||
dest = *out;
|
||||
if (dest->data) {
|
||||
dest->length = 0;
|
||||
OPENSSL_free(dest->data);
|
||||
dest->data = NULL;
|
||||
}
|
||||
dest->type = str_type;
|
||||
} else {
|
||||
free_out = 1;
|
||||
dest = ASN1_STRING_type_new(str_type);
|
||||
if (!dest) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
*out = dest;
|
||||
}
|
||||
|
||||
/* If both the same type just copy across */
|
||||
if (inform == outform) {
|
||||
if (!ASN1_STRING_set(dest, in, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
return str_type;
|
||||
}
|
||||
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, size_estimate + 1)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
CBS_init(&cbs, in, len);
|
||||
while (CBS_len(&cbs) != 0) {
|
||||
uint32_t c;
|
||||
if (!decode_func(&cbs, &c) ||
|
||||
!encode_func(&cbb, c)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
uint8_t *data = NULL;
|
||||
size_t data_len;
|
||||
if (/* OpenSSL historically NUL-terminated this value with a single byte,
|
||||
* even for |MBSTRING_BMP| and |MBSTRING_UNIV|. */
|
||||
!CBB_add_u8(&cbb, 0) ||
|
||||
!CBB_finish(&cbb, &data, &data_len) ||
|
||||
data_len < 1 ||
|
||||
data_len > INT_MAX) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR);
|
||||
OPENSSL_free(data);
|
||||
goto err;
|
||||
}
|
||||
dest->length = (int)(data_len - 1);
|
||||
dest->data = data;
|
||||
return str_type;
|
||||
|
||||
err:
|
||||
if (free_out)
|
||||
ASN1_STRING_free(dest);
|
||||
CBB_cleanup(&cbb);
|
||||
if (minsize > 0 && nchar < (size_t)minsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
|
||||
BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
|
||||
ERR_add_error_data(2, "minsize=", strbuf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (maxsize > 0 && nchar > (size_t)maxsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
|
||||
ERR_add_error_data(2, "maxsize=", strbuf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Now work out output format and string type
|
||||
int (*encode_func)(CBB *, uint32_t) = cbb_add_latin1;
|
||||
size_t size_estimate = nchar;
|
||||
int outform = MBSTRING_ASC;
|
||||
if (mask & B_ASN1_PRINTABLESTRING) {
|
||||
str_type = V_ASN1_PRINTABLESTRING;
|
||||
} else if (mask & B_ASN1_IA5STRING) {
|
||||
str_type = V_ASN1_IA5STRING;
|
||||
} else if (mask & B_ASN1_T61STRING) {
|
||||
str_type = V_ASN1_T61STRING;
|
||||
} else if (mask & B_ASN1_BMPSTRING) {
|
||||
str_type = V_ASN1_BMPSTRING;
|
||||
outform = MBSTRING_BMP;
|
||||
encode_func = cbb_add_ucs2_be;
|
||||
size_estimate = 2 * nchar;
|
||||
} else if (mask & B_ASN1_UNIVERSALSTRING) {
|
||||
str_type = V_ASN1_UNIVERSALSTRING;
|
||||
encode_func = cbb_add_utf32_be;
|
||||
size_estimate = 4 * nchar;
|
||||
outform = MBSTRING_UNIV;
|
||||
} else if (mask & B_ASN1_UTF8STRING) {
|
||||
str_type = V_ASN1_UTF8STRING;
|
||||
outform = MBSTRING_UTF8;
|
||||
encode_func = cbb_add_utf8;
|
||||
size_estimate = utf8_len;
|
||||
} else {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!out) {
|
||||
return str_type;
|
||||
}
|
||||
if (*out) {
|
||||
free_out = 0;
|
||||
dest = *out;
|
||||
if (dest->data) {
|
||||
dest->length = 0;
|
||||
OPENSSL_free(dest->data);
|
||||
dest->data = NULL;
|
||||
}
|
||||
dest->type = str_type;
|
||||
} else {
|
||||
free_out = 1;
|
||||
dest = ASN1_STRING_type_new(str_type);
|
||||
if (!dest) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
*out = dest;
|
||||
}
|
||||
|
||||
// If both the same type just copy across
|
||||
if (inform == outform) {
|
||||
if (!ASN1_STRING_set(dest, in, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
return str_type;
|
||||
}
|
||||
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, size_estimate + 1)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
CBS_init(&cbs, in, len);
|
||||
while (CBS_len(&cbs) != 0) {
|
||||
uint32_t c;
|
||||
if (!decode_func(&cbs, &c) || !encode_func(&cbb, c)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
uint8_t *data = NULL;
|
||||
size_t data_len;
|
||||
if (// OpenSSL historically NUL-terminated this value with a single byte,
|
||||
// even for |MBSTRING_BMP| and |MBSTRING_UNIV|.
|
||||
!CBB_add_u8(&cbb, 0) || !CBB_finish(&cbb, &data, &data_len) ||
|
||||
data_len < 1 || data_len > INT_MAX) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR);
|
||||
OPENSSL_free(data);
|
||||
goto err;
|
||||
}
|
||||
dest->length = (int)(data_len - 1);
|
||||
dest->data = data;
|
||||
return str_type;
|
||||
|
||||
err:
|
||||
if (free_out) {
|
||||
ASN1_STRING_free(dest);
|
||||
}
|
||||
CBB_cleanup(&cbb);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int asn1_is_printable(uint32_t value)
|
||||
{
|
||||
if (value > 0x7f) {
|
||||
return 0;
|
||||
}
|
||||
/* Note we cannot use |isalnum| because it is locale-dependent. */
|
||||
return ('a' <= value && value <= 'z') || //
|
||||
('A' <= value && value <= 'Z') || //
|
||||
('0' <= value && value <= '9') || //
|
||||
value == ' ' || value == '\'' || value == '(' || value == ')' ||
|
||||
value == '+' || value == ',' || value == '-' || value == '.' ||
|
||||
value == '/' || value == ':' || value == '=' || value == '?';
|
||||
int asn1_is_printable(uint32_t value) {
|
||||
if (value > 0x7f) {
|
||||
return 0;
|
||||
}
|
||||
// Note we cannot use |isalnum| because it is locale-dependent.
|
||||
return ('a' <= value && value <= 'z') || //
|
||||
('A' <= value && value <= 'Z') || //
|
||||
('0' <= value && value <= '9') || //
|
||||
value == ' ' || value == '\'' || value == '(' || value == ')' ||
|
||||
value == '+' || value == ',' || value == '-' || value == '.' ||
|
||||
value == '/' || value == ':' || value == '=' || value == '?';
|
||||
}
|
||||
@@ -67,233 +67,224 @@
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
|
||||
{
|
||||
if (a == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return -1;
|
||||
}
|
||||
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) {
|
||||
if (a == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (a->length == 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
|
||||
return -1;
|
||||
}
|
||||
if (a->length == 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
|
||||
if (pp == NULL || objsize == -1) {
|
||||
return objsize;
|
||||
}
|
||||
|
||||
unsigned char *p, *allocated = NULL;
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
|
||||
OPENSSL_memcpy(p, a->data, a->length);
|
||||
|
||||
/*
|
||||
* If a new buffer was allocated, just return it back.
|
||||
* If not, return the incremented buffer pointer.
|
||||
*/
|
||||
*pp = allocated != NULL ? allocated : p + a->length;
|
||||
int objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
|
||||
if (pp == NULL || objsize == -1) {
|
||||
return objsize;
|
||||
}
|
||||
|
||||
unsigned char *p, *allocated = NULL;
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
|
||||
OPENSSL_memcpy(p, a->data, a->length);
|
||||
|
||||
// If a new buffer was allocated, just return it back.
|
||||
// If not, return the incremented buffer pointer.
|
||||
*pp = allocated != NULL ? allocated : p + a->length;
|
||||
return objsize;
|
||||
}
|
||||
|
||||
int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
|
||||
{
|
||||
return OBJ_obj2txt(buf, buf_len, a, 0);
|
||||
int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a) {
|
||||
return OBJ_obj2txt(buf, buf_len, a, 0);
|
||||
}
|
||||
|
||||
static int write_str(BIO *bp, const char *str)
|
||||
{
|
||||
int len = strlen(str);
|
||||
return BIO_write(bp, str, len) == len ? len : -1;
|
||||
static int write_str(BIO *bp, const char *str) {
|
||||
int len = strlen(str);
|
||||
return BIO_write(bp, str, len) == len ? len : -1;
|
||||
}
|
||||
|
||||
int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
|
||||
{
|
||||
if (a == NULL || a->data == NULL) {
|
||||
return write_str(bp, "NULL");
|
||||
}
|
||||
int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a) {
|
||||
if (a == NULL || a->data == NULL) {
|
||||
return write_str(bp, "NULL");
|
||||
}
|
||||
|
||||
char buf[80], *allocated = NULL;
|
||||
const char *str = buf;
|
||||
int len = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
|
||||
if (len > (int)sizeof(buf) - 1) {
|
||||
/* The input was truncated. Allocate a buffer that fits. */
|
||||
allocated = OPENSSL_malloc(len + 1);
|
||||
if (allocated == NULL) {
|
||||
return -1;
|
||||
}
|
||||
len = i2t_ASN1_OBJECT(allocated, len + 1, a);
|
||||
str = allocated;
|
||||
}
|
||||
if (len <= 0) {
|
||||
str = "<INVALID>";
|
||||
char buf[80], *allocated = NULL;
|
||||
const char *str = buf;
|
||||
int len = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
|
||||
if (len > (int)sizeof(buf) - 1) {
|
||||
// The input was truncated. Allocate a buffer that fits.
|
||||
allocated = OPENSSL_malloc(len + 1);
|
||||
if (allocated == NULL) {
|
||||
return -1;
|
||||
}
|
||||
len = i2t_ASN1_OBJECT(allocated, len + 1, a);
|
||||
str = allocated;
|
||||
}
|
||||
if (len <= 0) {
|
||||
str = "<INVALID>";
|
||||
}
|
||||
|
||||
int ret = write_str(bp, str);
|
||||
OPENSSL_free(allocated);
|
||||
return ret;
|
||||
int ret = write_str(bp, str);
|
||||
OPENSSL_free(allocated);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
long length)
|
||||
{
|
||||
long len;
|
||||
int tag, xclass;
|
||||
const unsigned char *p = *pp;
|
||||
int inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
return NULL;
|
||||
}
|
||||
long length) {
|
||||
long len;
|
||||
int tag, xclass;
|
||||
const unsigned char *p = *pp;
|
||||
int inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return NULL;
|
||||
}
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (tag != V_ASN1_OBJECT || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_AN_OBJECT);
|
||||
return NULL;
|
||||
}
|
||||
ASN1_OBJECT *ret = c2i_ASN1_OBJECT(a, &p, len);
|
||||
if (ret) {
|
||||
*pp = p;
|
||||
}
|
||||
return ret;
|
||||
if (tag != V_ASN1_OBJECT || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_AN_OBJECT);
|
||||
return NULL;
|
||||
}
|
||||
ASN1_OBJECT *ret = c2i_ASN1_OBJECT(a, &p, len);
|
||||
if (ret) {
|
||||
*pp = p;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
long len)
|
||||
{
|
||||
ASN1_OBJECT *ret = NULL;
|
||||
const unsigned char *p;
|
||||
unsigned char *data;
|
||||
int i, length;
|
||||
long len) {
|
||||
ASN1_OBJECT *ret = NULL;
|
||||
const unsigned char *p;
|
||||
unsigned char *data;
|
||||
int i, length;
|
||||
|
||||
/*
|
||||
* Sanity check OID encoding. Need at least one content octet. MSB must
|
||||
* be clear in the last octet. can't have leading 0x80 in subidentifiers,
|
||||
* see: X.690 8.19.2
|
||||
*/
|
||||
if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
|
||||
p[len - 1] & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
/* Now 0 < len <= INT_MAX, so the cast is safe. */
|
||||
length = (int)len;
|
||||
for (i = 0; i < length; i++, p++) {
|
||||
if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
// Sanity check OID encoding. Need at least one content octet. MSB must
|
||||
// be clear in the last octet. can't have leading 0x80 in subidentifiers,
|
||||
// see: X.690 8.19.2
|
||||
if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
|
||||
p[len - 1] & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
// Now 0 < len <= INT_MAX, so the cast is safe.
|
||||
length = (int)len;
|
||||
for (i = 0; i < length; i++, p++) {
|
||||
if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if ((a == NULL) || ((*a) == NULL) ||
|
||||
!((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
|
||||
if ((ret = ASN1_OBJECT_new()) == NULL)
|
||||
return (NULL);
|
||||
} else {
|
||||
ret = (*a);
|
||||
if ((a == NULL) || ((*a) == NULL) ||
|
||||
!((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
|
||||
if ((ret = ASN1_OBJECT_new()) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret = (*a);
|
||||
}
|
||||
|
||||
p = *pp;
|
||||
/* detach data from object */
|
||||
data = (unsigned char *)ret->data;
|
||||
ret->data = NULL;
|
||||
/* once detached we can change it */
|
||||
if ((data == NULL) || (ret->length < length)) {
|
||||
ret->length = 0;
|
||||
if (data != NULL)
|
||||
OPENSSL_free(data);
|
||||
data = (unsigned char *)OPENSSL_malloc(length);
|
||||
if (data == NULL) {
|
||||
i = ERR_R_MALLOC_FAILURE;
|
||||
goto err;
|
||||
}
|
||||
ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
}
|
||||
OPENSSL_memcpy(data, p, length);
|
||||
/* If there are dynamic strings, free them here, and clear the flag */
|
||||
if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
|
||||
OPENSSL_free((char *)ret->sn);
|
||||
OPENSSL_free((char *)ret->ln);
|
||||
ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
|
||||
}
|
||||
/* reattach data to object, after which it remains const */
|
||||
ret->data = data;
|
||||
ret->length = length;
|
||||
ret->sn = NULL;
|
||||
ret->ln = NULL;
|
||||
p += length;
|
||||
|
||||
if (a != NULL)
|
||||
(*a) = ret;
|
||||
*pp = p;
|
||||
return (ret);
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, i);
|
||||
if ((ret != NULL) && ((a == NULL) || (*a != ret)))
|
||||
ASN1_OBJECT_free(ret);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_new(void)
|
||||
{
|
||||
ASN1_OBJECT *ret;
|
||||
|
||||
ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return (NULL);
|
||||
}
|
||||
p = *pp;
|
||||
// detach data from object
|
||||
data = (unsigned char *)ret->data;
|
||||
ret->data = NULL;
|
||||
// once detached we can change it
|
||||
if ((data == NULL) || (ret->length < length)) {
|
||||
ret->length = 0;
|
||||
ret->data = NULL;
|
||||
ret->nid = 0;
|
||||
ret->sn = NULL;
|
||||
ret->ln = NULL;
|
||||
ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
|
||||
return (ret);
|
||||
OPENSSL_free(data);
|
||||
data = (unsigned char *)OPENSSL_malloc(length);
|
||||
if (data == NULL) {
|
||||
i = ERR_R_MALLOC_FAILURE;
|
||||
goto err;
|
||||
}
|
||||
ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
}
|
||||
OPENSSL_memcpy(data, p, length);
|
||||
// If there are dynamic strings, free them here, and clear the flag
|
||||
if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
|
||||
OPENSSL_free((char *)ret->sn);
|
||||
OPENSSL_free((char *)ret->ln);
|
||||
ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
|
||||
}
|
||||
// reattach data to object, after which it remains const
|
||||
ret->data = data;
|
||||
ret->length = length;
|
||||
ret->sn = NULL;
|
||||
ret->ln = NULL;
|
||||
p += length;
|
||||
|
||||
if (a != NULL) {
|
||||
(*a) = ret;
|
||||
}
|
||||
*pp = p;
|
||||
return ret;
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, i);
|
||||
if ((ret != NULL) && ((a == NULL) || (*a != ret))) {
|
||||
ASN1_OBJECT_free(ret);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ASN1_OBJECT_free(ASN1_OBJECT *a)
|
||||
{
|
||||
if (a == NULL)
|
||||
return;
|
||||
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
|
||||
OPENSSL_free((void *)a->sn);
|
||||
OPENSSL_free((void *)a->ln);
|
||||
a->sn = a->ln = NULL;
|
||||
}
|
||||
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
|
||||
OPENSSL_free((void *)a->data);
|
||||
a->data = NULL;
|
||||
a->length = 0;
|
||||
}
|
||||
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
|
||||
OPENSSL_free(a);
|
||||
ASN1_OBJECT *ASN1_OBJECT_new(void) {
|
||||
ASN1_OBJECT *ret;
|
||||
|
||||
ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
ret->length = 0;
|
||||
ret->data = NULL;
|
||||
ret->nid = 0;
|
||||
ret->sn = NULL;
|
||||
ret->ln = NULL;
|
||||
ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ASN1_OBJECT_free(ASN1_OBJECT *a) {
|
||||
if (a == NULL) {
|
||||
return;
|
||||
}
|
||||
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
|
||||
OPENSSL_free((void *)a->sn);
|
||||
OPENSSL_free((void *)a->ln);
|
||||
a->sn = a->ln = NULL;
|
||||
}
|
||||
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
|
||||
OPENSSL_free((void *)a->data);
|
||||
a->data = NULL;
|
||||
a->length = 0;
|
||||
}
|
||||
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) {
|
||||
OPENSSL_free(a);
|
||||
}
|
||||
}
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, int len,
|
||||
const char *sn, const char *ln)
|
||||
{
|
||||
ASN1_OBJECT o;
|
||||
const char *sn, const char *ln) {
|
||||
ASN1_OBJECT o;
|
||||
|
||||
o.sn = sn;
|
||||
o.ln = ln;
|
||||
o.data = data;
|
||||
o.nid = nid;
|
||||
o.length = len;
|
||||
o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
|
||||
ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
return (OBJ_dup(&o));
|
||||
o.sn = sn;
|
||||
o.ln = ln;
|
||||
o.data = data;
|
||||
o.nid = nid;
|
||||
o.length = len;
|
||||
o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
|
||||
ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
return (OBJ_dup(&o));
|
||||
}
|
||||
@@ -59,19 +59,16 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *x)
|
||||
{
|
||||
return ASN1_STRING_dup(x);
|
||||
ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *x) {
|
||||
return ASN1_STRING_dup(x);
|
||||
}
|
||||
|
||||
int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a,
|
||||
const ASN1_OCTET_STRING *b)
|
||||
{
|
||||
return ASN1_STRING_cmp(a, b);
|
||||
const ASN1_OCTET_STRING *b) {
|
||||
return ASN1_STRING_cmp(a, b);
|
||||
}
|
||||
|
||||
int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *x, const unsigned char *d,
|
||||
int len)
|
||||
{
|
||||
return ASN1_STRING_set(x, d, len);
|
||||
int len) {
|
||||
return ASN1_STRING_set(x, d, len);
|
||||
}
|
||||
@@ -61,23 +61,22 @@
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_PRINTABLE_type(const unsigned char *s, int len)
|
||||
{
|
||||
if (len < 0) {
|
||||
len = strlen((const char *)s);
|
||||
}
|
||||
int ASN1_PRINTABLE_type(const unsigned char *s, int len) {
|
||||
if (len < 0) {
|
||||
len = strlen((const char *)s);
|
||||
}
|
||||
|
||||
int printable = 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
unsigned char c = s[i];
|
||||
if (c & 0x80) {
|
||||
/* No need to continue iterating. */
|
||||
return V_ASN1_T61STRING;
|
||||
}
|
||||
if (!asn1_is_printable(c)) {
|
||||
printable = 0;
|
||||
}
|
||||
int printable = 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
unsigned char c = s[i];
|
||||
if (c & 0x80) {
|
||||
// No need to continue iterating.
|
||||
return V_ASN1_T61STRING;
|
||||
}
|
||||
if (!asn1_is_printable(c)) {
|
||||
printable = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return printable ? V_ASN1_PRINTABLESTRING : V_ASN1_IA5STRING;
|
||||
return printable ? V_ASN1_PRINTABLESTRING : V_ASN1_IA5STRING;
|
||||
}
|
||||
@@ -60,10 +60,13 @@
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../bytestring/internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
@@ -71,242 +74,201 @@
|
||||
(ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_QUOTE | ASN1_STRFLGS_ESC_CTRL | \
|
||||
ASN1_STRFLGS_ESC_MSB)
|
||||
|
||||
static int maybe_write(BIO *out, const void *buf, int len)
|
||||
{
|
||||
/* If |out| is NULL, ignore the output but report the length. */
|
||||
return out == NULL || BIO_write(out, buf, len) == len;
|
||||
static int maybe_write(BIO *out, const void *buf, int len) {
|
||||
// If |out| is NULL, ignore the output but report the length.
|
||||
return out == NULL || BIO_write(out, buf, len) == len;
|
||||
}
|
||||
|
||||
static int is_control_character(unsigned char c)
|
||||
{
|
||||
return c < 32 || c == 127;
|
||||
}
|
||||
static int is_control_character(unsigned char c) { return c < 32 || c == 127; }
|
||||
|
||||
static int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes,
|
||||
BIO *out, int is_first, int is_last)
|
||||
{
|
||||
/* |c| is a |uint32_t| because, depending on |ASN1_STRFLGS_UTF8_CONVERT|,
|
||||
* we may be escaping bytes or Unicode codepoints. */
|
||||
char buf[16]; /* Large enough for "\\W01234567". */
|
||||
unsigned char u8 = (unsigned char)c;
|
||||
if (c > 0xffff) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\W%08" PRIX32, c);
|
||||
} else if (c > 0xff) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\U%04" PRIX32, c);
|
||||
} else if ((flags & ASN1_STRFLGS_ESC_MSB) && c > 0x7f) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%02X", c);
|
||||
} else if ((flags & ASN1_STRFLGS_ESC_CTRL) && is_control_character(c)) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%02X", c);
|
||||
} else if (flags & ASN1_STRFLGS_ESC_2253) {
|
||||
/* See RFC 2253, sections 2.4 and 4. */
|
||||
if (c == '\\' || c == '"') {
|
||||
/* Quotes and backslashes are always escaped, quoted or not. */
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
} else if (c == ',' || c == '+' || c == '<' || c == '>' || c == ';' ||
|
||||
(is_first && (c == ' ' || c == '#')) ||
|
||||
(is_last && (c == ' '))) {
|
||||
if (flags & ASN1_STRFLGS_ESC_QUOTE) {
|
||||
/* No need to escape, just tell the caller to quote. */
|
||||
if (do_quotes != NULL) {
|
||||
*do_quotes = 1;
|
||||
}
|
||||
return maybe_write(out, &u8, 1) ? 1 : -1;
|
||||
}
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
} else {
|
||||
return maybe_write(out, &u8, 1) ? 1 : -1;
|
||||
BIO *out, int is_first, int is_last) {
|
||||
// |c| is a |uint32_t| because, depending on |ASN1_STRFLGS_UTF8_CONVERT|,
|
||||
// we may be escaping bytes or Unicode codepoints.
|
||||
char buf[16]; // Large enough for "\\W01234567".
|
||||
unsigned char u8 = (unsigned char)c;
|
||||
if (c > 0xffff) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\W%08" PRIX32, c);
|
||||
} else if (c > 0xff) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\U%04" PRIX32, c);
|
||||
} else if ((flags & ASN1_STRFLGS_ESC_MSB) && c > 0x7f) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%02X", c);
|
||||
} else if ((flags & ASN1_STRFLGS_ESC_CTRL) && is_control_character(c)) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%02X", c);
|
||||
} else if (flags & ASN1_STRFLGS_ESC_2253) {
|
||||
// See RFC 2253, sections 2.4 and 4.
|
||||
if (c == '\\' || c == '"') {
|
||||
// Quotes and backslashes are always escaped, quoted or not.
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
} else if (c == ',' || c == '+' || c == '<' || c == '>' || c == ';' ||
|
||||
(is_first && (c == ' ' || c == '#')) ||
|
||||
(is_last && (c == ' '))) {
|
||||
if (flags & ASN1_STRFLGS_ESC_QUOTE) {
|
||||
// No need to escape, just tell the caller to quote.
|
||||
if (do_quotes != NULL) {
|
||||
*do_quotes = 1;
|
||||
}
|
||||
} else if ((flags & ESC_FLAGS) && c == '\\') {
|
||||
/* If any escape flags are set, also escape backslashes. */
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
} else {
|
||||
return maybe_write(out, &u8, 1) ? 1 : -1;
|
||||
}
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
} else {
|
||||
return maybe_write(out, &u8, 1) ? 1 : -1;
|
||||
}
|
||||
} else if ((flags & ESC_FLAGS) && c == '\\') {
|
||||
// If any escape flags are set, also escape backslashes.
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
} else {
|
||||
return maybe_write(out, &u8, 1) ? 1 : -1;
|
||||
}
|
||||
|
||||
int len = strlen(buf);
|
||||
return maybe_write(out, buf, len) ? len : -1;
|
||||
int len = strlen(buf);
|
||||
return maybe_write(out, buf, len) ? len : -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function sends each character in a buffer to do_esc_char(). It
|
||||
* interprets the content formats and converts to or from UTF8 as
|
||||
* appropriate.
|
||||
*/
|
||||
// This function sends each character in a buffer to do_esc_char(). It
|
||||
// interprets the content formats and converts to or from UTF8 as
|
||||
// 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. */
|
||||
switch (encoding) {
|
||||
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;
|
||||
}
|
||||
break;
|
||||
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;
|
||||
}
|
||||
break;
|
||||
}
|
||||
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;
|
||||
int outlen = 0;
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, buf, buflen);
|
||||
int outlen = 0;
|
||||
while (CBS_len(&cbs) != 0) {
|
||||
const int is_first = CBS_data(&cbs) == buf;
|
||||
uint32_t c;
|
||||
if (!get_char(&cbs, &c)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, get_char_error);
|
||||
return -1;
|
||||
}
|
||||
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);
|
||||
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);
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
}
|
||||
outlen += len;
|
||||
}
|
||||
} else {
|
||||
int len = do_esc_char(c, flags, quotes, out, is_first, is_last);
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
}
|
||||
outlen += len;
|
||||
}
|
||||
}
|
||||
return outlen;
|
||||
}
|
||||
|
||||
// This function hex dumps a buffer of characters
|
||||
|
||||
static int do_hex_dump(BIO *out, unsigned char *buf, int buflen) {
|
||||
static const char hexdig[] = "0123456789ABCDEF";
|
||||
unsigned char *p, *q;
|
||||
char hextmp[2];
|
||||
if (out) {
|
||||
p = buf;
|
||||
q = buf + buflen;
|
||||
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. */
|
||||
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;
|
||||
}
|
||||
const int is_last = p == q;
|
||||
if (utf8_convert) {
|
||||
unsigned char utfbuf[6];
|
||||
int utflen;
|
||||
utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
|
||||
for (int i = 0; i < utflen; i++) {
|
||||
/*
|
||||
* We don't need to worry about setting orflags correctly
|
||||
* because if utflen==1 its value will be correct anyway
|
||||
* otherwise each character will be > 0x7f and so the
|
||||
* character will never be escaped on first and last.
|
||||
*/
|
||||
int len = do_esc_char(utfbuf[i], flags, quotes, out, is_first,
|
||||
is_last);
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
}
|
||||
outlen += len;
|
||||
}
|
||||
} else {
|
||||
int len = do_esc_char(c, flags, quotes, out, is_first, is_last);
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
}
|
||||
outlen += len;
|
||||
}
|
||||
hextmp[0] = hexdig[*p >> 4];
|
||||
hextmp[1] = hexdig[*p & 0xf];
|
||||
if (!maybe_write(out, hextmp, 2)) {
|
||||
return -1;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
return outlen;
|
||||
}
|
||||
return buflen << 1;
|
||||
}
|
||||
|
||||
/* This function hex dumps a buffer of characters */
|
||||
// "dump" a string. This is done when the type is unknown, or the flags
|
||||
// request it. We can either dump the content octets or the entire DER
|
||||
// encoding. This uses the RFC 2253 #01234 format.
|
||||
|
||||
static int do_hex_dump(BIO *out, unsigned char *buf, int buflen)
|
||||
{
|
||||
static const char hexdig[] = "0123456789ABCDEF";
|
||||
unsigned char *p, *q;
|
||||
char hextmp[2];
|
||||
if (out) {
|
||||
p = buf;
|
||||
q = buf + buflen;
|
||||
while (p != q) {
|
||||
hextmp[0] = hexdig[*p >> 4];
|
||||
hextmp[1] = hexdig[*p & 0xf];
|
||||
if (!maybe_write(out, hextmp, 2))
|
||||
return -1;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
return buflen << 1;
|
||||
}
|
||||
static int do_dump(unsigned long flags, BIO *out, const ASN1_STRING *str) {
|
||||
if (!maybe_write(out, "#", 1)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* "dump" a string. This is done when the type is unknown, or the flags
|
||||
* request it. We can either dump the content octets or the entire DER
|
||||
* encoding. This uses the RFC 2253 #01234 format.
|
||||
*/
|
||||
|
||||
static int do_dump(unsigned long flags, BIO *out, const ASN1_STRING *str)
|
||||
{
|
||||
if (!maybe_write(out, "#", 1)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If we don't dump DER encoding just dump content octets */
|
||||
if (!(flags & ASN1_STRFLGS_DUMP_DER)) {
|
||||
int outlen = do_hex_dump(out, str->data, str->length);
|
||||
if (outlen < 0) {
|
||||
return -1;
|
||||
}
|
||||
return outlen + 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Placing the ASN1_STRING in a temporary ASN1_TYPE allows the DER encoding
|
||||
* to readily obtained.
|
||||
*/
|
||||
ASN1_TYPE t;
|
||||
t.type = str->type;
|
||||
/* Negative INTEGER and ENUMERATED values are the only case where
|
||||
* |ASN1_STRING| and |ASN1_TYPE| types do not match.
|
||||
*
|
||||
* TODO(davidben): There are also some type fields which, in |ASN1_TYPE|, do
|
||||
* not correspond to |ASN1_STRING|. It is unclear whether those are allowed
|
||||
* in |ASN1_STRING| at all, or what the space of allowed types is.
|
||||
* |ASN1_item_ex_d2i| will never produce such a value so, for now, we say
|
||||
* this is an invalid input. But this corner of the library in general
|
||||
* should be more robust. */
|
||||
if (t.type == V_ASN1_NEG_INTEGER) {
|
||||
t.type = V_ASN1_INTEGER;
|
||||
} else if (t.type == V_ASN1_NEG_ENUMERATED) {
|
||||
t.type = V_ASN1_ENUMERATED;
|
||||
}
|
||||
t.value.asn1_string = (ASN1_STRING *)str;
|
||||
unsigned char *der_buf = NULL;
|
||||
int der_len = i2d_ASN1_TYPE(&t, &der_buf);
|
||||
if (der_len < 0) {
|
||||
return -1;
|
||||
}
|
||||
int outlen = do_hex_dump(out, der_buf, der_len);
|
||||
OPENSSL_free(der_buf);
|
||||
// If we don't dump DER encoding just dump content octets
|
||||
if (!(flags & ASN1_STRFLGS_DUMP_DER)) {
|
||||
int outlen = do_hex_dump(out, str->data, str->length);
|
||||
if (outlen < 0) {
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
return outlen + 1;
|
||||
}
|
||||
|
||||
// Placing the ASN1_STRING in a temporary ASN1_TYPE allows the DER encoding
|
||||
// to readily obtained.
|
||||
ASN1_TYPE t;
|
||||
t.type = str->type;
|
||||
// Negative INTEGER and ENUMERATED values are the only case where
|
||||
// |ASN1_STRING| and |ASN1_TYPE| types do not match.
|
||||
//
|
||||
// TODO(davidben): There are also some type fields which, in |ASN1_TYPE|, do
|
||||
// not correspond to |ASN1_STRING|. It is unclear whether those are allowed
|
||||
// in |ASN1_STRING| at all, or what the space of allowed types is.
|
||||
// |ASN1_item_ex_d2i| will never produce such a value so, for now, we say
|
||||
// this is an invalid input. But this corner of the library in general
|
||||
// should be more robust.
|
||||
if (t.type == V_ASN1_NEG_INTEGER) {
|
||||
t.type = V_ASN1_INTEGER;
|
||||
} else if (t.type == V_ASN1_NEG_ENUMERATED) {
|
||||
t.type = V_ASN1_ENUMERATED;
|
||||
}
|
||||
t.value.asn1_string = (ASN1_STRING *)str;
|
||||
unsigned char *der_buf = NULL;
|
||||
int der_len = i2d_ASN1_TYPE(&t, &der_buf);
|
||||
if (der_len < 0) {
|
||||
return -1;
|
||||
}
|
||||
int outlen = do_hex_dump(out, der_buf, der_len);
|
||||
OPENSSL_free(der_buf);
|
||||
if (outlen < 0) {
|
||||
return -1;
|
||||
}
|
||||
return outlen + 1;
|
||||
}
|
||||
|
||||
/* string_type_to_encoding returns the |MBSTRING_*| constant for the encoding
|
||||
* used by the |ASN1_STRING| type |type|, or -1 if |tag| is not a string
|
||||
* type. */
|
||||
// string_type_to_encoding returns the |MBSTRING_*| constant for the encoding
|
||||
// used by the |ASN1_STRING| type |type|, or -1 if |tag| is not a string
|
||||
// type.
|
||||
static int string_type_to_encoding(int type) {
|
||||
/* This function is sometimes passed ASN.1 universal types and sometimes
|
||||
* passed |ASN1_STRING| type values */
|
||||
switch (type) {
|
||||
// This function is sometimes passed ASN.1 universal types and sometimes
|
||||
// passed |ASN1_STRING| type values
|
||||
switch (type) {
|
||||
case V_ASN1_UTF8STRING:
|
||||
return MBSTRING_UTF8;
|
||||
return MBSTRING_UTF8;
|
||||
case V_ASN1_NUMERICSTRING:
|
||||
case V_ASN1_PRINTABLESTRING:
|
||||
case V_ASN1_T61STRING:
|
||||
@@ -314,307 +276,188 @@ static int string_type_to_encoding(int type) {
|
||||
case V_ASN1_UTCTIME:
|
||||
case V_ASN1_GENERALIZEDTIME:
|
||||
case V_ASN1_ISO64STRING:
|
||||
/* |MBSTRING_ASC| refers to Latin-1, not ASCII. */
|
||||
return MBSTRING_ASC;
|
||||
// |MBSTRING_ASC| refers to Latin-1, not ASCII.
|
||||
return MBSTRING_ASC;
|
||||
case V_ASN1_UNIVERSALSTRING:
|
||||
return MBSTRING_UNIV;
|
||||
return MBSTRING_UNIV;
|
||||
case V_ASN1_BMPSTRING:
|
||||
return MBSTRING_BMP;
|
||||
}
|
||||
return -1;
|
||||
return MBSTRING_BMP;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* This is the main function, print out an ASN1_STRING taking note of various
|
||||
* escape and display options. Returns number of characters written or -1 if
|
||||
* an error occurred.
|
||||
*/
|
||||
// This is the main function, print out an ASN1_STRING taking note of various
|
||||
// escape and display options. Returns number of characters written or -1 if
|
||||
// an error occurred.
|
||||
|
||||
int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags)
|
||||
{
|
||||
int type = str->type;
|
||||
int outlen = 0;
|
||||
if (flags & ASN1_STRFLGS_SHOW_TYPE) {
|
||||
const char *tagname = ASN1_tag2str(type);
|
||||
outlen += strlen(tagname);
|
||||
if (!maybe_write(out, tagname, outlen) || !maybe_write(out, ":", 1))
|
||||
return -1;
|
||||
outlen++;
|
||||
int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str,
|
||||
unsigned long flags) {
|
||||
int type = str->type;
|
||||
int outlen = 0;
|
||||
if (flags & ASN1_STRFLGS_SHOW_TYPE) {
|
||||
const char *tagname = ASN1_tag2str(type);
|
||||
outlen += strlen(tagname);
|
||||
if (!maybe_write(out, tagname, outlen) || !maybe_write(out, ":", 1)) {
|
||||
return -1;
|
||||
}
|
||||
outlen++;
|
||||
}
|
||||
|
||||
/* Decide what to do with |str|, either dump the contents or display it. */
|
||||
int encoding;
|
||||
if (flags & ASN1_STRFLGS_DUMP_ALL) {
|
||||
/* Dump everything. */
|
||||
encoding = -1;
|
||||
} else if (flags & ASN1_STRFLGS_IGNORE_TYPE) {
|
||||
/* Ignore the string type and interpret the contents as Latin-1. */
|
||||
encoding = MBSTRING_ASC;
|
||||
} else {
|
||||
encoding = string_type_to_encoding(type);
|
||||
if (encoding == -1 && (flags & ASN1_STRFLGS_DUMP_UNKNOWN) == 0) {
|
||||
encoding = MBSTRING_ASC;
|
||||
}
|
||||
// Decide what to do with |str|, either dump the contents or display it.
|
||||
int encoding;
|
||||
if (flags & ASN1_STRFLGS_DUMP_ALL) {
|
||||
// Dump everything.
|
||||
encoding = -1;
|
||||
} else if (flags & ASN1_STRFLGS_IGNORE_TYPE) {
|
||||
// Ignore the string type and interpret the contents as Latin-1.
|
||||
encoding = MBSTRING_ASC;
|
||||
} else {
|
||||
encoding = string_type_to_encoding(type);
|
||||
if (encoding == -1 && (flags & ASN1_STRFLGS_DUMP_UNKNOWN) == 0) {
|
||||
encoding = MBSTRING_ASC;
|
||||
}
|
||||
}
|
||||
|
||||
if (encoding == -1) {
|
||||
int len = do_dump(flags, out, str);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
outlen += len;
|
||||
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,
|
||||
"es, NULL);
|
||||
if (encoding == -1) {
|
||||
int len = do_dump(flags, out, str);
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
outlen += len;
|
||||
if (quotes) {
|
||||
outlen += 2;
|
||||
}
|
||||
if (!out) {
|
||||
return outlen;
|
||||
}
|
||||
|
||||
/* Encode the value. */
|
||||
if ((quotes && !maybe_write(out, "\"", 1)) ||
|
||||
do_buf(str->data, str->length, encoding, utf8_convert, flags, NULL,
|
||||
out) < 0 ||
|
||||
(quotes && !maybe_write(out, "\"", 1))) {
|
||||
return -1;
|
||||
}
|
||||
return outlen;
|
||||
}
|
||||
|
||||
// Measure the length.
|
||||
char quotes = 0;
|
||||
int len = do_buf(str->data, str->length, encoding, flags, "es, NULL);
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
}
|
||||
outlen += len;
|
||||
if (quotes) {
|
||||
outlen += 2;
|
||||
}
|
||||
if (!out) {
|
||||
return outlen;
|
||||
}
|
||||
|
||||
// Encode the value.
|
||||
if ((quotes && !maybe_write(out, "\"", 1)) ||
|
||||
do_buf(str->data, str->length, encoding, flags, NULL, out) < 0 ||
|
||||
(quotes && !maybe_write(out, "\"", 1))) {
|
||||
return -1;
|
||||
}
|
||||
return outlen;
|
||||
}
|
||||
|
||||
int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str,
|
||||
unsigned long flags)
|
||||
{
|
||||
BIO *bio = NULL;
|
||||
if (fp != NULL) {
|
||||
/* If |fp| is NULL, this function returns the number of bytes without
|
||||
* writing. */
|
||||
bio = BIO_new_fp(fp, BIO_NOCLOSE);
|
||||
if (bio == NULL) {
|
||||
return -1;
|
||||
}
|
||||
unsigned long flags) {
|
||||
BIO *bio = NULL;
|
||||
if (fp != NULL) {
|
||||
// If |fp| is NULL, this function returns the number of bytes without
|
||||
// writing.
|
||||
bio = BIO_new_fp(fp, BIO_NOCLOSE);
|
||||
if (bio == NULL) {
|
||||
return -1;
|
||||
}
|
||||
int ret = ASN1_STRING_print_ex(bio, str, flags);
|
||||
BIO_free(bio);
|
||||
}
|
||||
int ret = ASN1_STRING_print_ex(bio, str, flags);
|
||||
BIO_free(bio);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in) {
|
||||
if (!in) {
|
||||
return -1;
|
||||
}
|
||||
int mbflag = string_type_to_encoding(in->type);
|
||||
if (mbflag == -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_TAG);
|
||||
return -1;
|
||||
}
|
||||
ASN1_STRING stmp, *str = &stmp;
|
||||
stmp.data = NULL;
|
||||
stmp.length = 0;
|
||||
stmp.flags = 0;
|
||||
int ret =
|
||||
ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
*out = stmp.data;
|
||||
return stmp.length;
|
||||
}
|
||||
|
||||
int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
|
||||
{
|
||||
if (!in)
|
||||
return -1;
|
||||
int mbflag = string_type_to_encoding(in->type);
|
||||
if (mbflag == -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_TAG);
|
||||
return -1;
|
||||
}
|
||||
ASN1_STRING stmp, *str = &stmp;
|
||||
stmp.data = NULL;
|
||||
stmp.length = 0;
|
||||
stmp.flags = 0;
|
||||
int ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
|
||||
B_ASN1_UTF8STRING);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
*out = stmp.data;
|
||||
return stmp.length;
|
||||
}
|
||||
int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v) {
|
||||
int i, n;
|
||||
char buf[80];
|
||||
const char *p;
|
||||
|
||||
int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
|
||||
{
|
||||
int i, n;
|
||||
char buf[80];
|
||||
const char *p;
|
||||
|
||||
if (v == NULL)
|
||||
return (0);
|
||||
n = 0;
|
||||
p = (const char *)v->data;
|
||||
for (i = 0; i < v->length; i++) {
|
||||
if ((p[i] > '~') || ((p[i] < ' ') &&
|
||||
(p[i] != '\n') && (p[i] != '\r')))
|
||||
buf[n] = '.';
|
||||
else
|
||||
buf[n] = p[i];
|
||||
n++;
|
||||
if (n >= 80) {
|
||||
if (BIO_write(bp, buf, n) <= 0)
|
||||
return (0);
|
||||
n = 0;
|
||||
}
|
||||
}
|
||||
if (n > 0)
|
||||
if (BIO_write(bp, buf, n) <= 0)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
|
||||
{
|
||||
if (tm->type == V_ASN1_UTCTIME)
|
||||
return ASN1_UTCTIME_print(bp, tm);
|
||||
if (tm->type == V_ASN1_GENERALIZEDTIME)
|
||||
return ASN1_GENERALIZEDTIME_print(bp, tm);
|
||||
BIO_write(bp, "Bad time value", 14);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static const char *const mon[12] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
|
||||
int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
|
||||
{
|
||||
char *v;
|
||||
int gmt = 0;
|
||||
int i;
|
||||
int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
|
||||
char *f = NULL;
|
||||
int f_len = 0;
|
||||
|
||||
i = tm->length;
|
||||
v = (char *)tm->data;
|
||||
|
||||
if (i < 12)
|
||||
goto err;
|
||||
if (v[i - 1] == 'Z')
|
||||
gmt = 1;
|
||||
for (i = 0; i < 12; i++)
|
||||
if ((v[i] > '9') || (v[i] < '0'))
|
||||
goto err;
|
||||
y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 + (v[2] - '0') * 10 + (v[3] -
|
||||
'0');
|
||||
M = (v[4] - '0') * 10 + (v[5] - '0');
|
||||
if ((M > 12) || (M < 1))
|
||||
goto err;
|
||||
d = (v[6] - '0') * 10 + (v[7] - '0');
|
||||
h = (v[8] - '0') * 10 + (v[9] - '0');
|
||||
m = (v[10] - '0') * 10 + (v[11] - '0');
|
||||
if (tm->length >= 14 &&
|
||||
(v[12] >= '0') && (v[12] <= '9') &&
|
||||
(v[13] >= '0') && (v[13] <= '9')) {
|
||||
s = (v[12] - '0') * 10 + (v[13] - '0');
|
||||
/* Check for fractions of seconds. */
|
||||
if (tm->length >= 15 && v[14] == '.') {
|
||||
int l = tm->length;
|
||||
f = &v[14]; /* The decimal point. */
|
||||
f_len = 1;
|
||||
while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
|
||||
++f_len;
|
||||
}
|
||||
}
|
||||
|
||||
if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
|
||||
mon[M - 1], d, h, m, s, f_len, f, y,
|
||||
(gmt) ? " GMT" : "") <= 0)
|
||||
return (0);
|
||||
else
|
||||
return (1);
|
||||
err:
|
||||
BIO_write(bp, "Bad time value", 14);
|
||||
return (0);
|
||||
}
|
||||
|
||||
// consume_two_digits is a helper function for ASN1_UTCTIME_print. If |*v|,
|
||||
// assumed to be |*len| bytes long, has two leading digits, updates |*out| with
|
||||
// their value, updates |v| and |len|, and returns one. Otherwise, returns
|
||||
// zero.
|
||||
static int consume_two_digits(int* out, const char **v, int *len) {
|
||||
if (*len < 2 || !isdigit((unsigned char)((*v)[0])) ||
|
||||
!isdigit((unsigned char)((*v)[1]))) {
|
||||
if (v == NULL) {
|
||||
return 0;
|
||||
}
|
||||
*out = ((*v)[0] - '0') * 10 + ((*v)[1] - '0');
|
||||
*len -= 2;
|
||||
*v += 2;
|
||||
n = 0;
|
||||
p = (const char *)v->data;
|
||||
for (i = 0; i < v->length; i++) {
|
||||
if ((p[i] > '~') || ((p[i] < ' ') && (p[i] != '\n') && (p[i] != '\r'))) {
|
||||
buf[n] = '.';
|
||||
} else {
|
||||
buf[n] = p[i];
|
||||
}
|
||||
n++;
|
||||
if (n >= 80) {
|
||||
if (BIO_write(bp, buf, n) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
n = 0;
|
||||
}
|
||||
}
|
||||
if (n > 0) {
|
||||
if (BIO_write(bp, buf, n) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// consume_zulu_timezone is a helper function for ASN1_UTCTIME_print. If |*v|,
|
||||
// assumed to be |*len| bytes long, starts with "Z" then it updates |*v| and
|
||||
// |*len| and returns one. Otherwise returns zero.
|
||||
static int consume_zulu_timezone(const char **v, int *len) {
|
||||
if (*len == 0 || (*v)[0] != 'Z') {
|
||||
int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm) {
|
||||
if (tm->type == V_ASN1_UTCTIME) {
|
||||
return ASN1_UTCTIME_print(bp, tm);
|
||||
}
|
||||
if (tm->type == V_ASN1_GENERALIZEDTIME) {
|
||||
return ASN1_GENERALIZEDTIME_print(bp, tm);
|
||||
}
|
||||
BIO_puts(bp, "Bad time value");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char *const mon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
||||
|
||||
int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) {
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, tm->data, tm->length);
|
||||
struct tm utc;
|
||||
if (!CBS_parse_generalized_time(&cbs, &utc, /*allow_timezone_offset=*/0)) {
|
||||
BIO_puts(bp, "Bad time value");
|
||||
return 0;
|
||||
}
|
||||
|
||||
*len -= 1;
|
||||
*v += 1;
|
||||
return 1;
|
||||
return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", mon[utc.tm_mon],
|
||||
utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
|
||||
utc.tm_year + 1900) > 0;
|
||||
}
|
||||
|
||||
int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) {
|
||||
const char *v = (const char *)tm->data;
|
||||
int len = tm->length;
|
||||
int Y = 0, M = 0, D = 0, h = 0, m = 0, s = 0;
|
||||
|
||||
// YYMMDDhhmm are required to be present.
|
||||
if (!consume_two_digits(&Y, &v, &len) ||
|
||||
!consume_two_digits(&M, &v, &len) ||
|
||||
!consume_two_digits(&D, &v, &len) ||
|
||||
!consume_two_digits(&h, &v, &len) ||
|
||||
!consume_two_digits(&m, &v, &len)) {
|
||||
goto err;
|
||||
}
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, requires seconds
|
||||
// to be present, but historically this code has forgiven its absence.
|
||||
consume_two_digits(&s, &v, &len);
|
||||
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, specifies this
|
||||
// interpretation of the year.
|
||||
if (Y < 50) {
|
||||
Y += 2000;
|
||||
} else {
|
||||
Y += 1900;
|
||||
}
|
||||
if (M > 12 || M == 0) {
|
||||
goto err;
|
||||
}
|
||||
if (D > 31 || D == 0) {
|
||||
goto err;
|
||||
}
|
||||
if (h > 23 || m > 59 || s > 60) {
|
||||
goto err;
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, tm->data, tm->length);
|
||||
struct tm utc;
|
||||
if (!CBS_parse_utc_time(&cbs, &utc, /*allow_timezone_offset=*/0)) {
|
||||
BIO_puts(bp, "Bad time value");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, requires the "Z"
|
||||
// to be present, but historically this code has forgiven its absence.
|
||||
const int is_gmt = consume_zulu_timezone(&v, &len);
|
||||
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, does not permit
|
||||
// the specification of timezones using the +hhmm / -hhmm syntax, which is
|
||||
// the only other thing that might legitimately be found at the end.
|
||||
if (len) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s", mon[M - 1], D, h, m, s, Y,
|
||||
is_gmt ? " GMT" : "") > 0;
|
||||
|
||||
err:
|
||||
BIO_write(bp, "Bad time value", 14);
|
||||
return 0;
|
||||
return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", mon[utc.tm_mon],
|
||||
utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
|
||||
utc.tm_year + 1900) > 0;
|
||||
}
|
||||
@@ -74,68 +74,55 @@ DEFINE_LHASH_OF(ASN1_STRING_TABLE)
|
||||
static LHASH_OF(ASN1_STRING_TABLE) *string_tables = NULL;
|
||||
static struct CRYPTO_STATIC_MUTEX string_tables_lock = CRYPTO_STATIC_MUTEX_INIT;
|
||||
|
||||
void ASN1_STRING_set_default_mask(unsigned long mask)
|
||||
{
|
||||
}
|
||||
void ASN1_STRING_set_default_mask(unsigned long mask) {}
|
||||
|
||||
unsigned long ASN1_STRING_get_default_mask(void)
|
||||
{
|
||||
return B_ASN1_UTF8STRING;
|
||||
}
|
||||
unsigned long ASN1_STRING_get_default_mask(void) { return B_ASN1_UTF8STRING; }
|
||||
|
||||
int ASN1_STRING_set_default_mask_asc(const char *p)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
int ASN1_STRING_set_default_mask_asc(const char *p) { return 1; }
|
||||
|
||||
static const ASN1_STRING_TABLE *asn1_string_table_get(int nid);
|
||||
|
||||
/*
|
||||
* The following function generates an ASN1_STRING based on limits in a
|
||||
* table. Frequently the types and length of an ASN1_STRING are restricted by
|
||||
* a corresponding OID. For example certificates and certificate requests.
|
||||
*/
|
||||
// The following function generates an ASN1_STRING based on limits in a
|
||||
// table. Frequently the types and length of an ASN1_STRING are restricted by
|
||||
// a corresponding OID. For example certificates and certificate requests.
|
||||
|
||||
ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
|
||||
int len, int inform, int nid)
|
||||
{
|
||||
ASN1_STRING *str = NULL;
|
||||
int ret;
|
||||
if (!out) {
|
||||
out = &str;
|
||||
int len, int inform, int nid) {
|
||||
ASN1_STRING *str = NULL;
|
||||
int ret;
|
||||
if (!out) {
|
||||
out = &str;
|
||||
}
|
||||
const ASN1_STRING_TABLE *tbl = asn1_string_table_get(nid);
|
||||
if (tbl != NULL) {
|
||||
unsigned long mask = tbl->mask;
|
||||
if (!(tbl->flags & STABLE_NO_MASK)) {
|
||||
mask &= B_ASN1_UTF8STRING;
|
||||
}
|
||||
const ASN1_STRING_TABLE *tbl = asn1_string_table_get(nid);
|
||||
if (tbl != NULL) {
|
||||
unsigned long mask = tbl->mask;
|
||||
if (!(tbl->flags & STABLE_NO_MASK)) {
|
||||
mask &= B_ASN1_UTF8STRING;
|
||||
}
|
||||
ret = ASN1_mbstring_ncopy(out, in, len, inform, mask, tbl->minsize,
|
||||
tbl->maxsize);
|
||||
} else {
|
||||
ret = ASN1_mbstring_copy(out, in, len, inform, B_ASN1_UTF8STRING);
|
||||
}
|
||||
if (ret <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
return *out;
|
||||
ret = ASN1_mbstring_ncopy(out, in, len, inform, mask, tbl->minsize,
|
||||
tbl->maxsize);
|
||||
} else {
|
||||
ret = ASN1_mbstring_copy(out, in, len, inform, B_ASN1_UTF8STRING);
|
||||
}
|
||||
if (ret <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
return *out;
|
||||
}
|
||||
|
||||
/*
|
||||
* Now the tables and helper functions for the string table:
|
||||
*/
|
||||
// Now the tables and helper functions for the string table:
|
||||
|
||||
/* See RFC 5280. */
|
||||
#define ub_name 32768
|
||||
#define ub_common_name 64
|
||||
#define ub_locality_name 128
|
||||
#define ub_state_name 128
|
||||
#define ub_organization_name 64
|
||||
#define ub_organization_unit_name 64
|
||||
#define ub_email_address 128
|
||||
#define ub_serial_number 64
|
||||
// See RFC 5280.
|
||||
#define ub_name 32768
|
||||
#define ub_common_name 64
|
||||
#define ub_locality_name 128
|
||||
#define ub_state_name 128
|
||||
#define ub_organization_name 64
|
||||
#define ub_organization_unit_name 64
|
||||
#define ub_email_address 128
|
||||
#define ub_serial_number 64
|
||||
|
||||
/* This table must be kept in NID order */
|
||||
// This table must be kept in NID order
|
||||
|
||||
static const ASN1_STRING_TABLE tbl_standard[] = {
|
||||
{NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
|
||||
@@ -159,108 +146,100 @@ static const ASN1_STRING_TABLE tbl_standard[] = {
|
||||
{NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
|
||||
{NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
|
||||
{NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
|
||||
{NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
|
||||
};
|
||||
{NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}};
|
||||
|
||||
static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
|
||||
{
|
||||
if (a->nid < b->nid) {
|
||||
return -1;
|
||||
}
|
||||
if (a->nid > b->nid) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b) {
|
||||
if (a->nid < b->nid) {
|
||||
return -1;
|
||||
}
|
||||
if (a->nid > b->nid) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int table_cmp_void(const void *a, const void *b)
|
||||
{
|
||||
return table_cmp(a, b);
|
||||
static int table_cmp_void(const void *a, const void *b) {
|
||||
return table_cmp(a, b);
|
||||
}
|
||||
|
||||
static uint32_t table_hash(const ASN1_STRING_TABLE *tbl)
|
||||
{
|
||||
return OPENSSL_hash32(&tbl->nid, sizeof(tbl->nid));
|
||||
static uint32_t table_hash(const ASN1_STRING_TABLE *tbl) {
|
||||
return OPENSSL_hash32(&tbl->nid, sizeof(tbl->nid));
|
||||
}
|
||||
|
||||
static const ASN1_STRING_TABLE *asn1_string_table_get(int nid)
|
||||
{
|
||||
ASN1_STRING_TABLE key;
|
||||
key.nid = nid;
|
||||
const ASN1_STRING_TABLE *tbl =
|
||||
bsearch(&key, tbl_standard, OPENSSL_ARRAY_SIZE(tbl_standard),
|
||||
sizeof(ASN1_STRING_TABLE), table_cmp_void);
|
||||
if (tbl != NULL) {
|
||||
return tbl;
|
||||
}
|
||||
|
||||
CRYPTO_STATIC_MUTEX_lock_read(&string_tables_lock);
|
||||
if (string_tables != NULL) {
|
||||
tbl = lh_ASN1_STRING_TABLE_retrieve(string_tables, &key);
|
||||
}
|
||||
CRYPTO_STATIC_MUTEX_unlock_read(&string_tables_lock);
|
||||
/* Note returning |tbl| without the lock is only safe because
|
||||
* |ASN1_STRING_TABLE_add| cannot modify or delete existing entries. If we
|
||||
* wish to support that, this function must copy the result under a lock. */
|
||||
static const ASN1_STRING_TABLE *asn1_string_table_get(int nid) {
|
||||
ASN1_STRING_TABLE key;
|
||||
key.nid = nid;
|
||||
const ASN1_STRING_TABLE *tbl =
|
||||
bsearch(&key, tbl_standard, OPENSSL_ARRAY_SIZE(tbl_standard),
|
||||
sizeof(ASN1_STRING_TABLE), table_cmp_void);
|
||||
if (tbl != NULL) {
|
||||
return tbl;
|
||||
}
|
||||
|
||||
CRYPTO_STATIC_MUTEX_lock_read(&string_tables_lock);
|
||||
if (string_tables != NULL) {
|
||||
tbl = lh_ASN1_STRING_TABLE_retrieve(string_tables, &key);
|
||||
}
|
||||
CRYPTO_STATIC_MUTEX_unlock_read(&string_tables_lock);
|
||||
// Note returning |tbl| without the lock is only safe because
|
||||
// |ASN1_STRING_TABLE_add| cannot modify or delete existing entries. If we
|
||||
// wish to support that, this function must copy the result under a lock.
|
||||
return tbl;
|
||||
}
|
||||
|
||||
int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize,
|
||||
unsigned long mask, unsigned long flags)
|
||||
{
|
||||
/* Existing entries cannot be overwritten. */
|
||||
if (asn1_string_table_get(nid) != NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
unsigned long mask, unsigned long flags) {
|
||||
// Existing entries cannot be overwritten.
|
||||
if (asn1_string_table_get(nid) != NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
CRYPTO_STATIC_MUTEX_lock_write(&string_tables_lock);
|
||||
int ret = 0;
|
||||
CRYPTO_STATIC_MUTEX_lock_write(&string_tables_lock);
|
||||
|
||||
if (string_tables == NULL) {
|
||||
string_tables = lh_ASN1_STRING_TABLE_new(table_hash, table_cmp);
|
||||
if (string_tables == NULL) {
|
||||
string_tables = lh_ASN1_STRING_TABLE_new(table_hash, table_cmp);
|
||||
if (string_tables == NULL) {
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
/* Check again for an existing entry. One may have been added while
|
||||
* unlocked. */
|
||||
ASN1_STRING_TABLE key;
|
||||
key.nid = nid;
|
||||
if (lh_ASN1_STRING_TABLE_retrieve(string_tables, &key) != NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
goto err;
|
||||
}
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
// Check again for an existing entry. One may have been added while
|
||||
// unlocked.
|
||||
ASN1_STRING_TABLE key;
|
||||
key.nid = nid;
|
||||
if (lh_ASN1_STRING_TABLE_retrieve(string_tables, &key) != NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
ASN1_STRING_TABLE *tbl = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
|
||||
if (tbl == NULL) {
|
||||
goto err;
|
||||
}
|
||||
tbl->nid = nid;
|
||||
tbl->flags = flags;
|
||||
tbl->minsize = minsize;
|
||||
tbl->maxsize = maxsize;
|
||||
tbl->mask = mask;
|
||||
ASN1_STRING_TABLE *old_tbl;
|
||||
if (!lh_ASN1_STRING_TABLE_insert(string_tables, &old_tbl, tbl)) {
|
||||
OPENSSL_free(tbl);
|
||||
goto err;
|
||||
}
|
||||
assert(old_tbl == NULL);
|
||||
ret = 1;
|
||||
ASN1_STRING_TABLE *tbl = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
|
||||
if (tbl == NULL) {
|
||||
goto err;
|
||||
}
|
||||
tbl->nid = nid;
|
||||
tbl->flags = flags;
|
||||
tbl->minsize = minsize;
|
||||
tbl->maxsize = maxsize;
|
||||
tbl->mask = mask;
|
||||
ASN1_STRING_TABLE *old_tbl;
|
||||
if (!lh_ASN1_STRING_TABLE_insert(string_tables, &old_tbl, tbl)) {
|
||||
OPENSSL_free(tbl);
|
||||
goto err;
|
||||
}
|
||||
assert(old_tbl == NULL);
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
CRYPTO_STATIC_MUTEX_unlock_write(&string_tables_lock);
|
||||
return ret;
|
||||
CRYPTO_STATIC_MUTEX_unlock_write(&string_tables_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ASN1_STRING_TABLE_cleanup(void)
|
||||
{
|
||||
}
|
||||
void ASN1_STRING_TABLE_cleanup(void) {}
|
||||
|
||||
void asn1_get_string_table_for_testing(const ASN1_STRING_TABLE **out_ptr,
|
||||
size_t *out_len) {
|
||||
*out_ptr = tbl_standard;
|
||||
*out_len = OPENSSL_ARRAY_SIZE(tbl_standard);
|
||||
*out_ptr = tbl_standard;
|
||||
*out_len = OPENSSL_ARRAY_SIZE(tbl_standard);
|
||||
}
|
||||
@@ -65,148 +65,180 @@
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
* This is an implementation of the ASN1 Time structure which is: Time ::=
|
||||
* CHOICE { utcTime UTCTime, generalTime GeneralizedTime } written by Steve
|
||||
* Henson.
|
||||
*/
|
||||
// This is an implementation of the ASN1 Time structure which is: Time ::=
|
||||
// CHOICE { utcTime UTCTime, generalTime GeneralizedTime } written by Steve
|
||||
// Henson.
|
||||
|
||||
IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
|
||||
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const(ASN1_TIME)
|
||||
|
||||
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
|
||||
{
|
||||
return ASN1_TIME_adj(s, t, 0, 0);
|
||||
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t) {
|
||||
return ASN1_TIME_adj(s, t, 0, 0);
|
||||
}
|
||||
|
||||
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
|
||||
int offset_day, long offset_sec)
|
||||
{
|
||||
struct tm *ts;
|
||||
struct tm data;
|
||||
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm *ts;
|
||||
struct tm data;
|
||||
|
||||
ts = OPENSSL_gmtime(&t, &data);
|
||||
if (ts == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ERROR_GETTING_TIME);
|
||||
return NULL;
|
||||
}
|
||||
if (offset_day || offset_sec) {
|
||||
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
|
||||
return NULL;
|
||||
}
|
||||
if ((ts->tm_year >= 50) && (ts->tm_year < 150))
|
||||
return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
|
||||
return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
|
||||
}
|
||||
|
||||
int ASN1_TIME_check(const ASN1_TIME *t)
|
||||
{
|
||||
if (t->type == V_ASN1_GENERALIZEDTIME)
|
||||
return ASN1_GENERALIZEDTIME_check(t);
|
||||
else if (t->type == V_ASN1_UTCTIME)
|
||||
return ASN1_UTCTIME_check(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Convert an ASN1_TIME structure to GeneralizedTime */
|
||||
ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
|
||||
ASN1_GENERALIZEDTIME **out)
|
||||
{
|
||||
ASN1_GENERALIZEDTIME *ret = NULL;
|
||||
char *str;
|
||||
int newlen;
|
||||
|
||||
if (!ASN1_TIME_check(t))
|
||||
return NULL;
|
||||
|
||||
if (!out || !*out) {
|
||||
if (!(ret = ASN1_GENERALIZEDTIME_new()))
|
||||
goto err;
|
||||
} else {
|
||||
ret = *out;
|
||||
}
|
||||
|
||||
/* If already GeneralizedTime just copy across */
|
||||
if (t->type == V_ASN1_GENERALIZEDTIME) {
|
||||
if (!ASN1_STRING_set(ret, t->data, t->length))
|
||||
goto err;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* grow the string */
|
||||
if (!ASN1_STRING_set(ret, NULL, t->length + 2))
|
||||
goto err;
|
||||
/* ASN1_STRING_set() allocated 'len + 1' bytes. */
|
||||
newlen = t->length + 2 + 1;
|
||||
str = (char *)ret->data;
|
||||
/* Work out the century and prepend */
|
||||
if (t->data[0] >= '5')
|
||||
OPENSSL_strlcpy(str, "19", newlen);
|
||||
else
|
||||
OPENSSL_strlcpy(str, "20", newlen);
|
||||
|
||||
OPENSSL_strlcat(str, (char *)t->data, newlen);
|
||||
|
||||
done:
|
||||
if (out != NULL && *out == NULL)
|
||||
*out = ret;
|
||||
return ret;
|
||||
|
||||
err:
|
||||
if (out == NULL || *out != ret)
|
||||
ASN1_GENERALIZEDTIME_free(ret);
|
||||
ts = OPENSSL_gmtime(&t, &data);
|
||||
if (ts == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ERROR_GETTING_TIME);
|
||||
return NULL;
|
||||
}
|
||||
if (offset_day || offset_sec) {
|
||||
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if ((ts->tm_year >= 50) && (ts->tm_year < 150)) {
|
||||
return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
|
||||
}
|
||||
return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
|
||||
}
|
||||
|
||||
int ASN1_TIME_check(const ASN1_TIME *t) {
|
||||
if (t->type == V_ASN1_GENERALIZEDTIME) {
|
||||
return ASN1_GENERALIZEDTIME_check(t);
|
||||
} else if (t->type == V_ASN1_UTCTIME) {
|
||||
return ASN1_UTCTIME_check(t);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Convert an ASN1_TIME structure to GeneralizedTime
|
||||
ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
|
||||
ASN1_GENERALIZEDTIME **out) {
|
||||
ASN1_GENERALIZEDTIME *ret = NULL;
|
||||
char *str;
|
||||
int newlen;
|
||||
|
||||
if (!ASN1_TIME_check(t)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!out || !*out) {
|
||||
if (!(ret = ASN1_GENERALIZEDTIME_new())) {
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
ret = *out;
|
||||
}
|
||||
|
||||
// If already GeneralizedTime just copy across
|
||||
if (t->type == V_ASN1_GENERALIZEDTIME) {
|
||||
if (!ASN1_STRING_set(ret, t->data, t->length)) {
|
||||
goto err;
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
|
||||
// grow the string
|
||||
if (!ASN1_STRING_set(ret, NULL, t->length + 2)) {
|
||||
goto err;
|
||||
}
|
||||
// ASN1_STRING_set() allocated 'len + 1' bytes.
|
||||
newlen = t->length + 2 + 1;
|
||||
str = (char *)ret->data;
|
||||
// Work out the century and prepend
|
||||
if (t->data[0] >= '5') {
|
||||
OPENSSL_strlcpy(str, "19", newlen);
|
||||
} else {
|
||||
OPENSSL_strlcpy(str, "20", newlen);
|
||||
}
|
||||
|
||||
OPENSSL_strlcat(str, (char *)t->data, newlen);
|
||||
|
||||
done:
|
||||
if (out != NULL && *out == NULL) {
|
||||
*out = ret;
|
||||
}
|
||||
return ret;
|
||||
|
||||
err:
|
||||
if (out == NULL || *out != ret) {
|
||||
ASN1_GENERALIZEDTIME_free(ret);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
|
||||
{
|
||||
ASN1_TIME t;
|
||||
int ASN1_TIME_set_string(ASN1_TIME *s, const char *str) {
|
||||
ASN1_TIME t;
|
||||
|
||||
t.length = strlen(str);
|
||||
t.data = (unsigned char *)str;
|
||||
t.flags = 0;
|
||||
t.length = strlen(str);
|
||||
t.data = (unsigned char *)str;
|
||||
t.flags = 0;
|
||||
|
||||
t.type = V_ASN1_UTCTIME;
|
||||
t.type = V_ASN1_UTCTIME;
|
||||
|
||||
if (!ASN1_TIME_check(&t)) {
|
||||
t.type = V_ASN1_GENERALIZEDTIME;
|
||||
if (!ASN1_TIME_check(&t)) {
|
||||
t.type = V_ASN1_GENERALIZEDTIME;
|
||||
if (!ASN1_TIME_check(&t))
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t)
|
||||
{
|
||||
if (t == NULL) {
|
||||
time_t now_t;
|
||||
time(&now_t);
|
||||
if (OPENSSL_gmtime(&now_t, tm))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (t->type == V_ASN1_UTCTIME)
|
||||
return asn1_utctime_to_tm(tm, t);
|
||||
else if (t->type == V_ASN1_GENERALIZEDTIME)
|
||||
return asn1_generalizedtime_to_tm(tm, t);
|
||||
|
||||
if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
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))
|
||||
return 0;
|
||||
if (!asn1_time_to_tm(&tm_to, to))
|
||||
return 0;
|
||||
return OPENSSL_gmtime_diff(out_days, out_seconds, &tm_from, &tm_to);
|
||||
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);
|
||||
if (OPENSSL_gmtime(&now_t, tm)) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (t->type == V_ASN1_UTCTIME) {
|
||||
return asn1_utctime_to_tm(tm, t, allow_timezone_offset);
|
||||
} else if (t->type == V_ASN1_GENERALIZEDTIME) {
|
||||
return asn1_generalizedtime_to_tm(tm, t);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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, /*allow_timezone_offset=*/1)) {
|
||||
return 0;
|
||||
}
|
||||
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);
|
||||
}
|
||||
@@ -64,76 +64,75 @@
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_TYPE_get(const ASN1_TYPE *a)
|
||||
{
|
||||
if (a->type == V_ASN1_BOOLEAN || a->type == V_ASN1_NULL ||
|
||||
a->value.ptr != NULL) {
|
||||
return a->type;
|
||||
}
|
||||
return 0;
|
||||
int ASN1_TYPE_get(const ASN1_TYPE *a) {
|
||||
if (a->type == V_ASN1_BOOLEAN || a->type == V_ASN1_NULL ||
|
||||
a->value.ptr != NULL) {
|
||||
return a->type;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a)
|
||||
{
|
||||
if (a->type == V_ASN1_BOOLEAN) {
|
||||
return a->value.boolean ? (void *)0xff : NULL;
|
||||
}
|
||||
if (a->type == V_ASN1_NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return a->value.ptr;
|
||||
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a) {
|
||||
if (a->type == V_ASN1_BOOLEAN) {
|
||||
return a->value.boolean ? (void *)0xff : NULL;
|
||||
}
|
||||
if (a->type == V_ASN1_NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return a->value.ptr;
|
||||
}
|
||||
|
||||
void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
|
||||
{
|
||||
ASN1_TYPE **tmp_a = &a;
|
||||
ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
|
||||
a->type = type;
|
||||
if (type == V_ASN1_BOOLEAN)
|
||||
a->value.boolean = value ? 0xff : 0;
|
||||
else
|
||||
a->value.ptr = value;
|
||||
void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value) {
|
||||
ASN1_TYPE **tmp_a = &a;
|
||||
ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
|
||||
a->type = type;
|
||||
if (type == V_ASN1_BOOLEAN) {
|
||||
a->value.boolean = value ? 0xff : 0;
|
||||
} else {
|
||||
a->value.ptr = value;
|
||||
}
|
||||
}
|
||||
|
||||
int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value)
|
||||
{
|
||||
if (!value || (type == V_ASN1_BOOLEAN)) {
|
||||
void *p = (void *)value;
|
||||
ASN1_TYPE_set(a, type, p);
|
||||
} else if (type == V_ASN1_OBJECT) {
|
||||
ASN1_OBJECT *odup;
|
||||
odup = OBJ_dup(value);
|
||||
if (!odup)
|
||||
return 0;
|
||||
ASN1_TYPE_set(a, type, odup);
|
||||
} else {
|
||||
ASN1_STRING *sdup;
|
||||
sdup = ASN1_STRING_dup(value);
|
||||
if (!sdup)
|
||||
return 0;
|
||||
ASN1_TYPE_set(a, type, sdup);
|
||||
int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value) {
|
||||
if (!value || (type == V_ASN1_BOOLEAN)) {
|
||||
void *p = (void *)value;
|
||||
ASN1_TYPE_set(a, type, p);
|
||||
} else if (type == V_ASN1_OBJECT) {
|
||||
ASN1_OBJECT *odup;
|
||||
odup = OBJ_dup(value);
|
||||
if (!odup) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
ASN1_TYPE_set(a, type, odup);
|
||||
} else {
|
||||
ASN1_STRING *sdup;
|
||||
sdup = ASN1_STRING_dup(value);
|
||||
if (!sdup) {
|
||||
return 0;
|
||||
}
|
||||
ASN1_TYPE_set(a, type, sdup);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Returns 0 if they are equal, != 0 otherwise. */
|
||||
int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b)
|
||||
{
|
||||
int result = -1;
|
||||
// Returns 0 if they are equal, != 0 otherwise.
|
||||
int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b) {
|
||||
int result = -1;
|
||||
|
||||
if (!a || !b || a->type != b->type)
|
||||
return -1;
|
||||
if (!a || !b || a->type != b->type) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (a->type) {
|
||||
switch (a->type) {
|
||||
case V_ASN1_OBJECT:
|
||||
result = OBJ_cmp(a->value.object, b->value.object);
|
||||
break;
|
||||
result = OBJ_cmp(a->value.object, b->value.object);
|
||||
break;
|
||||
case V_ASN1_NULL:
|
||||
result = 0; /* They do not have content. */
|
||||
break;
|
||||
result = 0; // They do not have content.
|
||||
break;
|
||||
case V_ASN1_BOOLEAN:
|
||||
result = a->value.boolean - b->value.boolean;
|
||||
break;
|
||||
result = a->value.boolean - b->value.boolean;
|
||||
break;
|
||||
case V_ASN1_INTEGER:
|
||||
case V_ASN1_ENUMERATED:
|
||||
case V_ASN1_BIT_STRING:
|
||||
@@ -155,9 +154,9 @@ int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b)
|
||||
case V_ASN1_UTF8STRING:
|
||||
case V_ASN1_OTHER:
|
||||
default:
|
||||
result = ASN1_STRING_cmp(a->value.asn1_string, b->value.asn1_string);
|
||||
break;
|
||||
}
|
||||
result = ASN1_STRING_cmp(a->value.asn1_string, b->value.asn1_string);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
@@ -64,121 +64,114 @@
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
|
||||
{
|
||||
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)) {
|
||||
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)) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_UTCTIME_check(const ASN1_UTCTIME *d) {
|
||||
return asn1_utctime_to_tm(NULL, d, /*allow_timezone_offset=*/1);
|
||||
}
|
||||
|
||||
int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) {
|
||||
ASN1_UTCTIME t;
|
||||
|
||||
t.type = V_ASN1_UTCTIME;
|
||||
t.length = strlen(str);
|
||||
t.data = (unsigned char *)str;
|
||||
if (ASN1_UTCTIME_check(&t)) {
|
||||
if (s != NULL) {
|
||||
if (!ASN1_STRING_set((ASN1_STRING *)s, (unsigned char *)str, t.length)) {
|
||||
return 0;
|
||||
}
|
||||
s->type = V_ASN1_UTCTIME;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
|
||||
{
|
||||
return asn1_utctime_to_tm(NULL, d);
|
||||
}
|
||||
|
||||
int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
|
||||
{
|
||||
ASN1_UTCTIME t;
|
||||
|
||||
t.type = V_ASN1_UTCTIME;
|
||||
t.length = strlen(str);
|
||||
t.data = (unsigned char *)str;
|
||||
if (ASN1_UTCTIME_check(&t)) {
|
||||
if (s != NULL) {
|
||||
if (!ASN1_STRING_set((ASN1_STRING *)s,
|
||||
(unsigned char *)str, t.length))
|
||||
return 0;
|
||||
s->type = V_ASN1_UTCTIME;
|
||||
}
|
||||
return (1);
|
||||
} else
|
||||
return (0);
|
||||
}
|
||||
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
|
||||
{
|
||||
return ASN1_UTCTIME_adj(s, t, 0, 0);
|
||||
}
|
||||
|
||||
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 (offset_day || offset_sec) {
|
||||
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
|
||||
goto err;
|
||||
}
|
||||
|
||||
if ((ts->tm_year < 50) || (ts->tm_year >= 150))
|
||||
goto err;
|
||||
|
||||
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;
|
||||
}
|
||||
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);
|
||||
s->type = V_ASN1_UTCTIME;
|
||||
return (s);
|
||||
err:
|
||||
if (free_s && 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))
|
||||
return -2;
|
||||
|
||||
if (!OPENSSL_gmtime(&t, &ttm))
|
||||
return -2;
|
||||
|
||||
if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
|
||||
return -2;
|
||||
|
||||
if (day > 0)
|
||||
return 1;
|
||||
if (day < 0)
|
||||
return -1;
|
||||
if (sec > 0)
|
||||
return 1;
|
||||
if (sec < 0)
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t) {
|
||||
return ASN1_UTCTIME_adj(s, t, 0, 0);
|
||||
}
|
||||
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm data;
|
||||
if (!OPENSSL_gmtime(&t, &data)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (offset_day || offset_sec) {
|
||||
if (!OPENSSL_gmtime_adj(&data, offset_day, offset_sec)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (data.tm_year < 50 || data.tm_year >= 150) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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 (!ASN1_STRING_set(s, buf, strlen(buf))) {
|
||||
if (free_s) {
|
||||
ASN1_UTCTIME_free(s);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
s->type = V_ASN1_UTCTIME;
|
||||
return s;
|
||||
}
|
||||
|
||||
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, /*allow_timezone_offset=*/1)) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (!OPENSSL_gmtime(&t, &ttm)) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm)) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (day > 0) {
|
||||
return 1;
|
||||
}
|
||||
if (day < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (sec > 0) {
|
||||
return 1;
|
||||
}
|
||||
if (sec < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -61,176 +61,82 @@
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
/* UTF8 utilities */
|
||||
// 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).
|
||||
*/
|
||||
// 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
|
||||
// in which case it just returns the number of characters. It will need at
|
||||
// most 6 characters.
|
||||
|
||||
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
|
||||
* in which case it just returns the number of characters. It will need at
|
||||
* most 6 characters.
|
||||
*/
|
||||
|
||||
int UTF8_putc(unsigned char *str, int len, uint32_t value)
|
||||
{
|
||||
if (!str)
|
||||
len = 6; /* Maximum we will need */
|
||||
else if (len <= 0)
|
||||
return -1;
|
||||
if (value < 0x80) {
|
||||
if (str)
|
||||
*str = (unsigned char)value;
|
||||
return 1;
|
||||
}
|
||||
if (value < 0x800) {
|
||||
if (len < 2)
|
||||
return -1;
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
if (value < 0x10000) {
|
||||
if (len < 3)
|
||||
return -1;
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
if (value < 0x200000) {
|
||||
if (len < 4)
|
||||
return -1;
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
|
||||
*str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
}
|
||||
return 4;
|
||||
}
|
||||
if (value < 0x4000000) {
|
||||
if (len < 5)
|
||||
return -1;
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
|
||||
*str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
}
|
||||
return 5;
|
||||
}
|
||||
if (len < 6)
|
||||
return -1;
|
||||
int UTF8_putc(unsigned char *str, int len, uint32_t value) {
|
||||
if (!str) {
|
||||
len = 6; // Maximum we will need
|
||||
} else if (len <= 0) {
|
||||
return -1;
|
||||
}
|
||||
if (value < 0x80) {
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
|
||||
*str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
*str = (unsigned char)value;
|
||||
}
|
||||
return 6;
|
||||
return 1;
|
||||
}
|
||||
if (value < 0x800) {
|
||||
if (len < 2) {
|
||||
return -1;
|
||||
}
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
if (value < 0x10000) {
|
||||
if (len < 3) {
|
||||
return -1;
|
||||
}
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
if (value < 0x200000) {
|
||||
if (len < 4) {
|
||||
return -1;
|
||||
}
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
|
||||
*str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
}
|
||||
return 4;
|
||||
}
|
||||
if (value < 0x4000000) {
|
||||
if (len < 5) {
|
||||
return -1;
|
||||
}
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
|
||||
*str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
}
|
||||
return 5;
|
||||
}
|
||||
if (len < 6) {
|
||||
return -1;
|
||||
}
|
||||
if (str) {
|
||||
*str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
|
||||
*str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
|
||||
*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
|
||||
*str = (unsigned char)((value & 0x3f) | 0x80);
|
||||
}
|
||||
return 6;
|
||||
}
|
||||
@@ -67,19 +67,17 @@
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
/* Cross-module errors from crypto/x509/i2d_pr.c. */
|
||||
// Cross-module errors from crypto/x509/i2d_pr.c.
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_PUBLIC_KEY_TYPE)
|
||||
|
||||
/* Cross-module errors from crypto/x509/algorithm.c. */
|
||||
// Cross-module errors from crypto/x509/algorithm.c.
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, CONTEXT_NOT_INITIALISED)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, DIGEST_AND_KEY_TYPE_NOT_SUPPORTED)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_MESSAGE_DIGEST_ALGORITHM)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_SIGNATURE_ALGORITHM)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, WRONG_PUBLIC_KEY_TYPE)
|
||||
/*
|
||||
* Cross-module errors from crypto/x509/asn1_gen.c. TODO(davidben): Remove
|
||||
* these once asn1_gen.c is gone.
|
||||
*/
|
||||
// Cross-module errors from crypto/x509/asn1_gen.c. TODO(davidben): Remove
|
||||
// these once asn1_gen.c is gone.
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, DEPTH_EXCEEDED)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BITSTRING_FORMAT)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BOOLEAN)
|
||||
@@ -107,299 +105,286 @@ OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE)
|
||||
static void asn1_put_length(unsigned char **pp, int length);
|
||||
|
||||
int ASN1_get_object(const unsigned char **inp, long *out_len, int *out_tag,
|
||||
int *out_class, long in_len)
|
||||
{
|
||||
if (in_len < 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return 0x80;
|
||||
}
|
||||
int *out_class, long in_len) {
|
||||
if (in_len < 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return 0x80;
|
||||
}
|
||||
|
||||
/* TODO(https://crbug.com/boringssl/354): This should use |CBS_get_asn1| to
|
||||
* reject non-minimal lengths, which are only allowed in BER. However,
|
||||
* Android sometimes needs allow a non-minimal length in certificate
|
||||
* signature fields (see b/18228011). Make this only apply to that field,
|
||||
* while requiring DER elsewhere. Better yet, it should be limited to an
|
||||
* preprocessing step in that part of Android. */
|
||||
unsigned tag;
|
||||
size_t header_len;
|
||||
int indefinite;
|
||||
CBS cbs, body;
|
||||
CBS_init(&cbs, *inp, (size_t)in_len);
|
||||
if (!CBS_get_any_ber_asn1_element(&cbs, &body, &tag, &header_len,
|
||||
/*out_ber_found=*/NULL, &indefinite) ||
|
||||
indefinite ||
|
||||
!CBS_skip(&body, header_len) ||
|
||||
/* Bound the length to comfortably fit in an int. Lengths in this
|
||||
* module often switch between int and long without overflow checks. */
|
||||
CBS_len(&body) > INT_MAX / 2) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return 0x80;
|
||||
}
|
||||
// TODO(https://crbug.com/boringssl/354): This should use |CBS_get_asn1| to
|
||||
// reject non-minimal lengths, which are only allowed in BER. However,
|
||||
// Android sometimes needs allow a non-minimal length in certificate
|
||||
// signature fields (see b/18228011). Make this only apply to that field,
|
||||
// while requiring DER elsewhere. Better yet, it should be limited to an
|
||||
// preprocessing step in that part of Android.
|
||||
unsigned tag;
|
||||
size_t header_len;
|
||||
int indefinite;
|
||||
CBS cbs, body;
|
||||
CBS_init(&cbs, *inp, (size_t)in_len);
|
||||
if (!CBS_get_any_ber_asn1_element(&cbs, &body, &tag, &header_len,
|
||||
/*out_ber_found=*/NULL, &indefinite) ||
|
||||
indefinite || !CBS_skip(&body, header_len) ||
|
||||
// Bound the length to comfortably fit in an int. Lengths in this
|
||||
// module often switch between int and long without overflow checks.
|
||||
CBS_len(&body) > INT_MAX / 2) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return 0x80;
|
||||
}
|
||||
|
||||
/* Convert between tag representations. */
|
||||
int tag_class = (tag & CBS_ASN1_CLASS_MASK) >> CBS_ASN1_TAG_SHIFT;
|
||||
int constructed = (tag & CBS_ASN1_CONSTRUCTED) >> CBS_ASN1_TAG_SHIFT;
|
||||
int tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK;
|
||||
// Convert between tag representations.
|
||||
int tag_class = (tag & CBS_ASN1_CLASS_MASK) >> CBS_ASN1_TAG_SHIFT;
|
||||
int constructed = (tag & CBS_ASN1_CONSTRUCTED) >> CBS_ASN1_TAG_SHIFT;
|
||||
int tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK;
|
||||
|
||||
/* To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. */
|
||||
if (tag_class == V_ASN1_UNIVERSAL && tag_number > V_ASN1_MAX_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return 0x80;
|
||||
}
|
||||
// To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags.
|
||||
if (tag_class == V_ASN1_UNIVERSAL && tag_number > V_ASN1_MAX_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return 0x80;
|
||||
}
|
||||
|
||||
*inp = CBS_data(&body);
|
||||
*out_len = CBS_len(&body);
|
||||
*out_tag = tag_number;
|
||||
*out_class = tag_class;
|
||||
return constructed;
|
||||
*inp = CBS_data(&body);
|
||||
*out_len = CBS_len(&body);
|
||||
*out_tag = tag_number;
|
||||
*out_class = tag_class;
|
||||
return constructed;
|
||||
}
|
||||
|
||||
/*
|
||||
* class 0 is constructed constructed == 2 for indefinite length constructed
|
||||
*/
|
||||
// class 0 is constructed constructed == 2 for indefinite length constructed
|
||||
void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
|
||||
int xclass)
|
||||
{
|
||||
unsigned char *p = *pp;
|
||||
int i, ttag;
|
||||
int xclass) {
|
||||
unsigned char *p = *pp;
|
||||
int i, ttag;
|
||||
|
||||
i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
|
||||
i |= (xclass & V_ASN1_PRIVATE);
|
||||
if (tag < 31)
|
||||
*(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
|
||||
else {
|
||||
*(p++) = i | V_ASN1_PRIMITIVE_TAG;
|
||||
for (i = 0, ttag = tag; ttag > 0; i++)
|
||||
ttag >>= 7;
|
||||
ttag = i;
|
||||
while (i-- > 0) {
|
||||
p[i] = tag & 0x7f;
|
||||
if (i != (ttag - 1))
|
||||
p[i] |= 0x80;
|
||||
tag >>= 7;
|
||||
}
|
||||
p += ttag;
|
||||
i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
|
||||
i |= (xclass & V_ASN1_PRIVATE);
|
||||
if (tag < 31) {
|
||||
*(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
|
||||
} else {
|
||||
*(p++) = i | V_ASN1_PRIMITIVE_TAG;
|
||||
for (i = 0, ttag = tag; ttag > 0; i++) {
|
||||
ttag >>= 7;
|
||||
}
|
||||
if (constructed == 2)
|
||||
*(p++) = 0x80;
|
||||
else
|
||||
asn1_put_length(&p, length);
|
||||
*pp = p;
|
||||
ttag = i;
|
||||
while (i-- > 0) {
|
||||
p[i] = tag & 0x7f;
|
||||
if (i != (ttag - 1)) {
|
||||
p[i] |= 0x80;
|
||||
}
|
||||
tag >>= 7;
|
||||
}
|
||||
p += ttag;
|
||||
}
|
||||
if (constructed == 2) {
|
||||
*(p++) = 0x80;
|
||||
} else {
|
||||
asn1_put_length(&p, length);
|
||||
}
|
||||
*pp = p;
|
||||
}
|
||||
|
||||
int ASN1_put_eoc(unsigned char **pp)
|
||||
{
|
||||
/* This function is no longer used in the library, but some external code
|
||||
* uses it. */
|
||||
unsigned char *p = *pp;
|
||||
*p++ = 0;
|
||||
*p++ = 0;
|
||||
*pp = p;
|
||||
return 2;
|
||||
int ASN1_put_eoc(unsigned char **pp) {
|
||||
// This function is no longer used in the library, but some external code
|
||||
// uses it.
|
||||
unsigned char *p = *pp;
|
||||
*p++ = 0;
|
||||
*p++ = 0;
|
||||
*pp = p;
|
||||
return 2;
|
||||
}
|
||||
|
||||
static void asn1_put_length(unsigned char **pp, int length)
|
||||
{
|
||||
unsigned char *p = *pp;
|
||||
int i, l;
|
||||
if (length <= 127)
|
||||
*(p++) = (unsigned char)length;
|
||||
else {
|
||||
l = length;
|
||||
for (i = 0; l > 0; i++)
|
||||
l >>= 8;
|
||||
*(p++) = i | 0x80;
|
||||
l = i;
|
||||
while (i-- > 0) {
|
||||
p[i] = length & 0xff;
|
||||
length >>= 8;
|
||||
}
|
||||
p += l;
|
||||
static void asn1_put_length(unsigned char **pp, int length) {
|
||||
unsigned char *p = *pp;
|
||||
int i, l;
|
||||
if (length <= 127) {
|
||||
*(p++) = (unsigned char)length;
|
||||
} else {
|
||||
l = length;
|
||||
for (i = 0; l > 0; i++) {
|
||||
l >>= 8;
|
||||
}
|
||||
*pp = p;
|
||||
*(p++) = i | 0x80;
|
||||
l = i;
|
||||
while (i-- > 0) {
|
||||
p[i] = length & 0xff;
|
||||
length >>= 8;
|
||||
}
|
||||
p += l;
|
||||
}
|
||||
*pp = p;
|
||||
}
|
||||
|
||||
int ASN1_object_size(int constructed, int length, int tag)
|
||||
{
|
||||
int ret = 1;
|
||||
if (length < 0)
|
||||
return -1;
|
||||
if (tag >= 31) {
|
||||
while (tag > 0) {
|
||||
tag >>= 7;
|
||||
ret++;
|
||||
}
|
||||
int ASN1_object_size(int constructed, int length, int tag) {
|
||||
int ret = 1;
|
||||
if (length < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (tag >= 31) {
|
||||
while (tag > 0) {
|
||||
tag >>= 7;
|
||||
ret++;
|
||||
}
|
||||
if (constructed == 2) {
|
||||
ret += 3;
|
||||
} else {
|
||||
}
|
||||
if (constructed == 2) {
|
||||
ret += 3;
|
||||
} else {
|
||||
ret++;
|
||||
if (length > 127) {
|
||||
int tmplen = length;
|
||||
while (tmplen > 0) {
|
||||
tmplen >>= 8;
|
||||
ret++;
|
||||
if (length > 127) {
|
||||
int tmplen = length;
|
||||
while (tmplen > 0) {
|
||||
tmplen >>= 8;
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret >= INT_MAX - length)
|
||||
return -1;
|
||||
return ret + length;
|
||||
}
|
||||
if (ret >= INT_MAX - length) {
|
||||
return -1;
|
||||
}
|
||||
return ret + length;
|
||||
}
|
||||
|
||||
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
|
||||
{
|
||||
if (str == NULL)
|
||||
return 0;
|
||||
if (!ASN1_STRING_set(dst, str->data, str->length))
|
||||
return 0;
|
||||
dst->type = str->type;
|
||||
dst->flags = str->flags;
|
||||
return 1;
|
||||
}
|
||||
|
||||
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
|
||||
{
|
||||
ASN1_STRING *ret;
|
||||
if (!str)
|
||||
return NULL;
|
||||
ret = ASN1_STRING_new();
|
||||
if (!ret)
|
||||
return NULL;
|
||||
if (!ASN1_STRING_copy(ret, str)) {
|
||||
ASN1_STRING_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
|
||||
{
|
||||
unsigned char *c;
|
||||
const char *data = _data;
|
||||
|
||||
if (len < 0) {
|
||||
if (data == NULL)
|
||||
return (0);
|
||||
else
|
||||
len = strlen(data);
|
||||
}
|
||||
if ((str->length <= len) || (str->data == NULL)) {
|
||||
c = str->data;
|
||||
if (c == NULL)
|
||||
str->data = OPENSSL_malloc(len + 1);
|
||||
else
|
||||
str->data = OPENSSL_realloc(c, len + 1);
|
||||
|
||||
if (str->data == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
str->data = c;
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
str->length = len;
|
||||
if (data != NULL) {
|
||||
OPENSSL_memcpy(str->data, data, len);
|
||||
/* an allowance for strings :-) */
|
||||
str->data[len] = '\0';
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
|
||||
{
|
||||
OPENSSL_free(str->data);
|
||||
str->data = data;
|
||||
str->length = len;
|
||||
}
|
||||
|
||||
ASN1_STRING *ASN1_STRING_new(void)
|
||||
{
|
||||
return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
|
||||
}
|
||||
|
||||
ASN1_STRING *ASN1_STRING_type_new(int type)
|
||||
{
|
||||
ASN1_STRING *ret;
|
||||
|
||||
ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return (NULL);
|
||||
}
|
||||
ret->length = 0;
|
||||
ret->type = type;
|
||||
ret->data = NULL;
|
||||
ret->flags = 0;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void ASN1_STRING_free(ASN1_STRING *str)
|
||||
{
|
||||
if (str == NULL)
|
||||
return;
|
||||
OPENSSL_free(str->data);
|
||||
OPENSSL_free(str);
|
||||
}
|
||||
|
||||
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
|
||||
{
|
||||
/* Capture padding bits and implicit truncation in BIT STRINGs. */
|
||||
int a_length = a->length, b_length = b->length;
|
||||
uint8_t a_padding = 0, b_padding = 0;
|
||||
if (a->type == V_ASN1_BIT_STRING) {
|
||||
a_length = asn1_bit_string_length(a, &a_padding);
|
||||
}
|
||||
if (b->type == V_ASN1_BIT_STRING) {
|
||||
b_length = asn1_bit_string_length(b, &b_padding);
|
||||
}
|
||||
|
||||
if (a_length < b_length) {
|
||||
return -1;
|
||||
}
|
||||
if (a_length > b_length) {
|
||||
return 1;
|
||||
}
|
||||
/* In a BIT STRING, the number of bits is 8 * length - padding. Invert this
|
||||
* comparison so we compare by lengths. */
|
||||
if (a_padding > b_padding) {
|
||||
return -1;
|
||||
}
|
||||
if (a_padding < b_padding) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ret = OPENSSL_memcmp(a->data, b->data, a_length);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Comparing the type first is more natural, but this matches OpenSSL. */
|
||||
if (a->type < b->type) {
|
||||
return -1;
|
||||
}
|
||||
if (a->type > b->type) {
|
||||
return 1;
|
||||
}
|
||||
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) {
|
||||
if (str == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if (!ASN1_STRING_set(dst, str->data, str->length)) {
|
||||
return 0;
|
||||
}
|
||||
dst->type = str->type;
|
||||
dst->flags = str->flags;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_STRING_length(const ASN1_STRING *str)
|
||||
{
|
||||
return str->length;
|
||||
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) {
|
||||
ASN1_STRING *ret;
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
ret = ASN1_STRING_new();
|
||||
if (!ret) {
|
||||
return NULL;
|
||||
}
|
||||
if (!ASN1_STRING_copy(ret, str)) {
|
||||
ASN1_STRING_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ASN1_STRING_type(const ASN1_STRING *str)
|
||||
{
|
||||
return str->type;
|
||||
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) {
|
||||
unsigned char *c;
|
||||
const char *data = _data;
|
||||
|
||||
if (len < 0) {
|
||||
if (data == NULL) {
|
||||
return 0;
|
||||
} else {
|
||||
len = strlen(data);
|
||||
}
|
||||
}
|
||||
if ((str->length <= len) || (str->data == NULL)) {
|
||||
c = str->data;
|
||||
if (c == NULL) {
|
||||
str->data = OPENSSL_malloc(len + 1);
|
||||
} else {
|
||||
str->data = OPENSSL_realloc(c, len + 1);
|
||||
}
|
||||
|
||||
if (str->data == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
str->data = c;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
str->length = len;
|
||||
if (data != NULL) {
|
||||
OPENSSL_memcpy(str->data, data, len);
|
||||
// an allowance for strings :-)
|
||||
str->data[len] = '\0';
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char *ASN1_STRING_data(ASN1_STRING *str)
|
||||
{
|
||||
return str->data;
|
||||
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) {
|
||||
OPENSSL_free(str->data);
|
||||
str->data = data;
|
||||
str->length = len;
|
||||
}
|
||||
|
||||
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *str)
|
||||
{
|
||||
return str->data;
|
||||
ASN1_STRING *ASN1_STRING_new(void) {
|
||||
return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
|
||||
}
|
||||
|
||||
ASN1_STRING *ASN1_STRING_type_new(int type) {
|
||||
ASN1_STRING *ret;
|
||||
|
||||
ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
ret->length = 0;
|
||||
ret->type = type;
|
||||
ret->data = NULL;
|
||||
ret->flags = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ASN1_STRING_free(ASN1_STRING *str) {
|
||||
if (str == NULL) {
|
||||
return;
|
||||
}
|
||||
OPENSSL_free(str->data);
|
||||
OPENSSL_free(str);
|
||||
}
|
||||
|
||||
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) {
|
||||
// Capture padding bits and implicit truncation in BIT STRINGs.
|
||||
int a_length = a->length, b_length = b->length;
|
||||
uint8_t a_padding = 0, b_padding = 0;
|
||||
if (a->type == V_ASN1_BIT_STRING) {
|
||||
a_length = asn1_bit_string_length(a, &a_padding);
|
||||
}
|
||||
if (b->type == V_ASN1_BIT_STRING) {
|
||||
b_length = asn1_bit_string_length(b, &b_padding);
|
||||
}
|
||||
|
||||
if (a_length < b_length) {
|
||||
return -1;
|
||||
}
|
||||
if (a_length > b_length) {
|
||||
return 1;
|
||||
}
|
||||
// In a BIT STRING, the number of bits is 8 * length - padding. Invert this
|
||||
// comparison so we compare by lengths.
|
||||
if (a_padding > b_padding) {
|
||||
return -1;
|
||||
}
|
||||
if (a_padding < b_padding) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ret = OPENSSL_memcmp(a->data, b->data, a_length);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Comparing the type first is more natural, but this matches OpenSSL.
|
||||
if (a->type < b->type) {
|
||||
return -1;
|
||||
}
|
||||
if (a->type > b->type) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ASN1_STRING_length(const ASN1_STRING *str) { return str->length; }
|
||||
|
||||
int ASN1_STRING_type(const ASN1_STRING *str) { return str->type; }
|
||||
|
||||
unsigned char *ASN1_STRING_data(ASN1_STRING *str) { return str->data; }
|
||||
|
||||
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *str) {
|
||||
return str->data;
|
||||
}
|
||||
@@ -57,46 +57,47 @@
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
|
||||
const char *ASN1_tag2str(int tag)
|
||||
{
|
||||
static const char *const tag2str[] = {
|
||||
"EOC",
|
||||
"BOOLEAN",
|
||||
"INTEGER",
|
||||
"BIT STRING",
|
||||
"OCTET STRING",
|
||||
"NULL",
|
||||
"OBJECT",
|
||||
"OBJECT DESCRIPTOR",
|
||||
"EXTERNAL",
|
||||
"REAL",
|
||||
"ENUMERATED",
|
||||
"<ASN1 11>",
|
||||
"UTF8STRING",
|
||||
"<ASN1 13>",
|
||||
"<ASN1 14>",
|
||||
"<ASN1 15>",
|
||||
"SEQUENCE",
|
||||
"SET",
|
||||
"NUMERICSTRING",
|
||||
"PRINTABLESTRING",
|
||||
"T61STRING",
|
||||
"VIDEOTEXSTRING",
|
||||
"IA5STRING",
|
||||
"UTCTIME",
|
||||
"GENERALIZEDTIME",
|
||||
"GRAPHICSTRING",
|
||||
"VISIBLESTRING",
|
||||
"GENERALSTRING",
|
||||
"UNIVERSALSTRING",
|
||||
"<ASN1 29>",
|
||||
"BMPSTRING",
|
||||
};
|
||||
const char *ASN1_tag2str(int tag) {
|
||||
static const char *const tag2str[] = {
|
||||
"EOC",
|
||||
"BOOLEAN",
|
||||
"INTEGER",
|
||||
"BIT STRING",
|
||||
"OCTET STRING",
|
||||
"NULL",
|
||||
"OBJECT",
|
||||
"OBJECT DESCRIPTOR",
|
||||
"EXTERNAL",
|
||||
"REAL",
|
||||
"ENUMERATED",
|
||||
"<ASN1 11>",
|
||||
"UTF8STRING",
|
||||
"<ASN1 13>",
|
||||
"<ASN1 14>",
|
||||
"<ASN1 15>",
|
||||
"SEQUENCE",
|
||||
"SET",
|
||||
"NUMERICSTRING",
|
||||
"PRINTABLESTRING",
|
||||
"T61STRING",
|
||||
"VIDEOTEXSTRING",
|
||||
"IA5STRING",
|
||||
"UTCTIME",
|
||||
"GENERALIZEDTIME",
|
||||
"GRAPHICSTRING",
|
||||
"VISIBLESTRING",
|
||||
"GENERALSTRING",
|
||||
"UNIVERSALSTRING",
|
||||
"<ASN1 29>",
|
||||
"BMPSTRING",
|
||||
};
|
||||
|
||||
if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
|
||||
tag &= ~V_ASN1_NEG;
|
||||
if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED)) {
|
||||
tag &= ~V_ASN1_NEG;
|
||||
}
|
||||
|
||||
if (tag < 0 || tag > 30)
|
||||
return "(unknown)";
|
||||
return tag2str[tag];
|
||||
if (tag < 0 || tag > 30) {
|
||||
return "(unknown)";
|
||||
}
|
||||
return tag2str[tag];
|
||||
}
|
||||
@@ -60,42 +60,40 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
|
||||
ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **out)
|
||||
{
|
||||
uint8_t *new_data = NULL;
|
||||
int len = ASN1_item_i2d(obj, &new_data, it);
|
||||
if (len <= 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ENCODE_ERROR);
|
||||
return NULL;
|
||||
}
|
||||
ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **out) {
|
||||
uint8_t *new_data = NULL;
|
||||
int len = ASN1_item_i2d(obj, &new_data, it);
|
||||
if (len <= 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ENCODE_ERROR);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ASN1_STRING *ret = NULL;
|
||||
if (out == NULL || *out == NULL) {
|
||||
ret = ASN1_STRING_new();
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_free(new_data);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret = *out;
|
||||
ASN1_STRING *ret = NULL;
|
||||
if (out == NULL || *out == NULL) {
|
||||
ret = ASN1_STRING_new();
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_free(new_data);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret = *out;
|
||||
}
|
||||
|
||||
ASN1_STRING_set0(ret, new_data, len);
|
||||
if (out != NULL) {
|
||||
*out = ret;
|
||||
}
|
||||
return ret;
|
||||
ASN1_STRING_set0(ret, new_data, len);
|
||||
if (out != NULL) {
|
||||
*out = ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it)
|
||||
{
|
||||
const unsigned char *p = oct->data;
|
||||
void *ret = ASN1_item_d2i(NULL, &p, oct->length, it);
|
||||
if (ret == NULL || p != oct->data + oct->length) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
|
||||
ASN1_item_free(ret, it);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it) {
|
||||
const unsigned char *p = oct->data;
|
||||
void *ret = ASN1_item_d2i(NULL, &p, oct->length, it);
|
||||
if (ret == NULL || p != oct->data + oct->length) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
|
||||
ASN1_item_free(ret, it);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -58,45 +58,48 @@
|
||||
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
|
||||
int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a)
|
||||
{
|
||||
int i, n = 0;
|
||||
static const char *h = "0123456789ABCDEF";
|
||||
char buf[2];
|
||||
int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a) {
|
||||
int i, n = 0;
|
||||
static const char *h = "0123456789ABCDEF";
|
||||
char buf[2];
|
||||
|
||||
if (a == NULL)
|
||||
return (0);
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (a->type & V_ASN1_NEG) {
|
||||
if (BIO_write(bp, "-", 1) != 1)
|
||||
goto err;
|
||||
n = 1;
|
||||
if (a->type & V_ASN1_NEG) {
|
||||
if (BIO_write(bp, "-", 1) != 1) {
|
||||
goto err;
|
||||
}
|
||||
n = 1;
|
||||
}
|
||||
|
||||
if (a->length == 0) {
|
||||
if (BIO_write(bp, "00", 2) != 2)
|
||||
goto err;
|
||||
n += 2;
|
||||
} else {
|
||||
for (i = 0; i < a->length; i++) {
|
||||
if ((i != 0) && (i % 35 == 0)) {
|
||||
if (BIO_write(bp, "\\\n", 2) != 2)
|
||||
goto err;
|
||||
n += 2;
|
||||
}
|
||||
buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
|
||||
buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
|
||||
if (BIO_write(bp, buf, 2) != 2)
|
||||
goto err;
|
||||
n += 2;
|
||||
if (a->length == 0) {
|
||||
if (BIO_write(bp, "00", 2) != 2) {
|
||||
goto err;
|
||||
}
|
||||
n += 2;
|
||||
} else {
|
||||
for (i = 0; i < a->length; i++) {
|
||||
if ((i != 0) && (i % 35 == 0)) {
|
||||
if (BIO_write(bp, "\\\n", 2) != 2) {
|
||||
goto err;
|
||||
}
|
||||
n += 2;
|
||||
}
|
||||
buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
|
||||
buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
|
||||
if (BIO_write(bp, buf, 2) != 2) {
|
||||
goto err;
|
||||
}
|
||||
n += 2;
|
||||
}
|
||||
return (n);
|
||||
err:
|
||||
return (-1);
|
||||
}
|
||||
return n;
|
||||
err:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a)
|
||||
{
|
||||
return i2a_ASN1_INTEGER(bp, a);
|
||||
int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a) {
|
||||
return i2a_ASN1_INTEGER(bp, a);
|
||||
}
|
||||
@@ -58,34 +58,37 @@
|
||||
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
|
||||
int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type)
|
||||
{
|
||||
int i, n = 0;
|
||||
static const char *h = "0123456789ABCDEF";
|
||||
char buf[2];
|
||||
int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type) {
|
||||
int i, n = 0;
|
||||
static const char *h = "0123456789ABCDEF";
|
||||
char buf[2];
|
||||
|
||||
if (a == NULL)
|
||||
return (0);
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (a->length == 0) {
|
||||
if (BIO_write(bp, "0", 1) != 1)
|
||||
goto err;
|
||||
n = 1;
|
||||
} else {
|
||||
for (i = 0; i < a->length; i++) {
|
||||
if ((i != 0) && (i % 35 == 0)) {
|
||||
if (BIO_write(bp, "\\\n", 2) != 2)
|
||||
goto err;
|
||||
n += 2;
|
||||
}
|
||||
buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
|
||||
buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
|
||||
if (BIO_write(bp, buf, 2) != 2)
|
||||
goto err;
|
||||
n += 2;
|
||||
}
|
||||
if (a->length == 0) {
|
||||
if (BIO_write(bp, "0", 1) != 1) {
|
||||
goto err;
|
||||
}
|
||||
return (n);
|
||||
err:
|
||||
return (-1);
|
||||
n = 1;
|
||||
} else {
|
||||
for (i = 0; i < a->length; i++) {
|
||||
if ((i != 0) && (i % 35 == 0)) {
|
||||
if (BIO_write(bp, "\\\n", 2) != 2) {
|
||||
goto err;
|
||||
}
|
||||
n += 2;
|
||||
}
|
||||
buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
|
||||
buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
|
||||
if (BIO_write(bp, buf, 2) != 2) {
|
||||
goto err;
|
||||
}
|
||||
n += 2;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
err:
|
||||
return -1;
|
||||
}
|
||||
@@ -69,42 +69,64 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* Wrapper functions for time functions. */
|
||||
// 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);
|
||||
|
||||
/* Internal ASN1 structures and functions: not for application use */
|
||||
// Internal ASN1 structures and functions: not for application use
|
||||
|
||||
/* These are used internally in the ASN1_OBJECT to keep track of
|
||||
* whether the names and data need to be free()ed */
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC 0x01 /* internal use */
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 /* internal use */
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 /* internal use */
|
||||
// These are used internally in the ASN1_OBJECT to keep track of
|
||||
// whether the names and data need to be free()ed
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC 0x01 // internal use
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 // internal use
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 // internal use
|
||||
|
||||
/* An asn1_object_st (aka |ASN1_OBJECT|) represents an ASN.1 OBJECT IDENTIFIER.
|
||||
* Note: Mutating an |ASN1_OBJECT| is only permitted when initializing it. The
|
||||
* library maintains a table of static |ASN1_OBJECT|s, which may be referenced
|
||||
* by non-const |ASN1_OBJECT| pointers. Code which receives an |ASN1_OBJECT|
|
||||
* pointer externally must assume it is immutable, even if the pointer is not
|
||||
* const. */
|
||||
// An asn1_object_st (aka |ASN1_OBJECT|) represents an ASN.1 OBJECT IDENTIFIER.
|
||||
// Note: Mutating an |ASN1_OBJECT| is only permitted when initializing it. The
|
||||
// library maintains a table of static |ASN1_OBJECT|s, which may be referenced
|
||||
// by non-const |ASN1_OBJECT| pointers. Code which receives an |ASN1_OBJECT|
|
||||
// pointer externally must assume it is immutable, even if the pointer is not
|
||||
// const.
|
||||
struct asn1_object_st {
|
||||
const char *sn, *ln;
|
||||
int nid;
|
||||
int length;
|
||||
const unsigned char *data; /* data remains const after init */
|
||||
int flags; /* Should we free this one */
|
||||
const unsigned char *data; // data remains const after init
|
||||
int flags; // Should we free this one
|
||||
};
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_new(void);
|
||||
@@ -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);
|
||||
@@ -144,31 +166,31 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it, int tag, int aclass, char opt,
|
||||
ASN1_TLC *ctx);
|
||||
|
||||
/* ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the
|
||||
* i2d output convention. It returns a non-zero length on success and -1 on
|
||||
* error. If |tag| is -1. the tag and class come from |it|. Otherwise, the tag
|
||||
* number is |tag| and the class is |aclass|. This is used for implicit tagging.
|
||||
* This function treats a missing value as an error, not an optional field. */
|
||||
// ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the
|
||||
// i2d output convention. It returns a non-zero length on success and -1 on
|
||||
// error. If |tag| is -1. the tag and class come from |it|. Otherwise, the tag
|
||||
// number is |tag| and the class is |aclass|. This is used for implicit tagging.
|
||||
// This function treats a missing value as an error, not an optional field.
|
||||
int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass);
|
||||
|
||||
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
/* asn1_get_choice_selector returns the CHOICE selector value for |*pval|, which
|
||||
* must of type |it|. */
|
||||
// asn1_get_choice_selector returns the CHOICE selector value for |*pval|, which
|
||||
// must of type |it|.
|
||||
int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
|
||||
|
||||
/* asn1_get_field_ptr returns a pointer to the field in |*pval| corresponding to
|
||||
* |tt|. */
|
||||
// asn1_get_field_ptr returns a pointer to the field in |*pval| corresponding to
|
||||
// |tt|.
|
||||
ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
|
||||
/* asn1_do_adb returns the |ASN1_TEMPLATE| for the ANY DEFINED BY field |tt|,
|
||||
* based on the selector INTEGER or OID in |*pval|. If |tt| is not an ADB field,
|
||||
* it returns |tt|. If the selector does not match any value, it returns NULL.
|
||||
* If |nullerr| is non-zero, it will additionally push an error to the error
|
||||
* queue when there is no match. */
|
||||
// asn1_do_adb returns the |ASN1_TEMPLATE| for the ANY DEFINED BY field |tt|,
|
||||
// based on the selector INTEGER or OID in |*pval|. If |tt| is not an ADB field,
|
||||
// it returns |tt|. If the selector does not match any value, it returns NULL.
|
||||
// If |nullerr| is non-zero, it will additionally push an error to the error
|
||||
// queue when there is no match.
|
||||
const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
|
||||
int nullerr);
|
||||
|
||||
@@ -178,29 +200,29 @@ int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
/* asn1_enc_restore, if |*pval| has a saved encoding, writes it to |out| under
|
||||
* the i2d output convention, sets |*len| to the length, and returns one. If it
|
||||
* has no saved encoding, it returns zero. */
|
||||
// asn1_enc_restore, if |*pval| has a saved encoding, writes it to |out| under
|
||||
// the i2d output convention, sets |*len| to the length, and returns one. If it
|
||||
// has no saved encoding, it returns zero.
|
||||
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
|
||||
const ASN1_ITEM *it);
|
||||
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
|
||||
const ASN1_ITEM *it);
|
||||
|
||||
/* asn1_type_value_as_pointer returns |a|'s value in pointer form. This is
|
||||
* usually the value object but, for BOOLEAN values, is 0 or 0xff cast to
|
||||
* a pointer. */
|
||||
// asn1_type_value_as_pointer returns |a|'s value in pointer form. This is
|
||||
// usually the value object but, for BOOLEAN values, is 0 or 0xff cast to
|
||||
// a pointer.
|
||||
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a);
|
||||
|
||||
/* asn1_is_printable returns one if |value| is a valid Unicode codepoint for an
|
||||
* ASN.1 PrintableString, and zero otherwise. */
|
||||
// asn1_is_printable returns one if |value| is a valid Unicode codepoint for an
|
||||
// ASN.1 PrintableString, and zero otherwise.
|
||||
int asn1_is_printable(uint32_t value);
|
||||
|
||||
/* asn1_bit_string_length returns the number of bytes in |str| and sets
|
||||
* |*out_padding_bits| to the number of padding bits.
|
||||
*
|
||||
* This function should be used instead of |ASN1_STRING_length| to correctly
|
||||
* handle the non-|ASN1_STRING_FLAG_BITS_LEFT| case. */
|
||||
// asn1_bit_string_length returns the number of bytes in |str| and sets
|
||||
// |*out_padding_bits| to the number of padding bits.
|
||||
//
|
||||
// This function should be used instead of |ASN1_STRING_length| to correctly
|
||||
// handle the non-|ASN1_STRING_FLAG_BITS_LEFT| case.
|
||||
int asn1_bit_string_length(const ASN1_BIT_STRING *str,
|
||||
uint8_t *out_padding_bits);
|
||||
|
||||
@@ -212,14 +234,14 @@ typedef struct {
|
||||
unsigned long flags;
|
||||
} ASN1_STRING_TABLE;
|
||||
|
||||
/* asn1_get_string_table_for_testing sets |*out_ptr| and |*out_len| to the table
|
||||
* of built-in |ASN1_STRING_TABLE| values. It is exported for testing. */
|
||||
// asn1_get_string_table_for_testing sets |*out_ptr| and |*out_len| to the table
|
||||
// of built-in |ASN1_STRING_TABLE| values. It is exported for testing.
|
||||
OPENSSL_EXPORT void asn1_get_string_table_for_testing(
|
||||
const ASN1_STRING_TABLE **out_ptr, size_t *out_len);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} /* extern C */
|
||||
} // extern C
|
||||
#endif
|
||||
|
||||
#endif /* OPENSSL_HEADER_ASN1_ASN1_LOCL_H */
|
||||
#endif // OPENSSL_HEADER_ASN1_ASN1_LOCL_H
|
||||
@@ -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;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -63,171 +63,179 @@
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
/* Free up an ASN1 structure */
|
||||
// Free up an ASN1 structure
|
||||
|
||||
void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
|
||||
{
|
||||
asn1_item_combine_free(&val, it, 0);
|
||||
void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) {
|
||||
asn1_item_combine_free(&val, it, 0);
|
||||
}
|
||||
|
||||
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
asn1_item_combine_free(pval, it, 0);
|
||||
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
asn1_item_combine_free(pval, it, 0);
|
||||
}
|
||||
|
||||
void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
|
||||
{
|
||||
const ASN1_TEMPLATE *tt = NULL, *seqtt;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
int i;
|
||||
if (!pval)
|
||||
return;
|
||||
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
|
||||
return;
|
||||
|
||||
switch (it->itype) {
|
||||
void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine) {
|
||||
const ASN1_TEMPLATE *tt = NULL, *seqtt;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
int i;
|
||||
if (!pval) {
|
||||
return;
|
||||
}
|
||||
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (it->itype) {
|
||||
case ASN1_ITYPE_PRIMITIVE:
|
||||
if (it->templates)
|
||||
ASN1_template_free(pval, it->templates);
|
||||
else
|
||||
ASN1_primitive_free(pval, it);
|
||||
break;
|
||||
if (it->templates) {
|
||||
ASN1_template_free(pval, it->templates);
|
||||
} else {
|
||||
ASN1_primitive_free(pval, it);
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_MSTRING:
|
||||
ASN1_primitive_free(pval, it);
|
||||
break;
|
||||
ASN1_primitive_free(pval, it);
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_CHOICE: {
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
|
||||
if (i == 2)
|
||||
return;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
|
||||
if (i == 2) {
|
||||
return;
|
||||
}
|
||||
i = asn1_get_choice_selector(pval, it);
|
||||
if ((i >= 0) && (i < it->tcount)) {
|
||||
ASN1_VALUE **pchval;
|
||||
tt = it->templates + i;
|
||||
pchval = asn1_get_field_ptr(pval, tt);
|
||||
ASN1_template_free(pchval, tt);
|
||||
}
|
||||
if (asn1_cb)
|
||||
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
|
||||
if (!combine) {
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
i = asn1_get_choice_selector(pval, it);
|
||||
if ((i >= 0) && (i < it->tcount)) {
|
||||
ASN1_VALUE **pchval;
|
||||
tt = it->templates + i;
|
||||
pchval = asn1_get_field_ptr(pval, tt);
|
||||
ASN1_template_free(pchval, tt);
|
||||
}
|
||||
if (asn1_cb) {
|
||||
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
|
||||
}
|
||||
if (!combine) {
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_EXTERN:
|
||||
ef = it->funcs;
|
||||
if (ef && ef->asn1_ex_free)
|
||||
ef->asn1_ex_free(pval, it);
|
||||
break;
|
||||
ef = it->funcs;
|
||||
if (ef && ef->asn1_ex_free) {
|
||||
ef->asn1_ex_free(pval, it);
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_SEQUENCE: {
|
||||
if (!asn1_refcount_dec_and_test_zero(pval, it))
|
||||
return;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
|
||||
if (i == 2)
|
||||
return;
|
||||
if (!asn1_refcount_dec_and_test_zero(pval, it)) {
|
||||
return;
|
||||
}
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
|
||||
if (i == 2) {
|
||||
return;
|
||||
}
|
||||
asn1_enc_free(pval, it);
|
||||
/*
|
||||
* If we free up as normal we will invalidate any ANY DEFINED BY
|
||||
* field and we wont be able to determine the type of the field it
|
||||
* defines. So free up in reverse order.
|
||||
*/
|
||||
tt = it->templates + it->tcount - 1;
|
||||
for (i = 0; i < it->tcount; tt--, i++) {
|
||||
ASN1_VALUE **pseqval;
|
||||
seqtt = asn1_do_adb(pval, tt, 0);
|
||||
if (!seqtt)
|
||||
continue;
|
||||
pseqval = asn1_get_field_ptr(pval, seqtt);
|
||||
ASN1_template_free(pseqval, seqtt);
|
||||
}
|
||||
asn1_enc_free(pval, it);
|
||||
// If we free up as normal we will invalidate any ANY DEFINED BY
|
||||
// field and we wont be able to determine the type of the field it
|
||||
// defines. So free up in reverse order.
|
||||
tt = it->templates + it->tcount - 1;
|
||||
for (i = 0; i < it->tcount; tt--, i++) {
|
||||
ASN1_VALUE **pseqval;
|
||||
seqtt = asn1_do_adb(pval, tt, 0);
|
||||
if (!seqtt) {
|
||||
continue;
|
||||
}
|
||||
if (asn1_cb)
|
||||
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
|
||||
if (!combine) {
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
{
|
||||
size_t i;
|
||||
if (tt->flags & ASN1_TFLG_SK_MASK) {
|
||||
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
|
||||
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
ASN1_VALUE *vtmp;
|
||||
vtmp = sk_ASN1_VALUE_value(sk, i);
|
||||
asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0);
|
||||
}
|
||||
sk_ASN1_VALUE_free(sk);
|
||||
pseqval = asn1_get_field_ptr(pval, seqtt);
|
||||
ASN1_template_free(pseqval, seqtt);
|
||||
}
|
||||
if (asn1_cb) {
|
||||
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
|
||||
}
|
||||
if (!combine) {
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
} else
|
||||
asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
|
||||
tt->flags & ASN1_TFLG_COMBINE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
int utype;
|
||||
/* Historically, |it->funcs| for primitive types contained an
|
||||
* |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
|
||||
assert(it == NULL || it->funcs == NULL);
|
||||
/* Special case: if 'it' is NULL free contents of ASN1_TYPE */
|
||||
if (!it) {
|
||||
ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
|
||||
utype = typ->type;
|
||||
pval = &typ->value.asn1_value;
|
||||
if (utype != V_ASN1_BOOLEAN && !*pval)
|
||||
return;
|
||||
} else if (it->itype == ASN1_ITYPE_MSTRING) {
|
||||
utype = -1;
|
||||
if (!*pval)
|
||||
return;
|
||||
} else {
|
||||
utype = it->utype;
|
||||
if ((utype != V_ASN1_BOOLEAN) && !*pval)
|
||||
return;
|
||||
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
size_t i;
|
||||
if (tt->flags & ASN1_TFLG_SK_MASK) {
|
||||
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
|
||||
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
ASN1_VALUE *vtmp;
|
||||
vtmp = sk_ASN1_VALUE_value(sk, i);
|
||||
asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0);
|
||||
}
|
||||
sk_ASN1_VALUE_free(sk);
|
||||
*pval = NULL;
|
||||
} else {
|
||||
asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
|
||||
tt->flags & ASN1_TFLG_COMBINE);
|
||||
}
|
||||
}
|
||||
|
||||
switch (utype) {
|
||||
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
int utype;
|
||||
// Historically, |it->funcs| for primitive types contained an
|
||||
// |ASN1_PRIMITIVE_FUNCS| table of calbacks.
|
||||
assert(it == NULL || it->funcs == NULL);
|
||||
// Special case: if 'it' is NULL free contents of ASN1_TYPE
|
||||
if (!it) {
|
||||
ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
|
||||
utype = typ->type;
|
||||
pval = &typ->value.asn1_value;
|
||||
if (utype != V_ASN1_BOOLEAN && !*pval) {
|
||||
return;
|
||||
}
|
||||
} else if (it->itype == ASN1_ITYPE_MSTRING) {
|
||||
utype = -1;
|
||||
if (!*pval) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
utype = it->utype;
|
||||
if ((utype != V_ASN1_BOOLEAN) && !*pval) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
switch (utype) {
|
||||
case V_ASN1_OBJECT:
|
||||
ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
|
||||
break;
|
||||
ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
|
||||
break;
|
||||
|
||||
case V_ASN1_BOOLEAN:
|
||||
if (it)
|
||||
*(ASN1_BOOLEAN *)pval = it->size;
|
||||
else
|
||||
*(ASN1_BOOLEAN *)pval = -1;
|
||||
return;
|
||||
if (it) {
|
||||
*(ASN1_BOOLEAN *)pval = it->size;
|
||||
} else {
|
||||
*(ASN1_BOOLEAN *)pval = -1;
|
||||
}
|
||||
return;
|
||||
|
||||
case V_ASN1_NULL:
|
||||
break;
|
||||
break;
|
||||
|
||||
case V_ASN1_ANY:
|
||||
ASN1_primitive_free(pval, NULL);
|
||||
OPENSSL_free(*pval);
|
||||
break;
|
||||
ASN1_primitive_free(pval, NULL);
|
||||
OPENSSL_free(*pval);
|
||||
break;
|
||||
|
||||
default:
|
||||
ASN1_STRING_free((ASN1_STRING *)*pval);
|
||||
*pval = NULL;
|
||||
break;
|
||||
}
|
||||
*pval = NULL;
|
||||
ASN1_STRING_free((ASN1_STRING *)*pval);
|
||||
*pval = NULL;
|
||||
break;
|
||||
}
|
||||
*pval = NULL;
|
||||
}
|
||||
@@ -63,8 +63,8 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "internal.h"
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
@@ -75,258 +75,266 @@ static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
|
||||
{
|
||||
ASN1_VALUE *ret = NULL;
|
||||
if (ASN1_item_ex_new(&ret, it) > 0)
|
||||
return ret;
|
||||
return NULL;
|
||||
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) {
|
||||
ASN1_VALUE *ret = NULL;
|
||||
if (ASN1_item_ex_new(&ret, it) > 0) {
|
||||
return ret;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Allocate an ASN1 structure */
|
||||
// Allocate an ASN1 structure
|
||||
|
||||
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
return asn1_item_ex_combine_new(pval, it, 0);
|
||||
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
return asn1_item_ex_combine_new(pval, it, 0);
|
||||
}
|
||||
|
||||
static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine)
|
||||
{
|
||||
const ASN1_TEMPLATE *tt = NULL;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
ASN1_VALUE **pseqval;
|
||||
int i;
|
||||
|
||||
switch (it->itype) {
|
||||
int combine) {
|
||||
const ASN1_TEMPLATE *tt = NULL;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
ASN1_VALUE **pseqval;
|
||||
int i;
|
||||
|
||||
switch (it->itype) {
|
||||
case ASN1_ITYPE_EXTERN:
|
||||
ef = it->funcs;
|
||||
if (ef && ef->asn1_ex_new) {
|
||||
if (!ef->asn1_ex_new(pval, it))
|
||||
goto memerr;
|
||||
ef = it->funcs;
|
||||
if (ef && ef->asn1_ex_new) {
|
||||
if (!ef->asn1_ex_new(pval, it)) {
|
||||
goto memerr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_PRIMITIVE:
|
||||
if (it->templates) {
|
||||
if (!ASN1_template_new(pval, it->templates))
|
||||
goto memerr;
|
||||
} else if (!ASN1_primitive_new(pval, it))
|
||||
goto memerr;
|
||||
break;
|
||||
if (it->templates) {
|
||||
if (!ASN1_template_new(pval, it->templates)) {
|
||||
goto memerr;
|
||||
}
|
||||
} else if (!ASN1_primitive_new(pval, it)) {
|
||||
goto memerr;
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_MSTRING:
|
||||
if (!ASN1_primitive_new(pval, it))
|
||||
goto memerr;
|
||||
break;
|
||||
if (!ASN1_primitive_new(pval, it)) {
|
||||
goto memerr;
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_CHOICE: {
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
|
||||
if (!i)
|
||||
goto auxerr;
|
||||
if (i == 2) {
|
||||
return 1;
|
||||
}
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
|
||||
if (!i) {
|
||||
goto auxerr;
|
||||
}
|
||||
if (!combine) {
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval)
|
||||
goto memerr;
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
if (i == 2) {
|
||||
return 1;
|
||||
}
|
||||
asn1_set_choice_selector(pval, -1, it);
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
|
||||
goto auxerr2;
|
||||
break;
|
||||
}
|
||||
if (!combine) {
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
}
|
||||
asn1_set_choice_selector(pval, -1, it);
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) {
|
||||
goto auxerr2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_SEQUENCE: {
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
|
||||
if (!i)
|
||||
goto auxerr;
|
||||
if (i == 2) {
|
||||
return 1;
|
||||
}
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
|
||||
if (!i) {
|
||||
goto auxerr;
|
||||
}
|
||||
if (!combine) {
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval)
|
||||
goto memerr;
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
asn1_refcount_set_one(pval, it);
|
||||
asn1_enc_init(pval, it);
|
||||
if (i == 2) {
|
||||
return 1;
|
||||
}
|
||||
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
|
||||
pseqval = asn1_get_field_ptr(pval, tt);
|
||||
if (!ASN1_template_new(pseqval, tt))
|
||||
goto memerr2;
|
||||
}
|
||||
if (!combine) {
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
|
||||
goto auxerr2;
|
||||
break;
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
asn1_refcount_set_one(pval, it);
|
||||
asn1_enc_init(pval, it);
|
||||
}
|
||||
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
|
||||
pseqval = asn1_get_field_ptr(pval, tt);
|
||||
if (!ASN1_template_new(pseqval, tt)) {
|
||||
goto memerr2;
|
||||
}
|
||||
}
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) {
|
||||
goto auxerr2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
|
||||
memerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
memerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
|
||||
auxerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
auxerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
|
||||
return 0;
|
||||
memerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
memerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
|
||||
auxerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
auxerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
|
||||
switch (it->itype) {
|
||||
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
|
||||
switch (it->itype) {
|
||||
case ASN1_ITYPE_EXTERN:
|
||||
ef = it->funcs;
|
||||
if (ef && ef->asn1_ex_clear)
|
||||
ef->asn1_ex_clear(pval, it);
|
||||
else
|
||||
*pval = NULL;
|
||||
break;
|
||||
ef = it->funcs;
|
||||
if (ef && ef->asn1_ex_clear) {
|
||||
ef->asn1_ex_clear(pval, it);
|
||||
} else {
|
||||
*pval = NULL;
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_PRIMITIVE:
|
||||
if (it->templates)
|
||||
asn1_template_clear(pval, it->templates);
|
||||
else
|
||||
asn1_primitive_clear(pval, it);
|
||||
break;
|
||||
if (it->templates) {
|
||||
asn1_template_clear(pval, it->templates);
|
||||
} else {
|
||||
asn1_primitive_clear(pval, it);
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_MSTRING:
|
||||
asn1_primitive_clear(pval, it);
|
||||
break;
|
||||
asn1_primitive_clear(pval, it);
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_CHOICE:
|
||||
case ASN1_ITYPE_SEQUENCE:
|
||||
*pval = NULL;
|
||||
break;
|
||||
}
|
||||
*pval = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
{
|
||||
const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
|
||||
int ret;
|
||||
if (tt->flags & ASN1_TFLG_OPTIONAL) {
|
||||
asn1_template_clear(pval, tt);
|
||||
return 1;
|
||||
}
|
||||
/* If ANY DEFINED BY nothing to do */
|
||||
static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
|
||||
int ret;
|
||||
if (tt->flags & ASN1_TFLG_OPTIONAL) {
|
||||
asn1_template_clear(pval, tt);
|
||||
return 1;
|
||||
}
|
||||
// If ANY DEFINED BY nothing to do
|
||||
|
||||
if (tt->flags & ASN1_TFLG_ADB_MASK) {
|
||||
*pval = NULL;
|
||||
return 1;
|
||||
if (tt->flags & ASN1_TFLG_ADB_MASK) {
|
||||
*pval = NULL;
|
||||
return 1;
|
||||
}
|
||||
// If SET OF or SEQUENCE OF, its a STACK
|
||||
if (tt->flags & ASN1_TFLG_SK_MASK) {
|
||||
STACK_OF(ASN1_VALUE) *skval;
|
||||
skval = sk_ASN1_VALUE_new_null();
|
||||
if (!skval) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
ret = 0;
|
||||
goto done;
|
||||
}
|
||||
/* If SET OF or SEQUENCE OF, its a STACK */
|
||||
if (tt->flags & ASN1_TFLG_SK_MASK) {
|
||||
STACK_OF(ASN1_VALUE) *skval;
|
||||
skval = sk_ASN1_VALUE_new_null();
|
||||
if (!skval) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
ret = 0;
|
||||
goto done;
|
||||
}
|
||||
*pval = (ASN1_VALUE *)skval;
|
||||
ret = 1;
|
||||
goto done;
|
||||
}
|
||||
/* Otherwise pass it back to the item routine */
|
||||
ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
|
||||
done:
|
||||
return ret;
|
||||
*pval = (ASN1_VALUE *)skval;
|
||||
ret = 1;
|
||||
goto done;
|
||||
}
|
||||
// Otherwise pass it back to the item routine
|
||||
ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
|
||||
done:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
{
|
||||
/* If ADB or STACK just NULL the field */
|
||||
if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
|
||||
*pval = NULL;
|
||||
else
|
||||
asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
|
||||
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
// If ADB or STACK just NULL the field
|
||||
if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) {
|
||||
*pval = NULL;
|
||||
} else {
|
||||
asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* NB: could probably combine most of the real XXX_new() behaviour and junk
|
||||
* all the old functions.
|
||||
*/
|
||||
// NB: could probably combine most of the real XXX_new() behaviour and junk
|
||||
// all the old functions.
|
||||
|
||||
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
ASN1_TYPE *typ;
|
||||
int utype;
|
||||
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
ASN1_TYPE *typ;
|
||||
int utype;
|
||||
|
||||
if (!it)
|
||||
return 0;
|
||||
if (!it) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Historically, |it->funcs| for primitive types contained an
|
||||
* |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
|
||||
assert(it->funcs == NULL);
|
||||
// Historically, |it->funcs| for primitive types contained an
|
||||
// |ASN1_PRIMITIVE_FUNCS| table of calbacks.
|
||||
assert(it->funcs == NULL);
|
||||
|
||||
if (it->itype == ASN1_ITYPE_MSTRING)
|
||||
utype = -1;
|
||||
else
|
||||
utype = it->utype;
|
||||
switch (utype) {
|
||||
if (it->itype == ASN1_ITYPE_MSTRING) {
|
||||
utype = -1;
|
||||
} else {
|
||||
utype = it->utype;
|
||||
}
|
||||
switch (utype) {
|
||||
case V_ASN1_OBJECT:
|
||||
*pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
|
||||
return 1;
|
||||
*pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
|
||||
return 1;
|
||||
|
||||
case V_ASN1_BOOLEAN:
|
||||
*(ASN1_BOOLEAN *)pval = it->size;
|
||||
return 1;
|
||||
*(ASN1_BOOLEAN *)pval = it->size;
|
||||
return 1;
|
||||
|
||||
case V_ASN1_NULL:
|
||||
*pval = (ASN1_VALUE *)1;
|
||||
return 1;
|
||||
*pval = (ASN1_VALUE *)1;
|
||||
return 1;
|
||||
|
||||
case V_ASN1_ANY:
|
||||
typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
|
||||
if (!typ)
|
||||
return 0;
|
||||
typ->value.ptr = NULL;
|
||||
typ->type = -1;
|
||||
*pval = (ASN1_VALUE *)typ;
|
||||
break;
|
||||
typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
|
||||
if (!typ) {
|
||||
return 0;
|
||||
}
|
||||
typ->value.ptr = NULL;
|
||||
typ->type = -1;
|
||||
*pval = (ASN1_VALUE *)typ;
|
||||
break;
|
||||
|
||||
default:
|
||||
*pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
|
||||
break;
|
||||
}
|
||||
if (*pval)
|
||||
return 1;
|
||||
return 0;
|
||||
*pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
|
||||
break;
|
||||
}
|
||||
if (*pval) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
int utype;
|
||||
/* Historically, |it->funcs| for primitive types contained an
|
||||
* |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
|
||||
assert(it == NULL || it->funcs == NULL);
|
||||
if (!it || (it->itype == ASN1_ITYPE_MSTRING))
|
||||
utype = -1;
|
||||
else
|
||||
utype = it->utype;
|
||||
if (utype == V_ASN1_BOOLEAN)
|
||||
*(ASN1_BOOLEAN *)pval = it->size;
|
||||
else
|
||||
*pval = NULL;
|
||||
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
int utype;
|
||||
// Historically, |it->funcs| for primitive types contained an
|
||||
// |ASN1_PRIMITIVE_FUNCS| table of calbacks.
|
||||
assert(it == NULL || it->funcs == NULL);
|
||||
if (!it || (it->itype == ASN1_ITYPE_MSTRING)) {
|
||||
utype = -1;
|
||||
} else {
|
||||
utype = it->utype;
|
||||
}
|
||||
if (utype == V_ASN1_BOOLEAN) {
|
||||
*(ASN1_BOOLEAN *)pval = it->size;
|
||||
} else {
|
||||
*pval = NULL;
|
||||
}
|
||||
}
|
||||
@@ -58,19 +58,13 @@
|
||||
|
||||
#include <CCryptoBoringSSL_asn1t.h>
|
||||
|
||||
/* Declarations for string types */
|
||||
// Declarations for string types
|
||||
|
||||
#define IMPLEMENT_ASN1_STRING_FUNCTIONS(sname) \
|
||||
IMPLEMENT_ASN1_TYPE(sname) \
|
||||
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(sname, sname, sname) \
|
||||
sname *sname##_new(void) \
|
||||
{ \
|
||||
return ASN1_STRING_type_new(V_##sname); \
|
||||
} \
|
||||
void sname##_free(sname *x) \
|
||||
{ \
|
||||
ASN1_STRING_free(x); \
|
||||
}
|
||||
#define IMPLEMENT_ASN1_STRING_FUNCTIONS(sname) \
|
||||
IMPLEMENT_ASN1_TYPE(sname) \
|
||||
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(sname, sname, sname) \
|
||||
sname *sname##_new(void) { return ASN1_STRING_type_new(V_##sname); } \
|
||||
void sname##_free(sname *x) { ASN1_STRING_free(x); }
|
||||
|
||||
IMPLEMENT_ASN1_STRING_FUNCTIONS(ASN1_OCTET_STRING)
|
||||
IMPLEMENT_ASN1_STRING_FUNCTIONS(ASN1_INTEGER)
|
||||
@@ -94,12 +88,12 @@ IMPLEMENT_ASN1_TYPE(ASN1_OBJECT)
|
||||
|
||||
IMPLEMENT_ASN1_TYPE(ASN1_ANY)
|
||||
|
||||
/* Just swallow an ASN1_SEQUENCE in an ASN1_STRING */
|
||||
// Just swallow an ASN1_SEQUENCE in an ASN1_STRING
|
||||
IMPLEMENT_ASN1_TYPE(ASN1_SEQUENCE)
|
||||
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_TYPE, ASN1_ANY, ASN1_TYPE)
|
||||
|
||||
/* Multistring types */
|
||||
// Multistring types
|
||||
|
||||
IMPLEMENT_ASN1_MSTRING(ASN1_PRINTABLE, B_ASN1_PRINTABLE)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_STRING, ASN1_PRINTABLE,
|
||||
@@ -112,20 +106,21 @@ IMPLEMENT_ASN1_MSTRING(DIRECTORYSTRING, B_ASN1_DIRECTORYSTRING)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_STRING, DIRECTORYSTRING,
|
||||
DIRECTORYSTRING)
|
||||
|
||||
/* Three separate BOOLEAN type: normal, DEFAULT TRUE and DEFAULT FALSE */
|
||||
// Three separate BOOLEAN type: normal, DEFAULT TRUE and DEFAULT FALSE
|
||||
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_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY)
|
||||
ASN1_ITEM_TEMPLATE_END(ASN1_SEQUENCE_ANY)
|
||||
|
||||
ASN1_ITEM_TEMPLATE(ASN1_SET_ANY) =
|
||||
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, ASN1_SET_ANY, ASN1_ANY)
|
||||
ASN1_ITEM_TEMPLATE(ASN1_SET_ANY) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0,
|
||||
ASN1_SET_ANY, ASN1_ANY)
|
||||
ASN1_ITEM_TEMPLATE_END(ASN1_SET_ANY)
|
||||
|
||||
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY)
|
||||
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY, ASN1_SET_ANY, ASN1_SET_ANY)
|
||||
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY,
|
||||
ASN1_SEQUENCE_ANY,
|
||||
ASN1_SEQUENCE_ANY)
|
||||
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY, ASN1_SET_ANY,
|
||||
ASN1_SET_ANY)
|
||||
@@ -60,27 +60,27 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1t.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
/* Utility functions for manipulating fields and offsets */
|
||||
// Utility functions for manipulating fields and offsets
|
||||
|
||||
/* Add 'offset' to 'addr' */
|
||||
// Add 'offset' to 'addr'
|
||||
#define offset2ptr(addr, offset) (void *)(((char *)(addr)) + (offset))
|
||||
|
||||
/* Given an ASN1_ITEM CHOICE type return the selector value */
|
||||
// Given an ASN1_ITEM CHOICE type return the selector value
|
||||
int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
int *sel = offset2ptr(*pval, it->utype);
|
||||
return *sel;
|
||||
}
|
||||
|
||||
/* Given an ASN1_ITEM CHOICE type set the selector value, return old value. */
|
||||
// Given an ASN1_ITEM CHOICE type set the selector value, return old value.
|
||||
int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
|
||||
const ASN1_ITEM *it) {
|
||||
int *sel, ret;
|
||||
@@ -146,7 +146,7 @@ void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
ASN1_ENCODING *enc;
|
||||
enc = asn1_get_enc_ptr(pval, it);
|
||||
if (enc) {
|
||||
if (enc->enc && !enc->alias_only) {
|
||||
if (!enc->alias_only) {
|
||||
OPENSSL_free(enc->enc);
|
||||
}
|
||||
enc->enc = NULL;
|
||||
@@ -173,7 +173,7 @@ int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
|
||||
enc->alias_only_on_next_parse = 0;
|
||||
|
||||
if (enc->alias_only) {
|
||||
enc->enc = (uint8_t *) in;
|
||||
enc->enc = (uint8_t *)in;
|
||||
} else {
|
||||
enc->enc = OPENSSL_malloc(inlen);
|
||||
if (!enc->enc) {
|
||||
@@ -205,20 +205,20 @@ int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Given an ASN1_TEMPLATE get a pointer to a field */
|
||||
// Given an ASN1_TEMPLATE get a pointer to a field
|
||||
ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
ASN1_VALUE **pvaltmp;
|
||||
if (tt->flags & ASN1_TFLG_COMBINE) {
|
||||
return pval;
|
||||
}
|
||||
pvaltmp = offset2ptr(*pval, tt->offset);
|
||||
/* NOTE for BOOLEAN types the field is just a plain int so we can't return
|
||||
* int **, so settle for (int *). */
|
||||
// NOTE for BOOLEAN types the field is just a plain int so we can't return
|
||||
// int **, so settle for (int *).
|
||||
return pvaltmp;
|
||||
}
|
||||
|
||||
/* Handle ANY DEFINED BY template, find the selector, look up the relevant
|
||||
* ASN1_TEMPLATE in the table and return it. */
|
||||
// Handle ANY DEFINED BY template, find the selector, look up the relevant
|
||||
// ASN1_TEMPLATE in the table and return it.
|
||||
const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
|
||||
int nullerr) {
|
||||
const ASN1_ADB *adb;
|
||||
@@ -229,13 +229,13 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
|
||||
return tt;
|
||||
}
|
||||
|
||||
/* Else ANY DEFINED BY ... get the table */
|
||||
// Else ANY DEFINED BY ... get the table
|
||||
adb = ASN1_ADB_ptr(tt->item);
|
||||
|
||||
/* Get the selector field */
|
||||
// Get the selector field
|
||||
sfld = offset2ptr(*pval, adb->offset);
|
||||
|
||||
/* Check if NULL */
|
||||
// Check if NULL
|
||||
if (*sfld == NULL) {
|
||||
if (!adb->null_tt) {
|
||||
goto err;
|
||||
@@ -243,16 +243,16 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
|
||||
return adb->null_tt;
|
||||
}
|
||||
|
||||
/* Convert type to a NID:
|
||||
* NB: don't check for NID_undef here because it
|
||||
* might be a legitimate value in the table */
|
||||
// Convert type to a NID:
|
||||
// NB: don't check for NID_undef here because it
|
||||
// might be a legitimate value in the table
|
||||
assert(tt->flags & ASN1_TFLG_ADB_OID);
|
||||
int selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
|
||||
|
||||
/* Try to find matching entry in table Maybe should check application types
|
||||
* first to allow application override? Might also be useful to have a flag
|
||||
* which indicates table is sorted and we can do a binary search. For now
|
||||
* stick to a linear search. */
|
||||
// Try to find matching entry in table Maybe should check application types
|
||||
// first to allow application override? Might also be useful to have a flag
|
||||
// which indicates table is sorted and we can do a binary search. For now
|
||||
// stick to a linear search.
|
||||
|
||||
for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) {
|
||||
if (atbl->value == selector) {
|
||||
@@ -260,16 +260,16 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
|
||||
}
|
||||
}
|
||||
|
||||
/* FIXME: need to search application table too */
|
||||
// FIXME: need to search application table too
|
||||
|
||||
/* No match, return default type */
|
||||
// No match, return default type
|
||||
if (!adb->default_tt) {
|
||||
goto err;
|
||||
}
|
||||
return adb->default_tt;
|
||||
|
||||
err:
|
||||
/* FIXME: should log the value or OID of unsupported type */
|
||||
// FIXME: should log the value or OID of unsupported type
|
||||
if (nullerr) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -60,8 +60,6 @@
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_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) {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
#include <CCryptoBoringSSL_blake2.h>
|
||||
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_aead.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <CCryptoBoringSSL_cipher.h>
|
||||
#include <CCryptoBoringSSL_crypto.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_aead.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_chacha.h>
|
||||
@@ -21,7 +22,6 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_poly1305.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_md5.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_sha.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_base.h>
|
||||
#include <CCryptoBoringSSL_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)
|
||||
|
||||
@@ -766,7 +766,7 @@ int NCONF_load_bio(CONF *conf, BIO *bio, long *out_error_line) {
|
||||
}
|
||||
|
||||
int CONF_parse_list(const char *list, char sep, int remove_whitespace,
|
||||
int (*list_cb)(const char *elem, int len, void *usr),
|
||||
int (*list_cb)(const char *elem, size_t len, void *usr),
|
||||
void *arg) {
|
||||
int ret;
|
||||
const char *lstart, *tmpend, *p;
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#ifndef OPENSSL_HEADER_CRYPTO_CONF_INTERNAL_H
|
||||
#define OPENSSL_HEADER_CRYPTO_CONF_INTERNAL_H
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -23,6 +25,16 @@ extern "C" {
|
||||
// CONF_VALUE_new returns a freshly allocated and zeroed |CONF_VALUE|.
|
||||
CONF_VALUE *CONF_VALUE_new(void);
|
||||
|
||||
// CONF_parse_list takes a list separated by 'sep' and calls |list_cb| giving
|
||||
// the start and length of each member, optionally stripping leading and
|
||||
// trailing whitespace. This can be used to parse comma separated lists for
|
||||
// example. If |list_cb| returns <= 0, then the iteration is halted and that
|
||||
// value is returned immediately. Otherwise it returns one. Note that |list_cb|
|
||||
// may be called on an empty member.
|
||||
OPENSSL_EXPORT int CONF_parse_list(
|
||||
const char *list, char sep, int remove_whitespace,
|
||||
int (*list_cb)(const char *elem, size_t len, void *usr), void *arg);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
|
||||
@@ -58,13 +58,15 @@ void OPENSSL_cpuid_setup(void) {
|
||||
OPENSSL_armcap_P =
|
||||
ARMV7_NEON | ARMV8_AES | ARMV8_PMULL | ARMV8_SHA1 | ARMV8_SHA256;
|
||||
|
||||
// macOS has sysctls named both like "hw.optional.arm.FEAT_SHA512" and like
|
||||
// "hw.optional.armv8_2_sha512". There does not appear to be documentation on
|
||||
// which to use. The "armv8_2_sha512" style omits statically-available
|
||||
// features, while the "FEAT_SHA512" style includes them. However, the
|
||||
// "FEAT_SHA512" style was added in macOS 12, so we use the older style for
|
||||
// better compatibility and handle static features above.
|
||||
if (has_hw_feature("hw.optional.armv8_2_sha512")) {
|
||||
// See Apple's documentation for sysctl names:
|
||||
// https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics
|
||||
//
|
||||
// The new feature names, e.g. "hw.optional.arm.FEAT_SHA512", are only
|
||||
// available in macOS 12. For compatibility with macOS 11, we also support
|
||||
// the old names. The old names don't have values for features like FEAT_AES,
|
||||
// so instead we detect them statically above.
|
||||
if (has_hw_feature("hw.optional.arm.FEAT_SHA512") ||
|
||||
has_hw_feature("hw.optional.armv8_2_sha512")) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA512;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_rand.h>
|
||||
#include <CCryptoBoringSSL_sha.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_rand.h>
|
||||
#include <CCryptoBoringSSL_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;
|
||||
|
||||
@@ -59,8 +59,26 @@
|
||||
#include "../fipsmodule/bn/internal.h"
|
||||
|
||||
|
||||
static BIGNUM *get_params(BIGNUM *ret, const BN_ULONG *words, size_t num_words) {
|
||||
BIGNUM *alloc = NULL;
|
||||
if (ret == NULL) {
|
||||
alloc = BN_new();
|
||||
if (alloc == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ret = alloc;
|
||||
}
|
||||
|
||||
if (!bn_set_words(ret, words, num_words)) {
|
||||
BN_free(alloc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
BIGNUM *BN_get_rfc3526_prime_1536(BIGNUM *ret) {
|
||||
static const BN_ULONG kPrime1536Data[] = {
|
||||
static const BN_ULONG kWords[] = {
|
||||
TOBN(0xffffffff, 0xffffffff), TOBN(0xf1746c08, 0xca237327),
|
||||
TOBN(0x670c354e, 0x4abc9804), TOBN(0x9ed52907, 0x7096966d),
|
||||
TOBN(0x1c62f356, 0x208552bb), TOBN(0x83655d23, 0xdca3ad96),
|
||||
@@ -74,24 +92,221 @@ BIGNUM *BN_get_rfc3526_prime_1536(BIGNUM *ret) {
|
||||
TOBN(0x29024e08, 0x8a67cc74), TOBN(0xc4c6628b, 0x80dc1cd1),
|
||||
TOBN(0xc90fdaa2, 0x2168c234), TOBN(0xffffffff, 0xffffffff),
|
||||
};
|
||||
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
|
||||
}
|
||||
|
||||
static const BIGNUM kPrime1536BN = STATIC_BIGNUM(kPrime1536Data);
|
||||
BIGNUM *BN_get_rfc3526_prime_2048(BIGNUM *ret) {
|
||||
static const BN_ULONG kWords[] = {
|
||||
TOBN(0xffffffff, 0xffffffff), TOBN(0x15728e5a, 0x8aacaa68),
|
||||
TOBN(0x15d22618, 0x98fa0510), TOBN(0x3995497c, 0xea956ae5),
|
||||
TOBN(0xde2bcbf6, 0x95581718), TOBN(0xb5c55df0, 0x6f4c52c9),
|
||||
TOBN(0x9b2783a2, 0xec07a28f), TOBN(0xe39e772c, 0x180e8603),
|
||||
TOBN(0x32905e46, 0x2e36ce3b), TOBN(0xf1746c08, 0xca18217c),
|
||||
TOBN(0x670c354e, 0x4abc9804), TOBN(0x9ed52907, 0x7096966d),
|
||||
TOBN(0x1c62f356, 0x208552bb), TOBN(0x83655d23, 0xdca3ad96),
|
||||
TOBN(0x69163fa8, 0xfd24cf5f), TOBN(0x98da4836, 0x1c55d39a),
|
||||
TOBN(0xc2007cb8, 0xa163bf05), TOBN(0x49286651, 0xece45b3d),
|
||||
TOBN(0xae9f2411, 0x7c4b1fe6), TOBN(0xee386bfb, 0x5a899fa5),
|
||||
TOBN(0x0bff5cb6, 0xf406b7ed), TOBN(0xf44c42e9, 0xa637ed6b),
|
||||
TOBN(0xe485b576, 0x625e7ec6), TOBN(0x4fe1356d, 0x6d51c245),
|
||||
TOBN(0x302b0a6d, 0xf25f1437), TOBN(0xef9519b3, 0xcd3a431b),
|
||||
TOBN(0x514a0879, 0x8e3404dd), TOBN(0x020bbea6, 0x3b139b22),
|
||||
TOBN(0x29024e08, 0x8a67cc74), TOBN(0xc4c6628b, 0x80dc1cd1),
|
||||
TOBN(0xc90fdaa2, 0x2168c234), TOBN(0xffffffff, 0xffffffff),
|
||||
};
|
||||
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
|
||||
}
|
||||
|
||||
BIGNUM *alloc = NULL;
|
||||
if (ret == NULL) {
|
||||
alloc = BN_new();
|
||||
if (alloc == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ret = alloc;
|
||||
}
|
||||
BIGNUM *BN_get_rfc3526_prime_3072(BIGNUM *ret) {
|
||||
static const BN_ULONG kWords[] = {
|
||||
TOBN(0xffffffff, 0xffffffff), TOBN(0x4b82d120, 0xa93ad2ca),
|
||||
TOBN(0x43db5bfc, 0xe0fd108e), TOBN(0x08e24fa0, 0x74e5ab31),
|
||||
TOBN(0x770988c0, 0xbad946e2), TOBN(0xbbe11757, 0x7a615d6c),
|
||||
TOBN(0x521f2b18, 0x177b200c), TOBN(0xd8760273, 0x3ec86a64),
|
||||
TOBN(0xf12ffa06, 0xd98a0864), TOBN(0xcee3d226, 0x1ad2ee6b),
|
||||
TOBN(0x1e8c94e0, 0x4a25619d), TOBN(0xabf5ae8c, 0xdb0933d7),
|
||||
TOBN(0xb3970f85, 0xa6e1e4c7), TOBN(0x8aea7157, 0x5d060c7d),
|
||||
TOBN(0xecfb8504, 0x58dbef0a), TOBN(0xa85521ab, 0xdf1cba64),
|
||||
TOBN(0xad33170d, 0x04507a33), TOBN(0x15728e5a, 0x8aaac42d),
|
||||
TOBN(0x15d22618, 0x98fa0510), TOBN(0x3995497c, 0xea956ae5),
|
||||
TOBN(0xde2bcbf6, 0x95581718), TOBN(0xb5c55df0, 0x6f4c52c9),
|
||||
TOBN(0x9b2783a2, 0xec07a28f), TOBN(0xe39e772c, 0x180e8603),
|
||||
TOBN(0x32905e46, 0x2e36ce3b), TOBN(0xf1746c08, 0xca18217c),
|
||||
TOBN(0x670c354e, 0x4abc9804), TOBN(0x9ed52907, 0x7096966d),
|
||||
TOBN(0x1c62f356, 0x208552bb), TOBN(0x83655d23, 0xdca3ad96),
|
||||
TOBN(0x69163fa8, 0xfd24cf5f), TOBN(0x98da4836, 0x1c55d39a),
|
||||
TOBN(0xc2007cb8, 0xa163bf05), TOBN(0x49286651, 0xece45b3d),
|
||||
TOBN(0xae9f2411, 0x7c4b1fe6), TOBN(0xee386bfb, 0x5a899fa5),
|
||||
TOBN(0x0bff5cb6, 0xf406b7ed), TOBN(0xf44c42e9, 0xa637ed6b),
|
||||
TOBN(0xe485b576, 0x625e7ec6), TOBN(0x4fe1356d, 0x6d51c245),
|
||||
TOBN(0x302b0a6d, 0xf25f1437), TOBN(0xef9519b3, 0xcd3a431b),
|
||||
TOBN(0x514a0879, 0x8e3404dd), TOBN(0x020bbea6, 0x3b139b22),
|
||||
TOBN(0x29024e08, 0x8a67cc74), TOBN(0xc4c6628b, 0x80dc1cd1),
|
||||
TOBN(0xc90fdaa2, 0x2168c234), TOBN(0xffffffff, 0xffffffff),
|
||||
};
|
||||
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
|
||||
}
|
||||
|
||||
if (!BN_copy(ret, &kPrime1536BN)) {
|
||||
BN_free(alloc);
|
||||
return NULL;
|
||||
}
|
||||
BIGNUM *BN_get_rfc3526_prime_4096(BIGNUM *ret) {
|
||||
static const BN_ULONG kWords[] = {
|
||||
TOBN(0xffffffff, 0xffffffff), TOBN(0x4df435c9, 0x34063199),
|
||||
TOBN(0x86ffb7dc, 0x90a6c08f), TOBN(0x93b4ea98, 0x8d8fddc1),
|
||||
TOBN(0xd0069127, 0xd5b05aa9), TOBN(0xb81bdd76, 0x2170481c),
|
||||
TOBN(0x1f612970, 0xcee2d7af), TOBN(0x233ba186, 0x515be7ed),
|
||||
TOBN(0x99b2964f, 0xa090c3a2), TOBN(0x287c5947, 0x4e6bc05d),
|
||||
TOBN(0x2e8efc14, 0x1fbecaa6), TOBN(0xdbbbc2db, 0x04de8ef9),
|
||||
TOBN(0x2583e9ca, 0x2ad44ce8), TOBN(0x1a946834, 0xb6150bda),
|
||||
TOBN(0x99c32718, 0x6af4e23c), TOBN(0x88719a10, 0xbdba5b26),
|
||||
TOBN(0x1a723c12, 0xa787e6d7), TOBN(0x4b82d120, 0xa9210801),
|
||||
TOBN(0x43db5bfc, 0xe0fd108e), TOBN(0x08e24fa0, 0x74e5ab31),
|
||||
TOBN(0x770988c0, 0xbad946e2), TOBN(0xbbe11757, 0x7a615d6c),
|
||||
TOBN(0x521f2b18, 0x177b200c), TOBN(0xd8760273, 0x3ec86a64),
|
||||
TOBN(0xf12ffa06, 0xd98a0864), TOBN(0xcee3d226, 0x1ad2ee6b),
|
||||
TOBN(0x1e8c94e0, 0x4a25619d), TOBN(0xabf5ae8c, 0xdb0933d7),
|
||||
TOBN(0xb3970f85, 0xa6e1e4c7), TOBN(0x8aea7157, 0x5d060c7d),
|
||||
TOBN(0xecfb8504, 0x58dbef0a), TOBN(0xa85521ab, 0xdf1cba64),
|
||||
TOBN(0xad33170d, 0x04507a33), TOBN(0x15728e5a, 0x8aaac42d),
|
||||
TOBN(0x15d22618, 0x98fa0510), TOBN(0x3995497c, 0xea956ae5),
|
||||
TOBN(0xde2bcbf6, 0x95581718), TOBN(0xb5c55df0, 0x6f4c52c9),
|
||||
TOBN(0x9b2783a2, 0xec07a28f), TOBN(0xe39e772c, 0x180e8603),
|
||||
TOBN(0x32905e46, 0x2e36ce3b), TOBN(0xf1746c08, 0xca18217c),
|
||||
TOBN(0x670c354e, 0x4abc9804), TOBN(0x9ed52907, 0x7096966d),
|
||||
TOBN(0x1c62f356, 0x208552bb), TOBN(0x83655d23, 0xdca3ad96),
|
||||
TOBN(0x69163fa8, 0xfd24cf5f), TOBN(0x98da4836, 0x1c55d39a),
|
||||
TOBN(0xc2007cb8, 0xa163bf05), TOBN(0x49286651, 0xece45b3d),
|
||||
TOBN(0xae9f2411, 0x7c4b1fe6), TOBN(0xee386bfb, 0x5a899fa5),
|
||||
TOBN(0x0bff5cb6, 0xf406b7ed), TOBN(0xf44c42e9, 0xa637ed6b),
|
||||
TOBN(0xe485b576, 0x625e7ec6), TOBN(0x4fe1356d, 0x6d51c245),
|
||||
TOBN(0x302b0a6d, 0xf25f1437), TOBN(0xef9519b3, 0xcd3a431b),
|
||||
TOBN(0x514a0879, 0x8e3404dd), TOBN(0x020bbea6, 0x3b139b22),
|
||||
TOBN(0x29024e08, 0x8a67cc74), TOBN(0xc4c6628b, 0x80dc1cd1),
|
||||
TOBN(0xc90fdaa2, 0x2168c234), TOBN(0xffffffff, 0xffffffff),
|
||||
};
|
||||
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
|
||||
}
|
||||
|
||||
return ret;
|
||||
BIGNUM *BN_get_rfc3526_prime_6144(BIGNUM *ret) {
|
||||
static const BN_ULONG kWords[] = {
|
||||
TOBN(0xffffffff, 0xffffffff), TOBN(0xe694f91e, 0x6dcc4024),
|
||||
TOBN(0x12bf2d5b, 0x0b7474d6), TOBN(0x043e8f66, 0x3f4860ee),
|
||||
TOBN(0x387fe8d7, 0x6e3c0468), TOBN(0xda56c9ec, 0x2ef29632),
|
||||
TOBN(0xeb19ccb1, 0xa313d55c), TOBN(0xf550aa3d, 0x8a1fbff0),
|
||||
TOBN(0x06a1d58b, 0xb7c5da76), TOBN(0xa79715ee, 0xf29be328),
|
||||
TOBN(0x14cc5ed2, 0x0f8037e0), TOBN(0xcc8f6d7e, 0xbf48e1d8),
|
||||
TOBN(0x4bd407b2, 0x2b4154aa), TOBN(0x0f1d45b7, 0xff585ac5),
|
||||
TOBN(0x23a97a7e, 0x36cc88be), TOBN(0x59e7c97f, 0xbec7e8f3),
|
||||
TOBN(0xb5a84031, 0x900b1c9e), TOBN(0xd55e702f, 0x46980c82),
|
||||
TOBN(0xf482d7ce, 0x6e74fef6), TOBN(0xf032ea15, 0xd1721d03),
|
||||
TOBN(0x5983ca01, 0xc64b92ec), TOBN(0x6fb8f401, 0x378cd2bf),
|
||||
TOBN(0x33205151, 0x2bd7af42), TOBN(0xdb7f1447, 0xe6cc254b),
|
||||
TOBN(0x44ce6cba, 0xced4bb1b), TOBN(0xda3edbeb, 0xcf9b14ed),
|
||||
TOBN(0x179727b0, 0x865a8918), TOBN(0xb06a53ed, 0x9027d831),
|
||||
TOBN(0xe5db382f, 0x413001ae), TOBN(0xf8ff9406, 0xad9e530e),
|
||||
TOBN(0xc9751e76, 0x3dba37bd), TOBN(0xc1d4dcb2, 0x602646de),
|
||||
TOBN(0x36c3fab4, 0xd27c7026), TOBN(0x4df435c9, 0x34028492),
|
||||
TOBN(0x86ffb7dc, 0x90a6c08f), TOBN(0x93b4ea98, 0x8d8fddc1),
|
||||
TOBN(0xd0069127, 0xd5b05aa9), TOBN(0xb81bdd76, 0x2170481c),
|
||||
TOBN(0x1f612970, 0xcee2d7af), TOBN(0x233ba186, 0x515be7ed),
|
||||
TOBN(0x99b2964f, 0xa090c3a2), TOBN(0x287c5947, 0x4e6bc05d),
|
||||
TOBN(0x2e8efc14, 0x1fbecaa6), TOBN(0xdbbbc2db, 0x04de8ef9),
|
||||
TOBN(0x2583e9ca, 0x2ad44ce8), TOBN(0x1a946834, 0xb6150bda),
|
||||
TOBN(0x99c32718, 0x6af4e23c), TOBN(0x88719a10, 0xbdba5b26),
|
||||
TOBN(0x1a723c12, 0xa787e6d7), TOBN(0x4b82d120, 0xa9210801),
|
||||
TOBN(0x43db5bfc, 0xe0fd108e), TOBN(0x08e24fa0, 0x74e5ab31),
|
||||
TOBN(0x770988c0, 0xbad946e2), TOBN(0xbbe11757, 0x7a615d6c),
|
||||
TOBN(0x521f2b18, 0x177b200c), TOBN(0xd8760273, 0x3ec86a64),
|
||||
TOBN(0xf12ffa06, 0xd98a0864), TOBN(0xcee3d226, 0x1ad2ee6b),
|
||||
TOBN(0x1e8c94e0, 0x4a25619d), TOBN(0xabf5ae8c, 0xdb0933d7),
|
||||
TOBN(0xb3970f85, 0xa6e1e4c7), TOBN(0x8aea7157, 0x5d060c7d),
|
||||
TOBN(0xecfb8504, 0x58dbef0a), TOBN(0xa85521ab, 0xdf1cba64),
|
||||
TOBN(0xad33170d, 0x04507a33), TOBN(0x15728e5a, 0x8aaac42d),
|
||||
TOBN(0x15d22618, 0x98fa0510), TOBN(0x3995497c, 0xea956ae5),
|
||||
TOBN(0xde2bcbf6, 0x95581718), TOBN(0xb5c55df0, 0x6f4c52c9),
|
||||
TOBN(0x9b2783a2, 0xec07a28f), TOBN(0xe39e772c, 0x180e8603),
|
||||
TOBN(0x32905e46, 0x2e36ce3b), TOBN(0xf1746c08, 0xca18217c),
|
||||
TOBN(0x670c354e, 0x4abc9804), TOBN(0x9ed52907, 0x7096966d),
|
||||
TOBN(0x1c62f356, 0x208552bb), TOBN(0x83655d23, 0xdca3ad96),
|
||||
TOBN(0x69163fa8, 0xfd24cf5f), TOBN(0x98da4836, 0x1c55d39a),
|
||||
TOBN(0xc2007cb8, 0xa163bf05), TOBN(0x49286651, 0xece45b3d),
|
||||
TOBN(0xae9f2411, 0x7c4b1fe6), TOBN(0xee386bfb, 0x5a899fa5),
|
||||
TOBN(0x0bff5cb6, 0xf406b7ed), TOBN(0xf44c42e9, 0xa637ed6b),
|
||||
TOBN(0xe485b576, 0x625e7ec6), TOBN(0x4fe1356d, 0x6d51c245),
|
||||
TOBN(0x302b0a6d, 0xf25f1437), TOBN(0xef9519b3, 0xcd3a431b),
|
||||
TOBN(0x514a0879, 0x8e3404dd), TOBN(0x020bbea6, 0x3b139b22),
|
||||
TOBN(0x29024e08, 0x8a67cc74), TOBN(0xc4c6628b, 0x80dc1cd1),
|
||||
TOBN(0xc90fdaa2, 0x2168c234), TOBN(0xffffffff, 0xffffffff),
|
||||
};
|
||||
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
|
||||
}
|
||||
|
||||
BIGNUM *BN_get_rfc3526_prime_8192(BIGNUM *ret) {
|
||||
static const BN_ULONG kWords[] = {
|
||||
TOBN(0xffffffff, 0xffffffff), TOBN(0x60c980dd, 0x98edd3df),
|
||||
TOBN(0xc81f56e8, 0x80b96e71), TOBN(0x9e3050e2, 0x765694df),
|
||||
TOBN(0x9558e447, 0x5677e9aa), TOBN(0xc9190da6, 0xfc026e47),
|
||||
TOBN(0x889a002e, 0xd5ee382b), TOBN(0x4009438b, 0x481c6cd7),
|
||||
TOBN(0x359046f4, 0xeb879f92), TOBN(0xfaf36bc3, 0x1ecfa268),
|
||||
TOBN(0xb1d510bd, 0x7ee74d73), TOBN(0xf9ab4819, 0x5ded7ea1),
|
||||
TOBN(0x64f31cc5, 0x0846851d), TOBN(0x4597e899, 0xa0255dc1),
|
||||
TOBN(0xdf310ee0, 0x74ab6a36), TOBN(0x6d2a13f8, 0x3f44f82d),
|
||||
TOBN(0x062b3cf5, 0xb3a278a6), TOBN(0x79683303, 0xed5bdd3a),
|
||||
TOBN(0xfa9d4b7f, 0xa2c087e8), TOBN(0x4bcbc886, 0x2f8385dd),
|
||||
TOBN(0x3473fc64, 0x6cea306b), TOBN(0x13eb57a8, 0x1a23f0c7),
|
||||
TOBN(0x22222e04, 0xa4037c07), TOBN(0xe3fdb8be, 0xfc848ad9),
|
||||
TOBN(0x238f16cb, 0xe39d652d), TOBN(0x3423b474, 0x2bf1c978),
|
||||
TOBN(0x3aab639c, 0x5ae4f568), TOBN(0x2576f693, 0x6ba42466),
|
||||
TOBN(0x741fa7bf, 0x8afc47ed), TOBN(0x3bc832b6, 0x8d9dd300),
|
||||
TOBN(0xd8bec4d0, 0x73b931ba), TOBN(0x38777cb6, 0xa932df8c),
|
||||
TOBN(0x74a3926f, 0x12fee5e4), TOBN(0xe694f91e, 0x6dbe1159),
|
||||
TOBN(0x12bf2d5b, 0x0b7474d6), TOBN(0x043e8f66, 0x3f4860ee),
|
||||
TOBN(0x387fe8d7, 0x6e3c0468), TOBN(0xda56c9ec, 0x2ef29632),
|
||||
TOBN(0xeb19ccb1, 0xa313d55c), TOBN(0xf550aa3d, 0x8a1fbff0),
|
||||
TOBN(0x06a1d58b, 0xb7c5da76), TOBN(0xa79715ee, 0xf29be328),
|
||||
TOBN(0x14cc5ed2, 0x0f8037e0), TOBN(0xcc8f6d7e, 0xbf48e1d8),
|
||||
TOBN(0x4bd407b2, 0x2b4154aa), TOBN(0x0f1d45b7, 0xff585ac5),
|
||||
TOBN(0x23a97a7e, 0x36cc88be), TOBN(0x59e7c97f, 0xbec7e8f3),
|
||||
TOBN(0xb5a84031, 0x900b1c9e), TOBN(0xd55e702f, 0x46980c82),
|
||||
TOBN(0xf482d7ce, 0x6e74fef6), TOBN(0xf032ea15, 0xd1721d03),
|
||||
TOBN(0x5983ca01, 0xc64b92ec), TOBN(0x6fb8f401, 0x378cd2bf),
|
||||
TOBN(0x33205151, 0x2bd7af42), TOBN(0xdb7f1447, 0xe6cc254b),
|
||||
TOBN(0x44ce6cba, 0xced4bb1b), TOBN(0xda3edbeb, 0xcf9b14ed),
|
||||
TOBN(0x179727b0, 0x865a8918), TOBN(0xb06a53ed, 0x9027d831),
|
||||
TOBN(0xe5db382f, 0x413001ae), TOBN(0xf8ff9406, 0xad9e530e),
|
||||
TOBN(0xc9751e76, 0x3dba37bd), TOBN(0xc1d4dcb2, 0x602646de),
|
||||
TOBN(0x36c3fab4, 0xd27c7026), TOBN(0x4df435c9, 0x34028492),
|
||||
TOBN(0x86ffb7dc, 0x90a6c08f), TOBN(0x93b4ea98, 0x8d8fddc1),
|
||||
TOBN(0xd0069127, 0xd5b05aa9), TOBN(0xb81bdd76, 0x2170481c),
|
||||
TOBN(0x1f612970, 0xcee2d7af), TOBN(0x233ba186, 0x515be7ed),
|
||||
TOBN(0x99b2964f, 0xa090c3a2), TOBN(0x287c5947, 0x4e6bc05d),
|
||||
TOBN(0x2e8efc14, 0x1fbecaa6), TOBN(0xdbbbc2db, 0x04de8ef9),
|
||||
TOBN(0x2583e9ca, 0x2ad44ce8), TOBN(0x1a946834, 0xb6150bda),
|
||||
TOBN(0x99c32718, 0x6af4e23c), TOBN(0x88719a10, 0xbdba5b26),
|
||||
TOBN(0x1a723c12, 0xa787e6d7), TOBN(0x4b82d120, 0xa9210801),
|
||||
TOBN(0x43db5bfc, 0xe0fd108e), TOBN(0x08e24fa0, 0x74e5ab31),
|
||||
TOBN(0x770988c0, 0xbad946e2), TOBN(0xbbe11757, 0x7a615d6c),
|
||||
TOBN(0x521f2b18, 0x177b200c), TOBN(0xd8760273, 0x3ec86a64),
|
||||
TOBN(0xf12ffa06, 0xd98a0864), TOBN(0xcee3d226, 0x1ad2ee6b),
|
||||
TOBN(0x1e8c94e0, 0x4a25619d), TOBN(0xabf5ae8c, 0xdb0933d7),
|
||||
TOBN(0xb3970f85, 0xa6e1e4c7), TOBN(0x8aea7157, 0x5d060c7d),
|
||||
TOBN(0xecfb8504, 0x58dbef0a), TOBN(0xa85521ab, 0xdf1cba64),
|
||||
TOBN(0xad33170d, 0x04507a33), TOBN(0x15728e5a, 0x8aaac42d),
|
||||
TOBN(0x15d22618, 0x98fa0510), TOBN(0x3995497c, 0xea956ae5),
|
||||
TOBN(0xde2bcbf6, 0x95581718), TOBN(0xb5c55df0, 0x6f4c52c9),
|
||||
TOBN(0x9b2783a2, 0xec07a28f), TOBN(0xe39e772c, 0x180e8603),
|
||||
TOBN(0x32905e46, 0x2e36ce3b), TOBN(0xf1746c08, 0xca18217c),
|
||||
TOBN(0x670c354e, 0x4abc9804), TOBN(0x9ed52907, 0x7096966d),
|
||||
TOBN(0x1c62f356, 0x208552bb), TOBN(0x83655d23, 0xdca3ad96),
|
||||
TOBN(0x69163fa8, 0xfd24cf5f), TOBN(0x98da4836, 0x1c55d39a),
|
||||
TOBN(0xc2007cb8, 0xa163bf05), TOBN(0x49286651, 0xece45b3d),
|
||||
TOBN(0xae9f2411, 0x7c4b1fe6), TOBN(0xee386bfb, 0x5a899fa5),
|
||||
TOBN(0x0bff5cb6, 0xf406b7ed), TOBN(0xf44c42e9, 0xa637ed6b),
|
||||
TOBN(0xe485b576, 0x625e7ec6), TOBN(0x4fe1356d, 0x6d51c245),
|
||||
TOBN(0x302b0a6d, 0xf25f1437), TOBN(0xef9519b3, 0xcd3a431b),
|
||||
TOBN(0x514a0879, 0x8e3404dd), TOBN(0x020bbea6, 0x3b139b22),
|
||||
TOBN(0x29024e08, 0x8a67cc74), TOBN(0xc4c6628b, 0x80dc1cd1),
|
||||
TOBN(0xc90fdaa2, 0x2168c234), TOBN(0xffffffff, 0xffffffff),
|
||||
};
|
||||
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
|
||||
}
|
||||
|
||||
int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator,
|
||||
|
||||
@@ -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 <CCryptoBoringSSL_digest.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_nid.h>
|
||||
#include <CCryptoBoringSSL_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-";
|
||||
|
||||
@@ -16,43 +16,43 @@
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_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,
|
||||
|
||||
@@ -239,18 +239,16 @@ struct evp_pkey_method_st {
|
||||
} /* EVP_PKEY_METHOD */;
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
uint8_t priv[64];
|
||||
struct {
|
||||
// Shift the location of the public key to align with where it is in the
|
||||
// private key representation.
|
||||
uint8_t pad[32];
|
||||
uint8_t value[32];
|
||||
} pub;
|
||||
} key;
|
||||
// key is the concatenation of the private seed and public key. It is stored
|
||||
// as a single 64-bit array to allow passing to |ED25519_sign|. If
|
||||
// |has_private| is false, the first 32 bytes are uninitialized and the public
|
||||
// key is in the last 32 bytes.
|
||||
uint8_t key[64];
|
||||
char has_private;
|
||||
} ED25519_KEY;
|
||||
|
||||
#define ED25519_PUBLIC_KEY_OFFSET 32
|
||||
|
||||
typedef struct {
|
||||
uint8_t pub[32];
|
||||
uint8_t priv[32];
|
||||
|
||||
@@ -277,3 +277,15 @@ const EVP_PKEY_ASN1_METHOD dsa_asn1_meth = {
|
||||
|
||||
int_dsa_free,
|
||||
};
|
||||
|
||||
int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits) {
|
||||
// BoringSSL does not support DSA in |EVP_PKEY_CTX|.
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits) {
|
||||
// BoringSSL does not support DSA in |EVP_PKEY_CTX|.
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
@@ -37,7 +37,7 @@ static int pkey_ed25519_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
|
||||
}
|
||||
|
||||
uint8_t pubkey_unused[32];
|
||||
ED25519_keypair(pubkey_unused, key->key.priv);
|
||||
ED25519_keypair(pubkey_unused, key->key);
|
||||
key->has_private = 1;
|
||||
|
||||
OPENSSL_free(pkey->pkey.ptr);
|
||||
@@ -64,7 +64,7 @@ static int pkey_ed25519_sign_message(EVP_PKEY_CTX *ctx, uint8_t *sig,
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!ED25519_sign(sig, tbs, tbslen, key->key.priv)) {
|
||||
if (!ED25519_sign(sig, tbs, tbslen, key->key)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ static int pkey_ed25519_verify_message(EVP_PKEY_CTX *ctx, const uint8_t *sig,
|
||||
size_t tbslen) {
|
||||
ED25519_KEY *key = ctx->pkey->pkey.ptr;
|
||||
if (siglen != 64 ||
|
||||
!ED25519_verify(tbs, tbslen, sig, key->key.pub.value)) {
|
||||
!ED25519_verify(tbs, tbslen, sig, key->key + ED25519_PUBLIC_KEY_OFFSET)) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SIGNATURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ static int ed25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
// The RFC 8032 encoding stores only the 32-byte seed, so we must recover the
|
||||
// full representation which we use from it.
|
||||
uint8_t pubkey_unused[32];
|
||||
ED25519_keypair_from_seed(pubkey_unused, key->key.priv, in);
|
||||
ED25519_keypair_from_seed(pubkey_unused, key->key, in);
|
||||
key->has_private = 1;
|
||||
|
||||
ed25519_free(pkey);
|
||||
@@ -63,7 +63,7 @@ static int ed25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
OPENSSL_memcpy(key->key.pub.value, in, 32);
|
||||
OPENSSL_memcpy(key->key + ED25519_PUBLIC_KEY_OFFSET, in, 32);
|
||||
key->has_private = 0;
|
||||
|
||||
ed25519_free(pkey);
|
||||
@@ -90,7 +90,7 @@ static int ed25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
|
||||
}
|
||||
|
||||
// The raw private key format is the first 32 bytes of the private key.
|
||||
OPENSSL_memcpy(out, key->key.priv, 32);
|
||||
OPENSSL_memcpy(out, key->key, 32);
|
||||
*out_len = 32;
|
||||
return 1;
|
||||
}
|
||||
@@ -108,7 +108,7 @@ static int ed25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
|
||||
return 0;
|
||||
}
|
||||
|
||||
OPENSSL_memcpy(out, key->key.pub.value, 32);
|
||||
OPENSSL_memcpy(out, key->key + ED25519_PUBLIC_KEY_OFFSET, 32);
|
||||
*out_len = 32;
|
||||
return 1;
|
||||
}
|
||||
@@ -136,7 +136,8 @@ static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
|
||||
!CBB_add_bytes(&oid, ed25519_asn1_meth.oid, ed25519_asn1_meth.oid_len) ||
|
||||
!CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
|
||||
!CBB_add_u8(&key_bitstring, 0 /* padding */) ||
|
||||
!CBB_add_bytes(&key_bitstring, key->key.pub.value, 32) ||
|
||||
!CBB_add_bytes(&key_bitstring, key->key + ED25519_PUBLIC_KEY_OFFSET,
|
||||
32) ||
|
||||
!CBB_flush(out)) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
@@ -148,7 +149,8 @@ static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
|
||||
static int ed25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
const ED25519_KEY *a_key = a->pkey.ptr;
|
||||
const ED25519_KEY *b_key = b->pkey.ptr;
|
||||
return OPENSSL_memcmp(a_key->key.pub.value, b_key->key.pub.value, 32) == 0;
|
||||
return OPENSSL_memcmp(a_key->key + ED25519_PUBLIC_KEY_OFFSET,
|
||||
b_key->key + ED25519_PUBLIC_KEY_OFFSET, 32) == 0;
|
||||
}
|
||||
|
||||
static int ed25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
|
||||
@@ -185,7 +187,7 @@ static int ed25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
|
||||
!CBB_add_asn1(&private_key, &inner, CBS_ASN1_OCTETSTRING) ||
|
||||
// The PKCS#8 encoding stores only the 32-byte seed which is the first 32
|
||||
// bytes of the private key.
|
||||
!CBB_add_bytes(&inner, key->key.priv, 32) ||
|
||||
!CBB_add_bytes(&inner, key->key, 32) ||
|
||||
!CBB_flush(out)) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_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;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "../../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
#if defined(OPENSSL_SSE2)
|
||||
#include <emmintrin.h>
|
||||
@@ -151,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.
|
||||
@@ -1181,29 +1182,27 @@ void aes_nohw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
|
||||
aes_nohw_expand_round_keys(&sched, key);
|
||||
|
||||
// Make |AES_NOHW_BATCH_SIZE| copies of |ivec|.
|
||||
alignas(AES_NOHW_WORD_SIZE) union {
|
||||
uint32_t u32[AES_NOHW_BATCH_SIZE * 4];
|
||||
uint8_t u8[AES_NOHW_BATCH_SIZE * 16];
|
||||
} ivs, enc_ivs;
|
||||
alignas(AES_NOHW_WORD_SIZE) uint8_t ivs[AES_NOHW_BATCH_SIZE * 16];
|
||||
alignas(AES_NOHW_WORD_SIZE) uint8_t enc_ivs[AES_NOHW_BATCH_SIZE * 16];
|
||||
for (size_t i = 0; i < AES_NOHW_BATCH_SIZE; i++) {
|
||||
memcpy(ivs.u8 + 16 * i, ivec, 16);
|
||||
memcpy(ivs + 16 * i, ivec, 16);
|
||||
}
|
||||
|
||||
uint32_t ctr = CRYPTO_bswap4(ivs.u32[3]);
|
||||
uint32_t ctr = CRYPTO_load_u32_be(ivs + 12);
|
||||
for (;;) {
|
||||
// Update counters.
|
||||
for (size_t i = 0; i < AES_NOHW_BATCH_SIZE; i++) {
|
||||
ivs.u32[4 * i + 3] = CRYPTO_bswap4(ctr + i);
|
||||
CRYPTO_store_u32_be(ivs + 16 * i + 12, ctr + (uint32_t)i);
|
||||
}
|
||||
|
||||
size_t todo = blocks >= AES_NOHW_BATCH_SIZE ? AES_NOHW_BATCH_SIZE : blocks;
|
||||
AES_NOHW_BATCH batch;
|
||||
aes_nohw_to_batch(&batch, ivs.u8, todo);
|
||||
aes_nohw_to_batch(&batch, ivs, todo);
|
||||
aes_nohw_encrypt_batch(&sched, key->rounds, &batch);
|
||||
aes_nohw_from_batch(enc_ivs.u8, todo, &batch);
|
||||
aes_nohw_from_batch(enc_ivs, todo, &batch);
|
||||
|
||||
for (size_t i = 0; i < todo; i++) {
|
||||
aes_nohw_xor_block(out + 16 * i, in + 16 * i, enc_ivs.u8 + 16 * i);
|
||||
aes_nohw_xor_block(out + 16 * i, in + 16 * i, enc_ivs + 16 * i);
|
||||
}
|
||||
|
||||
blocks -= todo;
|
||||
|
||||
@@ -164,10 +164,8 @@ static const uint8_t kPaddingConstant[4] = {0xa6, 0x59, 0x59, 0xa6};
|
||||
int AES_wrap_key_padded(const AES_KEY *key, uint8_t *out, size_t *out_len,
|
||||
size_t max_out, const uint8_t *in, size_t in_len) {
|
||||
// See https://tools.ietf.org/html/rfc5649#section-4.1
|
||||
const uint32_t in_len32_be = CRYPTO_bswap4(in_len);
|
||||
const uint64_t in_len64 = in_len;
|
||||
const size_t padded_len = (in_len + 7) & ~7;
|
||||
|
||||
*out_len = 0;
|
||||
if (in_len == 0 || in_len64 > 0xffffffffu || in_len + 7 < in_len ||
|
||||
padded_len + 8 < padded_len || max_out < padded_len + 8) {
|
||||
@@ -176,7 +174,7 @@ int AES_wrap_key_padded(const AES_KEY *key, uint8_t *out, size_t *out_len,
|
||||
|
||||
uint8_t block[AES_BLOCK_SIZE];
|
||||
memcpy(block, kPaddingConstant, sizeof(kPaddingConstant));
|
||||
memcpy(block + 4, &in_len32_be, sizeof(in_len32_be));
|
||||
CRYPTO_store_u32_be(block + 4, (uint32_t)in_len);
|
||||
|
||||
if (in_len <= 8) {
|
||||
memset(block + 8, 0, 8);
|
||||
@@ -226,9 +224,7 @@ int AES_unwrap_key_padded(const AES_KEY *key, uint8_t *out, size_t *out_len,
|
||||
crypto_word_t ok = constant_time_eq_int(
|
||||
CRYPTO_memcmp(iv, kPaddingConstant, sizeof(kPaddingConstant)), 0);
|
||||
|
||||
uint32_t claimed_len32;
|
||||
memcpy(&claimed_len32, iv + 4, sizeof(claimed_len32));
|
||||
const size_t claimed_len = CRYPTO_bswap4(claimed_len32);
|
||||
const size_t claimed_len = CRYPTO_load_u32_be(iv + 4);
|
||||
ok &= ~constant_time_is_zero_w(claimed_len);
|
||||
ok &= constant_time_eq_w((claimed_len - 1) >> 3, (in_len - 9) >> 3);
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
|
||||
#include <CCryptoBoringSSL_bn.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -66,6 +67,11 @@
|
||||
#include "../delocate.h"
|
||||
|
||||
|
||||
// BN_MAX_WORDS is the maximum number of words allowed in a |BIGNUM|. It is
|
||||
// sized so byte and bit counts of a |BIGNUM| always fit in |int|, with room to
|
||||
// spare.
|
||||
#define BN_MAX_WORDS (INT_MAX / (4 * BN_BITS2))
|
||||
|
||||
BIGNUM *BN_new(void) {
|
||||
BIGNUM *bn = OPENSSL_malloc(sizeof(BIGNUM));
|
||||
|
||||
@@ -80,6 +86,8 @@ BIGNUM *BN_new(void) {
|
||||
return bn;
|
||||
}
|
||||
|
||||
BIGNUM *BN_secure_new(void) { return BN_new(); }
|
||||
|
||||
void BN_init(BIGNUM *bn) {
|
||||
OPENSSL_memset(bn, 0, sizeof(BIGNUM));
|
||||
}
|
||||
@@ -289,8 +297,9 @@ void bn_set_static_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
|
||||
}
|
||||
bn->d = (BN_ULONG *)words;
|
||||
|
||||
bn->width = num;
|
||||
bn->dmax = num;
|
||||
assert(num <= BN_MAX_WORDS);
|
||||
bn->width = (int)num;
|
||||
bn->dmax = (int)num;
|
||||
bn->neg = 0;
|
||||
bn->flags |= BN_FLG_STATIC_DATA;
|
||||
}
|
||||
@@ -343,7 +352,7 @@ int bn_wexpand(BIGNUM *bn, size_t words) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (words > (INT_MAX / (4 * BN_BITS2))) {
|
||||
if (words > BN_MAX_WORDS) {
|
||||
OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
@@ -400,7 +409,7 @@ int bn_resize_words(BIGNUM *bn, size_t words) {
|
||||
}
|
||||
OPENSSL_memset(bn->d + bn->width, 0,
|
||||
(words - bn->width) * sizeof(BN_ULONG));
|
||||
bn->width = words;
|
||||
bn->width = (int)words;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -409,15 +418,15 @@ int bn_resize_words(BIGNUM *bn, size_t words) {
|
||||
OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
bn->width = words;
|
||||
bn->width = (int)words;
|
||||
return 1;
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
|
||||
BN_free(bn);
|
||||
return NULL;
|
||||
}
|
||||
ret->width = num_words;
|
||||
ret->width = (int)num_words;
|
||||
|
||||
// Make sure the top bytes will be zeroed.
|
||||
ret->d[num_words - 1] = 0;
|
||||
|
||||
@@ -56,8 +56,9 @@
|
||||
|
||||
#include <CCryptoBoringSSL_bn.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_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;
|
||||
|
||||
@@ -552,7 +552,7 @@ static BIGNUM *bn_scratch_space_from_ctx(size_t width, BN_CTX *ctx) {
|
||||
return NULL;
|
||||
}
|
||||
ret->neg = 0;
|
||||
ret->width = width;
|
||||
ret->width = (int)width;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ uint16_t bn_mod_u16_consttime(const BIGNUM *bn, uint16_t d) {
|
||||
// This operation is not constant-time, but |p| and |d| are public values.
|
||||
// Note that |p| is at most 16, so the computation fits in |uint64_t|.
|
||||
assert(p <= 16);
|
||||
uint32_t m = ((UINT64_C(1) << (32 + p)) + d - 1) / d;
|
||||
uint32_t m = (uint32_t)(((UINT64_C(1) << (32 + p)) + d - 1) / d);
|
||||
|
||||
uint16_t ret = 0;
|
||||
for (int i = bn->width - 1; i >= 0; i--) {
|
||||
|
||||
@@ -240,8 +240,10 @@ int bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse, const BIGNUM *a,
|
||||
|
||||
// Each loop iteration halves at least one of |u| and |v|. Thus we need at
|
||||
// most the combined bit width of inputs for at least one value to be zero.
|
||||
unsigned a_bits = a_width * BN_BITS2, n_bits = n_width * BN_BITS2;
|
||||
unsigned num_iters = a_bits + n_bits;
|
||||
// |a_bits| and |n_bits| cannot overflow because |bn_wexpand| ensures bit
|
||||
// counts fit in even |int|.
|
||||
size_t a_bits = a_width * BN_BITS2, n_bits = n_width * BN_BITS2;
|
||||
size_t num_iters = a_bits + n_bits;
|
||||
if (num_iters < a_bits) {
|
||||
OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
|
||||
goto err;
|
||||
@@ -260,7 +262,7 @@ int bn_mod_inverse_consttime(BIGNUM *r, int *out_no_inverse, const BIGNUM *a,
|
||||
//
|
||||
// After each loop iteration, u and v only get smaller, and at least one of
|
||||
// them shrinks by at least a factor of two.
|
||||
for (unsigned i = 0; i < num_iters; i++) {
|
||||
for (size_t i = 0; i < num_iters; i++) {
|
||||
BN_ULONG both_odd = word_is_odd_mask(u->d[0]) & word_is_odd_mask(v->d[0]);
|
||||
|
||||
// If both |u| and |v| are odd, subtract the smaller from the larger.
|
||||
|
||||
@@ -439,7 +439,7 @@ int bn_jacobi(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
|
||||
|
||||
// bn_is_bit_set_words returns one if bit |bit| is set in |a| and zero
|
||||
// otherwise.
|
||||
int bn_is_bit_set_words(const BN_ULONG *a, size_t num, unsigned bit);
|
||||
int bn_is_bit_set_words(const BN_ULONG *a, size_t num, size_t bit);
|
||||
|
||||
// bn_one_to_montgomery sets |r| to one in Montgomery form. It returns one on
|
||||
// success and zero on error. This function treats the bit width of the modulus
|
||||
|
||||
@@ -116,7 +116,6 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_bn.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_rand.h>
|
||||
#include <CCryptoBoringSSL_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];
|
||||
@@ -336,7 +336,7 @@ int bn_rand_secret_range(BIGNUM *r, int *out_is_uniform, BN_ULONG min_inclusive,
|
||||
assert(bn_in_range_words(r->d, min_inclusive, max_exclusive->d, words));
|
||||
|
||||
r->neg = 0;
|
||||
r->width = words;
|
||||
r->width = (int)words;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_bn.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
@@ -258,9 +258,9 @@ int BN_clear_bit(BIGNUM *a, int n) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int bn_is_bit_set_words(const BN_ULONG *a, size_t num, unsigned bit) {
|
||||
unsigned i = bit / BN_BITS2;
|
||||
unsigned j = bit % BN_BITS2;
|
||||
int bn_is_bit_set_words(const BN_ULONG *a, size_t num, size_t bit) {
|
||||
size_t i = bit / BN_BITS2;
|
||||
size_t j = bit % BN_BITS2;
|
||||
if (i >= num) {
|
||||
return 0;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -104,6 +104,11 @@ int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (in->poisoned) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX_cleanup(out);
|
||||
OPENSSL_memcpy(out, in, sizeof(EVP_CIPHER_CTX));
|
||||
|
||||
@@ -226,6 +231,9 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
||||
|
||||
ctx->buf_len = 0;
|
||||
ctx->final_used = 0;
|
||||
// Clear the poisoned flag to permit re-use of a CTX that previously had a
|
||||
// failed operation.
|
||||
ctx->poisoned = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -250,6 +258,15 @@ static int block_remainder(const EVP_CIPHER_CTX *ctx, int len) {
|
||||
|
||||
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
const uint8_t *in, int in_len) {
|
||||
if (ctx->poisoned) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
// If the first call to |cipher| succeeds and the second fails, |ctx| may be
|
||||
// left in an indeterminate state. We set a poison flag on failure to ensure
|
||||
// callers do not continue to use the object in that case.
|
||||
ctx->poisoned = 1;
|
||||
|
||||
// Ciphers that use blocks may write up to |bl| extra bytes. Ensure the output
|
||||
// does not overflow |*out_len|.
|
||||
int bl = ctx->cipher->block_size;
|
||||
@@ -265,17 +282,23 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
} else {
|
||||
*out_len = ret;
|
||||
}
|
||||
ctx->poisoned = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (in_len <= 0) {
|
||||
*out_len = 0;
|
||||
return in_len == 0;
|
||||
if (in_len == 0) {
|
||||
ctx->poisoned = 0;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx->buf_len == 0 && block_remainder(ctx, in_len) == 0) {
|
||||
if (ctx->cipher->cipher(ctx, out, in, in_len)) {
|
||||
*out_len = in_len;
|
||||
ctx->poisoned = 0;
|
||||
return 1;
|
||||
} else {
|
||||
*out_len = 0;
|
||||
@@ -290,6 +313,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
OPENSSL_memcpy(&ctx->buf[i], in, in_len);
|
||||
ctx->buf_len += in_len;
|
||||
*out_len = 0;
|
||||
ctx->poisoned = 0;
|
||||
return 1;
|
||||
} else {
|
||||
int j = bl - i;
|
||||
@@ -319,6 +343,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
OPENSSL_memcpy(ctx->buf, &in[in_len], i);
|
||||
}
|
||||
ctx->buf_len = i;
|
||||
ctx->poisoned = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -326,6 +351,11 @@ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
|
||||
int n;
|
||||
unsigned int i, b, bl;
|
||||
|
||||
if (ctx->poisoned) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
|
||||
// When EVP_CIPH_FLAG_CUSTOM_CIPHER is set, the return value of |cipher| is
|
||||
// the number of bytes written, or -1 on error. Otherwise the return value
|
||||
@@ -371,6 +401,11 @@ out:
|
||||
|
||||
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
const uint8_t *in, int in_len) {
|
||||
if (ctx->poisoned) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Ciphers that use blocks may write up to |bl| extra bytes. Ensure the output
|
||||
// does not overflow |*out_len|.
|
||||
unsigned int b = ctx->cipher->block_size;
|
||||
@@ -433,6 +468,11 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *out_len) {
|
||||
unsigned int b;
|
||||
*out_len = 0;
|
||||
|
||||
if (ctx->poisoned) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
|
||||
i = ctx->cipher->cipher(ctx, out, NULL, 0);
|
||||
if (i < 0) {
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
* ==================================================================== */
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_aead.h>
|
||||
@@ -289,8 +290,10 @@ static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key,
|
||||
block128_f *out_block, const uint8_t *key,
|
||||
size_t key_bytes) {
|
||||
// This function assumes the key length was previously validated.
|
||||
assert(key_bytes == 128 / 8 || key_bytes == 192 / 8 || key_bytes == 256 / 8);
|
||||
if (hwaes_capable()) {
|
||||
aes_hw_set_encrypt_key(key, key_bytes * 8, aes_key);
|
||||
aes_hw_set_encrypt_key(key, (int)key_bytes * 8, aes_key);
|
||||
if (gcm_key != NULL) {
|
||||
CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_hw_encrypt, 1);
|
||||
}
|
||||
@@ -301,7 +304,7 @@ ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key,
|
||||
}
|
||||
|
||||
if (vpaes_capable()) {
|
||||
vpaes_set_encrypt_key(key, key_bytes * 8, aes_key);
|
||||
vpaes_set_encrypt_key(key, (int)key_bytes * 8, aes_key);
|
||||
if (out_block) {
|
||||
*out_block = vpaes_encrypt;
|
||||
}
|
||||
@@ -318,7 +321,7 @@ ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key,
|
||||
#endif
|
||||
}
|
||||
|
||||
aes_nohw_set_encrypt_key(key, key_bytes * 8, aes_key);
|
||||
aes_nohw_set_encrypt_key(key, (int)key_bytes * 8, aes_key);
|
||||
if (gcm_key != NULL) {
|
||||
CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_nohw_encrypt, 0);
|
||||
}
|
||||
@@ -335,7 +338,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");
|
||||
|
||||
@@ -552,6 +555,14 @@ static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (len > INT_MAX) {
|
||||
// This function signature can only express up to |INT_MAX| bytes encrypted.
|
||||
//
|
||||
// TODO(https://crbug.com/boringssl/494): Make the internal |EVP_CIPHER|
|
||||
// calling convention |size_t|-clean.
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (in) {
|
||||
if (out == NULL) {
|
||||
if (!CRYPTO_gcm128_aad(&gctx->gcm, in, len)) {
|
||||
@@ -580,7 +591,7 @@ static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
}
|
||||
}
|
||||
}
|
||||
return len;
|
||||
return (int)len;
|
||||
} else {
|
||||
if (!ctx->encrypt) {
|
||||
if (gctx->taglen < 0 ||
|
||||
@@ -921,12 +932,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 +1270,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) {
|
||||
@@ -1297,12 +1308,9 @@ static int aead_aes_gcm_tls12_seal_scatter(
|
||||
}
|
||||
|
||||
// The given nonces must be strictly monotonically increasing.
|
||||
uint64_t given_counter;
|
||||
OPENSSL_memcpy(&given_counter, nonce + nonce_len - sizeof(given_counter),
|
||||
sizeof(given_counter));
|
||||
given_counter = CRYPTO_bswap8(given_counter);
|
||||
if (given_counter == UINT64_MAX ||
|
||||
given_counter < gcm_ctx->min_next_nonce) {
|
||||
uint64_t given_counter =
|
||||
CRYPTO_load_u64_be(nonce + nonce_len - sizeof(uint64_t));
|
||||
if (given_counter == UINT64_MAX || given_counter < gcm_ctx->min_next_nonce) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE);
|
||||
return 0;
|
||||
}
|
||||
@@ -1356,12 +1364,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) {
|
||||
@@ -1397,10 +1405,8 @@ static int aead_aes_gcm_tls13_seal_scatter(
|
||||
// The given nonces must be strictly monotonically increasing. See
|
||||
// https://tools.ietf.org/html/rfc8446#section-5.3 for details of the TLS 1.3
|
||||
// nonce construction.
|
||||
uint64_t given_counter;
|
||||
OPENSSL_memcpy(&given_counter, nonce + nonce_len - sizeof(given_counter),
|
||||
sizeof(given_counter));
|
||||
given_counter = CRYPTO_bswap8(given_counter);
|
||||
uint64_t given_counter =
|
||||
CRYPTO_load_u64_be(nonce + nonce_len - sizeof(uint64_t));
|
||||
|
||||
if (gcm_ctx->first) {
|
||||
// In the first call the sequence number will be zero and therefore the
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -49,6 +49,7 @@
|
||||
#include <CCryptoBoringSSL_cmac.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_aes.h>
|
||||
@@ -269,7 +270,10 @@ int CMAC_Update(CMAC_CTX *ctx, const uint8_t *in, size_t in_len) {
|
||||
}
|
||||
|
||||
OPENSSL_memcpy(ctx->block, in, in_len);
|
||||
ctx->block_used = in_len;
|
||||
// |in_len| is bounded by |block_size|, which fits in |unsigned|.
|
||||
static_assert(EVP_MAX_BLOCK_LENGTH < UINT_MAX,
|
||||
"EVP_MAX_BLOCK_LENGTH is too large");
|
||||
ctx->block_used = (unsigned)in_len;
|
||||
ret = 1;
|
||||
|
||||
out:
|
||||
|
||||
@@ -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; }
|
||||
@@ -366,7 +368,8 @@ int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
|
||||
int ret = -1;
|
||||
BIGNUM *shared_key = BN_CTX_get(ctx);
|
||||
if (shared_key && dh_compute_key(dh, shared_key, peers_key, ctx)) {
|
||||
ret = BN_bn2bin(shared_key, out);
|
||||
// A |BIGNUM|'s byte count fits in |int|.
|
||||
ret = (int)BN_bn2bin(shared_key, out);
|
||||
}
|
||||
|
||||
BN_CTX_end(ctx);
|
||||
|
||||
@@ -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 <CCryptoBoringSSL_base.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bn.h>
|
||||
#include <CCryptoBoringSSL_ec.h>
|
||||
#include <CCryptoBoringSSL_ex_data.h>
|
||||
#include <CCryptoBoringSSL_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.
|
||||
|
||||
@@ -277,11 +277,6 @@ static void ecp_nistz256_windowed_mul(const EC_GROUP *group, P256_POINT *r,
|
||||
ecp_nistz256_point_add(r, r, &h);
|
||||
}
|
||||
|
||||
typedef union {
|
||||
P256_POINT p;
|
||||
P256_POINT_AFFINE a;
|
||||
} p256_point_union_t;
|
||||
|
||||
static crypto_word_t calc_first_wvalue(size_t *index, const uint8_t p_str[33]) {
|
||||
static const size_t kWindowSize = 7;
|
||||
static const crypto_word_t kMask = (1 << (7 /* kWindowSize */ + 1)) - 1;
|
||||
@@ -318,8 +313,6 @@ static void ecp_nistz256_point_mul(const EC_GROUP *group, EC_RAW_POINT *r,
|
||||
|
||||
static void ecp_nistz256_point_mul_base(const EC_GROUP *group, EC_RAW_POINT *r,
|
||||
const EC_SCALAR *scalar) {
|
||||
alignas(32) p256_point_union_t t, p;
|
||||
|
||||
uint8_t p_str[33];
|
||||
OPENSSL_memcpy(p_str, scalar->words, 32);
|
||||
p_str[32] = 0;
|
||||
@@ -328,33 +321,38 @@ static void ecp_nistz256_point_mul_base(const EC_GROUP *group, EC_RAW_POINT *r,
|
||||
size_t index = 0;
|
||||
crypto_word_t wvalue = calc_first_wvalue(&index, p_str);
|
||||
|
||||
ecp_nistz256_select_w7(&p.a, ecp_nistz256_precomputed[0], wvalue >> 1);
|
||||
ecp_nistz256_neg(p.p.Z, p.p.Y);
|
||||
copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
|
||||
alignas(32) P256_POINT_AFFINE t;
|
||||
alignas(32) P256_POINT p;
|
||||
ecp_nistz256_select_w7(&t, ecp_nistz256_precomputed[0], wvalue >> 1);
|
||||
ecp_nistz256_neg(p.Z, t.Y);
|
||||
copy_conditional(t.Y, p.Z, wvalue & 1);
|
||||
|
||||
// Convert |p| from affine to Jacobian coordinates. We set Z to zero if |p|
|
||||
// is infinity and |ONE| otherwise. |p| was computed from the table, so it
|
||||
// Convert |t| from affine to Jacobian coordinates. We set Z to zero if |t|
|
||||
// is infinity and |ONE| otherwise. |t| was computed from the table, so it
|
||||
// is infinity iff |wvalue >> 1| is zero.
|
||||
OPENSSL_memset(p.p.Z, 0, sizeof(p.p.Z));
|
||||
copy_conditional(p.p.Z, ONE, is_not_zero(wvalue >> 1));
|
||||
OPENSSL_memcpy(p.X, t.X, sizeof(p.X));
|
||||
OPENSSL_memcpy(p.Y, t.Y, sizeof(p.Y));
|
||||
OPENSSL_memset(p.Z, 0, sizeof(p.Z));
|
||||
copy_conditional(p.Z, ONE, is_not_zero(wvalue >> 1));
|
||||
|
||||
for (int i = 1; i < 37; i++) {
|
||||
wvalue = calc_wvalue(&index, p_str);
|
||||
|
||||
ecp_nistz256_select_w7(&t.a, ecp_nistz256_precomputed[i], wvalue >> 1);
|
||||
ecp_nistz256_select_w7(&t, ecp_nistz256_precomputed[i], wvalue >> 1);
|
||||
|
||||
ecp_nistz256_neg(t.p.Z, t.a.Y);
|
||||
copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
|
||||
alignas(32) BN_ULONG neg_Y[P256_LIMBS];
|
||||
ecp_nistz256_neg(neg_Y, t.Y);
|
||||
copy_conditional(t.Y, neg_Y, wvalue & 1);
|
||||
|
||||
// Note |ecp_nistz256_point_add_affine| does not work if |p.p| and |t.a|
|
||||
// are the same non-infinity point.
|
||||
ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
|
||||
// Note |ecp_nistz256_point_add_affine| does not work if |p| and |t| are the
|
||||
// same non-infinity point.
|
||||
ecp_nistz256_point_add_affine(&p, &p, &t);
|
||||
}
|
||||
|
||||
assert(group->field.width == P256_LIMBS);
|
||||
OPENSSL_memcpy(r->X.words, p.p.X, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->Y.words, p.p.Y, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->Z.words, p.p.Z, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->X.words, p.X, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->Y.words, p.Y, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->Z.words, p.Z, P256_LIMBS * sizeof(BN_ULONG));
|
||||
}
|
||||
|
||||
static void ecp_nistz256_points_mul_public(const EC_GROUP *group,
|
||||
@@ -364,7 +362,7 @@ static void ecp_nistz256_points_mul_public(const EC_GROUP *group,
|
||||
const EC_SCALAR *p_scalar) {
|
||||
assert(p_ != NULL && p_scalar != NULL && g_scalar != NULL);
|
||||
|
||||
alignas(32) p256_point_union_t t, p;
|
||||
alignas(32) P256_POINT p;
|
||||
uint8_t p_str[33];
|
||||
OPENSSL_memcpy(p_str, g_scalar->words, 32);
|
||||
p_str[32] = 0;
|
||||
@@ -377,45 +375,48 @@ static void ecp_nistz256_points_mul_public(const EC_GROUP *group,
|
||||
// is infinity and |ONE| otherwise. |p| was computed from the table, so it
|
||||
// is infinity iff |wvalue >> 1| is zero.
|
||||
if ((wvalue >> 1) != 0) {
|
||||
OPENSSL_memcpy(&p.a, &ecp_nistz256_precomputed[0][(wvalue >> 1) - 1],
|
||||
sizeof(p.a));
|
||||
OPENSSL_memcpy(&p.p.Z, ONE, sizeof(p.p.Z));
|
||||
OPENSSL_memcpy(p.X, &ecp_nistz256_precomputed[0][(wvalue >> 1) - 1].X,
|
||||
sizeof(p.X));
|
||||
OPENSSL_memcpy(p.Y, &ecp_nistz256_precomputed[0][(wvalue >> 1) - 1].Y,
|
||||
sizeof(p.Y));
|
||||
OPENSSL_memcpy(p.Z, ONE, sizeof(p.Z));
|
||||
} else {
|
||||
OPENSSL_memset(&p.a, 0, sizeof(p.a));
|
||||
OPENSSL_memset(p.p.Z, 0, sizeof(p.p.Z));
|
||||
OPENSSL_memset(p.X, 0, sizeof(p.X));
|
||||
OPENSSL_memset(p.Y, 0, sizeof(p.Y));
|
||||
OPENSSL_memset(p.Z, 0, sizeof(p.Z));
|
||||
}
|
||||
|
||||
if ((wvalue & 1) == 1) {
|
||||
ecp_nistz256_neg(p.p.Y, p.p.Y);
|
||||
ecp_nistz256_neg(p.Y, p.Y);
|
||||
}
|
||||
|
||||
for (int i = 1; i < 37; i++) {
|
||||
wvalue = calc_wvalue(&index, p_str);
|
||||
|
||||
if ((wvalue >> 1) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
OPENSSL_memcpy(&t.a, &ecp_nistz256_precomputed[i][(wvalue >> 1) - 1],
|
||||
sizeof(p.a));
|
||||
|
||||
alignas(32) P256_POINT_AFFINE t;
|
||||
OPENSSL_memcpy(&t, &ecp_nistz256_precomputed[i][(wvalue >> 1) - 1],
|
||||
sizeof(t));
|
||||
if ((wvalue & 1) == 1) {
|
||||
ecp_nistz256_neg(t.a.Y, t.a.Y);
|
||||
ecp_nistz256_neg(t.Y, t.Y);
|
||||
}
|
||||
|
||||
// Note |ecp_nistz256_point_add_affine| does not work if |p.p| and |t.a|
|
||||
// are the same non-infinity point, so it is important that we compute the
|
||||
// Note |ecp_nistz256_point_add_affine| does not work if |p| and |t| are
|
||||
// the same non-infinity point, so it is important that we compute the
|
||||
// |g_scalar| term before the |p_scalar| term.
|
||||
ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
|
||||
ecp_nistz256_point_add_affine(&p, &p, &t);
|
||||
}
|
||||
|
||||
ecp_nistz256_windowed_mul(group, &t.p, p_, p_scalar);
|
||||
ecp_nistz256_point_add(&p.p, &p.p, &t.p);
|
||||
alignas(32) P256_POINT tmp;
|
||||
ecp_nistz256_windowed_mul(group, &tmp, p_, p_scalar);
|
||||
ecp_nistz256_point_add(&p, &p, &tmp);
|
||||
|
||||
assert(group->field.width == P256_LIMBS);
|
||||
OPENSSL_memcpy(r->X.words, p.p.X, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->Y.words, p.p.Y, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->Z.words, p.p.Z, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->X.words, p.X, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->Y.words, p.Y, P256_LIMBS * sizeof(BN_ULONG));
|
||||
OPENSSL_memcpy(r->Z.words, p.Z, P256_LIMBS * sizeof(BN_ULONG));
|
||||
}
|
||||
|
||||
static int ecp_nistz256_get_affine(const EC_GROUP *group,
|
||||
|
||||
@@ -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 <CCryptoBoringSSL_ec.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_sha.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "internal.h"
|
||||
#include "../../internal.h"
|
||||
|
||||
@@ -116,16 +114,12 @@ void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
|
||||
assert(inptr >= outptr || inptr + len <= outptr);
|
||||
|
||||
size_t n;
|
||||
union {
|
||||
crypto_word_t t[16 / sizeof(crypto_word_t)];
|
||||
uint8_t c[16];
|
||||
} tmp;
|
||||
|
||||
alignas(16) uint8_t tmp[16];
|
||||
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);
|
||||
@@ -140,14 +134,14 @@ 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.c, key);
|
||||
(*block)(in, tmp, key);
|
||||
for (n = 0; n < 16; n += sizeof(crypto_word_t)) {
|
||||
crypto_word_t c = CRYPTO_load_word_le(in + n);
|
||||
CRYPTO_store_word_le(out + n, tmp.t[n / sizeof(crypto_word_t)] ^
|
||||
CRYPTO_store_word_le(out + n, CRYPTO_load_word_le(tmp + n) ^
|
||||
CRYPTO_load_word_le(ivec + n));
|
||||
CRYPTO_store_word_le(ivec + n, c);
|
||||
}
|
||||
@@ -159,10 +153,10 @@ void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
|
||||
|
||||
while (len) {
|
||||
uint8_t c;
|
||||
(*block)(in, tmp.c, key);
|
||||
(*block)(in, tmp, key);
|
||||
for (n = 0; n < 16 && n < len; ++n) {
|
||||
c = in[n];
|
||||
out[n] = tmp.c[n] ^ ivec[n];
|
||||
out[n] = tmp[n] ^ ivec[n];
|
||||
ivec[n] = c;
|
||||
}
|
||||
if (len <= 16) {
|
||||
|
||||
@@ -46,16 +46,13 @@
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ==================================================================== */
|
||||
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_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 <CCryptoBoringSSL_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 <CCryptoBoringSSL_rand.h>
|
||||
#include <CCryptoBoringSSL_ctrdrbg.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
#include <CCryptoBoringSSL_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) {
|
||||
@@ -59,20 +75,20 @@ int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
|
||||
}
|
||||
|
||||
drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, seed_material, 32);
|
||||
OPENSSL_memcpy(drbg->counter.bytes, seed_material + 32, 16);
|
||||
OPENSSL_memcpy(drbg->counter, seed_material + 32, 16);
|
||||
drbg->reseed_counter = 1;
|
||||
|
||||
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.
|
||||
static void ctr32_add(CTR_DRBG_STATE *drbg, uint32_t n) {
|
||||
drbg->counter.words[3] =
|
||||
CRYPTO_bswap4(CRYPTO_bswap4(drbg->counter.words[3]) + n);
|
||||
uint32_t ctr = CRYPTO_load_u32_be(drbg->counter + 12);
|
||||
CRYPTO_store_u32_be(drbg->counter + 12, ctr + n);
|
||||
}
|
||||
|
||||
static int ctr_drbg_update(CTR_DRBG_STATE *drbg, const uint8_t *data,
|
||||
@@ -87,7 +103,7 @@ static int ctr_drbg_update(CTR_DRBG_STATE *drbg, const uint8_t *data,
|
||||
uint8_t temp[CTR_DRBG_ENTROPY_LEN];
|
||||
for (size_t i = 0; i < CTR_DRBG_ENTROPY_LEN; i += AES_BLOCK_SIZE) {
|
||||
ctr32_add(drbg, 1);
|
||||
drbg->block(drbg->counter.bytes, temp + i, &drbg->ks);
|
||||
drbg->block(drbg->counter, temp + i, &drbg->ks);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < data_len; i++) {
|
||||
@@ -95,7 +111,7 @@ static int ctr_drbg_update(CTR_DRBG_STATE *drbg, const uint8_t *data,
|
||||
}
|
||||
|
||||
drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, temp, 32);
|
||||
OPENSSL_memcpy(drbg->counter.bytes, temp + 32, 16);
|
||||
OPENSSL_memcpy(drbg->counter, temp + 32, 16);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -167,12 +183,12 @@ int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, size_t out_len,
|
||||
if (drbg->ctr) {
|
||||
OPENSSL_memset(out, 0, todo);
|
||||
ctr32_add(drbg, 1);
|
||||
drbg->ctr(out, out, num_blocks, &drbg->ks, drbg->counter.bytes);
|
||||
ctr32_add(drbg, num_blocks - 1);
|
||||
drbg->ctr(out, out, num_blocks, &drbg->ks, drbg->counter);
|
||||
ctr32_add(drbg, (uint32_t)(num_blocks - 1));
|
||||
} else {
|
||||
for (size_t i = 0; i < todo; i += AES_BLOCK_SIZE) {
|
||||
ctr32_add(drbg, 1);
|
||||
drbg->block(drbg->counter.bytes, out + i, &drbg->ks);
|
||||
drbg->block(drbg->counter, out + i, &drbg->ks);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,7 +199,7 @@ int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, size_t out_len,
|
||||
if (out_len > 0) {
|
||||
uint8_t block[AES_BLOCK_SIZE];
|
||||
ctr32_add(drbg, 1);
|
||||
drbg->block(drbg->counter.bytes, block, &drbg->ks);
|
||||
drbg->block(drbg->counter, block, &drbg->ks);
|
||||
|
||||
OPENSSL_memcpy(out, block, out_len);
|
||||
}
|
||||
|
||||
@@ -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 <CCryptoBoringSSL_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 <CCryptoBoringSSL_aes.h>
|
||||
#include <CCryptoBoringSSL_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,20 +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;
|
||||
union {
|
||||
uint8_t bytes[16];
|
||||
uint32_t words[4];
|
||||
} counter;
|
||||
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
|
||||
@@ -119,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 <CCryptoBoringSSL_chacha.h>
|
||||
#include <CCryptoBoringSSL_ctrdrbg.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_thread.h>
|
||||
#include <CCryptoBoringSSL_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;
|
||||
}
|
||||
|
||||
|
||||
@@ -300,7 +300,7 @@ int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
|
||||
return -1;
|
||||
}
|
||||
return out_len;
|
||||
return (int)out_len;
|
||||
}
|
||||
|
||||
static int rsa_sign_raw_no_self_test(RSA *rsa, size_t *out_len, uint8_t *out,
|
||||
@@ -332,7 +332,7 @@ int RSA_private_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
|
||||
return -1;
|
||||
}
|
||||
return out_len;
|
||||
return (int)out_len;
|
||||
}
|
||||
|
||||
int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
|
||||
@@ -347,7 +347,6 @@ int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
|
||||
int RSA_private_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
||||
int padding) {
|
||||
size_t out_len;
|
||||
|
||||
if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
|
||||
return -1;
|
||||
}
|
||||
@@ -356,13 +355,12 @@ int RSA_private_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
|
||||
return -1;
|
||||
}
|
||||
return out_len;
|
||||
return (int)out_len;
|
||||
}
|
||||
|
||||
int RSA_public_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
||||
int padding) {
|
||||
size_t out_len;
|
||||
|
||||
if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
|
||||
return -1;
|
||||
}
|
||||
@@ -371,15 +369,16 @@ int RSA_public_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
|
||||
return -1;
|
||||
}
|
||||
return out_len;
|
||||
return (int)out_len;
|
||||
}
|
||||
|
||||
unsigned RSA_size(const RSA *rsa) {
|
||||
if (rsa->meth->size) {
|
||||
return rsa->meth->size(rsa);
|
||||
}
|
||||
|
||||
return rsa_default_size(rsa);
|
||||
size_t ret = rsa->meth->size ? rsa->meth->size(rsa) : rsa_default_size(rsa);
|
||||
// RSA modulus sizes are bounded by |BIGNUM|, which must fit in |unsigned|.
|
||||
//
|
||||
// TODO(https://crbug.com/boringssl/516): Should we make this return |size_t|?
|
||||
assert(ret < UINT_MAX);
|
||||
return (unsigned)ret;
|
||||
}
|
||||
|
||||
int RSA_is_opaque(const RSA *rsa) {
|
||||
@@ -474,8 +473,6 @@ static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
|
||||
int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
|
||||
int *is_alloced, int hash_nid, const uint8_t *digest,
|
||||
size_t digest_len) {
|
||||
unsigned i;
|
||||
|
||||
if (hash_nid == NID_md5_sha1) {
|
||||
// Special case: SSL signature, just check the length.
|
||||
if (digest_len != SSL_SIG_LENGTH) {
|
||||
@@ -489,7 +486,7 @@ int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
|
||||
for (size_t i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
|
||||
const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
|
||||
if (sig_prefix->nid != hash_nid) {
|
||||
continue;
|
||||
@@ -501,17 +498,14 @@ int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
|
||||
}
|
||||
|
||||
const uint8_t* prefix = sig_prefix->bytes;
|
||||
unsigned prefix_len = sig_prefix->len;
|
||||
unsigned signed_msg_len;
|
||||
uint8_t *signed_msg;
|
||||
|
||||
signed_msg_len = prefix_len + digest_len;
|
||||
size_t prefix_len = sig_prefix->len;
|
||||
size_t signed_msg_len = prefix_len + digest_len;
|
||||
if (signed_msg_len < prefix_len) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
|
||||
signed_msg = OPENSSL_malloc(signed_msg_len);
|
||||
uint8_t *signed_msg = OPENSSL_malloc(signed_msg_len);
|
||||
if (!signed_msg) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
@@ -554,7 +548,12 @@ int rsa_sign_no_self_test(int hash_nid, const uint8_t *digest,
|
||||
goto err;
|
||||
}
|
||||
|
||||
*out_len = size_t_out_len;
|
||||
if (size_t_out_len > UINT_MAX) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
|
||||
goto err;
|
||||
}
|
||||
|
||||
*out_len = (unsigned)size_t_out_len;
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
|
||||
@@ -64,7 +64,6 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
#include <CCryptoBoringSSL_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 <CCryptoBoringSSL_aead.h>
|
||||
#include <CCryptoBoringSSL_aes.h>
|
||||
#include <CCryptoBoringSSL_bn.h>
|
||||
#include <CCryptoBoringSSL_ctrdrbg.h>
|
||||
#include <CCryptoBoringSSL_dh.h>
|
||||
#include <CCryptoBoringSSL_digest.h>
|
||||
#include <CCryptoBoringSSL_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;
|
||||
|
||||
Loaded 100 of 233 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user