2 * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
15 #include "internal/cryptlib.h"
16 #include "internal/e_os.h"
18 char *ossl_safe_getenv(const char *name)
20 #if defined(_WIN32) && defined(CP_UTF8) && !defined(_WIN32_WCE)
21 if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
27 DWORD dwFlags = MB_ERR_INVALID_CHARS;
34 * For the code pages listed below, dwFlags must be set to 0.
35 * Otherwise, the function fails with ERROR_INVALID_FLAGS.
37 if (curacp == 50220 || curacp == 50221 || curacp == 50222 ||
38 curacp == 50225 || curacp == 50227 || curacp == 50229 ||
39 (57002 <= curacp && curacp <=57011) || curacp == 65000 ||
43 /* query for buffer len */
44 rsize = MultiByteToWideChar(curacp, dwFlags, name, -1, NULL, 0);
45 /* if name is valid string and can be converted to wide string */
47 namew = _malloca(rsize * sizeof(WCHAR));
50 /* convert name to wide string */
51 fsize = MultiByteToWideChar(curacp, dwFlags, name, -1, namew, rsize);
52 /* if conversion is ok, then determine value string size in wchars */
54 envlen = GetEnvironmentVariableW(namew, NULL, 0);
58 valw = _malloca(envlen * sizeof(WCHAR));
61 /* if can get env value as wide string */
62 if (GetEnvironmentVariableW(namew, valw, envlen) < envlen) {
63 /* determine value string size in utf-8 */
64 vallen = WideCharToMultiByte(CP_UTF8, 0, valw, -1, NULL, 0,
70 val = OPENSSL_malloc(vallen);
73 /* convert value string from wide to utf-8 */
74 if (WideCharToMultiByte(CP_UTF8, 0, valw, -1, val, vallen,
91 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
92 # if __GLIBC_PREREQ(2, 17)
93 # define SECURE_GETENV
94 return secure_getenv(name);
99 if (OPENSSL_issetugid())