OSSL_PARAM functions: change to allow the data field to be NULL
authorRichard Levitte <levitte@openssl.org>
Thu, 26 Sep 2019 05:45:33 +0000 (07:45 +0200)
committerRichard Levitte <levitte@openssl.org>
Fri, 27 Sep 2019 17:03:34 +0000 (19:03 +0200)
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/10025)

crypto/params.c
doc/man3/OSSL_PARAM_int.pod

index 20082ad90bda03d306313187896f65ce5fdbcfdb..b2ceb132781fb9902414005500e5265a28fc3b0f 100644 (file)
@@ -211,6 +211,8 @@ int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
     p->return_size = 0;
     if (p->data_type == OSSL_PARAM_INTEGER) {
         p->return_size = sizeof(int32_t); /* Minimum expected size */
     p->return_size = 0;
     if (p->data_type == OSSL_PARAM_INTEGER) {
         p->return_size = sizeof(int32_t); /* Minimum expected size */
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(int32_t):
             *(int32_t *)p->data = val;
         switch (p->data_size) {
         case sizeof(int32_t):
             *(int32_t *)p->data = val;
@@ -222,6 +224,8 @@ int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
         }
     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
         p->return_size = sizeof(uint32_t); /* Minimum expected size */
         }
     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
         p->return_size = sizeof(uint32_t); /* Minimum expected size */
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(uint32_t):
             *(uint32_t *)p->data = (uint32_t)val;
         switch (p->data_size) {
         case sizeof(uint32_t):
             *(uint32_t *)p->data = (uint32_t)val;
@@ -233,6 +237,8 @@ int OSSL_PARAM_set_int32(OSSL_PARAM *p, int32_t val)
         }
     } else if (p->data_type == OSSL_PARAM_REAL) {
         p->return_size = sizeof(double);
         }
     } else if (p->data_type == OSSL_PARAM_REAL) {
         p->return_size = sizeof(double);
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(double):
             *(double *)p->data = (double)val;
         switch (p->data_size) {
         case sizeof(double):
             *(double *)p->data = (double)val;
@@ -310,6 +316,8 @@ int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
 
     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
         p->return_size = sizeof(uint32_t); /* Minimum expected size */
 
     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
         p->return_size = sizeof(uint32_t); /* Minimum expected size */
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(uint32_t):
             *(uint32_t *)p->data = val;
         switch (p->data_size) {
         case sizeof(uint32_t):
             *(uint32_t *)p->data = val;
@@ -321,6 +329,8 @@ int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
         }
     } else if (p->data_type == OSSL_PARAM_INTEGER) {
         p->return_size = sizeof(int32_t); /* Minimum expected size */
         }
     } else if (p->data_type == OSSL_PARAM_INTEGER) {
         p->return_size = sizeof(int32_t); /* Minimum expected size */
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(int32_t):
             if (val <= INT32_MAX) {
         switch (p->data_size) {
         case sizeof(int32_t):
             if (val <= INT32_MAX) {
@@ -335,6 +345,8 @@ int OSSL_PARAM_set_uint32(OSSL_PARAM *p, uint32_t val)
         }
     } else if (p->data_type == OSSL_PARAM_REAL) {
         p->return_size = sizeof(double);
         }
     } else if (p->data_type == OSSL_PARAM_REAL) {
         p->return_size = sizeof(double);
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(double):
             *(double *)p->data = (double)val;
         switch (p->data_size) {
         case sizeof(double):
             *(double *)p->data = (double)val;
@@ -403,6 +415,8 @@ int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
     p->return_size = 0;
     if (p->data_type == OSSL_PARAM_INTEGER) {
         p->return_size = sizeof(int64_t); /* Expected size */
     p->return_size = 0;
     if (p->data_type == OSSL_PARAM_INTEGER) {
         p->return_size = sizeof(int64_t); /* Expected size */
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(int32_t):
             if (val >= INT32_MIN && val <= INT32_MAX) {
         switch (p->data_size) {
         case sizeof(int32_t):
             if (val >= INT32_MIN && val <= INT32_MAX) {
@@ -417,6 +431,8 @@ int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
         }
     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
         p->return_size = sizeof(uint64_t); /* Expected size */
         }
     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER && val >= 0) {
         p->return_size = sizeof(uint64_t); /* Expected size */
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(uint32_t):
             if (val <= UINT32_MAX) {
         switch (p->data_size) {
         case sizeof(uint32_t):
             if (val <= UINT32_MAX) {
@@ -431,6 +447,8 @@ int OSSL_PARAM_set_int64(OSSL_PARAM *p, int64_t val)
         }
     } else if (p->data_type == OSSL_PARAM_REAL) {
         p->return_size = sizeof(double);
         }
     } else if (p->data_type == OSSL_PARAM_REAL) {
         p->return_size = sizeof(double);
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(double):
             u64 = val < 0 ? -val : val;
         switch (p->data_size) {
         case sizeof(double):
             u64 = val < 0 ? -val : val;
@@ -506,6 +524,8 @@ int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
 
     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
         p->return_size = sizeof(uint64_t); /* Expected size */
 
     if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
         p->return_size = sizeof(uint64_t); /* Expected size */
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(uint32_t):
             if (val <= UINT32_MAX) {
         switch (p->data_size) {
         case sizeof(uint32_t):
             if (val <= UINT32_MAX) {
@@ -520,6 +540,8 @@ int OSSL_PARAM_set_uint64(OSSL_PARAM *p, uint64_t val)
         }
     } else if (p->data_type == OSSL_PARAM_INTEGER) {
         p->return_size = sizeof(int64_t); /* Expected size */
         }
     } else if (p->data_type == OSSL_PARAM_INTEGER) {
         p->return_size = sizeof(int64_t); /* Expected size */
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(int32_t):
             if (val <= INT32_MAX) {
         switch (p->data_size) {
         case sizeof(int32_t):
             if (val <= INT32_MAX) {
@@ -616,6 +638,8 @@ int OSSL_PARAM_set_BN(OSSL_PARAM *p, const BIGNUM *val)
 
     bytes = (size_t)BN_num_bytes(val);
     p->return_size = bytes;
 
     bytes = (size_t)BN_num_bytes(val);
     p->return_size = bytes;
+    if (p->data == NULL)
+        return 1;
     return p->data_size >= bytes
         && BN_bn2nativepad(val, p->data, bytes) >= 0;
 }
     return p->data_size >= bytes
         && BN_bn2nativepad(val, p->data, bytes) >= 0;
 }
@@ -680,6 +704,8 @@ int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
 
     if (p->data_type == OSSL_PARAM_REAL) {
         p->return_size = sizeof(double);
 
     if (p->data_type == OSSL_PARAM_REAL) {
         p->return_size = sizeof(double);
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(double):
             *(double *)p->data = val;
         switch (p->data_size) {
         case sizeof(double):
             *(double *)p->data = val;
@@ -688,6 +714,8 @@ int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
                && val == (ossl_uintmax_t)val) {
         p->return_size = sizeof(double);
     } else if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
                && val == (ossl_uintmax_t)val) {
         p->return_size = sizeof(double);
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(uint32_t):
             if (val >= 0 && val <= UINT32_MAX) {
         switch (p->data_size) {
         case sizeof(uint32_t):
             if (val >= 0 && val <= UINT32_MAX) {
@@ -705,6 +733,8 @@ int OSSL_PARAM_set_double(OSSL_PARAM *p, double val)
             break;            }
     } else if (p->data_type == OSSL_PARAM_INTEGER && val == (ossl_intmax_t)val) {
         p->return_size = sizeof(double);
             break;            }
     } else if (p->data_type == OSSL_PARAM_INTEGER && val == (ossl_intmax_t)val) {
         p->return_size = sizeof(double);
+        if (p->data == NULL)
+            return 1;
         switch (p->data_size) {
         case sizeof(int32_t):
             if (val >= INT32_MIN && val <= INT32_MAX) {
         switch (p->data_size) {
         case sizeof(int32_t):
             if (val >= INT32_MIN && val <= INT32_MAX) {
@@ -775,6 +805,8 @@ static int set_string_internal(OSSL_PARAM *p, const void *val, size_t len,
                                unsigned int type)
 {
     p->return_size = len;
                                unsigned int type)
 {
     p->return_size = len;
+    if (p->data == NULL)
+        return 1;
     if (p->data_type != type || p->data_size < len)
         return 0;
 
     if (p->data_type != type || p->data_size < len)
         return 0;
 
@@ -847,7 +879,8 @@ static int set_ptr_internal(OSSL_PARAM *p, const void *val,
     p->return_size = len;
     if (p->data_type != type)
         return 0;
     p->return_size = len;
     if (p->data_type != type)
         return 0;
-    *(const void **)p->data = val;
+    if (p->data != NULL)
+        *(const void **)p->data = val;
     return 1;
 }
 
     return 1;
 }
 
index 742e8d57745942e1d1ad428d257d3b30165e7505..991b3d1212fa204db23a41b4066662508c4428ca 100644 (file)
@@ -198,6 +198,8 @@ Type coercion takes place as discussed in the NOTES section.
 
 OSSL_PARAM_set_TYPE() stores a value B<val> of type B<TYPE> into the parameter
 B<p>.
 
 OSSL_PARAM_set_TYPE() stores a value B<val> of type B<TYPE> into the parameter
 B<p>.
+If the parameter's I<data> field is NULL, then only its I<return_size> field
+will be assigned the size the parameter's I<data> buffer should have.
 Type coercion takes place as discussed in the NOTES section.
 
 OSSL_PARAM_get_BN() retrieves a BIGNUM from the parameter pointed to by B<p>.
 Type coercion takes place as discussed in the NOTES section.
 
 OSSL_PARAM_get_BN() retrieves a BIGNUM from the parameter pointed to by B<p>.
@@ -205,6 +207,8 @@ The BIGNUM referenced by B<val> is updated and is allocated if B<*val> is
 B<NULL>.
 
 OSSL_PARAM_set_BN() stores the BIGNUM B<val> into the parameter B<p>.
 B<NULL>.
 
 OSSL_PARAM_set_BN() stores the BIGNUM B<val> into the parameter B<p>.
+If the parameter's I<data> field is NULL, then only its I<return_size> field
+will be assigned the size the parameter's I<data> buffer should have.
 
 OSSL_PARAM_get_utf8_string() retrieves a UTF8 string from the parameter
 pointed to by B<p>.
 
 OSSL_PARAM_get_utf8_string() retrieves a UTF8 string from the parameter
 pointed to by B<p>.
@@ -215,6 +219,8 @@ If memory is allocated by this function, it must be freed by the caller.
 
 OSSL_PARAM_set_utf8_string() sets a UTF8 string from the parameter pointed to
 by B<p> to the value referenced by B<val>.
 
 OSSL_PARAM_set_utf8_string() sets a UTF8 string from the parameter pointed to
 by B<p> to the value referenced by B<val>.
+If the parameter's I<data> field is NULL, then only its I<return_size> field
+will be assigned the size the parameter's I<data> buffer should have.
 
 OSSL_PARAM_get_octet_string() retrieves an OCTET string from the parameter
 pointed to by B<p>.
 
 OSSL_PARAM_get_octet_string() retrieves an OCTET string from the parameter
 pointed to by B<p>.
@@ -225,6 +231,8 @@ If memory is allocated by this function, it must be freed by the caller.
 
 OSSL_PARAM_set_octet_string() sets an OCTET string from the parameter
 pointed to by B<p> to the value referenced by B<val>.
 
 OSSL_PARAM_set_octet_string() sets an OCTET string from the parameter
 pointed to by B<p> to the value referenced by B<val>.
+If the parameter's I<data> field is NULL, then only its I<return_size> field
+will be assigned the size the parameter's I<data> buffer should have.
 
 OSSL_PARAM_get_utf8_ptr() retrieves the UTF8 string pointer from the parameter
 referenced by B<p> and stores it in B<*val>.
 
 OSSL_PARAM_get_utf8_ptr() retrieves the UTF8 string pointer from the parameter
 referenced by B<p> and stores it in B<*val>.
@@ -260,9 +268,9 @@ representable by the target type or parameter.
 Apart from that, the functions must be used appropriately for the
 expected type of the parameter.
 
 Apart from that, the functions must be used appropriately for the
 expected type of the parameter.
 
-For OSSL_PARAM_get_utf8_ptr() and OSSL_PARAM_get_octet_ptr(), B<bsize>
-is not relevant if the purpose is to send the B<OSSL_PARAM> array to a
-I<responder>, i.e. to get parameter data back.
+For OSSL_PARAM_construct_utf8_ptr() and OSSL_PARAM_consstruct_octet_ptr(),
+B<bsize> is not relevant if the purpose is to send the B<OSSL_PARAM> array
+to a I<responder>, i.e. to get parameter data back.
 In that case, B<bsize> can safely be given zero.
 See L<OSSL_PARAM(3)/DESCRIPTION> for further information on the
 possible purposes.
 In that case, B<bsize> can safely be given zero.
 See L<OSSL_PARAM(3)/DESCRIPTION> for further information on the
 possible purposes.