Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

js_cryptcon.c 35.3 KB
Newer Older
1
/* $Id: js_cryptcon.c,v 1.20 2018/04/06 02:42:37 rswindell Exp $ */
2

3 4 5
// Cyrptlib encryption context...

#include "sbbs.h"
6
#include <cryptlib.h>
7
#include "js_request.h"
deuce's avatar
deuce committed
8
#include "js_cryptcon.h"
9
#include "ssl.h"
10
#include "base64.h"
11

deuce's avatar
deuce committed
12
JSClass js_cryptcon_class;
13
static const char* getprivate_failure = "line %d %s %s JS_GetPrivate failed";
14 15 16 17 18 19 20 21

// Helpers
static void
js_cryptcon_error(JSContext *cx, CRYPT_CONTEXT ctx, int error)
{
	char *errstr;
	int errlen;

deuce's avatar
deuce committed
22
	if (cryptGetAttributeString(ctx, CRYPT_ATTRIBUTE_ERRORMESSAGE, NULL, &errlen) != CRYPT_OK) {
23 24 25 26 27 28 29
		JS_ReportError(cx, "CryptLib error %d", error);
		return;
	}
	if ((errstr = (char *)malloc(errlen+1)) == NULL) {
		JS_ReportError(cx, "CryptLib error %d", error);
		return;
	}
deuce's avatar
deuce committed
30
	if (cryptGetAttributeString(ctx, CRYPT_ATTRIBUTE_ERRORMESSAGE, errstr, &errlen) != CRYPT_OK) {
31 32 33 34 35 36 37 38 39 40
		free(errstr);
		JS_ReportError(cx, "CryptLib error %d", error);
		return;
	}
	errstr[errlen+1] = 0;

	JS_ReportError(cx, "Cryptlib error %d (%s)", error, errstr);
	free(errstr);
}

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
static size_t js_asn1_len(unsigned char *data, size_t len, size_t *off)
{
	size_t sz = 0;
	size_t bytes;
	size_t i;

	if (*off >= len)
		return 0;

	if ((data[*off] & 0x80) == 0) {
		
		sz = data[*off];
		(*off)++;
	}
	else if(data[*off] == 0x80) {
		// We can't actually handle this when we're this simple.
		(*off)++;
	}
	else {
		bytes = data[(*off)++] & 0x7f;
		for (i = 0; i < bytes && *off < len; i++) {
			sz <<= 8;
			sz |= data[(*off)++];
		}
	}

	return sz;
}

static unsigned char js_asn1_type(unsigned char *data, size_t len, size_t *off)
{
	unsigned char t = 0;

	if ((data[*off] & 0x1f) == 0x1f) {
		for ((*off)++; *off < len && data[*off]; (*off)++) {
			if ((data[*off] & 0x80) == 0)
				break;
		}
	}
	else {
		t = data[*off];
		(*off)++;
	}

	return t;
}

