goto done;
}
- ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
+ ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, NULL);
/* Ensure cast to size_t is safe */
if (ciphertext_leni < 0) {
ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
+
+ if (*ciphertext_len < (size_t)ciphertext_leni) {
+ ERR_raise(ERR_LIB_SM2, SM2_R_BUFFER_TOO_SMALL);
+ goto done;
+ }
+
+ ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
+ if (ciphertext_leni < 0) {
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
+ goto done;
+ }
*ciphertext_len = (size_t)ciphertext_leni;
rc = 1;