mbedtls
源码地址:ARMmbed
mbedtls 目前维护了两个主版本:3.x 和 2.x,两个版本的一个区别是 3.x 改动了 RSA 算法。因安全原因 3.x 版本不再支持 RSA 公私钥加解密的混用。涉及变更的方法包括:
mbedtls_rsa_pkcs1_encrypt
mbedtls_rsa_pkcs1_decrypt
...
相关讨论:
https://forums.mbed.com/t/mbedtls-troubles-with-signature/4660
https://github.com/Mbed-TLS/mbedtls/issues/4278
3.x 和 2.x 版本都适用于 curl。
nghttp2
源码地址:nghttp2
项目官网:https://nghttp2.org
编译命令:
./configure --host=${HOST}-apple-darwin \
--prefix="${OUTPUT_PATH}" \
--disable-shared \
--disable-app \
--disable-threads \
--enable-lib-only # Use --enable-lib-only to ensure that only libnghttp2 is built.
curl --with-mbedtls --with-nghttp2 参数
首先编译 mbedtls 和 nghttp2。编译 curl 时,配置依赖库路径:
./configure --host=${HOST}-apple-darwin \
--with-mbedtls=$LIB_PATH \
--with-nghttp2=$LIB_PATH \
--enable-static \
--disable-shared \
--disable-dependency-tracking \
--enable-http \
--enable-hsts \
--enable-ipv6 \
...
--with-mbedtls 和 --with-nghttp2 应填入对应的库路径,确保库二进制文件位于:
$LIB_PATH/lib/libmbedtls.a
$LIB_PATH/lib/libnghttp2.a
编译 curl 时,编译 log 中必须要有如下信息输出才算配置正确。
...
mbedtls_ssl_init in -lmbedtls... yes
...
nghttp2_session_get_stream_local_window_size in -lnghttp2... yes
mbedtls TLS1.2
编译 mbedtls 时,需指定 MBEDTLS_CONFIG_FILE 宏:
export CFLAGS="-arch ${ARCH} -pipe -Os -isysroot ${SDKROOT} -I${SOURCE_PATH}/include -I$SCRIPT_PATH -DMBEDTLS_CONFIG_FILE='<mbedtls_config.h>'"
mbedtls_config.h 中可开启指定宏以选择需要的加密算法和特性。如需支持TLS1.2,可开启以下配置:
/* mbed TLS feature support */
#define MBEDTLS_CIPHER_MODE_CBC
#define MBEDTLS_PKCS1_V15
#define MBEDTLS_SSL_PROTO_TLS1_2 //目前不支持 TLS1.3
// 指定交换算法,将会影响到 clinet suppported CIPHERSUITES
// https://github.com/Mbed-TLS/mbedtls/blob/mbedtls-2.28/include/mbedtls/check_config.h
#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
#define MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
#define MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
#define MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED
// http2 ALPN
#define MBEDTLS_SSL_ALPN
...
//末尾增加 check_config.h 的引用,以便检查宏定义是否有依赖缺失或不一致的情形。
#include "mbedtls/check_config.h"
MBEDTLS_KEY_EXCHANGE相关的宏能开启的尽量开启,即尽量支持所有的密钥交换算法,避免因为服务端使用了未受 curl 支持的算法而导致 https 请求失败。

留言板