deuce's avatar
deuce committed
88 89 90 91 92 93 94 95
static int js_ecc_to_prop(unsigned char *data, size_t len, size_t *off, JSContext *cx, JSObject *parent)
{
	size_t sz;
	JSObject *obj;
	JSString* xstr;
	JSString* ystr;
	char *x;
	char *y;
deuce's avatar
deuce committed
96 97
	unsigned char *z;
	size_t zcnt;
deuce's avatar
deuce committed
98 99 100 101 102 103 104 105 106 107 108
	char *x64;
	char *y64;
	size_t half;

	if (js_asn1_type(data, len, off) == 3) {
		sz = js_asn1_len(data,len,off);
		if (data[*off] == 0 && data[(*off)+1] == 4 && ((sz%1)==0)) {
			half = (sz - 2) / 2;
			x = malloc(half);
			if (x == NULL)
				return 0;
deuce's avatar
deuce committed
109 110 111
			for (z=data+(*off)+2, zcnt=half; *z==0 && half; z++, zcnt--);
			memcpy(x, z, zcnt);
			x64 = malloc(zcnt*4/3+3);
deuce's avatar
deuce committed
112 113 114 115
			if (x64 == NULL) {
				free(x);
				return 0;
			}
deuce's avatar
deuce committed
116
			b64_encode(x64, zcnt*4/3+3, x, zcnt);
deuce's avatar
deuce committed
117 118 119 120 121 122 123 124 125 126 127 128 129
			free(x);
			for (x=x64; *x; x++) {
				if (*x == '+')
					*x = '-';
				else if (*x == '/')
					*x = '_';
				else if (*x == '=')
					*x = 0;
			}

			y = malloc(half);
			if (y == NULL)
				return 0;
deuce's avatar
deuce committed
130 131 132
			for (z=data+(*off)+2+half, zcnt=half; *z==0 && half; z++, zcnt--);
			memcpy(y, z, zcnt);
			y64 = malloc(zcnt*4/3+3);
deuce's avatar
deuce committed
133
			if (y64 == NULL) {
134
				free(x);
deuce's avatar
deuce committed
135 136 137
				free(y);
				return 0;
			}
deuce's avatar
deuce committed
138
			b64_encode(y64, zcnt*4/3+3, y, zcnt);
deuce's avatar
deuce committed
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
			free(y);
			for (y=y64; *y; y++) {
				if (*y == '+')
					*y = '-';
				else if (*y == '/')
					*y = '_';
				else if (*y == '=')
					*y = 0;
			}

			obj=JS_NewObject(cx, NULL, NULL, parent);
			JS_DefineProperty(cx, parent, "public_key", OBJECT_TO_JSVAL(obj), NULL, NULL, JSPROP_ENUMERATE|JSPROP_READONLY);
			xstr=JS_NewStringCopyZ(cx, x64);
			free(x64);
			if (xstr != NULL)
				JS_DefineProperty(cx, obj, "x", STRING_TO_JSVAL(xstr), NULL, NULL, JSPROP_ENUMERATE|JSPROP_READONLY);
			ystr=JS_NewStringCopyZ(cx, y64);
			free(y64);
			if (ystr != NULL)
				JS_DefineProperty(cx, obj, "y", STRING_TO_JSVAL(ystr), NULL, NULL, JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DeepFreezeObject(cx, obj);
			return 1;
		}
	}
	return 0;
}

166 167 168 169
static void js_simple_asn1(unsigned char *data, size_t len, JSContext *cx, JSObject *parent)
{
	unsigned char t;
	size_t off=0;
deuce's avatar
deuce committed
170
	size_t off2;
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
	size_t sz;
	char *e;
	char *n;
	char *e64;
	char *n64;
	JSObject *obj;
	JSString* estr;
	JSString* nstr;

	while(off < len) {
		/* Parse identifier */
		t = js_asn1_type(data, len, &off);

		/* Parse length */
		sz = js_asn1_len(data, len, &off);

		switch(t) {
			case 48:
				// Sequence, descend into it.
				break;
			case 6:
				// OID.... check if it's PKCS #1
				if (strncmp((char *)data+off, "\x2a\x86\x48\x86\xF7\x0D\x01\x01\x01", 9)==0) {
					// YEP!
					off += sz;
deuce's avatar
deuce committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209
					for (; off < len;) {
						off2 = off;
						t = js_asn1_type(data, len, &off2);
						if (t == 2)
							break;
						off = off2;
						sz = js_asn1_len(data,len,&off);
						if (t != 48 && t != 3)
							off += sz;
						if (t == 3)
							off++;
					}
					if (off >= len)
						return;
210 211 212 213 214
					if (js_asn1_type(data, len, &off) == 2) {
						sz = js_asn1_len(data,len,&off);
						n = malloc(sz);
						if (n == NULL)
							return;
215 216 217 218
						while(data[off] == 0) {
							off++;
							sz--;
						}
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
						memcpy(n, data+off, sz);
						n64 = malloc(sz*4/3+3);
						if (n64 == NULL) {
							free(n);
							return;
						}
						b64_encode(n64, sz*4/3+3, n, sz);
						free(n);
						for (n=n64; *n; n++) {
							if (*n == '+')
								*n = '-';
							else if (*n == '/')
								*n = '_';
							else if (*n == '=')
								*n = 0;
						}
						off += sz;
						if (js_asn1_type(data, len, &off) != 2) {
							free(n64);
							return;
						}
						sz = js_asn1_len(data,len,&off);
						e = malloc(sz);
						if (e == NULL) {
							free(n64);
							return;
						}
246 247 248 249
						while(data[off] == 0) {
							off++;
							sz--;
						}
250 251 252 253 254 255 256 257
						memcpy(e, data+off, sz);
						e64 = malloc(sz*4/3+3);
						if (e64 == NULL) {
							free(e);
							free(n64);
							return;
						}
						b64_encode(e64, sz*4/3+3, e, sz);
258
						free(e);
259 260 261 262 263 264 265 266 267 268 269
						for (e=e64; *e; e++) {
							if (*e == '+')
								*e = '-';
							else if (*e == '/')
								*e = '_';
							else if (*e == '=')
								*e = 0;
						}
						obj=JS_NewObject(cx, NULL, NULL, parent);
						JS_DefineProperty(cx, parent, "public_key", OBJECT_TO_JSVAL(obj), NULL, NULL, JSPROP_ENUMERATE|JSPROP_READONLY);
						nstr=JS_NewStringCopyZ(cx, n64);
deuce's avatar
deuce committed
270
						free(n64);
271 272 273
						if (nstr != NULL)
							JS_DefineProperty(cx, obj, "n", STRING_TO_JSVAL(nstr), NULL, NULL, JSPROP_ENUMERATE|JSPROP_READONLY);
						estr=JS_NewStringCopyZ(cx, e64);
deuce's avatar
deuce committed
274
						free(e64);
275 276 277 278 279 280
						if (estr != NULL)
							JS_DefineProperty(cx, obj, "e", STRING_TO_JSVAL(estr), NULL, NULL, JSPROP_ENUMERATE|JSPROP_READONLY);
						JS_DeepFreezeObject(cx, obj);
					}
					off = len;
				}
deuce's avatar
deuce committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
				else if (strncmp((char *)data+off, "\x2A\x86\x48\xCE\x3D\x03\x01\x07", 8) == 0) {
					// P-256
					off += sz;
					if (js_ecc_to_prop(data, len, &off, cx, parent))
						off = len;
				}
				else if (strncmp((char *)data+off, "\x2B\x81\x04\x00\x22", 5) == 0) {
					// P-384
					off += sz;
					if (js_ecc_to_prop(data, len, &off, cx, parent))
						off = len;
				}
				else if (strncmp((char *)data+off, "\x2B\x81\x04\x00\x23", 5) == 0) {
					// P-521
					off += sz;
					if (js_ecc_to_prop(data, len, &off, cx, parent))
						off = len;
				}
				if (off < len)
					off += sz;
301 302 303 304 305 306 307 308 309 310
				break;
			default:
				off += sz;
				break;
		}
	}
}

static void js_create_key_object(JSContext *cx, JSObject *parent)
{
deuce's avatar
deuce committed
311
	struct js_cryptcon_private_data* p;
312 313 314 315 316 317 318
	jsrefcount rc;
	int status;
	int val;
	int sz;
	CRYPT_CERTIFICATE cert;	// Just to hold the public key...
	unsigned char *certbuf;

deuce's avatar
deuce committed
319
	if ((p=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,parent))==NULL)
320 321 322 323 324 325 326 327 328
		return;

	rc = JS_SUSPENDREQUEST(cx);

	status = cryptGetAttribute(p->ctx, CRYPT_CTXINFO_ALGO, &val);
	if (status != CRYPT_OK) {
		lprintf(LOG_ERR, "cryptGetAttribute(ALGO) returned %d\n", status);
		goto resume;
	}
deuce's avatar
deuce committed
329
	if (val != CRYPT_ALGO_RSA && val != CRYPT_ALGO_ECDSA)
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
		goto resume;

	status = cryptCreateCert(&cert, CRYPT_UNUSED, CRYPT_CERTTYPE_CERTIFICATE);
	if (status != CRYPT_OK) {
		lprintf(LOG_ERR, "cryptCreateCert() returned %d\n", status);
		goto resume;
	}
	status = cryptSetAttribute(cert, CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO, p->ctx);
	if (status != CRYPT_OK) {
		lprintf(LOG_ERR, "cryptSetAttribute(PKI) returned %d\n", status);
		goto resume;
	}
	status = cryptSetAttribute(cert, CRYPT_CERTINFO_XYZZY, 1);
	if (status != CRYPT_OK) {
		lprintf(LOG_ERR, "cryptSetAttribute(XYZZY) returned %d\n", status);
		goto resume;
	}
	status = cryptSetAttributeString(cert, CRYPT_CERTINFO_COMMONNAME, "Key", 3);
	if (status != CRYPT_OK) {
		lprintf(LOG_ERR, "cryptSetAttributeString(CN) returned %d\n", status);
		goto resume;
	}
	status = cryptSignCert(cert, p->ctx);
	if (status != CRYPT_OK) {
		lprintf(LOG_ERR, "cryptSignCert() returned %d\n", status);
		goto resume;
	}
	status = cryptExportCert(NULL, 0, &sz, CRYPT_CERTFORMAT_CERTIFICATE, cert);
	if (status != CRYPT_OK) {
		lprintf(LOG_ERR, "cryptExportCert(NULL) returned %d\n", status);
		goto resume;
	}
	certbuf = malloc(sz);
	if (certbuf == NULL) {
		lprintf(LOG_ERR, "Unable to allocate %d bytes\n", sz);
		goto resume;
	}
	status = cryptExportCert(certbuf, sz, &sz, CRYPT_CERTFORMAT_CERTIFICATE, cert);
	if (status != CRYPT_OK) {
		lprintf(LOG_ERR, "cryptExportCert(certbuf) returned %d\n", status);
		goto resume;
	}
	cryptDestroyCert(cert);

	js_simple_asn1(certbuf, sz, cx, parent);
	free(certbuf);

resume:
	JS_RESUMEREQUEST(cx, rc);
	return;
}

