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
|
# internal debugging flags
DBG, DBG_CRYPTO, DBG_PACKET, DBG_AUTH, DBG_PROC, DBG_PROTO, DBG_IO, DBG_SCP: con 1<<iota;
# protocol packet types
SSH_MSG_NONE, # 0
SSH_MSG_DISCONNECT,
SSH_SMSG_PUBLIC_KEY,
SSH_CMSG_SESSION_KEY,
SSH_CMSG_USER,
SSH_CMSG_AUTH_RHOSTS,
SSH_CMSG_AUTH_RSA,
SSH_SMSG_AUTH_RSA_CHALLENGE,
SSH_CMSG_AUTH_RSA_RESPONSE,
SSH_CMSG_AUTH_PASSWORD, # 10
SSH_CMSG_REQUEST_PTY,
SSH_CMSG_WINDOW_SIZE,
SSH_CMSG_EXEC_SHELL,
SSH_CMSG_EXEC_CMD,
SSH_SMSG_SUCCESS,
SSH_SMSG_FAILURE,
SSH_CMSG_STDIN_DATA,
SSH_SMSG_STDOUT_DATA,
SSH_SMSG_STDERR_DATA,
SSH_CMSG_EOF, # 20
SSH_SMSG_EXITSTATUS,
SSH_MSG_CHANNEL_OPEN_CONFIRMATION,
SSH_MSG_CHANNEL_OPEN_FAILURE,
SSH_MSG_CHANNEL_DATA,
SSH_MSG_CHANNEL_INPUT_EOF,
SSH_MSG_CHANNEL_OUTPUT_CLOSED,
SSH_MSG_UNIX_DOMAIN_X11_FORWARDING, # obsolete
SSH_SMSG_X11_OPEN,
SSH_CMSG_PORT_FORWARD_REQUEST,
SSH_MSG_PORT_OPEN, # 30
SSH_CMSG_AGENT_REQUEST_FORWARDING,
SSH_SMSG_AGENT_OPEN,
SSH_MSG_IGNORE,
SSH_CMSG_EXIT_CONFIRMATION,
SSH_CMSG_X11_REQUEST_FORWARDING,
SSH_CMSG_AUTH_RHOSTS_RSA,
SSH_MSG_DEBUG,
SSH_CMSG_REQUEST_COMPRESSION,
SSH_CMSG_MAX_PACKET_SIZE,
SSH_CMSG_AUTH_TIS, # 40
SSH_SMSG_AUTH_TIS_CHALLENGE,
SSH_CMSG_AUTH_TIS_RESPONSE,
SSH_CMSG_AUTH_KERBEROS,
SSH_SMSG_AUTH_KERBEROS_RESPONSE,
SSH_CMSG_HAVE_KERBEROS_TGT: con iota;
SSH_MSG_ERROR: con -1;
# protocol flags
SSH_PROTOFLAG_SCREEN_NUMBER: con 1<<0;
SSH_PROTOFLAG_HOST_IN_FWD_OPEN: con 1<<1;
# agent protocol packet types
SSH_AGENTC_NONE,
SSH_AGENTC_REQUEST_RSA_IDENTITIES,
SSH_AGENT_RSA_IDENTITIES_ANSWER,
SSH_AGENTC_RSA_CHALLENGE,
SSH_AGENT_RSA_RESPONSE,
SSH_AGENT_FAILURE,
SSH_AGENT_SUCCESS,
SSH_AGENTC_ADD_RSA_IDENTITY,
SSH_AGENTC_REMOVE_RSA_IDENTITY: con iota;
# protocol constants
SSH_MAX_DATA: con 256*1024;
SSH_MAX_MSG: con SSH_MAX_DATA+4;
SESSKEYLEN: con 32;
SESSIDLEN: con 16;
COOKIELEN: con 8;
# crypto ids
SSH_CIPHER_NONE,
SSH_CIPHER_IDEA,
SSH_CIPHER_DES,
SSH_CIPHER_3DES,
SSH_CIPHER_TSS,
SSH_CIPHER_RC4,
SSH_CIPHER_BLOWFISH: con iota;
# auth method ids
SSH_AUTH_RHOSTS,
SSH_AUTH_RSA,
SSH_AUTH_PASSWORD,
SSH_AUTH_RHOSTS_RSA,
SSH_AUTH_TIS,
SSH_AUTH_USER_RSA: con 1+iota;
Edecode: con "error decoding input packet";
Eencode: con "out of space encoding output packet (BUG)";
Ehangup: con "hungup connection";
Ememory: con "out of memory";
Cipher: module
{
id: fn(): int;
init: fn(key: array of byte, isserver: int);
encrypt: fn(a: array of byte, n: int);
decrypt: fn(a: array of byte, n: int);
};
Auth: module
{
AuthInfo: adt{
user: string;
cap: string;
};
id: fn(): int;
firstmsg: fn(): int;
init: fn(nil: Sshio);
authsrv: fn(nil: ref Sshio->Conn, nil: ref Sshio->Msg): ref AuthInfo;
auth: fn(nil: ref Sshio->Conn): int;
};
Sshio: module
{
PATH: con "sshio.dis";
Conn: adt{
in: chan of (ref Msg, string);
out: chan of ref Msg;
sessid: array of byte;
sesskey: array of byte;
hostkey: ref Keyring->PK.RSA;
flags: int;
cipher: Cipher; # chosen cipher
user: string;
host: string;
interactive: int;
unget: ref Msg;
mk: fn(host: string, fd: ref Sys->FD): ref Conn;
setkey: fn(c: self ref Conn, key: ref Keyring->PK.RSA);
};
Msg: adt{
mtype: int;
data: array of byte;
rp: int; # read pointer
wp: int; # write pointer
ep: int; # byte just beyond message data
mk: fn(mtype: int, length: int): ref Msg;
text: fn(m: self ref Msg): string;
fulltext: fn(m: self ref Msg): string;
get1: fn(m: self ref Msg): int;
get2: fn(m: self ref Msg): int;
get4: fn(m: self ref Msg): int;
getstring: fn(m: self ref Msg): string;
getbytes: fn(m: self ref Msg, n: int): array of byte;
getarray: fn(m: self ref Msg): array of byte;
getipint: fn(m: self ref Msg): ref Keyring->IPint;
getpk: fn(m: self ref Msg): ref Keyring->PK.RSA;
put1: fn(m: self ref Msg, nil: int);
put2: fn(m: self ref Msg, nil: int);
put4: fn(m: self ref Msg, nil: int);
putstring: fn(m: self ref Msg, s: string);
putbytes: fn(m: self ref Msg, a: array of byte, n: int);
putipint: fn(m: self ref Msg, mp: ref Keyring->IPint);
putpk: fn(m: self ref Msg, pk: ref Keyring->PK.RSA);
};
init: fn();
badmsg: fn(nil: ref Msg, nil: int, err: string);
recvmsg: fn(nil: ref Conn, nil: int): ref Msg;
unrecvmsg: fn(nil: ref Conn, nil: ref Msg);
rsapad: fn(nil: ref Keyring->IPint, nil: int): ref Keyring->IPint;
rsaunpad: fn(nil: ref Keyring->IPint): ref Keyring->IPint;
iptorjustbe: fn(nil: ref Keyring->IPint, nil: array of byte, nil: int);
rsaencryptbuf: fn(nil: ref Keyring->PK.RSA, nil: array of byte, nil: int): ref Keyring->IPint;
rsagen: fn(nbits: int): ref Keyring->SK.RSA;
rsaencrypt: fn(key: ref Keyring->PK.RSA, b: ref Keyring->IPint): ref Keyring->IPint;
rsadecrypt: fn(key: ref Keyring->SK.RSA, b: ref Keyring->IPint): ref Keyring->IPint;
debug: fn(nil: int, nil: string);
error: fn(nil: string);
readstrnl: fn(fd: ref Sys->FD, buf: array of byte, nbytes: int): int;
calcsessid: fn(hostmod: ref Keyring->IPint, servermod: ref Keyring->IPint, cookie: array of byte): array of byte;
# sshlog: fn(nil: array of byte); # TBA was ...
fastrand: fn(): int;
eqbytes: fn(a: array of byte, b: array of byte, n: int): int;
readversion: fn(fd: ref Sys->FD): (int, int, string);
hex: fn(a: array of byte): string;
};
|