| 1 |
/* |
| 2 |
* tls.c - tls secure connection interface |
| 3 |
* |
| 4 |
* Copyright (c) 2011 Kirill Zorin <[email protected]> |
| 5 |
* Copyright (c) 2018 YOKOTA Hiroshi |
| 6 |
* Copyright (c) 2018-2024 Shiro Kawai <[email protected]> |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions |
| 10 |
* are met: |
| 11 |
* |
| 12 |
* 1. Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* |
| 15 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 16 |
* notice, this list of conditions and the following disclaimer in the |
| 17 |
* documentation and/or other materials provided with the distribution. |
| 18 |
* |
| 19 |
* 3. Neither the name of the authors nor the names of its contributors |
| 20 |
* may be used to endorse or promote products derived from this |
| 21 |
* software without specific prior written permission. |
| 22 |
* |
| 23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 26 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 27 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 28 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 29 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 30 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 31 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 32 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 |
*/ |
| 35 |
|
| 36 |
#include "gauche-tls.h" |
| 37 |
#include <gauche/extend.h> |
| 38 |
|
| 39 |
/* |
| 40 |
* Class |
| 41 |
*/ |
| 42 |
|
| 43 |
static void tls_print(ScmObj obj, ScmPort* port, ScmWriteContext* ctx); |
| 44 |
|
| 45 |
static ScmClass *tlsclass_cpa[] = { |
| 46 |
SCM_CLASS_STATIC_PTR(Scm_TLSClass), |
| 47 |
SCM_CLASS_STATIC_PTR(Scm_ConnectionClass), |
| 48 |
SCM_CLASS_STATIC_PTR(Scm_TopClass), |
| 49 |
NULL |
| 50 |
}; |
| 51 |
|
| 52 |
SCM_DEFINE_BUILTIN_CLASS(Scm_TLSClass, tls_print, NULL, NULL, NULL, |
| 53 |
tlsclass_cpa+1); |
| 54 |
|
| 55 |
static void tls_print(ScmObj obj, ScmPort* port, |
| 56 |
ScmWriteContext* ctx SCM_UNUSED) |
| 57 |
{ |
| 58 |
Scm_Printf(port, "#<%A", Scm_ShortClassName(SCM_CLASS_OF(obj))); |
| 59 |
/* at the moment there's not much to print, so we leave this hole |
| 60 |
for future development. */ |
| 61 |
Scm_Printf(port, ">"); |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* Global stuff |
| 66 |
*/ |
| 67 |
|
| 68 |
static ScmPrimitiveParameter *ca_bundle_path; |
| 69 |
static ScmPrimitiveParameter *default_tls_class; |
| 70 |
static ScmObj s_system; |
| 71 |
static ScmObj s_check; |
| 72 |
static ScmObj s_tcp; |
| 73 |
static ScmObj s_udp; |
| 74 |
static ScmObj k_options; |
| 75 |
static ScmObj k_num_sessions; |
| 76 |
|
| 77 |
/* |
| 78 |
* Common operations |
| 79 |
*/ |
| 80 |
|
| 81 |
ScmObj Scm_MakeTLS(ScmObj initargs) |
| 82 |
{ |
| 83 |
ScmObj klass = Scm_PrimitiveParameterRef(Scm_VM(), default_tls_class); |
| 84 |
if (!SCM_CLASSP(klass) || !Scm_SubtypeP(SCM_CLASS(klass), &Scm_TLSClass)) { |
| 85 |
Scm_Error("default-tls-class needs to be a subclass of <tls>, " |
| 86 |
"but got: %S", klass); |
| 87 |
} |
| 88 |
return Scm_Allocate(SCM_CLASS(klass), initargs); |
| 89 |
} |
| 90 |
|
| 91 |
ScmObj Scm_TLSClose(ScmTLS* t) |
| 92 |
{ |
| 93 |
/* NB: pots are already closed */ |
| 94 |
|
| 95 |
/* Extra parenthesis to keep close() from being macro-expanded by |
| 96 |
windows compatibility macro. */ |
| 97 |
return (t->close)(t); |
| 98 |
} |
| 99 |
|
| 100 |
ScmObj Scm_TLSLoadCertificate(ScmTLS* t, const char *filename) |
| 101 |
{ |
| 102 |
if (!t->loadCertificate) { |
| 103 |
Scm_Error("tls-load-certificate is not supported on %S", t); |
| 104 |
} |
| 105 |
return t->loadCertificate(t, filename); |
| 106 |
} |
| 107 |
|
| 108 |
ScmObj Scm_TLSLoadPrivateKey(ScmTLS* t, |
| 109 |
const char *filename, |
| 110 |
const char *password) /* can be NULL */ |
| 111 |
{ |
| 112 |
if (!t->loadPrivateKey) { |
| 113 |
Scm_Error("tls-load-private-key is not supported on %S", t); |
| 114 |
} |
| 115 |
return t->loadPrivateKey(t, filename, password); |
| 116 |
} |
| 117 |
|
| 118 |
static int parse_proto(ScmObj proto) |
| 119 |
{ |
| 120 |
if (SCM_EQ(proto, s_tcp)) return SCM_TLS_PROTO_TCP; |
| 121 |
if (SCM_EQ(proto, s_udp)) return SCM_TLS_PROTO_UDP; |
| 122 |
Scm_Error("proto must be either 'tcp or 'udp, but got: %S", proto); |
| 123 |
return 0; /* dummy */ |
| 124 |
} |
| 125 |
|
| 126 |
ScmObj Scm_TLSConnect(ScmTLS *t, const char *host, const char *port, |
| 127 |
ScmObj proto) /* symbol */ |
| 128 |
{ |
| 129 |
return t->connect(t, host, port, parse_proto(proto)); |
| 130 |
} |
| 131 |
|
| 132 |
ScmObj Scm_TLSBind(ScmTLS *t, const char *ip, const char *port, |
| 133 |
ScmObj proto) /* symbol */ |
| 134 |
{ |
| 135 |
return t->bind(t, ip, port, parse_proto(proto)); |
| 136 |
} |
| 137 |
|
| 138 |
ScmObj Scm_TLSAccept(ScmTLS *t) |
| 139 |
{ |
| 140 |
return t->accept(t); |
| 141 |
} |
| 142 |
|
| 143 |
ScmObj Scm_TLSRead(ScmTLS* t) |
| 144 |
{ |
| 145 |
/* Extra parenthesis to keep read() from being macro-expanded by |
| 146 |
windows compatibility macro. */ |
| 147 |
return (t->read)(t); |
| 148 |
} |
| 149 |
|
| 150 |
ScmObj Scm_TLSWrite(ScmTLS* t, ScmObj msg) |
| 151 |
{ |
| 152 |
/* Extra parenthesis to keep write() from being macro-expanded by |
| 153 |
windows compatibility macro. */ |
| 154 |
return (t->write)(t, msg); |
| 155 |
} |
| 156 |
|
| 157 |
ScmObj Scm_TLSInputPort(ScmTLS* t) |
| 158 |
{ |
| 159 |
return t->in_port; |
| 160 |
} |
| 161 |
|
| 162 |
ScmObj Scm_TLSOutputPort(ScmTLS* t) |
| 163 |
{ |
| 164 |
return t->out_port; |
| 165 |
} |
| 166 |
|
| 167 |
ScmObj Scm_TLSInputPortSet(ScmTLS* t, ScmObj port) |
| 168 |
{ |
| 169 |
t->in_port = port; |
| 170 |
return SCM_UNDEFINED; |
| 171 |
} |
| 172 |
|
| 173 |
ScmObj Scm_TLSOutputPortSet(ScmTLS* t, ScmObj port) |
| 174 |
{ |
| 175 |
t->out_port = port; |
| 176 |
return SCM_UNDEFINED; |
| 177 |
} |
| 178 |
|
| 179 |
u_long Scm_TLSPoll(ScmTLS *t, u_long rwflags, ScmTimeSpec *timeout) |
| 180 |
{ |
| 181 |
return t->poll(t, rwflags, timeout); |
| 182 |
} |
| 183 |
|
| 184 |
ScmObj Scm_TLSGetConnectionAddress(ScmTLS *t, int who) |
| 185 |
{ |
| 186 |
if (t->getConnectionAddress) { |
| 187 |
return t->getConnectionAddress(t, who); |
| 188 |
} else { |
| 189 |
return SCM_FALSE; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/* |
| 194 |
* Debug level management |
| 195 |
*/ |
| 196 |
#define MAX_DEBUG_LEVEL_SETTERS 4 |
| 197 |
static ScmObj debug_level_setters[MAX_DEBUG_LEVEL_SETTERS]; |
| 198 |
static int debug_level_setter_count = 0; |
| 199 |
static int debug_level = 0; |
| 200 |
static ScmInternalMutex debug_level_setter_mutex; |
| 201 |
|
| 202 |
void Scm_TLSSetDebugLevel(int level) |
| 203 |
{ |
| 204 |
if (level < 0) level = 0; |
| 205 |
if (level > 9) level = 9; |
| 206 |
(void)SCM_INTERNAL_MUTEX_LOCK(debug_level_setter_mutex); |
| 207 |
debug_level = level; |
| 208 |
(void)SCM_INTERNAL_MUTEX_UNLOCK(debug_level_setter_mutex); |
| 209 |
for (int i=0; i < debug_level_setter_count; i++) { |
| 210 |
Scm_ApplyRec1(debug_level_setters[i], SCM_MAKE_INT(level)); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
void Scm_TLSRegisterDebugLevelCallback(ScmObj setter) |
| 215 |
{ |
| 216 |
int overflow = FALSE; |
| 217 |
|
| 218 |
(void)SCM_INTERNAL_MUTEX_LOCK(debug_level_setter_mutex); |
| 219 |
if (debug_level_setter_count >= MAX_DEBUG_LEVEL_SETTERS) { |
| 220 |
overflow = TRUE; |
| 221 |
} else { |
| 222 |
debug_level_setters[debug_level_setter_count++] = setter; |
| 223 |
} |
| 224 |
(void)SCM_INTERNAL_MUTEX_UNLOCK(debug_level_setter_mutex); |
| 225 |
if (overflow) { |
| 226 |
Scm_Error("[internal] Too many TLS debug level callbacks"); |
| 227 |
} |
| 228 |
Scm_ApplyRec1(setter, SCM_MAKE_INT(debug_level)); |
| 229 |
} |
| 230 |
|
| 231 |
#if !HAVE_WINCRYPT_H |
| 232 |
#include "in_gauche_cacert_path.c" |
| 233 |
#endif |
| 234 |
|
| 235 |
int Scm_TLSSystemCABundleAvailable(void) |
| 236 |
{ |
| 237 |
#if HAVE_WINCRYPT_H |
| 238 |
/* On Windows, we can count on system's cert store. */ |
| 239 |
return TRUE; |
| 240 |
#else /* !HAVE_WINCRYPT_H */ |
| 241 |
static ScmObj available = SCM_UNDEFINED; |
| 242 |
|
| 243 |
if (SCM_UNDEFINEDP(available)) { |
| 244 |
const char *cacert_paths[] = { |
| 245 |
SYSTEM_CA_CERT_PATHS, |
| 246 |
in_gauche_cacert_path(), |
| 247 |
NULL |
| 248 |
}; |
| 249 |
|
| 250 |
const char **p = cacert_paths; |
| 251 |
for (;*p != NULL; p++) { |
| 252 |
if (access(*p, R_OK) == 0) { |
| 253 |
available = SCM_TRUE; |
| 254 |
break; |
| 255 |
} |
| 256 |
} |
| 257 |
if (*p == NULL) available = SCM_FALSE; |
| 258 |
} |
| 259 |
return SCM_BOOL_VALUE(available); |
| 260 |
#endif /* !HAVE_WINCRYPT_H */ |
| 261 |
} |
| 262 |
|
| 263 |
static inline ScmObj default_ca_bundle(void) |
| 264 |
{ |
| 265 |
#if defined(GAUCHE_CA_BUNDLE_FILE) |
| 266 |
return SCM_MAKE_STR(GAUCHE_CA_BUNDLE); |
| 267 |
#elif defined(GAUCHE_CA_BUNDLE_SYSTEM) |
| 268 |
return s_system; |
| 269 |
#elif defined(GAUCHE_CA_BUNDLE_CHECK) |
| 270 |
return s_check; |
| 271 |
#else |
| 272 |
return SCM_FALSE; |
| 273 |
#endif |
| 274 |
} |
| 275 |
|
| 276 |
void Scm_Init_tls(ScmModule *mod) |
| 277 |
{ |
| 278 |
Scm_InitStaticClass(&Scm_TLSClass, "<tls>", mod, NULL, 0); |
| 279 |
|
| 280 |
s_system = SCM_INTERN("system"); |
| 281 |
s_check = SCM_INTERN("check"); |
| 282 |
s_tcp = SCM_INTERN("tcp"); |
| 283 |
s_udp = SCM_INTERN("udp"); |
| 284 |
|
| 285 |
/* Set default-tls-class to be lazy (see tls.scm for the reason) */ |
| 286 |
default_tls_class = |
| 287 |
Scm_BindPrimitiveParameter(mod, "default-tls-class", |
| 288 |
SCM_FALSE, |
| 289 |
SCM_PARAMETER_LAZY); |
| 290 |
|
| 291 |
/* tls-ca-bundle-path paramter. It can be 'check, 'system, #f, |
| 292 |
or a string pathname. |
| 293 |
If it is 'check, we check the system ca-bundle at the initialization |
| 294 |
time (see tls.scm) and switch it to either 'system or #f. So the |
| 295 |
user will never see 'check. */ |
| 296 |
ca_bundle_path = |
| 297 |
Scm_BindPrimitiveParameter(mod, "tls-ca-bundle-path", |
| 298 |
default_ca_bundle(), 0); |
| 299 |
k_options = SCM_MAKE_KEYWORD("options"); |
| 300 |
k_num_sessions = SCM_MAKE_KEYWORD("num-sessions"); |
| 301 |
|
| 302 |
SCM_INTERNAL_MUTEX_INIT(debug_level_setter_mutex); |
| 303 |
} |