]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
apps_ui.c: Correct password prompt for ui_method
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Mon, 11 May 2020 13:31:53 +0000 (15:31 +0200)
committerDr. David von Oheimb <David.von.Oheimb@siemens.com>
Thu, 10 Sep 2020 20:01:07 +0000 (22:01 +0200)
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/12493)

apps/lib/apps_ui.c
crypto/ui/ui_lib.c
doc/man3/UI_new.pod
include/openssl/ui.h

index dd794ec747ec724fdb95b3cec724054e29b06e47..880e9a4f6d84257e1a8c7eea00fe151df44872da 100644 (file)
@@ -99,6 +99,19 @@ static int ui_close(UI *ui)
     return 1;
 }
 
+/* object_name defaults to prompt_info from ui user data if present */
+static char *ui_prompt_construct(UI *ui, const char *phrase_desc,
+                                 const char *object_name)
+{
+    PW_CB_DATA *cb_data = (PW_CB_DATA *)UI_get0_user_data(ui);
+
+    if (phrase_desc == NULL)
+        phrase_desc = "pass phrase";
+    if (object_name == NULL && cb_data != NULL)
+        object_name = cb_data->prompt_info;
+    return UI_construct_prompt(NULL, phrase_desc, object_name);
+}
+
 int setup_ui_method(void)
 {
     ui_fallback_method = UI_null();
index 85bf8c1f80b615be7f4b0baae58f58f8a19e7fed..8c6dc6dd89981348f89a811a264b2c845bbfaa21 100644 (file)
@@ -356,22 +356,22 @@ int UI_dup_error_string(UI *ui, const char *text)
                                    0, 0, NULL);
 }
 
-char *UI_construct_prompt(UI *ui, const char *object_desc,
+char *UI_construct_prompt(UI *ui, const char *phrase_desc,
                           const char *object_name)
 {
     char *prompt = NULL;
 
-    if (ui->meth->ui_construct_prompt != NULL)
-        prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
+    if (ui != NULL && ui->meth != NULL && ui->meth->ui_construct_prompt != NULL)
+        prompt = ui->meth->ui_construct_prompt(ui, phrase_desc, object_name);
     else {
         char prompt1[] = "Enter ";
         char prompt2[] = " for ";
         char prompt3[] = ":";
         int len = 0;
 
-        if (object_desc == NULL)
+        if (phrase_desc == NULL)
             return NULL;
-        len = sizeof(prompt1) - 1 + strlen(object_desc);
+        len = sizeof(prompt1) - 1 + strlen(phrase_desc);
         if (object_name != NULL)
             len += sizeof(prompt2) - 1 + strlen(object_name);
         len += sizeof(prompt3) - 1;
@@ -381,7 +381,7 @@ char *UI_construct_prompt(UI *ui, const char *object_desc,
             return NULL;
         }
         OPENSSL_strlcpy(prompt, prompt1, len + 1);
-        OPENSSL_strlcat(prompt, object_desc, len + 1);
+        OPENSSL_strlcat(prompt, phrase_desc, len + 1);
         if (object_name != NULL) {
             OPENSSL_strlcat(prompt, prompt2, len + 1);
             OPENSSL_strlcat(prompt, object_name, len + 1);
@@ -690,10 +690,8 @@ int UI_method_set_data_duplicator(UI_METHOD *method,
 
 int UI_method_set_prompt_constructor(UI_METHOD *method,
                                      char *(*prompt_constructor) (UI *ui,
-                                                                  const char
-                                                                  *object_desc,
-                                                                  const char
-                                                                  *object_name))
+                                                                  const char *,
+                                                                  const char *))
 {
     if (method != NULL) {
         method->ui_construct_prompt = prompt_constructor;
index 0615e2766cfe5be92582a9c8a4a567e173920c44..c3852587eb08c7cf91f87450cacb59c12f22079b 100644 (file)
@@ -44,7 +44,7 @@ UI_get_method, UI_set_method, UI_OpenSSL, UI_null - user interface
  int UI_dup_error_string(UI *ui, const char *text);
 
  char *UI_construct_prompt(UI *ui_method,
-        const char *object_desc, const char *object_name);
+                           const char *phrase_desc, const char *object_name);
 
  void *UI_add_user_data(UI *ui, void *user_data);
  int UI_dup_user_data(UI *ui, void *user_data);
@@ -149,10 +149,12 @@ as their UI_add counterparts, except that they make their own copies
 of all strings.
 
 UI_construct_prompt() is a helper function that can be used to create
-a prompt from two pieces of information: an description and a name.
+a prompt from two pieces of information: a phrase description I<phrase_desc>
+and an object name I<object_name>, where the latter may be NULL.
 The default constructor (if there is none provided by the method used)
-creates a string "Enter I<description> for I<name>:".  With the
-description "pass phrase" and the filename "foo.key", that becomes
+creates a string "Enter I<phrase_desc> for I<object_name>:"
+where the " for I<object_name>" part is left out if I<object_name> is NULL.
+With the description "pass phrase" and the filename "foo.key", that becomes
 "Enter pass phrase for foo.key:".  Other methods may create whatever
 string and may include encodings that will be processed by the other
 method functions.
index fa55d92ac8419d9b50fc864ea0a9a6ea149b7e9c..f68a4e90a81a8d5c3caeb34765c363346be74b97 100644 (file)
@@ -138,25 +138,26 @@ int UI_dup_error_string(UI *ui, const char *text);
 # define UI_INPUT_FLAG_USER_BASE 16
 
 /*-
- * The following function helps construct a prompt.  object_desc is a
- * textual short description of the object, for example "pass phrase",
- * and object_name is the name of the object (might be a card name or
- * a file name.
+ * The following function helps construct a prompt.
+ * phrase_desc is a textual short description of the phrase to enter,
+ * for example "pass phrase", and
+ * object_name is the name of the object
+ * (which might be a card name or a file name) or NULL.
  * The returned string shall always be allocated on the heap with
  * OPENSSL_malloc(), and need to be free'd with OPENSSL_free().
  *
  * If the ui_method doesn't contain a pointer to a user-defined prompt
  * constructor, a default string is built, looking like this:
  *
- *       "Enter {object_desc} for {object_name}:"
+ *       "Enter {phrase_desc} for {object_name}:"
  *
- * So, if object_desc has the value "pass phrase" and object_name has
+ * So, if phrase_desc has the value "pass phrase" and object_name has
  * the value "foo.key", the resulting string is:
  *
  *       "Enter pass phrase for foo.key:"
 */
 char *UI_construct_prompt(UI *ui_method,
-                          const char *object_desc, const char *object_name);
+                          const char *phrase_desc, const char *object_name);
 
 /*
  * The following function is used to store a pointer to user-specific data.
@@ -315,7 +316,7 @@ int UI_method_set_data_duplicator(UI_METHOD *method,
 int UI_method_set_prompt_constructor(UI_METHOD *method,
                                      char *(*prompt_constructor) (UI *ui,
                                                                   const char
-                                                                  *object_desc,
+                                                                  *phrase_desc,
                                                                   const char
                                                                   *object_name));
 int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data);