VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR3LibCredentials.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/* $Id: VBoxGuestR3LibCredentials.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, user credentials.
4 */
5
6/*
7 * Copyright (C) 2009-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
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 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
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
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.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/asm.h>
42#include <iprt/mem.h>
43#include <iprt/rand.h>
44#include <iprt/string.h>
45#include <iprt/utf16.h>
46#include <VBox/log.h>
47
48#include "VBoxGuestR3LibInternal.h"
49
50
51/**
52 * Checks whether user credentials are available to the guest or not.
53 *
54 * @returns IPRT status value; VINF_SUCCESS if credentials are available,
55 * VERR_NOT_FOUND if not. Otherwise an error is occurred.
56 */
57VBGLR3DECL(int) VbglR3CredentialsQueryAvailability(void)
58{
59 VMMDevCredentials Req;
60 RT_ZERO(Req);
61 vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
62 Req.u32Flags |= VMMDEV_CREDENTIALS_QUERYPRESENCE;
63
64 int rc = vbglR3GRPerform(&Req.header);
65 if (RT_SUCCESS(rc))
66 {
67 if ((Req.u32Flags & VMMDEV_CREDENTIALS_PRESENT) == 0)
68 rc = VERR_NOT_FOUND;
69 }
70 return rc;
71}
72
73
74/**
75 * Retrieves and clears the user credentials for logging into the guest OS.
76 *
77 * @returns IPRT status value
78 * @param ppszUser Receives pointer of allocated user name string.
79 * The returned pointer must be freed using VbglR3CredentialsDestroy().
80 * @param ppszPassword Receives pointer of allocated user password string.
81 * The returned pointer must be freed using VbglR3CredentialsDestroy().
82 * @param ppszDomain Receives pointer of allocated domain name string.
83 * The returned pointer must be freed using VbglR3CredentialsDestroy().
84 */
85VBGLR3DECL(int) VbglR3CredentialsRetrieve(char **ppszUser, char **ppszPassword, char **ppszDomain)
86{
87 AssertPtrReturn(ppszUser, VERR_INVALID_POINTER);
88 AssertPtrReturn(ppszPassword, VERR_INVALID_POINTER);
89 AssertPtrReturn(ppszDomain, VERR_INVALID_POINTER);
90
91 VMMDevCredentials Req;
92 RT_ZERO(Req);
93 vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
94 Req.u32Flags |= VMMDEV_CREDENTIALS_READ | VMMDEV_CREDENTIALS_CLEAR;
95
96 int rc = vbglR3GRPerform(&Req.header);
97 if (RT_SUCCESS(rc))
98 {
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 }
113 }
114 return rc;
115}
116
117
118/**
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().
125 * @param ppwszPassword Receives pointer of allocated user password string.
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 */
130VBGLR3DECL(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
159 VbglR3CredentialsDestroyUtf16(pwszUser, pwszPassword, pwszDomain, 3 /* Passes */);
160 VbglR3CredentialsDestroy(pszUser, pszPassword, pszDomain, 3 /* Passes */);
161 }
162
163 return rc;
164}
165
166
167/**
168 * Clears and frees the three strings.
169 *
170 * @param pszUser Receives pointer of the user name string to destroy.
171 * Optional.
172 * @param pszPassword Receives pointer of the password string to destroy.
173 * Optional.
174 * @param pszDomain Receives pointer of allocated domain name string.
175 * Optional.
176 * @param cPasses Number of wipe passes. The more the better + slower.
177 */
178VBGLR3DECL(void) VbglR3CredentialsDestroy(char *pszUser, char *pszPassword, char *pszDomain, uint32_t cPasses)
179{
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);
187
188 /* then free. */
189 RTStrFree(pszUser);
190 RTStrFree(pszPassword);
191 RTStrFree(pszDomain);
192}
193
194
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 */
206VBGLR3DECL(void) VbglR3CredentialsDestroyUtf16(PRTUTF16 pwszUser, PRTUTF16 pwszPassword, PRTUTF16 pwszDomain,
207 uint32_t cPasses)
208{
209 /* wipe first */
210 if (pwszUser)
211 RTMemWipeThoroughly(pwszUser, (RTUtf16Len(pwszUser) + 1) * sizeof(RTUTF16), cPasses);
212 if (pwszPassword)
213 RTMemWipeThoroughly(pwszPassword, (RTUtf16Len(pwszPassword) + 1) * sizeof(RTUTF16), cPasses);
214 if (pwszDomain)
215 RTMemWipeThoroughly(pwszDomain, (RTUtf16Len(pwszDomain) + 1) * sizeof(RTUTF16), cPasses);
216
217 /* then free. */
218 RTUtf16Free(pwszUser);
219 RTUtf16Free(pwszPassword);
220 RTUtf16Free(pwszDomain);
221}
222
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use