382 383 384 385 386
// Destructor

static void
js_finalize_cryptcon(JSContext *cx, JSObject *obj)
{
deuce's avatar
deuce committed
387
	struct js_cryptcon_private_data* p;
388

deuce's avatar
deuce committed
389
	if ((p=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,obj))==NULL)
390 391 392 393 394 395 396 397 398 399 400 401 402
		return;

	cryptDestroyContext(p->ctx);
	free(p);

	JS_SetPrivate(cx, obj, NULL);
}

// Methods

static JSBool
js_generate_key(JSContext *cx, uintN argc, jsval *arglist)
{
deuce's avatar
deuce committed
403
	struct js_cryptcon_private_data* p;
404 405 406 407
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsrefcount rc;
	int status;

deuce's avatar
deuce committed
408
	if ((p=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,obj))==NULL) {
409 410 411 412 413
		JS_ReportError(cx, getprivate_failure, WHERE);
		return JS_FALSE;
	}

	rc = JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
414
	status = cryptGenerateKey(p->ctx);
415
	JS_RESUMEREQUEST(cx, rc);
416 417
	if (status == CRYPT_OK)
		js_create_key_object(cx, obj);
418 419 420 421 422 423 424 425 426 427 428
	if (cryptStatusError(status)) {
		js_cryptcon_error(cx, p->ctx, status);
		return JS_FALSE;
	}
	JS_SET_RVAL(cx, arglist, JSVAL_VOID);
	return JS_TRUE;
}

static JSBool
js_set_key(JSContext *cx, uintN argc, jsval *arglist)
{
deuce's avatar
deuce committed
429
	struct js_cryptcon_private_data* p;
430 431 432
	JSObject *obj;
	jsval *argv;
	size_t len;
433
	char* key = NULL;
434 435 436 437 438 439 440 441 442
	int status;
	jsrefcount rc;

	if (!js_argc(cx, argc, 1))
		return JS_FALSE;

	argv = JS_ARGV(cx, arglist);

	JSVALUE_TO_MSTRING(cx, argv[0], key, &len);
443
	HANDLE_PENDING(cx, key);
444 445
	if (key == NULL)
		return JS_FALSE;
446 447

	obj = JS_THIS_OBJECT(cx, arglist);
deuce's avatar
deuce committed
448
	if ((p=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,obj))==NULL) {
449 450 451 452 453 454 455 456 457 458 459 460 461
		free(key);
		JS_ReportError(cx, getprivate_failure, WHERE);
		return JS_FALSE;
	}

	rc = JS_SUSPENDREQUEST(cx);
	status = cryptSetAttributeString(p->ctx, CRYPT_CTXINFO_KEY, key, len);
	free(key);
	JS_RESUMEREQUEST(cx, rc);
	if (cryptStatusError(status)) {
		js_cryptcon_error(cx, p->ctx, status);
		return JS_FALSE;
	}
462
	js_create_key_object(cx, obj);
463 464 465 466 467 468 469 470

	JS_SET_RVAL(cx, arglist, JSVAL_VOID);
	return JS_TRUE;
}

static JSBool
js_derive_key(JSContext *cx, uintN argc, jsval *arglist)
{
deuce's avatar
deuce committed
471
	struct js_cryptcon_private_data* p;
472 473 474
	JSObject *obj;
	jsval *argv;
	size_t len;
475
	char* key = NULL;
476 477 478 479 480 481 482 483 484
	int status;
	jsrefcount rc;

	if (!js_argc(cx, argc, 1))
		return JS_FALSE;

	argv = JS_ARGV(cx, arglist);

	JSVALUE_TO_MSTRING(cx, argv[0], key, &len);
485
	HANDLE_PENDING(cx, key);
486 487
	if (key == NULL)
		return JS_FALSE;
488 489 490 491 492 493 494 495

	if (len < 8 || len > CRYPT_MAX_HASHSIZE) {
		free(key);
		JS_ReportError(cx, "Illegal key value length of %d (must be between 8 and %d)", len, CRYPT_MAX_HASHSIZE);
		return JS_FALSE;
	}

	obj = JS_THIS_OBJECT(cx, arglist);
deuce's avatar
deuce committed
496
	if ((p=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,obj))==NULL) {
497 498 499 500 501 502 503 504 505 506 507 508 509
		free(key);
		JS_ReportError(cx, getprivate_failure, WHERE);
		return JS_FALSE;
	}

	rc = JS_SUSPENDREQUEST(cx);
	status = cryptSetAttributeString(p->ctx, CRYPT_CTXINFO_KEYING_VALUE, key, len);
	free(key);
	JS_RESUMEREQUEST(cx, rc);
	if (cryptStatusError(status)) {
		js_cryptcon_error(cx, p->ctx, status);
		return JS_FALSE;
	}
510
	js_create_key_object(cx, obj);
511 512 513 514 515 516 517 518

	JS_SET_RVAL(cx, arglist, JSVAL_VOID);
	return JS_TRUE;
}

