[24311] | 1 | /* $Id: VBoxGuestR3LibCredentials.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
[24259] | 2 | /** @file
|
---|
| 3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, user credentials.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
[24259] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[24259] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[26425] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[26425] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[24259] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[26243] | 41 | #include <iprt/asm.h>
|
---|
[30829] | 42 | #include <iprt/mem.h>
|
---|
| 43 | #include <iprt/rand.h>
|
---|
[24259] | 44 | #include <iprt/string.h>
|
---|
[76408] | 45 | #include <iprt/utf16.h>
|
---|
[24259] | 46 | #include <VBox/log.h>
|
---|
| 47 |
|
---|
[68650] | 48 | #include "VBoxGuestR3LibInternal.h"
|
---|
[24259] | 49 |
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Checks whether user credentials are available to the guest or not.
|
---|
| 53 | *
|
---|
[26374] | 54 | * @returns IPRT status value; VINF_SUCCESS if credentials are available,
|
---|
[33540] | 55 | * VERR_NOT_FOUND if not. Otherwise an error is occurred.
|
---|
[24259] | 56 | */
|
---|
[26380] | 57 | VBGLR3DECL(int) VbglR3CredentialsQueryAvailability(void)
|
---|
[24259] | 58 | {
|
---|
[24311] | 59 | VMMDevCredentials Req;
|
---|
| 60 | RT_ZERO(Req);
|
---|
| 61 | vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
|
---|
| 62 | Req.u32Flags |= VMMDEV_CREDENTIALS_QUERYPRESENCE;
|
---|
[24259] | 63 |
|
---|
[24311] | 64 | int rc = vbglR3GRPerform(&Req.header);
|
---|
[26374] | 65 | if (RT_SUCCESS(rc))
|
---|
| 66 | {
|
---|
| 67 | if ((Req.u32Flags & VMMDEV_CREDENTIALS_PRESENT) == 0)
|
---|
| 68 | rc = VERR_NOT_FOUND;
|
---|
| 69 | }
|
---|
| 70 | return rc;
|
---|
[24259] | 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
[24311] | 75 | * Retrieves and clears the user credentials for logging into the guest OS.
|
---|
[24259] | 76 | *
|
---|
| 77 | * @returns IPRT status value
|
---|
[24311] | 78 | * @param ppszUser Receives pointer of allocated user name string.
|
---|
[26148] | 79 | * The returned pointer must be freed using VbglR3CredentialsDestroy().
|
---|
[24311] | 80 | * @param ppszPassword Receives pointer of allocated user password string.
|
---|
[26148] | 81 | * The returned pointer must be freed using VbglR3CredentialsDestroy().
|
---|
[24311] | 82 | * @param ppszDomain Receives pointer of allocated domain name string.
|
---|
[26148] | 83 | * The returned pointer must be freed using VbglR3CredentialsDestroy().
|
---|
[24259] | 84 | */
|
---|
| 85 | VBGLR3DECL(int) VbglR3CredentialsRetrieve(char **ppszUser, char **ppszPassword, char **ppszDomain)
|
---|
| 86 | {
|
---|
[40211] | 87 | AssertPtrReturn(ppszUser, VERR_INVALID_POINTER);
|
---|
| 88 | AssertPtrReturn(ppszPassword, VERR_INVALID_POINTER);
|
---|
| 89 | AssertPtrReturn(ppszDomain, VERR_INVALID_POINTER);
|
---|
| 90 |
|
---|
[24311] | 91 | VMMDevCredentials Req;
|
---|
| 92 | RT_ZERO(Req);
|
---|
| 93 | vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
|
---|
| 94 | Req.u32Flags |= VMMDEV_CREDENTIALS_READ | VMMDEV_CREDENTIALS_CLEAR;
|
---|
[24259] | 95 |
|
---|
[24311] | 96 | int rc = vbglR3GRPerform(&Req.header);
|
---|
[24259] | 97 | if (RT_SUCCESS(rc))
|
---|
| 98 | {
|
---|
[24311] | 99 | rc = RTStrDupEx(ppszUser, Req.szUserName);
|
---|
| 100 | if (RT_SUCCESS(rc))
|
---|
| 101 | {
|
---|
| 102 | rc = RTStrDupEx(ppszPassword, Req.szPassword);
|
---|
| 103 | if (RT_SUCCESS(rc))
|
---|
| 104 | {
|
---|
| 105 | rc = RTStrDupEx(ppszDomain, Req.szDomain);
|
---|
| 106 | if (RT_SUCCESS(rc))
|
---|
| 107 | return VINF_SUCCESS;
|
---|
| 108 |
|
---|
| 109 | RTStrFree(*ppszPassword);
|
---|
| 110 | }
|
---|
| 111 | RTStrFree(*ppszUser);
|
---|
| 112 | }
|
---|
[24259] | 113 | }
|
---|
| 114 | return rc;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[26148] | 117 |
|
---|
| 118 | /**
|
---|
[40211] | 119 | * Retrieves and clears the user credentials for logging into the guest OS.
|
---|
| 120 | * UTF-16 version.
|
---|
| 121 | *
|
---|
| 122 | * @returns IPRT status value
|
---|
| 123 | * @param ppwszUser Receives pointer of allocated user name string.
|
---|
| 124 | * The returned pointer must be freed using VbglR3CredentialsDestroyUtf16().
|
---|
[58204] | 125 | * @param ppwszPassword Receives pointer of allocated user password string.
|
---|
[40211] | 126 | * The returned pointer must be freed using VbglR3CredentialsDestroyUtf16().
|
---|
| 127 | * @param ppwszDomain Receives pointer of allocated domain name string.
|
---|
| 128 | * The returned pointer must be freed using VbglR3CredentialsDestroyUtf16().
|
---|
| 129 | */
|
---|
| 130 | VBGLR3DECL(int) VbglR3CredentialsRetrieveUtf16(PRTUTF16 *ppwszUser, PRTUTF16 *ppwszPassword, PRTUTF16 *ppwszDomain)
|
---|
| 131 | {
|
---|
| 132 | AssertPtrReturn(ppwszUser, VERR_INVALID_POINTER);
|
---|
| 133 | AssertPtrReturn(ppwszPassword, VERR_INVALID_POINTER);
|
---|
| 134 | AssertPtrReturn(ppwszDomain, VERR_INVALID_POINTER);
|
---|
| 135 |
|
---|
| 136 | char *pszUser, *pszPassword, *pszDomain;
|
---|
| 137 | int rc = VbglR3CredentialsRetrieve(&pszUser, &pszPassword, &pszDomain);
|
---|
| 138 | if (RT_SUCCESS(rc))
|
---|
| 139 | {
|
---|
| 140 | PRTUTF16 pwszUser = NULL;
|
---|
| 141 | PRTUTF16 pwszPassword = NULL;
|
---|
| 142 | PRTUTF16 pwszDomain = NULL;
|
---|
| 143 |
|
---|
| 144 | rc = RTStrToUtf16(pszUser, &pwszUser);
|
---|
| 145 | if (RT_SUCCESS(rc))
|
---|
| 146 | {
|
---|
| 147 | rc = RTStrToUtf16(pszPassword, &pwszPassword);
|
---|
| 148 | if (RT_SUCCESS(rc))
|
---|
| 149 | rc = RTStrToUtf16(pszDomain, &pwszDomain);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | if (RT_SUCCESS(rc))
|
---|
| 153 | {
|
---|
| 154 | *ppwszUser = pwszUser;
|
---|
| 155 | *ppwszPassword = pwszPassword;
|
---|
| 156 | *ppwszDomain = pwszDomain;
|
---|
| 157 | }
|
---|
| 158 | else
|
---|
[40436] | 159 | VbglR3CredentialsDestroyUtf16(pwszUser, pwszPassword, pwszDomain, 3 /* Passes */);
|
---|
| 160 | VbglR3CredentialsDestroy(pszUser, pszPassword, pszDomain, 3 /* Passes */);
|
---|
[40211] | 161 | }
|
---|
| 162 |
|
---|
| 163 | return rc;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[40436] | 166 |
|
---|
[40211] | 167 | /**
|
---|
[26243] | 168 | * Clears and frees the three strings.
|
---|
[26148] | 169 | *
|
---|
[26149] | 170 | * @param pszUser Receives pointer of the user name string to destroy.
|
---|
[26148] | 171 | * Optional.
|
---|
[26149] | 172 | * @param pszPassword Receives pointer of the password string to destroy.
|
---|
[26148] | 173 | * Optional.
|
---|
| 174 | * @param pszDomain Receives pointer of allocated domain name string.
|
---|
| 175 | * Optional.
|
---|
[26243] | 176 | * @param cPasses Number of wipe passes. The more the better + slower.
|
---|
[26148] | 177 | */
|
---|
[26243] | 178 | VBGLR3DECL(void) VbglR3CredentialsDestroy(char *pszUser, char *pszPassword, char *pszDomain, uint32_t cPasses)
|
---|
[26148] | 179 | {
|
---|
[30829] | 180 | /* wipe first */
|
---|
| 181 | if (pszUser)
|
---|
| 182 | RTMemWipeThoroughly(pszUser, strlen(pszUser) + 1, cPasses);
|
---|
| 183 | if (pszPassword)
|
---|
| 184 | RTMemWipeThoroughly(pszPassword, strlen(pszPassword) + 1, cPasses);
|
---|
| 185 | if (pszDomain)
|
---|
| 186 | RTMemWipeThoroughly(pszDomain, strlen(pszDomain) + 1, cPasses);
|
---|
[26149] | 187 |
|
---|
[30829] | 188 | /* then free. */
|
---|
[26243] | 189 | RTStrFree(pszUser);
|
---|
| 190 | RTStrFree(pszPassword);
|
---|
| 191 | RTStrFree(pszDomain);
|
---|
[26148] | 192 | }
|
---|
[26243] | 193 |
|
---|
[40436] | 194 |
|
---|
[40211] | 195 | /**
|
---|
| 196 | * Clears and frees the three strings. UTF-16 version.
|
---|
| 197 | *
|
---|
| 198 | * @param pwszUser Receives pointer of the user name string to destroy.
|
---|
| 199 | * Optional.
|
---|
| 200 | * @param pwszPassword Receives pointer of the password string to destroy.
|
---|
| 201 | * Optional.
|
---|
| 202 | * @param pwszDomain Receives pointer of allocated domain name string.
|
---|
| 203 | * Optional.
|
---|
| 204 | * @param cPasses Number of wipe passes. The more the better + slower.
|
---|
| 205 | */
|
---|
| 206 | VBGLR3DECL(void) VbglR3CredentialsDestroyUtf16(PRTUTF16 pwszUser, PRTUTF16 pwszPassword, PRTUTF16 pwszDomain,
|
---|
| 207 | uint32_t cPasses)
|
---|
| 208 | {
|
---|
| 209 | /* wipe first */
|
---|
| 210 | if (pwszUser)
|
---|
[40436] | 211 | RTMemWipeThoroughly(pwszUser, (RTUtf16Len(pwszUser) + 1) * sizeof(RTUTF16), cPasses);
|
---|
[40211] | 212 | if (pwszPassword)
|
---|
[40436] | 213 | RTMemWipeThoroughly(pwszPassword, (RTUtf16Len(pwszPassword) + 1) * sizeof(RTUTF16), cPasses);
|
---|
[40211] | 214 | if (pwszDomain)
|
---|
[40436] | 215 | RTMemWipeThoroughly(pwszDomain, (RTUtf16Len(pwszDomain) + 1) * sizeof(RTUTF16), cPasses);
|
---|
[40211] | 216 |
|
---|
| 217 | /* then free. */
|
---|
| 218 | RTUtf16Free(pwszUser);
|
---|
| 219 | RTUtf16Free(pwszPassword);
|
---|
| 220 | RTUtf16Free(pwszDomain);
|
---|
| 221 | }
|
---|
| 222 |
|
---|