1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15 #include "hitls_build.h"
16 #ifdef HITLS_TLS_FEATURE_SNI
17 #include <ctype.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include "securec.h"
21 #include "hitls_error.h"
22 #include "hitls_config.h"
23 #include "hitls_sni.h"
24 #include "session.h"
25 #include "tls_binlog_id.h"
26 #include "tls.h"
27 #include "hs.h"
28 #include "sni.h"
29
HITLS_GetServerName(const HITLS_Ctx * ctx,const int type)30 const char *HITLS_GetServerName(const HITLS_Ctx *ctx, const int type)
31 {
32 if (ctx == NULL || type != HITLS_SNI_HOSTNAME_TYPE) {
33 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16756, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "input null", 0, 0, 0, 0);
34 return NULL;
35 }
36 bool isClient = ctx->isClient;
37 bool isResume = ctx->negotiatedInfo.isResume;
38 uint16_t version = ctx->config.tlsConfig.maxVersion;
39 uint8_t *hostName = NULL;
40 uint32_t nameSize = 0u;
41 SESS_GetHostName(ctx->session, &nameSize, &hostName);
42
43 if (!isClient) {
44 /* Before Handshake */
45 if (ctx->state == CM_STATE_IDLE) {
46 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16757, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
47 "ctx->state is CM_STATE_IDLE", 0, 0, 0, 0);
48 return NULL;
49 }
50 /* During or after handshake */
51 /* TLS protocol version < TLS1.3 session resumption */
52 if ((version < HITLS_VERSION_TLS13 || version == HITLS_VERSION_DTLS12) && isResume && ctx->session != NULL) {
53 return (char *)hostName;
54 }
55 } else {
56 /* Before Handshake */
57 if (ctx->state == CM_STATE_IDLE) {
58 /* resume the session */
59 if (ctx->config.tlsConfig.serverName == NULL && ctx->session != NULL &&
60 (version < HITLS_VERSION_TLS13 || version == HITLS_VERSION_DTLS12)) {
61 return (char *)hostName;
62 }
63 /* resume non-session */
64 return (char *)ctx->config.tlsConfig.serverName;
65 } else {
66 /* During or after handshake */
67 /* resume the session */
68 if (ctx->session != NULL && (version < HITLS_VERSION_TLS13 || version == HITLS_VERSION_DTLS12)) {
69 return (char *)hostName;
70 }
71 /* resume non-session */
72 return (char *)ctx->config.tlsConfig.serverName;
73 }
74 }
75
76 return HS_GetServerName(ctx);
77 }
78
HITLS_GetServernameType(const HITLS_Ctx * ctx)79 int32_t HITLS_GetServernameType(const HITLS_Ctx *ctx)
80 {
81 int32_t ret = -1;
82 if (HITLS_GetServerName(ctx, HITLS_SNI_HOSTNAME_TYPE) != NULL) {
83 return HITLS_SNI_HOSTNAME_TYPE;
84 }
85 return ret;
86 }
87
88 /* Check whether the host names are the same */
SNI_StrcaseCmp(const char * s1,const char * s2)89 int32_t SNI_StrcaseCmp(const char *s1, const char *s2)
90 {
91 int32_t ret = -1;
92
93 if (s1 == NULL && s2 == NULL) {
94 return 0;
95 }
96 if (s1 == NULL || s2 == NULL) {
97 return ret;
98 }
99 const char *a = s1;
100 const char *b = s2;
101 int32_t len1 = (int32_t)strlen(s1);
102 int32_t len2 = (int32_t)strlen(s2);
103 if (len1 != len2) {
104 return ret;
105 }
106
107 while (tolower((int32_t)*a) == tolower((int32_t)*b)) {
108 if (*a == '\0') {
109 return 0;
110 }
111
112 a++;
113 b++;
114 }
115
116 return ret;
117 }
118 #endif /* HITLS_TLS_FEATURE_SNI */