static JSBool
js_do_encrption(JSContext *cx, uintN argc, jsval *arglist, int encrypt)
{
deuce's avatar
deuce committed
519
	struct js_cryptcon_private_data* p;
520 521 522
	JSObject *obj;
	jsval *argv;
	size_t len;
523
	char *cipherText = NULL;
524 525 526 527 528 529 530 531 532 533
	int status;
	jsrefcount rc;
	JSString* str;

	if (!js_argc(cx, argc, 1))
		return JS_FALSE;

	argv = JS_ARGV(cx, arglist);

	obj = JS_THIS_OBJECT(cx, arglist);
deuce's avatar
deuce committed
534
	if ((p=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,obj))==NULL) {
535 536 537 538 539
		JS_ReportError(cx, getprivate_failure, WHERE);
		return JS_FALSE;
	}

	JSVALUE_TO_MSTRING(cx, argv[0], cipherText, &len);
540
	HANDLE_PENDING(cx, cipherText);
541 542
	if (cipherText == NULL)
		return JS_FALSE;
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

	rc = JS_SUSPENDREQUEST(cx);
	if (encrypt)
		status = cryptEncrypt(p->ctx, cipherText, len);
	else
		status = cryptDecrypt(p->ctx, cipherText, len);
	JS_RESUMEREQUEST(cx, rc);
	if (cryptStatusError(status)) {
		free(cipherText);
		js_cryptcon_error(cx, p->ctx, status);
		return JS_FALSE;
	}
	str = JS_NewStringCopyN(cx, cipherText, len);
	free(cipherText);
	if(str==NULL)
		return(JS_FALSE);
	JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(str));
	return JS_TRUE;
}


static JSBool
js_encrypt(JSContext *cx, uintN argc, jsval *arglist)
{
	return js_do_encrption(cx, argc, arglist, TRUE);
}

static JSBool
js_decrypt(JSContext *cx, uintN argc, jsval *arglist)
{
	return js_do_encrption(cx, argc, arglist, FALSE);
}

deuce's avatar
deuce committed
576 577 578
static JSBool
js_create_signature(JSContext *cx, uintN argc, jsval *arglist)
{
deuce's avatar
deuce committed
579 580
	struct js_cryptcon_private_data* p;
	struct js_cryptcon_private_data* scp;
deuce's avatar
deuce committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
	JSObject *sigCtx;
	JSObject *obj;
	jsval *argv;
	int len;
	char *signature = NULL;
	int status;
	jsrefcount rc;
	JSString* str;

	if (!js_argc(cx, argc, 1))
		return JS_FALSE;

	argv = JS_ARGV(cx, arglist);

	obj = JS_THIS_OBJECT(cx, arglist);
deuce's avatar
deuce committed
596
	if ((p=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,obj))==NULL) {
deuce's avatar
deuce committed
597 598 599 600 601 602
		JS_ReportError(cx, getprivate_failure, WHERE);
		return JS_FALSE;
	}
	sigCtx = JSVAL_TO_OBJECT(argv[0]);
	if (!JS_InstanceOf(cx, sigCtx, &js_cryptcon_class, NULL))
		return JS_FALSE;
603
	HANDLE_PENDING(cx, NULL);
deuce's avatar
deuce committed
604
	if ((scp=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,sigCtx))==NULL) {
deuce's avatar
deuce committed
605 606 607 608 609 610 611 612 613 614 615 616 617
		JS_ReportError(cx, getprivate_failure, WHERE);
		return JS_FALSE;
	}

	rc = JS_SUSPENDREQUEST(cx);
	status = cryptCreateSignature(NULL, 0, &len, scp->ctx, p->ctx);
	if (cryptStatusError(status)) {
		JS_RESUMEREQUEST(cx, rc);
		js_cryptcon_error(cx, p->ctx, status);
		return JS_FALSE;
	}
	signature = malloc(len);
	if (signature == NULL) {
618
		lprintf(LOG_ERR, "Unable to allocate %u bytes\n", len);
deuce's avatar
deuce committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
		JS_RESUMEREQUEST(cx, rc);
		return JS_FALSE;
	}
	status = cryptCreateSignature(signature, len, &len, scp->ctx, p->ctx);
	JS_RESUMEREQUEST(cx, rc);
	if (cryptStatusError(status)) {
		free(signature);
		js_cryptcon_error(cx, p->ctx, status);
		return JS_FALSE;
	}
	str = JS_NewStringCopyN(cx, signature, len);
	free(signature);
	if(str==NULL)
		return(JS_FALSE);
	JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(str));
	return JS_TRUE;
}

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
// Properties

enum {
	 CRYPTCON_PROP_ALGO
	,CRYPTCON_PROP_BLOCKSIZE
	,CRYPTCON_PROP_HASHVALUE
	,CRYPTCON_PROP_IV
	,CRYPTCON_PROP_IVSIZE
	,CRYPTCON_PROP_KEYING_ALGO
	,CRYPTCON_PROP_KEYING_ITERATIONS
	,CRYPTCON_PROP_KEYING_SALT
	,CRYPTCON_PROP_KEYSIZE
	,CRYPTCON_PROP_LABEL
	,CRYPTCON_PROP_MODE
	,CRYPTCON_PROP_NAME_ALGO
	,CRYPTCON_PROP_NAME_MODE
};

#ifdef BUILD_JSDOCS
static char* cryptcon_prop_desc[] = {
657
	 "Algorithm constant (CryptContext.ALGO.XXX)"
658 659 660 661 662 663 664 665 666
	,"Cipher block size in bytes"
	,"Output of hasing algorithms (ie: MD5, SHA1, etc)"
	,"Cipher IV"
	,"Cipher IV size in bytes"
	,"The keying algorithm used to derive the key"
	,"The number of iterates used to derive the key"
	,"The salt value used to derive an encryption key from a key (Length must be between 8 and 64)"
	,"Key size in bytes"
	,"Key label"
667
	,"Mode constant (CryptContext.MODE.XXX)"
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
	,"Algorithm name"
	,"Mode name"
	,NULL
};
#endif

static JSBool
js_cryptcon_attr_set(JSContext *cx, jsval *vp, CRYPT_CONTEXT ctx, CRYPT_ATTRIBUTE_TYPE type)
{
	int status;
	int val;

	if (!JS_ValueToInt32(cx, *vp, &val))
		return JS_FALSE;
	status = cryptSetAttribute(ctx, type, val);
	if (cryptStatusError(status)) {
		js_cryptcon_error(cx, ctx, status);
		return JS_FALSE;
	}
	return JS_TRUE;
}

