c - Handling errors in libcrypto BIO base64 decoding -


i've been playing around libcrypto's bio, , can't find way detect errors during base64 decoding.

even if data complete garbage, bio_read returns zero, , error queue - examined err_get_error - remains empty.

the same issue happens bio_flags_base64_no_nl flag: in case of mismatch (that is, data contains newlines, flag set; , vice versa), there's no indication of error, there's no data.

so, there way catch decoding errors?

static unsigned char *base64_decode(unsigned char *data, size_t len, size_t *out_len) {     // chain should     // b64 - mem     // when read b64, gets data mem , decodes      bio *bio_b64;     bio *bio_mem;     size_t res_capacity;     size_t res_size;     unsigned char *res;      size_t ret;      bio_b64 = bio_new(bio_f_base64());     bio_mem = bio_new_mem_buf(data, len);      res_capacity = 1024;     res_size = 0;     res = malloc(res_capacity);      // don't care newlines     bio_set_flags(bio_b64, bio_flags_base64_no_nl);      bio_push(bio_b64, bio_mem);      // empty error queue, in case     while (err_get_error() != 0);      while (1) {         ret = bio_read(bio_b64, &res[res_size], res_capacity - res_size);         if (ret == (res_capacity - res_size)) {             res_size += ret;             res_capacity *= 2;             res = realloc(res, res_capacity);         } else if (ret == 0) {             break;          } else {             res_size += ret;         }     }      if (err_get_error() != 0) {         free(res);         return null;     }      bio_free_all(bio_b64);      *out_len = res_size;     return res; } 

unfortunately bio_read does't emit error if malformed data. can either check expected size (4/3) or reimplement without openssl.


Comments