summaryrefslogtreecommitdiffstats
path: root/src/VBox/Storage/testcase/vdkeystoremgr.cpp
blob: 2f871604b875ddf6fdb9649d5afac7b8b30e218b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* $Id: vdkeystoremgr.cpp $ */
/** @file
 * Keystore utility for debugging.
 */

/*
 * Copyright (C) 2016-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <VBox/vd.h>
#include <iprt/errcore.h>
#include <VBox/version.h>
#include <iprt/initterm.h>
#include <iprt/base64.h>
#include <iprt/buildconfig.h>
#include <iprt/path.h>
#include <iprt/string.h>
#include <iprt/uuid.h>
#include <iprt/stream.h>
#include <iprt/message.h>
#include <iprt/getopt.h>
#include <iprt/assert.h>

#include "../VDKeyStore.h"

/** command handler argument */
struct HandlerArg
{
    int argc;
    char **argv;
};

static const char *g_pszProgName = "";
static void printUsage(PRTSTREAM pStrm)
{
    RTStrmPrintf(pStrm,
                 "Usage: %s\n"
                 "   create       --password <password>\n"
                 "                --cipher <cipher>\n"
                 "                --dek <dek in base64>\n"
                 "\n"
                 "   dump         --keystore <keystore data in base64>\n"
                 "                [--password <password to decrypt the DEK inside]\n",
                 g_pszProgName);
}

static void showLogo(PRTSTREAM pStrm)
{
    static bool s_fShown; /* show only once */

    if (!s_fShown)
    {
        RTStrmPrintf(pStrm, VBOX_PRODUCT " VD Keystore Mgr " VBOX_VERSION_STRING "\n"
                     "Copyright (C) 2016-" VBOX_C_YEAR " " VBOX_VENDOR "\n\n");
        s_fShown = true;
    }
}

/**
 * Print a usage synopsis and the syntax error message.
 */
static int errorSyntax(const char *pszFormat, ...)
{
    va_list args;
    showLogo(g_pStdErr); // show logo even if suppressed
    va_start(args, pszFormat);
    RTStrmPrintf(g_pStdErr, "\nSyntax error: %N\n", pszFormat, &args);
    va_end(args);
    printUsage(g_pStdErr);
    return 1;
}

static int errorRuntime(const char *pszFormat, ...)
{
    va_list args;

    va_start(args, pszFormat);
    RTMsgErrorV(pszFormat, args);
    va_end(args);
    return 1;
}

