|
22 | 22 | #include <openssl/dh.h>
|
23 | 23 | #include <openssl/bn.h>
|
24 | 24 | #include <openssl/engine.h>
|
| 25 | +#include <openssl/x509v3.h> |
25 | 26 | #include <internal/cryptlib.h>
|
26 | 27 |
|
27 | 28 | static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL *s, PACKET *pkt);
|
@@ -1728,6 +1729,28 @@ MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
|
1728 | 1729 | goto err;
|
1729 | 1730 | }
|
1730 | 1731 |
|
| 1732 | +#ifndef OPENSSL_NO_SM2 |
| 1733 | + /* |
| 1734 | + * To use the cipher suites TLS_SM4_GCM_SM3 and TLS_SM4_CCM_SM3, |
| 1735 | + * RFC 8998 demand that: |
| 1736 | + * For the key_share extension, a KeyShareEntry with SM2-related |
| 1737 | + * values MUST be added. |
| 1738 | + */ |
| 1739 | + if (SSL_IS_TLS13(s) && s->enable_sm_tls13_strict == 1) { |
| 1740 | + const SSL_CIPHER *cipher = s->s3->tmp.new_cipher; |
| 1741 | + |
| 1742 | + if (cipher->id == TLS1_3_CK_SM4_GCM_SM3 |
| 1743 | + || cipher->id == TLS1_3_CK_SM4_CCM_SM3) { |
| 1744 | + if (s->s3->group_id != TLSEXT_curve_SM2) { |
| 1745 | + SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 1746 | + SSL_F_TLS_PROCESS_SERVER_HELLO, |
| 1747 | + SSL_R_BAD_KEY_SHARE); |
| 1748 | + goto err; |
| 1749 | + } |
| 1750 | + } |
| 1751 | + } |
| 1752 | +#endif |
| 1753 | + |
1731 | 1754 | #ifndef OPENSSL_NO_SCTP
|
1732 | 1755 | if (SSL_IS_DTLS(s) && s->hit) {
|
1733 | 1756 | unsigned char sctpauthkey[64];
|
@@ -2030,6 +2053,48 @@ MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
|
2030 | 2053 | goto err;
|
2031 | 2054 | }
|
2032 | 2055 | }
|
| 2056 | + |
| 2057 | +#ifndef OPENSSL_NO_SM2 |
| 2058 | + /* |
| 2059 | + * RFC 8998 requires that |
| 2060 | + * The public key in the certificate MUST be a valid SM2 public key. |
| 2061 | + * The signature algorithm used by the CA to sign the current |
| 2062 | + * certificate MUST be "sm2sig_sm3". |
| 2063 | + * The certificate MUST be capable of signing; e.g., the digitalSignature |
| 2064 | + * bit of X.509's Key Usage extension is set. |
| 2065 | + */ |
| 2066 | + if (SSL_IS_TLS13(s) && s->enable_sm_tls13_strict == 1) { |
| 2067 | + const SSL_CIPHER *cipher = s->s3->tmp.new_cipher; |
| 2068 | + |
| 2069 | + if (cipher->id == TLS1_3_CK_SM4_GCM_SM3 |
| 2070 | + || cipher->id == TLS1_3_CK_SM4_CCM_SM3) { |
| 2071 | + if (EVP_PKEY_id(pkey) != EVP_PKEY_SM2) { |
| 2072 | + x = NULL; |
| 2073 | + SSLfatal(s, SSL_AD_BAD_CERTIFICATE, |
| 2074 | + SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
| 2075 | + SSL_R_WRONG_CERTIFICATE_TYPE); |
| 2076 | + goto err; |
| 2077 | + } |
| 2078 | + |
| 2079 | + if (X509_get_signature_nid(x) != NID_SM2_with_SM3) { |
| 2080 | + x = NULL; |
| 2081 | + SSLfatal(s, SSL_AD_BAD_CERTIFICATE, |
| 2082 | + SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
| 2083 | + SSL_R_BAD_CERTIFICATE_SIGNATURE_TYPE); |
| 2084 | + goto err; |
| 2085 | + } |
| 2086 | + |
| 2087 | + if ((X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE) == 0) { |
| 2088 | + x = NULL; |
| 2089 | + SSLfatal(s, SSL_AD_BAD_CERTIFICATE, |
| 2090 | + SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
| 2091 | + SSL_R_BAD_CERTIFICATE_USAGE); |
| 2092 | + goto err; |
| 2093 | + } |
| 2094 | + } |
| 2095 | + } |
| 2096 | +#endif |
| 2097 | + |
2033 | 2098 | s->session->peer_type = certidx;
|
2034 | 2099 |
|
2035 | 2100 | X509_free(s->session->peer);
|
@@ -2587,6 +2652,41 @@ MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
|
2587 | 2652 | return MSG_PROCESS_ERROR;
|
2588 | 2653 | }
|
2589 | 2654 | OPENSSL_free(rawexts);
|
| 2655 | +#ifndef OPENSSL_NO_SM2 |
| 2656 | + /* |
| 2657 | + * RFC 8998 requires that |
| 2658 | + * if the server chooses TLS_SM4_GCM_SM3 or TLS_SM4_CCM_SM3, |
| 2659 | + * the only valid signature algorithm present in |
| 2660 | + * "signature_algorithms" extension MUST be "sm2sig_sm3". |
| 2661 | + */ |
| 2662 | + if (s->enable_sm_tls13_strict == 1) { |
| 2663 | + const SSL_CIPHER *cipher = s->s3->tmp.new_cipher; |
| 2664 | + |
| 2665 | + if (cipher->id == TLS1_3_CK_SM4_GCM_SM3 |
| 2666 | + || cipher->id == TLS1_3_CK_SM4_CCM_SM3) { |
| 2667 | + |
| 2668 | + if (s->s3->tmp.peer_sigalgslen > 0 |
| 2669 | + && (s->s3->tmp.peer_sigalgslen != 1 |
| 2670 | + || s->s3->tmp.peer_sigalgs[0] != TLSEXT_SIGALG_sm2sig_sm3)) |
| 2671 | + { |
| 2672 | + SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 2673 | + SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
| 2674 | + SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 2675 | + return MSG_PROCESS_ERROR; |
| 2676 | + } |
| 2677 | + |
| 2678 | + if (s->s3->tmp.peer_cert_sigalgslen > 0 |
| 2679 | + && (s->s3->tmp.peer_cert_sigalgslen != 1 |
| 2680 | + || s->s3->tmp.peer_cert_sigalgs[0] != TLSEXT_SIGALG_sm2sig_sm3)) |
| 2681 | + { |
| 2682 | + SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 2683 | + SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
| 2684 | + SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
| 2685 | + return MSG_PROCESS_ERROR; |
| 2686 | + } |
| 2687 | + } |
| 2688 | + } |
| 2689 | +#endif |
2590 | 2690 | if (!tls1_process_sigalgs(s)) {
|
2591 | 2691 | SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
2592 | 2692 | SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
|
|
0 commit comments