static JSBool
js_cryptcon_attrstr_set(JSContext *cx, jsval *vp, CRYPT_CONTEXT ctx, CRYPT_ATTRIBUTE_TYPE type)
{
	int status;
694
	char *val = NULL;
695 696 697
	size_t len;

	JSVALUE_TO_MSTRING(cx, *vp, val, &len);
698
	HANDLE_PENDING(cx, val);
699 700
	if (val == NULL)
		return JS_FALSE;
701 702 703 704 705 706 707 708 709 710 711 712 713 714

	status = cryptSetAttributeString(ctx, type, val, len);
	if (cryptStatusError(status)) {
		js_cryptcon_error(cx, ctx, status);
		return JS_FALSE;
	}
	return JS_TRUE;
}

static JSBool
js_cryptcon_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)
{
	jsval idval;
    jsint tiny;
deuce's avatar
deuce committed
715
	struct js_cryptcon_private_data* p;
716

deuce's avatar
deuce committed
717
	if ((p=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,obj))==NULL) {
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
		JS_ReportError(cx, getprivate_failure, WHERE);
		return JS_FALSE;
	}

    JS_IdToValue(cx, id, &idval);
    tiny = JSVAL_TO_INT(idval);

	switch (tiny) {
		case CRYPTCON_PROP_ALGO:
			return js_cryptcon_attr_set(cx, vp, p->ctx, CRYPT_CTXINFO_ALGO);
		case CRYPTCON_PROP_BLOCKSIZE:
			return js_cryptcon_attr_set(cx, vp, p->ctx, CRYPT_CTXINFO_BLOCKSIZE);
		case CRYPTCON_PROP_IVSIZE:
			return js_cryptcon_attr_set(cx, vp, p->ctx, CRYPT_CTXINFO_IVSIZE);
		case CRYPTCON_PROP_KEYING_ALGO:
			return js_cryptcon_attr_set(cx, vp, p->ctx, CRYPT_CTXINFO_KEYING_ALGO);
		case CRYPTCON_PROP_KEYING_ITERATIONS:
			return js_cryptcon_attr_set(cx, vp, p->ctx, CRYPT_CTXINFO_KEYING_ITERATIONS);
		case CRYPTCON_PROP_KEYSIZE:
			return js_cryptcon_attr_set(cx, vp, p->ctx, CRYPT_CTXINFO_KEYSIZE);
		case CRYPTCON_PROP_MODE:
			return js_cryptcon_attr_set(cx, vp, p->ctx, CRYPT_CTXINFO_MODE);
		case CRYPTCON_PROP_HASHVALUE:
			return js_cryptcon_attrstr_set(cx, vp, p->ctx, CRYPT_CTXINFO_HASHVALUE);
		case CRYPTCON_PROP_IV:
			return js_cryptcon_attrstr_set(cx, vp, p->ctx, CRYPT_CTXINFO_IV);
		case CRYPTCON_PROP_KEYING_SALT:
			return js_cryptcon_attrstr_set(cx, vp, p->ctx, CRYPT_CTXINFO_KEYING_SALT);
		case CRYPTCON_PROP_LABEL:
			return js_cryptcon_attrstr_set(cx, vp, p->ctx, CRYPT_CTXINFO_LABEL);
		case CRYPTCON_PROP_NAME_ALGO:
			return js_cryptcon_attrstr_set(cx, vp, p->ctx, CRYPT_CTXINFO_NAME_ALGO);
		case CRYPTCON_PROP_NAME_MODE:
			return js_cryptcon_attrstr_set(cx, vp, p->ctx, CRYPT_CTXINFO_NAME_MODE);
	}
	return JS_TRUE;
}

static JSBool
js_cryptcon_attr_get(JSContext *cx, jsval *vp, CRYPT_CONTEXT ctx, CRYPT_ATTRIBUTE_TYPE type)
{
	int status;
	int val;

	status = cryptGetAttribute(ctx, type, &val);
	if (cryptStatusError(status)) {
		//js_cryptcon_error(cx, ctx, status);
		//return JS_FALSE;
		*vp = JSVAL_VOID;
		return JS_TRUE;
	}
	*vp = INT_TO_JSVAL(val);

	return JS_TRUE;
}

static JSBool
js_cryptcon_attrstr_get(JSContext *cx, jsval *vp, CRYPT_CONTEXT ctx, CRYPT_ATTRIBUTE_TYPE type)
{
	int status;
	char *val;
	int len;
	JSString *js_str;

	status = cryptGetAttributeString(ctx, type, NULL, &len);
	if (cryptStatusError(status)) {
		*vp = JSVAL_VOID;
deuce's avatar
deuce committed
785
		return JS_TRUE;	// Do not return JS_FALSE here, or jsdocs build will break.
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
	}
	if ((val = (char *)malloc(len)) == NULL) {
		JS_ReportError(cx, "malloc(%d) failure", len);
		return JS_FALSE;
	}
	status = cryptGetAttributeString(ctx, type, val, &len);
	if (cryptStatusError(status)) {
		js_cryptcon_error(cx, ctx, status);
		return JS_FALSE;
	}
	if((js_str=JS_NewStringCopyN(cx, val, len))==NULL) {
		free(val);
		return(JS_FALSE);
	}
	free(val);
	*vp = STRING_TO_JSVAL(js_str);

	return JS_TRUE;
}

static JSBool
js_cryptcon_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
	jsval idval;
    jsint tiny;
deuce's avatar
deuce committed
811
	struct js_cryptcon_private_data* p;
812

