summaryrefslogtreecommitdiff
path: root/src/ui/certimportwidget.c
blob: d47851f5cdab8fe0cf907a09b4c80083d0e0253e (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
/* Copyright 2021 Jaakko Keränen <jaakko.keranen@iki.fi>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

#include "certimportwidget.h"

#include "app.h"
#include "color.h"
#include "command.h"
#include "gmcerts.h"
#include "inputwidget.h"
#include "labelwidget.h"
#include "text.h"
#include "ui/util.h"

#include <the_Foundation/file.h>
#include <the_Foundation/tlsrequest.h>
#include <the_Foundation/path.h>
#include <SDL_clipboard.h>

iDefineObjectConstruction(CertImportWidget)

struct Impl_CertImportWidget {
    iWidget widget;
    iLabelWidget *info;
    iLabelWidget *crtLabel;
    iLabelWidget *keyLabel;
    iInputWidget *notes;
    iTlsCertificate *cert;
};

static const char *infoText_ = "Paste a PEM-encoded certificate and/or private key,\n"
                               "or drop a .crt/.key file on the window.";

static iBool tryImport_CertImportWidget_(iCertImportWidget *d, const iBlock *data) {
    iBool ok = iFalse;
    iString pem;
    initBlock_String(&pem, data);
    iTlsCertificate *newCert = newPemKey_TlsCertificate(&pem, &pem);
    const iBool gotNewCrt = !isEmpty_TlsCertificate(newCert);
    const iBool gotNewKey = hasPrivateKey_TlsCertificate(newCert);
    if (d->cert && (gotNewCrt ^ gotNewKey)) { /* One new part? Merge with existing. */
        const iString *crt = collect_String(pem_TlsCertificate(gotNewCrt ? newCert : d->cert));
        const iString *key = collect_String(privateKeyPem_TlsCertificate(gotNewKey ? newCert : d->cert));
        delete_TlsCertificate(d->cert);
        delete_TlsCertificate(newCert);
        d->cert = newPemKey_TlsCertificate(crt, key);
        ok = iTrue;
    }
    else if (gotNewCrt || gotNewKey) {
        delete_TlsCertificate(d->cert);
        d->cert = newCert;
        ok = iTrue;
    }
    else {
        delete_TlsCertificate(newCert);
    }
    deinit_String(&pem);
    /* Update the labels. */ {
        if (d->cert && !isEmpty_TlsCertificate(d->cert)) {
            setTextCStr_LabelWidget(
                d->crtLabel,
                format_CStr("%s%s",
                            uiTextAction_ColorEscape,
                            cstrCollect_String(subject_TlsCertificate(d->cert))));
            setFrameColor_Widget(as_Widget(d->crtLabel), uiTextAction_ColorId);
        }
        else {
            setTextCStr_LabelWidget(d->crtLabel, uiTextCaution_ColorEscape "No Certificate");
            setFrameColor_Widget(as_Widget(d->crtLabel), uiTextCaution_ColorId);
        }
        if (d->cert && hasPrivateKey_TlsCertificate(d->cert)) {
            iString *fng = collect_String(
                hexEncode_Block(collect_Block(privateKeyFingerprint_TlsCertificate(d->cert))));
            insertData_Block(&fng->chars, size_String(fng) / 2, "\n", 1);
            setTextCStr_LabelWidget(
                d->keyLabel, format_CStr("%s%s", uiTextAction_ColorEscape, cstr_String(fng)));
            setFrameColor_Widget(as_Widget(d->keyLabel), uiTextAction_ColorId);
        }
        else {
            setTextCStr_LabelWidget(d->keyLabel, uiTextCaution_ColorEscape "No Private Key");
            setFrameColor_Widget(as_Widget(d->keyLabel), uiTextCaution_ColorId);
        }
    }
    return ok;
}

