Pārlūkot izejas kodu

fareqr hacking

* bug fix, not closing file descriptor when found
* added helper function to lookup from just seed file and fareqr string
* compiling proper files
* passdb compiles now (but is completely untested)
clementinecomputing 4 gadi atpakaļ
vecāks
revīzija
4224b1bea6

+ 1 - 0
busunit/common/common_config.h

@@ -321,6 +321,7 @@
 //Local database files
 #define BILLING_FILE   (DATABASE_FILE_PATH "billing.mem")
 #define PASSES_FILE    (DATABASE_FILE_PATH "passes.mem")
+#define QRSEED_FILE    (DATABASE_FILE_PATH "qr.seed")
 
 //Local configuration files
 #define DRIVER_MENU_FILE  (CONFIG_FILE_PATH "menu.xml")

+ 1 - 1
busunit/passdb/buildit.sh

@@ -6,7 +6,7 @@ rm -f passdb scheme.o
 
 #$target_cc $tinyscheme_opts tinyscheme1.39/scheme.c
 $target_cc $tinyscheme_opts tinyscheme/scheme.c
-$target_cc $target_ccopts -g -o passdb passdb.c rfid_decoder.c pass_communication.c rules.c qr_lookup.c ../common/common_defs.c ../common/gpsmath.c ../commhub/commhub.c ../commhub/client_utils.c scheme.o -lm -lz
+$target_cc $target_ccopts -g -o passdb passdb.c rfid_decoder.c pass_communication.c rules.c fareqr.c b64.c ../common/common_defs.c ../common/gpsmath.c ../commhub/commhub.c ../commhub/client_utils.c scheme.o -lm -lz
 
 rm -f send_magstripe
 $target_cc $target_ccopts -o send_magstripe send_magstripe.c ../commhub/commhub.c ../common/common_defs.c

+ 88 - 37
busunit/passdb/fareqr.c

@@ -20,11 +20,56 @@
 
 #include "fareqr.h"
 
