mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-01 10:06:53 +03:00
Merge remote-tracking branch 'origin/development' into asn1write-size
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
option(USE_STATIC_MBEDTLS_LIBRARY "Build mbed TLS static library." ON)
|
||||
option(USE_SHARED_MBEDTLS_LIBRARY "Build mbed TLS shared library." OFF)
|
||||
option(LINK_WITH_PTHREAD "Explicitly link mbed TLS library to pthread." OFF)
|
||||
option(LINK_WITH_TRUSTED_STORAGE "Explicitly link mbed TLS library to trusted_storage." OFF)
|
||||
option(USE_STATIC_MBEDTLS_LIBRARY "Build Mbed TLS static library." ON)
|
||||
option(USE_SHARED_MBEDTLS_LIBRARY "Build Mbed TLS shared library." OFF)
|
||||
option(LINK_WITH_PTHREAD "Explicitly link Mbed TLS library to pthread." OFF)
|
||||
option(LINK_WITH_TRUSTED_STORAGE "Explicitly link Mbed TLS library to trusted_storage." OFF)
|
||||
|
||||
# Set the project root directory if it's not already defined, as may happen if
|
||||
# the library folder is included directly by a parent project, without
|
||||
|
@ -34,20 +34,19 @@
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/error.h"
|
||||
|
||||
#if defined(__aarch64__)
|
||||
#if defined(MBEDTLS_ARCH_IS_ARM64)
|
||||
#if !defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
|
||||
#error "MBEDTLS_AES_USE_HARDWARE_ONLY defined, but not all prerequisites"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__amd64__) || defined(__x86_64__) || \
|
||||
((defined(_M_X64) || defined(_M_AMD64)) && !defined(_M_ARM64EC))
|
||||
#if defined(MBEDTLS_ARCH_IS_X64)
|
||||
#if !defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
|
||||
#error "MBEDTLS_AES_USE_HARDWARE_ONLY defined, but not all prerequisites"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__i386__) || defined(_M_IX86)
|
||||
#if defined(MBEDTLS_ARCH_IS_X86)
|
||||
#if defined(MBEDTLS_AES_USE_HARDWARE_ONLY) && !defined(MBEDTLS_AESNI_C)
|
||||
#error "MBEDTLS_AES_USE_HARDWARE_ONLY defined, but not all prerequisites"
|
||||
#endif
|
||||
@ -652,7 +651,7 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(MBEDTLS_AESCE_HAVE_CODE)
|
||||
if (MBEDTLS_AESCE_HAS_SUPPORT()) {
|
||||
return mbedtls_aesce_setkey_enc((unsigned char *) RK, key, keybits);
|
||||
}
|
||||
@ -764,7 +763,7 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(MBEDTLS_AESCE_HAVE_CODE)
|
||||
if (MBEDTLS_AESCE_HAS_SUPPORT()) {
|
||||
mbedtls_aesce_inverse_key(
|
||||
(unsigned char *) RK,
|
||||
@ -1091,7 +1090,7 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(MBEDTLS_AESCE_HAVE_CODE)
|
||||
if (MBEDTLS_AESCE_HAS_SUPPORT()) {
|
||||
return mbedtls_aesce_crypt_ecb(ctx, mode, input, output);
|
||||
}
|
||||
@ -1910,7 +1909,7 @@ int mbedtls_aes_self_test(int verbose)
|
||||
mbedtls_printf(" AES note: using VIA Padlock.\n");
|
||||
} else
|
||||
#endif
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(MBEDTLS_AESCE_HAVE_CODE)
|
||||
if (MBEDTLS_AESCE_HAS_SUPPORT()) {
|
||||
mbedtls_printf(" AES note: using AESCE.\n");
|
||||
} else
|
||||
|
@ -46,7 +46,7 @@
|
||||
|
||||
#include "aesce.h"
|
||||
|
||||
#if defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(MBEDTLS_ARCH_IS_ARM64)
|
||||
|
||||
/* Compiler version checks. */
|
||||
#if defined(__clang__)
|
||||
@ -510,6 +510,6 @@ void mbedtls_aesce_gcm_mult(unsigned char c[16],
|
||||
#undef MBEDTLS_POP_TARGET_PRAGMA
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_HAVE_ARM64 */
|
||||
#endif /* MBEDTLS_ARCH_IS_ARM64 */
|
||||
|
||||
#endif /* MBEDTLS_AESCE_C */
|
||||
|
@ -30,13 +30,10 @@
|
||||
|
||||
#include "mbedtls/aes.h"
|
||||
|
||||
#if !defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
|
||||
#define MBEDTLS_HAVE_ARM64
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_ARCH_IS_ARM64)
|
||||
|
||||
#define MBEDTLS_AESCE_HAVE_CODE
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -131,6 +128,6 @@ int mbedtls_aesce_setkey_enc(unsigned char *rk,
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_HAVE_ARM64 */
|
||||
#endif /* MBEDTLS_AESCE_C && MBEDTLS_ARCH_IS_ARM64 */
|
||||
|
||||
#endif /* MBEDTLS_AESCE_H */
|
||||
|
@ -32,23 +32,8 @@
|
||||
#define MBEDTLS_AESNI_AES 0x02000000u
|
||||
#define MBEDTLS_AESNI_CLMUL 0x00000002u
|
||||
|
||||
/* Can we do AESNI with inline assembly?
|
||||
* (Only implemented with gas syntax, only for 64-bit.)
|
||||
*/
|
||||
#if !defined(MBEDTLS_HAVE_X86_64) && \
|
||||
(defined(__amd64__) || defined(__x86_64__) || \
|
||||
defined(_M_X64) || defined(_M_AMD64)) && \
|
||||
!defined(_M_ARM64EC)
|
||||
#define MBEDTLS_HAVE_X86_64
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_HAVE_X86) && \
|
||||
(defined(__i386__) || defined(_M_IX86))
|
||||
#define MBEDTLS_HAVE_X86
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESNI_C) && \
|
||||
(defined(MBEDTLS_HAVE_X86_64) || defined(MBEDTLS_HAVE_X86))
|
||||
(defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_X86))
|
||||
|
||||
/* Can we do AESNI with intrinsics?
|
||||
* (Only implemented with certain compilers, only for certain targets.)
|
||||
@ -75,7 +60,10 @@
|
||||
#if defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
|
||||
#define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
|
||||
#elif defined(MBEDTLS_HAVE_ASM) && \
|
||||
defined(__GNUC__) && defined(MBEDTLS_HAVE_X86_64)
|
||||
defined(__GNUC__) && defined(MBEDTLS_ARCH_IS_X64)
|
||||
/* Can we do AESNI with inline assembly?
|
||||
* (Only implemented with gas syntax, only for 64-bit.)
|
||||
*/
|
||||
#define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
|
||||
#elif defined(__GNUC__)
|
||||
# error "Must use `-mpclmul -msse2 -maes` for MBEDTLS_AESNI_C"
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_ASN1_PARSE_C)
|
||||
#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_X509_CREATE_C)
|
||||
|
||||
#include "mbedtls/asn1.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
@ -114,7 +114,9 @@ int mbedtls_asn1_get_tag(unsigned char **p,
|
||||
|
||||
return mbedtls_asn1_get_len(p, end, len);
|
||||
}
|
||||
#endif /* MBEDTLS_ASN1_PARSE_C || MBEDTLS_X509_CREATE_C */
|
||||
|
||||
#if defined(MBEDTLS_ASN1_PARSE_C)
|
||||
int mbedtls_asn1_get_bool(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
int *val)
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_ASN1_WRITE_C)
|
||||
#if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C)
|
||||
|
||||
#include "mbedtls/asn1write.h"
|
||||
#include "mbedtls/error.h"
|
||||
@ -74,7 +74,9 @@ int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsign
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C */
|
||||
|
||||
#if defined(MBEDTLS_ASN1_WRITE_C)
|
||||
static int mbedtls_asn1_write_len_and_tag(unsigned char **p,
|
||||
const unsigned char *start,
|
||||
size_t len,
|
||||
|
@ -72,7 +72,7 @@ int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
|
||||
}
|
||||
|
||||
/*
|
||||
* Set sign_N to 1 if N >= 0, 0 if N < 0.
|
||||
* Set N_is_negative to MBEDTLS_CT_FALSE if N >= 0, MBEDTLS_CT_TRUE if N < 0.
|
||||
* We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
|
||||
*/
|
||||
X_is_negative = mbedtls_ct_bool((X->s & 2) >> 1);
|
||||
@ -83,7 +83,7 @@ int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
|
||||
* That is if X is negative (X_is_negative == 1), then X < Y is true and it
|
||||
* is false if X is positive (X_is_negative == 0).
|
||||
*/
|
||||
different_sign = mbedtls_ct_bool_xor(X_is_negative, Y_is_negative); // non-zero if different sign
|
||||
different_sign = mbedtls_ct_bool_xor(X_is_negative, Y_is_negative); // true if different sign
|
||||
result = mbedtls_ct_bool_and(different_sign, X_is_negative);
|
||||
|
||||
/*
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* \file cipher.c
|
||||
*
|
||||
* \brief Generic cipher wrapper for mbed TLS
|
||||
* \brief Generic cipher wrapper for Mbed TLS
|
||||
*
|
||||
* \author Adriaan de Jong <dejong@fox-it.com>
|
||||
*
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* \file cipher_wrap.c
|
||||
*
|
||||
* \brief Generic cipher wrapper for mbed TLS
|
||||
* \brief Generic cipher wrapper for Mbed TLS
|
||||
*
|
||||
* \author Adriaan de Jong <dejong@fox-it.com>
|
||||
*
|
||||
|
@ -1,15 +1,6 @@
|
||||
/**
|
||||
* Constant-time functions
|
||||
*
|
||||
* For readability, the static inline definitions are here, and
|
||||
* constant_time_internal.h has only the declarations.
|
||||
*
|
||||
* This results in duplicate declarations of the form:
|
||||
* static inline void f() { ... }
|
||||
* static inline void f();
|
||||
* when constant_time_internal.h is included. This appears to behave
|
||||
* exactly as if the declaration-without-definition was not present.
|
||||
*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
@ -37,11 +28,20 @@
|
||||
#include "mbedtls/bignum.h"
|
||||
#endif
|
||||
|
||||
/* constant_time_impl.h contains all the static inline implementations,
|
||||
* so that constant_time_internal.h is more readable.
|
||||
/*
|
||||
* To improve readability of constant_time_internal.h, the static inline
|
||||
* definitions are here, and constant_time_internal.h has only the declarations.
|
||||
*
|
||||
* gcc generates warnings about duplicate declarations, so disable this
|
||||
* warning.
|
||||
* This results in duplicate declarations of the form:
|
||||
* static inline void f(); // from constant_time_internal.h
|
||||
* static inline void f() { ... } // from constant_time_impl.h
|
||||
* when constant_time_internal.h is included.
|
||||
*
|
||||
* This appears to behave as if the declaration-without-definition was not present
|
||||
* (except for warnings if gcc -Wredundant-decls or similar is used).
|
||||
*
|
||||
* Disable -Wredundant-decls so that gcc does not warn about this. This is re-enabled
|
||||
* at the bottom of this file.
|
||||
*/
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
@ -71,6 +71,10 @@
|
||||
#define MBEDTLS_CT_ARM_ASM
|
||||
#elif defined(__aarch64__)
|
||||
#define MBEDTLS_CT_AARCH64_ASM
|
||||
#elif defined(__amd64__) || defined(__x86_64__)
|
||||
#define MBEDTLS_CT_X86_64_ASM
|
||||
#elif defined(__i386__)
|
||||
#define MBEDTLS_CT_X86_ASM
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@ -132,7 +136,7 @@ static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
|
||||
* seem to apply unified syntax globally, which breaks other asm code.
|
||||
*/
|
||||
#if !defined(__clang__)
|
||||
#define RESTORE_ASM_SYNTAX ".syntax divided \n\t"
|
||||
#define RESTORE_ASM_SYNTAX ".syntax divided \n\t"
|
||||
#else
|
||||
#define RESTORE_ASM_SYNTAX
|
||||
#endif
|
||||
@ -150,9 +154,9 @@ static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
|
||||
*/
|
||||
#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
||||
mbedtls_ct_uint_t s;
|
||||
asm volatile ("neg %x[s], %x[x] \n\t"
|
||||
"orr %x[x], %x[s], %x[x] \n\t"
|
||||
"asr %x[x], %x[x], 63"
|
||||
asm volatile ("neg %x[s], %x[x] \n\t"
|
||||
"orr %x[x], %x[s], %x[x] \n\t"
|
||||
"asr %x[x], %x[x], 63 \n\t"
|
||||
:
|
||||
[s] "=&r" (s),
|
||||
[x] "+&r" (x)
|
||||
@ -162,10 +166,10 @@ static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
|
||||
return (mbedtls_ct_condition_t) x;
|
||||
#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
||||
uint32_t s;
|
||||
asm volatile (".syntax unified \n\t"
|
||||
"negs %[s], %[x] \n\t"
|
||||
"orrs %[x], %[x], %[s] \n\t"
|
||||
"asrs %[x], %[x], #31 \n\t"
|
||||
asm volatile (".syntax unified \n\t"
|
||||
"negs %[s], %[x] \n\t"
|
||||
"orrs %[x], %[x], %[s] \n\t"
|
||||
"asrs %[x], %[x], #31 \n\t"
|
||||
RESTORE_ASM_SYNTAX
|
||||
:
|
||||
[s] "=&l" (s),
|
||||
@ -175,6 +179,32 @@ static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
|
||||
"cc" /* clobbers flag bits */
|
||||
);
|
||||
return (mbedtls_ct_condition_t) x;
|
||||
#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
||||
uint64_t s;
|
||||
asm volatile ("mov %[x], %[s] \n\t"
|
||||
"neg %[s] \n\t"
|
||||
"or %[x], %[s] \n\t"
|
||||
"sar $63, %[s] \n\t"
|
||||
:
|
||||
[s] "=&a" (s)
|
||||
:
|
||||
[x] "D" (x)
|
||||
:
|
||||
);
|
||||
return (mbedtls_ct_condition_t) s;
|
||||
#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
||||
uint32_t s;
|
||||
asm volatile ("mov %[x], %[s] \n\t"
|
||||
"neg %[s] \n\t"
|
||||
"or %[s], %[x] \n\t"
|
||||
"sar $31, %[x] \n\t"
|
||||
:
|
||||
[s] "=&c" (s),
|
||||
[x] "+&a" (x)
|
||||
:
|
||||
:
|
||||
);
|
||||
return (mbedtls_ct_condition_t) x;
|
||||
#else
|
||||
const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
|
||||
#if defined(_MSC_VER)
|
||||
@ -202,9 +232,9 @@ static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
|
||||
mbedtls_ct_uint_t if0)
|
||||
{
|
||||
#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
||||
asm volatile ("and %x[if1], %x[if1], %x[condition] \n\t"
|
||||
"mvn %x[condition], %x[condition] \n\t"
|
||||
"and %x[condition], %x[condition], %x[if0] \n\t"
|
||||
asm volatile ("and %x[if1], %x[if1], %x[condition] \n\t"
|
||||
"mvn %x[condition], %x[condition] \n\t"
|
||||
"and %x[condition], %x[condition], %x[if0] \n\t"
|
||||
"orr %x[condition], %x[if1], %x[condition]"
|
||||
:
|
||||
[condition] "+&r" (condition),
|
||||
@ -215,11 +245,11 @@ static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
|
||||
);
|
||||
return (mbedtls_ct_uint_t) condition;
|
||||
#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
||||
asm volatile (".syntax unified \n\t"
|
||||
"ands %[if1], %[if1], %[condition] \n\t"
|
||||
"mvns %[condition], %[condition] \n\t"
|
||||
"ands %[condition], %[condition], %[if0] \n\t"
|
||||
"orrs %[condition], %[if1], %[condition] \n\t"
|
||||
asm volatile (".syntax unified \n\t"
|
||||
"ands %[if1], %[if1], %[condition] \n\t"
|
||||
"mvns %[condition], %[condition] \n\t"
|
||||
"ands %[condition], %[condition], %[if0] \n\t"
|
||||
"orrs %[condition], %[if1], %[condition] \n\t"
|
||||
RESTORE_ASM_SYNTAX
|
||||
:
|
||||
[condition] "+&l" (condition),
|
||||
@ -230,6 +260,32 @@ static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
|
||||
"cc"
|
||||
);
|
||||
return (mbedtls_ct_uint_t) condition;
|
||||
#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
||||
asm volatile ("and %[condition], %[if1] \n\t"
|
||||
"not %[condition] \n\t"
|
||||
"and %[condition], %[if0] \n\t"
|
||||
"or %[if1], %[if0] \n\t"
|
||||
:
|
||||
[condition] "+&D" (condition),
|
||||
[if1] "+&S" (if1),
|
||||
[if0] "+&a" (if0)
|
||||
:
|
||||
:
|
||||
);
|
||||
return if0;
|
||||
#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
||||
asm volatile ("and %[condition], %[if1] \n\t"
|
||||
"not %[condition] \n\t"
|
||||
"and %[if0], %[condition] \n\t"
|
||||
"or %[condition], %[if1] \n\t"
|
||||
:
|
||||
[condition] "+&c" (condition),
|
||||
[if1] "+&a" (if1)
|
||||
:
|
||||
[if0] "b" (if0)
|
||||
:
|
||||
);
|
||||
return if1;
|
||||
#else
|
||||
mbedtls_ct_condition_t not_cond =
|
||||
(mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
|
||||
@ -241,39 +297,81 @@ static inline mbedtls_ct_condition_t mbedtls_ct_uint_lt(mbedtls_ct_uint_t x, mbe
|
||||
{
|
||||
#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
||||
uint64_t s1;
|
||||
asm volatile ("eor %x[s1], %x[y], %x[x] \n\t"
|
||||
"sub %x[x], %x[x], %x[y] \n\t"
|
||||
"bic %x[x], %x[x], %x[s1] \n\t"
|
||||
"and %x[s1], %x[s1], %x[y] \n\t"
|
||||
"orr %x[s1], %x[x], %x[s1] \n\t"
|
||||
asm volatile ("eor %x[s1], %x[y], %x[x] \n\t"
|
||||
"sub %x[x], %x[x], %x[y] \n\t"
|
||||
"bic %x[x], %x[x], %x[s1] \n\t"
|
||||
"and %x[s1], %x[s1], %x[y] \n\t"
|
||||
"orr %x[s1], %x[x], %x[s1] \n\t"
|
||||
"asr %x[x], %x[s1], 63"
|
||||
: [s1] "=&r" (s1), [x] "+&r" (x)
|
||||
: [y] "r" (y)
|
||||
:
|
||||
[s1] "=&r" (s1),
|
||||
[x] "+&r" (x)
|
||||
:
|
||||
[y] "r" (y)
|
||||
:
|
||||
);
|
||||
return (mbedtls_ct_condition_t) x;
|
||||
#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
||||
uint32_t s1;
|
||||
asm volatile (
|
||||
".syntax unified \n\t"
|
||||
".syntax unified \n\t"
|
||||
#if defined(__thumb__) && !defined(__thumb2__)
|
||||
"movs %[s1], %[x] \n\t"
|
||||
"eors %[s1], %[s1], %[y] \n\t"
|
||||
"movs %[s1], %[x] \n\t"
|
||||
"eors %[s1], %[s1], %[y] \n\t"
|
||||
#else
|
||||
"eors %[s1], %[x], %[y] \n\t"
|
||||
"eors %[s1], %[x], %[y] \n\t"
|
||||
#endif
|
||||
"subs %[x], %[x], %[y] \n\t"
|
||||
"bics %[x], %[x], %[s1] \n\t"
|
||||
"ands %[y], %[s1], %[y] \n\t"
|
||||
"orrs %[x], %[x], %[y] \n\t"
|
||||
"asrs %[x], %[x], #31 \n\t"
|
||||
"subs %[x], %[x], %[y] \n\t"
|
||||
"bics %[x], %[x], %[s1] \n\t"
|
||||
"ands %[y], %[s1], %[y] \n\t"
|
||||
"orrs %[x], %[x], %[y] \n\t"
|
||||
"asrs %[x], %[x], #31 \n\t"
|
||||
RESTORE_ASM_SYNTAX
|
||||
: [s1] "=&l" (s1), [x] "+&l" (x), [y] "+&l" (y)
|
||||
:
|
||||
[s1] "=&l" (s1),
|
||||
[x] "+&l" (x),
|
||||
[y] "+&l" (y)
|
||||
:
|
||||
:
|
||||
"cc"
|
||||
);
|
||||
return (mbedtls_ct_condition_t) x;
|
||||
#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
||||
uint64_t s;
|
||||
asm volatile ("mov %[x], %[s] \n\t"
|
||||
"xor %[y], %[s] \n\t"
|
||||
"sub %[y], %[x] \n\t"
|
||||
"and %[s], %[y] \n\t"
|
||||
"not %[s] \n\t"
|
||||
"and %[s], %[x] \n\t"
|
||||
"or %[y], %[x] \n\t"
|
||||
"sar $63, %[x] \n\t"
|
||||
:
|
||||
[s] "=&a" (s),
|
||||
[x] "+&D" (x),
|
||||
[y] "+&S" (y)
|
||||
:
|
||||
:
|
||||
);
|
||||
return (mbedtls_ct_condition_t) x;
|
||||
#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
||||
uint32_t s;
|
||||
asm volatile ("mov %[x], %[s] \n\t"
|
||||
"xor %[y], %[s] \n\t"
|
||||
"sub %[y], %[x] \n\t"
|
||||
"and %[s], %[y] \n\t"
|
||||
"not %[s] \n\t"
|
||||
"and %[s], %[x] \n\t"
|
||||
"or %[y], %[x] \n\t"
|
||||
"sar $31, %[x] \n\t"
|
||||
:
|
||||
[s] "=&b" (s),
|
||||
[x] "+&a" (x),
|
||||
[y] "+&c" (y)
|
||||
:
|
||||
:
|
||||
);
|
||||
return (mbedtls_ct_condition_t) x;
|
||||
#else
|
||||
/* Ensure that the compiler cannot optimise the following operations over x and y,
|
||||
* even if it knows the value of x and y.
|
||||
@ -431,6 +529,7 @@ static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t
|
||||
}
|
||||
|
||||
#ifdef __GNUC__
|
||||
/* Restore warnings for -Wredundant-decls on gcc */
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
|
@ -47,7 +47,7 @@
|
||||
* These are all named mbedtls_ct_<type>_if and mbedtls_ct_<type>_if_else_0
|
||||
* All arguments are considered secret.
|
||||
* example: size_t a = x ? b : c => a = mbedtls_ct_size_if(x, b, c)
|
||||
* example: unsigned a = x ? b : 0 => a = mbedtls_ct_uint__if_else_0(x, b)
|
||||
* example: unsigned a = x ? b : 0 => a = mbedtls_ct_uint_if_else_0(x, b)
|
||||
*
|
||||
* - block memory operations
|
||||
* Only some arguments are considered secret, as documented for each
|
||||
@ -455,8 +455,8 @@ void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,
|
||||
*
|
||||
* memcpy(dst, src + offset, len)
|
||||
*
|
||||
* This function copies \p len bytes from \p src_base + \p offset to \p
|
||||
* dst, with a code flow and memory access pattern that does not depend on
|
||||
* This function copies \p len bytes from \p src + \p offset to
|
||||
* \p dst, with a code flow and memory access pattern that does not depend on
|
||||
* \p offset, but only on \p offset_min, \p offset_max and \p len.
|
||||
*
|
||||
* \note This function reads from \p dest, but the value that
|
||||
|
@ -1131,7 +1131,7 @@ int mbedtls_ecjpake_self_test(int verbose)
|
||||
#if !defined(MBEDTLS_ECJPAKE_ALT)
|
||||
/* 'reference handshake' tests can only be run against implementations
|
||||
* for which we have 100% control over how the random ephemeral keys
|
||||
* are generated. This is only the case for the internal mbed TLS
|
||||
* are generated. This is only the case for the internal Mbed TLS
|
||||
* implementation, so these tests are skipped in case the internal
|
||||
* implementation is swapped out for an alternative one. */
|
||||
if (verbose != 0) {
|
||||
|
@ -50,7 +50,7 @@ int mbedtls_platform_entropy_poll(void *data,
|
||||
/**
|
||||
* \brief Entropy poll callback for a hardware source
|
||||
*
|
||||
* \warning This is not provided by mbed TLS!
|
||||
* \warning This is not provided by Mbed TLS!
|
||||
* See \c MBEDTLS_ENTROPY_HARDWARE_ALT in mbedtls_config.h.
|
||||
*
|
||||
* \note This must accept NULL as its first argument.
|
||||
|
@ -97,7 +97,7 @@ static int gcm_gen_table(mbedtls_gcm_context *ctx)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(MBEDTLS_AESCE_HAVE_CODE)
|
||||
if (MBEDTLS_AESCE_HAS_SUPPORT()) {
|
||||
return 0;
|
||||
}
|
||||
@ -208,7 +208,7 @@ static void gcm_mult(mbedtls_gcm_context *ctx, const unsigned char x[16],
|
||||
}
|
||||
#endif /* MBEDTLS_AESNI_HAVE_CODE */
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(MBEDTLS_AESCE_HAVE_CODE)
|
||||
if (MBEDTLS_AESCE_HAS_SUPPORT()) {
|
||||
unsigned char h[16];
|
||||
|
||||
@ -885,7 +885,7 @@ int mbedtls_gcm_self_test(int verbose)
|
||||
} else
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
|
||||
#if defined(MBEDTLS_AESCE_HAVE_CODE)
|
||||
if (MBEDTLS_AESCE_HAS_SUPPORT()) {
|
||||
mbedtls_printf(" GCM note: using AESCE.\n");
|
||||
} else
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* \file md.c
|
||||
*
|
||||
* \brief Generic message digest wrapper for mbed TLS
|
||||
* \brief Generic message digest wrapper for Mbed TLS
|
||||
*
|
||||
* \author Adriaan de Jong <dejong@fox-it.com>
|
||||
*
|
||||
|
@ -14,7 +14,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -14,7 +14,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -14,7 +14,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -14,7 +14,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -31,7 +31,7 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_HAVE_X86)
|
||||
#if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
|
||||
|
||||
/*
|
||||
* PadLock detection routine
|
||||
@ -162,6 +162,6 @@ int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_HAVE_X86 */
|
||||
#endif /* MBEDTLS_VIA_PADLOCK_HAVE_CODE */
|
||||
|
||||
#endif /* MBEDTLS_PADLOCK_C */
|
||||
|
@ -38,16 +38,17 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Some versions of ASan result in errors about not enough registers */
|
||||
#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) && \
|
||||
/*
|
||||
* - `padlock` is implements with GNUC assembly for x86 target.
|
||||
* - Some versions of ASan result in errors about not enough registers.
|
||||
*/
|
||||
#if defined(MBEDTLS_PADLOCK_C) && \
|
||||
defined(__GNUC__) && defined(MBEDTLS_ARCH_IS_X86) && \
|
||||
defined(MBEDTLS_HAVE_ASM) && \
|
||||
!defined(MBEDTLS_HAVE_ASAN)
|
||||
|
||||
#define MBEDTLS_VIA_PADLOCK_HAVE_CODE
|
||||
|
||||
#ifndef MBEDTLS_HAVE_X86
|
||||
#define MBEDTLS_HAVE_X86
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define MBEDTLS_PADLOCK_RNG 0x000C
|
||||
|
@ -77,7 +77,7 @@ static inline int mbedtls_pk_is_rfc8410(const mbedtls_pk_context *pk)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_PEM_WRITE_C)
|
||||
/* It is assumed that the input key is opaque */
|
||||
static psa_ecc_family_t pk_get_opaque_ec_family(const mbedtls_pk_context *pk)
|
||||
{
|
||||
@ -92,7 +92,7 @@ static psa_ecc_family_t pk_get_opaque_ec_family(const mbedtls_pk_context *pk)
|
||||
|
||||
return ec_family;
|
||||
}
|
||||
#endif /* MBETLS_USE_PSA_CRYPTO */
|
||||
#endif /* MBETLS_USE_PSA_CRYPTO && MBEDTLS_PEM_WRITE_C */
|
||||
#endif /* MBEDTLS_PK_HAVE_RFC8410_CURVES */
|
||||
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
||||
|
||||
|
@ -4174,7 +4174,7 @@ psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
|
||||
size_t default_iv_length;
|
||||
size_t default_iv_length = 0;
|
||||
|
||||
if (operation->id == 0) {
|
||||
status = PSA_ERROR_BAD_STATE;
|
||||
@ -4784,7 +4784,7 @@ psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
|
||||
size_t required_nonce_size;
|
||||
size_t required_nonce_size = 0;
|
||||
|
||||
*nonce_length = 0;
|
||||
|
||||
|
@ -225,12 +225,12 @@ psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
|
||||
const uint8_t *data,
|
||||
size_t data_length);
|
||||
|
||||
/** Convert an mbed TLS error code to a PSA error code
|
||||
/** Convert an Mbed TLS error code to a PSA error code
|
||||
*
|
||||
* \note This function is provided solely for the convenience of
|
||||
* Mbed TLS and may be removed at any time without notice.
|
||||
*
|
||||
* \param ret An mbed TLS-thrown error code
|
||||
* \param ret An Mbed TLS-thrown error code
|
||||
*
|
||||
* \return The corresponding PSA error code
|
||||
*/
|
||||
|
@ -264,7 +264,7 @@ int mbedtls_ssl_cache_set(void *data,
|
||||
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
|
||||
mbedtls_ssl_cache_entry *cur;
|
||||
|
||||
size_t session_serialized_len;
|
||||
size_t session_serialized_len = 0;
|
||||
unsigned char *session_serialized = NULL;
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* \file ssl_ciphersuites.c
|
||||
*
|
||||
* \brief SSL ciphersuites for mbed TLS
|
||||
* \brief SSL ciphersuites for Mbed TLS
|
||||
*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
|
@ -16,7 +16,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS ( https://tls.mbed.org )
|
||||
* This file is part of Mbed TLS ( https://tls.mbed.org )
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
@ -260,7 +260,7 @@ static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
|
||||
for (; *group_list != 0; group_list++) {
|
||||
int propose_group = 0;
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("got supported group(%04x)", *group_list));
|
||||
MBEDTLS_SSL_DEBUG_MSG(3, ("got supported group(%04x)", *group_list));
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
||||
if (flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG) {
|
||||
@ -648,14 +648,16 @@ static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
||||
if (
|
||||
int write_sig_alg_ext = 0;
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
||||
(propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl)) ||
|
||||
write_sig_alg_ext = write_sig_alg_ext ||
|
||||
(propose_tls13 && mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl));
|
||||
#endif
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
propose_tls12 ||
|
||||
write_sig_alg_ext = write_sig_alg_ext || propose_tls12;
|
||||
#endif
|
||||
0) {
|
||||
|
||||
if (write_sig_alg_ext) {
|
||||
ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
|
@ -1504,7 +1504,8 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
|
||||
int auth_done = 0;
|
||||
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
||||
size_t padlen = 0, correct = 1;
|
||||
size_t padlen = 0;
|
||||
mbedtls_ct_condition_t correct = MBEDTLS_CT_TRUE;
|
||||
#endif
|
||||
unsigned char *data;
|
||||
/* For an explanation of the additional data length see
|
||||
@ -1921,7 +1922,7 @@ hmac_failed_etm_enabled:
|
||||
const mbedtls_ct_condition_t ge = mbedtls_ct_uint_ge(
|
||||
rec->data_len,
|
||||
padlen + 1);
|
||||
correct = mbedtls_ct_size_if_else_0(ge, correct);
|
||||
correct = mbedtls_ct_bool_and(ge, correct);
|
||||
padlen = mbedtls_ct_size_if_else_0(ge, padlen);
|
||||
} else {
|
||||
#if defined(MBEDTLS_SSL_DEBUG_ALL)
|
||||
@ -1937,7 +1938,7 @@ hmac_failed_etm_enabled:
|
||||
const mbedtls_ct_condition_t ge = mbedtls_ct_uint_ge(
|
||||
rec->data_len,
|
||||
transform->maclen + padlen + 1);
|
||||
correct = mbedtls_ct_size_if_else_0(ge, correct);
|
||||
correct = mbedtls_ct_bool_and(ge, correct);
|
||||
padlen = mbedtls_ct_size_if_else_0(ge, padlen);
|
||||
}
|
||||
|
||||
@ -1973,14 +1974,14 @@ hmac_failed_etm_enabled:
|
||||
increment = mbedtls_ct_size_if_else_0(b, increment);
|
||||
pad_count += increment;
|
||||
}
|
||||
correct = mbedtls_ct_size_if_else_0(mbedtls_ct_uint_eq(pad_count, padlen), padlen);
|
||||
correct = mbedtls_ct_bool_and(mbedtls_ct_uint_eq(pad_count, padlen), correct);
|
||||
|
||||
#if defined(MBEDTLS_SSL_DEBUG_ALL)
|
||||
if (padlen > 0 && correct == 0) {
|
||||
if (padlen > 0 && correct == MBEDTLS_CT_FALSE) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected"));
|
||||
}
|
||||
#endif
|
||||
padlen = mbedtls_ct_size_if_else_0(mbedtls_ct_bool(correct), padlen);
|
||||
padlen = mbedtls_ct_size_if_else_0(correct, padlen);
|
||||
|
||||
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
||||
|
||||
@ -2075,7 +2076,7 @@ hmac_failed_etm_enabled:
|
||||
#if defined(MBEDTLS_SSL_DEBUG_ALL)
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
|
||||
#endif
|
||||
correct = 0;
|
||||
correct = MBEDTLS_CT_FALSE;
|
||||
}
|
||||
auth_done++;
|
||||
|
||||
@ -2090,7 +2091,7 @@ hmac_failed_etm_disabled:
|
||||
/*
|
||||
* Finally check the correct flag
|
||||
*/
|
||||
if (correct == 0) {
|
||||
if (correct == MBEDTLS_CT_FALSE) {
|
||||
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
||||
|
@ -4578,13 +4578,14 @@ static int ssl_context_load(mbedtls_ssl_context *ssl,
|
||||
* We can't check that the config matches the initial one, but we can at
|
||||
* least check it matches the requirements for serializing.
|
||||
*/
|
||||
if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
|
||||
ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
|
||||
ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
|
||||
if (
|
||||
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
||||
ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
|
||||
#endif
|
||||
0) {
|
||||
ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
|
||||
ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
|
||||
ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
|
||||
) {
|
||||
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS ( https://tls.mbed.org )
|
||||
* This file is part of Mbed TLS ( https://tls.mbed.org )
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
@ -837,6 +837,8 @@ int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
|
||||
ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
|
||||
|
||||
cleanup:
|
||||
#else /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
||||
(void) ssl;
|
||||
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
|
||||
|
103
library/x509.c
103
library/x509.c
@ -43,6 +43,8 @@
|
||||
#include "mbedtls/pem.h"
|
||||
#endif
|
||||
|
||||
#include "mbedtls/asn1write.h"
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
#if defined(MBEDTLS_HAVE_TIME)
|
||||
@ -810,6 +812,11 @@ int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char nibble_to_hex_digit(int i)
|
||||
{
|
||||
return (i < 10) ? (i + '0') : (i - 10 + 'A');
|
||||
}
|
||||
|
||||
/*
|
||||
* Store the name in printable form into buf; no more
|
||||
* than size characters will be written
|
||||
@ -817,11 +824,16 @@ int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
|
||||
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t i, j, n;
|
||||
size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start;
|
||||
/* 6 is enough as our asn1 write functions only write one byte for the tag and at most five bytes for the length*/
|
||||
unsigned char asn1_tag_len_buf[6];
|
||||
unsigned char *asn1_len_p;
|
||||
unsigned char c, merge = 0;
|
||||
const mbedtls_x509_name *name;
|
||||
const char *short_name = NULL;
|
||||
char lowbits, highbits;
|
||||
char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
|
||||
int print_hexstring;
|
||||
|
||||
memset(s, 0, sizeof(s));
|
||||
|
||||
@ -840,32 +852,91 @@ int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
|
||||
MBEDTLS_X509_SAFE_SNPRINTF;
|
||||
}
|
||||
|
||||
ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
|
||||
print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) &&
|
||||
(name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) &&
|
||||
(name->val.tag != MBEDTLS_ASN1_IA5_STRING);
|
||||
|
||||
if (ret == 0) {
|
||||
if ((ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name)) == 0) {
|
||||
ret = mbedtls_snprintf(p, n, "%s=", short_name);
|
||||
} else {
|
||||
ret = mbedtls_snprintf(p, n, "\?\?=");
|
||||
if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) {
|
||||
n -= ret;
|
||||
p += ret;
|
||||
ret = mbedtls_snprintf(p, n, "=");
|
||||
print_hexstring = 1;
|
||||
} else if (ret == MBEDTLS_ERR_OID_BUF_TOO_SMALL) {
|
||||
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
||||
} else {
|
||||
ret = mbedtls_snprintf(p, n, "\?\?=");
|
||||
}
|
||||
}
|
||||
MBEDTLS_X509_SAFE_SNPRINTF;
|
||||
|
||||
for (i = 0, j = 0; i < name->val.len; i++, j++) {
|
||||
if (j >= sizeof(s) - 1) {
|
||||
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
||||
}
|
||||
if (print_hexstring) {
|
||||
s[0] = '#';
|
||||
|
||||
c = name->val.p[i];
|
||||
// Special characters requiring escaping, RFC 1779
|
||||
if (c && strchr(",=+<>#;\"\\", c)) {
|
||||
asn1_len_p = asn1_tag_len_buf + sizeof(asn1_tag_len_buf);
|
||||
if ((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) {
|
||||
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
}
|
||||
asn1_len_size = ret;
|
||||
if ((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) {
|
||||
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
}
|
||||
asn1_tag_size = ret;
|
||||
asn1_tag_len_buf_start = sizeof(asn1_tag_len_buf) - asn1_len_size - asn1_tag_size;
|
||||
for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) {
|
||||
if (j + 1 >= sizeof(s) - 1) {
|
||||
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
||||
}
|
||||
s[j++] = '\\';
|
||||
c = asn1_tag_len_buf[asn1_tag_len_buf_start+i];
|
||||
lowbits = (c & 0x0F);
|
||||
highbits = c >> 4;
|
||||
s[j++] = nibble_to_hex_digit(highbits);
|
||||
s[j++] = nibble_to_hex_digit(lowbits);
|
||||
}
|
||||
if (c < 32 || c >= 127) {
|
||||
s[j] = '?';
|
||||
} else {
|
||||
s[j] = c;
|
||||
for (i = 0; i < name->val.len; i++) {
|
||||
if (j + 1 >= sizeof(s) - 1) {
|
||||
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
||||
}
|
||||
c = name->val.p[i];
|
||||
lowbits = (c & 0x0F);
|
||||
highbits = c >> 4;
|
||||
s[j++] = nibble_to_hex_digit(highbits);
|
||||
s[j++] = nibble_to_hex_digit(lowbits);
|
||||
}
|
||||
} else {
|
||||
for (i = 0, j = 0; i < name->val.len; i++, j++) {
|
||||
if (j >= sizeof(s) - 1) {
|
||||
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
c = name->val.p[i];
|
||||
// Special characters requiring escaping, RFC 4514 Section 2.4
|
||||
if (c == '\0') {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
} else {
|
||||
if (strchr(",=+<>;\"\\", c) ||
|
||||
((i == 0) && strchr("# ", c)) ||
|
||||
((i == name->val.len-1) && (c == ' '))) {
|
||||
if (j + 1 >= sizeof(s) - 1) {
|
||||
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
||||
}
|
||||
s[j++] = '\\';
|
||||
}
|
||||
}
|
||||
if (c < 32 || c >= 127) {
|
||||
if (j + 3 >= sizeof(s) - 1) {
|
||||
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
||||
}
|
||||
s[j++] = '\\';
|
||||
lowbits = (c & 0x0F);
|
||||
highbits = c >> 4;
|
||||
s[j++] = nibble_to_hex_digit(highbits);
|
||||
s[j] = nibble_to_hex_digit(lowbits);
|
||||
} else {
|
||||
s[j] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
s[j] = '\0';
|
||||
|
@ -28,6 +28,10 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
#include "mbedtls/asn1.h"
|
||||
|
||||
/* Structure linking OIDs for X.509 DN AttributeTypes to their
|
||||
* string representations and default string encodings used by Mbed TLS. */
|
||||
typedef struct {
|
||||
@ -35,7 +39,8 @@ typedef struct {
|
||||
* "CN" or "emailAddress". */
|
||||
size_t name_len; /* Length of 'name', without trailing 0 byte. */
|
||||
const char *oid; /* String representation of OID of AttributeType,
|
||||
* as per RFC 5280, Appendix A.1. */
|
||||
* as per RFC 5280, Appendix A.1. encoded as per
|
||||
* X.690 */
|
||||
int default_tag; /* The default character encoding used for the
|
||||
* given attribute type, e.g.
|
||||
* MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
|
||||
@ -123,79 +128,200 @@ static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name,
|
||||
return cur;
|
||||
}
|
||||
|
||||
static int hex_to_int(char c)
|
||||
{
|
||||
return ('0' <= c && c <= '9') ? (c - '0') :
|
||||
('a' <= c && c <= 'f') ? (c - 'a' + 10) :
|
||||
('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1;
|
||||
}
|
||||
|
||||
static int hexpair_to_int(const char *hexpair)
|
||||
{
|
||||
int n1 = hex_to_int(*hexpair);
|
||||
int n2 = hex_to_int(*(hexpair + 1));
|
||||
|
||||
if (n1 != -1 && n2 != -1) {
|
||||
return (n1 << 4) | n2;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int parse_attribute_value_string(const char *s,
|
||||
int len,
|
||||
unsigned char *data,
|
||||
size_t *data_len)
|
||||
{
|
||||
const char *c;
|
||||
const char *end = s + len;
|
||||
unsigned char *d = data;
|
||||
int n;
|
||||
|
||||
for (c = s; c < end; c++) {
|
||||
if (*c == '\\') {
|
||||
c++;
|
||||
|
||||
/* Check for valid escaped characters as per RFC 4514 Section 3 */
|
||||
if (c + 1 < end && (n = hexpair_to_int(c)) != -1) {
|
||||
if (n == 0) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
*(d++) = n;
|
||||
c++;
|
||||
} else if (c < end && strchr(" ,=+<>#;\"\\", *c)) {
|
||||
*(d++) = *c;
|
||||
} else {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
} else {
|
||||
*(d++) = *c;
|
||||
}
|
||||
|
||||
if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
}
|
||||
*data_len = d - data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_attribute_value_der_encoded(const char *s,
|
||||
int len,
|
||||
unsigned char *data,
|
||||
size_t *data_len,
|
||||
int *tag)
|
||||
{
|
||||
const char *c = s;
|
||||
const char *end = c + len;
|
||||
unsigned char asn1_der_buf[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
||||
unsigned char *asn1_der_end;
|
||||
unsigned char *p;
|
||||
unsigned char *d = data;
|
||||
int n;
|
||||
|
||||
/* Converting from hexstring to raw binary so we can use asn1parse.c */
|
||||
if ((len < 5) || (*c != '#')) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
c++;
|
||||
if ((*tag = hexpair_to_int(c)) == -1) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
c += 2;
|
||||
p = asn1_der_buf;
|
||||
for (p = asn1_der_buf; c < end; c += 2) {
|
||||
if ((c + 1 >= end) || (n = hexpair_to_int(c)) == -1) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
if (MBEDTLS_ASN1_IS_STRING_TAG(*tag) && n == 0) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
*(p++) = n;
|
||||
}
|
||||
asn1_der_end = p;
|
||||
|
||||
p = asn1_der_buf;
|
||||
if (mbedtls_asn1_get_len(&p, asn1_der_end, data_len) != 0) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
|
||||
while (p < asn1_der_end) {
|
||||
*(d++) = *(p++);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
int parse_ret = 0;
|
||||
const char *s = name, *c = s;
|
||||
const char *end = s + strlen(s);
|
||||
const char *oid = NULL;
|
||||
mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
|
||||
const x509_attr_descriptor_t *attr_descr = NULL;
|
||||
int in_tag = 1;
|
||||
char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
||||
char *d = data;
|
||||
int in_attr_type = 1;
|
||||
int tag;
|
||||
int numericoid = 0;
|
||||
unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
||||
size_t data_len = 0;
|
||||
|
||||
/* Clear existing chain if present */
|
||||
mbedtls_asn1_free_named_data_list(head);
|
||||
|
||||
while (c <= end) {
|
||||
if (in_tag && *c == '=') {
|
||||
if (in_attr_type && *c == '=') {
|
||||
if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) {
|
||||
ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
|
||||
goto exit;
|
||||
if ((mbedtls_oid_from_numeric_string(&oid, s, c - s)) != 0) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
} else {
|
||||
numericoid = 1;
|
||||
}
|
||||
} else {
|
||||
oid.len = strlen(attr_descr->oid);
|
||||
oid.p = mbedtls_calloc(1, oid.len);
|
||||
memcpy(oid.p, attr_descr->oid, oid.len);
|
||||
numericoid = 0;
|
||||
}
|
||||
|
||||
oid = attr_descr->oid;
|
||||
s = c + 1;
|
||||
in_tag = 0;
|
||||
d = data;
|
||||
in_attr_type = 0;
|
||||
}
|
||||
|
||||
if (!in_tag && *c == '\\' && c != end) {
|
||||
c++;
|
||||
|
||||
/* Check for valid escaped characters */
|
||||
if (c == end || *c != ',') {
|
||||
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
goto exit;
|
||||
if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
|
||||
if (s == c) {
|
||||
mbedtls_free(oid.p);
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
} else if (*s == '#') {
|
||||
if ((parse_ret =
|
||||
parse_attribute_value_der_encoded(s, (int) (c - s), data, &data_len,
|
||||
&tag)) != 0) {
|
||||
mbedtls_free(oid.p);
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
} else {
|
||||
if (numericoid) {
|
||||
mbedtls_free(oid.p);
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
} else {
|
||||
if ((parse_ret =
|
||||
parse_attribute_value_string(s, (int) (c - s), data,
|
||||
&data_len)) != 0) {
|
||||
mbedtls_free(oid.p);
|
||||
return parse_ret;
|
||||
}
|
||||
tag = attr_descr->default_tag;
|
||||
}
|
||||
}
|
||||
} else if (!in_tag && (*c == ',' || c == end)) {
|
||||
mbedtls_asn1_named_data *cur =
|
||||
mbedtls_asn1_store_named_data(head, oid, strlen(oid),
|
||||
(unsigned char *) data,
|
||||
d - data);
|
||||
|
||||
mbedtls_asn1_named_data *cur =
|
||||
mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
|
||||
(unsigned char *) data,
|
||||
data_len);
|
||||
mbedtls_free(oid.p);
|
||||
oid.p = NULL;
|
||||
if (cur == NULL) {
|
||||
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
||||
}
|
||||
|
||||
// set tagType
|
||||
cur->val.tag = attr_descr->default_tag;
|
||||
cur->val.tag = tag;
|
||||
|
||||
while (c < end && *(c + 1) == ' ') {
|
||||
c++;
|
||||
}
|
||||
|
||||
s = c + 1;
|
||||
in_tag = 1;
|
||||
in_attr_type = 1;
|
||||
|
||||
/* Successfully parsed one name, update ret to success */
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
if (!in_tag && s != c + 1) {
|
||||
*(d++) = *c;
|
||||
|
||||
if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
|
||||
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
c++;
|
||||
}
|
||||
|
||||
exit:
|
||||
|
||||
if (oid.p != NULL) {
|
||||
mbedtls_free(oid.p);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user