Hi team, I find a segment fault in BIO_read() on openssl 3.0 about calculate digest with BIO using md4 algorithm. This is my test code, put it in a.c, build & run, it will crash. If don’t load legacy provider:
If load legacy provider, this program works ok. ------------------------------------------------------------------------ #include <openssl/provider.h> #include <openssl/err.h> #include <openssl/types.h> #include <openssl/evp.h> #include <openssl/bn.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { EVP_MD *md = NULL; const char *datain = "a.c"; const int BUF_SIZE = 8192; char *buff = NULL; char *ptr = NULL; const char *dgst = "md4"; int ret = 0, len = 0, i = 0; BIO *in = NULL, *out = NULL, *bmd = NULL, *rbio = NULL, *err; if ((err = BIO_new(BIO_s_file())) != NULL) BIO_set_fp(err, stderr, BIO_NOCLOSE|BIO_FP_TEXT); md = EVP_get_digestbyname(dgst); if (!md) { printf("Error EVP_get_digestbyname %s\n", dgst); goto err_exit; } in = BIO_new_file(datain, "rb"); if (!in) { printf("Error BIO_new_file %s\n", datain); goto err_exit; } out = BIO_new(BIO_s_mem()); if (!out) { printf("Error BIO_new out\n"); goto err_exit; } rbio = in; bmd = BIO_new(BIO_f_md()); if (!bmd){ printf("Error BIO_new bmd\n"); goto err_exit; } BIO_set_md(bmd, md); rbio = BIO_push(bmd, rbio); buff = (char *)malloc(BUF_SIZE); if (!buff) { printf("Error malloc\n"); goto err_exit; } for (;;) { ret = BIO_read(rbio, buff, BUF_SIZE); /* this call will segment fault */ if (ret <= 0) break; } len = BIO_gets(rbio, buff, BUF_SIZE); len = BIO_write(out, buff, len); if (!BIO_flush(out)) { printf("Error BIO_flush\n"); goto err_exit; } len = BIO_get_mem_data(out, &ptr); printf("digest success, len=%d\n", len); for (i = 0; i < len; i++) printf("%d ", ptr[i]); printf("\n"); err_exit: ERR_print_errors(err); if (in) BIO_free(in); if (out) BIO_free_all(out); if (err) BIO_free(err); if (bmd) BIO_free(bmd); if (buff) free(buff); if (md) EVP_MD_free(md); ERR_clear_error(); return 0; } --------------------------------------------------------------------------------- ================================ |