Debian Codegraph @

c/src/sasl/sasl.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "sasl-internal.h"
23
24 #include "core/autodetect.h"
25 #include "core/consumers.h"
26 #include "core/engine-internal.h"
27 #include "core/framing.h"
28 #include "core/frame_generators.h"
29 #include "core/frame_consumers.h"
30 #include "core/util.h"
31 #include "platform/platform_fmt.h"
32 #include "protocol.h"
33
34 #include "proton/ssl.h"
35 #include "proton/types.h"
36
37 #include <assert.h>
38
39 // Machinery to allow plugin SASL implementations
40 // Change this to &default_sasl_impl when we change cyrus to opt in
41 static const pnx_sasl_implementation *global_sasl_impl = NULL;
42
43 // List of SASL mechansms to exclude by default as they cause user pain
44 static const char pni_excluded_mechs[] = "GSSAPI GSS-SPNEGO GS2-KRB5 GS2-IAKERB";
45
46 //-----------------------------------------------------------------------------
47 // pnx_sasl: API for SASL implementations to use
48
49 void pnx_sasl_logf(pn_transport_t *logger, pn_log_level_t level, const char *fmt, ...)
50 {
51 va_list ap;
52 va_start(ap, fmt);
53 if (PN_SHOULD_LOG(&logger->logger, PN_SUBSYSTEM_SASL, level))
54 pni_logger_vlogf(&logger->logger, PN_SUBSYSTEM_SASL, level, fmt, ap);
55 va_end(ap);
56 }
57
58 void pnx_sasl_error(pn_transport_t *logger, const char* err, const char* condition_name)
59 {
60 pnx_sasl_logf(logger, PN_LEVEL_ERROR, "sasl error: %s", err);
61 pn_condition_t* c = pn_transport_condition(logger);
62 pn_condition_set_name(c, condition_name);
63 pn_condition_set_description(c, err);
64 }
65
66 void *pnx_sasl_get_context(pn_transport_t *transport)
67 {
68 return transport->sasl ? transport->sasl->impl_context : NULL;
69 }
70
71 void pnx_sasl_set_context(pn_transport_t *transport, void *context)
72 {
73 if (transport->sasl) transport->sasl->impl_context = context;
74 }
75
76 bool pnx_sasl_is_client(pn_transport_t *transport)
77 {
78 return transport->sasl ? transport->sasl->client : false;
79 }
80
81 bool pnx_sasl_is_transport_encrypted(pn_transport_t *transport)
82 {
83 return transport->sasl ? transport->sasl->external_ssf>0 : false;
84 }
85
86 bool pnx_sasl_get_allow_insecure_mechanisms(pn_transport_t *transport)
87 {
88 return transport->sasl ? transport->sasl->allow_insecure_mechs : false;
89 }
90
91 bool pnx_sasl_get_authentication_required(pn_transport_t *transport)
92 {
93 return transport->auth_required;
94 }
95
96 const char *pnx_sasl_get_username(pn_transport_t *transport)
97 {
98 return transport->sasl ? transport->sasl->username : NULL;
99 }
100
101 const char *pnx_sasl_get_authorization(pn_transport_t *transport)
102 {
103 return transport->sasl ? transport->sasl->authzid : NULL;
104 }
105
106 const char *pnx_sasl_get_external_username(pn_transport_t *transport)
107 {
108 return transport->sasl ? transport->sasl->external_auth : NULL;
109 }
110
111 int pnx_sasl_get_external_ssf(pn_transport_t *transport)
112 {
113 return transport->sasl ? transport->sasl->external_ssf : 0;
114 }
115
116 const char *pnx_sasl_get_password(pn_transport_t *transport)
117 {
118 return transport->sasl ? transport->sasl->password : NULL;
119 }
120
121 void pnx_sasl_clear_password(pn_transport_t *transport)
122 {
123 if (transport->sasl) {
124 char *password = transport->sasl->password;
125 free(memset(password, 0, strlen(password)));
126 transport->sasl->password = NULL;
127 }
128 }
129
130 const char *pnx_sasl_get_remote_fqdn(pn_transport_t *transport)
131 {
132 return transport->sasl ? transport->sasl->remote_fqdn : NULL;
133 }
134
135 const char *pnx_sasl_get_selected_mechanism(pn_transport_t *transport)
136 {
137 return transport->sasl ? transport->sasl->selected_mechanism : NULL;
138 }
139
140 void pnx_sasl_set_bytes_out(pn_transport_t *transport, pn_bytes_t bytes)
141 {
142 if (transport->sasl) {
143 transport->sasl->bytes_out = bytes;
144 }
145 }
146
147 void pnx_sasl_set_selected_mechanism(pn_transport_t *transport, const char *mechanism)
148 {
149 if (transport->sasl) {
150 transport->sasl->selected_mechanism = pn_strdup(mechanism);
151 }
152 }
153
154 void pnx_sasl_set_succeeded(pn_transport_t *transport, const char *username, const char *authzid)
155 {
156 if (transport->sasl) {
157 transport->sasl->username = username;
158 transport->sasl->authzid = authzid;
159 transport->sasl->outcome = PN_SASL_OK;
160 transport->authenticated = true;
161
162 if (authzid) {
163 PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_INFO, "Authenticated user: %s for %s with mechanism %s",
164 username, authzid, transport->sasl->selected_mechanism);
165 } else {
166 PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_INFO, "Authenticated user: %s with mechanism %s",
167 username, transport->sasl->selected_mechanism);
168 }
169 }
170 }
171
172 void pnx_sasl_set_failed(pn_transport_t *transport)
173 {
174 if (transport->sasl) {
175 transport->sasl->outcome = PN_SASL_AUTH;
176 }
177 }
178
179 void pnx_sasl_set_implementation(pn_transport_t *transport, const pnx_sasl_implementation *i, void* context)
180 {
181 transport->sasl->impl = i;
182 transport->sasl->impl_context = context;
183 }
184
185 void pnx_sasl_set_default_implementation(const pnx_sasl_implementation* impl)
186 {
187 global_sasl_impl = impl;
188 }
189
190 // Aliases for linkage compatibility in face of renamings
191 PN_EXTERN bool pnx_sasl_is_included_mech(pn_transport_t *transport, pn_bytes_t s)
192 { return pnx_sasl_is_mechanism_included(transport, s); }
193 PN_EXTERN bool pnx_sasl_get_allow_insecure_mechs(pn_transport_t *transport)
194 { return pnx_sasl_get_allow_insecure_mechanisms(transport); }
195 PN_EXTERN bool pnx_sasl_get_auth_required(pn_transport_t *transport)
196 { return pnx_sasl_get_authentication_required(transport); }
197 PN_EXTERN void pnx_sasl_succeed_authentication(pn_transport_t *transport, const char *username, const char *authzid)
198 { pnx_sasl_set_succeeded(transport, username, authzid);}
199 PN_EXTERN void pnx_sasl_fail_authentication(pn_transport_t *transport)
200 {pnx_sasl_set_failed(transport); }
201
202 //-----------------------------------------------------------------------------
203 // pni_sasl_impl: Abstract the entry points to the SASL implementation (virtual function calls)
204
205 static inline void pni_sasl_impl_free(pn_transport_t *transport)
206 {
207 transport->sasl->impl->free(transport);
208 }
209
210 static inline const char *pni_sasl_impl_list_mechs(pn_transport_t *transport)
211 {
212 return transport->sasl->impl->list_mechanisms(transport);
213 }
214
215 static inline bool pni_sasl_impl_init_server(pn_transport_t *transport)
216 {
217 return transport->sasl->impl->init_server(transport);
218 }
219
220 static inline void pni_sasl_impl_prepare_write(pn_transport_t *transport)
221 {
222 transport->sasl->impl->prepare_write(transport);
223 }
224
225 static inline void pni_sasl_impl_process_init(pn_transport_t *transport, const char *mechanism, const pn_bytes_t *recv)
226 {
227 transport->sasl->impl->process_init(transport, mechanism, recv);
228 }
229
230 static inline void pni_sasl_impl_process_response(pn_transport_t *transport, const pn_bytes_t *recv)
231 {
232 transport->sasl->impl->process_response(transport, recv);
233 }
234
235 static inline bool pni_sasl_impl_init_client(pn_transport_t *transport)
236 {
237 return transport->sasl->impl->init_client(transport);
238 }
239
240 static inline bool pni_sasl_impl_process_mechanisms(pn_transport_t *transport, const char *mechs)
241 {
242 return transport->sasl->impl->process_mechanisms(transport, mechs);
243 }
244
245 static inline void pni_sasl_impl_process_challenge(pn_transport_t *transport, const pn_bytes_t *recv)
246 {
247 transport->sasl->impl->process_challenge(transport, recv);
248 }
249
250 static inline void pni_sasl_impl_process_outcome(pn_transport_t *transport, const pn_bytes_t *recv)
251 {
252 transport->sasl->impl->process_outcome(transport, recv);
253 }
254
255 static inline bool pni_sasl_impl_can_encrypt(pn_transport_t *transport)
256 {
257 return transport->sasl->impl->can_encrypt(transport);
258 }
259
260 static inline ssize_t pni_sasl_impl_max_encrypt_size(pn_transport_t *transport)
261 {
262 return transport->sasl->impl->max_encrypt_size(transport);
263 }
264
265 static inline ssize_t pni_sasl_impl_encode(pn_transport_t *transport, pn_bytes_t in, pn_bytes_t *out)
266 {
267 return transport->sasl->impl->encode(transport, in, out);
268 }
269
270 static inline ssize_t pni_sasl_impl_decode(pn_transport_t *transport, pn_bytes_t in, pn_bytes_t *out)
271 {
272 return transport->sasl->impl->decode(transport, in, out);
273 }
274
275 //-----------------------------------------------------------------------------
276 // General SASL implementation
277
278 static inline pni_sasl_t *get_sasl_internal(pn_sasl_t *sasl)
279 {
280 // The external pn_sasl_t is really a pointer to the internal pni_transport_t
281 return sasl ? ((pn_transport_t *)sasl)->sasl : NULL;
282 }
283
284 static ssize_t pn_input_read_sasl_header(pn_transport_t* transport, unsigned int layer, const char* bytes, size_t available);
285 static ssize_t pn_input_read_sasl(pn_transport_t *transport, unsigned int layer, const char *bytes, size_t available);
286 static ssize_t pn_input_read_sasl_encrypt(pn_transport_t *transport, unsigned int layer, const char *bytes, size_t available);
287 static ssize_t pn_output_write_sasl_header(pn_transport_t* transport, unsigned int layer, char* bytes, size_t size);
288 static ssize_t pn_output_write_sasl(pn_transport_t *transport, unsigned int layer, char *bytes, size_t available);
289 static ssize_t pn_output_write_sasl_encrypt(pn_transport_t *transport, unsigned int layer, char *bytes, size_t available);
290 static void pn_error_sasl(pn_transport_t* transport, unsigned int layer);
291
292 const pn_io_layer_t sasl_header_layer = {
293 pn_input_read_sasl_header,
294 pn_output_write_sasl_header,
295 pn_error_sasl,
296 NULL,
297 NULL
298 };
299
300 const pn_io_layer_t sasl_write_header_layer = {
301 pn_input_read_sasl,
302 pn_output_write_sasl_header,
303 pn_error_sasl,
304 NULL,
305 NULL
306 };
307
308 const pn_io_layer_t sasl_read_header_layer = {
309 pn_input_read_sasl_header,
310 pn_output_write_sasl,
311 pn_error_sasl,
312 NULL,
313 NULL
314 };
315
316 const pn_io_layer_t sasl_layer = {
317 pn_input_read_sasl,
318 pn_output_write_sasl,
319 pn_error_sasl,
320 NULL,
321 NULL
322 };
323
324 const pn_io_layer_t sasl_encrypt_layer = {
325 pn_input_read_sasl_encrypt,
326 pn_output_write_sasl_encrypt,
327 NULL,
328 NULL,
329 NULL
330 };
331
332 #define SASL_HEADER ("AMQP\x03\x01\x00\x00")
333 #define SASL_HEADER_LEN 8
334
335 static bool pni_sasl_is_server_state(enum pnx_sasl_state state)
336 {
337 return state==SASL_NONE
338 || state==SASL_POSTED_MECHANISMS
339 || state==SASL_POSTED_CHALLENGE
340 || state==SASL_POSTED_OUTCOME
341 || state==SASL_ERROR;
342 }
343
344 static bool pni_sasl_is_client_state(enum pnx_sasl_state state)
345 {
346 return state==SASL_NONE
347 || state==SASL_POSTED_INIT
348 || state==SASL_POSTED_RESPONSE
349 || state==SASL_RECVED_SUCCESS
350 || state==SASL_RECVED_FAILURE
351 || state==SASL_ERROR;
352 }
353
354 static bool pni_sasl_is_final_input_state(pni_sasl_t *sasl)
355 {
356 enum pnx_sasl_state desired_state = sasl->desired_state;
357 return desired_state==SASL_RECVED_SUCCESS
358 || desired_state==SASL_RECVED_FAILURE
359 || desired_state==SASL_ERROR
360 || desired_state==SASL_POSTED_OUTCOME;
361 }
362
363 static bool pni_sasl_is_final_output_state(pni_sasl_t *sasl)
364 {
365 enum pnx_sasl_state last_state = sasl->last_state;
366 enum pnx_sasl_state desired_state = sasl->desired_state;
367 return (desired_state==SASL_RECVED_SUCCESS && last_state>=SASL_POSTED_INIT)
368 || last_state==SASL_RECVED_SUCCESS
369 || last_state==SASL_RECVED_FAILURE
370 || last_state==SASL_ERROR
371 || last_state==SASL_POSTED_OUTCOME;
372 }
373
374 static void pni_emit(pn_transport_t *transport)
375 {
376 if (transport->connection && transport->connection->collector) {
377 pn_collector_t *collector = transport->connection->collector;
378 pn_collector_put(collector, PN_OBJECT, transport, PN_TRANSPORT);
379 }
380 }
381
382 void pnx_sasl_set_desired_state(pn_transport_t *transport, enum pnx_sasl_state desired_state)
383 {
384 pni_sasl_t *sasl = transport->sasl;
385 if (sasl->last_state > desired_state) {
386 PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_ERROR, "Trying to send SASL frame (%d), but illegal: already in later state (%d)", desired_state, sasl->last_state);
387 } else if (sasl->client && !pni_sasl_is_client_state(desired_state)) {
388 PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_ERROR, "Trying to send server SASL frame (%d) on a client", desired_state);
389 } else if (!sasl->client && !pni_sasl_is_server_state(desired_state)) {
390 PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_ERROR, "Trying to send client SASL frame (%d) on a server", desired_state);
391 } else {
392 // If we need to repeat CHALLENGE or RESPONSE frames adjust current state to seem
393 // like they haven't been sent yet
394 if (sasl->last_state==desired_state && desired_state==SASL_POSTED_RESPONSE) {
395 sasl->last_state = SASL_POSTED_INIT;
396 }
397 if (sasl->last_state==desired_state && desired_state==SASL_POSTED_CHALLENGE) {
398 sasl->last_state = SASL_POSTED_MECHANISMS;
399 }
400 bool changed = sasl->desired_state != desired_state;
401 sasl->desired_state = desired_state;
402 // Don't emit transport event on error as there will be a TRANSPORT_ERROR event
403 if (desired_state != SASL_ERROR && changed) pni_emit(transport);
404 }
405 }
406
407 // Look for symbol in the mech include list - not particularly efficient,
408 // but probably not used enough to matter.
409 //
410 // Note that if there is no inclusion list then every mech is implicitly included.
411 static bool pni_sasl_included_mech(const char *included_mech_list, pn_bytes_t s)
412 {
413 const char * end_list = included_mech_list+strlen(included_mech_list);
414 size_t len = s.size;
415 const char *c = included_mech_list;
416 while (c!=NULL) {
417 // If there are not enough chars left in the list no matches
418 if ((ptrdiff_t)len > end_list-c) return false;
419
420 // Is word equal with a space or end of string afterwards?
421 if (pn_strncasecmp(c, s.start, len)==0 && (c[len]==' ' || c[len]==0) ) return true;
422
423 c = strchr(c, ' ');
424 c = c ? c+1 : NULL;
425 }
426 return false;
427 }
428
429 static bool pni_sasl_server_included_mech(const char *included_mech_list, pn_bytes_t s)
430 {
431 if (!included_mech_list) return true;
432
433 return pni_sasl_included_mech(included_mech_list, s);
434 }
435
436 static bool pni_sasl_client_included_mech(const char *included_mech_list, pn_bytes_t s)
437 {
438 if (!included_mech_list) return !pni_sasl_included_mech(pni_excluded_mechs, s);
439
440 return pni_sasl_included_mech(included_mech_list, s);
441 }
442
443 // Look for symbol in the mech include list - plugin API version
444 //
445 // Note that if there is no inclusion list then every mech is implicitly included.
446 bool pnx_sasl_is_mechanism_included(pn_transport_t* transport, pn_bytes_t s)
447 {
448 return pni_sasl_server_included_mech(transport->sasl->included_mechanisms, s);
449 }
450
451 // This takes a space separated list and zero terminates it in place
452 // whilst adding pointers to the existing strings in a string array.
453 // This means that you can't free the original storage until you have
454 // finished with the resulting list.
455 static void pni_split_mechs(char *mechlist, const char* included_mechs, char *mechs[], int *count)
456 {
457 char *start = mechlist;
458 char *end = start;
459
460 while (*end) {
461 if (*end == ' ') {
462 if (start != end) {
463 *end = '\0';
464 if (pni_sasl_server_included_mech(included_mechs, pn_bytes(end-start, start))) {
465 mechs[(*count)++] = start;
466 }
467 }
468 end++;
469 start = end;
470 } else {
471 end++;
472 }
473 }
474
475 if (start != end) {
476 if (pni_sasl_server_included_mech(included_mechs, pn_bytes(end-start, start))) {
477 mechs[(*count)++] = start;
478 }
479 }
480 }
481
482 // Post SASL frame
483 static void pni_post_sasl_frame(pn_transport_t *transport)
484 {
485 pni_sasl_t *sasl = transport->sasl;
486 pn_bytes_t out = sasl->bytes_out;
487 enum pnx_sasl_state desired_state = sasl->desired_state;
488 while (sasl->desired_state > sasl->last_state) {
489 switch (desired_state) {
490 case SASL_POSTED_INIT: {
491 /* GENERATE_CODEC_CODE: "DL[szS]" */
492 pn_bytes_t buf = pn_amqp_encode_DLEszSe(transport->frame, SASL_INIT, sasl->selected_mechanism, out.size, out.start, sasl->local_fqdn);
493 pn_framing_send_sasl(transport, buf);
494 pni_emit(transport);
495 break;
496 }
497 case SASL_POSTED_MECHANISMS: {
498 // TODO(PROTON-2122) Replace magic number 32 with dynamically sized memory
499 char *mechs[32];
500 char *mechlist = pn_strdup(pni_sasl_impl_list_mechs(transport));
501
502 int count = 0;
503 if (mechlist) {
504 pni_split_mechs(mechlist, sasl->included_mechanisms, mechs, &count);
505 }
506 /* GENERATE_CODEC_CODE: "DL[@T[*s]]" */
507 pn_bytes_t buf = pn_amqp_encode_DLEATEjsee(transport->frame, SASL_MECHANISMS, PN_SYMBOL, count, mechs);
508 free(mechlist);
509 pn_framing_send_sasl(transport, buf);
510 pni_emit(transport);
511 break;
512 }
513 case SASL_POSTED_RESPONSE:
514 if (sasl->last_state != SASL_POSTED_RESPONSE) {
515 /* "DL[Z]" */
516 pn_bytes_t buf = pn_amqp_encode_DLEZe(transport->frame, SASL_RESPONSE, out.size, out.start);
517 pn_framing_send_sasl(transport, buf);
518 pni_emit(transport);
519 }
520 break;
521 case SASL_POSTED_CHALLENGE:
522 if (sasl->last_state < SASL_POSTED_MECHANISMS) {
523 desired_state = SASL_POSTED_MECHANISMS;
524 continue;
525 } else if (sasl->last_state != SASL_POSTED_CHALLENGE) {
526 /* "DL[Z]" */
527 pn_bytes_t buf = pn_amqp_encode_DLEZe(transport->frame, SASL_CHALLENGE, out.size, out.start);
528 pn_framing_send_sasl(transport, buf);
529 pni_emit(transport);
530 }
531 break;
532 case SASL_POSTED_OUTCOME: {
533 if (sasl->last_state < SASL_POSTED_MECHANISMS) {
534 desired_state = SASL_POSTED_MECHANISMS;
535 continue;
536 }
537 /* "DL[Bz]" */
538 pn_bytes_t buf = pn_amqp_encode_DLEBze(transport->frame, SASL_OUTCOME, sasl->outcome, out.size, out.start);
539 pn_framing_send_sasl(transport, buf);
540 pni_emit(transport);
541 if (sasl->outcome!=PN_SASL_OK) {
542 pn_do_error(transport, "amqp:unauthorized-access", "Failed to authenticate client [mech=%s]",
543 transport->sasl->selected_mechanism ? transport->sasl->selected_mechanism : "none");
544 desired_state = SASL_ERROR;
545 }
546 break;
547 }
548 case SASL_RECVED_SUCCESS:
549 if (sasl->last_state < SASL_POSTED_INIT) {
550 desired_state = SASL_POSTED_INIT;
551 continue;
552 }
553 break;
554 case SASL_RECVED_FAILURE:
555 pn_do_error(transport, "amqp:unauthorized-access", "Authentication failed [mech=%s]",
556 transport->sasl->selected_mechanism ? transport->sasl->selected_mechanism : "none");
557 desired_state = SASL_ERROR;
558 break;
559 case SASL_ERROR:
560 break;
561 case SASL_NONE:
562 return;
563 }
564 sasl->last_state = desired_state;
565 desired_state = sasl->desired_state;
566 }
567 }
568
569 static void pn_error_sasl(pn_transport_t* transport, unsigned int layer)
570 {
571 transport->close_sent = true;
572 pnx_sasl_set_desired_state(transport, SASL_ERROR);
573 }
574
575 static ssize_t pn_input_read_sasl_header(pn_transport_t* transport, unsigned int layer, const char* bytes, size_t available)
576 {
577 bool eos = transport->tail_closed;
578 if (eos && available==0) {
579 pn_do_error(transport, "amqp:connection:framing-error",
580 "Expected SASL protocol header: no protocol header found (connection aborted)");
581 pn_set_error_layer(transport);
582 return PN_EOS;
583 }
584 pni_protocol_type_t protocol = pni_sniff_header(bytes, available);
585 switch (protocol) {
586 case PNI_PROTOCOL_AMQP_SASL:
587 transport->present_layers |= LAYER_AMQPSASL;
588 if (transport->io_layers[layer] == &sasl_read_header_layer) {
589 transport->io_layers[layer] = &sasl_layer;
590 } else {
591 transport->io_layers[layer] = &sasl_write_header_layer;
592 }
593 PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_FRAME, " <- %s", "SASL");
594 pni_sasl_set_external_security(transport, pn_ssl_get_ssf((pn_ssl_t*)transport), pn_ssl_get_remote_subject((pn_ssl_t*)transport));
595 return SASL_HEADER_LEN;
596 case PNI_PROTOCOL_INSUFFICIENT:
597 if (!eos) return 0;
598 /* Fallthru */
599 default:
600 break;
601 }
602 char quoted[1024];
603 pn_quote_data(quoted, 1024, bytes, available);
604 pn_do_error(transport, "amqp:connection:framing-error",
605 "Expected SASL protocol header got: %s ['%s']%s", pni_protocol_name(protocol), quoted,
606 !eos ? "" : " (connection aborted)");
607 pn_set_error_layer(transport);
608 return PN_EOS;
609 }
610
611 static void pni_sasl_start_server_if_needed(pn_transport_t *transport)
612 {
613 pni_sasl_t *sasl = transport->sasl;
614 if (!sasl->client && sasl->desired_state<SASL_POSTED_MECHANISMS) {
615 pni_sasl_impl_init_server(transport);
616 }
617 }
618
619 static ssize_t pn_input_read_sasl(pn_transport_t* transport, unsigned int layer, const char* bytes, size_t available)
620 {
621 pni_sasl_t *sasl = transport->sasl;
622
623 bool eos = transport->tail_closed;
624 if (eos) {
625 pn_do_error(transport, "amqp:connection:framing-error", "connection aborted");
626 pn_set_error_layer(transport);
627 return PN_EOS;
628 }
629
630 pni_sasl_start_server_if_needed(transport);
631
632 if (!pni_sasl_is_final_input_state(sasl)) {
633 ssize_t n = pn_dispatcher_input(transport, bytes, available, false, &transport->halt);
634 if (n < 0 || transport->close_rcvd) {
635 return PN_EOS;
636 } else {
637 return n;
638 }
639 }
640
641 if (!pni_sasl_is_final_output_state(sasl)) {
642 return pni_passthru_layer.process_input(transport, layer, bytes, available);
643 }
644
645 if (pni_sasl_impl_can_encrypt(transport)) {
646 sasl->max_encrypt_size = pni_sasl_impl_max_encrypt_size(transport);
647 PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_INFO, "Encryption enabled: buffer=%" PN_ZU, sasl->max_encrypt_size);
648 transport->io_layers[layer] = &sasl_encrypt_layer;
649 } else {
650 transport->io_layers[layer] = &pni_passthru_layer;
651 }
652 return transport->io_layers[layer]->process_input(transport, layer, bytes, available);
653 }
654
655 static ssize_t pn_input_read_sasl_encrypt(pn_transport_t* transport, unsigned int layer, const char* bytes, size_t available)
656 {
657 pn_buffer_t *in = transport->sasl->decoded_buffer;
658 const size_t max_buffer = transport->sasl->max_encrypt_size;
659 for (size_t processed = 0; processed<available;) {
660 pn_bytes_t decoded = pn_bytes(0, NULL);
661 size_t decode_size = (available-processed)<=max_buffer?(available-processed):max_buffer;
662 ssize_t size = pni_sasl_impl_decode(transport, pn_bytes(decode_size, bytes+processed), &decoded);
663 if (size<0) return size;
664 if (size>0) {
665 size = pn_buffer_append(in, decoded.start, decoded.size);
666 if (size) return size;
667 }
668 processed += decode_size;
669 }
670 pn_bytes_t decoded = pn_buffer_bytes(in);
671 size_t processed_size = 0;
672 while (processed_size < decoded.size) {
673 ssize_t size = pni_passthru_layer.process_input(transport, layer, decoded.start+processed_size, decoded.size-processed_size);
674 if (size==0) break;
675 if (size<0) return size;
676 pn_buffer_trim(in, size, 0);
677 processed_size += size;
678 }
679 return available;
680 }
681
682 static ssize_t pn_output_write_sasl_header(pn_transport_t *transport, unsigned int layer, char *bytes, size_t size)
683 {
684 PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_FRAME, " -> %s", "SASL");
685 assert(size >= SASL_HEADER_LEN);
686 memmove(bytes, SASL_HEADER, SASL_HEADER_LEN);
687 if (transport->io_layers[layer]==&sasl_write_header_layer) {
688 transport->io_layers[layer] = &sasl_layer;
689 } else {
690 transport->io_layers[layer] = &sasl_read_header_layer;
691 }
692 return SASL_HEADER_LEN;
693 }
694
695 static ssize_t pn_output_write_sasl(pn_transport_t* transport, unsigned int layer, char* bytes, size_t available)
696 {
697 pni_sasl_t *sasl = transport->sasl;
698
699 // this accounts for when pn_do_error is invoked, e.g. by idle timeout
700 if (transport->close_sent) return PN_EOS;
701
702 pni_sasl_start_server_if_needed(transport);
703
704 pni_sasl_impl_prepare_write(transport);
705
706 pni_post_sasl_frame(transport);
707
708 if (pn_buffer_size(transport->output_buffer) != 0 || !pni_sasl_is_final_output_state(sasl)) {
709 return pn_dispatcher_output(transport, bytes, available);
710 }
711
712 if (!pni_sasl_is_final_input_state(sasl)) {
713 return pni_passthru_layer.process_output(transport, layer, bytes, available );
714 }
715
716 // We only get here if there is nothing to output and we're in a final state
717 if (sasl->outcome != PN_SASL_OK) {
718 return PN_EOS;
719 }
720
721 // We know that auth succeeded or we're not in final input state
722 if (pni_sasl_impl_can_encrypt(transport)) {
723 sasl->max_encrypt_size = pni_sasl_impl_max_encrypt_size(transport);
724 PN_LOG(&transport->logger, PN_SUBSYSTEM_SASL, PN_LEVEL_INFO, "Encryption enabled: buffer=%" PN_ZU, sasl->max_encrypt_size);
725 transport->io_layers[layer] = &sasl_encrypt_layer;
726 } else {
727 transport->io_layers[layer] = &pni_passthru_layer;
728 }
729 return transport->io_layers[layer]->process_output(transport, layer, bytes, available);
730 }
731
732 static ssize_t pn_output_write_sasl_encrypt(pn_transport_t* transport, unsigned int layer, char* bytes, size_t available)
733 {
734 ssize_t clear_size = pni_passthru_layer.process_output(transport, layer, bytes, available );
735 if (clear_size<0) return clear_size;
736
737 const ssize_t max_buffer = transport->sasl->max_encrypt_size;
738 pn_buffer_t *out = transport->sasl->encoded_buffer;
739 for (ssize_t processed = 0; processed<clear_size;) {
740 pn_bytes_t encoded = pn_bytes(0, NULL);
741 ssize_t encode_size = (clear_size-processed)<=max_buffer?(clear_size-processed):max_buffer;
742 ssize_t size = pni_sasl_impl_encode(transport, pn_bytes(encode_size, bytes+processed), &encoded);
743 if (size<0) return size;
744 if (size>0) {
745 size = pn_buffer_append(out, encoded.start, encoded.size);
746 if (size) return size;
747 }
748 processed += encode_size;
749 }
750 ssize_t size = pn_buffer_get(out, 0, available, bytes);
751 pn_buffer_trim(out, size, 0);
752 return size;
753 }
754
755 pn_sasl_t *pn_sasl(pn_transport_t *transport)
756 {
757 if (!transport->sasl) {
758 pni_sasl_t *sasl = (pni_sasl_t *) malloc(sizeof(pni_sasl_t));
759
760 sasl->impl_context = NULL;
761 // Change this to just global_sasl_impl when we make cyrus opt in
762 sasl->impl = global_sasl_impl ? global_sasl_impl : cyrus_sasl_impl ? cyrus_sasl_impl : &default_sasl_impl;
763 sasl->client = !transport->server;
764 sasl->selected_mechanism = NULL;
765 sasl->included_mechanisms = NULL;
766 sasl->username = NULL;
767 sasl->authzid = NULL;
768 sasl->password = NULL;
769 sasl->remote_fqdn = NULL;
770 sasl->local_fqdn = NULL;
771 sasl->external_auth = NULL;
772 sasl->external_ssf = 0;
773 sasl->outcome = PN_SASL_NONE;
774 sasl->decoded_buffer = pn_buffer(0);
775 sasl->encoded_buffer = pn_buffer(0);
776 sasl->bytes_out.size = 0;
777 sasl->bytes_out.start = NULL;
778 sasl->desired_state = SASL_NONE;
779 sasl->last_state = SASL_NONE;
780 sasl->allow_insecure_mechs = false;
781
782 transport->sasl = sasl;
783 }
784
785 // The actual external pn_sasl_t pointer is a pointer to its enclosing pn_transport_t
786 return (pn_sasl_t *)transport;
787 }
788
789 void pn_sasl_free(pn_transport_t *transport)
790 {
791 if (transport) {
792 pni_sasl_t *sasl = transport->sasl;
793 if (sasl) {
794 free(sasl->selected_mechanism);
795 free(sasl->included_mechanisms);
796 free(sasl->password);
797 free(sasl->external_auth);
798 free(sasl->local_fqdn);
799
800 if (sasl->impl_context) {
801 pni_sasl_impl_free(transport);
802 }
803 pn_buffer_free(sasl->decoded_buffer);
804 pn_buffer_free(sasl->encoded_buffer);
805 free(sasl);
806 }
807 }
808 }
809
810 void pni_sasl_set_remote_hostname(pn_transport_t * transport, const char * fqdn)
811 {
812 pni_sasl_t *sasl = transport->sasl;
813 sasl->remote_fqdn = fqdn;
814 }
815
816 void pnx_sasl_set_local_hostname(pn_transport_t * transport, const char * fqdn)
817 {
818 pni_sasl_t *sasl = transport->sasl;
819 sasl->local_fqdn = pn_strdup(fqdn);
820 }
821
822 void pni_sasl_set_user_password(pn_transport_t *transport, const char *user, const char *authzid, const char *password)
823 {
824 pni_sasl_t *sasl = transport->sasl;
825 sasl->username = user;
826 sasl->authzid = authzid;
827 free(sasl->password);
828 sasl->password = password ? pn_strdup(password) : NULL;
829 }
830
831 void pni_sasl_set_external_security(pn_transport_t *transport, int ssf, const char *authid)
832 {
833 pni_sasl_t *sasl = transport->sasl;
834 sasl->external_ssf = ssf;
835 free(sasl->external_auth);
836 sasl->external_auth = authid ? pn_strdup(authid) : NULL;
837 }
838
839 const char *pn_sasl_get_user(pn_sasl_t *sasl0)
840 {
841 pni_sasl_t *sasl = get_sasl_internal(sasl0);
842 return sasl->username;
843 }
844
845 const char *pn_sasl_get_authorization(pn_sasl_t *sasl0)
846 {
847 pni_sasl_t *sasl = get_sasl_internal(sasl0);
848 return sasl->authzid;
849 }
850
851 const char *pn_sasl_get_mech(pn_sasl_t *sasl0)
852 {
853 pni_sasl_t *sasl = get_sasl_internal(sasl0);
854 return sasl->selected_mechanism;
855 }
856
857 void pn_sasl_allowed_mechs(pn_sasl_t *sasl0, const char *mechs)
858 {
859 pni_sasl_t *sasl = get_sasl_internal(sasl0);
860 free(sasl->included_mechanisms);
861 sasl->included_mechanisms = mechs ? pn_strdup(mechs) : NULL;
862 }
863
864 void pn_sasl_set_allow_insecure_mechs(pn_sasl_t *sasl0, bool insecure)
865 {
866 pni_sasl_t *sasl = get_sasl_internal(sasl0);
867 sasl->allow_insecure_mechs = insecure;
868 }
869
870 bool pn_sasl_get_allow_insecure_mechs(pn_sasl_t *sasl0)
871 {
872 pni_sasl_t *sasl = get_sasl_internal(sasl0);
873 return sasl->allow_insecure_mechs;
874 }
875
876 void pn_sasl_done(pn_sasl_t *sasl0, pn_sasl_outcome_t outcome)
877 {
878 pni_sasl_t *sasl = get_sasl_internal(sasl0);
879 if (sasl) {
880 sasl->outcome = outcome;
881 }
882 }
883
884 pn_sasl_outcome_t pn_sasl_outcome(pn_sasl_t *sasl0)
885 {
886 pni_sasl_t *sasl = get_sasl_internal(sasl0);
887 return sasl ? sasl->outcome : PN_SASL_NONE;
888 }
889
890 // Received Server side
891 int pn_do_init(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload)
892 {
893 pni_sasl_t *sasl = transport->sasl;
894
895 // If we haven't got an sasl struct yet we've in an error state
896 // This can only happen if our peer sent SASL frames even though he didn't send the SASL header
897 if (!sasl) return PN_ERR;
898
899 // We should only receive this if we are a sasl server
900 if (sasl->client) return PN_ERR;
901
902 pn_bytes_t mech;
903 pn_bytes_t recv;
904
905 pn_amqp_decode_DqEsze(payload, &mech, &recv);
906 sasl->selected_mechanism = pn_strndup(mech.start, mech.size);
907
908 // We need to filter out a supplied mech in in the inclusion list
909 // as the client could have used a mech that we support, but that
910 // wasn't on the list we sent.
911 if (!pni_sasl_server_included_mech(sasl->included_mechanisms, mech)) {
912 pnx_sasl_error(transport, "Client mechanism not in mechanism inclusion list.", "amqp:unauthorized-access");
913 sasl->outcome = PN_SASL_AUTH;
914 pnx_sasl_set_desired_state(transport, SASL_POSTED_OUTCOME);
915 } else {
916 pni_sasl_impl_process_init(transport, sasl->selected_mechanism, &recv);
917 }
918
919 return 0;
920 }
921
922 // Received client side
923 int pn_do_mechanisms(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload)
924 {
925 pni_sasl_t *sasl = transport->sasl;
926
927 // If we haven't got an sasl struct yet we've in an error state
928 // This can only happen if our peer sent SASL frames even though we didn't send the SASL header
929 if (!sasl) return PN_ERR;
930
931 // We should only receive this if we are a sasl client
932 if (!sasl->client) return PN_ERR;
933
934 pn_string_t *mechs = pn_string("");
935
936 pn_bytes_t subpayload;
937 pn_amqp_decode_DqERe(payload, &subpayload);
938 pni_consumer_t consumer = make_consumer_from_bytes(subpayload);
939
940 uint8_t element_type;
941 uint32_t element_count;
942 pni_consumer_t subconsumer;
943 if (consume_array(&consumer, &subconsumer, &element_count, &element_type) && (element_type==PNE_SYM32 || element_type==PNE_SYM8)) {
944 // If this is an array of symbols decode each symbol
945 pn_bytes_t symbol;
946 switch (element_type) {
947 case PNE_SYM8:
948 while (element_count) {
949 if (!pni_consumer_readv8(&subconsumer, &symbol)) break;
950 if (pni_sasl_client_included_mech(sasl->included_mechanisms, symbol)) {
951 pn_string_addf(mechs, "%.*s ", (int)symbol.size, symbol.start);
952 }
953 --element_count;
954 }
955 break;
956 case PNE_SYM32:
957 while (element_count) {
958 if (!pni_consumer_readv32(&subconsumer, &symbol)) break;
959 if (pni_sasl_client_included_mech(sasl->included_mechanisms, symbol)) {
960 pn_string_addf(mechs, "%.*s ", (int)symbol.size, symbol.start);
961 }
962 --element_count;
963 }
964 break;
965 }
966 if (pn_string_size(mechs)) {
967 pn_string_buffer(mechs)[pn_string_size(mechs)-1] = 0;
968 }
969 } else {
970 // If not then see if it is a single symbol
971 pn_bytes_t symbol;
972 pn_amqp_decode_DqEse(payload, &symbol);
973
974 if (pni_sasl_client_included_mech(sasl->included_mechanisms, symbol)) {
975 pn_string_setn(mechs, symbol.start, symbol.size);
976 }
977 }
978
979 if (!(pni_sasl_impl_init_client(transport) &&
980 pn_string_size(mechs) &&
981 pni_sasl_impl_process_mechanisms(transport, pn_string_get(mechs)))) {
982 sasl->outcome = PN_SASL_PERM;
983 pnx_sasl_set_desired_state(transport, SASL_RECVED_FAILURE);
984 }
985
986 pn_free(mechs);
987 return 0;
988 }
989
990 // Received client side
991 int pn_do_challenge(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload)
992 {
993 pni_sasl_t *sasl = transport->sasl;
994
995 // If we haven't got an sasl struct yet we've in an error state
996 // This can only happen if our peer sent SASL frames even though we didn't send the SASL header
997 if (!sasl) return PN_ERR;
998
999 // We should only receive this if we are a sasl client
1000 if (!sasl->client) return PN_ERR;
1001
1002 pn_bytes_t recv;
1003
1004 pn_amqp_decode_DqEze(payload, &recv);
1005
1006 pni_sasl_impl_process_challenge(transport, &recv);
1007
1008 return 0;
1009 }
1010
1011 // Received server side
1012 int pn_do_response(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload)
1013 {
1014 pni_sasl_t *sasl = transport->sasl;
1015
1016 // If we haven't got an sasl struct yet we've in an error state
1017 // This can only happen if our peer sent SASL frames even though he didn't send the SASL header
1018 if (!sasl) return PN_ERR;
1019
1020 // We should only receive this if we are a sasl server
1021 if (sasl->client) return PN_ERR;
1022
1023 pn_bytes_t recv;
1024
1025 pn_amqp_decode_DqEze(payload, &recv);
1026
1027 pni_sasl_impl_process_response(transport, &recv);
1028
1029 return 0;
1030 }
1031
1032 // Received client side
1033 int pn_do_outcome(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload)
1034 {
1035 pni_sasl_t *sasl = transport->sasl;
1036
1037 // If we haven't got an sasl struct yet we've in an error state
1038 // This can only happen if our peer sent SASL frames even though we didn't send the SASL header
1039 if (!sasl) return PN_ERR;
1040
1041 // We should only receive this if we are a sasl client
1042 if (!sasl->client) return PN_ERR;
1043
1044 uint8_t outcome;
1045 pn_bytes_t recv;
1046
1047 pn_amqp_decode_DqEBze(payload, &outcome, &recv);
1048
1049 // Preset the outcome to what the server sent us - the plugin can alter this.
1050 // In practise the plugin processing here should only fail because it fails
1051 // to authenticate the server id after the server authenticates our user.
1052 // It should never succeed after the server outcome was failure.
1053 sasl->outcome = (pn_sasl_outcome_t) outcome;
1054
1055 pni_sasl_impl_process_outcome(transport, &recv);
1056
1057 bool authenticated = sasl->outcome==PN_SASL_OK;
1058 transport->authenticated = authenticated;
1059 pnx_sasl_set_desired_state(transport, authenticated ? SASL_RECVED_SUCCESS : SASL_RECVED_FAILURE);
1060 return 0;
1061 }
1062