-
Notifications
You must be signed in to change notification settings - Fork 2
/
pageant.c
2691 lines (2322 loc) · 84.1 KB
/
pageant.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* pageant.c: cross-platform code to implement Pageant.
*/
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include "putty.h"
#include "mpint.h"
#include "ssh.h"
#include "sshcr.h"
#include "pageant.h"
/*
* We need this to link with the RSA code, because rsa_ssh1_encrypt()
* pads its data with random bytes. Since we only use rsa_ssh1_decrypt()
* and the signing functions, which are deterministic, this should
* never be called.
*
* If it _is_ called, there is a _serious_ problem, because it
* won't generate true random numbers. So we must scream, panic,
* and exit immediately if that should happen.
*/
void random_read(void *buf, size_t size)
{
modalfatalbox("Internal error: attempt to use random numbers in Pageant");
}
static bool pageant_local = false;
struct PageantClientDialogId {
int dummy;
};
typedef struct PageantPrivateKeySort PageantPrivateKeySort;
typedef struct PageantPublicKeySort PageantPublicKeySort;
typedef struct PageantPrivateKey PageantPrivateKey;
typedef struct PageantPublicKey PageantPublicKey;
typedef struct PageantAsyncOp PageantAsyncOp;
typedef struct PageantAsyncOpVtable PageantAsyncOpVtable;
typedef struct PageantClientRequestNode PageantClientRequestNode;
typedef struct PageantKeyRequestNode PageantKeyRequestNode;
struct PageantClientRequestNode {
PageantClientRequestNode *prev, *next;
};
struct PageantKeyRequestNode {
PageantKeyRequestNode *prev, *next;
};
struct PageantClientInfo {
PageantClient *pc; /* goes to NULL when client is unregistered */
PageantClientRequestNode head;
};
struct PageantAsyncOp {
const PageantAsyncOpVtable *vt;
PageantClientInfo *info;
PageantClientRequestNode cr;
PageantClientRequestId *reqid;
};
struct PageantAsyncOpVtable {
void (*coroutine)(PageantAsyncOp *pao);
void (*free)(PageantAsyncOp *pao);
};
static inline void pageant_async_op_coroutine(PageantAsyncOp *pao)
{ pao->vt->coroutine(pao); }
static inline void pageant_async_op_free(PageantAsyncOp *pao)
{
delete_callbacks_for_context(pao);
pao->vt->free(pao);
}
static inline void pageant_async_op_unlink(PageantAsyncOp *pao)
{
pao->cr.prev->next = pao->cr.next;
pao->cr.next->prev = pao->cr.prev;
}
static inline void pageant_async_op_unlink_and_free(PageantAsyncOp *pao)
{
pageant_async_op_unlink(pao);
pageant_async_op_free(pao);
}
static void pageant_async_op_callback(void *vctx)
{
pageant_async_op_coroutine((PageantAsyncOp *)vctx);
}
/*
* Master lists of all the keys we have stored, in any form at all.
*
* We store private and public keys in separate lists, because
* multiple public keys can share the same private key (due to one
* having a certificate and the other not, or having more than one
* different certificate). And when we decrypt or re-encrypt a private
* key, we don't really want to faff about doing it multiple times if
* there's more than one public key it goes with. If someone tries to
* re-encrypt a key to make their machine safer against unattended
* access, then it would be embarrassing to find they'd forgotten to
* re-encrypt the _other_ copy of it; conversely, once you've
* decrypted a key, it's pointless to make someone type yet another
* passphrase.
*
* (Causing multiple keys to become decrypted in one go isn't a
* security hole in its own right, because the signatures generated by
* certified and uncertified keys are identical. So an attacker
* gaining access to an agent containing one encrypted and one
* cleartext key with the same private half would still be *able* to
* generate signatures that went with the encrypted one, even if the
* agent refused to hand them out in response to the most obvious kind
* of request.)
*/
struct PageantPrivateKeySort {
/*
* Information used by the sorting criterion for the private key
* tree.
*/
int ssh_version; /* 1 or 2; primary sort key */
ptrlen base_pub; /* secondary sort key; never includes a certificate */
};
static int privkey_cmpfn(void *av, void *bv)
{
PageantPrivateKeySort *a = (PageantPrivateKeySort *)av;
PageantPrivateKeySort *b = (PageantPrivateKeySort *)bv;
if (a->ssh_version != b->ssh_version)
return a->ssh_version < b->ssh_version ? -1 : +1;
else
return ptrlen_strcmp(a->base_pub, b->base_pub);
}
struct PageantPublicKeySort {
/*
* Information used by the sorting criterion for the public key
* tree. Begins with the private key sorting criterion, so that
* all the public keys sharing a private key appear adjacent in
* the tree. That's a reasonably sensible order to list them in
* for the user, and more importantly, it makes it easy to
* discover when we're deleting the last public key that goes with
* a particular private one, so as to delete that too. Easier than
* messing about with fragile reference counts.
*/
PageantPrivateKeySort priv;
ptrlen full_pub; /* may match priv.base_pub, or may include a cert */
};
static int pubkey_cmpfn(void *av, void *bv)
{
PageantPublicKeySort *a = (PageantPublicKeySort *)av;
PageantPublicKeySort *b = (PageantPublicKeySort *)bv;
int c = privkey_cmpfn(&a->priv, &b->priv);
if (c)
return c;
else
return ptrlen_strcmp(a->full_pub, b->full_pub);
}
struct PageantPrivateKey {
PageantPrivateKeySort sort;
strbuf *base_pub; /* the true owner of sort.base_pub */
union {
RSAKey *rkey; /* if sort.priv.ssh_version == 1 */
ssh_key *skey; /* if sort.priv.ssh_version == 2 */
};
strbuf *encrypted_key_file;
/* encrypted_key_comment stores the comment belonging to the
* encrypted key file. This is used when presenting deferred
* decryption prompts, because if the user had encrypted their
* uncert and cert keys with different passphrases, the passphrase
* prompt must reliably signal which file they're supposed to be
* entering the passphrase for. */
char *encrypted_key_comment;
bool decryption_prompt_active;
PageantKeyRequestNode blocked_requests;
PageantClientDialogId dlgid;
};
static tree234 *privkeytree;
struct PageantPublicKey {
PageantPublicKeySort sort;
strbuf *base_pub; /* the true owner of sort.priv.base_pub */
strbuf *full_pub; /* the true owner of sort.full_pub */
char *comment;
};
static tree234 *pubkeytree;
typedef struct PageantSignOp PageantSignOp;
struct PageantSignOp {
PageantPrivateKey *priv;
strbuf *data_to_sign;
unsigned flags;
int crLine;
unsigned char failure_type;
PageantKeyRequestNode pkr;
PageantAsyncOp pao;
};
/* Master lock that indicates whether a GUI request is currently in
* progress */
static bool gui_request_in_progress = false;
static PageantKeyRequestNode requests_blocked_on_gui =
{ &requests_blocked_on_gui, &requests_blocked_on_gui };
static void failure(PageantClient *pc, PageantClientRequestId *reqid,
strbuf *sb, unsigned char type, const char *fmt, ...);
static void fail_requests_for_key(PageantPrivateKey *priv, const char *reason);
static PageantPublicKey *pageant_nth_pubkey(int ssh_version, int i);
static void pk_priv_free(PageantPrivateKey *priv)
{
if (priv->base_pub)
strbuf_free(priv->base_pub);
if (priv->sort.ssh_version == 1 && priv->rkey) {
freersakey(priv->rkey);
sfree(priv->rkey);
}
if (priv->sort.ssh_version == 2 && priv->skey) {
ssh_key_free(priv->skey);
}
if (priv->encrypted_key_file)
strbuf_free(priv->encrypted_key_file);
if (priv->encrypted_key_comment)
sfree(priv->encrypted_key_comment);
fail_requests_for_key(priv, "key deleted from Pageant while signing "
"request was pending");
sfree(priv);
}
static void pk_pub_free(PageantPublicKey *pub)
{
if (pub->full_pub)
strbuf_free(pub->full_pub);
sfree(pub->comment);
sfree(pub);
}
static strbuf *makeblob1(RSAKey *rkey)
{
strbuf *blob = strbuf_new();
rsa_ssh1_public_blob(BinarySink_UPCAST(blob), rkey,
RSA_SSH1_EXPONENT_FIRST);
return blob;
}
static strbuf *makeblob2full(ssh_key *key)
{
strbuf *blob = strbuf_new();
ssh_key_public_blob(key, BinarySink_UPCAST(blob));
return blob;
}
static strbuf *makeblob2base(ssh_key *key)
{
strbuf *blob = strbuf_new();
ssh_key_public_blob(ssh_key_base_key(key), BinarySink_UPCAST(blob));
return blob;
}
static PageantPrivateKey *pub_to_priv(PageantPublicKey *pub)
{
PageantPrivateKey *priv = find234(privkeytree, &pub->sort.priv, NULL);
assert(priv && "Public and private trees out of sync!");
return priv;
}
static PageantPublicKey *findpubkey1(RSAKey *reqkey)
{
strbuf *blob = makeblob1(reqkey);
PageantPublicKeySort sort;
sort.priv.ssh_version = 1;
sort.priv.base_pub = ptrlen_from_strbuf(blob);
sort.full_pub = ptrlen_from_strbuf(blob);
PageantPublicKey *toret = find234(pubkeytree, &sort, NULL);
strbuf_free(blob);
return toret;
}
/*
* Constructs the base_pub element of a PageantPublicKeySort, starting
* from full_pub. This may involve allocating a strbuf to store it in,
* which must survive until after you've finished using the resulting
* PageantPublicKeySort. Hence, the strbuf (if any) is returned from
* this function, and if it's non-NULL then the caller must eventually
* free it.
*/
static strbuf *make_base_pub_2(PageantPublicKeySort *sort)
{
/* Start with the fallback option of making base_pub equal full_pub */
sort->priv.base_pub = sort->full_pub;
/* Now reconstruct a distinct base_pub without a cert, if possible
* and necessary */
strbuf *base_pub = NULL;
BinarySource src[1];
BinarySource_BARE_INIT_PL(src, sort->full_pub);
ptrlen algname = get_string(src);
const ssh_keyalg *alg = find_pubkey_alg_len(algname);
if (alg && alg->is_certificate) {
ssh_key *key = ssh_key_new_pub(alg, sort->full_pub);
if (key) {
base_pub = strbuf_new();
ssh_key_public_blob(ssh_key_base_key(key),
BinarySink_UPCAST(base_pub));
sort->priv.base_pub = ptrlen_from_strbuf(base_pub);
ssh_key_free(key);
}
}
return base_pub; /* caller must free once they're done with sort */
}
static PageantPublicKey *findpubkey2(ptrlen full_pub)
{
PageantPublicKeySort sort;
sort.priv.ssh_version = 2;
sort.full_pub = full_pub;
strbuf *base_pub = make_base_pub_2(&sort);
PageantPublicKey *toret = find234(pubkeytree, &sort, NULL);
if (base_pub)
strbuf_free(base_pub);
return toret;
}
static int find_first_pubkey_for_version(int ssh_version)
{
PageantPublicKeySort sort;
sort.priv.ssh_version = ssh_version;
sort.priv.base_pub = PTRLEN_LITERAL("");
sort.full_pub = PTRLEN_LITERAL("");
int pos;
if (findrelpos234(pubkeytree, &sort, NULL, REL234_GE, &pos))
return pos;
return count234(pubkeytree);
}
static int count_keys(int ssh_version)
{
return (find_first_pubkey_for_version(ssh_version + 1) -
find_first_pubkey_for_version(ssh_version));
}
int pageant_count_ssh1_keys(void) { return count_keys(1); }
int pageant_count_ssh2_keys(void) { return count_keys(2); }
/*
* Common code to add a key to the trees. We fill in as many fields
* here as we can share between SSH versions: the ptrlens in the
* sorting field, the whole of pub->sort.priv, and the linked list of
* blocked requests.
*/
static bool pageant_add_key_common(PageantPublicKey *pub,
PageantPrivateKey *priv)
{
int ssh_version = priv->sort.ssh_version;
priv->sort.base_pub = ptrlen_from_strbuf(priv->base_pub);
pub->base_pub = strbuf_dup(priv->sort.base_pub);
pub->sort.priv.ssh_version = priv->sort.ssh_version;
pub->sort.priv.base_pub = ptrlen_from_strbuf(pub->base_pub);
pub->sort.full_pub = ptrlen_from_strbuf(pub->full_pub);
priv->blocked_requests.next = priv->blocked_requests.prev =
&priv->blocked_requests;
/*
* Try to add the private key to privkeytree, or combine new parts
* of it with what's already there.
*/
PageantPrivateKey *priv_in_tree = add234(privkeytree, priv);
if (priv_in_tree == priv) {
/* The key wasn't in the tree at all, and we've just added it. */
} else {
/* The key was already in the tree, so we'll be freeing priv. */
if (ssh_version == 2 && priv->skey && !priv_in_tree->skey) {
/* The key was only stored encrypted, and now we have an
* unencrypted version to add to the existing record. */
priv_in_tree->skey = priv->skey;
priv->skey = NULL; /* so pk_priv_free won't free it */
}
if (ssh_version == 2 && priv->encrypted_key_file &&
!priv_in_tree->encrypted_key_file) {
/* Conversely, the key was only stored in clear, and now
* we have an encrypted version to add to it. */
priv_in_tree->encrypted_key_file = priv->encrypted_key_file;
priv->encrypted_key_file = NULL;
priv_in_tree->encrypted_key_comment = priv->encrypted_key_comment;
priv->encrypted_key_comment = NULL;
}
pk_priv_free(priv);
}
/*
* Try to add the public key.
*/
PageantPublicKey *pub_in_tree = add234(pubkeytree, pub);
if (pub_in_tree == pub) {
/* Successfully added a new key. */
return true;
} else {
/* This public key was already there. */
pk_pub_free(pub);
return false;
}
}
static bool pageant_add_ssh1_key(RSAKey *rkey)
{
PageantPublicKey *pub = snew(PageantPublicKey);
memset(pub, 0, sizeof(PageantPublicKey));
PageantPrivateKey *priv = snew(PageantPrivateKey);
memset(priv, 0, sizeof(PageantPrivateKey));
priv->sort.ssh_version = 1;
priv->base_pub = makeblob1(rkey);
pub->full_pub = makeblob1(rkey);
if (rkey->comment)
pub->comment = dupstr(rkey->comment);
priv->rkey = snew(RSAKey);
duprsakey(priv->rkey, rkey);
return pageant_add_key_common(pub, priv);
}
static bool pageant_add_ssh2_key(ssh2_userkey *skey)
{
PageantPublicKey *pub = snew(PageantPublicKey);
memset(pub, 0, sizeof(PageantPublicKey));
PageantPrivateKey *priv = snew(PageantPrivateKey);
memset(priv, 0, sizeof(PageantPrivateKey));
priv->sort.ssh_version = 2;
priv->base_pub = makeblob2base(skey->key);
pub->full_pub = makeblob2full(skey->key);
if (skey->comment)
pub->comment = dupstr(skey->comment);
/* Duplicate the ssh_key to go in priv */
{
strbuf *tmp = strbuf_new_nm();
ssh_key_openssh_blob(skey->key, BinarySink_UPCAST(tmp));
BinarySource src[1];
BinarySource_BARE_INIT_PL(src, ptrlen_from_strbuf(tmp));
priv->skey = ssh_key_new_priv_openssh(ssh_key_alg(skey->key), src);
strbuf_free(tmp);
}
return pageant_add_key_common(pub, priv);
}
static bool pageant_add_ssh2_key_encrypted(PageantPublicKeySort sort,
const char *comment, ptrlen keyfile)
{
PageantPublicKey *pub = snew(PageantPublicKey);
memset(pub, 0, sizeof(PageantPublicKey));
PageantPrivateKey *priv = snew(PageantPrivateKey);
memset(priv, 0, sizeof(PageantPrivateKey));
assert(sort.priv.ssh_version == 2);
priv->sort.ssh_version = sort.priv.ssh_version;
priv->base_pub = strbuf_dup(sort.priv.base_pub);
pub->full_pub = strbuf_dup(sort.full_pub);
pub->comment = dupstr(comment);
priv->encrypted_key_file = strbuf_dup_nm(keyfile);
priv->encrypted_key_comment = dupstr(comment);
return pageant_add_key_common(pub, priv);
}
static void remove_pubkey_cleanup(PageantPublicKey *pub)
{
/* Common function called when we've just removed a public key
* from pubkeytree: we must also check whether that was the last
* public key sharing a private half, and if so, remove the
* corresponding private entry too. */
PageantPublicKeySort pubsearch;
pubsearch.priv = pub->sort.priv;
pubsearch.full_pub = PTRLEN_LITERAL("");
PageantPublicKey *pubfound = findrel234(
pubkeytree, &pubsearch, NULL, REL234_GE);
if (pubfound && !privkey_cmpfn(&pub->sort.priv, &pubfound->sort.priv)) {
/* There's still a public key which has the same sort.priv as
* the one we've just removed. We're good. */
} else {
/* We've just removed the last public key of the family, so
* delete the private half as well. */
PageantPrivateKey *priv = del234(privkeytree, &pub->sort.priv);
assert(priv);
assert(!privkey_cmpfn(&priv->sort, &pub->sort.priv));
pk_priv_free(priv);
}
}
static PageantPublicKey *del_pubkey_pos(int pos)
{
PageantPublicKey *deleted = delpos234(pubkeytree, pos);
remove_pubkey_cleanup(deleted);
return deleted;
}
static void del_pubkey(PageantPublicKey *to_delete)
{
PageantPublicKey *deleted = del234(pubkeytree, to_delete);
remove_pubkey_cleanup(deleted);
}
static void remove_all_keys(int ssh_version)
{
int start = find_first_pubkey_for_version(ssh_version);
int end = find_first_pubkey_for_version(ssh_version + 1);
while (end > start) {
PageantPublicKey *pub = del_pubkey_pos(--end);
assert(pub->sort.priv.ssh_version == ssh_version);
pk_pub_free(pub);
}
}
static void list_keys(BinarySink *bs, int ssh_version, bool extended)
{
int i;
PageantPublicKey *pub;
put_uint32(bs, count_keys(ssh_version));
for (i = find_first_pubkey_for_version(ssh_version);
NULL != (pub = index234(pubkeytree, i)); i++) {
if (pub->sort.priv.ssh_version != ssh_version)
break;
if (ssh_version > 1)
put_stringpl(bs, pub->sort.full_pub);
else
put_datapl(bs, pub->sort.full_pub); /* no header */
put_stringpl(bs, ptrlen_from_asciz(pub->comment));
if (extended) {
assert(ssh_version == 2); /* extended lists not supported in v1 */
/*
* Append to each key entry a string containing extension
* data. This string begins with a flags word, and may in
* future contain further data if flag bits are set saying
* that it does. Hence, it's wrapped in a containing
* string, so that clients that only partially understand
* it can still find the parts they do understand.
*/
PageantPrivateKey *priv = pub_to_priv(pub);
strbuf *sb = strbuf_new();
uint32_t flags = 0;
if (!priv->skey)
flags |= LIST_EXTENDED_FLAG_HAS_NO_CLEARTEXT_KEY;
if (priv->encrypted_key_file)
flags |= LIST_EXTENDED_FLAG_HAS_ENCRYPTED_KEY_FILE;
put_uint32(sb, flags);
put_stringsb(bs, sb);
}
}
}
void pageant_make_keylist1(BinarySink *bs) { list_keys(bs, 1, false); }
void pageant_make_keylist2(BinarySink *bs) { list_keys(bs, 2, false); }
void pageant_make_keylist_extended(BinarySink *bs) { list_keys(bs, 2, true); }
void pageant_register_client(PageantClient *pc)
{
pc->info = snew(PageantClientInfo);
pc->info->pc = pc;
pc->info->head.prev = pc->info->head.next = &pc->info->head;
}
void pageant_unregister_client(PageantClient *pc)
{
PageantClientInfo *info = pc->info;
assert(info);
assert(info->pc == pc);
while (pc->info->head.next != &pc->info->head) {
PageantAsyncOp *pao = container_of(pc->info->head.next,
PageantAsyncOp, cr);
pageant_async_op_unlink_and_free(pao);
}
sfree(pc->info);
}
static PRINTF_LIKE(5, 6) void failure(
PageantClient *pc, PageantClientRequestId *reqid, strbuf *sb,
unsigned char type, const char *fmt, ...)
{
strbuf_clear(sb);
put_byte(sb, type);
if (!pc->suppress_logging) {
va_list ap;
va_start(ap, fmt);
char *msg = dupvprintf(fmt, ap);
va_end(ap);
pageant_client_log(pc, reqid, "reply: SSH_AGENT_FAILURE (%s)", msg);
sfree(msg);
}
}
static void signop_link_to_key(PageantSignOp *so)
{
assert(!so->pkr.prev);
assert(!so->pkr.next);
so->pkr.prev = so->priv->blocked_requests.prev;
so->pkr.next = &so->priv->blocked_requests;
so->pkr.prev->next = &so->pkr;
so->pkr.next->prev = &so->pkr;
}
static void signop_link_to_pending_gui_request(PageantSignOp *so)
{
assert(!so->pkr.prev);
assert(!so->pkr.next);
so->pkr.prev = requests_blocked_on_gui.prev;
so->pkr.next = &requests_blocked_on_gui;
so->pkr.prev->next = &so->pkr;
so->pkr.next->prev = &so->pkr;
}
static void signop_unlink(PageantSignOp *so)
{
if (so->pkr.next) {
assert(so->pkr.prev);
so->pkr.next->prev = so->pkr.prev;
so->pkr.prev->next = so->pkr.next;
so->pkr.prev = so->pkr.next = NULL;
} else {
assert(!so->pkr.prev);
}
}
static void signop_free(PageantAsyncOp *pao)
{
PageantSignOp *so = container_of(pao, PageantSignOp, pao);
strbuf_free(so->data_to_sign);
sfree(so);
}
static bool request_passphrase(PageantClient *pc, PageantPrivateKey *priv)
{
if (!priv->decryption_prompt_active) {
assert(!gui_request_in_progress);
bool created_dlg = pageant_client_ask_passphrase(
pc, &priv->dlgid, priv->encrypted_key_comment);
if (!created_dlg)
return false;
gui_request_in_progress = true;
priv->decryption_prompt_active = true;
}
return true;
}
static void signop_coroutine(PageantAsyncOp *pao)
{
PageantSignOp *so = container_of(pao, PageantSignOp, pao);
strbuf *response;
crBegin(so->crLine);
while (!so->priv->skey && gui_request_in_progress) {
signop_link_to_pending_gui_request(so);
crReturnV;
signop_unlink(so);
}
if (!so->priv->skey) {
assert(so->priv->encrypted_key_file);
if (!request_passphrase(so->pao.info->pc, so->priv)) {
response = strbuf_new();
failure(so->pao.info->pc, so->pao.reqid, response,
so->failure_type, "on-demand decryption could not "
"prompt for a passphrase");
goto respond;
}
signop_link_to_key(so);
crReturnV;
signop_unlink(so);
}
uint32_t supported_flags = ssh_key_supported_flags(so->priv->skey);
if (so->flags & ~supported_flags) {
/*
* We MUST reject any message containing flags we don't
* understand.
*/
response = strbuf_new();
failure(so->pao.info->pc, so->pao.reqid, response, so->failure_type,
"unsupported flag bits 0x%08"PRIx32,
so->flags & ~supported_flags);
goto respond;
}
char *invalid = ssh_key_invalid(so->priv->skey, so->flags);
if (invalid) {
response = strbuf_new();
failure(so->pao.info->pc, so->pao.reqid, response, so->failure_type,
"key invalid: %s", invalid);
sfree(invalid);
goto respond;
}
strbuf *signature = strbuf_new();
ssh_key_sign(so->priv->skey, ptrlen_from_strbuf(so->data_to_sign),
so->flags, BinarySink_UPCAST(signature));
response = strbuf_new();
put_byte(response, SSH2_AGENT_SIGN_RESPONSE);
put_stringsb(response, signature);
respond:
pageant_client_got_response(so->pao.info->pc, so->pao.reqid,
ptrlen_from_strbuf(response));
strbuf_free(response);
pageant_async_op_unlink_and_free(&so->pao);
crFinishFreedV;
}
static const PageantAsyncOpVtable signop_vtable = {
.coroutine = signop_coroutine,
.free = signop_free,
};
static void fail_requests_for_key(PageantPrivateKey *priv, const char *reason)
{
while (priv->blocked_requests.next != &priv->blocked_requests) {
PageantSignOp *so = container_of(priv->blocked_requests.next,
PageantSignOp, pkr);
signop_unlink(so);
strbuf *sb = strbuf_new();
failure(so->pao.info->pc, so->pao.reqid, sb, so->failure_type,
"%s", reason);
pageant_client_got_response(so->pao.info->pc, so->pao.reqid,
ptrlen_from_strbuf(sb));
strbuf_free(sb);
pageant_async_op_unlink_and_free(&so->pao);
}
}
static void unblock_requests_for_key(PageantPrivateKey *priv)
{
for (PageantKeyRequestNode *pkr = priv->blocked_requests.next;
pkr != &priv->blocked_requests; pkr = pkr->next) {
PageantSignOp *so = container_of(pkr, PageantSignOp, pkr);
queue_toplevel_callback(pageant_async_op_callback, &so->pao);
}
}
static void unblock_pending_gui_requests(void)
{
for (PageantKeyRequestNode *pkr = requests_blocked_on_gui.next;
pkr != &requests_blocked_on_gui; pkr = pkr->next) {
PageantSignOp *so = container_of(pkr, PageantSignOp, pkr);
queue_toplevel_callback(pageant_async_op_callback, &so->pao);
}
}
void pageant_passphrase_request_success(PageantClientDialogId *dlgid,
ptrlen passphrase)
{
PageantPrivateKey *priv = container_of(dlgid, PageantPrivateKey, dlgid);
assert(gui_request_in_progress);
gui_request_in_progress = false;
priv->decryption_prompt_active = false;
if (!priv->skey) {
const char *error;
BinarySource src[1];
BinarySource_BARE_INIT_PL(src, ptrlen_from_strbuf(
priv->encrypted_key_file));
strbuf *ppsb = strbuf_dup_nm(passphrase);
ssh2_userkey *skey = ppk_load_s(src, ppsb->s, &error);
strbuf_free(ppsb);
if (!skey) {
fail_requests_for_key(priv, "unable to decrypt key");
return;
} else if (skey == SSH2_WRONG_PASSPHRASE) {
/*
* Find a PageantClient to use for another attempt at
* request_passphrase.
*/
PageantKeyRequestNode *pkr = priv->blocked_requests.next;
if (pkr == &priv->blocked_requests) {
/*
* Special case: if all the requests have gone away at
* this point, we need not bother putting up a request
* at all any more.
*/
return;
}
PageantSignOp *so = container_of(priv->blocked_requests.next,
PageantSignOp, pkr);
priv->decryption_prompt_active = false;
if (!request_passphrase(so->pao.info->pc, so->priv)) {
fail_requests_for_key(priv, "unable to continue creating "
"passphrase prompts");
}
return;
} else {
priv->skey = skey->key;
sfree(skey->comment);
sfree(skey);
keylist_update();
}
}
unblock_requests_for_key(priv);
unblock_pending_gui_requests();
}
void pageant_passphrase_request_refused(PageantClientDialogId *dlgid)
{
PageantPrivateKey *priv = container_of(dlgid, PageantPrivateKey, dlgid);
assert(gui_request_in_progress);
gui_request_in_progress = false;
priv->decryption_prompt_active = false;
fail_requests_for_key(priv, "user refused to supply passphrase");
unblock_pending_gui_requests();
}
typedef struct PageantImmOp PageantImmOp;
struct PageantImmOp {
int crLine;
strbuf *response;
PageantAsyncOp pao;
};
static void immop_free(PageantAsyncOp *pao)
{
PageantImmOp *io = container_of(pao, PageantImmOp, pao);
if (io->response)
strbuf_free(io->response);
sfree(io);
}
static void immop_coroutine(PageantAsyncOp *pao)
{
PageantImmOp *io = container_of(pao, PageantImmOp, pao);
crBegin(io->crLine);
if (0) crReturnV;
pageant_client_got_response(io->pao.info->pc, io->pao.reqid,
ptrlen_from_strbuf(io->response));
pageant_async_op_unlink_and_free(&io->pao);
crFinishFreedV;
}
static const PageantAsyncOpVtable immop_vtable = {
.coroutine = immop_coroutine,
.free = immop_free,
};
static bool reencrypt_key(PageantPublicKey *pub)
{
PageantPrivateKey *priv = pub_to_priv(pub);
if (priv->sort.ssh_version != 2) {
/*
* We don't support storing SSH-1 keys in encrypted form at
* all.
*/
return false;
}
if (!priv->encrypted_key_file) {
/*
* We can't re-encrypt a key if it doesn't have an encrypted
* form. (We could make one up, of course - but with what
* passphrase that we could expect the user to know later?)
*/
return false;
}
/* Only actually free priv->skey if it exists. But we return success
* regardless, so that 'please ensure this key isn't stored
* decrypted' is idempotent. */
if (priv->skey) {
ssh_key_free(priv->skey);
priv->skey = NULL;
}
return true;
}
#define DECL_EXT_ENUM(id, name) id,
enum Extension { KNOWN_EXTENSIONS(DECL_EXT_ENUM) EXT_UNKNOWN };
#define DEF_EXT_NAMES(id, name) PTRLEN_DECL_LITERAL(name),
static const ptrlen extension_names[] = { KNOWN_EXTENSIONS(DEF_EXT_NAMES) };
static PageantAsyncOp *pageant_make_op(
PageantClient *pc, PageantClientRequestId *reqid, ptrlen msgpl)
{
BinarySource msg[1];
strbuf *sb = strbuf_new_nm();
unsigned char failure_type = SSH_AGENT_FAILURE;
int type;
#define fail(...) failure(pc, reqid, sb, failure_type, __VA_ARGS__)
BinarySource_BARE_INIT_PL(msg, msgpl);
type = get_byte(msg);
if (get_err(msg)) {
fail("message contained no type code");
goto responded;
}
switch (type) {
case SSH1_AGENTC_REQUEST_RSA_IDENTITIES: {
/*
* Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
*/
pageant_client_log(pc, reqid,
"request: SSH1_AGENTC_REQUEST_RSA_IDENTITIES");
put_byte(sb, SSH1_AGENT_RSA_IDENTITIES_ANSWER);
pageant_make_keylist1(BinarySink_UPCAST(sb));
pageant_client_log(pc, reqid,
"reply: SSH1_AGENT_RSA_IDENTITIES_ANSWER");
if (!pc->suppress_logging) {
int i;
PageantPublicKey *pub;
for (i = 0; NULL != (pub = pageant_nth_pubkey(1, i)); i++) {
PageantPrivateKey *priv = pub_to_priv(pub);
char *fingerprint = rsa_ssh1_fingerprint(priv->rkey);
pageant_client_log(pc, reqid, "returned key: %s",
fingerprint);
sfree(fingerprint);
}
}
break;
}
case SSH2_AGENTC_REQUEST_IDENTITIES: {
/*
* Reply with SSH2_AGENT_IDENTITIES_ANSWER.
*/
pageant_client_log(pc, reqid,
"request: SSH2_AGENTC_REQUEST_IDENTITIES");
put_byte(sb, SSH2_AGENT_IDENTITIES_ANSWER);
pageant_make_keylist2(BinarySink_UPCAST(sb));
pageant_client_log(pc, reqid, "reply: SSH2_AGENT_IDENTITIES_ANSWER");
if (!pc->suppress_logging) {
int i;
PageantPublicKey *pub;
for (i = 0; NULL != (pub = pageant_nth_pubkey(2, i)); i++) {
char *fingerprint = ssh2_double_fingerprint_blob(
pub->sort.full_pub, SSH_FPTYPE_DEFAULT);
pageant_client_log(pc, reqid, "returned key: %s %s",
fingerprint, pub->comment);
sfree(fingerprint);
}
}
break;
}
case SSH1_AGENTC_RSA_CHALLENGE: {
/*
* Reply with either SSH1_AGENT_RSA_RESPONSE or
* SSH_AGENT_FAILURE, depending on whether we have that key
* or not.
*/
RSAKey reqkey;
PageantPublicKey *pub;