File: run-createkey.cpp

package info (click to toggle)
gpgmepp 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,172 kB
  • sloc: cpp: 12,050; sh: 148; makefile: 5
file content (178 lines) | stat: -rw-r--r-- 5,942 bytes parent folder | download
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
/*
    run-createkey.cpp
    Copyright (c) 2025 g10 Code GmbH
    Software engineering by Ingo Klöcker <dev@ingo-kloecker.de>

    This file is part of GPGME++'s test suite.

    GPGME++ is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    GPGME++ 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 Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this program; if not, see <https://gnu.org/licenses/>.
    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include <context.h>
#include <keygenerationresult.h>

#include <iostream>

using namespace GpgME;

static int show_usage(int ex)
{
    auto stream = (ex == 0) ? &std::cout : &std::cerr;
    if (ex == 0) {
        *stream << "Create an OpenPGP key or add a subkey to an existing key\n\n";
    }
    *stream << "usage: run-createkey [options] USERID\n"
               "   or: run-createkey --addkey [options] FPR\n"
               "Options:\n"
               "  --cert[ify]       create a certification key\n"
               "  --sign            create a signing key\n"
               "  --encr[ypt]       create an encryption key\n"
               "  --auth[enticate]  create an authentication key\n"
               "  --group           flag created key as group key\n"
               "  --unprotected     do not use a passphrase\n"
               "  --force           do not check for a duplicated user id\n";
    exit(ex);
}

enum Command
{
    CreateKey,
    CreateSubkey,
};

struct Options
{
    Command command = Command::CreateKey;
    std::string userid;
    std::string fingerprint;
    Context::CreationFlags flags = Context::CreateUseDefaults;
};

static Options parseCommandLine(int argc, char **argv)
{
    Options options;

    if (argc) {
        argc--; argv++;
    }

    int last_argc = -1;
    while (argc && last_argc != argc ) {
        last_argc = argc;
        if (!strcmp(*argv, "--")) {
            argc--; argv++;
            break;
        } else if (!strcmp(*argv, "--help")) {
            show_usage(0);
        } else if (!strcmp(*argv, "--addkey")) {
            options.command = CreateSubkey;
            argc--; argv++;
        } else if (!strcmp(*argv, "--certify")) {
            options.flags |= Context::CreateCertify;
            argc--; argv++;
        } else if (!strcmp(*argv, "--sign")) {
            options.flags |= Context::CreateSign;
            argc--; argv++;
        } else if (!strcmp(*argv, "--encrypt")) {
            options.flags |= Context::CreateEncrypt;
            argc--; argv++;
        } else if (!strcmp(*argv, "--authenticate")) {
            options.flags |= Context::CreateAuthenticate;
            argc--; argv++;
        } else if (!strcmp(*argv, "--group")) {
            options.flags |= Context::CreateGroupKey;
            argc--; argv++;
        } else if (!strcmp(*argv, "--unprotected")) {
            options.flags |= Context::CreateNoPassword;
            argc--; argv++;
        } else if (!strcmp(*argv, "--force")) {
            options.flags |= Context::CreateForce;
            argc--; argv++;
        } else if (!strncmp(*argv, "--", 2)) {
            show_usage(1);
        }
    }
    if (argc != 1) {
        show_usage(1);
    }

    switch (options.command) {
    case CreateKey:
        options.userid = *argv;
        break;
    case CreateSubkey:
        options.fingerprint = *argv;
        break;
    }

    return options;
}

int main(int argc, char **argv)
{
    Options options = parseCommandLine(argc, argv);

    GpgME::initializeLibrary();
    auto ctx = Context::create(OpenPGP);
    if (!ctx) {
        std::cerr << "Failed to create Context";
        return -1;
    }

    switch (options.command) {
        case CreateKey: {
            const auto result = ctx->createKey(options.userid.c_str(), std::string{}, 0, options.flags);
            if (result.error().isError()) {
                std::cerr << "Error: Failed to create key: " << result.error().asStdString() << std::endl;
            } else if (!result.error().isCanceled()) {
                std::cout << "Created key with fingerprint " << result.fingerprint() << std::endl;
                Error err;
                const Key key = ctx->key(result.fingerprint(), err, true);
                if (err.isError()) {
                    std::cerr << "Error: Failed to read key: " << err.asStdString() << std::endl;
                }
                if (!key.isNull()) {
                    std::cout << key << std::endl;
                }
            }
            break;
        }
        case CreateSubkey: {
            Error err;
            Key key = ctx->key(options.fingerprint.c_str(), err);
            if (err.isError()) {
                std::cerr << "Error: Failed to read key: " << err.asStdString() << std::endl;
                return 1;
            }
            const auto result = ctx->createSubkey(key, std::string{}, 0, options.flags);
            if (result.error()) {
                std::cerr << "Error: Failed to create subkey: " << result.error().asStdString() << std::endl;
            } else {
                std::cout << "Created subkey with fingerprint " << result.fingerprint() << std::endl;
                key.update();
                for (const auto &subkey : key.subkeys()) {
                    if (!strcmp(subkey.fingerprint(), result.fingerprint())) {
                        std::cout << subkey;
                        break;
                    }
                }
            }
            break;
        }
        default: ;
    }

    return 0;
}