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 #if OPENSSL_VERSION_NUMBER < 0x10002000L
419 BIO *mem = BIO_new_mem_buf((
void *)pem_str, -1);
421 BIO *mem = BIO_new_mem_buf(pem_str, -1);
428 EVP_PKEY *pkey =
NULL;
437 is_priv ?
"private" :
"public");
452 #if OPENSSL_VERSION_NUMBER < 0x10002000L
453 BIO *mem = BIO_new_mem_buf((
void *)pem_str, -1);
455 BIO *mem = BIO_new_mem_buf(pem_str, -1);
479 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
495 int r2 = ERR_get_error();
497 ERR_error_string_n(r2,
ctx->error_message,
sizeof(
ctx->error_message));
499 ctx->error_message[0] =
'\0';
502 return ctx->error_message;
508 c->tls_shared.udp = udp;
515 const char*
dst =
"EXTRACTOR-dtls_srtp";
518 ret = SSL_export_keying_material(
c->ssl, dtls_srtp_materials, materials_sz,
530 return c->tls_shared.state;
538 #if OPENSSL_VERSION_NUMBER < 0x10100000L
546 #include <openssl/crypto.h>
550 static void openssl_lock(
int mode,
int type,
const char *file,
int line)
552 if (
mode & CRYPTO_LOCK)
557 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
558 static unsigned long openssl_thread_id(
void)
560 return (intptr_t) pthread_self();
570 SSL_load_error_strings();
572 if (!CRYPTO_get_locking_callback()) {
575 if (!openssl_mutexes) {
580 for (
i = 0;
i < CRYPTO_num_locks();
i++)
582 CRYPTO_set_locking_callback(openssl_lock);
583 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
584 CRYPTO_set_id_callback(openssl_thread_id);
601 if (CRYPTO_get_locking_callback() == openssl_lock) {
603 CRYPTO_set_locking_callback(
NULL);
604 for (
i = 0;
i < CRYPTO_num_locks();
i++)
619 int err = SSL_get_error(
c->ssl,
ret);
620 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
623 while ((e = ERR_get_error()) != 0) {
642 SSL_shutdown(
c->ssl);
646 SSL_CTX_free(
c->ctx);
648 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
649 if (
c->url_bio_method)
650 BIO_meth_free(
c->url_bio_method);
652 #if OPENSSL_VERSION_NUMBER < 0x10100000L
660 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
662 BIO_set_data(
b,
NULL);
677 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
678 #define GET_BIO_DATA(x) BIO_get_data(x)
680 #define GET_BIO_DATA(x) (x)->ptr
686 int ret =
ffurl_read(
c->tls_shared.is_dtls ?
c->tls_shared.udp :
c->tls_shared.tcp, buf,
len);
689 BIO_clear_retry_flags(
b);
693 BIO_set_retry_read(
b);
705 BIO_clear_retry_flags(
b);
709 BIO_set_retry_write(
b);
717 if (cmd == BIO_CTRL_FLUSH) {
718 BIO_clear_retry_flags(
b);
729 #if OPENSSL_VERSION_NUMBER < 0x1010000fL
731 .type = BIO_TYPE_SOURCE_SINK,
732 .name =
"urlprotocol bio",
747 #if OPENSSL_VERSION_NUMBER >= 0x1010000fL
748 p->url_bio_method = BIO_meth_new(BIO_TYPE_SOURCE_SINK,
"urlprotocol bio");
755 bio = BIO_new(p->url_bio_method);
756 BIO_set_data(bio, p);
761 SSL_set_bio(p->
ssl, bio, bio);
765 const char *method =
"undefined";
768 if (where & SSL_ST_CONNECT) {
769 method =
"SSL_connect";
770 }
else if (where & SSL_ST_ACCEPT)
771 method =
"SSL_accept";
773 if (where & SSL_CB_LOOP) {
775 method, SSL_state_string(ssl), SSL_state_string_long(ssl), where,
ret);
776 }
else if (where & SSL_CB_ALERT) {
777 method = (where & SSL_CB_READ) ?
"read":
"write";
779 method, SSL_state_string(ssl), SSL_state_string_long(ssl), where,
ret);
797 r0 = SSL_do_handshake(p->
ssl);
798 r1 = SSL_get_error(p->
ssl, r0);
800 if (r1 != SSL_ERROR_WANT_READ && r1 != SSL_ERROR_WANT_WRITE && r1 != SSL_ERROR_ZERO_RETURN) {
810 if (SSL_is_init_finished(p->
ssl) != 1)
823 EVP_PKEY *pkey =
NULL;
827 if (!SSL_CTX_load_verify_locations(p->
ctx,
c->ca_file,
NULL))
832 ret = SSL_CTX_use_certificate_chain_file(p->
ctx,
c->cert_file);
841 if (SSL_CTX_use_certificate(p->
ctx, cert) != 1) {
853 ret = SSL_CTX_use_PrivateKey_file(p->
ctx,
c->key_file, SSL_FILETYPE_PEM);
862 if (SSL_CTX_use_PrivateKey(p->
ctx, pkey) != 1) {
887 const char* ciphers =
"ALL";
888 #if OPENSSL_VERSION_NUMBER < 0x10002000L // v1.0.2
889 EC_KEY *ec_key =
NULL;
895 const char*
profiles =
"SRTP_AES128_CM_SHA1_80";
897 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
898 const char*
curves =
"X25519:P-256:P-384:P-521";
899 #elif OPENSSL_VERSION_NUMBER >= 0x10002000L
900 const char*
curves =
"P-256:P-384:P-521";
903 #if OPENSSL_VERSION_NUMBER < 0x10002000L
904 p->
ctx = SSL_CTX_new(DTLSv1_method());
906 p->
ctx = SSL_CTX_new(DTLS_method());
913 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
915 if (SSL_CTX_set1_curves_list(p->
ctx,
curves) != 1) {
927 if (SSL_CTX_set_cipher_list(p->
ctx, ciphers) != 1) {
936 #if OPENSSL_VERSION_NUMBER < 0x10100000L // v1.1.x
937 #if OPENSSL_VERSION_NUMBER < 0x10002000L // v1.0.2
939 ec_key = EVP_PKEY_get1_EC_KEY(p->
pkey);
941 SSL_CTX_set_tmp_ecdh(p->
ctx, ec_key);
943 SSL_CTX_set_ecdh_auto(p->
ctx, 1);
951 SSL_CTX_set_verify_depth(p->
ctx, 4);
953 SSL_CTX_set_read_ahead(p->
ctx, 1);
955 if (SSL_CTX_set_tlsext_use_srtp(p->
ctx,
profiles)) {
956 av_log(p,
AV_LOG_ERROR,
"TLS: Init SSL_CTX_set_tlsext_use_srtp failed, profiles=%s, %s\n",
970 SSL_set_ex_data(p->
ssl, 0, p);
976 SSL_set_options(p->
ssl, SSL_OP_NO_QUERY_MTU);
978 #if OPENSSL_VERSION_NUMBER >= 0x100010b0L
991 c->listen ? SSL_set_accept_state(p->
ssl) : SSL_set_connect_state(p->
ssl);
1018 #if OPENSSL_VERSION_NUMBER < 0x10002000L // v1.0.2
1019 EC_KEY_free(ec_key);
1031 SSL_CTX_free(
ctx->ctx);
1035 EVP_PKEY_free(
ctx->pkey);
1045 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1057 p->
ctx = SSL_CTX_new(
c->listen ? SSLv23_server_method() : SSLv23_client_method());
1063 SSL_CTX_set_options(p->
ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
1069 SSL_CTX_set_verify(p->
ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
NULL);
1070 p->
ssl = SSL_new(p->
ctx);
1076 SSL_set_ex_data(p->
ssl, 0, p);
1079 if (!
c->listen && !
c->numerichost)
1080 SSL_set_tlsext_host_name(p->
ssl,
c->host);
1081 ret =
c->listen ? SSL_accept(p->
ssl) : SSL_connect(p->
ssl);
1086 }
else if (
ret < 0) {
1100 URLContext *uc =
c->tls_shared.is_dtls ?
c->tls_shared.udp
1101 :
c->tls_shared.tcp;
1117 URLContext *uc =
c->tls_shared.is_dtls ?
c->tls_shared.udp
1118 :
c->tls_shared.tcp;
1123 ret = SSL_write(
c->ssl, buf,
size);