EC: Refactor ec_curve_name2nid() to accept NIST curve names
[openssl.git] / crypto / params_from_text.c
index 72770ddc531a13824023fd54982da07d2e23a5f8..71fa8aef9226d765254ed9ccdb22d5d56b1b1852 100644 (file)
@@ -24,7 +24,7 @@ static int prepare_from_text(const OSSL_PARAM *paramdefs, const char *key,
                              const char *value, size_t value_n,
                              /* Output parameters */
                              const OSSL_PARAM **paramdef, int *ishex,
-                             size_t *buf_n, BIGNUM **tmpbn)
+                             size_t *buf_n, BIGNUM **tmpbn, int *found)
 {
     const OSSL_PARAM *p;
 
@@ -38,6 +38,8 @@ static int prepare_from_text(const OSSL_PARAM *paramdefs, const char *key,
         key += 3;
 
     p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key);
+    if (found != NULL)
+        *found = p != NULL;
     if (p == NULL)
         return 0;
 
@@ -107,48 +109,49 @@ static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
     if (buf == NULL)
         return 0;
 
-    switch (paramdef->data_type) {
-    case OSSL_PARAM_INTEGER:
-    case OSSL_PARAM_UNSIGNED_INTEGER:
-        /*
-        {
-            if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
-                BN_free(a);
-                break;
+    if (buf_n > 0) {
+        switch (paramdef->data_type) {
+        case OSSL_PARAM_INTEGER:
+        case OSSL_PARAM_UNSIGNED_INTEGER:
+            /*
+            {
+                if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
+                    BN_free(a);
+                    break;
+                }
+            */
+
+            BN_bn2nativepad(tmpbn, buf, buf_n);
+
+            /*
+             * 2s complement negate, part two.
+             *
+             * Because we did the first part on the BIGNUM itself, we can just
+             * invert all the bytes here and be done with it.
+             */
+            if (paramdef->data_type == OSSL_PARAM_INTEGER
+                && BN_is_negative(tmpbn)) {
+                unsigned char *cp;
+                size_t i = buf_n;
+
+                for (cp = buf; i-- > 0; cp++)
+                    *cp ^= 0xFF;
             }
-        */
-
-        BN_bn2nativepad(tmpbn, buf, buf_n);
-
-        /*
-         * 2s complement negate, part two.
-         *
-         * Because we did the first part on the BIGNUM itself, we can just
-         * invert all the bytes here and be done with it.
-         */
-        if (paramdef->data_type == OSSL_PARAM_INTEGER
-            && BN_is_negative(tmpbn)) {
-            unsigned char *cp;
-            size_t i = buf_n;
-
-            for (cp = buf; i-- > 0; cp++)
-                *cp ^= 0xFF;
-        }
-        break;
-    case OSSL_PARAM_UTF8_STRING:
-        strncpy(buf, value, buf_n);
-        break;
-    case OSSL_PARAM_OCTET_STRING:
-        if (ishex) {
-            size_t l = 0;
-
-            if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value))
-                return 0;
-        } else {
-            memcpy(buf, value, buf_n);
-
+            break;
+        case OSSL_PARAM_UTF8_STRING:
+            strncpy(buf, value, buf_n);
+            break;
+        case OSSL_PARAM_OCTET_STRING:
+            if (ishex) {
+                size_t l = 0;
+
+                if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value))
+                    return 0;
+            } else {
+                memcpy(buf, value, buf_n);
+            }
+            break;
         }
-        break;
     }
 
     *to = *paramdef;
@@ -159,41 +162,10 @@ static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
     return 1;
 }
 
-int OSSL_PARAM_construct_from_text(OSSL_PARAM *to,
-                                   const OSSL_PARAM *paramdefs,
-                                   const char *key, const char *value,
-                                   size_t value_n,
-                                   void *buf, size_t *buf_n)
-{
-    const OSSL_PARAM *paramdef = NULL;
-    int ishex = 0;
-    BIGNUM *tmpbn = NULL;
-    int ok = 0;
-
-    if (to == NULL || paramdefs == NULL)
-        return 0;
-
-    if (!prepare_from_text(paramdefs, key, value, value_n,
-                           &paramdef, &ishex, buf_n, &tmpbn))
-        return 0;
-
-    /*
-     * The user gets the expected buffer size back even if the buffer isn't
-     * allocated.
-     */
-    if (buf == NULL)
-        return 1;
-
-    ok = construct_from_text(to, paramdef, value, value_n, ishex,
-                             buf, *buf_n, tmpbn);
-    BN_free(tmpbn);
-    return ok;
-}
-
 int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
                                   const OSSL_PARAM *paramdefs,
                                   const char *key, const char *value,
-                                  size_t value_n)
+                                  size_t value_n, int *found)
 {
     const OSSL_PARAM *paramdef = NULL;
     int ishex = 0;
@@ -206,10 +178,10 @@ int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
         return 0;
 
     if (!prepare_from_text(paramdefs, key, value, value_n,
-                           &paramdef, &ishex, &buf_n, &tmpbn))
+                           &paramdef, &ishex, &buf_n, &tmpbn, found))
         return 0;
 
-    if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
+    if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL) {
         CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
         return 0;
     }
@@ -217,5 +189,7 @@ int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
     ok = construct_from_text(to, paramdef, value, value_n, ishex,
                              buf, buf_n, tmpbn);
     BN_free(tmpbn);
+    if (!ok)
+        OPENSSL_free(buf);
     return ok;
 }