void init_CertImportWidget(iCertImportWidget *d) {
    iWidget *w = as_Widget(d);
    init_Widget(w);
    setId_Widget(w, "certimport");
    d->cert = NULL;
    /* This should behave similar to sheets. */  {
        setPadding1_Widget(w, 3 * gap_UI);
        setFrameColor_Widget(w, uiSeparator_ColorId);
        setBackgroundColor_Widget(w, uiBackground_ColorId);
        setFlags_Widget(w,
                        mouseModal_WidgetFlag | keepOnTop_WidgetFlag | arrangeVertical_WidgetFlag |
                            arrangeSize_WidgetFlag | centerHorizontal_WidgetFlag |
                            overflowScrollable_WidgetFlag,
                        iTrue);
    }
    addChildFlags_Widget(w,
                         iClob(new_LabelWidget(uiHeading_ColorEscape "IMPORT IDENTITY", NULL)),
                         frameless_WidgetFlag);
    d->info = addChildFlags_Widget(w, iClob(new_LabelWidget(infoText_, NULL)), frameless_WidgetFlag);
    addChild_Widget(w, iClob(makePadding_Widget(gap_UI)));
    d->crtLabel = new_LabelWidget("", NULL); {
        setFont_LabelWidget(d->crtLabel, uiContent_FontId);
        addChildFlags_Widget(w, iClob(d->crtLabel), 0);
        setFrameColor_Widget(as_Widget(d->crtLabel), uiTextCaution_ColorId);
    }
    d->keyLabel = new_LabelWidget("", NULL); {
        setFont_LabelWidget(d->keyLabel, uiContent_FontId);
        addChild_Widget(w, iClob(makePadding_Widget(gap_UI)));
        addChildFlags_Widget(w, iClob(d->keyLabel), 0);
        setFrameColor_Widget(as_Widget(d->keyLabel), uiTextCaution_ColorId);
    }
    addChild_Widget(w, iClob(makePadding_Widget(gap_UI)));
    iWidget *page = new_Widget(); {
        setFlags_Widget(page, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
        iWidget *headings = addChildFlags_Widget(
            page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
        iWidget *values = addChildFlags_Widget(
            page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
        addChild_Widget(headings, iClob(makeHeading_Widget("Notes:")));
        addChild_Widget(values, iClob(d->notes = new_InputWidget(0)));
        setHint_InputWidget(d->notes, "description");
        as_Widget(d->notes)->rect.size.x = gap_UI * 70;
    }
    addChild_Widget(w, iClob(page));
    arrange_Widget(w);
    setSize_Widget(as_Widget(d->crtLabel), init_I2(width_Widget(w) - 6.5 * gap_UI, gap_UI * 12));
    setSize_Widget(as_Widget(d->keyLabel), init_I2(width_Widget(w) - 6.5 * gap_UI, gap_UI * 12));
    /* Buttons. */
    addChild_Widget(w, iClob(makePadding_Widget(gap_UI)));
    iWidget *buttons = makeDialogButtons_Widget(
        (iMenuItem[]){
            { "${cancel}", 0, 0, NULL },
            { uiTextAction_ColorEscape "Import", SDLK_RETURN, KMOD_PRIMARY, "certimport.accept" } },
        2);
    addChild_Widget(w, iClob(buttons));
    arrange_Widget(w);
    if (deviceType_App() != desktop_AppDeviceType) {
        /* Try auto-pasting. */
        postCommand_App("certimport.paste");
    }
}

void deinit_CertImportWidget(iCertImportWidget *d) {
    delete_TlsCertificate(d->cert);
}

static iBool isComplete_CertImportWidget_(const iCertImportWidget *d) {
    return d->cert && !isEmpty_TlsCertificate(d->cert) && hasPrivateKey_TlsCertificate(d->cert);
}

void setPageContent_CertImportWidget(iCertImportWidget *d, const iBlock *content) {
    if (tryImport_CertImportWidget_(d, content)) {
        setTextCStr_LabelWidget(d->info, infoText_);
        if (isComplete_CertImportWidget_(d)) {
            setFocus_Widget(as_Widget(d->notes));
        }
    }
    else {
        setTextCStr_LabelWidget(
            d->info, format_CStr("No certificate/key found on the current page.\n%s", infoText_));
    }
    arrange_Widget(as_Widget(d));
}

static iBool tryImportFromClipboard_CertImportWidget_(iCertImportWidget *d) {
    return tryImport_CertImportWidget_(d, collect_Block(newCStr_Block(SDL_GetClipboardText())));
}

static iBool processEvent_CertImportWidget_(iCertImportWidget *d, const SDL_Event *ev) {
    iWidget *w = as_Widget(d);
    if (ev->type == SDL_KEYDOWN) {
        const int key  = ev->key.keysym.sym;
        const int mods = keyMods_Sym(ev->key.keysym.mod);
        if (key == SDLK_v && mods == KMOD_PRIMARY) {
            if (!tryImportFromClipboard_CertImportWidget_(d)) {
                makeMessage_Widget(uiTextCaution_ColorEscape "PASTED FROM CLIPBOARD",
                                   "No certificate or private key was found.");
            }
            postRefresh_App();
            return iTrue;
        }
    }
    if (isCommand_UserEvent(ev, "certimport.paste")) {
        tryImportFromClipboard_CertImportWidget_(d);
        return iTrue;
    }
    if (isCommand_Widget(w, ev, "cancel")) {
        destroy_Widget(w);
        return iTrue;
    }
    if (isCommand_Widget(w, ev, "certimport.accept")) {
        if (d->cert && !isEmpty_TlsCertificate(d->cert) && hasPrivateKey_TlsCertificate(d->cert)) {
            importIdentity_GmCerts(certs_App(), d->cert, text_InputWidget(d->notes));
            d->cert = NULL; /* taken */
            destroy_Widget(w);
            postCommand_App("idents.changed");
        }
        return iTrue;
    }
    if (ev->type == SDL_DROPFILE) {
        const iString *name = collectNewCStr_String(ev->drop.file);
        iFile *f = new_File(name);
        if (open_File(f, readOnly_FileMode | text_FileMode)) {
            if (tryImport_CertImportWidget_(d, collect_Block(readAll_File(f)))) {
                if (isComplete_CertImportWidget_(d)) {
                    setFocus_Widget(as_Widget(d->notes));
                }
            }
            else {
                makeMessage_Widget(uiTextCaution_ColorEscape "DROPPED FILE",
                                   "No certificate or private key was found.");
            }
        }
        iRelease(f);
        return iTrue;
    }
    return processEvent_Widget(w, ev);
}

static void draw_CertImportWidget_(const iCertImportWidget *d) {
    const iWidget *w = constAs_Widget(d);
    draw_Widget(w);
}

iBeginDefineSubclass(CertImportWidget, Widget)
    .processEvent = (iAny *) processEvent_CertImportWidget_,
    .draw         = (iAny *) draw_CertImportWidget_,
iEndDefineSubclass(CertImportWidget)