|
|
@@ -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) {
|