Check the plaintext buffer is large enough when decrypting SM2
authorMatt Caswell <matt@openssl.org>
Fri, 13 Aug 2021 15:58:21 +0000 (16:58 +0100)
committerMatt Caswell <matt@openssl.org>
Tue, 24 Aug 2021 12:24:36 +0000 (13:24 +0100)
Previously there was no check that the supplied buffer was large enough.
It was just assumed to be sufficient. Instead we should check and fail if
not.

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com>
crypto/sm2/sm2_crypt.c

index 1188abfc6b5756c3bf86fee8ca9e66d22391fec2..00055a4e510e1711bbd040a7c9aa8f94b9112efd 100644 (file)
@@ -294,6 +294,10 @@ int sm2_decrypt(const EC_KEY *key,
     C2 = sm2_ctext->C2->data;
     C3 = sm2_ctext->C3->data;
     msg_len = sm2_ctext->C2->length;
+    if (*ptext_len < (size_t)msg_len) {
+        SM2err(SM2_F_SM2_DECRYPT, SM2_R_BUFFER_TOO_SMALL);
+        goto done;
+    }
 
     ctx = BN_CTX_new();
     if (ctx == NULL) {