+int fareqr_lookup_decode(char *seedfn, char *fareqr_s, char *dst_cred) {
+  int i, n, r, _ret = 0;;
+  char *p=NULL, *stop_tok=NULL;
+  char *enc_str = NULL, *dec_str = NULL, *plain_str=NULL,
+    *pub_key = NULL, *priv_key = NULL;
+
+  if ((!seedfn) || (!fareqr_s) || (!dst_cred)) { return -1; }
+  if (fareqr_s[0] != '@') { return -2; }
+  stop_tok = strchr(fareqr_s, '%');
+  if (!stop_tok) { return -3; }
+
+  pub_key = (char *)malloc(sizeof(char)*LINE_BUFFER_SIZE);
+  priv_key = (char *)malloc(sizeof(char)*LINE_BUFFER_SIZE);
+  enc_str = (char *)malloc(sizeof(char)*LINE_BUFFER_SIZE);
+  dec_str = (char *)malloc(sizeof(char)*LINE_BUFFER_SIZE);
+  plain_str = (char *)malloc(sizeof(char)*LINE_BUFFER_SIZE);
+  pub_key[0] = priv_key[0] = enc_str[0] = dec_str[0] = plain_str[0] = '\0';
+
+  for (i=0, p = (fareqr_s+1); (i<(LINE_BUFFER_SIZE-1)) && (p<stop_tok) && (*p); p++, i++) {
+    pub_key[i] = *p;
+  }
+  pub_key[i] = '\0';
+
+  for (n=0, p = (stop_tok+1); (n<(LINE_BUFFER_SIZE-1)) && (*p) && ((*p) != '$'); p++, n++) {
+    enc_str[n] = *p;
+  }
+  enc_str[n] = '\0';
+
+  r = fareqr_lookup_seed_secret(seedfn, pub_key, priv_key);
+  if (r<0) { _ret = r; }
+  else {
+    r = fareqr_decode(fareqr_s, pub_key, priv_key, dst_cred);
+    if (r<0) { _ret = r; }
+  }
+
+  if (pub_key) { free(pub_key); }
+  if (priv_key) { free(priv_key); }
+  if (enc_str) { free(enc_str); }
+  if (dec_str) { free(dec_str); }
+  if (plain_str) { free(plain_str); }
+
+  return _ret;
+}
+
 int fareqr_encode(char *tok_public, char *tok_secret, char *tok_cred, char *fareqr_str) {
   int i, r;
   uint8_t x,y,z;
   uint8_t *src_data = NULL, *dst_data = NULL;
-  int src_data_n = 0, dst_data_n = 0;
+  int src_data_n = 0;
+  //int dst_data_n = 0;
 
   src_data = (uint8_t *)malloc(sizeof(uint8_t)*LINE_BUFFER_SIZE);
   dst_data = (uint8_t *)malloc(sizeof(uint8_t)*LINE_BUFFER_SIZE);
@@ -41,12 +86,17 @@ int fareqr_encode(char *tok_public, char *tok_secret, char *tok_cred, char *fare
     x = (uint8_t)tok_secret[i];
   }
 
-  dst_data_n = Base64encode_len(src_data_n);
+  //dst_data_n = Base64encode_len(src_data_n);
 
   r = Base64encode((char *)dst_data, (const char *)src_data, src_data_n);
+  if (r<=0) {
+    free(src_data);
+    free(dst_data);
+    return -1;
+  }
 
   if (fareqr_str) {
-    snprintf(fareqr_str, LINE_BUFFER_SIZE-1, "@%s%%%s$\n", tok_public, dst_data);
+    snprintf(fareqr_str, LINE_BUFFER_SIZE-1, "@%s%%%s$", tok_public, dst_data);
   }
 
   free(src_data);
@@ -57,7 +107,7 @@ int fareqr_encode(char *tok_public, char *tok_secret, char *tok_cred, char *fare
 
 
 int fareqr_decode(char *fareqr_s, char *check_pub, char *tok_secret, char *dst_cred) {
-  int i, n;
+  int i, n, _ret=0;
   char *p, *stop_tok;
   int pub_tok_read_len = 0, check_pub_len = 0;
 
@@ -88,7 +138,10 @@ int fareqr_decode(char *fareqr_s, char *check_pub, char *tok_secret, char *dst_c
   Base64decode(dec_str, enc_str);
 
   for (i=0; dec_str[i]; i++) {
-    if (tok_secret[i]==0) { return -5; }
+    if (tok_secret[i]==0) {
+      _ret = -5;
+      goto _fareqr_decode_cleanup;
+    }
     x = (uint8_t)dec_str[i];
     y = (uint8_t)tok_secret[i];
     z = (x^y);
@@ -97,21 +150,20 @@ int fareqr_decode(char *fareqr_s, char *check_pub, char *tok_secret, char *dst_c
   plain_str[i]='\0';
 
 
-
-  printf("%s\n", plain_str);
-
   if (dst_cred) {
-    for (i=0; dec_str[i]; i++) {
-      dst_cred[i] = dec_str[i];
+    for (i=0; plain_str[i]; i++) {
+      dst_cred[i] = plain_str[i];
     }
     dst_cred[i] = '\0';
   }
 
-  free(enc_str);
-  free(dec_str);
-  free(plain_str);
+_fareqr_decode_cleanup:
 
-  return 0;
+  if (enc_str) { free(enc_str); }
+  if (dec_str) { free(dec_str); }
+  if (plain_str) { free(plain_str); }
+
+  return _ret;
 }
 
 // return negative on error or not found
@@ -121,13 +173,10 @@ int fareqr_lookup_seed_secret(char *seedfn, char *pub, char *priv) {
   int i;
   FILE *fp;
   char buf[LINE_BUFFER_SIZE] = {0};
-  int pos = 0, ch=0, line_no=0, src_n;
-  char *p = NULL;
+  int pos = 0, ch=0, line_no=0;
   char *tok0_ptr=NULL,
        *tok1_ptr=NULL;
 
-  uint8_t z;
-
   fp = fopen(seedfn, "r");
   if (!fp) { perror(seedfn); return -2;}
   while (!feof(fp)) {
@@ -165,6 +214,7 @@ int fareqr_lookup_seed_secret(char *seedfn, char *pub, char *priv) {
               priv[i] = tok1_ptr[i];
             }
             priv[i]='\0';
+            fclose(fp);
             return 0;
 
           }
@@ -197,7 +247,7 @@ void show_help(FILE *ofp) {
   fprintf(ofp, "\nusage:\n\n");
   fprintf(ofp, "  fareqr encode <pubkey> <privatekey> <str>\n");
   fprintf(ofp, "  fareqr decode <privatekey> <encstr>\n");
-  fprintf(ofp, "  fareqr dbdecode <qrseedfile> <pubkey> <encstr>\n");
+  fprintf(ofp, "  fareqr dbdecode <qrseedfile> <fareqr>\n");
   fprintf(ofp, "  fareqr help\n");
   fprintf(ofp, "\n");
   fprintf(ofp, "fareqr is a program to help with encoding and decoding 'fareqr' strings.\n");
@@ -216,11 +266,11 @@ void show_help(FILE *ofp) {
   fprintf(ofp, "Here is some example usage:\n");
   fprintf(ofp, "\n");
   fprintf(ofp, "  $ fareqr encode 'wu9XouSh' 'ohNgizahkephain3aosoh2AeH1aethoo4cie6oiSaezimaighai2eiVaefahfien' ';123456789060535?'\n");
-  fprintf(ofp, "  @wu9XouSh%VFl8VF1PV19TXEBeUVxdBl4=\n");
-  fprintf(ofp, "  $ fareqr decode 'ohNgizahkephain3aosoh2AeH1aethoo4cie6oiSaezimaighai2eiVaefahfien' '@wu9XouSh%VFl8VF1PV19TXEBeUVxdBl4=$'\n");
+  fprintf(ofp, "  @wu9XouSh%%VFl8VF1PV19TXEBeUVxdBl4=\n");
+  fprintf(ofp, "  $ fareqr decode 'ohNgizahkephain3aosoh2AeH1aethoo4cie6oiSaezimaighai2eiVaefahfien' '@wu9XouSh%%VFl8VF1PV19TXEBeUVxdBl4=$'\n");
   fprintf(ofp, "  ;123456789060535?\n");
   fprintf(ofp, "  $ echo 'wu9XouSh ohNgizahkephain3aosoh2AeH1aethoo4cie6oiSaezimaighai2eiVaefahfien' > ./qr.seed\n");
-  fprintf(ofp, "  $ fareqr dbdecode ./qr.seed 'wu9XouSh' '@wu9XouSh%VFl8VF1PV19TXEBeUVxdBl4=$'\n");
+  fprintf(ofp, "  $ fareqr dbdecode ./qr.seed '@wu9XouSh%%VFl8VF1PV19TXEBeUVxdBl4=$'\n");
   fprintf(ofp, "  ;123456789060535?\n");
   fprintf(ofp, "\n");
   fprintf(ofp, "Where 'wu9XouSh' is the public key, 'ohNgizahkephain3aosoh2AeH1aethoo4cie6oiSaezimaighai2eiVaefahfien' is the private key and\n");
@@ -240,7 +290,7 @@ void show_help(FILE *ofp) {
  * $ fareqr decode 'ohNgizahkephain3aosoh2AeH1aethoo4cie6oiSaezimaighai2eiVaefahfien' '@wu9XouSh%VFl8VF1PV19TXEBeUVxdBl4=$' 
  * ;123456789060535?
  *
- * $ fareqr dbdecode 'wu9XouSh' '@wu9XouSh%VFl8VF1PV19TXEBeUVxdBl4=$'
+ * $ fareqr dbdecode '@wu9XouSh%VFl8VF1PV19TXEBeUVxdBl4=$'
  * ;123456789060535?
  *
  */
@@ -297,35 +347,36 @@ int main(int argc, char **argv) {
         fareqr_str = strdup(argv[3]);
       }
     }
-    r = fareqr_decode(fareqr_str, tok_public, tok_secret, NULL);
+    tok_cred = (char *)malloc(sizeof(char)*LINE_BUFFER_SIZE);
+    tok_cred[0] = '0';
+    r = fareqr_decode(fareqr_str, tok_public, tok_secret, tok_cred);
     if (r<0) {
       fprintf(stderr, "error, failed to decode qr fare string (%i)\n", r);
     }
+    else {
+      printf("%s\n", tok_cred);
+    }
   }
 
   else if (strcmp(argv[1], "dbdecode")==0) {
     if (argc>2) {
       fn = strdup(argv[2]);
       if (argc>3) {
-        tok_public = strdup(argv[3]);
-        if (argc>4) {
-          fareqr_str = strdup(argv[4]);
-        }
+        fareqr_str = strdup(argv[3]);
       }
     }
-    tok_secret = (char *)malloc(sizeof(char)*LINE_BUFFER_SIZE);
-    tok_secret[0] = '\0';
-    r = fareqr_lookup_seed_secret(fn, tok_public, tok_secret);
+
+    tok_cred = (char *)malloc(sizeof(char)*LINE_BUFFER_SIZE);
+    tok_cred[0] = '0';
+    r = fareqr_lookup_decode(fn, fareqr_str, tok_cred);
     if (r<0) {
-      fprintf(stderr, "could not find '%s' in db '%s', exiting (got %i)\n",
-          tok_public, fn, r);
+      fprintf(stderr, "could not decode '%s' with db '%s', exiting (got %i)\n",
+          fareqr_str, fn, r);
     }
     else {
-      r = fareqr_decode(fareqr_str, tok_public, tok_secret, NULL);
-      if (r<0) {
-        fprintf(stderr, "error, failed to decode qr fare string (%i)\n", r);
-      }
+      printf("%s\n", tok_cred);
     }
+
   }
 
   else if (strcmp(argv[1], "help")==0) {

+ 1 - 0
busunit/passdb/fareqr.h

@@ -36,5 +36,6 @@
 int fareqr_encode(char *tok_public, char *tok_secret, char *tok_cred, char *fareqr_str);
 int fareqr_decode(char *fareqr_s, char *check_pub, char *tok_secret, char *dst_cred);
 int fareqr_lookup_seed_secret(char *seedfn, char *pub, char *priv);
+int fareqr_lookup_decode(char *seedfn, char *fareqr_s, char *dst_cred);
 
 #endif

+ 5 - 3
busunit/passdb/pass_communication.c

@@ -42,6 +42,8 @@
 #include "rules.h"
 #include "rfid_decoder.h"
 
+#include "fareqr.h"
+
 //----------GLOBAL STATE VARIABLES
 
 int flush_in_progress = 0;  //This flag is used to tell if there is a flush in progress
@@ -954,7 +956,7 @@ message_callback_return handle_token_rfid_message(struct message_record *msg, vo
 }
 
 message_callback_return handle_token_qr_message(struct message_record *msg, void *param) {
-  int idx;
+  int idx, r;
   char cred[LINE_BUFFER_SIZE] = {0};
   passdb_context *ctx = (passdb_context *)param;
 
@@ -973,8 +975,8 @@ message_callback_return handle_token_qr_message(struct message_record *msg, void
 
   //---
 
-  idx = lookup_qr_credential(ctx, (char *)(msg->payload), clear_payload);
-  if (idx < 0) {
+  r = fareqr_lookup_decode(QRSEED_FILE, (char *)(msg->payload), clear_payload);
+  if (r<0) {
     reject_unknown_card(cred);
     return MESSAGE_HANDLED_CONT;
   }