Path: blob/main/contrib/libfido2/openbsd-compat/readpassphrase_win32.c
39536 views
/*1* Author: Manoj Ampalam <[email protected]>2*3* Author: Bryan Berns <[email protected]>4* Modified group detection use s4u token information5*6* Copyright(c) 2016 Microsoft Corp.7* All rights reserved8*9* Misc Unix POSIX routine implementations for Windows10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met :14*15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and / or other materials provided with the distribution.20*21* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR22* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES23* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.24* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,25* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT26* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF30* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233#define UMDF_USING_NTSTATUS34#define SECURITY_WIN3235#include <windows.h>36#include <stdio.h>37#include <time.h>38#include <shlwapi.h>39#include <conio.h>40#include <lm.h>41#include <sddl.h>42#include <aclapi.h>43#include <ntsecapi.h>44#include <security.h>45#include <ntstatus.h>46#include <wchar.h>4748#include "openbsd-compat.h"4950#ifndef HAVE_READPASSPHRASE5152/*on error returns NULL and sets errno*/53static wchar_t *54utf8_to_utf16(const char *utf8)55{56int needed = 0;57wchar_t* utf16 = NULL;58if ((needed = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0)) == 0 ||59(utf16 = malloc(needed * sizeof(wchar_t))) == NULL ||60MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, needed) == 0) {61/* debug3("failed to convert utf8 payload:%s error:%d", utf8, GetLastError()); */62errno = ENOMEM;63return NULL;64}6566return utf16;67}6869char *70readpassphrase(const char *prompt, char *outBuf, size_t outBufLen, int flags)71{72size_t current_index = 0;73char ch;74wchar_t* wtmp = NULL;7576if (outBufLen == 0) {77errno = EINVAL;78return NULL;79}8081while (_kbhit()) (void)_getch();8283wtmp = utf8_to_utf16(prompt);84if (wtmp == NULL)85errx(1, "unable to alloc memory");8687_cputws(wtmp);88free(wtmp);8990while (current_index < outBufLen - 1) {91ch = (char)_getch();9293if (ch == '\r') {94if (_kbhit()) (void)_getch(); /* read linefeed if its there */95break;96} else if (ch == '\n') {97break;98} else if (ch == '\b') { /* backspace */99if (current_index > 0) {100if (flags & RPP_ECHO_ON)101printf_s("%c \b", ch);102103current_index--; /* overwrite last character */104}105} else if (ch == '\003') { /* exit on Ctrl+C */106errx(1, "");107} else {108if (flags & RPP_SEVENBIT)109ch &= 0x7f;110111if (isalpha((unsigned char)ch)) {112if(flags & RPP_FORCELOWER)113ch = (char)tolower((unsigned char)ch);114if(flags & RPP_FORCEUPPER)115ch = (char)toupper((unsigned char)ch);116}117118outBuf[current_index++] = ch;119if(flags & RPP_ECHO_ON)120printf_s("%c", ch);121}122}123124outBuf[current_index] = '\0';125_cputs("\n");126127return outBuf;128}129130#endif /* HAVE_READPASSPHRASE */131132133