Debian Codegraph @

ext/tls/gauche-tls.h

raw
1 /*
2 * gauche-tls.h - TLS secure connection interface
3 *
4 * Copyright (c) 2011 Kirill Zorin <[email protected]>
5 * 2018 YOKOTA Hiroshi
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. Neither the name of the authors nor the names of its contributors
19 * may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #ifndef GAUCHE_TLS_H
36 #define GAUCHE_TLS_H
37
38 #include <gauche.h>
39 #include <gauche/net.h>
40
41 #if defined(EXTTLS_EXPORTS)
42 #define LIBGAUCHE_EXT_BODY
43 #endif
44 #include <gauche/extern.h>
45 #include <gauche/class.h>
46
47 #define GAUCHE_CA_SYSTEM "system"
48
49 /* We try these paths to find ca certs on Unix-like platforms */
50 #define SYSTEM_CA_CERT_PATHS \
51 "/etc/ssl/certs/ca-certificates.crt", /* ubnutu */ \
52 "/usr/share/pki/ca-trust-source/ca-bundle.trust.crt", /* fedora */ \
53 "/etc/pki/tls/certs/ca-budle.crt", /* fedora (compat) */ \
54 "/usr/local/etc/openssl/cert.pem" /* osx homebrew openssl */
55
56 /* This is a 'success' code of AxTLS. mbedtls x509 routine returns 0
57 on success, which happens to be the same as SSL_OK.
58 We just need this in case we're configured with mbedtls and without axtls.
59 */
60 #ifndef SSL_OK
61 #define SSL_OK 0
62 #endif
63
64 #ifndef GAUCHE_USE_AXTLS
65 /* dummy symbols */
66 #define SSL_CLIENT_AUTHENTICATION 0x00010000
67 #define SSL_SERVER_VERIFY_LATER 0x00020000
68 #define SSL_NO_DEFAULT_KEY 0x00040000
69 #define SSL_DISPLAY_STATES 0x00080000
70 #define SSL_DISPLAY_BYTES 0x00100000
71 #define SSL_DISPLAY_CERTS 0x00200000
72 #define SSL_DISPLAY_RSA 0x00400000
73 #define SSL_CONNECT_IN_PARTS 0x00800000
74 #define SSL_OBJ_X509_CERT 1
75 #define SSL_OBJ_X509_CACERT 2
76 #define SSL_OBJ_RSA_KEY 3
77 #define SSL_OBJ_PKCS8 4
78 #define SSL_OBJ_PKCS12 5
79 #endif
80
81 /* 'proto' parameter */
82 enum {
83 SCM_TLS_PROTO_TCP,
84 SCM_TLS_PROTO_UDP
85 };
86
87 SCM_DECL_BEGIN
88
89 /* Common structure */
90
91 typedef struct ScmTLSRec ScmTLS;
92
93 struct ScmTLSRec {
94 SCM_HEADER;
95 ScmObj in_port;
96 ScmObj out_port;
97
98 ScmObj (*connect)(ScmTLS*, const char*, const char*, int);
99 ScmObj (*bind)(ScmTLS*, const char*, const char*, int);
100 ScmObj (*accept)(ScmTLS*);
101 ScmObj (*read)(ScmTLS*);
102 ScmObj (*write)(ScmTLS*, ScmObj);
103 ScmObj (*close)(ScmTLS*);
104 u_long (*poll)(ScmTLS*, u_long, ScmTimeSpec*);
105 ScmObj (*loadCertificate)(ScmTLS*, const char*);
106 ScmObj (*loadPrivateKey)(ScmTLS*, const char*, const char*);
107 ScmObj (*getConnectionAddress)(ScmTLS*, int);
108 void (*finalize)(ScmObj, void*);
109 };
110
111 SCM_CLASS_DECL(Scm_TLSClass);
112 #if defined(GAUCHE_USE_AXTLS)
113 SCM_CLASS_DECL(Scm_AxTLSClass);
114 #endif /*GAUCHE_USE_AXTLS*/
115
116 #define SCM_CLASS_TLS (&Scm_TLSClass)
117 #define SCM_TLS(obj) ((ScmTLS*)obj)
118 #define SCM_TLSP(obj) SCM_ISA(obj, SCM_CLASS_TLS)
119
120 extern ScmObj Scm_MakeTLS(ScmObj);
121 extern ScmObj Scm_TLSLoadCertificate(ScmTLS *t, const char *path);
122 extern ScmObj Scm_TLSLoadPrivateKey(ScmTLS *t, const char *path,
123 const char *password);
124 extern ScmObj Scm_TLSConnect(ScmTLS *t,
125 const char *host,
126 const char *port, /* number or service name */
127 ScmObj proto);
128
129 extern ScmObj Scm_TLSBind(ScmTLS *t,
130 const char *ip,
131 const char *port, /* numeric or service name */
132 ScmObj proto);
133 extern ScmObj Scm_TLSAccept(ScmTLS *t); /* returns connected <tls> */
134 extern ScmObj Scm_TLSClose(ScmTLS* t);
135
136 /* Set global debug trace level. 0 = no trace, 9 = maximum trace.
137 This affect all submodules. */
138 extern void Scm_TLSSetDebugLevel(int level);
139
140 /* Each submodule registers debug level setter through this. */
141 extern void Scm_TLSRegisterDebugLevelCallback(ScmObj setter);
142
143 extern int Scm_TLSSystemCABundleAvailable(void);
144
145 extern ScmObj Scm_TLSGetConnectionAddress(ScmTLS *t, int who);
146 enum {
147 TLS_SELF_ADDRESS = 0,
148 TLS_PEER_ADDRESS = 1
149 };
150
151 extern u_long Scm_TLSPoll(ScmTLS *t, u_long rwflags, ScmTimeSpec *timeout);
152 enum {
153 TLS_POLL_READ = 1,
154 TLS_POLL_WRITE = 2
155 };
156
157 /*
158 KZ: presumably due to block sizes imposed by the crypto algorithms
159 used, TLSRead() doesn't take a desired size and instead returns
160 whatever the underlying TLS layer was able to read and
161 decrypt. size accommodation is implemented in tls.scm.
162 */
163 extern ScmObj Scm_TLSRead(ScmTLS* t);
164 extern ScmObj Scm_TLSWrite(ScmTLS* t, ScmObj msg);
165
166 extern ScmObj Scm_TLSInputPort(ScmTLS* t);
167 extern ScmObj Scm_TLSOutputPort(ScmTLS* t);
168
169 /* internal, for tls.scm implementation convenience */
170 extern ScmObj Scm_TLSInputPortSet(ScmTLS* t, ScmObj port);
171 extern ScmObj Scm_TLSOutputPortSet(ScmTLS* t, ScmObj port);
172
173
174 SCM_DECL_END
175
176 #endif /*GAUCHE_TLS_H */