deuce's avatar
deuce committed
813
	if ((p=(struct js_cryptcon_private_data *)JS_GetPrivate(cx,obj))==NULL) {
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
		return JS_TRUE;
	}

    JS_IdToValue(cx, id, &idval);
    tiny = JSVAL_TO_INT(idval);

	switch (tiny) {
		case CRYPTCON_PROP_ALGO:
			return js_cryptcon_attr_get(cx, vp, p->ctx, CRYPT_CTXINFO_ALGO);
		case CRYPTCON_PROP_BLOCKSIZE:
			return js_cryptcon_attr_get(cx, vp, p->ctx, CRYPT_CTXINFO_BLOCKSIZE);
		case CRYPTCON_PROP_IVSIZE:
			return js_cryptcon_attr_get(cx, vp, p->ctx, CRYPT_CTXINFO_IVSIZE);
		case CRYPTCON_PROP_KEYING_ALGO:
			return js_cryptcon_attr_get(cx, vp, p->ctx, CRYPT_CTXINFO_KEYING_ALGO);
		case CRYPTCON_PROP_KEYING_ITERATIONS:
			return js_cryptcon_attr_get(cx, vp, p->ctx, CRYPT_CTXINFO_KEYING_ITERATIONS);
		case CRYPTCON_PROP_KEYSIZE:
			return js_cryptcon_attr_get(cx, vp, p->ctx, CRYPT_CTXINFO_KEYSIZE);
		case CRYPTCON_PROP_MODE:
			return js_cryptcon_attr_get(cx, vp, p->ctx, CRYPT_CTXINFO_MODE);
		case CRYPTCON_PROP_HASHVALUE:
deuce's avatar
deuce committed
836
			cryptEncrypt(p->ctx, p, 0);
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
			return js_cryptcon_attrstr_get(cx, vp, p->ctx, CRYPT_CTXINFO_HASHVALUE);
		case CRYPTCON_PROP_IV:
			return js_cryptcon_attrstr_get(cx, vp, p->ctx, CRYPT_CTXINFO_IV);
		case CRYPTCON_PROP_KEYING_SALT:
			return js_cryptcon_attrstr_get(cx, vp, p->ctx, CRYPT_CTXINFO_KEYING_SALT);
		case CRYPTCON_PROP_LABEL:
			return js_cryptcon_attrstr_get(cx, vp, p->ctx, CRYPT_CTXINFO_LABEL);
		case CRYPTCON_PROP_NAME_ALGO:
			return js_cryptcon_attrstr_get(cx, vp, p->ctx, CRYPT_CTXINFO_NAME_ALGO);
		case CRYPTCON_PROP_NAME_MODE:
			return js_cryptcon_attrstr_get(cx, vp, p->ctx, CRYPT_CTXINFO_NAME_MODE);
	}
	return JS_TRUE;
}

static jsSyncPropertySpec js_cryptcon_properties[] = {
/*	name				,tinyid						,flags,				ver	*/
	{"algo"				,CRYPTCON_PROP_ALGO				,JSPROP_ENUMERATE	,316},
	{"blocksize"		,CRYPTCON_PROP_BLOCKSIZE		,JSPROP_ENUMERATE	,316},
	{"hashvalue"		,CRYPTCON_PROP_HASHVALUE		,JSPROP_ENUMERATE	,316},
	{"iv"				,CRYPTCON_PROP_IV				,JSPROP_ENUMERATE	,316},
	{"ivsize"			,CRYPTCON_PROP_IVSIZE			,JSPROP_ENUMERATE	,316},
	{"keying_algo"		,CRYPTCON_PROP_KEYING_ITERATIONS,JSPROP_ENUMERATE	,316},
	{"keying_iterations",CRYPTCON_PROP_BLOCKSIZE		,JSPROP_ENUMERATE	,316},
	{"keying_salt"		,CRYPTCON_PROP_KEYING_SALT		,JSPROP_ENUMERATE	,316},
	{"keysize"			,CRYPTCON_PROP_KEYSIZE			,JSPROP_ENUMERATE	,316},
	{"label"			,CRYPTCON_PROP_LABEL			,JSPROP_ENUMERATE	,316},
	{"mode"				,CRYPTCON_PROP_MODE				,JSPROP_ENUMERATE	,316},
	{"algo_name"		,CRYPTCON_PROP_NAME_ALGO		,JSPROP_ENUMERATE	,316},
	{"mode_name"		,CRYPTCON_PROP_NAME_MODE		,JSPROP_ENUMERATE	,316},
	{0}
};

static jsSyncMethodSpec js_cryptcon_functions[] = {
	{"generate_key",	js_generate_key,	0,	JSTYPE_VOID,	""
	,JSDOCSTR("Generate a new key for the context.")
	,316
	},
	{"set_key",			js_set_key,			0,	JSTYPE_VOID,	"key_str"
	,JSDOCSTR("Set the key for the context directly.")
	,316
	},
	{"derive_key",		js_derive_key,		0,	JSTYPE_VOID,	"keying_data"
	,JSDOCSTR("Derive the key from the keying data using keying_algo, keying_iterations, and keying_salt.")
	,316
	},
	{"encrypt",			js_encrypt,			0,	JSTYPE_STRING,	"plaintext"
	,JSDOCSTR("Encrypt the string and return as a new string.")
	,316
	},
	{"decrypt",			js_decrypt,			0,	JSTYPE_STRING,	"ciphertext"
	,JSDOCSTR("Decrypt the string and return as a new string.")
	,316
	},
deuce's avatar
deuce committed
891 892 893 894
	{"create_signature",js_create_signature,0,	JSTYPE_STRING,	"sigContext"
	,JSDOCSTR("Create a signature signed with the sigContext CryptContext object.")
	,316
	},
895 896 897 898 899 900 901 902 903 904 905 906 907 908
	{0}
};

static JSBool js_cryptcon_resolve(JSContext *cx, JSObject *obj, jsid id)
{
	char*			name=NULL;
	JSBool			ret;

	if(id != JSID_VOID && id != JSID_EMPTY) {
		jsval idval;
		
		JS_IdToValue(cx, id, &idval);
		if(JSVAL_IS_STRING(idval)) {
			JSSTRING_TO_MSTRING(cx, JSVAL_TO_STRING(idval), name, NULL);
909
			HANDLE_PENDING(cx, name);
910 911 912 913 914 915 916 917 918 919 920 921 922 923
		}
	}

	ret=js_SyncResolve(cx, obj, name, js_cryptcon_properties, js_cryptcon_functions, NULL, 0);
	if(name)
		free(name);
	return ret;
}

static JSBool js_cryptcon_enumerate(JSContext *cx, JSObject *obj)
{
	return(js_cryptcon_resolve(cx, obj, JSID_VOID));
}

deuce's avatar
deuce committed
924
JSClass js_cryptcon_class = {
925 926 927 928 929 930 931 932 933 934 935 936
     "CryptContext"			/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,js_cryptcon_get		/* getProperty	*/
	,js_cryptcon_set		/* setProperty	*/
	,js_cryptcon_enumerate	/* enumerate	*/
	,js_cryptcon_resolve	/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,js_finalize_cryptcon		/* finalize		*/
};

deuce's avatar
deuce committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
JSObject* DLLCALL js_CreateCryptconObject(JSContext* cx, CRYPT_CONTEXT ctx)
{
	JSObject *obj;
	struct js_cryptcon_private_data *p;

	obj=JS_NewObject(cx, &js_cryptcon_class, NULL, NULL);

	if((p=(struct js_cryptcon_private_data *)malloc(sizeof(struct js_cryptcon_private_data)))==NULL) {
		JS_ReportError(cx,"malloc failed");
		return NULL;
	}
	memset(p,0,sizeof(struct js_cryptcon_private_data));
	p->ctx = ctx;

	if(!JS_SetPrivate(cx, obj, p)) {
		JS_ReportError(cx,"JS_SetPrivate failed");
		return NULL;
	}
	js_create_key_object(cx, obj);

	return obj;
}

960 961 962 963 964 965 966
// Constructor

static JSBool
js_cryptcon_constructor(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject *obj;
	jsval *argv=JS_ARGV(cx, arglist);
deuce's avatar
deuce committed
967
	struct js_cryptcon_private_data *p;
968 969 970 971 972 973 974 975 976 977 978 979 980 981
	jsrefcount rc;
	int status;
	int algo;

	do_cryptInit();
	obj=JS_NewObject(cx, &js_cryptcon_class, NULL, NULL);
	JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(obj));
	if(argc != 1) {
		JS_ReportError(cx, "Incorrect number of arguments to CryptContext constructor.  Got %d, expected 1.", argc);
		return JS_FALSE;
	}
	if (!JS_ValueToInt32(cx,argv[0],&algo))
		return JS_FALSE;

deuce's avatar
deuce committed
982
	if((p=(struct js_cryptcon_private_data *)malloc(sizeof(struct js_cryptcon_private_data)))==NULL) {
983 984 985
		JS_ReportError(cx,"malloc failed");
		return(JS_FALSE);
	}
deuce's avatar
deuce committed
986
	memset(p,0,sizeof(struct js_cryptcon_private_data));
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

	if(!JS_SetPrivate(cx, obj, p)) {
		JS_ReportError(cx,"JS_SetPrivate failed");
		return(JS_FALSE);
	}

	rc = JS_SUSPENDREQUEST(cx);
	status = cryptCreateContext(&p->ctx, CRYPT_UNUSED, algo);
	JS_RESUMEREQUEST(cx, rc);
	if (cryptStatusError(status)) {
		js_cryptcon_error(cx, p->ctx, status);
		return JS_FALSE;
	}

#ifdef BUILD_JSDOCS
	js_DescribeSyncObject(cx,obj,"Class used for encryption/decryption",31601);
	js_DescribeSyncConstructor(cx,obj,"To create a new CryptContext object: "
		"var c = new CryptContext('<i>algorithm</i>')</tt><br>"
		"where <i>algorithm</i> is a property of CryptContext.ALGO"
		);
	js_CreateArrayOfStrings(cx, obj, "_property_desc_list", cryptcon_prop_desc, JSPROP_READONLY);
#endif

	return(JS_TRUE);
}

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
#ifdef BUILD_JSDOCS
static char* cryptcon_algo_prop_desc[] = {
	"No encryption",
	"<p>DES is a 64-bit block cipher with a 56-bit key. Note that this algorithm is no longer considered secure and should not be used. It is present in cryptlib only for compatibility with legacy applications.</p>"
	 "<p>Although cryptlib uses 64-bit DES keys, only 56 bits of the key are actually used. The least significant bit in each byte is used as a parity bit (cryptlib will set the correct parity values for you, so you don't have to worry about this). You can treat the algorithm as having a 64-bit key, but bear in mind that only the high 7 bits of each byte are actually used as keying material.</p>"
	 "<p>Loading a key will return a CRYPT_ERROR_PARAM3 error if the key is a weak key. cryptExportKey will export the correct parity-adjusted version of the key.</p>",
	"<p>Triple DES is a 64-bit block cipher with a 112/168-bit key.</p>"
	 "<p>Although cryptlib uses 128, or 192-bit DES keys (depending on whether two- or three-key triple DES is being used), only 112 or 168 bits of the key are actually used. The least significant bit in each byte is used as a parity bit (cryptlib will set the correct parity values for you, so you don't have to worry about this). You can treat the algorithm as having a 128 or 192-bit key, but bear in mind that only the high 7 bits of each byte are actually used as keying material.</p>"
	 "<p>Loading a key will return a CRYPT_ERROR_PARAM3 error if the key is a weak key. cryptExportKey will export the correct parity-adjusted version of the key.</p>",
	"IDEA is a 64-bit block cipher with a 128-bit key. IDEA was formerly covered by patents, but these have now all expired.",
	"CAST-128 is a 64-bit block cipher with a 128-bit key",
	"RC2 (disabled by default, used for PKCS #12)",
	"RC4 is an 8-bit stream cipher with a key of up to 1024 bits. Some weaknesses have been found in this algorithm, and it's proven to be extremely difficult to employ in a safe manner. For this reason it should not be used any more except for legacy application support, and is disabled by default.</p>"
	 "<p>The term \"RC4\" is trademarked in the US. It may be necessary to refer to it as \"an algorithm compatible with RC4\" in products that use RC4 and are distributed in the US. Common practice is to refer to it as ArcFour.</p>",
	"AES is a 128-bit block cipher with a 128-bit key.",
	"<p>Diffie-Hellman is a key-agreement algorithm with a key size of up to 4096 bits.</p>"
	 "<p>Diffie-Hellman was formerly covered by a patent in the US, this has now expired.</p>",
	"<p>RSA is a public-key encryption/digital signature algorithm with a key size of up to 4096 bits.</p>"
	 "<p>RSA was formerly covered by a patent in the US, this has now expired.</p>",
	"<p>DSA is a digital signature algorithm with a key size of up to 1024 bits and has the cryptlib algorithm identifier CRYPT_ALGO_DSA.</p>"
	 "<p>DSA is covered by US patent 5,231,668, with the patent held by the US government. This patent has been made available royalty-free to all users world-wide. The US Department of Commerce is not aware of any other patents that would be infringed by the DSA. US patent 4,995,082, \"Method for identifying subscribers and for generating and verifying electronic signatures in a data exchange system\" (\"the Schnorr patent\") relates to the DSA algorithm but only applies to a very restricted set of smart-card based applications and does not affect the DSA implementation in cryptlib.</p>",
	"<p>Elgamal is a public-key encryption/digital signature algorithm with a key size of up to 4096 bits.</p>"
	 "<p>Elgamal was formerly covered (indirectly) by a patent in the US, this has now expired.</p>",
	"ECDSA is a digital signature algorithm with a key size of up to 521 bits.",
	"ECDH is a key-agreement algorithm with a key size of up to 521 bits.",
	"MD5 (only used for TLS 1.0/1.1)",
	"SHA1 is a message digest/hash algorithm with a digest/hash size of 160 bits. This algorithm has poor long-term security prospects and should be deprecated in favour of SHA-2.",
	"SHA2/SHA256 is a message digest/hash algorithm with a digest/hash size of 256 bits.",
	"Future SHA-nextgen standard",
	"HMAC-SHA1 is a MAC algorithm with a key size of up to 1024 bits.",
	"HMAC-SHA2 is a MAC algorithm with a key size of up to 1024 bits.",
	"HMAC-future-SHA-nextgen",
	NULL
};

static char* cryptcon_mode_prop_desc[] = {
	"No encryption mode",
	"ECB",
	"CBC",
	"CFB",
	"GCM",
	NULL
};
#endif

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
JSObject* DLLCALL js_CreateCryptContextClass(JSContext* cx, JSObject* parent)
{
	JSObject*	ccobj;
	JSObject*	constructor;
	JSObject*	algo;
	JSObject*	mode;
	jsval		val;

	ccobj = JS_InitClass(cx, parent, NULL
		,&js_cryptcon_class
		,js_cryptcon_constructor
		,1	/* number of constructor args */
		,NULL /* props, specified in constructor */
		,NULL /* funcs, specified in constructor */
		,NULL, NULL);

	if(JS_GetProperty(cx, parent, js_cryptcon_class.name, &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToObject(cx,val,&constructor);
		algo = JS_DefineObject(cx, constructor, "ALGO", NULL, NULL, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
		if(algo != NULL) {
			JS_DefineProperty(cx, algo, "None", INT_TO_JSVAL(CRYPT_ALGO_NONE), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "DES", INT_TO_JSVAL(CRYPT_ALGO_DES), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "3DES", INT_TO_JSVAL(CRYPT_ALGO_3DES), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "IDEA", INT_TO_JSVAL(CRYPT_ALGO_IDEA), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
deuce's avatar
deuce committed
1086 1087
			JS_DefineProperty(cx, algo, "CAST", INT_TO_JSVAL(CRYPT_ALGO_CAST), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
1088 1089 1090 1091
			JS_DefineProperty(cx, algo, "RC2", INT_TO_JSVAL(CRYPT_ALGO_RC2), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "RC4", INT_TO_JSVAL(CRYPT_ALGO_RC4), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
1092
			/* CRYPT_ALGO_RC5 no longer supported. */
1093 1094
			JS_DefineProperty(cx, algo, "AES", INT_TO_JSVAL(CRYPT_ALGO_AES), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
1095
			/* CRYPT_ALGO_BLOWFISH no longer supported */
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
			JS_DefineProperty(cx, algo, "DH", INT_TO_JSVAL(CRYPT_ALGO_DH), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "RSA", INT_TO_JSVAL(CRYPT_ALGO_RSA), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "DSA", INT_TO_JSVAL(CRYPT_ALGO_DSA), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "ELGAMAL", INT_TO_JSVAL(CRYPT_ALGO_ELGAMAL), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "ECDSA", INT_TO_JSVAL(CRYPT_ALGO_ECDSA), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "ECDH", INT_TO_JSVAL(CRYPT_ALGO_ECDH), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "MD5", INT_TO_JSVAL(CRYPT_ALGO_MD5), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "SHA1", INT_TO_JSVAL(CRYPT_ALGO_SHA1), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "SHA2", INT_TO_JSVAL(CRYPT_ALGO_SHA2), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
1114
			/* CRYPT_ALGO_RIPEMD160 no longer supported */
1115 1116
			JS_DefineProperty(cx, algo, "SHAng", INT_TO_JSVAL(CRYPT_ALGO_SHAng), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
1117
			/* CRYPT_ALGO_HMAC_MD5 no longer supported */
1118 1119 1120 1121
			JS_DefineProperty(cx, algo, "HMAC-SHA1", INT_TO_JSVAL(CRYPT_ALGO_HMAC_SHA1), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, algo, "HMAC-SHA2", INT_TO_JSVAL(CRYPT_ALGO_HMAC_SHA2), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
1122
			/* CRYPT_ALGO_HMAC_RIPEMD160 no longer supported */
1123 1124
			JS_DefineProperty(cx, algo, "HMAC-SHAng", INT_TO_JSVAL(CRYPT_ALGO_HMAC_SHAng), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
1125 1126 1127 1128
#ifdef BUILD_JSDOCS
			js_CreateArrayOfStrings(cx, algo, "_property_desc_list", cryptcon_algo_prop_desc, JSPROP_READONLY);
			js_DescribeSyncObject(cx, algo, "Associative array of crypto algorithm constants",318);
#endif
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
			JS_DeepFreezeObject(cx, algo);
		}
		mode = JS_DefineObject(cx, constructor, "MODE", NULL, NULL, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
		if(JS_GetProperty(cx, constructor, "MODE", &val) && !JSVAL_NULL_OR_VOID(val)) {
			JS_DefineProperty(cx, mode, "None", INT_TO_JSVAL(CRYPT_MODE_NONE), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, mode, "ECB", INT_TO_JSVAL(CRYPT_MODE_ECB), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, mode, "CBC", INT_TO_JSVAL(CRYPT_MODE_CBC), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, mode, "CFB", INT_TO_JSVAL(CRYPT_MODE_CFB), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
1141
			/* CRYPT_MODE_OFB no longer supported */
1142 1143
			JS_DefineProperty(cx, mode, "GCM", INT_TO_JSVAL(CRYPT_MODE_GCM), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
1144 1145 1146 1147
#ifdef BUILD_JSDOCS
			js_CreateArrayOfStrings(cx, mode, "_property_desc_list", cryptcon_mode_prop_desc, JSPROP_READONLY);
			js_DescribeSyncObject(cx, mode, "Associative array of crypto mode constants",318);
#endif
1148 1149 1150 1151 1152 1153
			JS_DeepFreezeObject(cx, mode);
		}
	}

	return(ccobj);
}