|
5 | 5 | #include "ngx_http_apisix_module.h" |
6 | 6 |
|
7 | 7 |
|
| 8 | +#define NGX_HTTP_APISIX_SSL_ENC 1 |
| 9 | +#define NGX_HTTP_APISIX_SSL_SIGN 2 |
| 10 | + |
| 11 | + |
8 | 12 | static ngx_str_t remote_addr = ngx_string("remote_addr"); |
9 | 13 | static ngx_str_t remote_port = ngx_string("remote_port"); |
10 | 14 | static ngx_str_t realip_remote_addr = ngx_string("realip_remote_addr"); |
@@ -640,3 +644,137 @@ ngx_http_apisix_is_body_filter_by_lua_skipped(ngx_http_request_t *r) |
640 | 644 |
|
641 | 645 | return 0; |
642 | 646 | } |
| 647 | + |
| 648 | + |
| 649 | +int |
| 650 | +ngx_http_apisix_set_gm_cert(ngx_http_request_t *r, void *cdata, char **err, ngx_flag_t type) |
| 651 | +{ |
| 652 | +#ifndef TONGSUO_VERSION_NUMBER |
| 653 | + |
| 654 | + *err = "only Tongsuo supported"; |
| 655 | + return NGX_ERROR; |
| 656 | + |
| 657 | +#else |
| 658 | + int i; |
| 659 | + X509 *x509 = NULL; |
| 660 | + ngx_ssl_conn_t *ssl_conn; |
| 661 | + STACK_OF(X509) *chain = cdata; |
| 662 | + |
| 663 | + if (r->connection == NULL || r->connection->ssl == NULL) { |
| 664 | + *err = "bad request"; |
| 665 | + return NGX_ERROR; |
| 666 | + } |
| 667 | + |
| 668 | + ssl_conn = r->connection->ssl->connection; |
| 669 | + if (ssl_conn == NULL) { |
| 670 | + *err = "bad ssl conn"; |
| 671 | + return NGX_ERROR; |
| 672 | + } |
| 673 | + |
| 674 | + if (sk_X509_num(chain) < 1) { |
| 675 | + *err = "invalid certificate chain"; |
| 676 | + goto failed; |
| 677 | + } |
| 678 | + |
| 679 | + x509 = sk_X509_value(chain, 0); |
| 680 | + if (x509 == NULL) { |
| 681 | + *err = "sk_X509_value() failed"; |
| 682 | + goto failed; |
| 683 | + } |
| 684 | + |
| 685 | + if (type == NGX_HTTP_APISIX_SSL_ENC) { |
| 686 | + if (SSL_use_enc_certificate(ssl_conn, x509) == 0) { |
| 687 | + *err = "SSL_use_enc_certificate() failed"; |
| 688 | + goto failed; |
| 689 | + } |
| 690 | + } else { |
| 691 | + if (SSL_use_sign_certificate(ssl_conn, x509) == 0) { |
| 692 | + *err = "SSL_use_sign_certificate() failed"; |
| 693 | + goto failed; |
| 694 | + } |
| 695 | + } |
| 696 | + |
| 697 | + x509 = NULL; |
| 698 | + |
| 699 | + /* read rest of the chain */ |
| 700 | + |
| 701 | + for (i = 1; i < sk_X509_num(chain); i++) { |
| 702 | + |
| 703 | + x509 = sk_X509_value(chain, i); |
| 704 | + if (x509 == NULL) { |
| 705 | + *err = "sk_X509_value() failed"; |
| 706 | + goto failed; |
| 707 | + } |
| 708 | + |
| 709 | + if (SSL_add1_chain_cert(ssl_conn, x509) == 0) { |
| 710 | + *err = "SSL_add1_chain_cert() failed"; |
| 711 | + goto failed; |
| 712 | + } |
| 713 | + } |
| 714 | + |
| 715 | + *err = NULL; |
| 716 | + return NGX_OK; |
| 717 | + |
| 718 | +failed: |
| 719 | + |
| 720 | + ERR_clear_error(); |
| 721 | + |
| 722 | + return NGX_ERROR; |
| 723 | + |
| 724 | +#endif |
| 725 | +} |
| 726 | + |
| 727 | + |
| 728 | +int |
| 729 | +ngx_http_apisix_set_gm_priv_key(ngx_http_request_t *r, |
| 730 | + void *cdata, char **err, ngx_flag_t type) |
| 731 | +{ |
| 732 | +#ifndef TONGSUO_VERSION_NUMBER |
| 733 | + |
| 734 | + *err = "only Tongsuo supported"; |
| 735 | + return NGX_ERROR; |
| 736 | + |
| 737 | +#else |
| 738 | + |
| 739 | + EVP_PKEY *pkey = NULL; |
| 740 | + ngx_ssl_conn_t *ssl_conn; |
| 741 | + |
| 742 | + if (r->connection == NULL || r->connection->ssl == NULL) { |
| 743 | + *err = "bad request"; |
| 744 | + return NGX_ERROR; |
| 745 | + } |
| 746 | + |
| 747 | + ssl_conn = r->connection->ssl->connection; |
| 748 | + if (ssl_conn == NULL) { |
| 749 | + *err = "bad ssl conn"; |
| 750 | + return NGX_ERROR; |
| 751 | + } |
| 752 | + |
| 753 | + pkey = cdata; |
| 754 | + if (pkey == NULL) { |
| 755 | + *err = "invalid private key failed"; |
| 756 | + goto failed; |
| 757 | + } |
| 758 | + |
| 759 | + if (type == NGX_HTTP_APISIX_SSL_ENC) { |
| 760 | + if (SSL_use_enc_PrivateKey(ssl_conn, pkey) == 0) { |
| 761 | + *err = "SSL_use_enc_PrivateKey() failed"; |
| 762 | + goto failed; |
| 763 | + } |
| 764 | + } else { |
| 765 | + if (SSL_use_sign_PrivateKey(ssl_conn, pkey) == 0) { |
| 766 | + *err = "SSL_use_sign_PrivateKey() failed"; |
| 767 | + goto failed; |
| 768 | + } |
| 769 | + } |
| 770 | + |
| 771 | + return NGX_OK; |
| 772 | + |
| 773 | +failed: |
| 774 | + |
| 775 | + ERR_clear_error(); |
| 776 | + |
| 777 | + return NGX_ERROR; |
| 778 | + |
| 779 | +#endif |
| 780 | +} |
0 commit comments