VirtualBox

source: vbox/trunk/src/VBox/Storage/testcase/vdkeystoremgr.cpp@ 96399

Last change on this file since 96399 was 96399, checked in by vboxsync, 2 years ago

/Config.kmk and many other places: Change VBOX_VENDOR to the official copyright holder text, needs follow-up changes and equivalent adjustments elsewhere.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: vdkeystoremgr.cpp 96399 2022-08-22 14:47:39Z vboxsync $ */
2/** @file
3 * Keystore utility for debugging.
4 */
5
6/*
7 * Copyright (C) 2016-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <VBox/vd.h>
23#include <iprt/errcore.h>
24#include <VBox/version.h>
25#include <iprt/initterm.h>
26#include <iprt/base64.h>
27#include <iprt/buildconfig.h>
28#include <iprt/path.h>
29#include <iprt/string.h>
30#include <iprt/uuid.h>
31#include <iprt/stream.h>
32#include <iprt/message.h>
33#include <iprt/getopt.h>
34#include <iprt/assert.h>
35
36#include "../VDKeyStore.h"
37
38/** command handler argument */
39struct HandlerArg
40{
41 int argc;
42 char **argv;
43};
44
45static const char *g_pszProgName = "";
46static void printUsage(PRTSTREAM pStrm)
47{
48 RTStrmPrintf(pStrm,
49 "Usage: %s\n"
50 " create --password <password>\n"
51 " --cipher <cipher>\n"
52 " --dek <dek in base64>\n"
53 "\n"
54 " dump --keystore <keystore data in base64>\n"
55 " [--password <password to decrypt the DEK inside]\n",
56 g_pszProgName);
57}
58
59static void showLogo(PRTSTREAM pStrm)
60{
61 static bool s_fShown; /* show only once */
62
63 if (!s_fShown)
64 {
65 RTStrmPrintf(pStrm, VBOX_PRODUCT " VD Keystore Mgr " VBOX_VERSION_STRING "\n"
66 "Copyright (C) 2016-" VBOX_C_YEAR " " VBOX_VENDOR "\n\n");
67 s_fShown = true;
68 }
69}
70
71/**
72 * Print a usage synopsis and the syntax error message.
73 */
74static int errorSyntax(const char *pszFormat, ...)
75{
76 va_list args;
77 showLogo(g_pStdErr); // show logo even if suppressed
78 va_start(args, pszFormat);
79 RTStrmPrintf(g_pStdErr, "\nSyntax error: %N\n", pszFormat, &args);
80 va_end(args);
81 printUsage(g_pStdErr);
82 return 1;
83}
84
85static int errorRuntime(const char *pszFormat, ...)
86{
87 va_list args;
88
89 va_start(args, pszFormat);
90 RTMsgErrorV(pszFormat, args);
91 va_end(args);
92 return 1;
93}
94
95static DECLCALLBACK(int) handleCreate(HandlerArg *pArgs)
96{
97 const char *pszPassword = NULL;
98 const char *pszCipher = NULL;
99 const char *pszDek = NULL;
100
101 /* Parse the command line. */
102 static const RTGETOPTDEF s_aOptions[] =
103 {
104 { "--password", 'p', RTGETOPT_REQ_STRING },
105 { "--cipher" , 'c', RTGETOPT_REQ_STRING },
106 { "--dek", 'd', RTGETOPT_REQ_STRING }
107 };
108
109 int ch;
110 RTGETOPTUNION ValueUnion;
111 RTGETOPTSTATE GetState;
112 RTGetOptInit(&GetState, pArgs->argc, pArgs->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
113 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
114 {
115 switch (ch)
116 {
117 case 'p': // --password
118 pszPassword = ValueUnion.psz;
119 break;
120 case 'c': // --cipher
121 pszCipher = ValueUnion.psz;
122 break;
123 case 'd': // --dek
124 pszDek = ValueUnion.psz;
125 break;
126 default:
127 ch = RTGetOptPrintError(ch, &ValueUnion);
128 printUsage(g_pStdErr);
129 return ch;
130 }
131 }
132
133 /* Check for mandatory parameters. */
134 if (!pszPassword)
135 return errorSyntax("Mandatory --password option missing\n");
136 if (!pszCipher)
137 return errorSyntax("Mandatory --cipher option missing\n");
138 if (!pszDek)
139 return errorSyntax("Mandatory --dek option missing\n");
140
141 /* Get the size of the decoded DEK. */
142 ssize_t cbDekDec = RTBase64DecodedSize(pszDek, NULL);
143 if (cbDekDec == -1)
144 return errorRuntime("The encoding of the base64 DEK is bad\n");
145
146 uint8_t *pbDek = (uint8_t *)RTMemAllocZ(cbDekDec);
147 size_t cbDek = cbDekDec;
148 if (!pbDek)
149 return errorRuntime("Failed to allocate memory for the DEK\n");
150
151 int rc = RTBase64Decode(pszDek, pbDek, cbDek, &cbDek, NULL);
152 if (RT_SUCCESS(rc))
153 {
154 char *pszKeyStoreEnc = NULL;
155 rc = vdKeyStoreCreate(pszPassword, pbDek, cbDek, pszCipher, &pszKeyStoreEnc);
156 if (RT_SUCCESS(rc))
157 {
158 RTPrintf("Successfully created keystore\n"
159 "Keystore (base64): \n"
160 "%s\n", pszKeyStoreEnc);
161 RTMemFree(pszKeyStoreEnc);
162 }
163 else
164 errorRuntime("Failed to create keystore with %Rrc\n", rc);
165 }
166 else
167 errorRuntime("Failed to decode the DEK with %Rrc\n", rc);
168
169 RTMemFree(pbDek);
170 return VERR_NOT_IMPLEMENTED;
171}
172
173static DECLCALLBACK(int) handleDump(HandlerArg *pArgs)
174{
175 return VERR_NOT_IMPLEMENTED;
176}
177
178int main(int argc, char *argv[])
179{
180 int exitcode = 0;
181
182 int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_STANDALONE_APP);
183 if (RT_FAILURE(rc))
184 return RTMsgInitFailure(rc);
185
186 g_pszProgName = RTPathFilename(argv[0]);
187
188 bool fShowLogo = false;
189 int iCmd = 1;
190 int iCmdArg;
191
192 /* global options */
193 for (int i = 1; i < argc || argc <= iCmd; i++)
194 {
195 if ( argc <= iCmd
196 || !strcmp(argv[i], "help")
197 || !strcmp(argv[i], "-?")
198 || !strcmp(argv[i], "-h")
199 || !strcmp(argv[i], "-help")
200 || !strcmp(argv[i], "--help"))
201 {
202 showLogo(g_pStdOut);
203 printUsage(g_pStdOut);
204 return 0;
205 }
206
207 if ( !strcmp(argv[i], "-v")
208 || !strcmp(argv[i], "-version")
209 || !strcmp(argv[i], "-Version")
210 || !strcmp(argv[i], "--version"))
211 {
212 /* Print version number, and do nothing else. */
213 RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
214 return 0;
215 }
216
217 if ( !strcmp(argv[i], "--nologo")
218 || !strcmp(argv[i], "-nologo")
219 || !strcmp(argv[i], "-q"))
220 {
221 /* suppress the logo */
222 fShowLogo = false;
223 iCmd++;
224 }
225 else
226 {
227 break;
228 }
229 }
230
231 iCmdArg = iCmd + 1;
232
233 if (fShowLogo)
234 showLogo(g_pStdOut);
235
236 /*
237 * All registered command handlers
238 */
239 static const struct
240 {
241 const char *command;
242 DECLR3CALLBACKMEMBER(int, handler, (HandlerArg *a));
243 } s_commandHandlers[] =
244 {
245 { "create", handleCreate },
246 { "dump", handleDump },
247 { NULL, NULL }
248 };
249
250 HandlerArg handlerArg = { 0, NULL };
251 int commandIndex;
252 for (commandIndex = 0; s_commandHandlers[commandIndex].command != NULL; commandIndex++)
253 {
254 if (!strcmp(s_commandHandlers[commandIndex].command, argv[iCmd]))
255 {
256 handlerArg.argc = argc - iCmdArg;
257 handlerArg.argv = &argv[iCmdArg];
258
259 exitcode = s_commandHandlers[commandIndex].handler(&handlerArg);
260 break;
261 }
262 }
263 if (!s_commandHandlers[commandIndex].command)
264 {
265 errorSyntax("Invalid command '%s'", argv[iCmd]);
266 return 1;
267 }
268
269 return exitcode;
270}
271
272/* dummy stub for RuntimeR3 */
273#ifndef RT_OS_WINDOWS
274RTDECL(bool) RTAssertShouldPanic(void)
275{
276 return true;
277}
278#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette