-
Notifications
You must be signed in to change notification settings - Fork 71
/
SqueakSSL.c
586 lines (519 loc) · 14.9 KB
/
SqueakSSL.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
/* Automatically generated by
VMPluginCodeGenerator VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1
from
SqueakSSLPlugin VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1
*/
static char __buildInfo[] = "SqueakSSLPlugin VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1 " __DATE__ ;
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Default EXPORT macro that does nothing (see comment in sq.h): */
#define EXPORT(returnType) returnType
/* Do not include the entire sq.h file but just those parts needed. */
#include "sqConfig.h" /* Configuration options */
#include "sqVirtualMachine.h" /* The virtual machine proxy definition */
#include "sqPlatformSpecific.h" /* Platform specific definitions */
#define true 1
#define false 0
#define null 0 /* using 'null' because nil is predefined in Think C */
#ifdef SQUEAK_BUILTIN_PLUGIN
# undef EXPORT
# define EXPORT(returnType) static returnType
#endif
#include "SqueakSSL.h"
#include "sqMemoryAccess.h"
/*** Function Prototypes ***/
EXPORT(const char*) getModuleName(void);
EXPORT(sqInt) primitiveAccept(void);
EXPORT(sqInt) primitiveConnect(void);
EXPORT(sqInt) primitiveCreate(void);
EXPORT(sqInt) primitiveDecrypt(void);
EXPORT(sqInt) primitiveDestroy(void);
EXPORT(sqInt) primitiveEncrypt(void);
EXPORT(sqInt) primitiveGetIntProperty(void);
EXPORT(sqInt) primitiveGetStringProperty(void);
EXPORT(sqInt) primitiveSetIntProperty(void);
EXPORT(sqInt) primitiveSetStringProperty(void);
EXPORT(sqInt) setInterpreter(struct VirtualMachine *anInterpreter);
/*** Variables ***/
#if !defined(SQUEAK_BUILTIN_PLUGIN)
static sqInt (*byteSizeOf)(sqInt oop);
static sqInt (*classString)(void);
static sqInt (*failed)(void);
static void * (*firstIndexableField)(sqInt oop);
static sqInt (*instantiateClassindexableSize)(sqInt classPointer, sqInt size);
static sqInt (*isBytes)(sqInt oop);
static sqInt (*methodArgumentCount)(void);
static sqInt (*nilObject)(void);
static sqInt (*pop)(sqInt nItems);
static sqInt (*popthenPush)(sqInt nItems, sqInt oop);
static sqInt (*primitiveFail)(void);
static sqInt (*pushInteger)(sqInt integerValue);
static sqInt (*signed32BitIntegerFor)(sqInt integerValue);
static int (*signed32BitValueOf)(sqInt oop);
static sqInt (*stackIntegerValue)(sqInt offset);
static sqInt (*stackValue)(sqInt offset);
#else /* !defined(SQUEAK_BUILTIN_PLUGIN) */
extern sqInt byteSizeOf(sqInt oop);
extern sqInt classString(void);
extern sqInt failed(void);
extern void * firstIndexableField(sqInt oop);
extern sqInt instantiateClassindexableSize(sqInt classPointer, sqInt size);
extern sqInt isBytes(sqInt oop);
extern sqInt methodArgumentCount(void);
extern sqInt nilObject(void);
extern sqInt pop(sqInt nItems);
extern sqInt popthenPush(sqInt nItems, sqInt oop);
extern sqInt primitiveFail(void);
extern sqInt pushInteger(sqInt integerValue);
extern sqInt signed32BitIntegerFor(sqInt integerValue);
extern int signed32BitValueOf(sqInt oop);
extern sqInt stackIntegerValue(sqInt offset);
extern sqInt stackValue(sqInt offset);
extern
#endif
struct VirtualMachine* interpreterProxy;
static const char *moduleName =
#ifdef SQUEAK_BUILTIN_PLUGIN
"SqueakSSL VMMaker.oscog-eem.2480 (i)"
#else
"SqueakSSL VMMaker.oscog-eem.2480 (e)"
#endif
;
/* Note: This is hardcoded so it can be run from Squeak.
The module name is used for validating a module *after*
it is loaded to check if it does really contain the module
we're thinking it contains. This is important! */
/* InterpreterPlugin>>#getModuleName */
EXPORT(const char*)
getModuleName(void)
{
return moduleName;
}
/* Primitive. Starts or continues a server handshake using the current
session. Will eventually produce output to be sent to the client. Requires
the host
and cert name to be set for the session. Returns a code indicating the
sate of the connection:
> 0 - Number of bytes to be sent to the client.
0 - Success. The connection is established.
-1 - More input is required.
< -1 - Other errors.
*/
/* SqueakSSLPlugin>>#primitiveAccept */
EXPORT(sqInt)
primitiveAccept(void)
{
sqInt dstLen;
sqInt dstOop;
char *dstPtr;
sqInt handle;
sqInt result;
sqInt srcLen;
sqInt srcOop;
char *srcPtr;
sqInt start;
if (!((methodArgumentCount()) == 5)) {
return primitiveFail();
}
dstOop = stackValue(0);
srcLen = stackIntegerValue(1);
start = stackIntegerValue(2);
srcOop = stackValue(3);
handle = stackIntegerValue(4);
if (failed()) {
return null;
}
if (!(((start > 0)
&& (srcLen >= 0))
&& ((isBytes(srcOop))
&& ((isBytes(dstOop))
&& ((byteSizeOf(srcOop)) >= ((start + srcLen) - 1)))))) {
return primitiveFail();
}
srcPtr = firstIndexableField(srcOop);
dstPtr = firstIndexableField(dstOop);
srcPtr = (srcPtr + start) - 1;
dstLen = byteSizeOf(dstOop);
result = sqAcceptSSL(handle, srcPtr, srcLen, dstPtr, dstLen);
if (failed()) {
return null;
}
pop((methodArgumentCount()) + 1);
pushInteger(result);
return 0;
}
/* Primitive. Starts or continues a client handshake using the provided data.
Will eventually produce output to be sent to the server. Requires the host
name to be set for the session.
Returns:
> 0 - Number of bytes to be sent to the server
0 - Success. The connection is established.
-1 - More input is required.
< -1 - Other errors.
*/
/* SqueakSSLPlugin>>#primitiveConnect */
EXPORT(sqInt)
primitiveConnect(void)
{
sqInt dstLen;
sqInt dstOop;
char *dstPtr;
sqInt handle;
sqInt result;
sqInt srcLen;
sqInt srcOop;
char *srcPtr;
sqInt start;
if (!((methodArgumentCount()) == 5)) {
return primitiveFail();
}
dstOop = stackValue(0);
srcLen = stackIntegerValue(1);
start = stackIntegerValue(2);
srcOop = stackValue(3);
handle = stackIntegerValue(4);
if (failed()) {
return null;
}
if (!(((start > 0)
&& (srcLen >= 0))
&& ((isBytes(srcOop))
&& ((isBytes(dstOop))
&& ((byteSizeOf(srcOop)) >= ((start + srcLen) - 1)))))) {
return primitiveFail();
}
srcPtr = firstIndexableField(srcOop);
dstPtr = firstIndexableField(dstOop);
srcPtr = (srcPtr + start) - 1;
dstLen = byteSizeOf(dstOop);
result = sqConnectSSL(handle, srcPtr, srcLen, dstPtr, dstLen);
if (failed()) {
return null;
}
pop((methodArgumentCount()) + 1);
pushInteger(result);
return 0;
}
/* Primitive. Creates a new SSL session and returns its handle. */
/* SqueakSSLPlugin>>#primitiveCreate */
EXPORT(sqInt)
primitiveCreate(void)
{
sqInt handle;
if (!((methodArgumentCount()) == 0)) {
return primitiveFail();
}
handle = sqCreateSSL();
if (handle == 0) {
return primitiveFail();
}
pop((methodArgumentCount()) + 1);
pushInteger(handle);
return 0;
}
/* Primitive. Decrypts a buffer sent via the connection.
Requires the session to be established.
Returns:
>=0 - Number of bytes decrypted in the result buffer
< -1 - Other errors.
*/
/* SqueakSSLPlugin>>#primitiveDecrypt */
EXPORT(sqInt)
primitiveDecrypt(void)
{
sqInt dstLen;
sqInt dstOop;
char *dstPtr;
sqInt handle;
sqInt result;
sqInt srcLen;
sqInt srcOop;
char *srcPtr;
sqInt start;
if (!((methodArgumentCount()) == 5)) {
return primitiveFail();
}
dstOop = stackValue(0);
srcLen = stackIntegerValue(1);
start = stackIntegerValue(2);
srcOop = stackValue(3);
handle = stackIntegerValue(4);
if (failed()) {
return null;
}
if (!(((start > 0)
&& (srcLen >= 0))
&& ((isBytes(srcOop))
&& ((isBytes(dstOop))
&& ((byteSizeOf(srcOop)) >= ((start + srcLen) - 1)))))) {
return primitiveFail();
}
srcPtr = firstIndexableField(srcOop);
dstPtr = firstIndexableField(dstOop);
srcPtr = (srcPtr + start) - 1;
dstLen = byteSizeOf(dstOop);
result = sqDecryptSSL(handle, srcPtr, srcLen, dstPtr, dstLen);
if (failed()) {
return null;
}
pop((methodArgumentCount()) + 1);
pushInteger(result);
return 0;
}
/* Primitive. Destroys an SSL session. */
/* SqueakSSLPlugin>>#primitiveDestroy */
EXPORT(sqInt)
primitiveDestroy(void)
{
sqInt handle;
sqInt result;
if (!((methodArgumentCount()) == 1)) {
return primitiveFail();
}
handle = stackIntegerValue(0);
if (failed()) {
return null;
}
result = sqDestroySSL(handle);
if (result == 0) {
return primitiveFail();
}
pop(methodArgumentCount());
return 0;
}
/* Primitive. Encrypts a buffer to be sent to the via the connection.
Requires the session to be established.
Returns:
>=0 - Number of bytes encrypted in the result buffer
< -1 - Other errors.
*/
/* SqueakSSLPlugin>>#primitiveEncrypt */
EXPORT(sqInt)
primitiveEncrypt(void)
{
sqInt dstLen;
sqInt dstOop;
char *dstPtr;
sqInt handle;
sqInt result;
sqInt srcLen;
sqInt srcOop;
char *srcPtr;
sqInt start;
if (!((methodArgumentCount()) == 5)) {
return primitiveFail();
}
dstOop = stackValue(0);
srcLen = stackIntegerValue(1);
start = stackIntegerValue(2);
srcOop = stackValue(3);
handle = stackIntegerValue(4);
if (failed()) {
return null;
}
if (!(((start > 0)
&& (srcLen >= 0))
&& ((isBytes(srcOop))
&& ((isBytes(dstOop))
&& ((byteSizeOf(srcOop)) >= ((start + srcLen) - 1)))))) {
return primitiveFail();
}
srcPtr = firstIndexableField(srcOop);
dstPtr = firstIndexableField(dstOop);
srcPtr = (srcPtr + start) - 1;
dstLen = byteSizeOf(dstOop);
result = sqEncryptSSL(handle, srcPtr, srcLen, dstPtr, dstLen);
if (failed()) {
return null;
}
pop((methodArgumentCount()) + 1);
pushInteger(result);
return 0;
}
/* Primitive. Returns an integer property for the session */
/* SqueakSSLPlugin>>#primitiveGetIntProperty */
EXPORT(sqInt)
primitiveGetIntProperty(void)
{
sqInt handle;
sqInt propID;
sqInt value;
if (!((methodArgumentCount()) == 2)) {
return primitiveFail();
}
propID = stackIntegerValue(0);
handle = stackIntegerValue(1);
if (failed()) {
return null;
}
value = sqGetIntPropertySSL(handle, propID);
if (failed()) {
return null;
}
popthenPush((methodArgumentCount()) + 1, signed32BitIntegerFor(value));
return 0;
}
/* Primitive. Returns a string property for the session */
/* SqueakSSLPlugin>>#primitiveGetStringProperty */
EXPORT(sqInt)
primitiveGetStringProperty(void)
{
sqInt handle;
sqInt i;
char *oopPtr;
sqInt propID;
sqInt stringLen;
sqInt stringOop;
char *stringPtr;
if (!((methodArgumentCount()) == 2)) {
return primitiveFail();
}
propID = stackIntegerValue(0);
handle = stackIntegerValue(1);
if (failed()) {
return null;
}
stringPtr = sqGetStringPropertySSL(handle, propID);
if (failed()) {
return null;
}
if (stringPtr == null) {
stringOop = nilObject();
}
else {
stringLen = strlen(stringPtr);
stringOop = instantiateClassindexableSize(classString(), stringLen);
oopPtr = firstIndexableField(stringOop);
for (i = 0; i < stringLen; i += 1) {
oopPtr[i] = (stringPtr[i]);
}
}
popthenPush((methodArgumentCount()) + 1, stringOop);
return 0;
}
/* Primitive. Sets a integer property for the session */
/* SqueakSSLPlugin>>#primitiveSetIntProperty */
EXPORT(sqInt)
primitiveSetIntProperty(void)
{
sqInt handle;
sqInt propID;
sqInt result;
sqInt value;
if (!((methodArgumentCount()) == 3)) {
return primitiveFail();
}
value = signed32BitValueOf(stackValue(0));
propID = stackIntegerValue(1);
handle = stackIntegerValue(2);
if (failed()) {
return null;
}
result = sqSetIntPropertySSL(handle, propID, value);
if (!result) {
return primitiveFail();
}
if (failed()) {
return null;
}
pop(methodArgumentCount());
return 0;
}
/* Primitive. Sets a string property for the session */
/* SqueakSSLPlugin>>#primitiveSetStringProperty */
EXPORT(sqInt)
primitiveSetStringProperty(void)
{
sqInt handle;
sqInt propID;
sqInt result;
sqInt srcLen;
sqInt srcOop;
char *srcPtr;
if (!((methodArgumentCount()) == 3)) {
return primitiveFail();
}
srcOop = stackValue(0);
propID = stackIntegerValue(1);
handle = stackIntegerValue(2);
if (failed()) {
return null;
}
if (!(isBytes(srcOop))) {
return primitiveFail();
}
srcPtr = firstIndexableField(srcOop);
srcLen = byteSizeOf(srcOop);
result = sqSetStringPropertySSL(handle, propID, srcPtr, srcLen);
if (!result) {
return primitiveFail();
}
if (failed()) {
return null;
}
pop(methodArgumentCount());
return 0;
}
/* Note: This is coded so that it can be run in Squeak. */
/* InterpreterPlugin>>#setInterpreter: */
EXPORT(sqInt)
setInterpreter(struct VirtualMachine *anInterpreter)
{
sqInt ok;
interpreterProxy = anInterpreter;
ok = ((interpreterProxy->majorVersion()) == (VM_PROXY_MAJOR))
&& ((interpreterProxy->minorVersion()) >= (VM_PROXY_MINOR));
if (ok) {
#if !defined(SQUEAK_BUILTIN_PLUGIN)
byteSizeOf = interpreterProxy->byteSizeOf;
classString = interpreterProxy->classString;
failed = interpreterProxy->failed;
firstIndexableField = interpreterProxy->firstIndexableField;
instantiateClassindexableSize = interpreterProxy->instantiateClassindexableSize;
isBytes = interpreterProxy->isBytes;
methodArgumentCount = interpreterProxy->methodArgumentCount;
nilObject = interpreterProxy->nilObject;
pop = interpreterProxy->pop;
popthenPush = interpreterProxy->popthenPush;
primitiveFail = interpreterProxy->primitiveFail;
pushInteger = interpreterProxy->pushInteger;
signed32BitIntegerFor = interpreterProxy->signed32BitIntegerFor;
signed32BitValueOf = interpreterProxy->signed32BitValueOf;
stackIntegerValue = interpreterProxy->stackIntegerValue;
stackValue = interpreterProxy->stackValue;
#endif /* !defined(SQUEAK_BUILTIN_PLUGIN) */
}
return ok;
}
#ifdef SQUEAK_BUILTIN_PLUGIN
static char _m[] = "SqueakSSL";
void* SqueakSSL_exports[][3] = {
{(void*)_m, "getModuleName", (void*)getModuleName},
{(void*)_m, "primitiveAccept\000\001", (void*)primitiveAccept},
{(void*)_m, "primitiveConnect\000\001", (void*)primitiveConnect},
{(void*)_m, "primitiveCreate\000\377", (void*)primitiveCreate},
{(void*)_m, "primitiveDecrypt\000\001", (void*)primitiveDecrypt},
{(void*)_m, "primitiveDestroy\000\000", (void*)primitiveDestroy},
{(void*)_m, "primitiveEncrypt\000\001", (void*)primitiveEncrypt},
{(void*)_m, "primitiveGetIntProperty\000\000", (void*)primitiveGetIntProperty},
{(void*)_m, "primitiveGetStringProperty\000\000", (void*)primitiveGetStringProperty},
{(void*)_m, "primitiveSetIntProperty\000\000", (void*)primitiveSetIntProperty},
{(void*)_m, "primitiveSetStringProperty\000\001", (void*)primitiveSetStringProperty},
{(void*)_m, "setInterpreter", (void*)setInterpreter},
{NULL, NULL, NULL}
};
#else /* ifdef SQ_BUILTIN_PLUGIN */
EXPORT(signed char) primitiveAcceptAccessorDepth = 1;
EXPORT(signed char) primitiveConnectAccessorDepth = 1;
EXPORT(signed char) primitiveDecryptAccessorDepth = 1;
EXPORT(signed char) primitiveDestroyAccessorDepth = 0;
EXPORT(signed char) primitiveEncryptAccessorDepth = 1;
EXPORT(signed char) primitiveGetIntPropertyAccessorDepth = 0;
EXPORT(signed char) primitiveGetStringPropertyAccessorDepth = 0;
EXPORT(signed char) primitiveSetIntPropertyAccessorDepth = 0;
EXPORT(signed char) primitiveSetStringPropertyAccessorDepth = 1;
#endif /* ifdef SQ_BUILTIN_PLUGIN */