windows
[tlspool] / include / tlspool / starttls.h
1 /* tlspool/starttls.h -- Library functions to invoke from clients/servers */
2
3 #ifndef TLSPOOL_STARTTLS_H
4 #define TLSPOOL_STARTTLS_H
5
6
7 #include <tlspool/commands.h>
8
9
10
11 /*
12  * These functions are used by application software to turn an existing
13  * connection into one that runs over TLS or DTLS.  These functions are
14  * not just used after an acknowledged STARTTLS exchange, but can also
15  * be used immediately on a newly opened connection running an xxxs:
16  * secure variation of a standard protocol xxx:  -- usually, the two
17  * versions each have their own default port.
18  *
19  * The starttls_ operations can consume a fair amount of time to finish,
20  * so future versions may provide asynchronous variations.  At present,
21  * the calls are not re-entrant.  TODO: asynchronicity & request pooling.
22  */
23
24
25 #define TLSPOOL_DEFAULT_SOCKET_PATH "/var/run/tlspool.sock"
26
27 /*
28  * Quick and dirty function to get the PID of the tlspool daemon
29  */
30
31 int tlspool_getpid ();
32
33 /* Setup the TLS pool socket to use, if it is not the default path name
34  * /var/run/tlspool.sock.  The return value is the file descriptor for the
35  * pool.  This function can be called again, in which case the argument is
36  * ignored and the previously set socket is returned.  The function can also
37  * be called with NULL in the first call, in which case the default location
38  * is used.
39  */
40 int tlspool_socket (char *path);
41
42
43 /* The library function for ping, which is called to establish the API
44  * version and a list of facilities supported by the TLS Pool.  The data
45  * supplied to the TLS Pool should represent the environment of the
46  * application, which is why no defaults are provided by this function
47  * but the application should supply all ping data.
48  *
49  * The pioc_ping structure will be copied into the command structure,
50  * and upon completion it will be copied back.  Normally, the application
51  * would set YYYYMMDD_producer to TLSPOOL_IDENTITY_V2, and facilities
52  * to PIOF_FACILITY_ALL_CURRENT.  The TLS Pool overwrites the former and
53  * resets unsupported bits in the latter.  Note that facilities may be
54  * unsupported due to the compile-time environment of the TLS Pool or
55  * because it was configured without the requested support.
56  *
57  * This function returns zero on success, and -1 on failure.  In case of
58  * failure, errno will be set.
59  */
60 int tlspool_ping (pingpool_t *pingdata);
61
62
63 /* The library function for starttls, which is normally called through one
64  * of the two inline variations below, which start client and server sides.
65  *
66  * The cryptfd handle supplies the TLS connection that is assumed to have
67  * been setup.  When the function ends, either in success or failure, this
68  * handle will no longer be available to the caller; the responsibility of
69  * closing it is passed on to the function and/or the TLS Pool.
70  *
71  * The tlsdata structure will be copied into the command structure,
72  * and upon completion it will be copied back.  You can use it to
73  * communicate flags, protocols and other parameters, including the
74  * most important settings -- local and remote identifiers.  See
75  * the socket protocol document for details.
76  *
77  * The privdata handle is used in conjunction with the namedconnect() call;
78  * it is passed on to connect the latter to the context from which it was
79  * called and is not further acted upon by this function.
80  *
81  * The namedconnect() function is called when the identities have been
82  * exchanged, and established, in the TLS handshake.  This is the point
83  * at which a connection to the plaintext side is needed, and a callback
84  * to namedconnect() is made to find a handle for it.  The function is
85  * called with a version of the tlsdata that has been updated by the
86  * TLS Pool to hold the local and remote identities.  The return value
87  * should be -1 on error, with errno set, or it should be a valid file
88  * handle that can be passed back to the TLS Pool to connect to.
89  *
90  * When the namedconnect argument passed is NULL, default behaviour is
91  * triggered.  This interprets the privdata handle as an (int *) holding
92  * a file descriptor.  If its value is valid, that is, >= 0, it will be
93  * returned directly; otherwise, a socketpair is constructed, one of the
94  * sockets is stored in privdata for use by the caller and the other is
95  * returned as the connected file descriptor for use by the TLS Pool.
96  * This means that the privdata must be properly initialised for this
97  * use, with either -1 (to create a socketpair) or the TLS Pool's
98  * plaintext file descriptor endpoint.  The file handle returned in
99  * privdata, if it is >= 0, should be closed by the caller, both in case
100  * of success and failure.
101  *
102  * This function returns zero on success, and -1 on failure.  In case of
103  * failure, errno will be set.
104  */
105 int tlspool_starttls (int cryptfd, starttls_t *tlsdata,
106                         void *privdata,
107                         int namedconnect (starttls_t *tlsdata, void *privdata));
108
109
110 /* The library function to send a control connection command, notably
111  * TLSPOOL_CONTROL_DETACH and TLSPOOL_CONTROL_REATTACH.
112  *
113  * This function returns zero on success, and -1 on failure.  In case of
114  * failure, errno will be set.
115  */
116 int _tlspool_control_command (int cmd, uint8_t *ctlkey);
117
118
119 /* Explicitly detach a TLS session from the controlling connection to the
120  * TLS Pool.  This means that the control connection (and so, this program)
121  * can be taken down without affecting the TLS session as it is setup.  It
122  * also means that any control connection (including ones from other processes
123  * and other programs) can reattach, using the ctlkey for the TLS session.
124  *
125  * The return value is 0 for success, -1 for failure.  In case of failure,
126  * errno will also be set.
127  */
128 static inline int tlspool_control_detach (uint8_t *ctlkey) {
129         return _tlspool_control_command (PIOC_CONTROL_DETACH_V2, ctlkey);
130 }
131
132 /* Explicitly reattach a control connection to a TLS session.  This may be
133  * called on a TLS session that is detached, by any process or program that
134  * presents the proper control key.
135  *
136  * The return value is 0 for success, -1 for failure.  In case of failure,
137  * errno will also be set.
138  */
139 static inline int tlspool_control_reattach (uint8_t *ctlkey) {
140         return _tlspool_control_command (PIOC_CONTROL_REATTACH_V2, ctlkey);
141 }
142
143
144 /* Register a callback function for local identity selection with the
145  * LIDENTRY API of the TLS Pool.  This will invoke the callback with zero
146  * or more database entries (marked with flag PIOF_LIDENTRY_DBENTRY) and an
147  * inquiry (without that flag) to enter a local identity.  The callback is
148  * expected to save the database entries in some sort of a menu structure
149  * (or ignore it if it is not interested in them) and use them in the
150  * selection process.  What it does precisely is up to the registered
151  * application.
152  *
153  * The callback behaviour of the API can be influenced in various ways;
154  * see the PIOF_LIDENTRY_xxx flags in <tlspool/commands.h> for details.
155  * Some flags are used during registration and supplied in regflags,
156  * some are used during callback and exchanged in the tlspool_command.
157  *
158  * The registration for callback terminates in the following situations:
159  *  - the TLS Pool file handle is closed
160  *  - the callback returns a wrong type of command, including PIOC_ERROR_xx
161  *  - the callback does not respond fast enough (other apps may overtake)
162  *
163  * The responsetimeout is set to the number of seconds that a callback
164  * may at most take to respond.  The claim on the registration will expire
165  * after this time has passed.
166  *
167  * Note that the service function does not return until the callback
168  * registration is terminated.  This is why it is called xxx_service and
169  * not xxx_callback.  You may want to use a thread if your intention is
170  * to do other things as well.  Note however, that it is usually a good
171  * idea to keep localid handling separate, as a GUI function, from the
172  * other components that interact with the TLS Pool for other purposes,
173  * such as wrapping an application protocol.
174  *
175  * This function returns 0 on success, meaning it has gotten to a stage
176  * where it was registered with the TLS Pool.  Otherwise, it returns -1
177  * and sets errno.
178  */
179 int tlspool_localid_service (char *path, uint32_t regflags, int responsetimeout, char * (*cb) (struct pioc_lidentry *entry, void *data), void *data);
180
181
182
183 /* The library function to service PIN entry callbacks.  It registers
184  * with the TLS Pool and will service callback requests until it is no
185  * longer welcomed.  Of course, if another process already has a claim on
186  * this functionality, the service offering will not be welcome from the
187  * start.
188  *
189  * This function differs from most other TLS Pool library functions in
190  * setting up a private socket.  This helps to avoid the overhead in the
191  * foreseeable applications that only do this; it also helps to close
192  * down the exclusive claim on local identity resolution when (part of)
193  * the program is torn down.  The function has been built to cleanup
194  * properly when it is subjected to pthread_cancel().
195  *
196  * The path parameter offers a mechanism to specify the socket path.  When
197  * set to NULL, the library's compiled-in default path will be used.
198  *
199  * In terms of linking, this routine is a separate archive object.  This
200  * minimizes the amount of code carried around in statically linked binaries.
201  *
202  * This function returns -1 on error, or 0 on success.
203  */
204 int tlspool_pin_service (char *path, uint32_t regflags, int responsetimeout_usec, void (*cb) (struct pioc_pinentry *entry, void *data), void *data);
205
206 /* Generate a pseudo-random sequence based on session cryptographic keys.
207  *
208  * In the case of TLS, this adheres to RFC 5705; other protocols may or
209  * may not support a similar mechanism, in which case an error is returned.
210  *
211  * This leans on access privileges to an existing connection at a meta-level,
212  * for which we use the customary ctlkey verification mechanism introduced with
213  * tlspool_starttls ().  Note that random material may be used for security
214  * purposes, such as finding the same session key for both sides deriving from
215  * prior key negotiation; the protection of a ctlkey for such applications is
216  * important.
217  * 
218  * The inputs to this function must adhere to the following restrictions:
219  *  - label must not be a NULL pointer, but opt_ctxvalue may be set to NULL
220  *    to bypass the use of a context value.  Note that passing an empty string
221  *    in opt_ctxvalue is different from not providing the string at all by
222  *    setting it to NULL.
223  *  - label  and  opt_ctxvalue  (if non-NULL) refer to ASCII strings with
224  *    printable characters, terminated with a NUL character.  The maximum
225  *    string length of each is 254 bytes.
226  *  - prng_len holds the requested number of pseudo-random bytes
227  *  - prng_buf points is a non-NULL pointer to a buffer that can hold
228  *    prng_len bytes.
229  *
230  * If the operation succeeds, then prng_buf holds prng_len bytes of random
231  * material, and zero is returned.  If the operation fails, then prng_buf
232  * is filled with zero bytes (to make it stand out as a rather rare case of
233  * a random byte string) and -1 is returned.
234  *
235  * Note a few restrictions to the generality of this function, as a result of
236  * the underlying packet format for the communication with the TLS Pool; but
237  * the dimensions have been choosen such that these restrictions would not
238  * typically be a problem in practice:
239  *  - it constrains the string lengths of label and opt_ctxvalue
240  *  - it constrains prng_len to a maximum value of TLSPOOL_PRNGBUFLEN
241  *
242  * The TLS Pool may limit certain TLS PRNG labels, in adherence to the
243  * IANA-maintained TLS Exporter Label Registry.  It additionally supports
244  * the EXPERIMENTAL label prefix specified in RFC 5705.
245  *
246  * Be advised that the maximum size of buffer may increase in future releases.
247  * So, be sure to use TLSPOOL_PRNGBUFLEN which holds the header-file defined
248  * size.
249  */
250 int tlspool_prng (char *label, char *opt_ctxvalue,
251                 uint16_t prng_len, uint8_t *prng_buf,
252                 uint8_t *ctlkey);
253
254 #endif // TLSPOOL_STARTTLS_H