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
|
#include "dat.h"
#include <mp.h>
#include <libsec.h>
typedef struct Sshrsastate Sshrsastate;
enum {
CReadpub,
CWritechal,
CReadresp,
};
struct State
{
RSApriv *priv;
Key *k;
mpint *resp;
int phase;
};
static RSApriv*
readrsapriv(char *s)
{
RSApriv *priv;
priv = rsaprivalloc();
strtoul(s, &s, 10);
if((priv->pub.ek=strtomp(s, &s, 16, nil)) == nil)
goto Error;
if((priv->dk=strtomp(s, &s, 16, nil)) == nil)
goto Error;
if((priv->pub.n=strtomp(s, &s, 16, nil)) == nil)
goto Error;
if((priv->p=strtomp(s, &s, 16, nil)) == nil)
goto Error;
if((priv->q=strtomp(s, &s, 16, nil)) == nil)
goto Error;
if((priv->kp=strtomp(s, &s, 16, nil)) == nil)
goto Error;
if((priv->kq=strtomp(s, &s, 16, nil)) == nil)
goto Error;
if((priv->c2=strtomp(s, &s, 16, nil)) == nil)
goto Error;
return priv;
Error:
rsaprivfree(priv);
return nil;
}
int
sshinit(Fsstate *fss,
sshrsaopen(Key *k, char*, int client)
{
Sshrsastate *s;
fmtinstall('B', mpconv);
assert(client);
s = emalloc(sizeof *s);
s->priv = readrsapriv(s_to_c(k->data));
s->k = k;
if(s->priv == nil){
agentlog("error parsing ssh key %s", k->file);
free(s);
return nil;
}
return s;
}
int
sshrsaread(void *va, void *buf, int n)
{
Sshrsastate *s;
s = va;
switch(s->phase){
case Readpub:
s->phase = Done;
return snprint(buf, n, "%B", s->priv->pub.n);
case Readresp:
s->phase = Done;
return snprint(buf, n, "%B", s->resp);
default:
return 0;
}
}
int
sshrsawrite(void *va, void *vbuf, int n)
{
mpint *m;
char *buf;
Sshrsastate *s;
s = va;
if((s->k->flags&Fconfirmuse) && confirm("ssh use") < 0)
return -1;
buf = emalloc(n+1);
memmove(buf, vbuf, n);
buf[n] = '\0';
m = strtomp(buf, nil, 16, nil);
free(buf);
if(m == nil){
werrstr("bad bignum");
return -1;
}
agentlog("ssh use");
m = rsadecrypt(s->priv, m, m);
s->resp = m;
s->phase = Readresp;
return n;
}
void
sshrsaclose(void *v)
{
Sshrsastate *s;
s = v;
rsaprivfree(s->priv);
mpfree(s->resp);
free(s);
}
Proto sshrsa = {
.name= "ssh-rsa",
.perm= 0666,
.open= sshrsaopen,
.read= sshrsaread,
.write= sshrsawrite,
.close= sshrsaclose,
};
|