lorauth.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. babeld-lor
  3. Copyright (C) 2017 Rodrigo Garcia
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "lorauth.h"
  16. #include "decrypt.h"
  17. #include "babeld.h"
  18. /**
  19. * \brief
  20. * Opens LORAUTH_TOKENS_DIR and LORAUTH_TOKENS_FILE to get the ciphered token
  21. * to be sent in the next update, according to `seqno' and `id' passed as
  22. * arguments, if the token is found it is stored on `dest', if it is not
  23. * , does not change `dest'
  24. * \return
  25. * Returns 0 if found, -1 if not found.
  26. *
  27. * \docs
  28. * See: docs-lorauth/babel-integration.es.md
  29. */
  30. int
  31. lorauth_token(unsigned char *dest, unsigned char id[8],
  32. unsigned short seqno)
  33. {
  34. char *file_abs_path = calloc(1, 150);
  35. strcat(file_abs_path, LORAUTH_TOKENS_DIR);
  36. strcat(file_abs_path, "/");
  37. strcat(file_abs_path, LORAUTH_TOKENS_FILE);
  38. FILE *fp = fopen(file_abs_path, "rt");
  39. int c = 0;
  40. if (fp)
  41. {
  42. // include here LORAUTH_CIPHER_LEN + 1
  43. char *line = (char *)malloc(513);
  44. size_t len = 0;
  45. unsigned int index = lorauth_token_index(id, seqno);
  46. // getting respective index token.
  47. while (getline(&line, &len, fp) != -1)
  48. {
  49. if(c == index)
  50. {
  51. strcpy((char *)dest, line);
  52. break;
  53. }
  54. c++;
  55. }
  56. if(fclose(fp)){
  57. printf(" Error fclose ");
  58. return -1;
  59. }
  60. free(file_abs_path);
  61. free(line);
  62. if(c!=index)
  63. {
  64. printf(" c %d index: %d\n", c, index);
  65. return -1;
  66. }
  67. return 0;
  68. }
  69. else{
  70. fprintf(stderr, "Couldn't read %s file\n", file_abs_path);
  71. free(file_abs_path);
  72. if(fclose(fp)){
  73. printf(" Error fclose ");
  74. }
  75. return -1;
  76. }
  77. }
  78. /**
  79. \brief
  80. Returns the index using:
  81. (((seqno*7)+1) xor (((router_id>>32)>>3) & (router_id&0xFFFFFFFF))) % 100
  82. */
  83. int
  84. lorauth_token_index(unsigned char id[8], unsigned short seqno)
  85. {
  86. unsigned int index = 0;
  87. unsigned char idh[] = {id[0],id[1],id[2],id[3]};
  88. unsigned char idl[] = {id[4],id[5],id[6],id[7]};
  89. unsigned long long H=0;
  90. unsigned long long L=0;
  91. int i=0;
  92. for(i=0; i<4; i++){
  93. H |= (unsigned long long) (idh[i]<<8*(3-i));
  94. L |= (unsigned long long) (idl[i]<<8*(3-i));
  95. }
  96. index = seqno*7;
  97. index += 1;
  98. index ^= (H>>3) & L;
  99. index %= 100;
  100. return index;
  101. }
  102. /**
  103. \brief
  104. * Checks if the cipher text is a valid authentication token
  105. * and it is following the lorauth specs (see doc-lorauth/)
  106. \returns
  107. * 0 if it is a valid ciphertext
  108. * -1 if the ciphertext is invalid or can't be decrypted using
  109. * the given public key.
  110. */
  111. int
  112. check_lorauth_token(unsigned char id[8],
  113. unsigned char prefix[16],
  114. unsigned short seqno,
  115. unsigned short clen,
  116. unsigned char *cipher)
  117. {
  118. if(clen != strlen((char *)cipher))
  119. {
  120. printf("\tcipher size (%d) different than clen %d",
  121. strlen((char*)cipher), clen);
  122. return -1;
  123. }
  124. /* if(clen < 513){ */
  125. /* /\* Seems that rsa_decrypt expects a message which ends in \n (10)*\/ */
  126. /* printf("\tcorrecting\t"); */
  127. /* strcat((char*)cipher, "\n"); */
  128. /* } */
  129. /* printf("%s\n",cipher); */
  130. /* int i; */
  131. /* for(i=0; i<clen; i++) */
  132. /* printf("%d ",(unsigned char)cipher[i]); */
  133. /* printf("-- %d \n",clen); */
  134. //printf("rsA_result decrypted: %d\t",strlen((char *)rsa_result));
  135. int rc = rsa_decrypt(&rsa_context, &rsa_entropy, &rsa_ctr_drbg,
  136. (char *)cipher, (unsigned char *)&rsa_result);
  137. if(rc!=0)
  138. {
  139. printf("\tFailed to decrypt ciphertext\n");
  140. return -1;
  141. }
  142. /* looking for the given prefix in the decrypted token
  143. for now it looks a contiguous space, but the prefix may be
  144. contained in an arbitrary order in the ciphertext later on.
  145. */
  146. // ipv4
  147. /*TODO: Add automatic check, by parsing by "." in decrypted string.
  148. the prefix is received like (0 0 0 0 0 0 0 0 0 0 255 255 80 0 1 0)
  149. */
  150. if(
  151. prefix[12] != (rsa_result[0]-48)*10 + (rsa_result[1]-48) ||
  152. prefix[13] != rsa_result[3]-48 ||
  153. prefix[14] != rsa_result[5]-48 ||
  154. prefix[15] != rsa_result[7]-48)
  155. {
  156. printf("\tprefix not found in decrypted token (contiguous check)\n");
  157. printf("\trsa_result: %s \nprefix: ", rsa_result);
  158. int i;
  159. for(i=0; i<16; i++)
  160. printf("%d ", (unsigned char)prefix[i]);
  161. return -1;
  162. }
  163. //TODO: implement ipv6 check
  164. //...
  165. unsigned int index = lorauth_token_index(id, seqno);
  166. size_t dc = strlen((char *)rsa_result);
  167. // only the last two (ascii) chars for now
  168. int ircv = (rsa_result[dc-2]-48)*10 + (rsa_result[dc-1]-48);
  169. if( ircv != index )
  170. {
  171. printf("\tnot correct token index received (%d), expected %d\n",
  172. ircv, index);
  173. printf("\trsa_result: %s \n", rsa_result);
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. /* Some utils */
  179. const char* reduced_lorauth_token(const unsigned char *token){
  180. static char red[8];
  181. if(strlen((char*)token)<4)
  182. {
  183. strcpy(red, "none.");
  184. return red;
  185. }
  186. else
  187. {
  188. strncpy(red, (char*)token, 3);
  189. strcpy(red+3, "..");
  190. strncpy(red+5, (char*)token+(LORAUTH_CIPHER_LEN-2), 2);
  191. return red;
  192. }
  193. }
  194. void clean_cipher(unsigned char *buffered_cipher){
  195. memset(buffered_cipher, 0, 514);
  196. }