2 * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include "crypto/cryptlib.h"
13 #include <openssl/safestack.h>
19 # if defined(_UNICODE) || defined(__UNICODE__)
20 # define _vsntprintf _vsnwprintf
22 # define _vsntprintf _vsnprintf
26 # define alloca _alloca
29 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
30 # ifdef OPENSSL_SYS_WIN_CORE
32 int OPENSSL_isservice(void)
34 /* OneCore API cannot interact with GUI */
38 int OPENSSL_isservice(void)
46 } _OPENSSL_isservice
= {
50 if (_OPENSSL_isservice
.p
== NULL
) {
51 HANDLE mod
= GetModuleHandle(NULL
);
55 f
= GetProcAddress(mod
, "_OPENSSL_isservice");
57 _OPENSSL_isservice
.p
= (void *)-1;
59 _OPENSSL_isservice
.f
= f
;
62 if (_OPENSSL_isservice
.p
!= (void *)-1)
63 return (*_OPENSSL_isservice
.f
) ();
65 h
= GetProcessWindowStation();
69 if (GetUserObjectInformationW(h
, UOI_NAME
, NULL
, 0, &len
) ||
70 GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
74 return -1; /* paranoia */
75 len
++, len
&= ~1; /* paranoia */
76 name
= (WCHAR
*)alloca(len
+ sizeof(WCHAR
));
77 if (!GetUserObjectInformationW(h
, UOI_NAME
, name
, len
, &len
))
80 len
++, len
&= ~1; /* paranoia */
81 name
[len
/ sizeof(WCHAR
)] = L
'\0'; /* paranoia */
84 * This doesn't cover "interactive" services [working with real
85 * WinSta0's] nor programs started non-interactively by Task Scheduler
86 * [those are working with SAWinSta].
88 if (wcsstr(name
, L
"Service-0x"))
91 /* This covers all non-interactive programs such as services. */
92 if (!wcsstr(name
, L
"WinSta0"))
100 int OPENSSL_isservice(void)
106 void OPENSSL_showfatal(const char *fmta
, ...)
112 * First check if it's a console application, in which case the
113 * error message would be printed to standard error.
114 * Windows CE does not have a concept of a console application,
115 * so we need to guard the check.
117 # ifdef STD_ERROR_HANDLE
120 if ((h
= GetStdHandle(STD_ERROR_HANDLE
)) != NULL
&&
121 GetFileType(h
) != FILE_TYPE_UNKNOWN
) {
122 /* must be console application */
127 len
= _vsnprintf((char *)buf
, sizeof(buf
), fmta
, ap
);
128 WriteFile(h
, buf
, len
< 0 ? sizeof(buf
) : (DWORD
) len
, &out
, NULL
);
134 if (sizeof(TCHAR
) == sizeof(char))
135 fmt
= (const TCHAR
*)fmta
;
139 size_t len_0
= strlen(fmta
) + 1, i
;
142 fmtw
= (WCHAR
*)alloca(len_0
* sizeof(WCHAR
));
144 fmt
= (const TCHAR
*)L
"no stack?";
147 if (!MultiByteToWideChar(CP_ACP
, 0, fmta
, len_0
, fmtw
, len_0
))
148 for (i
= 0; i
< len_0
; i
++)
149 fmtw
[i
] = (WCHAR
)fmta
[i
];
150 for (i
= 0; i
< len_0
; i
++) {
154 switch (fmtw
[i
+ 1]) {
186 fmt
= (const TCHAR
*)fmtw
;
190 _vsntprintf(buf
, OSSL_NELEM(buf
) - 1, fmt
, ap
);
191 buf
[OSSL_NELEM(buf
) - 1] = _T('\0');
194 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
195 # ifdef OPENSSL_SYS_WIN_CORE
196 /* ONECORE is always NONGUI and NT >= 0x0601 */
199 * TODO: (For non GUI and no std error cases)
200 * Add event logging feature here.
203 # if !defined(NDEBUG)
205 * We are in a situation where we tried to report a critical
206 * error and this failed for some reason. As a last resort,
207 * in debug builds, send output to the debugger or any other
208 * tool like DebugView which can monitor the output.
210 OutputDebugString(buf
);
213 /* this -------------v--- guards NT-specific calls */
214 if (check_winnt() && OPENSSL_isservice() > 0) {
215 HANDLE hEventLog
= RegisterEventSource(NULL
, _T("OpenSSL"));
217 if (hEventLog
!= NULL
) {
218 const TCHAR
*pmsg
= buf
;
220 if (!ReportEvent(hEventLog
, EVENTLOG_ERROR_TYPE
, 0, 0, NULL
,
221 1, 0, &pmsg
, NULL
)) {
222 # if !defined(NDEBUG)
224 * We are in a situation where we tried to report a critical
225 * error and this failed for some reason. As a last resort,
226 * in debug builds, send output to the debugger or any other
227 * tool like DebugView which can monitor the output.
229 OutputDebugString(pmsg
);
233 (void)DeregisterEventSource(hEventLog
);
236 MessageBox(NULL
, buf
, _T("OpenSSL: FATAL"), MB_OK
| MB_ICONERROR
);
240 MessageBox(NULL
, buf
, _T("OpenSSL: FATAL"), MB_OK
| MB_ICONERROR
);
244 void OPENSSL_showfatal(const char *fmta
, ...)
246 #ifndef OPENSSL_NO_STDIO
250 vfprintf(stderr
, fmta
, ap
);
255 int OPENSSL_isservice(void)
261 void OPENSSL_die(const char *message
, const char *file
, int line
)
263 OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
264 file
, line
, message
);
269 * Win32 abort() customarily shows a dialog, but we just did that...
271 # if !defined(_WIN32_WCE)
278 #if defined(__TANDEM) && defined(OPENSSL_VPROC)
280 * Define a VPROC function for HP NonStop build crypto library.
281 * This is used by platform version identification tools.
282 * Do not inline this procedure or make it static.
284 # define OPENSSL_VPROC_STRING_(x) x##_CRYPTO
285 # define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x)
286 # define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC)
287 void OPENSSL_VPROC_FUNC(void) {}
288 #endif /* __TANDEM */