aboutsummaryrefslogtreecommitdiffstats
path: root/lang/cpp/src/gpgaddexistingsubkeyeditinteractor.cpp
blob: 547e613d3f0a92058c8071d790733af8942a3709 (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
/*
  gpgaddexistingsubkeyeditinteractor.cpp - Edit Interactor to add an existing subkey to an OpenPGP key
  Copyright (c) 2022 g10 Code GmbH
  Software engineering by Ingo Klöcker <[email protected]>

  This file is part of GPGME++.

  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 GPGME++; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#ifdef HAVE_CONFIG_H
 #include "config.h"
#endif

#include "gpgaddexistingsubkeyeditinteractor.h"

#include "error.h"

#include <gpgme.h>

// avoid conflict (msvc)
#ifdef ERROR
# undef ERROR
#endif

using namespace GpgME;

class GpgAddExistingSubkeyEditInteractor::Private
{
    enum {
        START = EditInteractor::StartState,
        COMMAND,
        ADD_EXISTING_KEY,
        KEYGRIP,
        FLAGS,
        VALID,
        KEY_CREATED,
        QUIT,
        SAVE,

        ERROR = EditInteractor::ErrorState
    };

    GpgAddExistingSubkeyEditInteractor *const q = nullptr;

public:
    Private(GpgAddExistingSubkeyEditInteractor *q, const std::string &keygrip)
        : q{q}
        , keygrip{keygrip}
    {
    }

    const char *action(Error &err) const;
    unsigned int nextState(unsigned int statusCode, const char *args, Error &err) const;

    std::string keygrip;
    std::string expiry;
};

const char *GpgAddExistingSubkeyEditInteractor::Private::action(Error &err) const
{
    switch (q->state()) {
    case COMMAND:
        return "addkey";
    case ADD_EXISTING_KEY:
        return "keygrip";
    case KEYGRIP:
        return keygrip.c_str();
    case FLAGS:
        return "Q"; // do not toggle any usage flags
    case VALID:
        return expiry.empty() ? "0" : expiry.c_str();
    case QUIT:
        return "quit";
    case SAVE:
        return "Y";
    case START:
    case KEY_CREATED:
    case ERROR:
        return nullptr;
    default:
        err = Error::fromCode(GPG_ERR_GENERAL);
        return nullptr;
    }
}

unsigned int GpgAddExistingSubkeyEditInteractor::Private::nextState(unsigned int status, const char *args, Error &err) const
{
    using std::strcmp;

    static const Error GENERAL_ERROR  = Error::fromCode(GPG_ERR_GENERAL);
    static const Error NO_KEY_ERROR = Error::fromCode(GPG_ERR_NO_KEY);
    static const Error INV_TIME_ERROR = Error::fromCode(GPG_ERR_INV_TIME);

    if (q->needsNoResponse(status)) {
        return q->state();
    }

    switch (q->state()) {
    case START:
        if (status == GPGME_STATUS_GET_LINE &&
                strcmp(args, "keyedit.prompt") == 0) {
            return COMMAND;
        }
        err = GENERAL_ERROR;
        return ERROR;
    case COMMAND:
        if (status == GPGME_STATUS_GET_LINE &&
                strcmp(args, "keygen.algo") == 0) {
            return ADD_EXISTING_KEY;
        }
        err = GENERAL_ERROR;
        return ERROR;
    case ADD_EXISTING_KEY:
        if (status == GPGME_STATUS_GET_LINE &&
                strcmp(args, "keygen.keygrip") == 0) {
            return KEYGRIP;
        }
        err = GENERAL_ERROR;
        return ERROR;
    case KEYGRIP:
        if (status == GPGME_STATUS_GET_LINE &&
                strcmp(args, "keygen.flags") == 0) {
            return FLAGS;
        } else if (status == GPGME_STATUS_GET_LINE &&
                   strcmp(args, "keygen.keygrip")) {
            err = NO_KEY_ERROR;
            return ERROR;
        }
        err = GENERAL_ERROR;
        return ERROR;
    case FLAGS:
        if (status == GPGME_STATUS_GET_LINE &&
                strcmp(args, "keygen.valid") == 0) {
            return VALID;
        }
        err = GENERAL_ERROR;
        return ERROR;
    case VALID:
        if (status == GPGME_STATUS_KEY_CREATED) {
            return KEY_CREATED;
        }
        if (status == GPGME_STATUS_GET_LINE &&
                strcmp(args, "keyedit.prompt") == 0) {
            return QUIT;
        } else if (status == GPGME_STATUS_GET_LINE &&
                   strcmp(args, "keygen.valid")) {
            err = INV_TIME_ERROR;
            return ERROR;
        }
        err = GENERAL_ERROR;
        return ERROR;
    case KEY_CREATED:
        return QUIT;
    case QUIT:
        if (status == GPGME_STATUS_GET_BOOL &&
                strcmp(args, "keyedit.save.okay") == 0) {
            return SAVE;
        }
        err = GENERAL_ERROR;
        return ERROR;
    case ERROR:
        if (status == GPGME_STATUS_GET_LINE &&
                strcmp(args, "keyedit.prompt") == 0) {
            return QUIT;
        }
        err = q->lastError();
        return ERROR;
    default:
        err = GENERAL_ERROR;
        return ERROR;
    }
}

GpgAddExistingSubkeyEditInteractor::GpgAddExistingSubkeyEditInteractor(const std::string &keygrip)
    : EditInteractor{}
    , d{new Private{this, keygrip}}
{
}

GpgAddExistingSubkeyEditInteractor::~GpgAddExistingSubkeyEditInteractor() = default;

void GpgAddExistingSubkeyEditInteractor::setExpiry(const std::string &timeString)
{
    d->expiry = timeString;
}

const char *GpgAddExistingSubkeyEditInteractor::action(Error &err) const
{
    return d->action(err);
}

unsigned int GpgAddExistingSubkeyEditInteractor::nextState(unsigned int status, const char *args, Error &err) const
{
    return d->nextState(status, args, err);
}