auth.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Copyright (c) 2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #include "config.h"
  15. #include <openssl/bio.h>
  16. #include <openssl/buffer.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/rand.h>
  19. #include "dynamic_security.h"
  20. #include "mosquitto.h"
  21. #include "mosquitto_broker.h"
  22. /* ################################################################
  23. * #
  24. * # Base64 encoding/decoding
  25. * #
  26. * ################################################################ */
  27. int dynsec_auth__base64_encode(unsigned char *in, int in_len, char **encoded)
  28. {
  29. BIO *bmem, *b64;
  30. BUF_MEM *bptr = NULL;
  31. if(in_len < 0) return 1;
  32. b64 = BIO_new(BIO_f_base64());
  33. if(b64 == NULL) return 1;
  34. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  35. bmem = BIO_new(BIO_s_mem());
  36. if(bmem == NULL){
  37. BIO_free_all(b64);
  38. return 1;
  39. }
  40. b64 = BIO_push(b64, bmem);
  41. BIO_write(b64, in, in_len);
  42. if(BIO_flush(b64) != 1){
  43. BIO_free_all(b64);
  44. return 1;
  45. }
  46. BIO_get_mem_ptr(b64, &bptr);
  47. *encoded = mosquitto_malloc(bptr->length+1);
  48. if(!(*encoded)){
  49. BIO_free_all(b64);
  50. return 1;
  51. }
  52. memcpy(*encoded, bptr->data, bptr->length);
  53. (*encoded)[bptr->length] = '\0';
  54. BIO_free_all(b64);
  55. return 0;
  56. }
  57. int dynsec_auth__base64_decode(char *in, unsigned char **decoded, int *decoded_len)
  58. {
  59. BIO *bmem, *b64;
  60. size_t slen;
  61. slen = strlen(in);
  62. b64 = BIO_new(BIO_f_base64());
  63. if(!b64){
  64. return 1;
  65. }
  66. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  67. bmem = BIO_new(BIO_s_mem());
  68. if(!bmem){
  69. BIO_free_all(b64);
  70. return 1;
  71. }
  72. b64 = BIO_push(b64, bmem);
  73. BIO_write(bmem, in, (int)slen);
  74. if(BIO_flush(bmem) != 1){
  75. BIO_free_all(b64);
  76. return 1;
  77. }
  78. *decoded = mosquitto_calloc(slen, 1);
  79. if(!(*decoded)){
  80. BIO_free_all(b64);
  81. return 1;
  82. }
  83. *decoded_len = BIO_read(b64, *decoded, (int)slen);
  84. BIO_free_all(b64);
  85. if(*decoded_len <= 0){
  86. mosquitto_free(*decoded);
  87. *decoded = NULL;
  88. *decoded_len = 0;
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. /* ################################################################
  94. * #
  95. * # Password functions
  96. * #
  97. * ################################################################ */
  98. int dynsec_auth__pw_hash(struct dynsec__client *client, const char *password, unsigned char *password_hash, int password_hash_len, bool new_password)
  99. {
  100. const EVP_MD *digest;
  101. int iterations;
  102. if(new_password){
  103. if(RAND_bytes(client->pw.salt, sizeof(client->pw.salt)) != 1){
  104. return MOSQ_ERR_UNKNOWN;
  105. }
  106. iterations = PW_DEFAULT_ITERATIONS;
  107. }else{
  108. iterations = client->pw.iterations;
  109. }
  110. if(iterations < 1){
  111. return MOSQ_ERR_INVAL;
  112. }
  113. client->pw.iterations = iterations;
  114. digest = EVP_get_digestbyname("sha512");
  115. if(!digest){
  116. return MOSQ_ERR_UNKNOWN;
  117. }
  118. return !PKCS5_PBKDF2_HMAC(password, (int)strlen(password),
  119. client->pw.salt, sizeof(client->pw.salt), iterations,
  120. digest, password_hash_len, password_hash);
  121. }
  122. /* ################################################################
  123. * #
  124. * # Username/password check
  125. * #
  126. * ################################################################ */
  127. static int memcmp_const(const void *a, const void *b, size_t len)
  128. {
  129. size_t i;
  130. int rc = 0;
  131. if(!a || !b) return 1;
  132. for(i=0; i<len; i++){
  133. if( ((char *)a)[i] != ((char *)b)[i] ){
  134. rc = 1;
  135. }
  136. }
  137. return rc;
  138. }
  139. int dynsec_auth__basic_auth_callback(int event, void *event_data, void *userdata)
  140. {
  141. struct mosquitto_evt_basic_auth *ed = event_data;
  142. struct dynsec__client *client;
  143. unsigned char password_hash[64]; /* For SHA512 */
  144. const char *clientid;
  145. UNUSED(event);
  146. UNUSED(userdata);
  147. if(ed->username == NULL || ed->password == NULL) return MOSQ_ERR_PLUGIN_DEFER;
  148. client = dynsec_clients__find(ed->username);
  149. if(client){
  150. if(client->disabled){
  151. return MOSQ_ERR_AUTH;
  152. }
  153. if(client->clientid){
  154. clientid = mosquitto_client_id(ed->client);
  155. if(clientid == NULL || strcmp(client->clientid, clientid)){
  156. return MOSQ_ERR_AUTH;
  157. }
  158. }
  159. if(client->pw.valid && dynsec_auth__pw_hash(client, ed->password, password_hash, sizeof(password_hash), false) == MOSQ_ERR_SUCCESS){
  160. if(memcmp_const(client->pw.password_hash, password_hash, sizeof(password_hash)) == 0){
  161. return MOSQ_ERR_SUCCESS;
  162. }else{
  163. return MOSQ_ERR_AUTH;
  164. }
  165. }else{
  166. return MOSQ_ERR_PLUGIN_DEFER;
  167. }
  168. }else{
  169. return MOSQ_ERR_PLUGIN_DEFER;
  170. }
  171. }