Update Files
This commit is contained in:
135
Kinc/Sources/kinc/libs/tls/mbedtls/CMakeLists.txt
Normal file
135
Kinc/Sources/kinc/libs/tls/mbedtls/CMakeLists.txt
Normal file
@ -0,0 +1,135 @@
|
||||
#
|
||||
# libwebsockets - small server side websockets and web server implementation
|
||||
#
|
||||
# Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to
|
||||
# deal in the Software without restriction, including without limitation the
|
||||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
# sell copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
# IN THE SOFTWARE.
|
||||
#
|
||||
# The strategy is to only export to PARENT_SCOPE
|
||||
#
|
||||
# - changes to LIB_LIST
|
||||
# - changes to SOURCES
|
||||
# - includes via include_directories
|
||||
#
|
||||
# and keep everything else private
|
||||
|
||||
include_directories(wrapper/include wrapper/include/internal)
|
||||
|
||||
set(LWS_WITH_SSL ON)
|
||||
|
||||
include_directories(wrapper/include)
|
||||
include_directories(wrapper/include/platform)
|
||||
include_directories(wrapper/include/internal)
|
||||
include_directories(wrapper/include/openssl)
|
||||
|
||||
if (LWS_WITH_NETWORK)
|
||||
list(APPEND HDR_PRIVATE
|
||||
tls/mbedtls/wrapper/include/internal/ssl3.h
|
||||
tls/mbedtls/wrapper/include/internal/ssl_cert.h
|
||||
tls/mbedtls/wrapper/include/internal/ssl_code.h
|
||||
tls/mbedtls/wrapper/include/internal/ssl_dbg.h
|
||||
tls/mbedtls/wrapper/include/internal/ssl_lib.h
|
||||
tls/mbedtls/wrapper/include/internal/ssl_methods.h
|
||||
tls/mbedtls/wrapper/include/internal/ssl_pkey.h
|
||||
tls/mbedtls/wrapper/include/internal/ssl_stack.h
|
||||
tls/mbedtls/wrapper/include/internal/ssl_types.h
|
||||
tls/mbedtls/wrapper/include/internal/ssl_x509.h
|
||||
tls/mbedtls/wrapper/include/internal/tls1.h
|
||||
tls/mbedtls/wrapper/include/internal/x509_vfy.h)
|
||||
|
||||
list(APPEND HDR_PRIVATE
|
||||
tls/mbedtls/wrapper/include/openssl/ssl.h)
|
||||
|
||||
list(APPEND HDR_PRIVATE
|
||||
tls/mbedtls/wrapper/include/platform/ssl_pm.h
|
||||
tls/mbedtls/wrapper/include/platform/ssl_port.h)
|
||||
|
||||
list(APPEND SOURCES
|
||||
tls/mbedtls/wrapper/library/ssl_cert.c
|
||||
tls/mbedtls/wrapper/library/ssl_lib.c
|
||||
tls/mbedtls/wrapper/library/ssl_methods.c
|
||||
tls/mbedtls/wrapper/library/ssl_pkey.c
|
||||
tls/mbedtls/wrapper/library/ssl_stack.c
|
||||
tls/mbedtls/wrapper/library/ssl_x509.c)
|
||||
|
||||
list(APPEND SOURCES
|
||||
tls/mbedtls/wrapper/platform/ssl_pm.c
|
||||
tls/mbedtls/wrapper/platform/ssl_port.c)
|
||||
endif()
|
||||
|
||||
set(_WANT_MBT 0)
|
||||
if (NOT LWS_PLAT_FREERTOS)
|
||||
if (NOT DEFINED LWS_MBEDTLS_LIBRARIES)
|
||||
set(_WANT_MBT 1)
|
||||
endif()
|
||||
if (NOT DEFINED LWS_MBEDTLS_INCLUDE_DIRS)
|
||||
set(_WANT_MBT 1)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (_WANT_MBT)
|
||||
|
||||
find_path(LWS_MBEDTLS_INCLUDE_DIRS mbedtls/ssl.h)
|
||||
|
||||
find_library(MBEDTLS_LIBRARY mbedtls)
|
||||
find_library(MBEDX509_LIBRARY mbedx509)
|
||||
find_library(MBEDCRYPTO_LIBRARY mbedcrypto)
|
||||
|
||||
set(LWS_MBEDTLS_LIBRARIES "${MBEDTLS_LIBRARY}" "${MBEDX509_LIBRARY}" "${MBEDCRYPTO_LIBRARY}")
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(MBEDTLS DEFAULT_MSG
|
||||
LWS_MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
|
||||
|
||||
mark_as_advanced(LWS_MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
|
||||
|
||||
if ("${LWS_MBEDTLS_LIBRARIES}" STREQUAL "" OR "${LWS_MBEDTLS_INCLUDE_DIRS}" STREQUAL "")
|
||||
message(FATAL_ERROR "You must set LWS_MBEDTLS_LIBRARIES and LWS_MBEDTLS_INCLUDE_DIRS when LWS_WITH_MBEDTLS is turned on.")
|
||||
endif()
|
||||
endif()
|
||||
if (LWS_MBEDTLS_LIBRARIES)
|
||||
set(MBEDTLS_LIBRARIES ${LWS_MBEDTLS_LIBRARIES})
|
||||
set(MBEDTLS_LIBRARIES ${LWS_MBEDTLS_LIBRARIES} PARENT_SCOPE)
|
||||
endif()
|
||||
if (LWS_MBEDTLS_INCLUDE_DIRS)
|
||||
set(MBEDTLS_INCLUDE_DIRS ${LWS_MBEDTLS_INCLUDE_DIRS})
|
||||
set(MBEDTLS_INCLUDE_DIRS ${LWS_MBEDTLS_INCLUDE_DIRS} PARENT_SCOPE)
|
||||
endif()
|
||||
set(USE_MBEDTLS 1 PARENT_SCOPE)
|
||||
if (DEFINED MBEDTLS_INCLUDE_DIRS)
|
||||
include_directories(${MBEDTLS_INCLUDE_DIRS})
|
||||
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${MBEDTLS_INCLUDE_DIRS})
|
||||
endif()
|
||||
|
||||
if (DEFINED MBEDTLS_LIBRARIES)
|
||||
list(APPEND LIB_LIST ${MBEDTLS_LIBRARIES})
|
||||
endif()
|
||||
|
||||
# old mbedtls has everything in mbedtls/net.h
|
||||
|
||||
CHECK_C_SOURCE_COMPILES("#include <mbedtls/net_sockets.h>\nint main(void) { return 0;}\n" LWS_HAVE_MBEDTLS_NET_SOCKETS)
|
||||
CHECK_C_SOURCE_COMPILES("#include <mbedtls/ssl.h>\nint main(void) { return MBEDTLS_SSL_NEW_SESSION_TICKET;}\n" LWS_HAVE_MBEDTLS_SSL_NEW_SESSION_TICKET)
|
||||
|
||||
#
|
||||
# Keep explicit parent scope exports at end
|
||||
#
|
||||
|
||||
exports_to_parent_scope()
|
||||
set(LWS_HAVE_MBEDTLS_NET_SOCKETS ${LWS_HAVE_MBEDTLS_NET_SOCKETS} PARENT_SCOPE)
|
||||
set(LWS_HAVE_MBEDTLS_SSL_NEW_SESSION_TICKET ${LWS_HAVE_MBEDTLS_SSL_NEW_SESSION_TICKET} PARENT_SCOPE)
|
447
Kinc/Sources/kinc/libs/tls/mbedtls/lws-genaes.c
Normal file
447
Kinc/Sources/kinc/libs/tls/mbedtls/lws-genaes.c
Normal file
@ -0,0 +1,447 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* lws_genaes provides an abstraction api for AES in lws that works the
|
||||
* same whether you are using openssl or mbedtls hash functions underneath.
|
||||
*/
|
||||
#include "private-lib-core.h"
|
||||
#if defined(LWS_WITH_JOSE)
|
||||
#include "private-lib-jose.h"
|
||||
#endif
|
||||
|
||||
static int operation_map[] = { MBEDTLS_AES_ENCRYPT, MBEDTLS_AES_DECRYPT };
|
||||
|
||||
static unsigned int
|
||||
_write_pkcs7_pad(uint8_t *p, int len)
|
||||
{
|
||||
unsigned int n = 0, padlen = LWS_AES_CBC_BLOCKLEN * ((unsigned int)len /
|
||||
LWS_AES_CBC_BLOCKLEN + 1) - (unsigned int)len;
|
||||
|
||||
p += len;
|
||||
|
||||
while (n++ < padlen)
|
||||
*p++ = (uint8_t)padlen;
|
||||
|
||||
return padlen;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genaes_create(struct lws_genaes_ctx *ctx, enum enum_aes_operation op,
|
||||
enum enum_aes_modes mode, struct lws_gencrypto_keyelem *el,
|
||||
enum enum_aes_padding padding, void *engine)
|
||||
{
|
||||
int n = 0;
|
||||
|
||||
ctx->mode = mode;
|
||||
ctx->k = el;
|
||||
ctx->op = (enum enum_aes_operation)operation_map[op];
|
||||
ctx->underway = 0;
|
||||
ctx->padding = padding == LWS_GAESP_WITH_PADDING;
|
||||
|
||||
switch (ctx->mode) {
|
||||
case LWS_GAESM_XTS:
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
mbedtls_aes_xts_init(&ctx->u.ctx_xts);
|
||||
break;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
case LWS_GAESM_GCM:
|
||||
mbedtls_gcm_init(&ctx->u.ctx_gcm);
|
||||
n = mbedtls_gcm_setkey(&ctx->u.ctx_gcm, MBEDTLS_CIPHER_ID_AES,
|
||||
ctx->k->buf, ctx->k->len * 8);
|
||||
if (n) {
|
||||
lwsl_notice("%s: mbedtls_gcm_setkey: -0x%x\n",
|
||||
__func__, -n);
|
||||
return n;
|
||||
}
|
||||
return n;
|
||||
default:
|
||||
mbedtls_aes_init(&ctx->u.ctx);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (op) {
|
||||
case LWS_GAESO_ENC:
|
||||
if (ctx->mode == LWS_GAESM_XTS)
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
n = mbedtls_aes_xts_setkey_enc(&ctx->u.ctx_xts,
|
||||
ctx->k->buf,
|
||||
ctx->k->len * 8);
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
else
|
||||
n = mbedtls_aes_setkey_enc(&ctx->u.ctx, ctx->k->buf,
|
||||
ctx->k->len * 8);
|
||||
break;
|
||||
case LWS_GAESO_DEC:
|
||||
switch (ctx->mode) {
|
||||
case LWS_GAESM_XTS:
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
n = mbedtls_aes_xts_setkey_dec(&ctx->u.ctx_xts,
|
||||
ctx->k->buf,
|
||||
ctx->k->len * 8);
|
||||
break;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
|
||||
case LWS_GAESM_CFB128:
|
||||
case LWS_GAESM_CFB8:
|
||||
case LWS_GAESM_CTR:
|
||||
case LWS_GAESM_OFB:
|
||||
n = mbedtls_aes_setkey_enc(&ctx->u.ctx, ctx->k->buf,
|
||||
ctx->k->len * 8);
|
||||
break;
|
||||
default:
|
||||
n = mbedtls_aes_setkey_dec(&ctx->u.ctx, ctx->k->buf,
|
||||
ctx->k->len * 8);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (n)
|
||||
lwsl_notice("%s: setting key: -0x%x\n", __func__, -n);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genaes_destroy(struct lws_genaes_ctx *ctx, unsigned char *tag, size_t tlen)
|
||||
{
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000
|
||||
size_t last_len = 0;
|
||||
uint8_t last[16];
|
||||
#endif
|
||||
int n;
|
||||
|
||||
if (ctx->mode == LWS_GAESM_GCM) {
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000
|
||||
n = mbedtls_gcm_finish(&ctx->u.ctx_gcm, last, sizeof(last),
|
||||
&last_len, tag, tlen);
|
||||
#else
|
||||
n = mbedtls_gcm_finish(&ctx->u.ctx_gcm, tag, tlen);
|
||||
#endif
|
||||
|
||||
if (n)
|
||||
lwsl_notice("%s: mbedtls_gcm_finish: -0x%x\n",
|
||||
__func__, -n);
|
||||
if (tag && ctx->op == MBEDTLS_AES_DECRYPT && !n) {
|
||||
if (lws_timingsafe_bcmp(ctx->tag, tag, (unsigned int)ctx->taglen)) {
|
||||
lwsl_err("%s: lws_genaes_crypt tag "
|
||||
"mismatch (bad first)\n",
|
||||
__func__);
|
||||
lwsl_hexdump_notice(tag, tlen);
|
||||
lwsl_hexdump_notice(ctx->tag, (unsigned int)ctx->taglen);
|
||||
n = -1;
|
||||
}
|
||||
}
|
||||
mbedtls_gcm_free(&ctx->u.ctx_gcm);
|
||||
return n;
|
||||
}
|
||||
if (ctx->mode == LWS_GAESM_XTS)
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
mbedtls_aes_xts_free(&ctx->u.ctx_xts);
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
else
|
||||
mbedtls_aes_free(&ctx->u.ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(LWS_HAVE_mbedtls_internal_aes_encrypt)
|
||||
static int
|
||||
lws_genaes_rfc3394_wrap(int wrap, int cek_bits, const uint8_t *kek,
|
||||
int kek_bits, const uint8_t *in, uint8_t *out)
|
||||
{
|
||||
int n, m, ret = -1, c64 = cek_bits / 64;
|
||||
mbedtls_aes_context ctx;
|
||||
uint8_t a[8], b[16];
|
||||
|
||||
/*
|
||||
* notice the KEK key used to perform the wrapping or unwrapping is
|
||||
* always the size of the AES key used, eg, A128KW == 128 bits. The
|
||||
* key being wrapped or unwrapped may be larger and is set by the
|
||||
* 'bits' parameter.
|
||||
*
|
||||
* If it's larger than the KEK key size bits, we iterate over it
|
||||
*/
|
||||
|
||||
mbedtls_aes_init(&ctx);
|
||||
|
||||
if (wrap) {
|
||||
/*
|
||||
* The inputs to the key wrapping process are the KEK and the
|
||||
* plaintext to be wrapped. The plaintext consists of n 64-bit
|
||||
* blocks, containing the key data being wrapped.
|
||||
*
|
||||
* Inputs: Plaintext, n 64-bit values {P1, P2, ..., Pn},
|
||||
* and Key, K (the KEK).
|
||||
* Outputs: Ciphertext, (n+1) 64-bit values
|
||||
* {C0, C1, ..., Cn}.
|
||||
*
|
||||
* The default initial value (IV) is defined to be the
|
||||
* hexadecimal constant:
|
||||
*
|
||||
* A[0] = IV = A6A6A6A6A6A6A6A6
|
||||
*/
|
||||
memset(out, 0xa6, 8);
|
||||
memcpy(out + 8, in, 8 * (unsigned int)c64);
|
||||
n = mbedtls_aes_setkey_enc(&ctx, kek, (unsigned int)kek_bits);
|
||||
} else {
|
||||
/*
|
||||
* 2.2.2 Key Unwrap
|
||||
*
|
||||
* The inputs to the unwrap process are the KEK and (n+1)
|
||||
* 64-bit blocks of ciphertext consisting of previously
|
||||
* wrapped key. It returns n blocks of plaintext consisting
|
||||
* of the n 64-bit blocks of the decrypted key data.
|
||||
*
|
||||
* Inputs: Ciphertext, (n+1) 64-bit values {C0, C1, ..., Cn},
|
||||
* and Key, K (the KEK).
|
||||
*
|
||||
* Outputs: Plaintext, n 64-bit values {P1, P2, ..., Pn}.
|
||||
*/
|
||||
memcpy(a, in, 8);
|
||||
memcpy(out, in + 8, 8 * (unsigned int)c64);
|
||||
n = mbedtls_aes_setkey_dec(&ctx, kek, (unsigned int)kek_bits);
|
||||
}
|
||||
|
||||
if (n < 0) {
|
||||
lwsl_err("%s: setkey failed\n", __func__);
|
||||
goto bail;
|
||||
}
|
||||
|
||||
if (wrap) {
|
||||
for (n = 0; n <= 5; n++) {
|
||||
uint8_t *r = out + 8;
|
||||
for (m = 1; m <= c64; m++) {
|
||||
memcpy(b, out, 8);
|
||||
memcpy(b + 8, r, 8);
|
||||
if (mbedtls_internal_aes_encrypt(&ctx, b, b))
|
||||
goto bail;
|
||||
|
||||
memcpy(out, b, 8);
|
||||
out[7] ^= (uint8_t)(c64 * n + m);
|
||||
memcpy(r, b + 8, 8);
|
||||
r += 8;
|
||||
}
|
||||
}
|
||||
ret = 0;
|
||||
} else {
|
||||
/*
|
||||
*
|
||||
*/
|
||||
for (n = 5; n >= 0; n--) {
|
||||
uint8_t *r = out + (c64 - 1) * 8;
|
||||
for (m = c64; m >= 1; m--) {
|
||||
memcpy(b, a, 8);
|
||||
b[7] ^= (uint8_t)(c64 * n + m);
|
||||
memcpy(b + 8, r, 8);
|
||||
if (mbedtls_internal_aes_decrypt(&ctx, b, b))
|
||||
goto bail;
|
||||
|
||||
memcpy(a, b, 8);
|
||||
memcpy(r, b + 8, 8);
|
||||
r -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
for (n = 0; n < 8; n++)
|
||||
if (a[n] != 0xa6)
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
bail:
|
||||
if (ret)
|
||||
lwsl_notice("%s: failed\n", __func__);
|
||||
mbedtls_aes_free(&ctx);
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
lws_genaes_crypt(struct lws_genaes_ctx *ctx, const uint8_t *in, size_t len,
|
||||
uint8_t *out, uint8_t *iv_or_nonce_ctr_or_data_unit_16,
|
||||
uint8_t *stream_block_16, size_t *nc_or_iv_off, int taglen)
|
||||
{
|
||||
uint8_t iv[LWS_JWE_AES_IV_BYTES], sb[16];
|
||||
int n = 0;
|
||||
|
||||
switch (ctx->mode) {
|
||||
case LWS_GAESM_KW:
|
||||
#if defined(LWS_HAVE_mbedtls_internal_aes_encrypt)
|
||||
/* a key of length ctx->k->len is wrapped by a 128-bit KEK */
|
||||
n = lws_genaes_rfc3394_wrap(ctx->op == MBEDTLS_AES_ENCRYPT,
|
||||
(ctx->op == MBEDTLS_AES_ENCRYPT ? (int)len * 8 :
|
||||
((int)len - 8) * 8), ctx->k->buf,
|
||||
(int)ctx->k->len * 8,
|
||||
in, out);
|
||||
break;
|
||||
#else
|
||||
lwsl_err("%s: your mbedtls is too old\n", __func__);
|
||||
return -1;
|
||||
#endif
|
||||
case LWS_GAESM_CBC:
|
||||
memcpy(iv, iv_or_nonce_ctr_or_data_unit_16, 16);
|
||||
|
||||
/*
|
||||
* If encrypting, we do the PKCS#7 padding.
|
||||
* During decryption, the caller will need to unpad.
|
||||
*/
|
||||
if (ctx->padding && ctx->op == MBEDTLS_AES_ENCRYPT) {
|
||||
/*
|
||||
* Since we don't want to burden the caller with
|
||||
* the over-allocation at the end of the input,
|
||||
* we have to allocate a temp with space for it
|
||||
*/
|
||||
uint8_t *padin = (uint8_t *)lws_malloc(
|
||||
lws_gencrypto_padded_length(LWS_AES_CBC_BLOCKLEN, len),
|
||||
__func__);
|
||||
|
||||
if (!padin)
|
||||
return -1;
|
||||
|
||||
memcpy(padin, in, len);
|
||||
len += _write_pkcs7_pad((uint8_t *)padin, (int)len);
|
||||
n = mbedtls_aes_crypt_cbc(&ctx->u.ctx, (int)ctx->op, len, iv,
|
||||
padin, out);
|
||||
lws_free(padin);
|
||||
} else
|
||||
n = mbedtls_aes_crypt_cbc(&ctx->u.ctx, (int)ctx->op, len, iv,
|
||||
in, out);
|
||||
|
||||
break;
|
||||
|
||||
case LWS_GAESM_CFB128:
|
||||
memcpy(iv, iv_or_nonce_ctr_or_data_unit_16, 16);
|
||||
n = mbedtls_aes_crypt_cfb128(&ctx->u.ctx, (int)ctx->op, len,
|
||||
nc_or_iv_off, iv, in, out);
|
||||
break;
|
||||
|
||||
case LWS_GAESM_CFB8:
|
||||
memcpy(iv, iv_or_nonce_ctr_or_data_unit_16, 16);
|
||||
n = mbedtls_aes_crypt_cfb8(&ctx->u.ctx, (int)ctx->op, len, iv,
|
||||
in, out);
|
||||
break;
|
||||
|
||||
case LWS_GAESM_CTR:
|
||||
memcpy(iv, iv_or_nonce_ctr_or_data_unit_16, 16);
|
||||
memcpy(sb, stream_block_16, 16);
|
||||
n = mbedtls_aes_crypt_ctr(&ctx->u.ctx, len, nc_or_iv_off,
|
||||
iv, sb, in, out);
|
||||
memcpy(iv_or_nonce_ctr_or_data_unit_16, iv, 16);
|
||||
memcpy(stream_block_16, sb, 16);
|
||||
break;
|
||||
|
||||
case LWS_GAESM_ECB:
|
||||
n = mbedtls_aes_crypt_ecb(&ctx->u.ctx, (int)ctx->op, in, out);
|
||||
break;
|
||||
|
||||
case LWS_GAESM_OFB:
|
||||
#if defined(MBEDTLS_CIPHER_MODE_OFB)
|
||||
memcpy(iv, iv_or_nonce_ctr_or_data_unit_16, 16);
|
||||
n = mbedtls_aes_crypt_ofb(&ctx->u.ctx, len, nc_or_iv_off, iv,
|
||||
in, out);
|
||||
break;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
|
||||
case LWS_GAESM_XTS:
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
memcpy(iv, iv_or_nonce_ctr_or_data_unit_16, 16);
|
||||
n = mbedtls_aes_crypt_xts(&ctx->u.ctx_xts, (int)ctx->op, len, iv,
|
||||
in, out);
|
||||
break;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
case LWS_GAESM_GCM:
|
||||
if (!ctx->underway) {
|
||||
ctx->underway = 1;
|
||||
|
||||
memcpy(ctx->tag, stream_block_16, (unsigned int)taglen);
|
||||
ctx->taglen = taglen;
|
||||
|
||||
/*
|
||||
* iv: iv_or_nonce_ctr_or_data_unit_16
|
||||
* iv_len: *nc_or_iv_off
|
||||
* stream_block_16: pointer to tag
|
||||
* additional data: in
|
||||
* additional data len: len
|
||||
*/
|
||||
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000
|
||||
n = mbedtls_gcm_starts(&ctx->u.ctx_gcm, (int)ctx->op,
|
||||
iv_or_nonce_ctr_or_data_unit_16,
|
||||
*nc_or_iv_off);
|
||||
if (!n)
|
||||
n = mbedtls_gcm_update_ad(&ctx->u.ctx_gcm,
|
||||
in, len);
|
||||
#else
|
||||
n = mbedtls_gcm_starts(&ctx->u.ctx_gcm, (int)ctx->op,
|
||||
iv_or_nonce_ctr_or_data_unit_16,
|
||||
*nc_or_iv_off, in, len);
|
||||
#endif
|
||||
if (n) {
|
||||
lwsl_notice("%s: mbedtls_gcm_starts: -0x%x\n",
|
||||
__func__, -n);
|
||||
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000
|
||||
{
|
||||
size_t al;
|
||||
|
||||
n = mbedtls_gcm_update(&ctx->u.ctx_gcm, in, len, out, len, &al);
|
||||
}
|
||||
#else
|
||||
n = mbedtls_gcm_update(&ctx->u.ctx_gcm, len, in, out);
|
||||
#endif
|
||||
if (n) {
|
||||
lwsl_notice("%s: mbedtls_gcm_update: -0x%x\n",
|
||||
__func__, -n);
|
||||
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (n) {
|
||||
lwsl_notice("%s: failed: -0x%x, len %d\n", __func__, -n, (int)len);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
67
Kinc/Sources/kinc/libs/tls/mbedtls/lws-gencrypto.c
Normal file
67
Kinc/Sources/kinc/libs/tls/mbedtls/lws-gencrypto.c
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* lws-gencrypto openssl-specific common code
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-tls-mbedtls.h"
|
||||
|
||||
mbedtls_md_type_t
|
||||
lws_gencrypto_mbedtls_hash_to_MD_TYPE(enum lws_genhash_types hash_type)
|
||||
{
|
||||
mbedtls_md_type_t h = (mbedtls_md_type_t)-1;
|
||||
|
||||
switch (hash_type) {
|
||||
case LWS_GENHASH_TYPE_MD5:
|
||||
h = MBEDTLS_MD_MD5;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA1:
|
||||
h = MBEDTLS_MD_SHA1;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA256:
|
||||
h = MBEDTLS_MD_SHA256;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA384:
|
||||
h = MBEDTLS_MD_SHA384;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA512:
|
||||
h = MBEDTLS_MD_SHA512;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
int
|
||||
lws_gencrypto_mbedtls_rngf(void *context, unsigned char *buf, size_t len)
|
||||
{
|
||||
if ((size_t)lws_get_random(context, buf, len) == len) {
|
||||
// lwsl_hexdump_err(buf, len);
|
||||
return 0;
|
||||
}
|
||||
lwsl_err("%s: rng failed\n", __func__);
|
||||
return -1;
|
||||
}
|
535
Kinc/Sources/kinc/libs/tls/mbedtls/lws-genec.c
Normal file
535
Kinc/Sources/kinc/libs/tls/mbedtls/lws-genec.c
Normal file
@ -0,0 +1,535 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* lws_genec provides an EC abstraction api in lws that works the
|
||||
* same whether you are using openssl or mbedtls crypto functions underneath.
|
||||
*/
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-tls-mbedtls.h"
|
||||
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000
|
||||
#define ECDHCTX(_c, _ins) _c->u.ctx_ecdh->MBEDTLS_PRIVATE(ctx).\
|
||||
MBEDTLS_PRIVATE(mbed_ecdh).MBEDTLS_PRIVATE(_ins)
|
||||
#define ECDSACTX(_c, _ins) _c->u.ctx_ecdsa->MBEDTLS_PRIVATE(_ins)
|
||||
#else
|
||||
#define ECDHCTX(_c, _ins) _c->u.ctx_ecdh->_ins
|
||||
#define ECDSACTX(_c, _ins) _c->u.ctx_ecdsa->_ins
|
||||
#endif
|
||||
|
||||
const struct lws_ec_curves lws_ec_curves[] = {
|
||||
/*
|
||||
* These are the curves we are willing to use by default...
|
||||
*
|
||||
* The 3 recommended+ (P-256) and optional curves in RFC7518 7.6
|
||||
*
|
||||
* Specific keys lengths from RFC8422 p20
|
||||
*/
|
||||
{ "P-256", MBEDTLS_ECP_DP_SECP256R1, 32 },
|
||||
{ "P-384", MBEDTLS_ECP_DP_SECP384R1, 48 },
|
||||
{ "P-521", MBEDTLS_ECP_DP_SECP521R1, 66 },
|
||||
|
||||
{ NULL, 0, 0 }
|
||||
};
|
||||
|
||||
static int
|
||||
lws_genec_keypair_import(struct lws_genec_ctx *ctx, enum enum_lws_dh_side side,
|
||||
const struct lws_gencrypto_keyelem *el)
|
||||
{
|
||||
const struct lws_ec_curves *curve;
|
||||
mbedtls_ecp_keypair kp;
|
||||
int ret = -1;
|
||||
|
||||
if (el[LWS_GENCRYPTO_EC_KEYEL_CRV].len < 4) {
|
||||
lwsl_notice("%s: crv '%s' (%d)\n", __func__,
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf ?
|
||||
(char *)el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf :
|
||||
"null",
|
||||
(int)el[LWS_GENCRYPTO_EC_KEYEL_CRV].len);
|
||||
return -21;
|
||||
}
|
||||
|
||||
curve = lws_genec_curve(ctx->curve_table,
|
||||
(char *)el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf);
|
||||
if (!curve)
|
||||
return -22;
|
||||
|
||||
/*
|
||||
* d (the private part) may be missing, otherwise it and everything
|
||||
* else must match the expected bignum size
|
||||
*/
|
||||
|
||||
if ((el[LWS_GENCRYPTO_EC_KEYEL_D].len &&
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_D].len != curve->key_bytes) ||
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_X].len != curve->key_bytes ||
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_Y].len != curve->key_bytes)
|
||||
return -23;
|
||||
|
||||
mbedtls_ecp_keypair_init(&kp);
|
||||
if (mbedtls_ecp_group_load(&kp.MBEDTLS_PRIVATE(grp),
|
||||
(mbedtls_ecp_group_id)curve->tls_lib_nid))
|
||||
goto bail1;
|
||||
|
||||
ctx->has_private = !!el[LWS_GENCRYPTO_EC_KEYEL_D].len;
|
||||
|
||||
/* d (the private key) is directly an mpi */
|
||||
|
||||
if (ctx->has_private &&
|
||||
mbedtls_mpi_read_binary(&kp.MBEDTLS_PRIVATE(d),
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_D].buf,
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_D].len))
|
||||
goto bail1;
|
||||
|
||||
mbedtls_ecp_set_zero(&kp.MBEDTLS_PRIVATE(Q));
|
||||
|
||||
if (mbedtls_mpi_read_binary(&kp.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X),
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_X].buf,
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_X].len))
|
||||
goto bail1;
|
||||
|
||||
if (mbedtls_mpi_read_binary(&kp.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y),
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_Y].buf,
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_Y].len))
|
||||
goto bail1;
|
||||
|
||||
mbedtls_mpi_lset(&kp.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 1);
|
||||
|
||||
switch (ctx->genec_alg) {
|
||||
case LEGENEC_ECDH:
|
||||
if (mbedtls_ecdh_get_params(ctx->u.ctx_ecdh, &kp,
|
||||
(mbedtls_ecdh_side)side))
|
||||
goto bail1;
|
||||
/* verify the key is consistent with the claimed curve */
|
||||
if (ctx->has_private &&
|
||||
mbedtls_ecp_check_privkey(&ECDHCTX(ctx, grp),
|
||||
&ECDHCTX(ctx, d)))
|
||||
goto bail1;
|
||||
if (mbedtls_ecp_check_pubkey(&ECDHCTX(ctx, grp),
|
||||
&ECDHCTX(ctx, Q)))
|
||||
goto bail1;
|
||||
break;
|
||||
case LEGENEC_ECDSA:
|
||||
if (mbedtls_ecdsa_from_keypair(ctx->u.ctx_ecdsa, &kp))
|
||||
goto bail1;
|
||||
/* verify the key is consistent with the claimed curve */
|
||||
if (ctx->has_private &&
|
||||
mbedtls_ecp_check_privkey(&ECDSACTX(ctx, grp),
|
||||
&ECDSACTX(ctx, d)))
|
||||
goto bail1;
|
||||
if (mbedtls_ecp_check_pubkey(&ECDSACTX(ctx, grp),
|
||||
&ECDSACTX(ctx, Q)))
|
||||
goto bail1;
|
||||
break;
|
||||
default:
|
||||
goto bail1;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
bail1:
|
||||
mbedtls_ecp_keypair_free(&kp);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genecdh_create(struct lws_genec_ctx *ctx, struct lws_context *context,
|
||||
const struct lws_ec_curves *curve_table)
|
||||
{
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
|
||||
ctx->context = context;
|
||||
ctx->curve_table = curve_table;
|
||||
ctx->genec_alg = LEGENEC_ECDH;
|
||||
|
||||
ctx->u.ctx_ecdh = lws_zalloc(sizeof(*ctx->u.ctx_ecdh), "genecdh");
|
||||
if (!ctx->u.ctx_ecdh)
|
||||
return 1;
|
||||
|
||||
mbedtls_ecdh_init(ctx->u.ctx_ecdh);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genecdsa_create(struct lws_genec_ctx *ctx, struct lws_context *context,
|
||||
const struct lws_ec_curves *curve_table)
|
||||
{
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
|
||||
ctx->context = context;
|
||||
ctx->curve_table = curve_table;
|
||||
ctx->genec_alg = LEGENEC_ECDSA;
|
||||
|
||||
ctx->u.ctx_ecdsa = lws_zalloc(sizeof(*ctx->u.ctx_ecdsa), "genecdsa");
|
||||
if (!ctx->u.ctx_ecdsa)
|
||||
return 1;
|
||||
|
||||
mbedtls_ecdsa_init(ctx->u.ctx_ecdsa);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lws_genecdh_set_key(struct lws_genec_ctx *ctx, struct lws_gencrypto_keyelem *el,
|
||||
enum enum_lws_dh_side side)
|
||||
{
|
||||
if (ctx->genec_alg != LEGENEC_ECDH)
|
||||
return -1;
|
||||
|
||||
return lws_genec_keypair_import(ctx, side, el);
|
||||
}
|
||||
|
||||
int
|
||||
lws_genecdsa_set_key(struct lws_genec_ctx *ctx,
|
||||
const struct lws_gencrypto_keyelem *el)
|
||||
{
|
||||
if (ctx->genec_alg != LEGENEC_ECDSA)
|
||||
return -1;
|
||||
|
||||
return lws_genec_keypair_import(ctx, 0, el);
|
||||
}
|
||||
|
||||
void
|
||||
lws_genec_destroy(struct lws_genec_ctx *ctx)
|
||||
{
|
||||
switch (ctx->genec_alg) {
|
||||
case LEGENEC_ECDH:
|
||||
if (ctx->u.ctx_ecdh) {
|
||||
mbedtls_ecdh_free(ctx->u.ctx_ecdh);
|
||||
lws_free(ctx->u.ctx_ecdh);
|
||||
ctx->u.ctx_ecdh = NULL;
|
||||
}
|
||||
break;
|
||||
case LEGENEC_ECDSA:
|
||||
if (ctx->u.ctx_ecdsa) {
|
||||
mbedtls_ecdsa_free(ctx->u.ctx_ecdsa);
|
||||
lws_free(ctx->u.ctx_ecdsa);
|
||||
ctx->u.ctx_ecdsa = NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
lws_genecdh_new_keypair(struct lws_genec_ctx *ctx, enum enum_lws_dh_side side,
|
||||
const char *curve_name,
|
||||
struct lws_gencrypto_keyelem *el)
|
||||
{
|
||||
const struct lws_ec_curves *curve;
|
||||
mbedtls_ecdsa_context ecdsa;
|
||||
mbedtls_ecp_keypair *kp;
|
||||
mbedtls_mpi *mpi[3];
|
||||
int n;
|
||||
|
||||
if (ctx->genec_alg != LEGENEC_ECDH)
|
||||
return -1;
|
||||
|
||||
curve = lws_genec_curve(ctx->curve_table, curve_name);
|
||||
if (!curve) {
|
||||
lwsl_err("%s: curve '%s' not supported\n",
|
||||
__func__, curve_name);
|
||||
|
||||
return -22;
|
||||
}
|
||||
|
||||
mbedtls_ecdsa_init(&ecdsa);
|
||||
n = mbedtls_ecdsa_genkey(&ecdsa, (mbedtls_ecp_group_id)curve->tls_lib_nid,
|
||||
lws_gencrypto_mbedtls_rngf,
|
||||
ctx->context);
|
||||
if (n) {
|
||||
lwsl_err("mbedtls_ecdsa_genkey failed 0x%x\n", -n);
|
||||
goto bail1;
|
||||
}
|
||||
|
||||
kp = (mbedtls_ecp_keypair *)&ecdsa;
|
||||
|
||||
n = mbedtls_ecdh_get_params(ctx->u.ctx_ecdh, kp,
|
||||
(mbedtls_ecdh_side)side);
|
||||
if (n) {
|
||||
lwsl_err("mbedtls_ecdh_get_params failed 0x%x\n", -n);
|
||||
goto bail1;
|
||||
}
|
||||
|
||||
/*
|
||||
* we need to capture the individual element BIGNUMs into
|
||||
* lws_gencrypto_keyelem, so they can be serialized, used in jwk etc
|
||||
*/
|
||||
|
||||
mpi[0] = &kp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X);
|
||||
mpi[1] = &kp->MBEDTLS_PRIVATE(d);
|
||||
mpi[2] = &kp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y);
|
||||
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_CRV].len = (uint32_t)strlen(curve_name) + 1;
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf =
|
||||
lws_malloc(el[LWS_GENCRYPTO_EC_KEYEL_CRV].len, "ec");
|
||||
if (!el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf)
|
||||
goto bail1;
|
||||
strcpy((char *)el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf, curve_name);
|
||||
|
||||
for (n = LWS_GENCRYPTO_EC_KEYEL_X; n < LWS_GENCRYPTO_EC_KEYEL_COUNT;
|
||||
n++) {
|
||||
el[n].len = curve->key_bytes;
|
||||
el[n].buf = lws_malloc(curve->key_bytes, "ec");
|
||||
if (!el[n].buf)
|
||||
goto bail2;
|
||||
|
||||
if (mbedtls_mpi_write_binary(mpi[n - 1], el[n].buf,
|
||||
curve->key_bytes))
|
||||
goto bail2;
|
||||
}
|
||||
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
|
||||
return 0;
|
||||
|
||||
bail2:
|
||||
for (n = 0; n < LWS_GENCRYPTO_EC_KEYEL_COUNT; n++)
|
||||
if (el[n].buf)
|
||||
lws_free_set_NULL(el[n].buf);
|
||||
bail1:
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
|
||||
lws_free_set_NULL(ctx->u.ctx_ecdh);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genecdsa_new_keypair(struct lws_genec_ctx *ctx, const char *curve_name,
|
||||
struct lws_gencrypto_keyelem *el)
|
||||
{
|
||||
const struct lws_ec_curves *curve;
|
||||
mbedtls_ecp_keypair *kp;
|
||||
mbedtls_mpi *mpi[3];
|
||||
int n;
|
||||
|
||||
if (ctx->genec_alg != LEGENEC_ECDSA)
|
||||
return -1;
|
||||
|
||||
curve = lws_genec_curve(ctx->curve_table, curve_name);
|
||||
if (!curve) {
|
||||
lwsl_err("%s: curve '%s' not supported\n",
|
||||
__func__, curve_name);
|
||||
|
||||
return -22;
|
||||
}
|
||||
|
||||
//mbedtls_ecdsa_init(ctx->u.ctx_ecdsa);
|
||||
n = mbedtls_ecdsa_genkey(ctx->u.ctx_ecdsa, (mbedtls_ecp_group_id)curve->tls_lib_nid,
|
||||
lws_gencrypto_mbedtls_rngf, ctx->context);
|
||||
if (n) {
|
||||
lwsl_err("mbedtls_ecdsa_genkey failed 0x%x\n", -n);
|
||||
goto bail1;
|
||||
}
|
||||
|
||||
/*
|
||||
* we need to capture the individual element BIGNUMs into
|
||||
* lws_gencrypto_keyelems, so they can be serialized, used in jwk etc
|
||||
*/
|
||||
|
||||
kp = (mbedtls_ecp_keypair *)ctx->u.ctx_ecdsa;
|
||||
|
||||
mpi[0] = &kp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X);
|
||||
mpi[1] = &kp->MBEDTLS_PRIVATE(d);
|
||||
mpi[2] = &kp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y);
|
||||
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_CRV].len = (uint32_t)strlen(curve_name) + 1;
|
||||
el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf =
|
||||
lws_malloc(el[LWS_GENCRYPTO_EC_KEYEL_CRV].len, "ec");
|
||||
if (!el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf)
|
||||
goto bail1;
|
||||
strcpy((char *)el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf, curve_name);
|
||||
|
||||
for (n = LWS_GENCRYPTO_EC_KEYEL_X; n < LWS_GENCRYPTO_EC_KEYEL_COUNT;
|
||||
n++) {
|
||||
el[n].len = curve->key_bytes;
|
||||
el[n].buf = lws_malloc(curve->key_bytes, "ec");
|
||||
if (!el[n].buf)
|
||||
goto bail2;
|
||||
|
||||
|
||||
if (mbedtls_mpi_write_binary(mpi[n - 1], el[n].buf, el[n].len)) {
|
||||
lwsl_err("%s: mbedtls_mpi_write_binary failed\n", __func__);
|
||||
goto bail2;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
bail2:
|
||||
for (n = 0; n < LWS_GENCRYPTO_EC_KEYEL_COUNT; n++)
|
||||
if (el[n].buf)
|
||||
lws_free_set_NULL(el[n].buf);
|
||||
bail1:
|
||||
|
||||
lws_free_set_NULL(ctx->u.ctx_ecdsa);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genecdsa_hash_sign_jws(struct lws_genec_ctx *ctx, const uint8_t *in,
|
||||
enum lws_genhash_types hash_type, int keybits,
|
||||
uint8_t *sig, size_t sig_len)
|
||||
{
|
||||
int n, keybytes = lws_gencrypto_bits_to_bytes(keybits);
|
||||
size_t hlen = lws_genhash_size(hash_type);
|
||||
mbedtls_mpi mpi_r, mpi_s;
|
||||
size_t slen = sig_len;
|
||||
|
||||
if (ctx->genec_alg != LEGENEC_ECDSA)
|
||||
return -1;
|
||||
|
||||
/*
|
||||
* The ECDSA P-256 SHA-256 digital signature is generated as follows:
|
||||
*
|
||||
* 1. Generate a digital signature of the JWS Signing Input using ECDSA
|
||||
* P-256 SHA-256 with the desired private key. The output will be
|
||||
* the pair (R, S), where R and S are 256-bit unsigned integers.
|
||||
*
|
||||
* 2. Turn R and S into octet sequences in big-endian order, with each
|
||||
* array being be 32 octets long. The octet sequence
|
||||
* representations MUST NOT be shortened to omit any leading zero
|
||||
* octets contained in the values.
|
||||
*
|
||||
* 3. Concatenate the two octet sequences in the order R and then S.
|
||||
* (Note that many ECDSA implementations will directly produce this
|
||||
* concatenation as their output.)
|
||||
*
|
||||
* 4. The resulting 64-octet sequence is the JWS Signature value.
|
||||
*/
|
||||
|
||||
mbedtls_mpi_init(&mpi_r);
|
||||
mbedtls_mpi_init(&mpi_s);
|
||||
|
||||
n = mbedtls_ecdsa_sign(&ECDSACTX(ctx, grp), &mpi_r, &mpi_s,
|
||||
&ECDSACTX(ctx, d), in, hlen,
|
||||
lws_gencrypto_mbedtls_rngf, ctx->context);
|
||||
if (n) {
|
||||
lwsl_err("%s: mbedtls_ecdsa_sign failed: -0x%x\n",
|
||||
__func__, -n);
|
||||
|
||||
goto bail2;
|
||||
}
|
||||
|
||||
if (mbedtls_mpi_write_binary(&mpi_r, sig, (unsigned int)keybytes))
|
||||
goto bail2;
|
||||
mbedtls_mpi_free(&mpi_r);
|
||||
if (mbedtls_mpi_write_binary(&mpi_s, sig + keybytes, (unsigned int)keybytes))
|
||||
goto bail1;
|
||||
mbedtls_mpi_free(&mpi_s);
|
||||
|
||||
return (int)slen;
|
||||
|
||||
bail2:
|
||||
mbedtls_mpi_free(&mpi_r);
|
||||
bail1:
|
||||
mbedtls_mpi_free(&mpi_s);
|
||||
|
||||
return -3;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genecdsa_hash_sig_verify_jws(struct lws_genec_ctx *ctx, const uint8_t *in,
|
||||
enum lws_genhash_types hash_type, int keybits,
|
||||
const uint8_t *sig, size_t sig_len)
|
||||
{
|
||||
int n, keybytes = lws_gencrypto_bits_to_bytes(keybits);
|
||||
size_t hlen = lws_genhash_size(hash_type);
|
||||
mbedtls_mpi mpi_r, mpi_s;
|
||||
|
||||
if (ctx->genec_alg != LEGENEC_ECDSA)
|
||||
return -1;
|
||||
|
||||
if ((int)sig_len != keybytes * 2)
|
||||
return -1;
|
||||
|
||||
/*
|
||||
* 1. The JWS Signature value MUST be a 64-octet sequence. If it is
|
||||
* not a 64-octet sequence, the validation has failed.
|
||||
*
|
||||
* 2. Split the 64-octet sequence into two 32-octet sequences. The
|
||||
* first octet sequence represents R and the second S. The values R
|
||||
* and S are represented as octet sequences using the Integer-to-
|
||||
* OctetString Conversion defined in Section 2.3.7 of SEC1 [SEC1]
|
||||
* (in big-endian octet order).
|
||||
*
|
||||
* 3. Submit the JWS Signing Input, R, S, and the public key (x, y) to
|
||||
* the ECDSA P-256 SHA-256 validator.
|
||||
*/
|
||||
|
||||
mbedtls_mpi_init(&mpi_r);
|
||||
mbedtls_mpi_init(&mpi_s);
|
||||
|
||||
if (mbedtls_mpi_read_binary(&mpi_r, sig, (unsigned int)keybytes))
|
||||
return -1;
|
||||
if (mbedtls_mpi_read_binary(&mpi_s, sig + keybytes, (unsigned int)keybytes))
|
||||
goto bail1;
|
||||
|
||||
n = mbedtls_ecdsa_verify(&ECDSACTX(ctx, grp), in, hlen,
|
||||
&ECDSACTX(ctx, Q), &mpi_r, &mpi_s);
|
||||
|
||||
mbedtls_mpi_free(&mpi_s);
|
||||
mbedtls_mpi_free(&mpi_r);
|
||||
|
||||
if (n) {
|
||||
lwsl_err("%s: mbedtls_ecdsa_verify failed: -0x%x\n",
|
||||
__func__, -n);
|
||||
|
||||
goto bail;
|
||||
}
|
||||
|
||||
return 0;
|
||||
bail1:
|
||||
mbedtls_mpi_free(&mpi_r);
|
||||
|
||||
bail:
|
||||
|
||||
return -3;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genecdh_compute_shared_secret(struct lws_genec_ctx *ctx, uint8_t *ss,
|
||||
int *ss_len)
|
||||
{
|
||||
int n;
|
||||
size_t st;
|
||||
if (mbedtls_ecp_check_pubkey(&ECDHCTX(ctx, grp), &ECDHCTX(ctx, Q)) ||
|
||||
mbedtls_ecp_check_pubkey(&ECDHCTX(ctx, grp), &ECDHCTX(ctx, Qp))) {
|
||||
lwsl_err("%s: both sides must be set up\n", __func__);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
n = mbedtls_ecdh_calc_secret(ctx->u.ctx_ecdh, &st, ss, (size_t)*ss_len,
|
||||
lws_gencrypto_mbedtls_rngf, ctx->context);
|
||||
if (n)
|
||||
return -1;
|
||||
|
||||
*ss_len = (int)st;
|
||||
|
||||
return 0;
|
||||
}
|
322
Kinc/Sources/kinc/libs/tls/mbedtls/lws-genhash.c
Normal file
322
Kinc/Sources/kinc/libs/tls/mbedtls/lws-genhash.c
Normal file
@ -0,0 +1,322 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* lws_genhash provides a hash / hmac abstraction api in lws that works the
|
||||
* same whether you are using openssl or mbedtls hash functions underneath.
|
||||
*/
|
||||
#include "libwebsockets.h"
|
||||
#include <mbedtls/version.h>
|
||||
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && (MBEDTLS_VERSION_NUMBER >= 0x03000000)
|
||||
#define mbedtls_md5_starts_ret mbedtls_md5_starts
|
||||
#define mbedtls_md5_update_ret mbedtls_md5_update
|
||||
#define mbedtls_md5_finish_ret mbedtls_md5_finish
|
||||
#define mbedtls_sha1_finish_ret mbedtls_sha1_finish
|
||||
#define mbedtls_sha1_update_ret mbedtls_sha1_update
|
||||
#define mbedtls_sha1_starts_ret mbedtls_sha1_starts
|
||||
#define mbedtls_sha256_starts_ret mbedtls_sha256_starts
|
||||
#define mbedtls_sha256_update_ret mbedtls_sha256_update
|
||||
#define mbedtls_sha256_finish_ret mbedtls_sha256_finish
|
||||
#define mbedtls_sha512_starts_ret mbedtls_sha512_starts
|
||||
#define mbedtls_sha512_update_ret mbedtls_sha512_update
|
||||
#define mbedtls_sha512_finish_ret mbedtls_sha512_finish
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && (MBEDTLS_VERSION_NUMBER >= 0x02070000)
|
||||
|
||||
/*
|
||||
* We have the _ret variants available, check the return codes on everything
|
||||
*/
|
||||
|
||||
int
|
||||
lws_genhash_init(struct lws_genhash_ctx *ctx, enum lws_genhash_types type)
|
||||
{
|
||||
ctx->type = (uint8_t)type;
|
||||
|
||||
switch (ctx->type) {
|
||||
case LWS_GENHASH_TYPE_MD5:
|
||||
mbedtls_md5_init(&ctx->u.md5);
|
||||
if (mbedtls_md5_starts_ret(&ctx->u.md5))
|
||||
return 1;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA1:
|
||||
mbedtls_sha1_init(&ctx->u.sha1);
|
||||
if (mbedtls_sha1_starts_ret(&ctx->u.sha1))
|
||||
return 1;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA256:
|
||||
mbedtls_sha256_init(&ctx->u.sha256);
|
||||
if (mbedtls_sha256_starts_ret(&ctx->u.sha256, 0))
|
||||
return 1;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA384:
|
||||
mbedtls_sha512_init(&ctx->u.sha512);
|
||||
if (mbedtls_sha512_starts_ret(&ctx->u.sha512, 1 /* is384 */))
|
||||
return 1;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA512:
|
||||
mbedtls_sha512_init(&ctx->u.sha512);
|
||||
if (mbedtls_sha512_starts_ret(&ctx->u.sha512, 0))
|
||||
return 1;
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genhash_update(struct lws_genhash_ctx *ctx, const void *in, size_t len)
|
||||
{
|
||||
if (!len)
|
||||
return 0;
|
||||
|
||||
switch (ctx->type) {
|
||||
case LWS_GENHASH_TYPE_MD5:
|
||||
if (mbedtls_md5_update_ret(&ctx->u.md5, in, len))
|
||||
return 1;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA1:
|
||||
if (mbedtls_sha1_update_ret(&ctx->u.sha1, in, len))
|
||||
return 1;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA256:
|
||||
if (mbedtls_sha256_update_ret(&ctx->u.sha256, in, len))
|
||||
return 1;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA384:
|
||||
if (mbedtls_sha512_update_ret(&ctx->u.sha512, in, len))
|
||||
return 1;
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA512:
|
||||
if (mbedtls_sha512_update_ret(&ctx->u.sha512, in, len))
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genhash_destroy(struct lws_genhash_ctx *ctx, void *result)
|
||||
{
|
||||
switch (ctx->type) {
|
||||
case LWS_GENHASH_TYPE_MD5:
|
||||
if (mbedtls_md5_finish_ret(&ctx->u.md5, result))
|
||||
return 1;
|
||||
mbedtls_md5_free(&ctx->u.md5);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA1:
|
||||
if (mbedtls_sha1_finish_ret(&ctx->u.sha1, result))
|
||||
return 1;
|
||||
mbedtls_sha1_free(&ctx->u.sha1);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA256:
|
||||
if (mbedtls_sha256_finish_ret(&ctx->u.sha256, result))
|
||||
return 1;
|
||||
mbedtls_sha256_free(&ctx->u.sha256);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA384:
|
||||
if (mbedtls_sha512_finish_ret(&ctx->u.sha512, result))
|
||||
return 1;
|
||||
mbedtls_sha512_free(&ctx->u.sha512);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA512:
|
||||
if (mbedtls_sha512_finish_ret(&ctx->u.sha512, result))
|
||||
return 1;
|
||||
mbedtls_sha512_free(&ctx->u.sha512);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/*
|
||||
* mbedtls is too old to have the _ret variants
|
||||
*/
|
||||
|
||||
int
|
||||
lws_genhash_init(struct lws_genhash_ctx *ctx, enum lws_genhash_types type)
|
||||
{
|
||||
ctx->type = type;
|
||||
|
||||
switch (ctx->type) {
|
||||
case LWS_GENHASH_TYPE_MD5:
|
||||
mbedtls_md5_init(&ctx->u.md5);
|
||||
mbedtls_md5_starts(&ctx->u.md5);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA1:
|
||||
mbedtls_sha1_init(&ctx->u.sha1);
|
||||
mbedtls_sha1_starts(&ctx->u.sha1);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA256:
|
||||
mbedtls_sha256_init(&ctx->u.sha256);
|
||||
mbedtls_sha256_starts(&ctx->u.sha256, 0);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA384:
|
||||
mbedtls_sha512_init(&ctx->u.sha512);
|
||||
mbedtls_sha512_starts(&ctx->u.sha512, 1 /* is384 */);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA512:
|
||||
mbedtls_sha512_init(&ctx->u.sha512);
|
||||
mbedtls_sha512_starts(&ctx->u.sha512, 0);
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genhash_update(struct lws_genhash_ctx *ctx, const void *in, size_t len)
|
||||
{
|
||||
if (!len)
|
||||
return 0;
|
||||
|
||||
switch (ctx->type) {
|
||||
case LWS_GENHASH_TYPE_MD5:
|
||||
mbedtls_md5_update(&ctx->u.md5, in, len);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA1:
|
||||
mbedtls_sha1_update(&ctx->u.sha1, in, len);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA256:
|
||||
mbedtls_sha256_update(&ctx->u.sha256, in, len);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA384:
|
||||
mbedtls_sha512_update(&ctx->u.sha512, in, len);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA512:
|
||||
mbedtls_sha512_update(&ctx->u.sha512, in, len);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genhash_destroy(struct lws_genhash_ctx *ctx, void *result)
|
||||
{
|
||||
switch (ctx->type) {
|
||||
case LWS_GENHASH_TYPE_MD5:
|
||||
mbedtls_md5_finish(&ctx->u.md5, result);
|
||||
mbedtls_md5_free(&ctx->u.md5);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA1:
|
||||
mbedtls_sha1_finish(&ctx->u.sha1, result);
|
||||
mbedtls_sha1_free(&ctx->u.sha1);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA256:
|
||||
mbedtls_sha256_finish(&ctx->u.sha256, result);
|
||||
mbedtls_sha256_free(&ctx->u.sha256);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA384:
|
||||
mbedtls_sha512_finish(&ctx->u.sha512, result);
|
||||
mbedtls_sha512_free(&ctx->u.sha512);
|
||||
break;
|
||||
case LWS_GENHASH_TYPE_SHA512:
|
||||
mbedtls_sha512_finish(&ctx->u.sha512, result);
|
||||
mbedtls_sha512_free(&ctx->u.sha512);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int
|
||||
lws_genhmac_init(struct lws_genhmac_ctx *ctx, enum lws_genhmac_types type,
|
||||
const uint8_t *key, size_t key_len)
|
||||
{
|
||||
int t;
|
||||
|
||||
ctx->type = (uint8_t)type;
|
||||
|
||||
switch (type) {
|
||||
case LWS_GENHMAC_TYPE_SHA256:
|
||||
t = MBEDTLS_MD_SHA256;
|
||||
break;
|
||||
case LWS_GENHMAC_TYPE_SHA384:
|
||||
t = MBEDTLS_MD_SHA384;
|
||||
break;
|
||||
case LWS_GENHMAC_TYPE_SHA512:
|
||||
t = MBEDTLS_MD_SHA512;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
ctx->hmac = mbedtls_md_info_from_type((mbedtls_md_type_t)t);
|
||||
if (!ctx->hmac)
|
||||
return -1;
|
||||
|
||||
#if !defined(LWS_HAVE_mbedtls_md_setup)
|
||||
if (mbedtls_md_init_ctx(&ctx->ctx, ctx->hmac))
|
||||
return -1;
|
||||
#else
|
||||
if (mbedtls_md_setup(&ctx->ctx, ctx->hmac, 1))
|
||||
return -1;
|
||||
#endif
|
||||
|
||||
if (mbedtls_md_hmac_starts(&ctx->ctx, key, key_len)) {
|
||||
mbedtls_md_free(&ctx->ctx);
|
||||
ctx->hmac = NULL;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genhmac_update(struct lws_genhmac_ctx *ctx, const void *in, size_t len)
|
||||
{
|
||||
if (!len)
|
||||
return 0;
|
||||
|
||||
if (mbedtls_md_hmac_update(&ctx->ctx, in, len))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genhmac_destroy(struct lws_genhmac_ctx *ctx, void *result)
|
||||
{
|
||||
int n = 0;
|
||||
|
||||
if (result)
|
||||
n = mbedtls_md_hmac_finish(&ctx->ctx, result);
|
||||
|
||||
mbedtls_md_free(&ctx->ctx);
|
||||
ctx->hmac = NULL;
|
||||
if (n)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
564
Kinc/Sources/kinc/libs/tls/mbedtls/lws-genrsa.c
Normal file
564
Kinc/Sources/kinc/libs/tls/mbedtls/lws-genrsa.c
Normal file
@ -0,0 +1,564 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* lws_genrsa provides an RSA abstraction api in lws that works the
|
||||
* same whether you are using openssl or mbedtls crypto functions underneath.
|
||||
*/
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-tls-mbedtls.h"
|
||||
#include <mbedtls/rsa.h>
|
||||
|
||||
void
|
||||
lws_genrsa_destroy_elements(struct lws_gencrypto_keyelem *el)
|
||||
{
|
||||
int n;
|
||||
|
||||
for (n = 0; n < LWS_GENCRYPTO_RSA_KEYEL_COUNT; n++)
|
||||
if (el[n].buf)
|
||||
lws_free_set_NULL(el[n].buf);
|
||||
}
|
||||
|
||||
static int mode_map[] = { MBEDTLS_RSA_PKCS_V15, MBEDTLS_RSA_PKCS_V21 };
|
||||
|
||||
int
|
||||
lws_genrsa_create(struct lws_genrsa_ctx *ctx,
|
||||
const struct lws_gencrypto_keyelem *el,
|
||||
struct lws_context *context, enum enum_genrsa_mode mode,
|
||||
enum lws_genhash_types oaep_hashid)
|
||||
{
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
ctx->ctx = lws_zalloc(sizeof(*ctx->ctx), "genrsa");
|
||||
if (!ctx->ctx)
|
||||
return 1;
|
||||
|
||||
ctx->context = context;
|
||||
ctx->mode = mode;
|
||||
|
||||
if (mode >= LGRSAM_COUNT)
|
||||
return -1;
|
||||
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
mbedtls_rsa_init(ctx->ctx, mode_map[mode], 0);
|
||||
#else
|
||||
mbedtls_rsa_init(ctx->ctx);
|
||||
mbedtls_rsa_set_padding(ctx->ctx, mode_map[mode], 0);
|
||||
#endif
|
||||
|
||||
ctx->ctx->MBEDTLS_PRIVATE(padding) = mode_map[mode];
|
||||
ctx->ctx->MBEDTLS_PRIVATE(hash_id) =
|
||||
(int)lws_gencrypto_mbedtls_hash_to_MD_TYPE(oaep_hashid);
|
||||
|
||||
{
|
||||
int n;
|
||||
|
||||
mbedtls_mpi *mpi[LWS_GENCRYPTO_RSA_KEYEL_COUNT] = {
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(E),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(N),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(D),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(P),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(Q),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(DP),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(DQ),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(QP),
|
||||
};
|
||||
|
||||
for (n = 0; n < LWS_GENCRYPTO_RSA_KEYEL_COUNT; n++)
|
||||
if (el[n].buf &&
|
||||
mbedtls_mpi_read_binary(mpi[n], el[n].buf,
|
||||
el[n].len)) {
|
||||
lwsl_notice("mpi load failed\n");
|
||||
lws_free_set_NULL(ctx->ctx);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* mbedtls... compute missing P & Q */
|
||||
|
||||
if ( el[LWS_GENCRYPTO_RSA_KEYEL_D].len &&
|
||||
!el[LWS_GENCRYPTO_RSA_KEYEL_P].len &&
|
||||
!el[LWS_GENCRYPTO_RSA_KEYEL_Q].len) {
|
||||
#if defined(LWS_HAVE_mbedtls_rsa_complete)
|
||||
if (mbedtls_rsa_complete(ctx->ctx)) {
|
||||
lwsl_notice("mbedtls_rsa_complete failed\n");
|
||||
#else
|
||||
{
|
||||
lwsl_notice("%s: you have to provide P and Q\n", __func__);
|
||||
#endif
|
||||
lws_free_set_NULL(ctx->ctx);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
ctx->ctx->MBEDTLS_PRIVATE(len) = el[LWS_GENCRYPTO_RSA_KEYEL_N].len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_rngf(void *context, unsigned char *buf, size_t len)
|
||||
{
|
||||
if ((size_t)lws_get_random(context, buf, len) == len)
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
|
||||
enum enum_genrsa_mode mode, struct lws_gencrypto_keyelem *el,
|
||||
int bits)
|
||||
{
|
||||
int n;
|
||||
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
ctx->ctx = lws_zalloc(sizeof(*ctx->ctx), "genrsa");
|
||||
if (!ctx->ctx)
|
||||
return -1;
|
||||
|
||||
ctx->context = context;
|
||||
ctx->mode = mode;
|
||||
|
||||
if (mode >= LGRSAM_COUNT)
|
||||
return -1;
|
||||
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
mbedtls_rsa_init(ctx->ctx, mode_map[mode], 0);
|
||||
#else
|
||||
mbedtls_rsa_init(ctx->ctx);
|
||||
mbedtls_rsa_set_padding(ctx->ctx, mode_map[mode], 0);
|
||||
#endif
|
||||
|
||||
n = mbedtls_rsa_gen_key(ctx->ctx, _rngf, context, (unsigned int)bits, 65537);
|
||||
if (n) {
|
||||
lwsl_err("mbedtls_rsa_gen_key failed 0x%x\n", -n);
|
||||
goto cleanup_1;
|
||||
}
|
||||
|
||||
{
|
||||
mbedtls_mpi *mpi[LWS_GENCRYPTO_RSA_KEYEL_COUNT] = {
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(E),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(N),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(D),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(P),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(Q),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(DP),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(DQ),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(QP),
|
||||
};
|
||||
|
||||
for (n = 0; n < LWS_GENCRYPTO_RSA_KEYEL_COUNT; n++)
|
||||
if (mpi[n] && mbedtls_mpi_size(mpi[n])) {
|
||||
el[n].buf = lws_malloc(
|
||||
mbedtls_mpi_size(mpi[n]), "genrsakey");
|
||||
if (!el[n].buf)
|
||||
goto cleanup;
|
||||
el[n].len = (uint32_t)mbedtls_mpi_size(mpi[n]);
|
||||
if (mbedtls_mpi_write_binary(mpi[n], el[n].buf,
|
||||
el[n].len))
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
cleanup:
|
||||
for (n = 0; n < LWS_GENCRYPTO_RSA_KEYEL_COUNT; n++)
|
||||
if (el[n].buf)
|
||||
lws_free_set_NULL(el[n].buf);
|
||||
cleanup_1:
|
||||
lws_free(ctx->ctx);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genrsa_public_decrypt(struct lws_genrsa_ctx *ctx, const uint8_t *in,
|
||||
size_t in_len, uint8_t *out, size_t out_max)
|
||||
{
|
||||
size_t olen = 0;
|
||||
int n;
|
||||
|
||||
ctx->ctx->MBEDTLS_PRIVATE(len) = in_len;
|
||||
|
||||
#if defined(LWS_HAVE_mbedtls_rsa_complete)
|
||||
mbedtls_rsa_complete(ctx->ctx);
|
||||
#endif
|
||||
|
||||
switch(ctx->mode) {
|
||||
case LGRSAM_PKCS1_1_5:
|
||||
n = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx->ctx, _rngf,
|
||||
ctx->context,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PUBLIC,
|
||||
#endif
|
||||
&olen, in, out,
|
||||
out_max);
|
||||
break;
|
||||
case LGRSAM_PKCS1_OAEP_PSS:
|
||||
n = mbedtls_rsa_rsaes_oaep_decrypt(ctx->ctx, _rngf,
|
||||
ctx->context,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PUBLIC,
|
||||
#endif
|
||||
NULL, 0,
|
||||
&olen, in, out, out_max);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
if (n) {
|
||||
lwsl_notice("%s: -0x%x\n", __func__, -n);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)olen;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genrsa_private_decrypt(struct lws_genrsa_ctx *ctx, const uint8_t *in,
|
||||
size_t in_len, uint8_t *out, size_t out_max)
|
||||
{
|
||||
size_t olen = 0;
|
||||
int n;
|
||||
|
||||
ctx->ctx->MBEDTLS_PRIVATE(len) = in_len;
|
||||
|
||||
#if defined(LWS_HAVE_mbedtls_rsa_complete)
|
||||
mbedtls_rsa_complete(ctx->ctx);
|
||||
#endif
|
||||
|
||||
switch(ctx->mode) {
|
||||
case LGRSAM_PKCS1_1_5:
|
||||
n = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx->ctx, _rngf,
|
||||
ctx->context,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PRIVATE,
|
||||
#endif
|
||||
&olen, in, out,
|
||||
out_max);
|
||||
break;
|
||||
case LGRSAM_PKCS1_OAEP_PSS:
|
||||
n = mbedtls_rsa_rsaes_oaep_decrypt(ctx->ctx, _rngf,
|
||||
ctx->context,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PRIVATE,
|
||||
#endif
|
||||
NULL, 0,
|
||||
&olen, in, out, out_max);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
if (n) {
|
||||
lwsl_notice("%s: -0x%x\n", __func__, -n);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)olen;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genrsa_public_encrypt(struct lws_genrsa_ctx *ctx, const uint8_t *in,
|
||||
size_t in_len, uint8_t *out)
|
||||
{
|
||||
int n;
|
||||
|
||||
#if defined(LWS_HAVE_mbedtls_rsa_complete)
|
||||
mbedtls_rsa_complete(ctx->ctx);
|
||||
#endif
|
||||
|
||||
switch(ctx->mode) {
|
||||
case LGRSAM_PKCS1_1_5:
|
||||
n = mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx->ctx, _rngf,
|
||||
ctx->context,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PUBLIC,
|
||||
#endif
|
||||
in_len, in, out);
|
||||
break;
|
||||
case LGRSAM_PKCS1_OAEP_PSS:
|
||||
n = mbedtls_rsa_rsaes_oaep_encrypt(ctx->ctx, _rngf,
|
||||
ctx->context,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PUBLIC,
|
||||
#endif
|
||||
NULL, 0,
|
||||
in_len, in, out);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
if (n < 0) {
|
||||
lwsl_notice("%s: -0x%x: in_len: %d\n", __func__, -n,
|
||||
(int)in_len);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)mbedtls_mpi_size(&ctx->ctx->MBEDTLS_PRIVATE(N));
|
||||
}
|
||||
|
||||
int
|
||||
lws_genrsa_private_encrypt(struct lws_genrsa_ctx *ctx, const uint8_t *in,
|
||||
size_t in_len, uint8_t *out)
|
||||
{
|
||||
int n;
|
||||
|
||||
#if defined(LWS_HAVE_mbedtls_rsa_complete)
|
||||
mbedtls_rsa_complete(ctx->ctx);
|
||||
#endif
|
||||
|
||||
switch(ctx->mode) {
|
||||
case LGRSAM_PKCS1_1_5:
|
||||
n = mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx->ctx, _rngf,
|
||||
ctx->context,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PRIVATE,
|
||||
#endif
|
||||
in_len, in, out);
|
||||
break;
|
||||
case LGRSAM_PKCS1_OAEP_PSS:
|
||||
n = mbedtls_rsa_rsaes_oaep_encrypt(ctx->ctx, _rngf,
|
||||
ctx->context,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PRIVATE,
|
||||
#endif
|
||||
NULL, 0,
|
||||
in_len, in, out);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
if (n) {
|
||||
lwsl_notice("%s: -0x%x: in_len: %d\n", __func__, -n,
|
||||
(int)in_len);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)mbedtls_mpi_size(&ctx->ctx->MBEDTLS_PRIVATE(N));
|
||||
}
|
||||
|
||||
int
|
||||
lws_genrsa_hash_sig_verify(struct lws_genrsa_ctx *ctx, const uint8_t *in,
|
||||
enum lws_genhash_types hash_type, const uint8_t *sig,
|
||||
size_t sig_len)
|
||||
{
|
||||
int n, h = (int)lws_gencrypto_mbedtls_hash_to_MD_TYPE(hash_type);
|
||||
|
||||
if (h < 0)
|
||||
return -1;
|
||||
|
||||
#if defined(LWS_HAVE_mbedtls_rsa_complete)
|
||||
mbedtls_rsa_complete(ctx->ctx);
|
||||
#endif
|
||||
|
||||
switch(ctx->mode) {
|
||||
case LGRSAM_PKCS1_1_5:
|
||||
n = mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx->ctx,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
NULL, NULL,
|
||||
MBEDTLS_RSA_PUBLIC,
|
||||
#endif
|
||||
(mbedtls_md_type_t)h,
|
||||
(unsigned int)lws_genhash_size(hash_type),
|
||||
in, sig);
|
||||
break;
|
||||
case LGRSAM_PKCS1_OAEP_PSS:
|
||||
n = mbedtls_rsa_rsassa_pss_verify(ctx->ctx,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
NULL, NULL,
|
||||
MBEDTLS_RSA_PUBLIC,
|
||||
#endif
|
||||
(mbedtls_md_type_t)h,
|
||||
(unsigned int)lws_genhash_size(hash_type),
|
||||
in, sig);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
if (n < 0) {
|
||||
lwsl_notice("%s: (mode %d) -0x%x\n", __func__, ctx->mode, -n);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int
|
||||
lws_genrsa_hash_sign(struct lws_genrsa_ctx *ctx, const uint8_t *in,
|
||||
enum lws_genhash_types hash_type, uint8_t *sig,
|
||||
size_t sig_len)
|
||||
{
|
||||
int n, h = (int)lws_gencrypto_mbedtls_hash_to_MD_TYPE(hash_type);
|
||||
|
||||
if (h < 0)
|
||||
return -1;
|
||||
|
||||
#if defined(LWS_HAVE_mbedtls_rsa_complete)
|
||||
mbedtls_rsa_complete(ctx->ctx);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* The "sig" buffer must be as large as the size of ctx->N
|
||||
* (eg. 128 bytes if RSA-1024 is used).
|
||||
*/
|
||||
if (sig_len < ctx->ctx->MBEDTLS_PRIVATE(len))
|
||||
return -1;
|
||||
|
||||
switch(ctx->mode) {
|
||||
case LGRSAM_PKCS1_1_5:
|
||||
n = mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx->ctx,
|
||||
mbedtls_ctr_drbg_random,
|
||||
&ctx->context->mcdc,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PRIVATE,
|
||||
#endif
|
||||
(mbedtls_md_type_t)h,
|
||||
(unsigned int)lws_genhash_size(hash_type),
|
||||
in, sig);
|
||||
break;
|
||||
case LGRSAM_PKCS1_OAEP_PSS:
|
||||
n = mbedtls_rsa_rsassa_pss_sign(ctx->ctx,
|
||||
mbedtls_ctr_drbg_random,
|
||||
&ctx->context->mcdc,
|
||||
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x03000000
|
||||
MBEDTLS_RSA_PRIVATE,
|
||||
#endif
|
||||
(mbedtls_md_type_t)h,
|
||||
(unsigned int)lws_genhash_size(hash_type),
|
||||
in, sig);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (n < 0) {
|
||||
lwsl_notice("%s: -0x%x\n", __func__, -n);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)ctx->ctx->MBEDTLS_PRIVATE(len);
|
||||
}
|
||||
|
||||
int
|
||||
lws_genrsa_render_pkey_asn1(struct lws_genrsa_ctx *ctx, int _private,
|
||||
uint8_t *pkey_asn1, size_t pkey_asn1_len)
|
||||
{
|
||||
uint8_t *p = pkey_asn1, *totlen, *end = pkey_asn1 + pkey_asn1_len - 1;
|
||||
mbedtls_mpi *mpi[LWS_GENCRYPTO_RSA_KEYEL_COUNT] = {
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(N),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(E),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(D),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(P),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(Q),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(DP),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(DQ),
|
||||
&ctx->ctx->MBEDTLS_PRIVATE(QP),
|
||||
};
|
||||
int n;
|
||||
|
||||
/* 30 82 - sequence
|
||||
* 09 29 <-- length(0x0929) less 4 bytes
|
||||
* 02 01 <- length (1)
|
||||
* 00
|
||||
* 02 82
|
||||
* 02 01 <- length (513) N
|
||||
* ...
|
||||
*
|
||||
* 02 03 <- length (3) E
|
||||
* 01 00 01
|
||||
*
|
||||
* 02 82
|
||||
* 02 00 <- length (512) D P Q EXP1 EXP2 COEFF
|
||||
*
|
||||
* */
|
||||
|
||||
*p++ = 0x30;
|
||||
*p++ = 0x82;
|
||||
totlen = p;
|
||||
p += 2;
|
||||
|
||||
*p++ = 0x02;
|
||||
*p++ = 0x01;
|
||||
*p++ = 0x00;
|
||||
|
||||
for (n = 0; n < LWS_GENCRYPTO_RSA_KEYEL_COUNT; n++) {
|
||||
int m = (int)mbedtls_mpi_size(mpi[n]);
|
||||
uint8_t *elen;
|
||||
|
||||
*p++ = 0x02;
|
||||
elen = p;
|
||||
if (m < 0x7f)
|
||||
*p++ = (uint8_t)m;
|
||||
else {
|
||||
*p++ = 0x82;
|
||||
*p++ = (uint8_t)(m >> 8);
|
||||
*p++ = (uint8_t)(m & 0xff);
|
||||
}
|
||||
|
||||
if (p + m > end)
|
||||
return -1;
|
||||
|
||||
if (mbedtls_mpi_write_binary(mpi[n], p, (unsigned int)m))
|
||||
return -1;
|
||||
if (p[0] & 0x80) {
|
||||
p[0] = 0x00;
|
||||
if (mbedtls_mpi_write_binary(mpi[n], &p[1], (unsigned int)m))
|
||||
return -1;
|
||||
m++;
|
||||
}
|
||||
if (m < 0x7f)
|
||||
*elen = (uint8_t)m;
|
||||
else {
|
||||
*elen++ = 0x82;
|
||||
*elen++ = (uint8_t)(m >> 8);
|
||||
*elen = (uint8_t)(m & 0xff);
|
||||
}
|
||||
p += m;
|
||||
}
|
||||
|
||||
n = lws_ptr_diff(p, pkey_asn1);
|
||||
|
||||
*totlen++ = (uint8_t)((n - 4) >> 8);
|
||||
*totlen = (uint8_t)((n - 4) & 0xff);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void
|
||||
lws_genrsa_destroy(struct lws_genrsa_ctx *ctx)
|
||||
{
|
||||
if (!ctx->ctx)
|
||||
return;
|
||||
mbedtls_rsa_free(ctx->ctx);
|
||||
lws_free(ctx->ctx);
|
||||
ctx->ctx = NULL;
|
||||
}
|
552
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-client.c
Normal file
552
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-client.c
Normal file
@ -0,0 +1,552 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-tls-mbedtls.h"
|
||||
|
||||
#if defined(LWS_WITH_TLS_JIT_TRUST)
|
||||
|
||||
/*
|
||||
* We get called for each peer certificate that was provided in turn.
|
||||
*
|
||||
* Our job is just to collect the AKID and SKIDs into ssl->kid_chain, and walk
|
||||
* later at verification result time if it failed.
|
||||
*
|
||||
* None of these should be trusted, even if a misconfigured server sends us
|
||||
* his root CA.
|
||||
*/
|
||||
|
||||
static int
|
||||
lws_mbedtls_client_verify_callback(SSL *ssl, mbedtls_x509_crt *x509)
|
||||
{
|
||||
union lws_tls_cert_info_results ci;
|
||||
|
||||
/* we reached the max we can hold? */
|
||||
|
||||
if (ssl->kid_chain.count == LWS_ARRAY_SIZE(ssl->kid_chain.akid))
|
||||
return 0;
|
||||
|
||||
/* if not, stash the SKID and AKID into the next kid slot */
|
||||
|
||||
if (!lws_tls_mbedtls_cert_info(x509, LWS_TLS_CERT_INFO_SUBJECT_KEY_ID,
|
||||
&ci, 0))
|
||||
lws_tls_kid_copy(&ci,
|
||||
&ssl->kid_chain.skid[ssl->kid_chain.count]);
|
||||
|
||||
if (!lws_tls_mbedtls_cert_info(x509, LWS_TLS_CERT_INFO_AUTHORITY_KEY_ID,
|
||||
&ci, 0))
|
||||
lws_tls_kid_copy(&ci,
|
||||
&ssl->kid_chain.akid[ssl->kid_chain.count]);
|
||||
|
||||
ssl->kid_chain.count++;
|
||||
|
||||
// lwsl_notice("%s: %u\n", __func__, ssl->kid_chain.count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int
|
||||
lws_ssl_client_bio_create(struct lws *wsi)
|
||||
{
|
||||
char hostname[128], *p;
|
||||
const char *alpn_comma = wsi->a.context->tls.alpn_default;
|
||||
struct alpn_ctx protos;
|
||||
int fl = SSL_VERIFY_PEER;
|
||||
|
||||
if (wsi->stash)
|
||||
lws_strncpy(hostname, wsi->stash->cis[CIS_HOST], sizeof(hostname));
|
||||
else
|
||||
if (lws_hdr_copy(wsi, hostname, sizeof(hostname),
|
||||
_WSI_TOKEN_CLIENT_HOST) <= 0) {
|
||||
lwsl_err("%s: Unable to get hostname\n", __func__);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* remove any :port part on the hostname... necessary for network
|
||||
* connection but typical certificates do not contain it
|
||||
*/
|
||||
p = hostname;
|
||||
while (*p) {
|
||||
if (*p == ':') {
|
||||
*p = '\0';
|
||||
break;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
|
||||
wsi->tls.ssl = SSL_new(wsi->a.vhost->tls.ssl_client_ctx);
|
||||
if (!wsi->tls.ssl) {
|
||||
lwsl_info("%s: SSL_new() failed\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_TLS_SESSIONS)
|
||||
if (!(wsi->a.vhost->options & LWS_SERVER_OPTION_DISABLE_TLS_SESSION_CACHE))
|
||||
lws_tls_reuse_session(wsi);
|
||||
#endif
|
||||
|
||||
if (wsi->a.vhost->tls.ssl_info_event_mask)
|
||||
SSL_set_info_callback(wsi->tls.ssl, lws_ssl_info_callback);
|
||||
|
||||
if (!(wsi->tls.use_ssl & LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK)) {
|
||||
X509_VERIFY_PARAM *param = SSL_get0_param(wsi->tls.ssl);
|
||||
/* Enable automatic hostname checks */
|
||||
// X509_VERIFY_PARAM_set_hostflags(param,
|
||||
// X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
|
||||
lwsl_info("%s: setting hostname %s\n", __func__, hostname);
|
||||
if (X509_VERIFY_PARAM_set1_host(param, hostname, 0) != 1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (wsi->a.vhost->tls.alpn)
|
||||
alpn_comma = wsi->a.vhost->tls.alpn;
|
||||
|
||||
if (wsi->stash) {
|
||||
lws_strncpy(hostname, wsi->stash->cis[CIS_HOST],
|
||||
sizeof(hostname));
|
||||
if (wsi->stash->cis[CIS_ALPN])
|
||||
alpn_comma = wsi->stash->cis[CIS_ALPN];
|
||||
} else {
|
||||
if (lws_hdr_copy(wsi, hostname, sizeof(hostname),
|
||||
_WSI_TOKEN_CLIENT_ALPN) > 0)
|
||||
alpn_comma = hostname;
|
||||
}
|
||||
|
||||
protos.len = (uint8_t)lws_alpn_comma_to_openssl(alpn_comma, protos.data,
|
||||
sizeof(protos.data) - 1);
|
||||
|
||||
lwsl_info("%s: %s: client conn sending ALPN list '%s' (protos.len %d)\n",
|
||||
__func__, lws_wsi_tag(wsi), alpn_comma, protos.len);
|
||||
|
||||
/* with mbedtls, protos is not pointed to after exit from this call */
|
||||
SSL_set_alpn_select_cb(wsi->tls.ssl, &protos);
|
||||
|
||||
if (wsi->flags & LCCSCF_ALLOW_SELFSIGNED) {
|
||||
lwsl_notice("%s: allowing selfsigned\n", __func__);
|
||||
fl = SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
|
||||
}
|
||||
|
||||
if (wsi->flags & LCCSCF_ALLOW_INSECURE)
|
||||
fl = SSL_VERIFY_NONE;
|
||||
|
||||
/*
|
||||
* use server name indication (SNI), if supported,
|
||||
* when establishing connection
|
||||
*/
|
||||
#if defined(LWS_WITH_TLS_JIT_TRUST)
|
||||
SSL_set_verify(wsi->tls.ssl, SSL_VERIFY_PEER,
|
||||
lws_mbedtls_client_verify_callback);
|
||||
(void)fl;
|
||||
#else
|
||||
SSL_set_verify(wsi->tls.ssl, fl, NULL);
|
||||
#endif
|
||||
|
||||
SSL_set_fd(wsi->tls.ssl, (int)wsi->desc.sockfd);
|
||||
|
||||
if (wsi->sys_tls_client_cert) {
|
||||
lws_system_blob_t *b = lws_system_get_blob(wsi->a.context,
|
||||
LWS_SYSBLOB_TYPE_CLIENT_CERT_DER,
|
||||
wsi->sys_tls_client_cert - 1);
|
||||
const uint8_t *pem_data = NULL;
|
||||
uint8_t *data = NULL;
|
||||
lws_filepos_t flen;
|
||||
size_t size;
|
||||
int err = 0;
|
||||
|
||||
if (!b)
|
||||
goto no_client_cert;
|
||||
|
||||
/*
|
||||
* Set up the per-connection client cert
|
||||
*/
|
||||
|
||||
size = lws_system_blob_get_size(b);
|
||||
if (!size)
|
||||
goto no_client_cert;
|
||||
|
||||
if (lws_system_blob_get_single_ptr(b, &pem_data))
|
||||
goto no_client_cert;
|
||||
|
||||
if (lws_tls_alloc_pem_to_der_file(wsi->a.context, NULL,
|
||||
(const char *)pem_data, size,
|
||||
&data, &flen))
|
||||
goto no_client_cert;
|
||||
size = (size_t) flen;
|
||||
|
||||
err = SSL_use_certificate_ASN1(wsi->tls.ssl, data, (int)size);
|
||||
lws_free_set_NULL(data);
|
||||
if (err != 1)
|
||||
goto no_client_cert;
|
||||
|
||||
b = lws_system_get_blob(wsi->a.context,
|
||||
LWS_SYSBLOB_TYPE_CLIENT_KEY_DER,
|
||||
wsi->sys_tls_client_cert - 1);
|
||||
if (!b)
|
||||
goto no_client_cert;
|
||||
size = lws_system_blob_get_size(b);
|
||||
if (!size)
|
||||
goto no_client_cert;
|
||||
|
||||
if (lws_system_blob_get_single_ptr(b, &pem_data))
|
||||
goto no_client_cert;
|
||||
|
||||
if (lws_tls_alloc_pem_to_der_file(wsi->a.context, NULL,
|
||||
(const char *)pem_data, size,
|
||||
&data, &flen))
|
||||
goto no_client_cert;
|
||||
size = (size_t) flen;
|
||||
|
||||
err = SSL_use_PrivateKey_ASN1(0, wsi->tls.ssl, data, (int)size);
|
||||
lws_free_set_NULL(data);
|
||||
if (err != 1)
|
||||
goto no_client_cert;
|
||||
|
||||
/* no wrapper api for check key */
|
||||
|
||||
lwsl_notice("%s: set system client cert %u\n", __func__,
|
||||
wsi->sys_tls_client_cert - 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
no_client_cert:
|
||||
lwsl_err("%s: unable to set up system client cert %d\n", __func__,
|
||||
wsi->sys_tls_client_cert - 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ERR_get_error(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum lws_ssl_capable_status
|
||||
lws_tls_client_connect(struct lws *wsi, char *errbuf, size_t elen)
|
||||
{
|
||||
int m, n = SSL_connect(wsi->tls.ssl), en;
|
||||
|
||||
if (n == 1) {
|
||||
lws_tls_server_conn_alpn(wsi);
|
||||
#if defined(LWS_WITH_TLS_SESSIONS)
|
||||
lws_tls_session_new_mbedtls(wsi);
|
||||
#endif
|
||||
lwsl_info("%s: client connect OK\n", __func__);
|
||||
return LWS_SSL_CAPABLE_DONE;
|
||||
}
|
||||
|
||||
en = (int)LWS_ERRNO;
|
||||
m = SSL_get_error(wsi->tls.ssl, n);
|
||||
|
||||
if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl))
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
||||
|
||||
if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl))
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE;
|
||||
|
||||
if (!n) /* we don't know what he wants, but he says to retry */
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
||||
|
||||
if (m == SSL_ERROR_SYSCALL && !en && n >= 0) /* otherwise we miss explicit failures and spin
|
||||
* in hs state 17 until timeout... */
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
||||
|
||||
lws_snprintf(errbuf, elen, "mbedtls connect %d %d %d", n, m, en);
|
||||
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_client_confirm_peer_cert(struct lws *wsi, char *ebuf, size_t ebuf_len)
|
||||
{
|
||||
int n;
|
||||
unsigned int avoid = 0;
|
||||
X509 *peer = SSL_get_peer_certificate(wsi->tls.ssl);
|
||||
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
||||
const char *type = "";
|
||||
char *sb = (char *)&pt->serv_buf[0];
|
||||
|
||||
if (!peer) {
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
lws_metrics_hist_bump_describe_wsi(wsi, lws_metrics_priv_to_pub(
|
||||
wsi->a.context->mth_conn_failures),
|
||||
"tls=\"nocert\"");
|
||||
#endif
|
||||
lwsl_info("peer did not provide cert\n");
|
||||
lws_snprintf(ebuf, ebuf_len, "no peer cert");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
n = (int)SSL_get_verify_result(wsi->tls.ssl);
|
||||
lwsl_debug("get_verify says %d\n", n);
|
||||
|
||||
switch (n) {
|
||||
case X509_V_OK:
|
||||
return 0;
|
||||
|
||||
case X509_V_ERR_HOSTNAME_MISMATCH:
|
||||
type = "hostname";
|
||||
avoid = LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
|
||||
break;
|
||||
|
||||
case X509_V_ERR_INVALID_CA:
|
||||
type = "invalidca";
|
||||
avoid = LCCSCF_ALLOW_SELFSIGNED;
|
||||
break;
|
||||
|
||||
case X509_V_ERR_CERT_NOT_YET_VALID:
|
||||
type = "notyetvalid";
|
||||
avoid = LCCSCF_ALLOW_EXPIRED;
|
||||
break;
|
||||
|
||||
case X509_V_ERR_CERT_HAS_EXPIRED:
|
||||
type = "expired";
|
||||
avoid = LCCSCF_ALLOW_EXPIRED;
|
||||
break;
|
||||
}
|
||||
|
||||
lwsl_info("%s: cert problem: %s\n", __func__, type);
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
{
|
||||
char buckname[64];
|
||||
lws_snprintf(buckname, sizeof(buckname), "tls=\"%s\"", type);
|
||||
lws_metrics_hist_bump_describe_wsi(wsi,
|
||||
lws_metrics_priv_to_pub(wsi->a.context->mth_conn_failures),
|
||||
buckname);
|
||||
}
|
||||
#endif
|
||||
if (wsi->tls.use_ssl & avoid) {
|
||||
lwsl_info("%s: allowing anyway\n", __func__);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_TLS_JIT_TRUST)
|
||||
if (n == X509_V_ERR_INVALID_CA)
|
||||
lws_tls_jit_trust_sort_kids(wsi, &wsi->tls.ssl->kid_chain);
|
||||
#endif
|
||||
lws_snprintf(ebuf, ebuf_len,
|
||||
"server's cert didn't look good, %s (use_ssl 0x%x) X509_V_ERR = %d: %s\n",
|
||||
type, (unsigned int)wsi->tls.use_ssl, n,
|
||||
ERR_error_string((unsigned long)n, sb));
|
||||
|
||||
lwsl_info("%s\n", ebuf);
|
||||
|
||||
lws_tls_err_describe_clear();
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_client_create_vhost_context(struct lws_vhost *vh,
|
||||
const struct lws_context_creation_info *info,
|
||||
const char *cipher_list,
|
||||
const char *ca_filepath,
|
||||
const void *ca_mem,
|
||||
unsigned int ca_mem_len,
|
||||
const char *cert_filepath,
|
||||
const void *cert_mem,
|
||||
unsigned int cert_mem_len,
|
||||
const char *private_key_filepath,
|
||||
const void *key_mem,
|
||||
unsigned int key_mem_len
|
||||
)
|
||||
{
|
||||
X509 *d2i_X509(X509 **cert, const unsigned char **buffer, long len);
|
||||
SSL_METHOD *method;
|
||||
unsigned long error;
|
||||
int n;
|
||||
|
||||
#if defined(LWS_WITH_TLS_SESSIONS)
|
||||
vh->tls_session_cache_max = info->tls_session_cache_max ?
|
||||
info->tls_session_cache_max : 10;
|
||||
lws_tls_session_cache(vh, info->tls_session_timeout);
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
method = (SSL_METHOD *)TLSv1_2_client_method();
|
||||
#elif defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
||||
method = (SSL_METHOD *)TLSv1_1_client_method();
|
||||
#elif defined(MBEDTLS_SSL_PROTO_TLS1)
|
||||
method = (SSL_METHOD *)TLSv1_client_method();
|
||||
#else
|
||||
method = (SSL_METHOD *)TLS_client_method();
|
||||
#endif
|
||||
|
||||
if (!method) {
|
||||
error = (unsigned long)ERR_get_error();
|
||||
lwsl_err("problem creating ssl method %lu: %s\n",
|
||||
error, ERR_error_string(error,
|
||||
(char *)vh->context->pt[0].serv_buf));
|
||||
return 1;
|
||||
}
|
||||
/* create context */
|
||||
vh->tls.ssl_client_ctx = SSL_CTX_new(method, &vh->context->mcdc);
|
||||
if (!vh->tls.ssl_client_ctx) {
|
||||
error = (unsigned long)ERR_get_error();
|
||||
lwsl_err("problem creating ssl context %lu: %s\n",
|
||||
error, ERR_error_string(error,
|
||||
(char *)vh->context->pt[0].serv_buf));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!ca_filepath && (!ca_mem || !ca_mem_len))
|
||||
return 0;
|
||||
|
||||
if (ca_filepath) {
|
||||
#if !defined(LWS_PLAT_OPTEE)
|
||||
uint8_t *buf;
|
||||
lws_filepos_t len;
|
||||
|
||||
if (alloc_file(vh->context, ca_filepath, &buf, &len)) {
|
||||
lwsl_err("Load CA cert file %s failed\n", ca_filepath);
|
||||
return 1;
|
||||
}
|
||||
vh->tls.x509_client_CA = d2i_X509(NULL, (const uint8_t **)&buf, (long)len);
|
||||
free(buf);
|
||||
|
||||
lwsl_info("Loading vh %s client CA for verification %s\n", vh->name, ca_filepath);
|
||||
#endif
|
||||
} else {
|
||||
vh->tls.x509_client_CA = d2i_X509(NULL, (const uint8_t **)&ca_mem, (long)ca_mem_len);
|
||||
lwsl_info("%s: using mem client CA cert %d\n",
|
||||
__func__, ca_mem_len);
|
||||
}
|
||||
|
||||
if (!vh->tls.x509_client_CA) {
|
||||
lwsl_err("client CA: x509 parse failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!vh->tls.ssl_ctx)
|
||||
SSL_CTX_add_client_CA(vh->tls.ssl_client_ctx, vh->tls.x509_client_CA);
|
||||
else
|
||||
SSL_CTX_add_client_CA(vh->tls.ssl_ctx, vh->tls.x509_client_CA);
|
||||
|
||||
/* support for client-side certificate authentication */
|
||||
if (cert_filepath) {
|
||||
#if !defined(LWS_PLAT_OPTEE)
|
||||
uint8_t *buf;
|
||||
lws_filepos_t amount;
|
||||
|
||||
if (lws_tls_use_any_upgrade_check_extant(cert_filepath) !=
|
||||
LWS_TLS_EXTANT_YES &&
|
||||
(info->options & LWS_SERVER_OPTION_IGNORE_MISSING_CERT))
|
||||
return 0;
|
||||
|
||||
lwsl_notice("%s: doing cert filepath %s\n", __func__,
|
||||
cert_filepath);
|
||||
|
||||
if (alloc_file(vh->context, cert_filepath, &buf, &amount))
|
||||
return 1;
|
||||
|
||||
buf[amount++] = '\0';
|
||||
|
||||
n = SSL_CTX_use_certificate_ASN1(vh->tls.ssl_client_ctx,
|
||||
(int)amount, buf);
|
||||
lws_free(buf);
|
||||
if (n < 1) {
|
||||
lwsl_err("problem %d getting cert '%s'\n", n,
|
||||
cert_filepath);
|
||||
lws_tls_err_describe_clear();
|
||||
return 1;
|
||||
}
|
||||
|
||||
lwsl_info("Loaded client cert %s\n", cert_filepath);
|
||||
#endif
|
||||
} else if (cert_mem && cert_mem_len) {
|
||||
/* lwsl_hexdump_notice(cert_mem, cert_mem_len - 1); */
|
||||
n = SSL_CTX_use_certificate_ASN1(vh->tls.ssl_client_ctx,
|
||||
(int)cert_mem_len, cert_mem);
|
||||
if (n < 1) {
|
||||
lwsl_err("%s: (mbedtls) problem interpreting client cert\n",
|
||||
__func__);
|
||||
lws_tls_err_describe_clear();
|
||||
return 1;
|
||||
}
|
||||
lwsl_info("%s: using mem client cert %d\n",
|
||||
__func__, cert_mem_len);
|
||||
}
|
||||
|
||||
if (private_key_filepath) {
|
||||
#if !defined(LWS_PLAT_OPTEE)
|
||||
|
||||
uint8_t *buf;
|
||||
lws_filepos_t amount;
|
||||
|
||||
lwsl_notice("%s: doing private key filepath %s\n", __func__,
|
||||
private_key_filepath);
|
||||
if (alloc_file(vh->context, private_key_filepath, &buf, &amount))
|
||||
return 1;
|
||||
|
||||
buf[amount++] = '\0';
|
||||
|
||||
n = SSL_CTX_use_PrivateKey_ASN1(0, vh->tls.ssl_client_ctx,
|
||||
buf, (long)amount);
|
||||
|
||||
lws_free(buf);
|
||||
if (n < 1) {
|
||||
lwsl_err("problem %d getting private key '%s'\n", n,
|
||||
private_key_filepath);
|
||||
lws_tls_err_describe_clear();
|
||||
return 1;
|
||||
}
|
||||
|
||||
lwsl_notice("Loaded private key %s\n", private_key_filepath);
|
||||
#endif
|
||||
} else if (key_mem && key_mem_len) {
|
||||
/* lwsl_hexdump_notice(cert_mem, cert_mem_len - 1); */
|
||||
n = SSL_CTX_use_PrivateKey_ASN1(0, vh->tls.ssl_client_ctx,
|
||||
key_mem, (long)key_mem_len - 1);
|
||||
|
||||
if (n < 1) {
|
||||
lwsl_err("%s: (mbedtls) problem interpreting private key\n",
|
||||
__func__);
|
||||
lws_tls_err_describe_clear();
|
||||
return 1;
|
||||
}
|
||||
lwsl_info("%s: using mem private key %d\n",
|
||||
__func__, key_mem_len);
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_client_vhost_extra_cert_mem(struct lws_vhost *vh,
|
||||
const uint8_t *der, size_t der_len)
|
||||
{
|
||||
if (SSL_CTX_add_client_CA_ASN1(vh->tls.ssl_client_ctx, (int)der_len, der) != 1) {
|
||||
lwsl_err("%s: failed\n", __func__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
512
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-extensions.c
Normal file
512
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-extensions.c
Normal file
@ -0,0 +1,512 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2022 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* These are additional apis that belong in mbedtls but do not yet exist there.
|
||||
* Alternaives are provided for lws to use that understand additional standard
|
||||
* v3 tls extensions. Error results are simplified to lws style.
|
||||
*
|
||||
* This file includes code taken from mbedtls and modified, and from an as of
|
||||
* 2021-06-11 unaccepted-upstream patch for mbedtls contributed by Gábor Tóth
|
||||
* <toth92g@gmail.com>. Gabor has graciously allowed use of his patch with more
|
||||
* liberal terms but to not complicate matters I provide it here under the same
|
||||
* Apache 2.0 terms as the mbedtls pieces.
|
||||
*
|
||||
* Those original pieces are licensed Apache-2.0 as follows
|
||||
*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-tls-mbedtls.h"
|
||||
#include <mbedtls/oid.h>
|
||||
#include <mbedtls/x509.h>
|
||||
|
||||
/*
|
||||
* This section from mbedtls oid.c
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
mbedtls_oid_descriptor_t descriptor;
|
||||
int ext_type;
|
||||
} oid_x509_ext_t;
|
||||
|
||||
#define ADD_LEN(s) s, MBEDTLS_OID_SIZE(s)
|
||||
|
||||
#define LWS_MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER MBEDTLS_OID_ID_CE "\x23" /**< id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 } */
|
||||
#define LWS_MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER MBEDTLS_OID_ID_CE "\x0E" /**< id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 14 } */
|
||||
|
||||
#define LWS_MBEDTLS_OID_X509_EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
|
||||
#define LWS_MBEDTLS_OID_X509_EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
|
||||
|
||||
#define LWS_MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER LWS_MBEDTLS_OID_X509_EXT_AUTHORITY_KEY_IDENTIFIER
|
||||
#define LWS_MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER LWS_MBEDTLS_OID_X509_EXT_SUBJECT_KEY_IDENTIFIER
|
||||
|
||||
#define LWS_MBEDTLS_X509_SAN_OTHER_NAME 0
|
||||
#define LWS_MBEDTLS_X509_SAN_RFC822_NAME 1
|
||||
#define LWS_MBEDTLS_X509_SAN_DNS_NAME 2
|
||||
|
||||
#define LWS_MBEDTLS_ASN1_TAG_CLASS_MASK 0xC0
|
||||
#define LWS_MBEDTLS_ASN1_TAG_VALUE_MASK 0x1F
|
||||
|
||||
static const oid_x509_ext_t oid_x509_ext[] = {
|
||||
{ {ADD_LEN( LWS_MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
|
||||
"id-ce-subjectKeyIdentifier",
|
||||
"Subject Key Identifier" },
|
||||
LWS_MBEDTLS_OID_X509_EXT_SUBJECT_KEY_IDENTIFIER,
|
||||
},
|
||||
{ {ADD_LEN( LWS_MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
|
||||
"id-ce-authorityKeyIdentifier",
|
||||
"Authority Key Identifier" },
|
||||
LWS_MBEDTLS_OID_X509_EXT_AUTHORITY_KEY_IDENTIFIER,
|
||||
},
|
||||
{ { NULL, 0, NULL, NULL }, 0 },
|
||||
};
|
||||
|
||||
#define FN_OID_TYPED_FROM_ASN1( TYPE_T, NAME, LIST ) \
|
||||
static const TYPE_T * oid_ ## NAME ## _from_asn1( \
|
||||
const mbedtls_asn1_buf *oid ) \
|
||||
{ \
|
||||
const TYPE_T *p = (LIST); \
|
||||
const mbedtls_oid_descriptor_t *cur = \
|
||||
(const mbedtls_oid_descriptor_t *) p; \
|
||||
if( p == NULL || oid == NULL ) return( NULL ); \
|
||||
while( cur->MBEDTLS_PRIVATE(asn1) != NULL ) { \
|
||||
if( cur->MBEDTLS_PRIVATE(asn1_len) == oid->MBEDTLS_PRIVATE_V30_ONLY(len) && \
|
||||
memcmp( cur->MBEDTLS_PRIVATE(asn1), oid->MBEDTLS_PRIVATE_V30_ONLY(p), oid->MBEDTLS_PRIVATE_V30_ONLY(len) ) == 0 ) { \
|
||||
return( p ); \
|
||||
} \
|
||||
p++; \
|
||||
cur = (const mbedtls_oid_descriptor_t *) p; \
|
||||
} \
|
||||
return( NULL ); \
|
||||
}
|
||||
|
||||
|
||||
#define FN_OID_GET_ATTR1(FN_NAME, TYPE_T, TYPE_NAME, ATTR1_TYPE, ATTR1) \
|
||||
int FN_NAME( const mbedtls_asn1_buf *oid, ATTR1_TYPE * ATTR1 ) \
|
||||
{ \
|
||||
const TYPE_T *data = oid_ ## TYPE_NAME ## _from_asn1( oid ); \
|
||||
if (!data) return 1; \
|
||||
*ATTR1 = data->ATTR1; \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
FN_OID_TYPED_FROM_ASN1(oid_x509_ext_t, x509_ext, oid_x509_ext)
|
||||
FN_OID_GET_ATTR1(lws_mbedtls_oid_get_x509_ext_type,
|
||||
oid_x509_ext_t, x509_ext, int, ext_type)
|
||||
|
||||
typedef struct lws_mbedtls_x509_san_other_name
|
||||
{
|
||||
/**
|
||||
* The type_id is an OID as deifned in RFC 5280.
|
||||
* To check the value of the type id, you should use
|
||||
* \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
|
||||
*/
|
||||
mbedtls_x509_buf type_id; /**< The type id. */
|
||||
union
|
||||
{
|
||||
/**
|
||||
* From RFC 4108 section 5:
|
||||
* HardwareModuleName ::= SEQUENCE {
|
||||
* hwType OBJECT IDENTIFIER,
|
||||
* hwSerialNum OCTET STRING }
|
||||
*/
|
||||
struct
|
||||
{
|
||||
mbedtls_x509_buf oid; /**< The object identifier. */
|
||||
mbedtls_x509_buf val; /**< The named value. */
|
||||
}
|
||||
hardware_module_name;
|
||||
}
|
||||
value;
|
||||
}
|
||||
lws_mbedtls_x509_san_other_name;
|
||||
|
||||
|
||||
typedef struct lws_mbedtls_x509_subject_alternative_name
|
||||
{
|
||||
int type; /**< The SAN type, value of LWS_MBEDTLS_X509_SAN_XXX. */
|
||||
union {
|
||||
lws_mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
|
||||
mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
|
||||
}
|
||||
san; /**< A union of the supported SAN types */
|
||||
}
|
||||
lws_mbedtls_x509_subject_alternative_name;
|
||||
|
||||
static int
|
||||
x509_get_skid(uint8_t **p, const uint8_t *end, mbedtls_x509_buf *skid)
|
||||
{
|
||||
int ret = 1;
|
||||
size_t len = 0u;
|
||||
|
||||
ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
skid->MBEDTLS_PRIVATE_V30_ONLY(len) = len;
|
||||
skid->MBEDTLS_PRIVATE_V30_ONLY(tag) = MBEDTLS_ASN1_OCTET_STRING;
|
||||
skid->MBEDTLS_PRIVATE_V30_ONLY(p) = *p;
|
||||
*p += len;
|
||||
|
||||
return *p != end;
|
||||
}
|
||||
|
||||
/*
|
||||
* Names may have multiple allocated segments in a linked-list, when the mbedtls
|
||||
* api mbedtls_x509_get_name() fails, it doesn't clean up any already-allocated
|
||||
* segments, wrongly leaving it to the caller to handle. This helper takes care
|
||||
* of the missing cleaning for allocation error path.
|
||||
*
|
||||
* name.next must be set to NULL by user code before calling ...get_name(...,
|
||||
* &name), since not every error exit sets it and it will contain garbage if
|
||||
* defined on stack as is usual.
|
||||
*/
|
||||
|
||||
static void
|
||||
lws_x509_clean_name(mbedtls_x509_name *name)
|
||||
{
|
||||
mbedtls_x509_name *n1;
|
||||
|
||||
if (!name)
|
||||
return;
|
||||
|
||||
n1 = name->MBEDTLS_PRIVATE_V30_ONLY(next);
|
||||
|
||||
while (n1) {
|
||||
name = n1->MBEDTLS_PRIVATE_V30_ONLY(next);
|
||||
free(n1);
|
||||
n1 = name;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
lws_mbedtls_x509_parse_general_name(const mbedtls_x509_buf *name_buf,
|
||||
lws_mbedtls_x509_subject_alternative_name *name)
|
||||
{
|
||||
// mbedtls_x509_name_other_name other_name;
|
||||
uint8_t *bufferPointer, **p, *end;
|
||||
mbedtls_x509_name rfc822Name;
|
||||
int ret;
|
||||
|
||||
switch (name_buf->MBEDTLS_PRIVATE_V30_ONLY(tag) &
|
||||
(LWS_MBEDTLS_ASN1_TAG_CLASS_MASK |
|
||||
LWS_MBEDTLS_ASN1_TAG_VALUE_MASK)) {
|
||||
|
||||
#if 0
|
||||
case MBEDTLS_ASN1_CONTEXT_SPECIFIC | LWS_MBEDTLS_X509_SAN_OTHER_NAME:
|
||||
ret = x509_get_other_name( name_buf, &other_name );
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
memset(name, 0, sizeof(*name));
|
||||
name->type = LWS_MBEDTLS_X509_SAN_OTHER_NAME;
|
||||
memcpy(&name->name.other_name, &other_name, sizeof(other_name));
|
||||
return 0;
|
||||
#endif
|
||||
case MBEDTLS_ASN1_SEQUENCE | LWS_MBEDTLS_X509_SAN_RFC822_NAME:
|
||||
|
||||
bufferPointer = name_buf->MBEDTLS_PRIVATE_V30_ONLY(p);
|
||||
p = &bufferPointer;
|
||||
end = name_buf->MBEDTLS_PRIVATE_V30_ONLY(p) +
|
||||
name_buf->MBEDTLS_PRIVATE_V30_ONLY(len);
|
||||
|
||||
/* The leading ASN1 tag and length has been processed.
|
||||
* Stepping back with 2 bytes, because mbedtls_x509_get_name
|
||||
* expects the beginning of the SET tag */
|
||||
*p = *p - 2;
|
||||
|
||||
rfc822Name.MBEDTLS_PRIVATE_V30_ONLY(next) = NULL;
|
||||
ret = mbedtls_x509_get_name( p, end, &rfc822Name );
|
||||
if (ret) {
|
||||
lws_x509_clean_name(&rfc822Name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
memset(name, 0, sizeof(*name));
|
||||
name->type = LWS_MBEDTLS_X509_SAN_OTHER_NAME;
|
||||
memcpy(&name->san.other_name,
|
||||
&rfc822Name, sizeof(rfc822Name));
|
||||
return 0;
|
||||
|
||||
case MBEDTLS_ASN1_CONTEXT_SPECIFIC | LWS_MBEDTLS_X509_SAN_DNS_NAME:
|
||||
memset(name, 0, sizeof(*name));
|
||||
name->type = LWS_MBEDTLS_X509_SAN_DNS_NAME;
|
||||
|
||||
memcpy(&name->san.unstructured_name,
|
||||
name_buf, sizeof(*name_buf) );
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
lws_x509_get_general_names(uint8_t **p, const uint8_t *end,
|
||||
mbedtls_x509_sequence *name )
|
||||
{
|
||||
mbedtls_asn1_sequence *cur = name;
|
||||
mbedtls_asn1_buf *buf;
|
||||
size_t len, tag_len;
|
||||
unsigned char tag;
|
||||
int r;
|
||||
|
||||
/* Get main sequence tag */
|
||||
r = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
|
||||
MBEDTLS_ASN1_SEQUENCE);
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
if (*p + len != end)
|
||||
return 1;
|
||||
|
||||
while (*p < end) {
|
||||
lws_mbedtls_x509_subject_alternative_name dnb;
|
||||
memset(&dnb, 0, sizeof(dnb));
|
||||
|
||||
tag = **p;
|
||||
(*p)++;
|
||||
|
||||
r = mbedtls_asn1_get_len(p, end, &tag_len);
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
/* Tag shall be CONTEXT_SPECIFIC or SET */
|
||||
if ((tag & LWS_MBEDTLS_ASN1_TAG_CLASS_MASK) !=
|
||||
MBEDTLS_ASN1_CONTEXT_SPECIFIC &&
|
||||
(tag & (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
|
||||
(MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE))
|
||||
return 1;
|
||||
|
||||
/*
|
||||
* Check that the name is structured correctly.
|
||||
*/
|
||||
r = lws_mbedtls_x509_parse_general_name(
|
||||
&cur->MBEDTLS_PRIVATE_V30_ONLY(buf), &dnb);
|
||||
/*
|
||||
* In case the extension is malformed, return an error,
|
||||
* and clear the allocated sequences.
|
||||
*/
|
||||
if (r && r != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
|
||||
mbedtls_x509_sequence *seq_cur = name->MBEDTLS_PRIVATE_V30_ONLY(next);
|
||||
mbedtls_x509_sequence *seq_prv;
|
||||
|
||||
while( seq_cur != NULL ) {
|
||||
seq_prv = seq_cur;
|
||||
seq_cur = seq_cur->MBEDTLS_PRIVATE_V30_ONLY(next);
|
||||
lws_explicit_bzero(seq_prv, sizeof(*seq_cur));
|
||||
lws_free(seq_prv);
|
||||
}
|
||||
|
||||
name->MBEDTLS_PRIVATE_V30_ONLY(next) = NULL;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/* Allocate and assign next pointer */
|
||||
if (cur->MBEDTLS_PRIVATE_V30_ONLY(buf).MBEDTLS_PRIVATE_V30_ONLY(p)) {
|
||||
if (cur->MBEDTLS_PRIVATE_V30_ONLY(next))
|
||||
return 1;
|
||||
|
||||
cur->MBEDTLS_PRIVATE_V30_ONLY(next) =
|
||||
lws_zalloc(sizeof(*cur), __func__);
|
||||
|
||||
if (!cur->MBEDTLS_PRIVATE_V30_ONLY(next))
|
||||
return 1;
|
||||
|
||||
cur = cur->MBEDTLS_PRIVATE_V30_ONLY(next);
|
||||
}
|
||||
|
||||
buf = &(cur->MBEDTLS_PRIVATE_V30_ONLY(buf));
|
||||
buf->MBEDTLS_PRIVATE_V30_ONLY(tag) = tag;
|
||||
buf->MBEDTLS_PRIVATE_V30_ONLY(p) = *p;
|
||||
buf->MBEDTLS_PRIVATE_V30_ONLY(len) = tag_len;
|
||||
|
||||
*p += buf->MBEDTLS_PRIVATE_V30_ONLY(len);
|
||||
}
|
||||
|
||||
/* Set final sequence entry's next pointer to NULL */
|
||||
cur->MBEDTLS_PRIVATE_V30_ONLY(next) = NULL;
|
||||
|
||||
return *p != end;
|
||||
}
|
||||
|
||||
static int
|
||||
x509_get_akid(uint8_t **p, uint8_t *end, lws_mbedtls_x509_authority *akid)
|
||||
{
|
||||
size_t len = 0u;
|
||||
int r;
|
||||
|
||||
r = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
|
||||
MBEDTLS_ASN1_SEQUENCE);
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
r = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONTEXT_SPECIFIC);
|
||||
if (!r) {
|
||||
akid->keyIdentifier.MBEDTLS_PRIVATE_V30_ONLY(len) = len;
|
||||
akid->keyIdentifier.MBEDTLS_PRIVATE_V30_ONLY(p) = *p;
|
||||
akid->keyIdentifier.MBEDTLS_PRIVATE_V30_ONLY(tag) = MBEDTLS_ASN1_OCTET_STRING;
|
||||
|
||||
*p += len;
|
||||
}
|
||||
|
||||
if (*p < end) {
|
||||
/* Getting authorityCertIssuer using the required specific
|
||||
* class tag [1] */
|
||||
r = mbedtls_asn1_get_tag(p, end, &len,
|
||||
MBEDTLS_ASN1_CONTEXT_SPECIFIC |
|
||||
MBEDTLS_ASN1_CONSTRUCTED | 1 );
|
||||
if (!r) {
|
||||
/* Getting directoryName using the required specific
|
||||
* class tag [4] */
|
||||
r = mbedtls_asn1_get_tag(p, end, &len,
|
||||
MBEDTLS_ASN1_CONTEXT_SPECIFIC |
|
||||
MBEDTLS_ASN1_CONSTRUCTED | 4);
|
||||
if (r)
|
||||
return(r);
|
||||
|
||||
/* "end" also includes the CertSerialNumber field
|
||||
* so "len" shall be used */
|
||||
r = lws_x509_get_general_names(p, (*p + len),
|
||||
&akid->authorityCertIssuer);
|
||||
}
|
||||
}
|
||||
|
||||
if (*p < end) {
|
||||
r = mbedtls_asn1_get_tag(p, end, &len,
|
||||
MBEDTLS_ASN1_CONTEXT_SPECIFIC |
|
||||
MBEDTLS_ASN1_INTEGER );
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
akid->authorityCertSerialNumber.MBEDTLS_PRIVATE_V30_ONLY(len) = len;
|
||||
akid->authorityCertSerialNumber.MBEDTLS_PRIVATE_V30_ONLY(p) = *p;
|
||||
akid->authorityCertSerialNumber.MBEDTLS_PRIVATE_V30_ONLY(tag) = MBEDTLS_ASN1_OCTET_STRING;
|
||||
*p += len;
|
||||
}
|
||||
|
||||
return *p != end;
|
||||
}
|
||||
|
||||
/*
|
||||
* Work around lack of this in mbedtls... we don't need to do sanity checks
|
||||
* sanity checks because they will be done at x509 validation time
|
||||
*/
|
||||
|
||||
int
|
||||
lws_x509_get_crt_ext(mbedtls_x509_crt *crt, mbedtls_x509_buf *skid,
|
||||
lws_mbedtls_x509_authority *akid)
|
||||
{
|
||||
uint8_t *p = crt->MBEDTLS_PRIVATE_V30_ONLY(v3_ext).MBEDTLS_PRIVATE_V30_ONLY(p),
|
||||
*end_ext_data, *end_ext_octet;
|
||||
const uint8_t *end = p + crt->MBEDTLS_PRIVATE_V30_ONLY(v3_ext).MBEDTLS_PRIVATE_V30_ONLY(len);
|
||||
size_t len;
|
||||
int r = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
|
||||
MBEDTLS_ASN1_SEQUENCE);
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
while (p < end) {
|
||||
mbedtls_x509_buf extn_oid = { 0, 0, NULL };
|
||||
int is_critical = 0; /* DEFAULT FALSE */
|
||||
int ext_type = 0;
|
||||
|
||||
r = mbedtls_asn1_get_tag(&p, end, &len,
|
||||
MBEDTLS_ASN1_CONSTRUCTED |
|
||||
MBEDTLS_ASN1_SEQUENCE);
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
end_ext_data = p + len;
|
||||
|
||||
/* Get extension ID */
|
||||
r = mbedtls_asn1_get_tag(&p, end_ext_data, &extn_oid.MBEDTLS_PRIVATE_V30_ONLY(len),
|
||||
MBEDTLS_ASN1_OID);
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
extn_oid.MBEDTLS_PRIVATE_V30_ONLY(tag) = MBEDTLS_ASN1_OID;
|
||||
extn_oid.MBEDTLS_PRIVATE_V30_ONLY(p) = p;
|
||||
p += extn_oid.MBEDTLS_PRIVATE_V30_ONLY(len);
|
||||
|
||||
/* Get optional critical */
|
||||
r = mbedtls_asn1_get_bool(&p, end_ext_data, &is_critical);
|
||||
if (r && r != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
|
||||
return r;
|
||||
|
||||
/* Data should be octet string type */
|
||||
r = mbedtls_asn1_get_tag(&p, end_ext_data, &len,
|
||||
MBEDTLS_ASN1_OCTET_STRING);
|
||||
if (r)
|
||||
return r;
|
||||
|
||||
end_ext_octet = p + len;
|
||||
|
||||
if (end_ext_octet != end_ext_data)
|
||||
return 1;
|
||||
|
||||
r = lws_mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
|
||||
if (r) {
|
||||
p = end_ext_octet;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (ext_type) {
|
||||
case LWS_MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
|
||||
/* Parse subject key identifier */
|
||||
r = x509_get_skid(&p, end_ext_data, skid);
|
||||
if (r)
|
||||
return r;
|
||||
break;
|
||||
|
||||
case LWS_MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
|
||||
/* Parse authority key identifier */
|
||||
r = x509_get_akid(&p, end_ext_octet, akid);
|
||||
if (r)
|
||||
return r;
|
||||
break;
|
||||
|
||||
default:
|
||||
p = end_ext_octet;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
703
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-server.c
Normal file
703
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-server.c
Normal file
@ -0,0 +1,703 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include <mbedtls/x509_csr.h>
|
||||
#include <errno.h>
|
||||
|
||||
int
|
||||
lws_tls_server_client_cert_verify_config(struct lws_vhost *vh)
|
||||
{
|
||||
int verify_options = SSL_VERIFY_PEER;
|
||||
|
||||
/* as a server, are we requiring clients to identify themselves? */
|
||||
if (!lws_check_opt(vh->options,
|
||||
LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT)) {
|
||||
lwsl_notice("no client cert required\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!lws_check_opt(vh->options, LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED))
|
||||
verify_options |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
|
||||
|
||||
lwsl_notice("%s: vh %s requires client cert %d\n", __func__, vh->name,
|
||||
verify_options);
|
||||
|
||||
SSL_CTX_set_verify(vh->tls.ssl_ctx, verify_options, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
lws_mbedtls_sni_cb(void *arg, mbedtls_ssl_context *mbedtls_ctx,
|
||||
const unsigned char *servername, size_t len)
|
||||
{
|
||||
SSL *ssl = SSL_SSL_from_mbedtls_ssl_context(mbedtls_ctx);
|
||||
struct lws_context *context = (struct lws_context *)arg;
|
||||
struct lws_vhost *vhost, *vh;
|
||||
|
||||
lwsl_notice("%s: %s\n", __func__, servername);
|
||||
|
||||
/*
|
||||
* We can only get ssl accepted connections by using a vhost's ssl_ctx
|
||||
* find out which listening one took us and only match vhosts on the
|
||||
* same port.
|
||||
*/
|
||||
vh = context->vhost_list;
|
||||
while (vh) {
|
||||
if (!vh->being_destroyed &&
|
||||
vh->tls.ssl_ctx == SSL_get_SSL_CTX(ssl))
|
||||
break;
|
||||
vh = vh->vhost_next;
|
||||
}
|
||||
|
||||
if (!vh) {
|
||||
assert(vh); /* can't match the incoming vh? */
|
||||
return 0;
|
||||
}
|
||||
|
||||
vhost = lws_select_vhost(context, vh->listen_port,
|
||||
(const char *)servername);
|
||||
if (!vhost) {
|
||||
lwsl_info("SNI: none: %s:%d\n", servername, vh->listen_port);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
lwsl_info("SNI: Found: %s:%d at vhost '%s'\n", servername,
|
||||
vh->listen_port, vhost->name);
|
||||
|
||||
if (!vhost->tls.ssl_ctx) {
|
||||
lwsl_err("%s: vhost %s matches SNI but no valid cert\n",
|
||||
__func__, vh->name);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* select the ssl ctx from the selected vhost for this conn */
|
||||
SSL_set_SSL_CTX(ssl, vhost->tls.ssl_ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_server_certs_load(struct lws_vhost *vhost, struct lws *wsi,
|
||||
const char *cert, const char *private_key,
|
||||
const char *mem_cert, size_t mem_cert_len,
|
||||
const char *mem_privkey, size_t mem_privkey_len)
|
||||
{
|
||||
lws_filepos_t flen;
|
||||
uint8_t *p = NULL;
|
||||
long err;
|
||||
int n;
|
||||
|
||||
if ((!cert || !private_key) && (!mem_cert || !mem_privkey)) {
|
||||
lwsl_notice("%s: no usable input\n", __func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
n = (int)lws_tls_generic_cert_checks(vhost, cert, private_key);
|
||||
|
||||
if (n == LWS_TLS_EXTANT_NO && (!mem_cert || !mem_privkey))
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* we can't read the root-privs files. But if mem_cert is provided,
|
||||
* we should use that.
|
||||
*/
|
||||
if (n == LWS_TLS_EXTANT_NO)
|
||||
n = LWS_TLS_EXTANT_ALTERNATIVE;
|
||||
|
||||
if (n == LWS_TLS_EXTANT_ALTERNATIVE && (!mem_cert || !mem_privkey))
|
||||
return 1; /* no alternative */
|
||||
|
||||
if (n == LWS_TLS_EXTANT_ALTERNATIVE) {
|
||||
/*
|
||||
* Although we have prepared update certs, we no longer have
|
||||
* the rights to read our own cert + key we saved.
|
||||
*
|
||||
* If we were passed copies in memory buffers, use those
|
||||
* instead.
|
||||
*
|
||||
* The passed memory-buffer cert image is in DER, and the
|
||||
* memory-buffer private key image is PEM.
|
||||
*/
|
||||
cert = NULL;
|
||||
private_key = NULL;
|
||||
}
|
||||
if (lws_tls_alloc_pem_to_der_file(vhost->context, cert, mem_cert,
|
||||
mem_cert_len, &p, &flen)) {
|
||||
lwsl_err("couldn't load cert file %s\n", cert);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
err = SSL_CTX_use_certificate_ASN1(vhost->tls.ssl_ctx, (int)flen, p);
|
||||
lws_free_set_NULL(p);
|
||||
if (!err) {
|
||||
lwsl_err("Problem loading cert\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (lws_tls_alloc_pem_to_der_file(vhost->context, private_key,
|
||||
(char *)mem_privkey, mem_privkey_len,
|
||||
&p, &flen)) {
|
||||
lwsl_err("couldn't find private key\n");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
err = SSL_CTX_use_PrivateKey_ASN1(0, vhost->tls.ssl_ctx, p, (long)flen);
|
||||
lws_free_set_NULL(p);
|
||||
if (!err) {
|
||||
lwsl_err("Problem loading key\n");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
vhost->tls.skipped_certs = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_server_vhost_backend_init(const struct lws_context_creation_info *info,
|
||||
struct lws_vhost *vhost, struct lws *wsi)
|
||||
{
|
||||
const SSL_METHOD *method = TLS_server_method();
|
||||
uint8_t *p;
|
||||
lws_filepos_t flen;
|
||||
int n;
|
||||
|
||||
vhost->tls.ssl_ctx = SSL_CTX_new(method, &vhost->context->mcdc); /* create context */
|
||||
if (!vhost->tls.ssl_ctx) {
|
||||
lwsl_err("problem creating ssl context\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!vhost->tls.use_ssl ||
|
||||
(!info->ssl_cert_filepath && !info->server_ssl_cert_mem))
|
||||
return 0;
|
||||
|
||||
if (info->ssl_ca_filepath) {
|
||||
lwsl_notice("%s: vh %s: loading CA filepath %s\n", __func__,
|
||||
vhost->name, info->ssl_ca_filepath);
|
||||
if (lws_tls_alloc_pem_to_der_file(vhost->context,
|
||||
info->ssl_ca_filepath, NULL, 0, &p, &flen)) {
|
||||
lwsl_err("couldn't find client CA file %s\n",
|
||||
info->ssl_ca_filepath);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (SSL_CTX_add_client_CA_ASN1(vhost->tls.ssl_ctx, (int)flen, p) != 1) {
|
||||
lwsl_err("%s: SSL_CTX_add_client_CA_ASN1 unhappy\n",
|
||||
__func__);
|
||||
free(p);
|
||||
return 1;
|
||||
}
|
||||
free(p);
|
||||
} else {
|
||||
if (info->server_ssl_ca_mem && info->server_ssl_ca_mem_len &&
|
||||
SSL_CTX_add_client_CA_ASN1(vhost->tls.ssl_ctx,
|
||||
(int)info->server_ssl_ca_mem_len,
|
||||
info->server_ssl_ca_mem) != 1) {
|
||||
lwsl_err("%s: mem SSL_CTX_add_client_CA_ASN1 unhappy\n",
|
||||
__func__);
|
||||
return 1;
|
||||
}
|
||||
lwsl_notice("%s: vh %s: mem CA OK\n", __func__, vhost->name);
|
||||
}
|
||||
|
||||
n = lws_tls_server_certs_load(vhost, wsi, info->ssl_cert_filepath,
|
||||
info->ssl_private_key_filepath,
|
||||
info->server_ssl_cert_mem,
|
||||
info->server_ssl_cert_mem_len,
|
||||
info->server_ssl_private_key_mem,
|
||||
info->server_ssl_private_key_mem_len);
|
||||
if (n)
|
||||
return n;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_server_new_nonblocking(struct lws *wsi, lws_sockfd_type accept_fd)
|
||||
{
|
||||
errno = 0;
|
||||
wsi->tls.ssl = SSL_new(wsi->a.vhost->tls.ssl_ctx);
|
||||
if (wsi->tls.ssl == NULL) {
|
||||
lwsl_err("SSL_new failed: errno %d\n", errno);
|
||||
|
||||
lws_tls_err_describe_clear();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SSL_set_fd(wsi->tls.ssl, (int)accept_fd);
|
||||
|
||||
if (wsi->a.vhost->tls.ssl_info_event_mask)
|
||||
SSL_set_info_callback(wsi->tls.ssl, lws_ssl_info_callback);
|
||||
|
||||
SSL_set_sni_callback(wsi->tls.ssl, lws_mbedtls_sni_cb, wsi->a.context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum lws_ssl_capable_status
|
||||
lws_tls_server_abort_connection(struct lws *wsi)
|
||||
{
|
||||
if (wsi->tls.use_ssl)
|
||||
__lws_tls_shutdown(wsi);
|
||||
|
||||
SSL_free(wsi->tls.ssl);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum lws_ssl_capable_status
|
||||
lws_tls_server_accept(struct lws *wsi)
|
||||
{
|
||||
union lws_tls_cert_info_results ir;
|
||||
int m, n;
|
||||
|
||||
n = SSL_accept(wsi->tls.ssl);
|
||||
|
||||
wsi->skip_fallback = 1;
|
||||
if (n == 1) {
|
||||
|
||||
if (strstr(wsi->a.vhost->name, ".invalid")) {
|
||||
lwsl_notice("%s: vhost has .invalid, "
|
||||
"rejecting accept\n", __func__);
|
||||
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
|
||||
n = lws_tls_peer_cert_info(wsi, LWS_TLS_CERT_INFO_COMMON_NAME,
|
||||
&ir, sizeof(ir.ns.name));
|
||||
if (!n)
|
||||
lwsl_notice("%s: client cert CN '%s'\n",
|
||||
__func__, ir.ns.name);
|
||||
else
|
||||
lwsl_info("%s: couldn't get client cert CN\n",
|
||||
__func__);
|
||||
return LWS_SSL_CAPABLE_DONE;
|
||||
}
|
||||
|
||||
m = SSL_get_error(wsi->tls.ssl, n);
|
||||
lwsl_debug("%s: %s: accept SSL_get_error %d errno %d\n", __func__,
|
||||
lws_wsi_tag(wsi), m, errno);
|
||||
|
||||
// mbedtls wrapper only
|
||||
if (m == SSL_ERROR_SYSCALL && errno == 11)
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
||||
|
||||
#if defined(__APPLE__)
|
||||
if (m == SSL_ERROR_SYSCALL && errno == 35)
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
||||
#endif
|
||||
|
||||
#if defined(WIN32)
|
||||
if (m == SSL_ERROR_SYSCALL && errno == 0)
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
||||
#endif
|
||||
|
||||
if (m == SSL_ERROR_SYSCALL || m == SSL_ERROR_SSL)
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
|
||||
if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl)) {
|
||||
if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
|
||||
lwsl_info("%s: WANT_READ change_pollfd failed\n",
|
||||
__func__);
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
|
||||
lwsl_info("SSL_ERROR_WANT_READ\n");
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
||||
}
|
||||
if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl)) {
|
||||
lwsl_debug("%s: WANT_WRITE\n", __func__);
|
||||
|
||||
if (lws_change_pollfd(wsi, 0, LWS_POLLOUT)) {
|
||||
lwsl_info("%s: WANT_WRITE change_pollfd failed\n",
|
||||
__func__);
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE;
|
||||
}
|
||||
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_ACME)
|
||||
/*
|
||||
* mbedtls doesn't support SAN for cert creation. So we use a known-good
|
||||
* tls-sni-01 cert from OpenSSL that worked on Let's Encrypt, and just replace
|
||||
* the pubkey n part and the signature part.
|
||||
*
|
||||
* This will need redoing for tls-sni-02...
|
||||
*/
|
||||
|
||||
static uint8_t ss_cert_leadin[] = {
|
||||
0x30, 0x82,
|
||||
0x05, 0x56, /* total length: LEN1 (+2 / +3) (correct for 513 + 512)*/
|
||||
|
||||
0x30, 0x82, /* length: LEN2 (+6 / +7) (correct for 513) */
|
||||
0x03, 0x3e,
|
||||
|
||||
/* addition: v3 cert (+5 bytes)*/
|
||||
0xa0, 0x03,
|
||||
0x02, 0x01, 0x02,
|
||||
|
||||
0x02, 0x01, 0x01,
|
||||
0x30, 0x0d, 0x06, 0x09, 0x2a,
|
||||
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x3f,
|
||||
0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x47,
|
||||
0x42, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0b,
|
||||
0x73, 0x6f, 0x6d, 0x65, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e, 0x79, 0x31,
|
||||
0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x11, 0x74, 0x65,
|
||||
0x6d, 0x70, 0x2e, 0x61, 0x63, 0x6d, 0x65, 0x2e, 0x69, 0x6e, 0x76, 0x61,
|
||||
0x6c, 0x69, 0x64, 0x30, 0x1e, 0x17, 0x0d,
|
||||
|
||||
/* from 2017-10-29 ... */
|
||||
0x31, 0x37, 0x31, 0x30, 0x32, 0x39, 0x31, 0x31, 0x34, 0x39, 0x34, 0x35,
|
||||
0x5a, 0x17, 0x0d,
|
||||
|
||||
/* thru 2049-10-29 we immediately discard the private key, no worries */
|
||||
0x34, 0x39, 0x31, 0x30, 0x32, 0x39, 0x31, 0x32, 0x34, 0x39, 0x34, 0x35,
|
||||
0x5a,
|
||||
|
||||
0x30, 0x3f, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
|
||||
0x02, 0x47, 0x42, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a,
|
||||
0x0c, 0x0b, 0x73, 0x6f, 0x6d, 0x65, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e,
|
||||
0x79, 0x31, 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x11,
|
||||
0x74, 0x65, 0x6d, 0x70, 0x2e, 0x61, 0x63, 0x6d, 0x65, 0x2e, 0x69, 0x6e,
|
||||
0x76, 0x61, 0x6c, 0x69, 0x64, 0x30,
|
||||
|
||||
0x82,
|
||||
0x02, 0x22, /* LEN3 (+C3 / C4) */
|
||||
0x30, 0x0d, 0x06,
|
||||
0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
|
||||
0x03,
|
||||
|
||||
0x82,
|
||||
0x02, 0x0f, /* LEN4 (+D6 / D7) */
|
||||
|
||||
0x00, 0x30, 0x82,
|
||||
|
||||
0x02, 0x0a, /* LEN5 (+ DB / DC) */
|
||||
|
||||
0x02, 0x82,
|
||||
|
||||
//0x02, 0x01, /* length of n in bytes (including leading 00 if any) */
|
||||
},
|
||||
|
||||
/* 1 + (keybits / 8) bytes N */
|
||||
|
||||
ss_cert_san_leadin[] = {
|
||||
/* e - fixed */
|
||||
0x02, 0x03, 0x01, 0x00, 0x01,
|
||||
|
||||
0xa3, 0x5d, 0x30, 0x5b, 0x30, 0x59, 0x06, 0x03, 0x55, 0x1d,
|
||||
0x11, 0x04, 0x52, 0x30, 0x50, /* <-- SAN length + 2 */
|
||||
|
||||
0x82, 0x4e, /* <-- SAN length */
|
||||
},
|
||||
|
||||
/* 78 bytes of SAN (tls-sni-01)
|
||||
0x61, 0x64, 0x34, 0x31, 0x61, 0x66, 0x62, 0x65, 0x30, 0x63, 0x61, 0x34,
|
||||
0x36, 0x34, 0x32, 0x66, 0x30, 0x61, 0x34, 0x34, 0x39, 0x64, 0x39, 0x63,
|
||||
0x61, 0x37, 0x36, 0x65, 0x62, 0x61, 0x61, 0x62, 0x2e, 0x32, 0x38, 0x39,
|
||||
0x34, 0x64, 0x34, 0x31, 0x36, 0x63, 0x39, 0x38, 0x33, 0x66, 0x31, 0x32,
|
||||
0x65, 0x64, 0x37, 0x33, 0x31, 0x61, 0x33, 0x30, 0x66, 0x35, 0x63, 0x34,
|
||||
0x34, 0x37, 0x37, 0x66, 0x65, 0x2e, 0x61, 0x63, 0x6d, 0x65, 0x2e, 0x69,
|
||||
0x6e, 0x76, 0x61, 0x6c, 0x69, 0x64, */
|
||||
|
||||
/* end of LEN2 area */
|
||||
|
||||
ss_cert_sig_leadin[] = {
|
||||
/* it's saying that the signature is SHA256 + RSA */
|
||||
0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
|
||||
0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
|
||||
|
||||
0x82,
|
||||
0x02, 0x01,
|
||||
0x00,
|
||||
};
|
||||
|
||||
/* (keybits / 8) bytes signature to end of LEN1 area */
|
||||
|
||||
#define SAN_A_LENGTH 78
|
||||
|
||||
int
|
||||
lws_tls_acme_sni_cert_create(struct lws_vhost *vhost, const char *san_a,
|
||||
const char *san_b)
|
||||
{
|
||||
int buflen = 0x560;
|
||||
uint8_t *buf = lws_malloc((unsigned int)buflen, "tmp cert buf"), *p = buf, *pkey_asn1;
|
||||
struct lws_genrsa_ctx ctx;
|
||||
struct lws_gencrypto_keyelem el[LWS_GENCRYPTO_RSA_KEYEL_COUNT];
|
||||
uint8_t digest[32];
|
||||
struct lws_genhash_ctx hash_ctx;
|
||||
int pkey_asn1_len = 3 * 1024;
|
||||
int n, m, keybits = lws_plat_recommended_rsa_bits(), adj;
|
||||
|
||||
if (!buf)
|
||||
return 1;
|
||||
|
||||
n = lws_genrsa_new_keypair(vhost->context, &ctx, LGRSAM_PKCS1_1_5,
|
||||
&el[0], keybits);
|
||||
if (n < 0) {
|
||||
lws_genrsa_destroy_elements(&el[0]);
|
||||
goto bail1;
|
||||
}
|
||||
|
||||
n = sizeof(ss_cert_leadin);
|
||||
memcpy(p, ss_cert_leadin, (unsigned int)n);
|
||||
p += n;
|
||||
|
||||
adj = (0x0556 - 0x401) + (keybits / 4) + 1;
|
||||
buf[2] = (uint8_t)(adj >> 8);
|
||||
buf[3] = (uint8_t)(adj & 0xff);
|
||||
|
||||
adj = (0x033e - 0x201) + (keybits / 8) + 1;
|
||||
buf[6] = (uint8_t)(adj >> 8);
|
||||
buf[7] = (uint8_t)(adj & 0xff);
|
||||
|
||||
adj = (0x0222 - 0x201) + (keybits / 8) + 1;
|
||||
buf[0xc3] = (uint8_t)(adj >> 8);
|
||||
buf[0xc4] = (uint8_t)(adj & 0xff);
|
||||
|
||||
adj = (0x020f - 0x201) + (keybits / 8) + 1;
|
||||
buf[0xd6] = (uint8_t)(adj >> 8);
|
||||
buf[0xd7] = (uint8_t)(adj & 0xff);
|
||||
|
||||
adj = (0x020a - 0x201) + (keybits / 8) + 1;
|
||||
buf[0xdb] = (uint8_t)(adj >> 8);
|
||||
buf[0xdc] = (uint8_t)(adj & 0xff);
|
||||
|
||||
*p++ = (uint8_t)(((keybits / 8) + 1) >> 8);
|
||||
*p++ = (uint8_t)(((keybits / 8) + 1) & 0xff);
|
||||
|
||||
/* we need to drop 1 + (keybits / 8) bytes of n in here, 00 + key */
|
||||
|
||||
*p++ = 0x00;
|
||||
memcpy(p, el[LWS_GENCRYPTO_RSA_KEYEL_N].buf, el[LWS_GENCRYPTO_RSA_KEYEL_N].len);
|
||||
p += el[LWS_GENCRYPTO_RSA_KEYEL_N].len;
|
||||
|
||||
memcpy(p, ss_cert_san_leadin, sizeof(ss_cert_san_leadin));
|
||||
p += sizeof(ss_cert_san_leadin);
|
||||
|
||||
/* drop in 78 bytes of san_a */
|
||||
|
||||
memcpy(p, san_a, SAN_A_LENGTH);
|
||||
p += SAN_A_LENGTH;
|
||||
memcpy(p, ss_cert_sig_leadin, sizeof(ss_cert_sig_leadin));
|
||||
|
||||
p[17] = (uint8_t)(((keybits / 8) + 1) >> 8);
|
||||
p[18] = (uint8_t)(((keybits / 8) + 1) & 0xff);
|
||||
|
||||
p += sizeof(ss_cert_sig_leadin);
|
||||
|
||||
/* hash the cert plaintext */
|
||||
|
||||
if (lws_genhash_init(&hash_ctx, LWS_GENHASH_TYPE_SHA256))
|
||||
goto bail2;
|
||||
|
||||
if (lws_genhash_update(&hash_ctx, buf, lws_ptr_diff_size_t(p, buf))) {
|
||||
lws_genhash_destroy(&hash_ctx, NULL);
|
||||
|
||||
goto bail2;
|
||||
}
|
||||
if (lws_genhash_destroy(&hash_ctx, digest))
|
||||
goto bail2;
|
||||
|
||||
/* sign the hash */
|
||||
|
||||
n = lws_genrsa_hash_sign(&ctx, digest, LWS_GENHASH_TYPE_SHA256, p,
|
||||
(size_t)((size_t)buflen - lws_ptr_diff_size_t(p, buf)));
|
||||
if (n < 0)
|
||||
goto bail2;
|
||||
p += n;
|
||||
|
||||
pkey_asn1 = lws_malloc((unsigned int)pkey_asn1_len, "mbed crt tmp");
|
||||
if (!pkey_asn1)
|
||||
goto bail2;
|
||||
|
||||
m = lws_genrsa_render_pkey_asn1(&ctx, 1, pkey_asn1, (size_t)pkey_asn1_len);
|
||||
if (m < 0) {
|
||||
lws_free(pkey_asn1);
|
||||
goto bail2;
|
||||
}
|
||||
|
||||
// lwsl_hexdump_level(LLL_DEBUG, buf, lws_ptr_diff(p, buf));
|
||||
n = SSL_CTX_use_certificate_ASN1(vhost->tls.ssl_ctx,
|
||||
lws_ptr_diff(p, buf), buf);
|
||||
if (n != 1) {
|
||||
lws_free(pkey_asn1);
|
||||
lwsl_err("%s: generated cert failed to load 0x%x\n",
|
||||
__func__, -n);
|
||||
} else {
|
||||
//lwsl_debug("private key\n");
|
||||
//lwsl_hexdump_level(LLL_DEBUG, pkey_asn1, n);
|
||||
|
||||
/* and to use our generated private key */
|
||||
n = SSL_CTX_use_PrivateKey_ASN1(0, vhost->tls.ssl_ctx,
|
||||
pkey_asn1, m);
|
||||
lws_free(pkey_asn1);
|
||||
if (n != 1) {
|
||||
lwsl_err("%s: SSL_CTX_use_PrivateKey_ASN1 failed\n",
|
||||
__func__);
|
||||
}
|
||||
}
|
||||
|
||||
lws_genrsa_destroy(&ctx);
|
||||
lws_genrsa_destroy_elements(&el[0]);
|
||||
|
||||
lws_free(buf);
|
||||
|
||||
return n != 1;
|
||||
|
||||
bail2:
|
||||
lws_genrsa_destroy(&ctx);
|
||||
lws_genrsa_destroy_elements(&el[0]);
|
||||
bail1:
|
||||
lws_free(buf);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
lws_tls_acme_sni_cert_destroy(struct lws_vhost *vhost)
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_JOSE)
|
||||
static int
|
||||
_rngf(void *context, unsigned char *buf, size_t len)
|
||||
{
|
||||
if ((size_t)lws_get_random(context, buf, len) == len)
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static const char *x5[] = { "C", "ST", "L", "O", "CN" };
|
||||
|
||||
/*
|
||||
* CSR is output formatted as b64url(DER)
|
||||
* Private key is output as a PEM in memory
|
||||
*/
|
||||
int
|
||||
lws_tls_acme_sni_csr_create(struct lws_context *context, const char *elements[],
|
||||
uint8_t *dcsr, size_t csr_len, char **privkey_pem,
|
||||
size_t *privkey_len)
|
||||
{
|
||||
mbedtls_x509write_csr csr;
|
||||
mbedtls_pk_context mpk;
|
||||
int buf_size = 4096, n;
|
||||
char subject[200], *p = subject, *end = p + sizeof(subject) - 1;
|
||||
uint8_t *buf = malloc((unsigned int)buf_size); /* malloc because given to user code */
|
||||
|
||||
if (!buf)
|
||||
return -1;
|
||||
|
||||
mbedtls_x509write_csr_init(&csr);
|
||||
|
||||
mbedtls_pk_init(&mpk);
|
||||
if (mbedtls_pk_setup(&mpk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA))) {
|
||||
lwsl_notice("%s: pk_setup failed\n", __func__);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
n = mbedtls_rsa_gen_key(mbedtls_pk_rsa(mpk), _rngf, context,
|
||||
(unsigned int)lws_plat_recommended_rsa_bits(), 65537);
|
||||
if (n) {
|
||||
lwsl_notice("%s: failed to generate keys\n", __func__);
|
||||
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
/* subject must be formatted like "C=TW,O=warmcat,CN=myserver" */
|
||||
|
||||
for (n = 0; n < (int)LWS_ARRAY_SIZE(x5); n++) {
|
||||
if (p != subject)
|
||||
*p++ = ',';
|
||||
if (elements[n])
|
||||
p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "%s=%s", x5[n],
|
||||
elements[n]);
|
||||
}
|
||||
|
||||
if (mbedtls_x509write_csr_set_subject_name(&csr, subject))
|
||||
goto fail1;
|
||||
|
||||
mbedtls_x509write_csr_set_key(&csr, &mpk);
|
||||
mbedtls_x509write_csr_set_md_alg(&csr, MBEDTLS_MD_SHA256);
|
||||
|
||||
/*
|
||||
* data is written at the end of the buffer! Use the
|
||||
* return value to determine where you should start
|
||||
* using the buffer
|
||||
*/
|
||||
n = mbedtls_x509write_csr_der(&csr, buf, (size_t)buf_size, _rngf, context);
|
||||
if (n < 0) {
|
||||
lwsl_notice("%s: write csr der failed\n", __func__);
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
/* we have it in DER, we need it in b64URL */
|
||||
|
||||
n = lws_jws_base64_enc((char *)(buf + buf_size) - n, (size_t)n,
|
||||
(char *)dcsr, csr_len);
|
||||
if (n < 0)
|
||||
goto fail1;
|
||||
|
||||
/*
|
||||
* okay, the CSR is done, last we need the private key in PEM
|
||||
* re-use the DER CSR buf as the result buffer since we cn do it in
|
||||
* one step
|
||||
*/
|
||||
|
||||
if (mbedtls_pk_write_key_pem(&mpk, buf, (size_t)buf_size)) {
|
||||
lwsl_notice("write key pem failed\n");
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
*privkey_pem = (char *)buf;
|
||||
*privkey_len = strlen((const char *)buf);
|
||||
|
||||
mbedtls_pk_free(&mpk);
|
||||
mbedtls_x509write_csr_free(&csr);
|
||||
|
||||
return n;
|
||||
|
||||
fail1:
|
||||
mbedtls_pk_free(&mpk);
|
||||
fail:
|
||||
mbedtls_x509write_csr_free(&csr);
|
||||
free(buf);
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
#endif
|
320
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-session.c
Normal file
320
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-session.c
Normal file
@ -0,0 +1,320 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
|
||||
typedef struct lws_tls_session_cache_mbedtls {
|
||||
lws_dll2_t list;
|
||||
|
||||
mbedtls_ssl_session session;
|
||||
lws_sorted_usec_list_t sul_ttl;
|
||||
|
||||
/* name is overallocated here */
|
||||
} lws_tls_scm_t;
|
||||
|
||||
#define lwsl_tlssess lwsl_info
|
||||
|
||||
|
||||
|
||||
static void
|
||||
__lws_tls_session_destroy(lws_tls_scm_t *ts)
|
||||
{
|
||||
lwsl_tlssess("%s: %s (%u)\n", __func__, (const char *)&ts[1],
|
||||
(unsigned int)(ts->list.owner->count - 1));
|
||||
|
||||
lws_sul_cancel(&ts->sul_ttl);
|
||||
mbedtls_ssl_session_free(&ts->session);
|
||||
lws_dll2_remove(&ts->list); /* vh lock */
|
||||
|
||||
lws_free(ts);
|
||||
}
|
||||
|
||||
static lws_tls_scm_t *
|
||||
__lws_tls_session_lookup_by_name(struct lws_vhost *vh, const char *name)
|
||||
{
|
||||
lws_start_foreach_dll(struct lws_dll2 *, p,
|
||||
lws_dll2_get_head(&vh->tls_sessions)) {
|
||||
lws_tls_scm_t *ts = lws_container_of(p, lws_tls_scm_t, list);
|
||||
const char *ts_name = (const char *)&ts[1];
|
||||
|
||||
if (!strcmp(name, ts_name))
|
||||
return ts;
|
||||
|
||||
} lws_end_foreach_dll(p);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* If possible, reuse an existing, cached session
|
||||
*/
|
||||
|
||||
void
|
||||
lws_tls_reuse_session(struct lws *wsi)
|
||||
{
|
||||
char buf[LWS_SESSION_TAG_LEN];
|
||||
mbedtls_ssl_context *msc;
|
||||
lws_tls_scm_t *ts;
|
||||
|
||||
if (!wsi->a.vhost ||
|
||||
wsi->a.vhost->options & LWS_SERVER_OPTION_DISABLE_TLS_SESSION_CACHE)
|
||||
return;
|
||||
|
||||
lws_context_lock(wsi->a.context, __func__); /* -------------- cx { */
|
||||
lws_vhost_lock(wsi->a.vhost); /* -------------- vh { */
|
||||
|
||||
if (lws_tls_session_tag_from_wsi(wsi, buf, sizeof(buf)))
|
||||
goto bail;
|
||||
|
||||
ts = __lws_tls_session_lookup_by_name(wsi->a.vhost, buf);
|
||||
|
||||
if (!ts) {
|
||||
lwsl_tlssess("%s: no existing session for %s\n", __func__, buf);
|
||||
goto bail;
|
||||
}
|
||||
|
||||
lwsl_tlssess("%s: %s\n", __func__, (const char *)&ts[1]);
|
||||
wsi->tls_session_reused = 1;
|
||||
|
||||
msc = SSL_mbedtls_ssl_context_from_SSL(wsi->tls.ssl);
|
||||
mbedtls_ssl_set_session(msc, &ts->session);
|
||||
|
||||
/* keep our session list sorted in lru -> mru order */
|
||||
|
||||
lws_dll2_remove(&ts->list);
|
||||
lws_dll2_add_tail(&ts->list, &wsi->a.vhost->tls_sessions);
|
||||
|
||||
bail:
|
||||
lws_vhost_unlock(wsi->a.vhost); /* } vh -------------- */
|
||||
lws_context_unlock(wsi->a.context); /* } cx -------------- */
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_session_is_reused(struct lws *wsi)
|
||||
{
|
||||
#if defined(LWS_WITH_CLIENT)
|
||||
struct lws *nwsi = lws_get_network_wsi(wsi);
|
||||
|
||||
if (!nwsi)
|
||||
return 0;
|
||||
|
||||
return nwsi->tls_session_reused;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int
|
||||
lws_tls_session_destroy_dll(struct lws_dll2 *d, void *user)
|
||||
{
|
||||
lws_tls_scm_t *ts = lws_container_of(d, lws_tls_scm_t, list);
|
||||
|
||||
__lws_tls_session_destroy(ts);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
lws_tls_session_vh_destroy(struct lws_vhost *vh)
|
||||
{
|
||||
lws_dll2_foreach_safe(&vh->tls_sessions, NULL,
|
||||
lws_tls_session_destroy_dll);
|
||||
}
|
||||
|
||||
static void
|
||||
lws_tls_session_expiry_cb(lws_sorted_usec_list_t *sul)
|
||||
{
|
||||
lws_tls_scm_t *ts = lws_container_of(sul, lws_tls_scm_t, sul_ttl);
|
||||
struct lws_vhost *vh = lws_container_of(ts->list.owner,
|
||||
struct lws_vhost, tls_sessions);
|
||||
|
||||
lws_context_lock(vh->context, __func__); /* -------------- cx { */
|
||||
lws_vhost_lock(vh); /* -------------- vh { */
|
||||
__lws_tls_session_destroy(ts);
|
||||
lws_vhost_unlock(vh); /* } vh -------------- */
|
||||
lws_context_unlock(vh->context); /* } cx -------------- */
|
||||
}
|
||||
|
||||
/*
|
||||
* Called after SSL_accept on the wsi
|
||||
*/
|
||||
|
||||
int
|
||||
lws_tls_session_new_mbedtls(struct lws *wsi)
|
||||
{
|
||||
char buf[LWS_SESSION_TAG_LEN];
|
||||
mbedtls_ssl_context *msc;
|
||||
struct lws_vhost *vh;
|
||||
lws_tls_scm_t *ts;
|
||||
size_t nl;
|
||||
#if !defined(LWS_WITH_NO_LOGS) && defined(_DEBUG)
|
||||
const char *disposition = "reuse";
|
||||
#endif
|
||||
|
||||
vh = wsi->a.vhost;
|
||||
if (vh->options & LWS_SERVER_OPTION_DISABLE_TLS_SESSION_CACHE)
|
||||
return 0;
|
||||
|
||||
if (lws_tls_session_tag_from_wsi(wsi, buf, sizeof(buf)))
|
||||
return 0;
|
||||
|
||||
nl = strlen(buf);
|
||||
|
||||
msc = SSL_mbedtls_ssl_context_from_SSL(wsi->tls.ssl);
|
||||
|
||||
lws_context_lock(vh->context, __func__); /* -------------- cx { */
|
||||
lws_vhost_lock(vh); /* -------------- vh { */
|
||||
|
||||
ts = __lws_tls_session_lookup_by_name(vh, buf);
|
||||
|
||||
if (!ts) {
|
||||
/*
|
||||
* We have to make our own, new session
|
||||
*/
|
||||
|
||||
if (vh->tls_sessions.count == vh->tls_session_cache_max) {
|
||||
|
||||
/*
|
||||
* We have reached the vhost's session cache limit,
|
||||
* prune the LRU / head
|
||||
*/
|
||||
ts = lws_container_of(vh->tls_sessions.head,
|
||||
lws_tls_scm_t, list);
|
||||
|
||||
lwsl_tlssess("%s: pruning oldest session (hit max %u)\n",
|
||||
__func__,
|
||||
(unsigned int)vh->tls_session_cache_max);
|
||||
|
||||
lws_vhost_lock(vh); /* -------------- vh { */
|
||||
__lws_tls_session_destroy(ts);
|
||||
lws_vhost_unlock(vh); /* } vh -------------- */
|
||||
}
|
||||
|
||||
ts = lws_malloc(sizeof(*ts) + nl + 1, __func__);
|
||||
|
||||
if (!ts)
|
||||
goto bail;
|
||||
|
||||
memset(ts, 0, sizeof(*ts));
|
||||
memcpy(&ts[1], buf, nl + 1);
|
||||
|
||||
if (mbedtls_ssl_get_session(msc, &ts->session)) {
|
||||
lws_free(ts);
|
||||
/* no joy for whatever reason */
|
||||
goto bail;
|
||||
}
|
||||
|
||||
lws_dll2_add_tail(&ts->list, &vh->tls_sessions);
|
||||
|
||||
lws_sul_schedule(wsi->a.context, wsi->tsi, &ts->sul_ttl,
|
||||
lws_tls_session_expiry_cb,
|
||||
(int64_t)vh->tls.tls_session_cache_ttl *
|
||||
LWS_US_PER_SEC);
|
||||
|
||||
#if !defined(LWS_WITH_NO_LOGS) && defined(_DEBUG)
|
||||
disposition = "new";
|
||||
#endif
|
||||
} else {
|
||||
|
||||
mbedtls_ssl_session_free(&ts->session);
|
||||
|
||||
if (mbedtls_ssl_get_session(msc, &ts->session))
|
||||
/* no joy for whatever reason */
|
||||
goto bail;
|
||||
|
||||
/* keep our session list sorted in lru -> mru order */
|
||||
|
||||
lws_dll2_remove(&ts->list);
|
||||
lws_dll2_add_tail(&ts->list, &vh->tls_sessions);
|
||||
}
|
||||
|
||||
lws_vhost_unlock(vh); /* } vh -------------- */
|
||||
lws_context_unlock(vh->context); /* } cx -------------- */
|
||||
|
||||
lwsl_tlssess("%s: %s: %s %s, (%s:%u)\n", __func__,
|
||||
wsi->lc.gutag, disposition, buf, vh->name,
|
||||
(unsigned int)vh->tls_sessions.count);
|
||||
|
||||
/*
|
||||
* indicate we will hold on to the SSL_SESSION reference, and take
|
||||
* responsibility to call SSL_SESSION_free() on it ourselves
|
||||
*/
|
||||
|
||||
return 1;
|
||||
|
||||
bail:
|
||||
lws_vhost_unlock(vh); /* } vh -------------- */
|
||||
lws_context_unlock(vh->context); /* } cx -------------- */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(LWS_TLS_SYNTHESIZE_CB)
|
||||
|
||||
/*
|
||||
* On openssl, there is an async cb coming when the server issues the session
|
||||
* information on the link, so we can pick it up and update the cache at the
|
||||
* right time.
|
||||
*
|
||||
* On mbedtls and some version at least of borning ssl, this cb is either not
|
||||
* part of the tls library apis or fails to arrive.
|
||||
*/
|
||||
|
||||
void
|
||||
lws_sess_cache_synth_cb(lws_sorted_usec_list_t *sul)
|
||||
{
|
||||
struct lws_lws_tls *tls = lws_container_of(sul, struct lws_lws_tls,
|
||||
sul_cb_synth);
|
||||
struct lws *wsi = lws_container_of(tls, struct lws, tls);
|
||||
|
||||
lws_tls_session_new_mbedtls(wsi);
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
lws_tls_session_cache(struct lws_vhost *vh, uint32_t ttl)
|
||||
{
|
||||
/* Default to 1hr max recommendation from RFC5246 F.1.4 */
|
||||
vh->tls.tls_session_cache_ttl = !ttl ? 3600 : ttl;
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_session_dump_save(struct lws_vhost *vh, const char *host, uint16_t port,
|
||||
lws_tls_sess_cb_t cb_save, void *opq)
|
||||
{
|
||||
/* there seems no serialization / deserialization helper in mbedtls */
|
||||
lwsl_warn("%s: only supported on openssl atm\n", __func__);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_session_dump_load(struct lws_vhost *vh, const char *host, uint16_t port,
|
||||
lws_tls_sess_cb_t cb_load, void *opq)
|
||||
{
|
||||
/* there seems no serialization / deserialization helper in mbedtls */
|
||||
lwsl_warn("%s: only supported on openssl atm\n", __func__);
|
||||
|
||||
return 1;
|
||||
}
|
356
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-ssl.c
Normal file
356
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-ssl.c
Normal file
@ -0,0 +1,356 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-tls-mbedtls.h"
|
||||
|
||||
void
|
||||
lws_ssl_destroy(struct lws_vhost *vhost)
|
||||
{
|
||||
if (!lws_check_opt(vhost->context->options,
|
||||
LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
|
||||
return;
|
||||
|
||||
if (vhost->tls.ssl_ctx)
|
||||
SSL_CTX_free(vhost->tls.ssl_ctx);
|
||||
if (!vhost->tls.user_supplied_ssl_ctx && vhost->tls.ssl_client_ctx)
|
||||
SSL_CTX_free(vhost->tls.ssl_client_ctx);
|
||||
|
||||
if (vhost->tls.x509_client_CA)
|
||||
X509_free(vhost->tls.x509_client_CA);
|
||||
}
|
||||
|
||||
int
|
||||
lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, size_t len)
|
||||
{
|
||||
struct lws_context *context = wsi->a.context;
|
||||
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
||||
int n = 0, m;
|
||||
|
||||
if (!wsi->tls.ssl)
|
||||
return lws_ssl_capable_read_no_ssl(wsi, buf, len);
|
||||
|
||||
errno = 0;
|
||||
n = SSL_read(wsi->tls.ssl, buf, (int)len);
|
||||
#if defined(LWS_PLAT_FREERTOS)
|
||||
if (!n && errno == LWS_ENOTCONN) {
|
||||
lwsl_debug("%s: SSL_read ENOTCONN\n", lws_wsi_tag(wsi));
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
lwsl_debug("%s: %s: SSL_read says %d\n", __func__, lws_wsi_tag(wsi), n);
|
||||
/* manpage: returning 0 means connection shut down */
|
||||
if (!n) {
|
||||
wsi->socket_is_permanently_unusable = 1;
|
||||
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
|
||||
if (n < 0) {
|
||||
m = SSL_get_error(wsi->tls.ssl, n);
|
||||
lwsl_debug("%s: %s: ssl err %d errno %d\n", __func__, lws_wsi_tag(wsi), m, errno);
|
||||
if (errno == LWS_ENOTCONN)
|
||||
/* If the socket isn't connected anymore, bail out. */
|
||||
goto do_err1;
|
||||
|
||||
#if defined(LWS_PLAT_FREERTOS)
|
||||
if (errno == LWS_ECONNABORTED)
|
||||
goto do_err1;
|
||||
#endif
|
||||
|
||||
if (m == SSL_ERROR_ZERO_RETURN ||
|
||||
m == SSL_ERROR_SYSCALL)
|
||||
goto do_err;
|
||||
|
||||
if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl)) {
|
||||
lwsl_debug("%s: WANT_READ\n", __func__);
|
||||
lwsl_debug("%s: LWS_SSL_CAPABLE_MORE_SERVICE\n", lws_wsi_tag(wsi));
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
||||
}
|
||||
if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl)) {
|
||||
lwsl_info("%s: WANT_WRITE\n", __func__);
|
||||
lwsl_debug("%s: LWS_SSL_CAPABLE_MORE_SERVICE\n", lws_wsi_tag(wsi));
|
||||
wsi->tls_read_wanted_write = 1;
|
||||
lws_callback_on_writable(wsi);
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
||||
}
|
||||
|
||||
do_err1:
|
||||
wsi->socket_is_permanently_unusable = 1;
|
||||
|
||||
do_err:
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
if (wsi->a.vhost)
|
||||
lws_metric_event(wsi->a.vhost->mt_traffic_rx, METRES_NOGO, 0);
|
||||
#endif
|
||||
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
|
||||
#if defined(LWS_TLS_LOG_PLAINTEXT_RX)
|
||||
/*
|
||||
* If using mbedtls type tls library, this is the earliest point for all
|
||||
* paths to dump what was received as decrypted data from the tls tunnel
|
||||
*/
|
||||
lwsl_notice("%s: len %d\n", __func__, n);
|
||||
lwsl_hexdump_notice(buf, (size_t)n);
|
||||
#endif
|
||||
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
if (wsi->a.vhost)
|
||||
lws_metric_event(wsi->a.vhost->mt_traffic_rx,
|
||||
METRES_GO /* rx */, (u_mt_t)n);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* if it was our buffer that limited what we read,
|
||||
* check if SSL has additional data pending inside SSL buffers.
|
||||
*
|
||||
* Because these won't signal at the network layer with POLLIN
|
||||
* and if we don't realize, this data will sit there forever
|
||||
*/
|
||||
if (n != (int)len)
|
||||
goto bail;
|
||||
if (!wsi->tls.ssl)
|
||||
goto bail;
|
||||
|
||||
if (SSL_pending(wsi->tls.ssl)) {
|
||||
if (lws_dll2_is_detached(&wsi->tls.dll_pending_tls))
|
||||
lws_dll2_add_head(&wsi->tls.dll_pending_tls,
|
||||
&pt->tls.dll_pending_tls_owner);
|
||||
} else
|
||||
__lws_ssl_remove_wsi_from_buffered_list(wsi);
|
||||
|
||||
return n;
|
||||
bail:
|
||||
lws_ssl_remove_wsi_from_buffered_list(wsi);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int
|
||||
lws_ssl_pending(struct lws *wsi)
|
||||
{
|
||||
if (!wsi->tls.ssl)
|
||||
return 0;
|
||||
|
||||
return SSL_pending(wsi->tls.ssl);
|
||||
}
|
||||
|
||||
int
|
||||
lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, size_t len)
|
||||
{
|
||||
int n, m;
|
||||
|
||||
#if defined(LWS_TLS_LOG_PLAINTEXT_TX)
|
||||
/*
|
||||
* If using mbedtls type tls library, this is the last point for all
|
||||
* paths before sending data into the tls tunnel, where you can dump it
|
||||
* and see what is being sent.
|
||||
*/
|
||||
lwsl_notice("%s: len %d\n", __func__, (int)len);
|
||||
lwsl_hexdump_notice(buf, len);
|
||||
#endif
|
||||
|
||||
if (!wsi->tls.ssl)
|
||||
return lws_ssl_capable_write_no_ssl(wsi, buf, len);
|
||||
|
||||
n = SSL_write(wsi->tls.ssl, buf, (int)len);
|
||||
if (n > 0) {
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
if (wsi->a.vhost)
|
||||
lws_metric_event(wsi->a.vhost->mt_traffic_tx,
|
||||
METRES_GO, (u_mt_t)n);
|
||||
#endif
|
||||
return n;
|
||||
}
|
||||
|
||||
m = SSL_get_error(wsi->tls.ssl, n);
|
||||
if (m != SSL_ERROR_SYSCALL) {
|
||||
if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl)) {
|
||||
lwsl_notice("%s: want read\n", __func__);
|
||||
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
||||
}
|
||||
|
||||
if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl)) {
|
||||
lws_set_blocking_send(wsi);
|
||||
lwsl_debug("%s: want write\n", __func__);
|
||||
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
||||
}
|
||||
}
|
||||
|
||||
lwsl_debug("%s failed: %d\n",__func__, m);
|
||||
wsi->socket_is_permanently_unusable = 1;
|
||||
|
||||
#if defined(LWS_WITH_SYS_METRICS)
|
||||
if (wsi->a.vhost)
|
||||
lws_metric_event(wsi->a.vhost->mt_traffic_tx,
|
||||
METRES_NOGO, (u_mt_t)n);
|
||||
#endif
|
||||
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
|
||||
int openssl_SSL_CTX_private_data_index;
|
||||
|
||||
void
|
||||
lws_ssl_info_callback(const SSL *ssl, int where, int ret)
|
||||
{
|
||||
struct lws *wsi;
|
||||
struct lws_context *context;
|
||||
struct lws_ssl_info si;
|
||||
|
||||
context = (struct lws_context *)SSL_CTX_get_ex_data(
|
||||
SSL_get_SSL_CTX(ssl),
|
||||
openssl_SSL_CTX_private_data_index);
|
||||
if (!context)
|
||||
return;
|
||||
wsi = wsi_from_fd(context, SSL_get_fd(ssl));
|
||||
if (!wsi)
|
||||
return;
|
||||
|
||||
if (!(where & wsi->a.vhost->tls.ssl_info_event_mask))
|
||||
return;
|
||||
|
||||
si.where = where;
|
||||
si.ret = ret;
|
||||
|
||||
if (user_callback_handle_rxflow(wsi->a.protocol->callback,
|
||||
wsi, LWS_CALLBACK_SSL_INFO,
|
||||
wsi->user_space, &si, 0))
|
||||
lws_set_timeout(wsi, PENDING_TIMEOUT_KILLED_BY_SSL_INFO, -1);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lws_ssl_close(struct lws *wsi)
|
||||
{
|
||||
lws_sockfd_type n;
|
||||
|
||||
if (!wsi->tls.ssl)
|
||||
return 0; /* not handled */
|
||||
|
||||
#if defined (LWS_HAVE_SSL_SET_INFO_CALLBACK)
|
||||
/* kill ssl callbacks, becausse we will remove the fd from the
|
||||
* table linking it to the wsi
|
||||
*/
|
||||
if (wsi->a.vhost->tls.ssl_info_event_mask)
|
||||
SSL_set_info_callback(wsi->tls.ssl, NULL);
|
||||
#endif
|
||||
|
||||
#if defined(LWS_TLS_SYNTHESIZE_CB)
|
||||
lws_sul_cancel(&wsi->tls.sul_cb_synth);
|
||||
/*
|
||||
* ... check the session in case it did not live long enough to get
|
||||
* the scheduled callback to sample it
|
||||
*/
|
||||
lws_sess_cache_synth_cb(&wsi->tls.sul_cb_synth);
|
||||
#endif
|
||||
|
||||
n = SSL_get_fd(wsi->tls.ssl);
|
||||
if (!wsi->socket_is_permanently_unusable)
|
||||
SSL_shutdown(wsi->tls.ssl);
|
||||
compatible_close(n);
|
||||
SSL_free(wsi->tls.ssl);
|
||||
wsi->tls.ssl = NULL;
|
||||
|
||||
lws_tls_restrict_return(wsi);
|
||||
|
||||
return 1; /* handled */
|
||||
}
|
||||
|
||||
void
|
||||
lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost)
|
||||
{
|
||||
if (vhost->tls.ssl_ctx)
|
||||
SSL_CTX_free(vhost->tls.ssl_ctx);
|
||||
|
||||
if (!vhost->tls.user_supplied_ssl_ctx && vhost->tls.ssl_client_ctx)
|
||||
SSL_CTX_free(vhost->tls.ssl_client_ctx);
|
||||
#if defined(LWS_WITH_ACME)
|
||||
lws_tls_acme_sni_cert_destroy(vhost);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
lws_ssl_context_destroy(struct lws_context *context)
|
||||
{
|
||||
}
|
||||
|
||||
lws_tls_ctx *
|
||||
lws_tls_ctx_from_wsi(struct lws *wsi)
|
||||
{
|
||||
if (!wsi->tls.ssl)
|
||||
return NULL;
|
||||
|
||||
return SSL_get_SSL_CTX(wsi->tls.ssl);
|
||||
}
|
||||
|
||||
enum lws_ssl_capable_status
|
||||
__lws_tls_shutdown(struct lws *wsi)
|
||||
{
|
||||
int n = SSL_shutdown(wsi->tls.ssl);
|
||||
|
||||
lwsl_debug("SSL_shutdown=%d for fd %d\n", n, wsi->desc.sockfd);
|
||||
|
||||
switch (n) {
|
||||
case 1: /* successful completion */
|
||||
(void)shutdown(wsi->desc.sockfd, SHUT_WR);
|
||||
return LWS_SSL_CAPABLE_DONE;
|
||||
|
||||
case 0: /* needs a retry */
|
||||
__lws_change_pollfd(wsi, 0, LWS_POLLIN);
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
||||
|
||||
default: /* fatal error, or WANT */
|
||||
n = SSL_get_error(wsi->tls.ssl, n);
|
||||
if (n != SSL_ERROR_SYSCALL && n != SSL_ERROR_SSL) {
|
||||
if (SSL_want_read(wsi->tls.ssl)) {
|
||||
lwsl_debug("(wants read)\n");
|
||||
__lws_change_pollfd(wsi, 0, LWS_POLLIN);
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
||||
}
|
||||
if (SSL_want_write(wsi->tls.ssl)) {
|
||||
lwsl_debug("(wants write)\n");
|
||||
__lws_change_pollfd(wsi, 0, LWS_POLLOUT);
|
||||
return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE;
|
||||
}
|
||||
}
|
||||
return LWS_SSL_CAPABLE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
tops_fake_POLLIN_for_buffered_mbedtls(struct lws_context_per_thread *pt)
|
||||
{
|
||||
return lws_tls_fake_POLLIN_for_buffered(pt);
|
||||
}
|
||||
|
||||
const struct lws_tls_ops tls_ops_mbedtls = {
|
||||
/* fake_POLLIN_for_buffered */ tops_fake_POLLIN_for_buffered_mbedtls,
|
||||
};
|
50
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-tls.c
Normal file
50
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-tls.c
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-tls-mbedtls.h"
|
||||
|
||||
void
|
||||
lws_tls_err_describe_clear(void)
|
||||
{
|
||||
}
|
||||
|
||||
int
|
||||
lws_context_init_ssl_library(struct lws_context *cx,
|
||||
const struct lws_context_creation_info *info)
|
||||
{
|
||||
lwsl_info(" Compiled with MbedTLS support");
|
||||
|
||||
if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
|
||||
lwsl_info(" SSL disabled: no "
|
||||
"LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
lws_context_deinit_ssl_library(struct lws_context *context)
|
||||
{
|
||||
|
||||
}
|
539
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-x509.c
Normal file
539
Kinc/Sources/kinc/libs/tls/mbedtls/mbedtls-x509.c
Normal file
@ -0,0 +1,539 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-tls-mbedtls.h"
|
||||
#include <mbedtls/oid.h>
|
||||
|
||||
#if defined(LWS_PLAT_OPTEE) || defined(OPTEE_DEV_KIT)
|
||||
struct tm {
|
||||
int tm_sec; // seconds [0,61]
|
||||
int tm_min; // minutes [0,59]
|
||||
int tm_hour; // hour [0,23]
|
||||
int tm_mday; // day of month [1,31]
|
||||
int tm_mon; // month of year [0,11]
|
||||
int tm_year; // years since 1900
|
||||
int tm_wday; // day of week [0,6] (Sunday = 0)
|
||||
int tm_yday; // day of year [0,365]
|
||||
int tm_isdst; // daylight savings flag
|
||||
};
|
||||
time_t mktime(struct tm *t)
|
||||
{
|
||||
return (time_t)0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static time_t
|
||||
lws_tls_mbedtls_time_to_unix(mbedtls_x509_time *xtime)
|
||||
{
|
||||
struct tm t;
|
||||
|
||||
if (!xtime || !xtime->MBEDTLS_PRIVATE_V30_ONLY(year) || xtime->MBEDTLS_PRIVATE_V30_ONLY(year) < 0)
|
||||
return (time_t)(long long)-1;
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
|
||||
t.tm_year = xtime->MBEDTLS_PRIVATE_V30_ONLY(year) - 1900;
|
||||
t.tm_mon = xtime->MBEDTLS_PRIVATE_V30_ONLY(mon) - 1; /* mbedtls months are 1+, tm are 0+ */
|
||||
t.tm_mday = xtime->MBEDTLS_PRIVATE_V30_ONLY(day) - 1; /* mbedtls days are 1+, tm are 0+ */
|
||||
t.tm_hour = xtime->MBEDTLS_PRIVATE_V30_ONLY(hour);
|
||||
t.tm_min = xtime->MBEDTLS_PRIVATE_V30_ONLY(min);
|
||||
t.tm_sec = xtime->MBEDTLS_PRIVATE_V30_ONLY(sec);
|
||||
t.tm_isdst = -1;
|
||||
|
||||
return mktime(&t);
|
||||
}
|
||||
|
||||
static int
|
||||
lws_tls_mbedtls_get_x509_name(mbedtls_x509_name *name,
|
||||
union lws_tls_cert_info_results *buf, size_t len)
|
||||
{
|
||||
int r = -1;
|
||||
|
||||
buf->ns.len = 0;
|
||||
|
||||
while (name) {
|
||||
/*
|
||||
if (MBEDTLS_OID_CMP(type, &name->oid)) {
|
||||
name = name->next;
|
||||
continue;
|
||||
}
|
||||
*/
|
||||
lws_strnncpy(&buf->ns.name[buf->ns.len],
|
||||
(const char *)name->MBEDTLS_PRIVATE_V30_ONLY(val).MBEDTLS_PRIVATE_V30_ONLY(p),
|
||||
name->MBEDTLS_PRIVATE_V30_ONLY(val).MBEDTLS_PRIVATE_V30_ONLY(len),
|
||||
len - (size_t)buf->ns.len);
|
||||
buf->ns.len = (int)strlen(buf->ns.name);
|
||||
|
||||
r = 0;
|
||||
name = name->MBEDTLS_PRIVATE_V30_ONLY(next);
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lws_tls_mbedtls_cert_info(mbedtls_x509_crt *x509, enum lws_tls_cert_info type,
|
||||
union lws_tls_cert_info_results *buf, size_t len)
|
||||
{
|
||||
mbedtls_x509_buf skid;
|
||||
lws_mbedtls_x509_authority akid;
|
||||
|
||||
if (!x509)
|
||||
return -1;
|
||||
|
||||
if (!len)
|
||||
len = sizeof(buf->ns.name);
|
||||
|
||||
switch (type) {
|
||||
case LWS_TLS_CERT_INFO_VALIDITY_FROM:
|
||||
buf->time = lws_tls_mbedtls_time_to_unix(&x509->MBEDTLS_PRIVATE_V30_ONLY(valid_from));
|
||||
if (buf->time == (time_t)(long long)-1)
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case LWS_TLS_CERT_INFO_VALIDITY_TO:
|
||||
buf->time = lws_tls_mbedtls_time_to_unix(&x509->MBEDTLS_PRIVATE_V30_ONLY(valid_to));
|
||||
if (buf->time == (time_t)(long long)-1)
|
||||
return -1;
|
||||
break;
|
||||
|
||||
case LWS_TLS_CERT_INFO_COMMON_NAME:
|
||||
return lws_tls_mbedtls_get_x509_name(&x509->MBEDTLS_PRIVATE_V30_ONLY(subject), buf, len);
|
||||
|
||||
case LWS_TLS_CERT_INFO_ISSUER_NAME:
|
||||
return lws_tls_mbedtls_get_x509_name(&x509->MBEDTLS_PRIVATE_V30_ONLY(issuer), buf, len);
|
||||
|
||||
case LWS_TLS_CERT_INFO_USAGE:
|
||||
buf->usage = x509->MBEDTLS_PRIVATE(key_usage);
|
||||
break;
|
||||
|
||||
case LWS_TLS_CERT_INFO_OPAQUE_PUBLIC_KEY:
|
||||
{
|
||||
char *p = buf->ns.name;
|
||||
size_t r = len, u;
|
||||
|
||||
switch (mbedtls_pk_get_type(&x509->MBEDTLS_PRIVATE_V30_ONLY(pk))) {
|
||||
case MBEDTLS_PK_RSA:
|
||||
{
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa(x509->MBEDTLS_PRIVATE_V30_ONLY(pk));
|
||||
|
||||
if (mbedtls_mpi_write_string(&rsa->MBEDTLS_PRIVATE(N), 16, p, r, &u))
|
||||
return -1;
|
||||
r -= u;
|
||||
p += u;
|
||||
if (mbedtls_mpi_write_string(&rsa->MBEDTLS_PRIVATE(E), 16, p, r, &u))
|
||||
return -1;
|
||||
|
||||
p += u;
|
||||
buf->ns.len = lws_ptr_diff(p, buf->ns.name);
|
||||
break;
|
||||
}
|
||||
case MBEDTLS_PK_ECKEY:
|
||||
{
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(x509->MBEDTLS_PRIVATE_V30_ONLY(pk));
|
||||
|
||||
if (mbedtls_mpi_write_string(&ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), 16, p, r, &u))
|
||||
return -1;
|
||||
r -= u;
|
||||
p += u;
|
||||
if (mbedtls_mpi_write_string(&ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), 16, p, r, &u))
|
||||
return -1;
|
||||
r -= u;
|
||||
p += u;
|
||||
if (mbedtls_mpi_write_string(&ecp->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 16, p, r, &u))
|
||||
return -1;
|
||||
p += u;
|
||||
buf->ns.len = lws_ptr_diff(p, buf->ns.name);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
lwsl_notice("%s: x509 has unsupported pubkey type %d\n",
|
||||
__func__,
|
||||
mbedtls_pk_get_type(&x509->MBEDTLS_PRIVATE_V30_ONLY(pk)));
|
||||
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LWS_TLS_CERT_INFO_DER_RAW:
|
||||
|
||||
buf->ns.len = (int)x509->MBEDTLS_PRIVATE_V30_ONLY(raw).MBEDTLS_PRIVATE_V30_ONLY(len);
|
||||
|
||||
if (len < x509->MBEDTLS_PRIVATE_V30_ONLY(raw).MBEDTLS_PRIVATE_V30_ONLY(len))
|
||||
/*
|
||||
* The buffer is too small and the attempt failed, but
|
||||
* the required object length is in buf->ns.len
|
||||
*/
|
||||
return -1;
|
||||
|
||||
memcpy(buf->ns.name, x509->MBEDTLS_PRIVATE_V30_ONLY(raw).MBEDTLS_PRIVATE_V30_ONLY(p),
|
||||
x509->MBEDTLS_PRIVATE_V30_ONLY(raw).MBEDTLS_PRIVATE_V30_ONLY(len));
|
||||
break;
|
||||
|
||||
case LWS_TLS_CERT_INFO_AUTHORITY_KEY_ID:
|
||||
|
||||
memset(&akid, 0, sizeof(akid));
|
||||
memset(&skid, 0, sizeof(skid));
|
||||
|
||||
lws_x509_get_crt_ext(x509, &skid, &akid);
|
||||
if (akid.keyIdentifier.MBEDTLS_PRIVATE_V30_ONLY(tag) != MBEDTLS_ASN1_OCTET_STRING)
|
||||
return 1;
|
||||
buf->ns.len = (int)akid.keyIdentifier.MBEDTLS_PRIVATE_V30_ONLY(len);
|
||||
if (!akid.keyIdentifier.MBEDTLS_PRIVATE_V30_ONLY(p) ||
|
||||
len < (size_t)buf->ns.len)
|
||||
return -1;
|
||||
memcpy(buf->ns.name, akid.keyIdentifier.MBEDTLS_PRIVATE_V30_ONLY(p), (size_t)buf->ns.len);
|
||||
break;
|
||||
|
||||
case LWS_TLS_CERT_INFO_AUTHORITY_KEY_ID_ISSUER: {
|
||||
mbedtls_x509_sequence * ip;
|
||||
|
||||
memset(&akid, 0, sizeof(akid));
|
||||
memset(&skid, 0, sizeof(skid));
|
||||
|
||||
lws_x509_get_crt_ext(x509, &skid, &akid);
|
||||
|
||||
ip = &akid.authorityCertIssuer;
|
||||
|
||||
buf->ns.len = 0;
|
||||
|
||||
while (ip) {
|
||||
if (akid.keyIdentifier.MBEDTLS_PRIVATE_V30_ONLY(tag) != MBEDTLS_ASN1_OCTET_STRING ||
|
||||
!ip->MBEDTLS_PRIVATE_V30_ONLY(buf).MBEDTLS_PRIVATE_V30_ONLY(p) ||
|
||||
ip->MBEDTLS_PRIVATE_V30_ONLY(buf).MBEDTLS_PRIVATE_V30_ONLY(len) < 9 ||
|
||||
len < (size_t)ip->MBEDTLS_PRIVATE_V30_ONLY(buf).MBEDTLS_PRIVATE_V30_ONLY(len) - 9u)
|
||||
break;
|
||||
|
||||
memcpy(buf->ns.name + buf->ns.len, ip->MBEDTLS_PRIVATE_V30_ONLY(buf).MBEDTLS_PRIVATE_V30_ONLY(p),
|
||||
(size_t)ip->MBEDTLS_PRIVATE_V30_ONLY(buf).MBEDTLS_PRIVATE_V30_ONLY(len) - 9);
|
||||
buf->ns.len = buf->ns.len + (int)ip->MBEDTLS_PRIVATE_V30_ONLY(buf).MBEDTLS_PRIVATE_V30_ONLY(len) - 9;
|
||||
|
||||
ip = ip->MBEDTLS_PRIVATE_V30_ONLY(next);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LWS_TLS_CERT_INFO_AUTHORITY_KEY_ID_SERIAL:
|
||||
|
||||
memset(&akid, 0, sizeof(akid));
|
||||
memset(&skid, 0, sizeof(skid));
|
||||
|
||||
lws_x509_get_crt_ext(x509, &skid, &akid);
|
||||
|
||||
if (akid.authorityCertSerialNumber.MBEDTLS_PRIVATE_V30_ONLY(tag) != MBEDTLS_ASN1_OCTET_STRING)
|
||||
return 1;
|
||||
buf->ns.len = (int)akid.authorityCertSerialNumber.MBEDTLS_PRIVATE_V30_ONLY(len);
|
||||
if (!akid.authorityCertSerialNumber.MBEDTLS_PRIVATE_V30_ONLY(p) ||
|
||||
len < (size_t)buf->ns.len)
|
||||
return -1;
|
||||
memcpy(buf->ns.name, akid.authorityCertSerialNumber.
|
||||
MBEDTLS_PRIVATE_V30_ONLY(p), (size_t)buf->ns.len);
|
||||
break;
|
||||
|
||||
case LWS_TLS_CERT_INFO_SUBJECT_KEY_ID:
|
||||
|
||||
memset(&akid, 0, sizeof(akid));
|
||||
memset(&skid, 0, sizeof(skid));
|
||||
|
||||
lws_x509_get_crt_ext(x509, &skid, &akid);
|
||||
|
||||
if (skid.MBEDTLS_PRIVATE_V30_ONLY(tag) != MBEDTLS_ASN1_OCTET_STRING)
|
||||
return 1;
|
||||
buf->ns.len = (int)skid.MBEDTLS_PRIVATE_V30_ONLY(len);
|
||||
if (len < (size_t)buf->ns.len)
|
||||
return -1;
|
||||
memcpy(buf->ns.name, skid.MBEDTLS_PRIVATE_V30_ONLY(p), (size_t)buf->ns.len);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_NETWORK)
|
||||
int
|
||||
lws_tls_vhost_cert_info(struct lws_vhost *vhost, enum lws_tls_cert_info type,
|
||||
union lws_tls_cert_info_results *buf, size_t len)
|
||||
{
|
||||
mbedtls_x509_crt *x509;
|
||||
|
||||
x509 = ssl_ctx_get_mbedtls_x509_crt(vhost->tls.ssl_ctx);
|
||||
|
||||
return lws_tls_mbedtls_cert_info(x509, type, buf, len);
|
||||
}
|
||||
|
||||
int
|
||||
lws_tls_peer_cert_info(struct lws *wsi, enum lws_tls_cert_info type,
|
||||
union lws_tls_cert_info_results *buf, size_t len)
|
||||
{
|
||||
mbedtls_x509_crt *x509;
|
||||
|
||||
wsi = lws_get_network_wsi(wsi);
|
||||
|
||||
x509 = ssl_get_peer_mbedtls_x509_crt(wsi->tls.ssl);
|
||||
|
||||
if (!x509)
|
||||
return -1;
|
||||
|
||||
switch (type) {
|
||||
case LWS_TLS_CERT_INFO_VERIFIED:
|
||||
buf->verified = SSL_get_verify_result(wsi->tls.ssl) == X509_V_OK;
|
||||
return 0;
|
||||
default:
|
||||
return lws_tls_mbedtls_cert_info(x509, type, buf, len);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
lws_x509_info(struct lws_x509_cert *x509, enum lws_tls_cert_info type,
|
||||
union lws_tls_cert_info_results *buf, size_t len)
|
||||
{
|
||||
return lws_tls_mbedtls_cert_info(&x509->cert, type, buf, len);
|
||||
}
|
||||
|
||||
int
|
||||
lws_x509_create(struct lws_x509_cert **x509)
|
||||
{
|
||||
*x509 = lws_malloc(sizeof(**x509), __func__);
|
||||
|
||||
return !(*x509);
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse one DER-encoded or one or more concatenated PEM-encoded certificates
|
||||
* and add them to the chained list.
|
||||
*/
|
||||
|
||||
int
|
||||
lws_x509_parse_from_pem(struct lws_x509_cert *x509, const void *pem, size_t len)
|
||||
{
|
||||
int ret;
|
||||
|
||||
mbedtls_x509_crt_init(&x509->cert);
|
||||
|
||||
ret = mbedtls_x509_crt_parse(&x509->cert, pem, len);
|
||||
if (ret) {
|
||||
if (ret > 0)
|
||||
mbedtls_x509_crt_free(&x509->cert);
|
||||
lwsl_err("%s: unable to parse PEM cert: -0x%x\n",
|
||||
__func__, -ret);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_x509_verify(struct lws_x509_cert *x509, struct lws_x509_cert *trusted,
|
||||
const char *common_name)
|
||||
{
|
||||
uint32_t flags = 0;
|
||||
int ret;
|
||||
|
||||
ret = mbedtls_x509_crt_verify_with_profile(&x509->cert, &trusted->cert,
|
||||
NULL,
|
||||
&mbedtls_x509_crt_profile_next,
|
||||
common_name, &flags, NULL,
|
||||
NULL);
|
||||
|
||||
if (ret) {
|
||||
lwsl_err("%s: unable to parse PEM cert: -0x%x\n",
|
||||
__func__, -ret);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_JOSE)
|
||||
|
||||
int
|
||||
lws_x509_public_to_jwk(struct lws_jwk *jwk, struct lws_x509_cert *x509,
|
||||
const char *curves, int rsa_min_bits)
|
||||
{
|
||||
int kt = (int)mbedtls_pk_get_type(&x509->cert.MBEDTLS_PRIVATE_V30_ONLY(pk)),
|
||||
n, count = 0, ret = -1;
|
||||
mbedtls_rsa_context *rsactx;
|
||||
mbedtls_ecp_keypair *ecpctx;
|
||||
mbedtls_mpi *mpi[LWS_GENCRYPTO_RSA_KEYEL_COUNT];
|
||||
|
||||
memset(jwk, 0, sizeof(*jwk));
|
||||
|
||||
switch (kt) {
|
||||
case MBEDTLS_PK_RSA:
|
||||
lwsl_notice("%s: RSA key\n", __func__);
|
||||
jwk->kty = LWS_GENCRYPTO_KTY_RSA;
|
||||
rsactx = mbedtls_pk_rsa(x509->cert.MBEDTLS_PRIVATE_V30_ONLY(pk));
|
||||
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_E] = &rsactx->MBEDTLS_PRIVATE(E);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_N] = &rsactx->MBEDTLS_PRIVATE(N);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_D] = &rsactx->MBEDTLS_PRIVATE(D);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_P] = &rsactx->MBEDTLS_PRIVATE(P);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_Q] = &rsactx->MBEDTLS_PRIVATE(Q);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_DP] = &rsactx->MBEDTLS_PRIVATE(DP);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_DQ] = &rsactx->MBEDTLS_PRIVATE(DQ);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_QI] = &rsactx->MBEDTLS_PRIVATE(QP);
|
||||
|
||||
count = LWS_GENCRYPTO_RSA_KEYEL_QI + 1;
|
||||
n = LWS_GENCRYPTO_RSA_KEYEL_E;
|
||||
break;
|
||||
|
||||
case MBEDTLS_PK_ECKEY:
|
||||
lwsl_notice("%s: EC key\n", __func__);
|
||||
jwk->kty = LWS_GENCRYPTO_KTY_EC;
|
||||
ecpctx = mbedtls_pk_ec(x509->cert.MBEDTLS_PRIVATE_V30_ONLY(pk));
|
||||
mpi[LWS_GENCRYPTO_EC_KEYEL_X] = &ecpctx->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X);
|
||||
mpi[LWS_GENCRYPTO_EC_KEYEL_D] = &ecpctx->MBEDTLS_PRIVATE(d);
|
||||
mpi[LWS_GENCRYPTO_EC_KEYEL_Y] = &ecpctx->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y);
|
||||
|
||||
if (lws_genec_confirm_curve_allowed_by_tls_id(curves,
|
||||
(int)ecpctx->MBEDTLS_PRIVATE(grp).id, jwk))
|
||||
/* already logged */
|
||||
goto bail;
|
||||
|
||||
count = LWS_GENCRYPTO_EC_KEYEL_COUNT;
|
||||
n = LWS_GENCRYPTO_EC_KEYEL_X;
|
||||
break;
|
||||
default:
|
||||
lwsl_err("%s: key type %d not supported\n", __func__, kt);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (; n < count; n++) {
|
||||
if (!mbedtls_mpi_size(mpi[n]))
|
||||
continue;
|
||||
|
||||
jwk->e[n].buf = lws_malloc(mbedtls_mpi_size(mpi[n]), "certjwk");
|
||||
if (!jwk->e[n].buf)
|
||||
goto bail;
|
||||
jwk->e[n].len = (uint32_t)mbedtls_mpi_size(mpi[n]);
|
||||
mbedtls_mpi_write_binary(mpi[n], jwk->e[n].buf, jwk->e[n].len);
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
bail:
|
||||
/* jwk destroy will clean up partials */
|
||||
if (ret)
|
||||
lws_jwk_destroy(jwk);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
lws_x509_jwk_privkey_pem(struct lws_context *cx, struct lws_jwk *jwk,
|
||||
void *pem, size_t len, const char *passphrase)
|
||||
{
|
||||
mbedtls_rsa_context *rsactx;
|
||||
mbedtls_ecp_keypair *ecpctx;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_mpi *mpi[LWS_GENCRYPTO_RSA_KEYEL_COUNT];
|
||||
int n, ret = -1, count = 0;
|
||||
|
||||
mbedtls_pk_init(&pk);
|
||||
|
||||
n = 0;
|
||||
if (passphrase)
|
||||
n = (int)strlen(passphrase);
|
||||
n = mbedtls_pk_parse_key(&pk, pem, len, (uint8_t *)passphrase, (unsigned int)n
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000
|
||||
, mbedtls_ctr_drbg_random, &cx->mcdc
|
||||
#endif
|
||||
);
|
||||
if (n) {
|
||||
lwsl_err("%s: parse PEM key failed: -0x%x\n", __func__, -n);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* the incoming private key type */
|
||||
switch (mbedtls_pk_get_type(&pk)) {
|
||||
case MBEDTLS_PK_RSA:
|
||||
if (jwk->kty != LWS_GENCRYPTO_KTY_RSA) {
|
||||
lwsl_err("%s: RSA privkey, non-RSA jwk\n", __func__);
|
||||
goto bail;
|
||||
}
|
||||
rsactx = mbedtls_pk_rsa(pk);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_D] = &rsactx->MBEDTLS_PRIVATE(D);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_P] = &rsactx->MBEDTLS_PRIVATE(P);
|
||||
mpi[LWS_GENCRYPTO_RSA_KEYEL_Q] = &rsactx->MBEDTLS_PRIVATE(Q);
|
||||
n = LWS_GENCRYPTO_RSA_KEYEL_D;
|
||||
count = LWS_GENCRYPTO_RSA_KEYEL_Q + 1;
|
||||
break;
|
||||
case MBEDTLS_PK_ECKEY:
|
||||
if (jwk->kty != LWS_GENCRYPTO_KTY_EC) {
|
||||
lwsl_err("%s: EC privkey, non-EC jwk\n", __func__);
|
||||
goto bail;
|
||||
}
|
||||
ecpctx = mbedtls_pk_ec(pk);
|
||||
mpi[LWS_GENCRYPTO_EC_KEYEL_D] = &ecpctx->MBEDTLS_PRIVATE(d);
|
||||
n = LWS_GENCRYPTO_EC_KEYEL_D;
|
||||
count = n + 1;
|
||||
break;
|
||||
default:
|
||||
lwsl_err("%s: unusable key type %d\n", __func__,
|
||||
mbedtls_pk_get_type(&pk));
|
||||
goto bail;
|
||||
}
|
||||
|
||||
for (; n < count; n++) {
|
||||
if (!mbedtls_mpi_size(mpi[n])) {
|
||||
lwsl_err("%s: empty privkey\n", __func__);
|
||||
goto bail;
|
||||
}
|
||||
|
||||
jwk->e[n].buf = lws_malloc(mbedtls_mpi_size(mpi[n]), "certjwk");
|
||||
if (!jwk->e[n].buf)
|
||||
goto bail;
|
||||
jwk->e[n].len = (uint32_t)mbedtls_mpi_size(mpi[n]);
|
||||
mbedtls_mpi_write_binary(mpi[n], jwk->e[n].buf, jwk->e[n].len);
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
bail:
|
||||
mbedtls_pk_free(&pk);
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
lws_x509_destroy(struct lws_x509_cert **x509)
|
||||
{
|
||||
if (!*x509)
|
||||
return;
|
||||
|
||||
mbedtls_x509_crt_free(&(*x509)->cert);
|
||||
|
||||
lws_free_set_NULL(*x509);
|
||||
}
|
65
Kinc/Sources/kinc/libs/tls/mbedtls/private-lib-tls-mbedtls.h
Normal file
65
Kinc/Sources/kinc/libs/tls/mbedtls/private-lib-tls-mbedtls.h
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* gencrypto mbedtls-specific helper declarations
|
||||
*/
|
||||
|
||||
#include <mbedtls/x509_crl.h>
|
||||
#include <errno.h>
|
||||
|
||||
struct lws_x509_cert {
|
||||
mbedtls_x509_crt cert; /* has a .next for linked-list / chain */
|
||||
};
|
||||
|
||||
typedef struct lws_mbedtls_x509_authority
|
||||
{
|
||||
mbedtls_x509_buf keyIdentifier;
|
||||
mbedtls_x509_sequence authorityCertIssuer;
|
||||
mbedtls_x509_buf authorityCertSerialNumber;
|
||||
mbedtls_x509_buf raw;
|
||||
}
|
||||
lws_mbedtls_x509_authority;
|
||||
|
||||
|
||||
mbedtls_md_type_t
|
||||
lws_gencrypto_mbedtls_hash_to_MD_TYPE(enum lws_genhash_types hash_type);
|
||||
|
||||
int
|
||||
lws_gencrypto_mbedtls_rngf(void *context, unsigned char *buf, size_t len);
|
||||
|
||||
int
|
||||
lws_tls_session_new_mbedtls(struct lws *wsi);
|
||||
|
||||
int
|
||||
lws_tls_mbedtls_cert_info(mbedtls_x509_crt *x509, enum lws_tls_cert_info type,
|
||||
union lws_tls_cert_info_results *buf, size_t len);
|
||||
|
||||
int
|
||||
lws_x509_get_crt_ext(mbedtls_x509_crt *crt, mbedtls_x509_buf *skid,
|
||||
lws_mbedtls_x509_authority *akid);
|
||||
|
||||
#if (MBEDTLS_VERSION_MAJOR == 3) && (MBEDTLS_VERSION_MINOR >= 5)
|
||||
int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_x509_name *cur);
|
||||
#endif
|
||||
|
@ -0,0 +1,44 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL3_H_
|
||||
#define _SSL3_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
# define SSL3_AD_CLOSE_NOTIFY 0
|
||||
# define SSL3_AD_UNEXPECTED_MESSAGE 10/* fatal */
|
||||
# define SSL3_AD_BAD_RECORD_MAC 20/* fatal */
|
||||
# define SSL3_AD_DECOMPRESSION_FAILURE 30/* fatal */
|
||||
# define SSL3_AD_HANDSHAKE_FAILURE 40/* fatal */
|
||||
# define SSL3_AD_NO_CERTIFICATE 41
|
||||
# define SSL3_AD_BAD_CERTIFICATE 42
|
||||
# define SSL3_AD_UNSUPPORTED_CERTIFICATE 43
|
||||
# define SSL3_AD_CERTIFICATE_REVOKED 44
|
||||
# define SSL3_AD_CERTIFICATE_EXPIRED 45
|
||||
# define SSL3_AD_CERTIFICATE_UNKNOWN 46
|
||||
# define SSL3_AD_ILLEGAL_PARAMETER 47/* fatal */
|
||||
|
||||
# define SSL3_AL_WARNING 1
|
||||
# define SSL3_AL_FATAL 2
|
||||
|
||||
#define SSL3_VERSION 0x0300
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,55 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_CERT_H_
|
||||
#define _SSL_CERT_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ssl_types.h"
|
||||
|
||||
/**
|
||||
* @brief create a certification object include private key object according to input certification
|
||||
*
|
||||
* @param ic - input certification point
|
||||
*
|
||||
* @return certification object point
|
||||
*/
|
||||
CERT *__ssl_cert_new(CERT *ic, void *rngctx);
|
||||
|
||||
/**
|
||||
* @brief create a certification object include private key object
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return certification object point
|
||||
*/
|
||||
CERT* ssl_cert_new(void *rngctx);
|
||||
|
||||
/**
|
||||
* @brief free a certification object
|
||||
*
|
||||
* @param cert - certification object point
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ssl_cert_free(CERT *cert);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,124 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_CODE_H_
|
||||
#define _SSL_CODE_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ssl3.h"
|
||||
#include "tls1.h"
|
||||
#include "x509_vfy.h"
|
||||
|
||||
/* Used in SSL_set_shutdown()/SSL_get_shutdown(); */
|
||||
# define SSL_SENT_SHUTDOWN 1
|
||||
# define SSL_RECEIVED_SHUTDOWN 2
|
||||
|
||||
# define SSL_VERIFY_NONE 0x00
|
||||
# define SSL_VERIFY_PEER 0x01
|
||||
# define SSL_VERIFY_FAIL_IF_NO_PEER_CERT 0x02
|
||||
# define SSL_VERIFY_CLIENT_ONCE 0x04
|
||||
|
||||
/*
|
||||
* The following 3 states are kept in ssl->rlayer.rstate when reads fail, you
|
||||
* should not need these
|
||||
*/
|
||||
# define SSL_ST_READ_HEADER 0xF0
|
||||
# define SSL_ST_READ_BODY 0xF1
|
||||
# define SSL_ST_READ_DONE 0xF2
|
||||
|
||||
# define SSL_NOTHING 1
|
||||
# define SSL_WRITING 2
|
||||
# define SSL_READING 3
|
||||
# define SSL_X509_LOOKUP 4
|
||||
# define SSL_ASYNC_PAUSED 5
|
||||
# define SSL_ASYNC_NO_JOBS 6
|
||||
|
||||
|
||||
# define SSL_ERROR_NONE 0
|
||||
# define SSL_ERROR_SSL 1
|
||||
# define SSL_ERROR_WANT_READ 2
|
||||
# define SSL_ERROR_WANT_WRITE 3
|
||||
# define SSL_ERROR_WANT_X509_LOOKUP 4
|
||||
# define SSL_ERROR_SYSCALL 5/* look at error stack/return value/errno */
|
||||
# define SSL_ERROR_ZERO_RETURN 6
|
||||
# define SSL_ERROR_WANT_CONNECT 7
|
||||
# define SSL_ERROR_WANT_ACCEPT 8
|
||||
# define SSL_ERROR_WANT_ASYNC 9
|
||||
# define SSL_ERROR_WANT_ASYNC_JOB 10
|
||||
|
||||
/* Message flow states */
|
||||
typedef enum {
|
||||
/* No handshake in progress */
|
||||
MSG_FLOW_UNINITED,
|
||||
/* A permanent error with this connection */
|
||||
MSG_FLOW_ERROR,
|
||||
/* We are about to renegotiate */
|
||||
MSG_FLOW_RENEGOTIATE,
|
||||
/* We are reading messages */
|
||||
MSG_FLOW_READING,
|
||||
/* We are writing messages */
|
||||
MSG_FLOW_WRITING,
|
||||
/* Handshake has finished */
|
||||
MSG_FLOW_FINISHED
|
||||
} MSG_FLOW_STATE;
|
||||
|
||||
/* SSL subsystem states */
|
||||
typedef enum {
|
||||
TLS_ST_BEFORE,
|
||||
TLS_ST_OK,
|
||||
DTLS_ST_CR_HELLO_VERIFY_REQUEST,
|
||||
TLS_ST_CR_SRVR_HELLO,
|
||||
TLS_ST_CR_CERT,
|
||||
TLS_ST_CR_CERT_STATUS,
|
||||
TLS_ST_CR_KEY_EXCH,
|
||||
TLS_ST_CR_CERT_REQ,
|
||||
TLS_ST_CR_SRVR_DONE,
|
||||
TLS_ST_CR_SESSION_TICKET,
|
||||
TLS_ST_CR_CHANGE,
|
||||
TLS_ST_CR_FINISHED,
|
||||
TLS_ST_CW_CLNT_HELLO,
|
||||
TLS_ST_CW_CERT,
|
||||
TLS_ST_CW_KEY_EXCH,
|
||||
TLS_ST_CW_CERT_VRFY,
|
||||
TLS_ST_CW_CHANGE,
|
||||
TLS_ST_CW_NEXT_PROTO,
|
||||
TLS_ST_CW_FINISHED,
|
||||
TLS_ST_SW_HELLO_REQ,
|
||||
TLS_ST_SR_CLNT_HELLO,
|
||||
DTLS_ST_SW_HELLO_VERIFY_REQUEST,
|
||||
TLS_ST_SW_SRVR_HELLO,
|
||||
TLS_ST_SW_CERT,
|
||||
TLS_ST_SW_KEY_EXCH,
|
||||
TLS_ST_SW_CERT_REQ,
|
||||
TLS_ST_SW_SRVR_DONE,
|
||||
TLS_ST_SR_CERT,
|
||||
TLS_ST_SR_KEY_EXCH,
|
||||
TLS_ST_SR_CERT_VRFY,
|
||||
TLS_ST_SR_NEXT_PROTO,
|
||||
TLS_ST_SR_CHANGE,
|
||||
TLS_ST_SR_FINISHED,
|
||||
TLS_ST_SW_SESSION_TICKET,
|
||||
TLS_ST_SW_CERT_STATUS,
|
||||
TLS_ST_SW_CHANGE,
|
||||
TLS_ST_SW_FINISHED
|
||||
} OSSL_HANDSHAKE_STATE;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,190 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_DEBUG_H_
|
||||
#define _SSL_DEBUG_H_
|
||||
|
||||
#include "ssl_port.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_OPENSSL_DEBUG_LEVEL
|
||||
#define SSL_DEBUG_LEVEL CONFIG_OPENSSL_DEBUG_LEVEL
|
||||
#else
|
||||
#define SSL_DEBUG_LEVEL 0
|
||||
#endif
|
||||
|
||||
#define SSL_DEBUG_ON (SSL_DEBUG_LEVEL + 1)
|
||||
#define SSL_DEBUG_OFF (SSL_DEBUG_LEVEL - 1)
|
||||
|
||||
#ifdef CONFIG_OPENSSL_DEBUG
|
||||
#ifndef SSL_DEBUG_LOG
|
||||
#error "SSL_DEBUG_LOG is not defined"
|
||||
#endif
|
||||
|
||||
#ifndef SSL_DEBUG_FL
|
||||
#define SSL_DEBUG_FL "\n"
|
||||
#endif
|
||||
|
||||
#define SSL_SHOW_LOCATION() \
|
||||
SSL_DEBUG_LOG("SSL assert : %s %d\n", \
|
||||
__FILE__, __LINE__)
|
||||
|
||||
#define SSL_DEBUG(level, fmt, ...) \
|
||||
{ \
|
||||
if (level > SSL_DEBUG_LEVEL) { \
|
||||
SSL_DEBUG_LOG(fmt SSL_DEBUG_FL, ##__VA_ARGS__); \
|
||||
} \
|
||||
}
|
||||
#else /* CONFIG_OPENSSL_DEBUG */
|
||||
#define SSL_SHOW_LOCATION()
|
||||
|
||||
#define SSL_DEBUG(level, fmt, ...)
|
||||
#endif /* CONFIG_OPENSSL_DEBUG */
|
||||
|
||||
/**
|
||||
* OpenSSL assert function
|
||||
*
|
||||
* if select "CONFIG_OPENSSL_ASSERT_DEBUG", SSL_ASSERT* will show error file name and line
|
||||
* if select "CONFIG_OPENSSL_ASSERT_EXIT", SSL_ASSERT* will just return error code.
|
||||
* if select "CONFIG_OPENSSL_ASSERT_DEBUG_EXIT" SSL_ASSERT* will show error file name and line,
|
||||
* then return error code.
|
||||
* if select "CONFIG_OPENSSL_ASSERT_DEBUG_BLOCK", SSL_ASSERT* will show error file name and line,
|
||||
* then block here with "while (1)"
|
||||
*
|
||||
* SSL_ASSERT1 may will return "-1", so function's return argument is integer.
|
||||
* SSL_ASSERT2 may will return "NULL", so function's return argument is a point.
|
||||
* SSL_ASSERT2 may will return nothing, so function's return argument is "void".
|
||||
*/
|
||||
#if defined(CONFIG_OPENSSL_ASSERT_DEBUG)
|
||||
#define SSL_ASSERT1(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
SSL_SHOW_LOCATION(); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SSL_ASSERT2(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
SSL_SHOW_LOCATION(); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SSL_ASSERT3(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
SSL_SHOW_LOCATION(); \
|
||||
} \
|
||||
}
|
||||
#elif defined(CONFIG_OPENSSL_ASSERT_EXIT)
|
||||
#define SSL_ASSERT1(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
return -1; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SSL_ASSERT2(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
return NULL; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SSL_ASSERT3(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
return ; \
|
||||
} \
|
||||
}
|
||||
#elif defined(CONFIG_OPENSSL_ASSERT_DEBUG_EXIT)
|
||||
#define SSL_ASSERT1(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
SSL_SHOW_LOCATION(); \
|
||||
return -1; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SSL_ASSERT2(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
SSL_SHOW_LOCATION(); \
|
||||
return NULL; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SSL_ASSERT3(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
SSL_SHOW_LOCATION(); \
|
||||
return ; \
|
||||
} \
|
||||
}
|
||||
#elif defined(CONFIG_OPENSSL_ASSERT_DEBUG_BLOCK)
|
||||
#define SSL_ASSERT1(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
SSL_SHOW_LOCATION(); \
|
||||
while (1); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SSL_ASSERT2(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
SSL_SHOW_LOCATION(); \
|
||||
while (1); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SSL_ASSERT3(s) \
|
||||
{ \
|
||||
if (!(s)) { \
|
||||
SSL_SHOW_LOCATION(); \
|
||||
while (1); \
|
||||
} \
|
||||
}
|
||||
#else
|
||||
#define SSL_ASSERT1(s)
|
||||
#define SSL_ASSERT2(s)
|
||||
#define SSL_ASSERT3(s)
|
||||
#endif
|
||||
|
||||
#define SSL_PLATFORM_DEBUG_LEVEL SSL_DEBUG_OFF
|
||||
#define SSL_PLATFORM_ERROR_LEVEL SSL_DEBUG_ON
|
||||
|
||||
#define SSL_CERT_DEBUG_LEVEL SSL_DEBUG_OFF
|
||||
#define SSL_CERT_ERROR_LEVEL SSL_DEBUG_ON
|
||||
|
||||
#define SSL_PKEY_DEBUG_LEVEL SSL_DEBUG_OFF
|
||||
#define SSL_PKEY_ERROR_LEVEL SSL_DEBUG_ON
|
||||
|
||||
#define SSL_X509_DEBUG_LEVEL SSL_DEBUG_OFF
|
||||
#define SSL_X509_ERROR_LEVEL SSL_DEBUG_ON
|
||||
|
||||
#define SSL_LIB_DEBUG_LEVEL SSL_DEBUG_OFF
|
||||
#define SSL_LIB_ERROR_LEVEL SSL_DEBUG_ON
|
||||
|
||||
#define SSL_STACK_DEBUG_LEVEL SSL_DEBUG_OFF
|
||||
#define SSL_STACK_ERROR_LEVEL SSL_DEBUG_ON
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,30 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_LIB_H_
|
||||
#define _SSL_LIB_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ssl_types.h"
|
||||
|
||||
void _ssl_set_alpn_list(const SSL *ssl);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,121 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_METHODS_H_
|
||||
#define _SSL_METHODS_H_
|
||||
|
||||
#include "ssl_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* TLS method function implement
|
||||
*/
|
||||
#define IMPLEMENT_TLS_METHOD_FUNC(func_name, \
|
||||
new, free, \
|
||||
handshake, shutdown, clear, \
|
||||
read, send, pending, \
|
||||
set_fd, get_fd, \
|
||||
set_bufflen, \
|
||||
get_verify_result, \
|
||||
get_state) \
|
||||
static const SSL_METHOD_FUNC func_name LOCAL_ATRR = { \
|
||||
new, \
|
||||
free, \
|
||||
handshake, \
|
||||
shutdown, \
|
||||
clear, \
|
||||
read, \
|
||||
send, \
|
||||
pending, \
|
||||
set_fd, \
|
||||
get_fd, \
|
||||
set_bufflen, \
|
||||
get_verify_result, \
|
||||
get_state \
|
||||
};
|
||||
|
||||
#define IMPLEMENT_TLS_METHOD(ver, mode, fun, func_name) \
|
||||
const SSL_METHOD* func_name(void) { \
|
||||
static const SSL_METHOD func_name##_data LOCAL_ATRR = { \
|
||||
ver, \
|
||||
mode, \
|
||||
&(fun), \
|
||||
}; \
|
||||
return &func_name##_data; \
|
||||
}
|
||||
|
||||
#define IMPLEMENT_SSL_METHOD(ver, mode, fun, func_name) \
|
||||
const SSL_METHOD* func_name(void) { \
|
||||
static const SSL_METHOD func_name##_data LOCAL_ATRR = { \
|
||||
ver, \
|
||||
mode, \
|
||||
&(fun), \
|
||||
}; \
|
||||
return &func_name##_data; \
|
||||
}
|
||||
|
||||
#define IMPLEMENT_X509_METHOD(func_name, \
|
||||
new, \
|
||||
free, \
|
||||
load, \
|
||||
show_info) \
|
||||
const X509_METHOD* func_name(void) { \
|
||||
static const X509_METHOD func_name##_data LOCAL_ATRR = { \
|
||||
new, \
|
||||
free, \
|
||||
load, \
|
||||
show_info \
|
||||
}; \
|
||||
return &func_name##_data; \
|
||||
}
|
||||
|
||||
#define IMPLEMENT_PKEY_METHOD(func_name, \
|
||||
new, \
|
||||
free, \
|
||||
load) \
|
||||
const PKEY_METHOD* func_name(void) { \
|
||||
static const PKEY_METHOD func_name##_data LOCAL_ATRR = { \
|
||||
new, \
|
||||
free, \
|
||||
load \
|
||||
}; \
|
||||
return &func_name##_data; \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get X509 object method
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return X509 object method point
|
||||
*/
|
||||
const X509_METHOD* X509_method(void);
|
||||
|
||||
/**
|
||||
* @brief get private key object method
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return private key object method point
|
||||
*/
|
||||
const PKEY_METHOD* EVP_PKEY_method(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,86 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_PKEY_H_
|
||||
#define _SSL_PKEY_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ssl_types.h"
|
||||
|
||||
/**
|
||||
* @brief create a private key object according to input private key
|
||||
*
|
||||
* @param ipk - input private key point
|
||||
*
|
||||
* @return new private key object point
|
||||
*/
|
||||
EVP_PKEY* __EVP_PKEY_new(EVP_PKEY *ipk, void *rngctx);
|
||||
|
||||
/**
|
||||
* @brief create a private key object
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return private key object point
|
||||
*/
|
||||
EVP_PKEY* EVP_PKEY_new(void *rngctx);
|
||||
|
||||
/**
|
||||
* @brief load a character key context into system context. If '*a' is pointed to the
|
||||
* private key, then load key into it. Or create a new private key object
|
||||
*
|
||||
* @param type - private key type
|
||||
* @param a - a point pointed to a private key point
|
||||
* @param pp - a point pointed to the key context memory point
|
||||
* @param length - key bytes
|
||||
*
|
||||
* @return private key object point
|
||||
*/
|
||||
EVP_PKEY* d2i_PrivateKey(int type,
|
||||
EVP_PKEY **a,
|
||||
const unsigned char **pp,
|
||||
long length, void *rngctx);
|
||||
|
||||
/**
|
||||
* @brief free a private key object
|
||||
*
|
||||
* @param pkey - private key object point
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void EVP_PKEY_free(EVP_PKEY *x);
|
||||
|
||||
/**
|
||||
* @brief load private key into the SSL
|
||||
*
|
||||
* @param type - private key type
|
||||
* @param ssl - SSL point
|
||||
* @param len - data bytes
|
||||
* @param d - data point
|
||||
*
|
||||
* @return result
|
||||
* 0 : failed
|
||||
* 1 : OK
|
||||
*/
|
||||
int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, long len);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,52 @@
|
||||
#ifndef _SSL_STACK_H_
|
||||
#define _SSL_STACK_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ssl_types.h"
|
||||
|
||||
#define STACK_OF(type) struct stack_st_##type
|
||||
|
||||
#define SKM_DEFINE_STACK_OF(t1, t2, t3) \
|
||||
STACK_OF(t1); \
|
||||
static ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
|
||||
{ \
|
||||
return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \
|
||||
} \
|
||||
|
||||
#define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
|
||||
|
||||
/**
|
||||
* @brief create a openssl stack object
|
||||
*
|
||||
* @param c - stack function
|
||||
*
|
||||
* @return openssl stack object point
|
||||
*/
|
||||
OPENSSL_STACK* OPENSSL_sk_new(OPENSSL_sk_compfunc c);
|
||||
|
||||
/**
|
||||
* @brief create a NULL function openssl stack object
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return openssl stack object point
|
||||
*/
|
||||
OPENSSL_STACK *OPENSSL_sk_new_null(void);
|
||||
|
||||
/**
|
||||
* @brief free openssl stack object
|
||||
*
|
||||
* @param openssl stack object point
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void OPENSSL_sk_free(OPENSSL_STACK *stack);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,321 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_TYPES_H_
|
||||
#define _SSL_TYPES_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//#include "private-lib-core.h"
|
||||
#include <lws_config.h>
|
||||
#if defined(LWS_PLAT_FREERTOS)
|
||||
/* AMAZON RTOS has its own setting via MTK_MBEDTLS_CONFIG_FILE */
|
||||
#if !defined(LWS_AMAZON_RTOS)
|
||||
#undef MBEDTLS_CONFIG_FILE
|
||||
#define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "ssl_code.h"
|
||||
|
||||
#include <mbedtls/x509_crt.h>
|
||||
|
||||
#include "private-jit-trust.h"
|
||||
|
||||
typedef void SSL_CIPHER;
|
||||
|
||||
typedef void X509_STORE_CTX;
|
||||
typedef void X509_STORE;
|
||||
|
||||
typedef void RSA;
|
||||
|
||||
typedef void STACK;
|
||||
typedef void BIO;
|
||||
|
||||
#if defined(WIN32) || defined(_WIN32)
|
||||
#define ossl_inline __inline
|
||||
#else
|
||||
#define ossl_inline inline
|
||||
#endif
|
||||
|
||||
#define SSL_METHOD_CALL(f, s, ...) s->method->func->ssl_##f(s, ##__VA_ARGS__)
|
||||
#define X509_METHOD_CALL(f, x, ...) x->method->x509_##f(x, ##__VA_ARGS__)
|
||||
#define EVP_PKEY_METHOD_CALL(f, k, ...) k->method->pkey_##f(k, ##__VA_ARGS__)
|
||||
|
||||
typedef int (*OPENSSL_sk_compfunc)(const void *, const void *);
|
||||
|
||||
struct stack_st;
|
||||
typedef struct stack_st OPENSSL_STACK;
|
||||
|
||||
struct ssl_method_st;
|
||||
typedef struct ssl_method_st SSL_METHOD;
|
||||
|
||||
struct ssl_method_func_st;
|
||||
typedef struct ssl_method_func_st SSL_METHOD_FUNC;
|
||||
|
||||
struct record_layer_st;
|
||||
typedef struct record_layer_st RECORD_LAYER;
|
||||
|
||||
struct ossl_statem_st;
|
||||
typedef struct ossl_statem_st OSSL_STATEM;
|
||||
|
||||
struct ssl_session_st;
|
||||
typedef struct ssl_session_st SSL_SESSION;
|
||||
|
||||
struct ssl_ctx_st;
|
||||
typedef struct ssl_ctx_st SSL_CTX;
|
||||
|
||||
struct ssl_st;
|
||||
typedef struct ssl_st SSL;
|
||||
|
||||
struct cert_st;
|
||||
typedef struct cert_st CERT;
|
||||
|
||||
struct x509_st;
|
||||
typedef struct x509_st X509;
|
||||
|
||||
struct X509_VERIFY_PARAM_st;
|
||||
typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM;
|
||||
|
||||
struct evp_pkey_st;
|
||||
typedef struct evp_pkey_st EVP_PKEY;
|
||||
|
||||
struct x509_method_st;
|
||||
typedef struct x509_method_st X509_METHOD;
|
||||
|
||||
struct pkey_method_st;
|
||||
typedef struct pkey_method_st PKEY_METHOD;
|
||||
|
||||
struct stack_st {
|
||||
|
||||
char **data;
|
||||
|
||||
int num_alloc;
|
||||
|
||||
OPENSSL_sk_compfunc c;
|
||||
};
|
||||
|
||||
struct evp_pkey_st {
|
||||
|
||||
void *pkey_pm;
|
||||
|
||||
const PKEY_METHOD *method;
|
||||
};
|
||||
|
||||
struct x509_st {
|
||||
|
||||
/* X509 certification platform private point */
|
||||
void *x509_pm;
|
||||
|
||||
const X509_METHOD *method;
|
||||
};
|
||||
|
||||
struct cert_st {
|
||||
|
||||
int sec_level;
|
||||
|
||||
X509 *x509;
|
||||
|
||||
EVP_PKEY *pkey;
|
||||
|
||||
};
|
||||
|
||||
struct ossl_statem_st {
|
||||
|
||||
MSG_FLOW_STATE state;
|
||||
|
||||
int hand_state;
|
||||
};
|
||||
|
||||
struct record_layer_st {
|
||||
|
||||
int rstate;
|
||||
|
||||
int read_ahead;
|
||||
};
|
||||
|
||||
struct ssl_session_st {
|
||||
|
||||
long timeout;
|
||||
|
||||
long time;
|
||||
|
||||
X509 *peer;
|
||||
};
|
||||
|
||||
struct X509_VERIFY_PARAM_st {
|
||||
|
||||
int depth;
|
||||
|
||||
};
|
||||
|
||||
typedef int (*next_proto_cb)(SSL *ssl, const unsigned char **out,
|
||||
unsigned char *outlen, const unsigned char *in,
|
||||
unsigned int inlen, void *arg);
|
||||
|
||||
|
||||
struct ssl_ctx_st
|
||||
{
|
||||
int version;
|
||||
|
||||
int references;
|
||||
|
||||
unsigned long options;
|
||||
|
||||
const SSL_METHOD *method;
|
||||
|
||||
CERT *cert;
|
||||
|
||||
X509 *client_CA;
|
||||
|
||||
const char **alpn_protos;
|
||||
|
||||
next_proto_cb alpn_cb;
|
||||
|
||||
int verify_mode;
|
||||
|
||||
int (*default_verify_callback) (SSL *, mbedtls_x509_crt *);
|
||||
|
||||
long session_timeout;
|
||||
|
||||
int read_ahead;
|
||||
|
||||
int read_buffer_len;
|
||||
|
||||
X509_VERIFY_PARAM param;
|
||||
|
||||
void *rngctx;
|
||||
};
|
||||
|
||||
struct ssl_st
|
||||
{
|
||||
/* protocol version(one of SSL3.0, TLS1.0, etc.) */
|
||||
int version;
|
||||
|
||||
unsigned long options;
|
||||
|
||||
/* shut things down(0x01 : sent, 0x02 : received) */
|
||||
int shutdown;
|
||||
|
||||
CERT *cert;
|
||||
|
||||
X509 *client_CA;
|
||||
|
||||
SSL_CTX *ctx;
|
||||
|
||||
const SSL_METHOD *method;
|
||||
|
||||
const char **alpn_protos;
|
||||
|
||||
RECORD_LAYER rlayer;
|
||||
|
||||
/* where we are */
|
||||
OSSL_STATEM statem;
|
||||
|
||||
SSL_SESSION *session;
|
||||
|
||||
int verify_mode;
|
||||
|
||||
int (*verify_callback) (SSL *, mbedtls_x509_crt *);
|
||||
|
||||
#if defined(LWS_WITH_TLS_JIT_TRUST)
|
||||
lws_tls_kid_chain_t kid_chain;
|
||||
#endif
|
||||
|
||||
int rwstate;
|
||||
int interrupted_remaining_write;
|
||||
|
||||
long verify_result;
|
||||
|
||||
X509_VERIFY_PARAM param;
|
||||
|
||||
int err;
|
||||
|
||||
void (*info_callback) (const SSL *ssl, int type, int val);
|
||||
|
||||
/* SSL low-level system arch point */
|
||||
void *ssl_pm;
|
||||
};
|
||||
|
||||
struct ssl_method_st {
|
||||
/* protocol version(one of SSL3.0, TLS1.0, etc.) */
|
||||
int version;
|
||||
|
||||
/* SSL mode(client(0) , server(1), not known(-1)) */
|
||||
int endpoint;
|
||||
|
||||
const SSL_METHOD_FUNC *func;
|
||||
};
|
||||
|
||||
struct ssl_method_func_st {
|
||||
|
||||
int (*ssl_new)(SSL *ssl);
|
||||
|
||||
void (*ssl_free)(SSL *ssl);
|
||||
|
||||
int (*ssl_handshake)(SSL *ssl);
|
||||
|
||||
int (*ssl_shutdown)(SSL *ssl);
|
||||
|
||||
int (*ssl_clear)(SSL *ssl);
|
||||
|
||||
int (*ssl_read)(SSL *ssl, void *buffer, int len);
|
||||
|
||||
int (*ssl_send)(SSL *ssl, const void *buffer, int len);
|
||||
|
||||
int (*ssl_pending)(const SSL *ssl);
|
||||
|
||||
void (*ssl_set_fd)(SSL *ssl, int fd, int mode);
|
||||
|
||||
int (*ssl_get_fd)(const SSL *ssl, int mode);
|
||||
|
||||
void (*ssl_set_bufflen)(SSL *ssl, int len);
|
||||
|
||||
long (*ssl_get_verify_result)(const SSL *ssl);
|
||||
|
||||
OSSL_HANDSHAKE_STATE (*ssl_get_state)(const SSL *ssl);
|
||||
};
|
||||
|
||||
struct x509_method_st {
|
||||
|
||||
int (*x509_new)(X509 *x, X509 *m_x);
|
||||
|
||||
void (*x509_free)(X509 *x);
|
||||
|
||||
int (*x509_load)(X509 *x, const unsigned char *buf, int len);
|
||||
|
||||
int (*x509_show_info)(X509 *x);
|
||||
};
|
||||
|
||||
struct pkey_method_st {
|
||||
|
||||
int (*pkey_new)(EVP_PKEY *pkey, EVP_PKEY *m_pkey, void *rngctx);
|
||||
|
||||
void (*pkey_free)(EVP_PKEY *pkey);
|
||||
|
||||
int (*pkey_load)(EVP_PKEY *pkey, const unsigned char *buf, int len);
|
||||
};
|
||||
|
||||
#define OPENSSL_NPN_NEGOTIATED 1
|
||||
|
||||
int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx);
|
||||
int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,111 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_X509_H_
|
||||
#define _SSL_X509_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ssl_types.h"
|
||||
#include "ssl_stack.h"
|
||||
|
||||
DEFINE_STACK_OF(X509_NAME)
|
||||
|
||||
/**
|
||||
* @brief create a X509 certification object according to input X509 certification
|
||||
*
|
||||
* @param ix - input X509 certification point
|
||||
*
|
||||
* @return new X509 certification object point
|
||||
*/
|
||||
X509* __X509_new(X509 *ix);
|
||||
|
||||
/**
|
||||
* @brief create a X509 certification object
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return X509 certification object point
|
||||
*/
|
||||
X509* X509_new(void);
|
||||
|
||||
/**
|
||||
* @brief load a character certification context into system context. If '*cert' is pointed to the
|
||||
* certification, then load certification into it. Or create a new X509 certification object
|
||||
*
|
||||
* @param cert - a point pointed to X509 certification
|
||||
* @param buffer - a point pointed to the certification context memory point
|
||||
* @param length - certification bytes
|
||||
*
|
||||
* @return X509 certification object point
|
||||
*/
|
||||
X509* d2i_X509(X509 **cert, const unsigned char **buffer, long len);
|
||||
|
||||
/**
|
||||
* @brief free a X509 certification object
|
||||
*
|
||||
* @param x - X509 certification object point
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void X509_free(X509 *x);
|
||||
|
||||
/**
|
||||
* @brief set SSL context client CA certification
|
||||
*
|
||||
* @param ctx - SSL context point
|
||||
* @param x - X509 certification point
|
||||
*
|
||||
* @return result
|
||||
* 0 : failed
|
||||
* 1 : OK
|
||||
*/
|
||||
int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x);
|
||||
|
||||
/**
|
||||
* @brief add CA client certification into the SSL
|
||||
*
|
||||
* @param ssl - SSL point
|
||||
* @param x - X509 certification point
|
||||
*
|
||||
* @return result
|
||||
* 0 : failed
|
||||
* 1 : OK
|
||||
*/
|
||||
int SSL_add_client_CA(SSL *ssl, X509 *x);
|
||||
|
||||
/**
|
||||
* @brief load certification into the SSL
|
||||
*
|
||||
* @param ssl - SSL point
|
||||
* @param len - data bytes
|
||||
* @param d - data point
|
||||
*
|
||||
* @return result
|
||||
* 0 : failed
|
||||
* 1 : OK
|
||||
*
|
||||
*/
|
||||
|
||||
int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len);
|
||||
|
||||
const char *X509_verify_cert_error_string(long n);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,58 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _TLS1_H_
|
||||
#define _TLS1_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
# define TLS1_AD_DECRYPTION_FAILED 21
|
||||
# define TLS1_AD_RECORD_OVERFLOW 22
|
||||
# define TLS1_AD_UNKNOWN_CA 48/* fatal */
|
||||
# define TLS1_AD_ACCESS_DENIED 49/* fatal */
|
||||
# define TLS1_AD_DECODE_ERROR 50/* fatal */
|
||||
# define TLS1_AD_DECRYPT_ERROR 51
|
||||
# define TLS1_AD_EXPORT_RESTRICTION 60/* fatal */
|
||||
# define TLS1_AD_PROTOCOL_VERSION 70/* fatal */
|
||||
# define TLS1_AD_INSUFFICIENT_SECURITY 71/* fatal */
|
||||
# define TLS1_AD_INTERNAL_ERROR 80/* fatal */
|
||||
# define TLS1_AD_INAPPROPRIATE_FALLBACK 86/* fatal */
|
||||
# define TLS1_AD_USER_CANCELLED 90
|
||||
# define TLS1_AD_NO_RENEGOTIATION 100
|
||||
/* codes 110-114 are from RFC3546 */
|
||||
# define TLS1_AD_UNSUPPORTED_EXTENSION 110
|
||||
# define TLS1_AD_CERTIFICATE_UNOBTAINABLE 111
|
||||
# define TLS1_AD_UNRECOGNIZED_NAME 112
|
||||
# define TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE 113
|
||||
# define TLS1_AD_BAD_CERTIFICATE_HASH_VALUE 114
|
||||
# define TLS1_AD_UNKNOWN_PSK_IDENTITY 115/* fatal */
|
||||
# define TLS1_AD_NO_APPLICATION_PROTOCOL 120 /* fatal */
|
||||
|
||||
/* Special value for method supporting multiple versions */
|
||||
#define TLS_ANY_VERSION 0x10000
|
||||
|
||||
#define TLS1_VERSION 0x0301
|
||||
#define TLS1_1_VERSION 0x0302
|
||||
#define TLS1_2_VERSION 0x0303
|
||||
|
||||
#define SSL_TLSEXT_ERR_OK 0
|
||||
#define SSL_TLSEXT_ERR_NOACK 3
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,111 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _X509_VFY_H_
|
||||
#define _X509_VFY_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define X509_V_OK 0
|
||||
#define X509_V_ERR_UNSPECIFIED 1
|
||||
#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2
|
||||
#define X509_V_ERR_UNABLE_TO_GET_CRL 3
|
||||
#define X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE 4
|
||||
#define X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE 5
|
||||
#define X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY 6
|
||||
#define X509_V_ERR_CERT_SIGNATURE_FAILURE 7
|
||||
#define X509_V_ERR_CRL_SIGNATURE_FAILURE 8
|
||||
#define X509_V_ERR_CERT_NOT_YET_VALID 9
|
||||
#define X509_V_ERR_CERT_HAS_EXPIRED 10
|
||||
#define X509_V_ERR_CRL_NOT_YET_VALID 11
|
||||
#define X509_V_ERR_CRL_HAS_EXPIRED 12
|
||||
#define X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD 13
|
||||
#define X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD 14
|
||||
#define X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD 15
|
||||
#define X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD 16
|
||||
#define X509_V_ERR_OUT_OF_MEM 17
|
||||
#define X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 18
|
||||
#define X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN 19
|
||||
#define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 20
|
||||
#define X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE 21
|
||||
#define X509_V_ERR_CERT_CHAIN_TOO_LONG 22
|
||||
#define X509_V_ERR_CERT_REVOKED 23
|
||||
#define X509_V_ERR_INVALID_CA 24
|
||||
#define X509_V_ERR_PATH_LENGTH_EXCEEDED 25
|
||||
#define X509_V_ERR_INVALID_PURPOSE 26
|
||||
#define X509_V_ERR_CERT_UNTRUSTED 27
|
||||
#define X509_V_ERR_CERT_REJECTED 28
|
||||
/* These are 'informational' when looking for issuer cert */
|
||||
#define X509_V_ERR_SUBJECT_ISSUER_MISMATCH 29
|
||||
#define X509_V_ERR_AKID_SKID_MISMATCH 30
|
||||
#define X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH 31
|
||||
#define X509_V_ERR_KEYUSAGE_NO_CERTSIGN 32
|
||||
#define X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER 33
|
||||
#define X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION 34
|
||||
#define X509_V_ERR_KEYUSAGE_NO_CRL_SIGN 35
|
||||
#define X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION 36
|
||||
#define X509_V_ERR_INVALID_NON_CA 37
|
||||
#define X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED 38
|
||||
#define X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE 39
|
||||
#define X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED 40
|
||||
#define X509_V_ERR_INVALID_EXTENSION 41
|
||||
#define X509_V_ERR_INVALID_POLICY_EXTENSION 42
|
||||
#define X509_V_ERR_NO_EXPLICIT_POLICY 43
|
||||
#define X509_V_ERR_DIFFERENT_CRL_SCOPE 44
|
||||
#define X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE 45
|
||||
#define X509_V_ERR_UNNESTED_RESOURCE 46
|
||||
#define X509_V_ERR_PERMITTED_VIOLATION 47
|
||||
#define X509_V_ERR_EXCLUDED_VIOLATION 48
|
||||
#define X509_V_ERR_SUBTREE_MINMAX 49
|
||||
/* The application is not happy */
|
||||
#define X509_V_ERR_APPLICATION_VERIFICATION 50
|
||||
#define X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE 51
|
||||
#define X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX 52
|
||||
#define X509_V_ERR_UNSUPPORTED_NAME_SYNTAX 53
|
||||
#define X509_V_ERR_CRL_PATH_VALIDATION_ERROR 54
|
||||
/* Another issuer check debug option */
|
||||
#define X509_V_ERR_PATH_LOOP 55
|
||||
/* Suite B mode algorithm violation */
|
||||
#define X509_V_ERR_SUITE_B_INVALID_VERSION 56
|
||||
#define X509_V_ERR_SUITE_B_INVALID_ALGORITHM 57
|
||||
#define X509_V_ERR_SUITE_B_INVALID_CURVE 58
|
||||
#define X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM 59
|
||||
#define X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED 60
|
||||
#define X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256 61
|
||||
/* Host, email and IP check errors */
|
||||
#define X509_V_ERR_HOSTNAME_MISMATCH 62
|
||||
#define X509_V_ERR_EMAIL_MISMATCH 63
|
||||
#define X509_V_ERR_IP_ADDRESS_MISMATCH 64
|
||||
/* DANE TLSA errors */
|
||||
#define X509_V_ERR_DANE_NO_MATCH 65
|
||||
/* security level errors */
|
||||
#define X509_V_ERR_EE_KEY_TOO_SMALL 66
|
||||
#define X509_V_ERR_CA_KEY_TOO_SMALL 67
|
||||
#define X509_V_ERR_CA_MD_TOO_WEAK 68
|
||||
/* Caller error */
|
||||
#define X509_V_ERR_INVALID_CALL 69
|
||||
/* Issuer lookup error */
|
||||
#define X509_V_ERR_STORE_LOOKUP 70
|
||||
/* Certificate transparency */
|
||||
#define X509_V_ERR_NO_VALID_SCTS 71
|
||||
|
||||
#define X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION 72
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
1829
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/include/openssl/ssl.h
Executable file
1829
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/include/openssl/ssl.h
Executable file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,61 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_PM_H_
|
||||
#define _SSL_PM_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include "ssl_types.h"
|
||||
#include "ssl_port.h"
|
||||
|
||||
#define LOCAL_ATRR
|
||||
|
||||
int ssl_pm_new(SSL *ssl);
|
||||
void ssl_pm_free(SSL *ssl);
|
||||
|
||||
int ssl_pm_handshake(SSL *ssl);
|
||||
int ssl_pm_shutdown(SSL *ssl);
|
||||
int ssl_pm_clear(SSL *ssl);
|
||||
|
||||
int ssl_pm_read(SSL *ssl, void *buffer, int len);
|
||||
int ssl_pm_send(SSL *ssl, const void *buffer, int len);
|
||||
int ssl_pm_pending(const SSL *ssl);
|
||||
|
||||
void ssl_pm_set_fd(SSL *ssl, int fd, int mode);
|
||||
int ssl_pm_get_fd(const SSL *ssl, int mode);
|
||||
|
||||
OSSL_HANDSHAKE_STATE ssl_pm_get_state(const SSL *ssl);
|
||||
|
||||
void ssl_pm_set_bufflen(SSL *ssl, int len);
|
||||
|
||||
int x509_pm_show_info(X509 *x);
|
||||
int x509_pm_new(X509 *x, X509 *m_x);
|
||||
void x509_pm_free(X509 *x);
|
||||
int x509_pm_load(X509 *x, const unsigned char *buffer, int len);
|
||||
|
||||
int pkey_pm_new(EVP_PKEY *pk, EVP_PKEY *m_pk, void *rngctx);
|
||||
void pkey_pm_free(EVP_PKEY *pk);
|
||||
int pkey_pm_load(EVP_PKEY *pk, const unsigned char *buffer, int len);
|
||||
|
||||
long ssl_pm_get_verify_result(const SSL *ssl);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,46 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _SSL_PORT_H_
|
||||
#define _SSL_PORT_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "string.h"
|
||||
#include "stdlib.h"
|
||||
#if defined(LWS_HAVE_MALLOC_H)
|
||||
#include "malloc.h"
|
||||
#endif
|
||||
|
||||
void *ssl_mem_zalloc(size_t size);
|
||||
|
||||
#define ssl_mem_malloc malloc
|
||||
#define ssl_mem_free free
|
||||
|
||||
#define ssl_memcpy memcpy
|
||||
#define ssl_strlen strlen
|
||||
|
||||
#define ssl_speed_up_enter()
|
||||
#define ssl_speed_up_exit()
|
||||
|
||||
#define SSL_DEBUG_FL
|
||||
#define SSL_DEBUG_LOG(fmt, ...) ESP_LOGI("openssl", fmt, ##__VA_ARGS__)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,89 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include "ssl_cert.h"
|
||||
#include "ssl_pkey.h"
|
||||
#include "ssl_x509.h"
|
||||
#include "ssl_dbg.h"
|
||||
#include "ssl_port.h"
|
||||
|
||||
/**
|
||||
* @brief create a certification object according to input certification
|
||||
*/
|
||||
CERT *__ssl_cert_new(CERT *ic, void *rngctx)
|
||||
{
|
||||
CERT *cert;
|
||||
|
||||
X509 *ix;
|
||||
EVP_PKEY *ipk;
|
||||
|
||||
cert = ssl_mem_zalloc(sizeof(CERT));
|
||||
if (!cert) {
|
||||
SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "no enough memory > (cert)");
|
||||
goto no_mem;
|
||||
}
|
||||
|
||||
if (ic) {
|
||||
ipk = ic->pkey;
|
||||
ix = ic->x509;
|
||||
} else {
|
||||
ipk = NULL;
|
||||
ix = NULL;
|
||||
}
|
||||
|
||||
cert->pkey = __EVP_PKEY_new(ipk, rngctx);
|
||||
if (!cert->pkey) {
|
||||
SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "__EVP_PKEY_new() return NULL");
|
||||
goto pkey_err;
|
||||
}
|
||||
|
||||
cert->x509 = __X509_new(ix);
|
||||
if (!cert->x509) {
|
||||
SSL_DEBUG(SSL_CERT_ERROR_LEVEL, "__X509_new() return NULL");
|
||||
goto x509_err;
|
||||
}
|
||||
|
||||
return cert;
|
||||
|
||||
x509_err:
|
||||
EVP_PKEY_free(cert->pkey);
|
||||
pkey_err:
|
||||
ssl_mem_free(cert);
|
||||
no_mem:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief create a certification object include private key object
|
||||
*/
|
||||
CERT *ssl_cert_new(void *rngctx)
|
||||
{
|
||||
return __ssl_cert_new(NULL, rngctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief free a certification object
|
||||
*/
|
||||
void ssl_cert_free(CERT *cert)
|
||||
{
|
||||
SSL_ASSERT3(cert);
|
||||
|
||||
X509_free(cert->x509);
|
||||
|
||||
EVP_PKEY_free(cert->pkey);
|
||||
|
||||
ssl_mem_free(cert);
|
||||
}
|
1246
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/library/ssl_lib.c
Normal file
1246
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/library/ssl_lib.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,89 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include "ssl_methods.h"
|
||||
#include "ssl_pm.h"
|
||||
|
||||
/**
|
||||
* TLS method function collection
|
||||
*/
|
||||
IMPLEMENT_TLS_METHOD_FUNC(TLS_method_func,
|
||||
ssl_pm_new, ssl_pm_free,
|
||||
ssl_pm_handshake, ssl_pm_shutdown, ssl_pm_clear,
|
||||
ssl_pm_read, ssl_pm_send, ssl_pm_pending,
|
||||
ssl_pm_set_fd, ssl_pm_get_fd,
|
||||
ssl_pm_set_bufflen,
|
||||
ssl_pm_get_verify_result,
|
||||
ssl_pm_get_state);
|
||||
|
||||
/**
|
||||
* TLS or SSL client method collection
|
||||
*/
|
||||
IMPLEMENT_TLS_METHOD(TLS_ANY_VERSION, 0, TLS_method_func, TLS_client_method);
|
||||
|
||||
IMPLEMENT_TLS_METHOD(TLS1_2_VERSION, 0, TLS_method_func, TLSv1_2_client_method);
|
||||
|
||||
#if 0
|
||||
IMPLEMENT_TLS_METHOD(TLS1_1_VERSION, 0, TLS_method_func, TLSv1_1_client_method);
|
||||
|
||||
IMPLEMENT_TLS_METHOD(TLS1_VERSION, 0, TLS_method_func, TLSv1_client_method);
|
||||
|
||||
IMPLEMENT_SSL_METHOD(SSL3_VERSION, 0, TLS_method_func, SSLv3_client_method);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* TLS or SSL server method collection
|
||||
*/
|
||||
IMPLEMENT_TLS_METHOD(TLS_ANY_VERSION, 1, TLS_method_func, TLS_server_method);
|
||||
|
||||
IMPLEMENT_TLS_METHOD(TLS1_2_VERSION, 1, TLS_method_func, TLSv1_2_server_method);
|
||||
|
||||
#if 0
|
||||
IMPLEMENT_TLS_METHOD(TLS1_1_VERSION, 1, TLS_method_func, TLSv1_1_server_method);
|
||||
|
||||
IMPLEMENT_TLS_METHOD(TLS1_VERSION, 0, TLS_method_func, TLSv1_server_method);
|
||||
|
||||
IMPLEMENT_SSL_METHOD(SSL3_VERSION, 1, TLS_method_func, SSLv3_server_method);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* TLS or SSL method collection
|
||||
*/
|
||||
IMPLEMENT_TLS_METHOD(TLS_ANY_VERSION, -1, TLS_method_func, TLS_method);
|
||||
|
||||
IMPLEMENT_SSL_METHOD(TLS1_2_VERSION, -1, TLS_method_func, TLSv1_2_method);
|
||||
|
||||
#if 0
|
||||
IMPLEMENT_SSL_METHOD(TLS1_1_VERSION, -1, TLS_method_func, TLSv1_1_method);
|
||||
|
||||
IMPLEMENT_SSL_METHOD(TLS1_VERSION, -1, TLS_method_func, TLSv1_method);
|
||||
|
||||
IMPLEMENT_SSL_METHOD(SSL3_VERSION, -1, TLS_method_func, SSLv3_method);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief get X509 object method
|
||||
*/
|
||||
IMPLEMENT_X509_METHOD(X509_method,
|
||||
x509_pm_new, x509_pm_free,
|
||||
x509_pm_load, x509_pm_show_info);
|
||||
|
||||
/**
|
||||
* @brief get private key object method
|
||||
*/
|
||||
IMPLEMENT_PKEY_METHOD(EVP_PKEY_method,
|
||||
pkey_pm_new, pkey_pm_free,
|
||||
pkey_pm_load);
|
241
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/library/ssl_pkey.c
Normal file
241
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/library/ssl_pkey.c
Normal file
@ -0,0 +1,241 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include "ssl_pkey.h"
|
||||
#include "ssl_methods.h"
|
||||
#include "ssl_dbg.h"
|
||||
#include "ssl_port.h"
|
||||
|
||||
/**
|
||||
* @brief create a private key object according to input private key
|
||||
*/
|
||||
EVP_PKEY* __EVP_PKEY_new(EVP_PKEY *ipk, void *rngctx)
|
||||
{
|
||||
int ret;
|
||||
EVP_PKEY *pkey;
|
||||
|
||||
pkey = ssl_mem_zalloc(sizeof(EVP_PKEY));
|
||||
if (!pkey) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "no enough memory > (pkey)");
|
||||
goto no_mem;
|
||||
}
|
||||
|
||||
if (ipk) {
|
||||
pkey->method = ipk->method;
|
||||
} else {
|
||||
pkey->method = EVP_PKEY_method();
|
||||
}
|
||||
|
||||
ret = EVP_PKEY_METHOD_CALL(new, pkey, ipk, rngctx);
|
||||
if (ret) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_METHOD_CALL(new) return %d", ret);
|
||||
goto failed;
|
||||
}
|
||||
|
||||
return pkey;
|
||||
|
||||
failed:
|
||||
ssl_mem_free(pkey);
|
||||
no_mem:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief create a private key object
|
||||
*/
|
||||
EVP_PKEY* EVP_PKEY_new(void *rngctx)
|
||||
{
|
||||
return __EVP_PKEY_new(NULL, rngctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief free a private key object
|
||||
*/
|
||||
void EVP_PKEY_free(EVP_PKEY *pkey)
|
||||
{
|
||||
SSL_ASSERT3(pkey);
|
||||
|
||||
EVP_PKEY_METHOD_CALL(free, pkey);
|
||||
|
||||
ssl_mem_free(pkey);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load a character key context into system context. If '*a' is pointed to the
|
||||
* private key, then load key into it. Or create a new private key object
|
||||
*/
|
||||
EVP_PKEY *d2i_PrivateKey(int type,
|
||||
EVP_PKEY **a,
|
||||
const unsigned char **pp,
|
||||
long length, void *rngctx)
|
||||
{
|
||||
int m = 0;
|
||||
int ret;
|
||||
EVP_PKEY *pkey;
|
||||
|
||||
SSL_ASSERT2(pp);
|
||||
SSL_ASSERT2(*pp);
|
||||
SSL_ASSERT2(length);
|
||||
|
||||
if (a && *a) {
|
||||
pkey = *a;
|
||||
} else {
|
||||
pkey = EVP_PKEY_new(rngctx);
|
||||
if (!pkey) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_new() return NULL");
|
||||
goto failed1;
|
||||
}
|
||||
|
||||
m = 1;
|
||||
}
|
||||
|
||||
ret = EVP_PKEY_METHOD_CALL(load, pkey, *pp, (int)length);
|
||||
if (ret) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "EVP_PKEY_METHOD_CALL(load) return %d", ret);
|
||||
goto failed2;
|
||||
}
|
||||
|
||||
if (a)
|
||||
*a = pkey;
|
||||
|
||||
return pkey;
|
||||
|
||||
failed2:
|
||||
if (m)
|
||||
EVP_PKEY_free(pkey);
|
||||
failed1:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief set the SSL context private key
|
||||
*/
|
||||
int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
|
||||
{
|
||||
SSL_ASSERT1(ctx);
|
||||
SSL_ASSERT1(pkey);
|
||||
|
||||
if (ctx->cert->pkey == pkey)
|
||||
return 1;
|
||||
|
||||
if (ctx->cert->pkey)
|
||||
EVP_PKEY_free(ctx->cert->pkey);
|
||||
|
||||
ctx->cert->pkey = pkey;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief set the SSL private key
|
||||
*/
|
||||
int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
|
||||
{
|
||||
SSL_ASSERT1(ssl);
|
||||
SSL_ASSERT1(pkey);
|
||||
|
||||
if (ssl->cert->pkey == pkey)
|
||||
return 1;
|
||||
|
||||
if (ssl->cert->pkey)
|
||||
EVP_PKEY_free(ssl->cert->pkey);
|
||||
|
||||
ssl->cert->pkey = pkey;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load private key into the SSL context
|
||||
*/
|
||||
int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
|
||||
const unsigned char *d, long len)
|
||||
{
|
||||
int ret;
|
||||
EVP_PKEY *pk;
|
||||
|
||||
pk = d2i_PrivateKey(0, NULL, &d, len, ctx->rngctx);
|
||||
if (!pk) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_PrivateKey() return NULL");
|
||||
goto failed1;
|
||||
}
|
||||
|
||||
ret = SSL_CTX_use_PrivateKey(ctx, pk);
|
||||
if (!ret) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_CTX_use_PrivateKey() return %d", ret);
|
||||
goto failed2;
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
failed2:
|
||||
EVP_PKEY_free(pk);
|
||||
failed1:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load private key into the SSL
|
||||
*/
|
||||
int SSL_use_PrivateKey_ASN1(int type, SSL *ssl,
|
||||
const unsigned char *d, long len)
|
||||
{
|
||||
int ret;
|
||||
EVP_PKEY *pk;
|
||||
|
||||
pk = d2i_PrivateKey(0, NULL, &d, len, ssl->ctx->rngctx);
|
||||
if (!pk) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_PrivateKey() return NULL");
|
||||
goto failed1;
|
||||
}
|
||||
|
||||
ret = SSL_use_PrivateKey(ssl, pk);
|
||||
if (!ret) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_use_PrivateKey() return %d", ret);
|
||||
goto failed2;
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
failed2:
|
||||
EVP_PKEY_free(pk);
|
||||
failed1:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load the private key file into SSL context
|
||||
*/
|
||||
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load the private key file into SSL
|
||||
*/
|
||||
int SSL_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load the RSA ASN1 private key into SSL context
|
||||
*/
|
||||
int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, long len)
|
||||
{
|
||||
return SSL_CTX_use_PrivateKey_ASN1(0, ctx, d, len);
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include "ssl_stack.h"
|
||||
#include "ssl_dbg.h"
|
||||
#include "ssl_port.h"
|
||||
|
||||
#ifndef CONFIG_MIN_NODES
|
||||
#define MIN_NODES 4
|
||||
#else
|
||||
#define MIN_NODES CONFIG_MIN_NODES
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief create a openssl stack object
|
||||
*/
|
||||
OPENSSL_STACK* OPENSSL_sk_new(OPENSSL_sk_compfunc c)
|
||||
{
|
||||
OPENSSL_STACK *stack;
|
||||
char **data;
|
||||
|
||||
stack = ssl_mem_zalloc(sizeof(OPENSSL_STACK));
|
||||
if (!stack) {
|
||||
SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (stack)");
|
||||
goto no_mem1;
|
||||
}
|
||||
|
||||
data = ssl_mem_zalloc(sizeof(*data) * MIN_NODES);
|
||||
if (!data) {
|
||||
SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (data)");
|
||||
goto no_mem2;
|
||||
}
|
||||
|
||||
stack->data = data;
|
||||
stack->num_alloc = MIN_NODES;
|
||||
stack->c = c;
|
||||
|
||||
return stack;
|
||||
|
||||
no_mem2:
|
||||
ssl_mem_free(stack);
|
||||
no_mem1:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief create a NULL function openssl stack object
|
||||
*/
|
||||
OPENSSL_STACK *OPENSSL_sk_new_null(void)
|
||||
{
|
||||
return OPENSSL_sk_new((OPENSSL_sk_compfunc)NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief free openssl stack object
|
||||
*/
|
||||
void OPENSSL_sk_free(OPENSSL_STACK *stack)
|
||||
{
|
||||
SSL_ASSERT3(stack);
|
||||
|
||||
ssl_mem_free(stack->data);
|
||||
ssl_mem_free(stack);
|
||||
}
|
349
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/library/ssl_x509.c
Normal file
349
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/library/ssl_x509.c
Normal file
@ -0,0 +1,349 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "private-lib-core.h"
|
||||
|
||||
#include "ssl_x509.h"
|
||||
#include "ssl_methods.h"
|
||||
#include "ssl_dbg.h"
|
||||
#include "ssl_port.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
/**
|
||||
* @brief show X509 certification information
|
||||
*/
|
||||
int __X509_show_info(X509 *x)
|
||||
{
|
||||
return X509_METHOD_CALL(show_info, x);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief create a X509 certification object according to input X509 certification
|
||||
*/
|
||||
X509* __X509_new(X509 *ix)
|
||||
{
|
||||
int ret;
|
||||
X509 *x;
|
||||
|
||||
x = ssl_mem_zalloc(sizeof(X509));
|
||||
if (!x) {
|
||||
SSL_DEBUG(SSL_X509_ERROR_LEVEL, "no enough memory > (x)");
|
||||
goto no_mem;
|
||||
}
|
||||
|
||||
if (ix)
|
||||
x->method = ix->method;
|
||||
else
|
||||
x->method = X509_method();
|
||||
|
||||
ret = X509_METHOD_CALL(new, x, ix);
|
||||
if (ret) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(new) return %d", ret);
|
||||
goto failed;
|
||||
}
|
||||
|
||||
return x;
|
||||
|
||||
failed:
|
||||
ssl_mem_free(x);
|
||||
no_mem:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief create a X509 certification object
|
||||
*/
|
||||
X509* X509_new(void)
|
||||
{
|
||||
return __X509_new(NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief free a X509 certification object
|
||||
*/
|
||||
void X509_free(X509 *x)
|
||||
{
|
||||
SSL_ASSERT3(x);
|
||||
|
||||
X509_METHOD_CALL(free, x);
|
||||
|
||||
ssl_mem_free(x);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief load a character certification context into system context. If '*cert' is pointed to the
|
||||
* certification, then load certification into it. Or create a new X509 certification object
|
||||
*/
|
||||
X509* d2i_X509(X509 **cert, const unsigned char **buffer, long len)
|
||||
{
|
||||
int m = 0;
|
||||
int ret;
|
||||
X509 *x;
|
||||
|
||||
SSL_ASSERT2(buffer);
|
||||
SSL_ASSERT2(len);
|
||||
|
||||
if (cert && *cert) {
|
||||
x = *cert;
|
||||
} else {
|
||||
x = X509_new();
|
||||
if (!x) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_new() return NULL");
|
||||
goto failed1;
|
||||
}
|
||||
m = 1;
|
||||
}
|
||||
|
||||
ret = X509_METHOD_CALL(load, x, *buffer, (int)len);
|
||||
if (ret) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "X509_METHOD_CALL(load) return %d", ret);
|
||||
goto failed2;
|
||||
}
|
||||
|
||||
return x;
|
||||
|
||||
failed2:
|
||||
if (m)
|
||||
X509_free(x);
|
||||
failed1:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief return SSL X509 verify parameters
|
||||
*/
|
||||
|
||||
X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
|
||||
{
|
||||
return &ssl->param;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief set X509 host verification flags
|
||||
*/
|
||||
|
||||
int X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
|
||||
unsigned long flags)
|
||||
{
|
||||
/* flags not supported yet */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief clear X509 host verification flags
|
||||
*/
|
||||
|
||||
int X509_VERIFY_PARAM_clear_hostflags(X509_VERIFY_PARAM *param,
|
||||
unsigned long flags)
|
||||
{
|
||||
/* flags not supported yet */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief set SSL context client CA certification
|
||||
*/
|
||||
int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
|
||||
{
|
||||
SSL_ASSERT1(ctx);
|
||||
SSL_ASSERT1(x);
|
||||
assert(ctx);
|
||||
if (ctx->client_CA == x)
|
||||
return 1;
|
||||
|
||||
X509_free(ctx->client_CA);
|
||||
|
||||
ctx->client_CA = x;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief add CA client certification into the SSL
|
||||
*/
|
||||
int SSL_CTX_add_client_CA_ASN1(SSL_CTX *ctx, int len,
|
||||
const unsigned char *d)
|
||||
{
|
||||
SSL_ASSERT1(ctx);
|
||||
|
||||
if (!d2i_X509(&ctx->client_CA, &d, len)) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief add CA client certification into the SSL
|
||||
*/
|
||||
int SSL_add_client_CA(SSL *ssl, X509 *x)
|
||||
{
|
||||
SSL_ASSERT1(ssl);
|
||||
SSL_ASSERT1(x);
|
||||
|
||||
if (ssl->client_CA == x)
|
||||
return 1;
|
||||
|
||||
X509_free(ssl->client_CA);
|
||||
|
||||
ssl->client_CA = x;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief set the SSL context certification
|
||||
*/
|
||||
int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
|
||||
{
|
||||
SSL_ASSERT1(ctx);
|
||||
SSL_ASSERT1(x);
|
||||
|
||||
if (ctx->cert->x509 == x)
|
||||
return 1;
|
||||
|
||||
X509_free(ctx->cert->x509);
|
||||
|
||||
ctx->cert->x509 = x;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief set the SSL certification
|
||||
*/
|
||||
int SSL_use_certificate(SSL *ssl, X509 *x)
|
||||
{
|
||||
SSL_ASSERT1(ssl);
|
||||
SSL_ASSERT1(x);
|
||||
|
||||
if (ssl->cert->x509 == x)
|
||||
return 1;
|
||||
|
||||
X509_free(ssl->cert->x509);
|
||||
|
||||
ssl->cert->x509 = x;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get the SSL certification point
|
||||
*/
|
||||
X509 *SSL_get_certificate(const SSL *ssl)
|
||||
{
|
||||
SSL_ASSERT2(ssl);
|
||||
|
||||
return ssl->cert->x509;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load certification into the SSL context
|
||||
*/
|
||||
int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len,
|
||||
const unsigned char *d)
|
||||
{
|
||||
int ret;
|
||||
X509 *x;
|
||||
|
||||
x = d2i_X509(NULL, &d, len);
|
||||
if (!x) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
|
||||
goto failed1;
|
||||
}
|
||||
|
||||
ret = SSL_CTX_use_certificate(ctx, x);
|
||||
if (!ret) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_CTX_use_certificate() return %d", ret);
|
||||
goto failed2;
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
failed2:
|
||||
X509_free(x);
|
||||
failed1:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load certification into the SSL
|
||||
*/
|
||||
int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
|
||||
{
|
||||
int ret;
|
||||
X509 *x;
|
||||
|
||||
x = d2i_X509(NULL, &d, len);
|
||||
if (!x) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "d2i_X509() return NULL");
|
||||
goto failed1;
|
||||
}
|
||||
|
||||
ret = SSL_use_certificate(ssl, x);
|
||||
if (!ret) {
|
||||
SSL_DEBUG(SSL_PKEY_ERROR_LEVEL, "SSL_use_certificate() return %d", ret);
|
||||
goto failed2;
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
failed2:
|
||||
X509_free(x);
|
||||
failed1:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load the certification file into SSL context
|
||||
*/
|
||||
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load the certification file into SSL
|
||||
*/
|
||||
int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get peer certification
|
||||
*/
|
||||
X509 *SSL_get_peer_certificate(const SSL *ssl)
|
||||
{
|
||||
SSL_ASSERT2(ssl);
|
||||
|
||||
return ssl->session->peer;
|
||||
}
|
||||
|
||||
int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
|
||||
{
|
||||
return X509_V_ERR_UNSPECIFIED;
|
||||
}
|
||||
|
||||
int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *X509_verify_cert_error_string(long n)
|
||||
{
|
||||
return "unknown";
|
||||
}
|
1008
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/platform/ssl_pm.c
Executable file
1008
Kinc/Sources/kinc/libs/tls/mbedtls/wrapper/platform/ssl_pm.c
Executable file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,29 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "ssl_port.h"
|
||||
|
||||
/*********************************************************************************************/
|
||||
/********************************* SSL general interface *************************************/
|
||||
|
||||
void *ssl_mem_zalloc(size_t size)
|
||||
{
|
||||
void *p = malloc(size);
|
||||
|
||||
if (p)
|
||||
memset(p, 0, size);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
Reference in New Issue
Block a user