31 #include <openssl/bio.h>
32 #include <openssl/ssl.h>
33 #include <openssl/err.h>
45 if (!(mem = BIO_new(BIO_s_mem())))
53 BIO_get_mem_ptr(mem, &bptr);
54 if (!bptr || !bptr->length)
63 memcpy(pem_str, bptr->data, bptr->length);
64 pem_str[bptr->length] =
'\0';
83 BIO *mem = BIO_new(BIO_s_mem());
90 if (!PEM_write_bio_X509(mem, cert))
93 BIO_get_mem_ptr(mem, &bptr);
94 if (!bptr || !bptr->length)
goto err;
99 memcpy(
out, bptr->data, bptr->length);
100 out[bptr->length] =
'\0';
124 unsigned char md[EVP_MAX_MD_SIZE];
126 AVBPrint fingerprint;
133 if (X509_digest(cert, EVP_sha256(),
md, &n) != 1) {
138 for (
i = 0;
i < n;
i++) {
144 if (!fingerprint.str || !strlen(fingerprint.str)) {
159 int ff_ssl_read_key_cert(
char *key_url,
char *cert_url,
char *key_buf,
size_t key_sz,
char *cert_buf,
size_t cert_sz,
char **fingerprint)
163 AVBPrint key_bp, cert_bp;
166 char *key_tem =
NULL, *cert_tem =
NULL;
179 if (!(key_b = BIO_new(BIO_s_mem()))) {
184 BIO_write(key_b, key_bp.str, key_bp.len);
185 pkey = PEM_read_bio_PrivateKey(key_b,
NULL,
NULL,
NULL);
199 if (!(cert_b = BIO_new(BIO_s_mem()))) {
204 BIO_write(cert_b, cert_bp.str, cert_bp.len);
215 snprintf(key_buf, key_sz,
"%s", key_tem);
216 snprintf(cert_buf, cert_sz,
"%s", cert_tem);
232 if (cert_tem)
av_free(cert_tem);
249 #if OPENSSL_VERSION_NUMBER < 0x30000000L
250 EC_GROUP *ecgroup =
NULL;
251 int curve = NID_X9_62_prime256v1;
253 const char *curve = SN_X9_62_prime256v1;
256 #if OPENSSL_VERSION_NUMBER < 0x30000000L
257 *pkey = EVP_PKEY_new();
258 *eckey = EC_KEY_new();
259 ecgroup = EC_GROUP_new_by_curve_name(curve);
265 #if OPENSSL_VERSION_NUMBER < 0x10100000L // v1.1.x
267 EC_GROUP_set_asn1_flag(ecgroup, OPENSSL_EC_NAMED_CURVE);
270 if (EC_KEY_set_group(*eckey, ecgroup) != 1) {
271 av_log(
NULL,
AV_LOG_ERROR,
"TLS: Generate private key, EC_KEY_set_group failed, %s\n", ERR_error_string(ERR_get_error(),
NULL));
275 if (EC_KEY_generate_key(*eckey) != 1) {
276 av_log(
NULL,
AV_LOG_ERROR,
"TLS: Generate private key, EC_KEY_generate_key failed, %s\n", ERR_error_string(ERR_get_error(),
NULL));
280 if (EVP_PKEY_set1_EC_KEY(*pkey, *eckey) != 1) {
281 av_log(
NULL,
AV_LOG_ERROR,
"TLS: Generate private key, EVP_PKEY_set1_EC_KEY failed, %s\n", ERR_error_string(ERR_get_error(),
NULL));
285 *pkey = EVP_EC_gen(curve);
287 av_log(
NULL,
AV_LOG_ERROR,
"TLS: Generate private key, EVP_EC_gen curve=%s failed, %s\n", curve, ERR_error_string(ERR_get_error(),
NULL));
296 #if OPENSSL_VERSION_NUMBER < 0x30000000L
297 EC_GROUP_free(ecgroup);
304 int ret = 0, serial, expire_day;
305 const char *aor =
"lavf";
306 X509_NAME* subject =
NULL;
314 subject = X509_NAME_new();
320 if (ASN1_INTEGER_set(X509_get_serialNumber(*cert), serial) != 1) {
325 if (X509_NAME_add_entry_by_txt(subject,
"CN", MBSTRING_ASC, aor, strlen(aor), -1, 0) != 1) {
330 if (X509_set_issuer_name(*cert, subject) != 1) {
334 if (X509_set_subject_name(*cert, subject) != 1) {
340 if (!X509_gmtime_adj(X509_get_notBefore(*cert), 0)) {
344 if (!X509_gmtime_adj(X509_get_notAfter(*cert), 60*60*24*expire_day)) {
349 if (X509_set_version(*cert, 2) != 1) {
354 if (X509_set_pubkey(*cert, pkey) != 1) {
359 if (!X509_sign(*cert, pkey, EVP_sha1())) {
376 X509_NAME_free(subject);
384 EVP_PKEY *pkey =
NULL;
385 EC_KEY *ec_key =
NULL;
387 char *key_tem =
NULL, *cert_tem =
NULL;
398 snprintf(key_buf, key_sz,
"%s", key_tem);
399 snprintf(cert_buf, cert_sz,
"%s", cert_tem);
402 if (cert_tem)
av_free(cert_tem);
418 BIO *mem = BIO_new_mem_buf(pem_str, -1);
424 EVP_PKEY *pkey =
NULL;
433 is_priv ?
"private" :
"public");
448 BIO *mem = BIO_new_mem_buf(pem_str, -1);
470 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
486 int r2 = ERR_get_error();
488 ERR_error_string_n(r2,
ctx->error_message,
sizeof(
ctx->error_message));
490 ctx->error_message[0] =
'\0';
493 return ctx->error_message;
499 c->tls_shared.udp = udp;
506 const char*
dst =
"EXTRACTOR-dtls_srtp";
509 ret = SSL_export_keying_material(
c->ssl, dtls_srtp_materials, materials_sz,
521 return c->tls_shared.state;
529 #if OPENSSL_VERSION_NUMBER < 0x10100000L
537 #include <openssl/crypto.h>
541 static void openssl_lock(
int mode,
int type,
const char *file,
int line)
543 if (
mode & CRYPTO_LOCK)
548 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
549 static unsigned long openssl_thread_id(
void)
551 return (intptr_t) pthread_self();
561 SSL_load_error_strings();
563 if (!CRYPTO_get_locking_callback()) {
566 if (!openssl_mutexes) {
571 for (
i = 0;
i < CRYPTO_num_locks();
i++)
573 CRYPTO_set_locking_callback(openssl_lock);
574 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
575 CRYPTO_set_id_callback(openssl_thread_id);
592 if (CRYPTO_get_locking_callback() == openssl_lock) {
594 CRYPTO_set_locking_callback(
NULL);
595 for (
i = 0;
i < CRYPTO_num_locks();
i++)
610 int err = SSL_get_error(
c->ssl,
ret);
611 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
614 while ((e = ERR_get_error()) != 0) {
633 SSL_shutdown(
c->ssl);
637 SSL_CTX_free(
c->ctx);
639 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
640 if (
c->url_bio_method)
641 BIO_meth_free(
c->url_bio_method);
643 #if OPENSSL_VERSION_NUMBER < 0x10100000L
651 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
653 BIO_set_data(
b,
NULL);
668 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
669 #define GET_BIO_DATA(x) BIO_get_data(x)
671 #define GET_BIO_DATA(x) (x)->ptr
677 int ret =
ffurl_read(
c->tls_shared.is_dtls ?
c->tls_shared.udp :
c->tls_shared.tcp, buf,
len);
680 BIO_clear_retry_flags(
b);
684 BIO_set_retry_read(
b);
696 BIO_clear_retry_flags(
b);
700 BIO_set_retry_write(
b);
708 if (cmd == BIO_CTRL_FLUSH) {
709 BIO_clear_retry_flags(
b);
720 #if OPENSSL_VERSION_NUMBER < 0x1010000fL
722 .type = BIO_TYPE_SOURCE_SINK,
723 .name =
"urlprotocol bio",
738 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
739 p->url_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK,
"urlprotocol bio");
746 bio = BIO_new(p->url_bio_method);
747 BIO_set_data(bio, p);
752 SSL_set_bio(p->
ssl, bio, bio);
756 const char *method =
"undefined";
759 if (where & SSL_ST_CONNECT) {
760 method =
"SSL_connect";
761 }
else if (where & SSL_ST_ACCEPT)
762 method =
"SSL_accept";
764 if (where & SSL_CB_LOOP) {
766 method, SSL_state_string(ssl), SSL_state_string_long(ssl), where,
ret);
767 }
else if (where & SSL_CB_ALERT) {
768 method = (where & SSL_CB_READ) ?
"read":
"write";
770 method, SSL_state_string(ssl), SSL_state_string_long(ssl), where,
ret);
788 r0 = SSL_do_handshake(p->
ssl);
789 r1 = SSL_get_error(p->
ssl, r0);
791 if (r1 != SSL_ERROR_WANT_READ && r1 != SSL_ERROR_WANT_WRITE && r1 != SSL_ERROR_ZERO_RETURN) {
801 if (SSL_is_init_finished(p->
ssl) != 1)
814 EVP_PKEY *pkey =
NULL;
818 if (!SSL_CTX_load_verify_locations(p->
ctx,
c->ca_file,
NULL))
823 ret = SSL_CTX_use_certificate_chain_file(p->
ctx,
c->cert_file);
832 if (SSL_CTX_use_certificate(p->
ctx, cert) != 1) {
844 ret = SSL_CTX_use_PrivateKey_file(p->
ctx,
c->key_file, SSL_FILETYPE_PEM);
853 if (SSL_CTX_use_PrivateKey(p->
ctx, pkey) != 1) {
878 const char* ciphers =
"ALL";
883 const char*
profiles =
"SRTP_AES128_CM_SHA1_80";
885 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
886 const char*
curves =
"X25519:P-256:P-384:P-521";
887 #elif OPENSSL_VERSION_NUMBER >= 0x10002000L
888 const char*
curves =
"P-256:P-384:P-521";
891 #if OPENSSL_VERSION_NUMBER < 0x10002000L
892 p->
ctx = SSL_CTX_new(DTLSv1_method());
894 p->
ctx = SSL_CTX_new(DTLS_method());
901 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
903 if (SSL_CTX_set1_curves_list(p->
ctx,
curves) != 1) {
911 #if OPENSSL_VERSION_NUMBER < 0x10100000L // v1.1.x
912 #if OPENSSL_VERSION_NUMBER < 0x10002000L // v1.0.2
914 SSL_CTX_set_tmp_ecdh(p->
ctx, p->dtls_eckey);
916 SSL_CTX_set_ecdh_auto(p->
ctx, 1);
924 if (SSL_CTX_set_cipher_list(p->
ctx, ciphers) != 1) {
937 SSL_CTX_set_verify_depth(p->
ctx, 4);
939 SSL_CTX_set_read_ahead(p->
ctx, 1);
941 if (SSL_CTX_set_tlsext_use_srtp(p->
ctx,
profiles)) {
942 av_log(p,
AV_LOG_ERROR,
"TLS: Init SSL_CTX_set_tlsext_use_srtp failed, profiles=%s, %s\n",
956 SSL_set_ex_data(p->
ssl, 0, p);
962 SSL_set_options(p->
ssl, SSL_OP_NO_QUERY_MTU);
964 #if OPENSSL_VERSION_NUMBER >= 0x100010b0L
977 c->listen ? SSL_set_accept_state(p->
ssl) : SSL_set_connect_state(p->
ssl);
1014 SSL_CTX_free(
ctx->ctx);
1018 #if OPENSSL_VERSION_NUMBER < 0x30000000L
1019 EC_KEY_free(
ctx->dtls_eckey);
1030 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1042 p->
ctx = SSL_CTX_new(
c->listen ? SSLv23_server_method() : SSLv23_client_method());
1048 SSL_CTX_set_options(p->
ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
1054 SSL_CTX_set_verify(p->
ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
NULL);
1055 p->
ssl = SSL_new(p->
ctx);
1061 SSL_set_ex_data(p->
ssl, 0, p);
1064 if (!
c->listen && !
c->numerichost)
1065 SSL_set_tlsext_host_name(p->
ssl,
c->host);
1066 ret =
c->listen ? SSL_accept(p->
ssl) : SSL_connect(p->
ssl);
1071 }
else if (
ret < 0) {
1085 URLContext *uc =
c->tls_shared.is_dtls ?
c->tls_shared.udp
1086 :
c->tls_shared.tcp;
1102 URLContext *uc =
c->tls_shared.is_dtls ?
c->tls_shared.udp
1103 :
c->tls_shared.tcp;
1108 ret = SSL_write(
c->ssl, buf,
size);