static DECLCALLBACK(int) handleCreate(HandlerArg *pArgs)
{
    const char *pszPassword = NULL;
    const char *pszCipher = NULL;
    const char *pszDek = NULL;

    /* Parse the command line. */
    static const RTGETOPTDEF s_aOptions[] =
    {
        { "--password", 'p', RTGETOPT_REQ_STRING },
        { "--cipher"  , 'c', RTGETOPT_REQ_STRING },
        { "--dek",      'd', RTGETOPT_REQ_STRING }
    };

    int ch;
    RTGETOPTUNION ValueUnion;
    RTGETOPTSTATE GetState;
    RTGetOptInit(&GetState, pArgs->argc, pArgs->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
    while ((ch = RTGetOpt(&GetState, &ValueUnion)))
    {
        switch (ch)
        {
            case 'p':   // --password
                pszPassword = ValueUnion.psz;
                break;
            case 'c':   // --cipher
                pszCipher = ValueUnion.psz;
                break;
            case 'd':   // --dek
                pszDek = ValueUnion.psz;
                break;
            default:
                ch = RTGetOptPrintError(ch, &ValueUnion);
                printUsage(g_pStdErr);
                return ch;
        }
    }

    /* Check for mandatory parameters. */
    if (!pszPassword)
        return errorSyntax("Mandatory --password option missing\n");
    if (!pszCipher)
        return errorSyntax("Mandatory --cipher option missing\n");
    if (!pszDek)
        return errorSyntax("Mandatory --dek option missing\n");

    /* Get the size of the decoded DEK. */
    ssize_t cbDekDec = RTBase64DecodedSize(pszDek, NULL);
    if (cbDekDec == -1)
        return errorRuntime("The encoding of the base64 DEK is bad\n");

    uint8_t *pbDek = (uint8_t *)RTMemAllocZ(cbDekDec);
    size_t cbDek = cbDekDec;
    if (!pbDek)
        return errorRuntime("Failed to allocate memory for the DEK\n");

    int rc = RTBase64Decode(pszDek, pbDek, cbDek, &cbDek, NULL);
    if (RT_SUCCESS(rc))
    {
        char *pszKeyStoreEnc = NULL;
        rc = vdKeyStoreCreate(pszPassword, pbDek, cbDek, pszCipher, &pszKeyStoreEnc);
        if (RT_SUCCESS(rc))
        {
            RTPrintf("Successfully created keystore\n"
                     "Keystore (base64): \n"
                     "%s\n", pszKeyStoreEnc);
            RTMemFree(pszKeyStoreEnc);
        }
        else
            errorRuntime("Failed to create keystore with %Rrc\n", rc);
    }
    else
        errorRuntime("Failed to decode the DEK with %Rrc\n", rc);

    RTMemFree(pbDek);
    return VERR_NOT_IMPLEMENTED;
}

static DECLCALLBACK(int) handleDump(HandlerArg *pArgs)
{
    return VERR_NOT_IMPLEMENTED;
}

int main(int argc, char *argv[])
{
    int exitcode = 0;

    int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_STANDALONE_APP);
    if (RT_FAILURE(rc))
        return RTMsgInitFailure(rc);

    g_pszProgName = RTPathFilename(argv[0]);

    bool fShowLogo = false;
    int  iCmd      = 1;
    int  iCmdArg;

    /* global options */
    for (int i = 1; i < argc || argc <= iCmd; i++)
    {
        if (    argc <= iCmd
            ||  !strcmp(argv[i], "help")
            ||  !strcmp(argv[i], "-?")
            ||  !strcmp(argv[i], "-h")
            ||  !strcmp(argv[i], "-help")
            ||  !strcmp(argv[i], "--help"))
        {
            showLogo(g_pStdOut);
            printUsage(g_pStdOut);
            return 0;
        }

        if (   !strcmp(argv[i], "-v")
            || !strcmp(argv[i], "-version")
            || !strcmp(argv[i], "-Version")
            || !strcmp(argv[i], "--version"))
        {
            /* Print version number, and do nothing else. */
            RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
            return 0;
        }

        if (   !strcmp(argv[i], "--nologo")
            || !strcmp(argv[i], "-nologo")
            || !strcmp(argv[i], "-q"))
        {
            /* suppress the logo */
            fShowLogo = false;
            iCmd++;
        }
        else
        {
            break;
        }
    }

    iCmdArg = iCmd + 1;

    if (fShowLogo)
        showLogo(g_pStdOut);

    /*
     * All registered command handlers
     */
    static const struct
    {
        const char *command;
        DECLR3CALLBACKMEMBER(int, handler, (HandlerArg *a));
    } s_commandHandlers[] =
    {
        { "create",       handleCreate       },
        { "dump",         handleDump         },
        { NULL,           NULL               }
    };

    HandlerArg handlerArg = { 0, NULL };
    int commandIndex;
    for (commandIndex = 0; s_commandHandlers[commandIndex].command != NULL; commandIndex++)
    {
        if (!strcmp(s_commandHandlers[commandIndex].command, argv[iCmd]))
        {
            handlerArg.argc = argc - iCmdArg;
            handlerArg.argv = &argv[iCmdArg];

            exitcode = s_commandHandlers[commandIndex].handler(&handlerArg);
            break;
        }
    }
    if (!s_commandHandlers[commandIndex].command)
    {
        errorSyntax("Invalid command '%s'", argv[iCmd]);
        return 1;
    }

    return exitcode;
}

/* dummy stub for RuntimeR3 */
#ifndef RT_OS_WINDOWS
RTDECL(bool) RTAssertShouldPanic(void)
{
    return true;
}
#endif