Windows: UTF-8 opt-in for command-line arguments and console input.
authorAndy Polyakov <appro@openssl.org>
Thu, 25 Aug 2016 06:06:26 +0000 (08:06 +0200)
committerMatt Caswell <matt@openssl.org>
Thu, 25 Aug 2016 10:56:28 +0000 (11:56 +0100)
User can make Windows openssl.exe to treat command-line arguments
and console input as UTF-8 By setting OPENSSL_WIN32_UTF8 environment
variable (to any value). This is likely to be required for data
interchangeability with other OSes and PKCS#12 containers generated
with Windows CryptoAPI.

Reviewed-by: Richard Levitte <levitte@openssl.org>
apps/win32_init.c
crypto/ui/ui_openssl.c
test/recipes/80-test_pkcs12.t

index 259e3f35ba05b1eaf7b26fdd5749743080a66a46..ebe92bcd4055b0a9023063c68ce49694d81a4872 100644 (file)
@@ -145,6 +145,9 @@ void win32_utf8argv(int *argc, char **argv[])
     int wlen, ulen, valid = 1;
     char *arg;
 
     int wlen, ulen, valid = 1;
     char *arg;
 
+    if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) == 0)
+        return;
+
     newargc = 0;
     newargv = NULL;
     if (!validate_argv(newargc))
     newargc = 0;
     newargv = NULL;
     if (!validate_argv(newargc))
index 98fb0e192efcb7637d8f996cf93b87210fef8161..befe97386e94c7fa46f61a79203dd460fc0e31ce 100644 (file)
@@ -305,23 +305,26 @@ static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
     if (is_a_tty) {
         DWORD numread;
 #  if defined(CP_UTF8)
     if (is_a_tty) {
         DWORD numread;
 #  if defined(CP_UTF8)
-        WCHAR wresult[BUFSIZ];
+        if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
+            WCHAR wresult[BUFSIZ];
 
 
-        if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
+            if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
                          wresult, maxsize, &numread, NULL)) {
                          wresult, maxsize, &numread, NULL)) {
-            if (numread >= 2 &&
-                wresult[numread-2] == L'\r' && wresult[numread-1] == L'\n') {
-                wresult[numread-2] = L'\n';
-                numread--;
+                if (numread >= 2 &&
+                    wresult[numread-2] == L'\r' &&
+                    wresult[numread-1] == L'\n') {
+                    wresult[numread-2] = L'\n';
+                    numread--;
+                }
+                wresult[numread] = '\0';
+                if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
+                                        result, sizeof(result), NULL, 0) > 0)
+                    p = result;
+
+                OPENSSL_cleanse(wresult, sizeof(wresult));
             }
             }
-            wresult[numread] = '\0';
-            if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
-                                    result, sizeof(result), NULL, 0) > 0)
-                p = result;
-
-            OPENSSL_cleanse(wresult, sizeof(wresult));
-        }
-#  else
+        } else
+#  endif
         if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
                          result, maxsize, &numread, NULL)) {
             if (numread >= 2 &&
         if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
                          result, maxsize, &numread, NULL)) {
             if (numread >= 2 &&
@@ -332,7 +335,6 @@ static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
             result[numread] = '\0';
             p = result;
         }
             result[numread] = '\0';
             p = result;
         }
-#  endif
     } else
 # elif defined(OPENSSL_SYS_MSDOS)
     if (!echo) {
     } else
 # elif defined(OPENSSL_SYS_MSDOS)
     if (!echo) {
index 95d1e69161d02d28616b85bc1b7035ac64a1546e..be9a72c8e283cd33d1ec6b5cb1ef3dc4a989a678 100644 (file)
@@ -53,6 +53,7 @@ if (eval { require Win32::API; 1; }) {
         }
     }
 }
         }
     }
 }
+$ENV{OPENSSL_WIN32_UTF8}=1;
 
 plan tests => 1;
 
 
 plan tests => 1;