Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw Errors instead of objects and strings #122

Merged
merged 1 commit into from
Jun 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions js/asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,9 @@ asn1.fromDer = function(bytes, strict) {

// minimum length for ASN.1 DER structure is 2
if(bytes.length() < 2) {
throw {
message: 'Too few bytes to parse DER.',
bytes: bytes.length()
};
var error = new Error('Too few bytes to parse DER.');
error.bytes = bytes.length();
throw error;
}

// get the first byte
Expand All @@ -286,10 +285,9 @@ asn1.fromDer = function(bytes, strict) {
// ensure there are enough bytes to get the value
if(bytes.length() < length) {
if(strict) {
throw {
message: 'Too few bytes to read ASN.1 value.',
detail: bytes.length() + ' < ' + length
};
var error = new Error('Too few bytes to read ASN.1 value.');
error.detail = bytes.length() + ' < ' + length;
throw error;
}
// Note: be lenient with truncated values
length = bytes.length();
Expand Down Expand Up @@ -363,9 +361,7 @@ asn1.fromDer = function(bytes, strict) {

if(length === undefined) {
if(strict) {
throw {
message: 'Non-constructed ASN.1 object of indefinite length.'
};
throw new Error('Non-constructed ASN.1 object of indefinite length.');
}
// be lenient and use remaining bytes
length = bytes.length();
Expand Down Expand Up @@ -777,10 +773,9 @@ asn1.integerToDer = function(x) {
if(x >= -0x80000000 && x < 0x80000000) {
return rval.putSignedInt(x, 32);
}
throw {
message: 'Integer too large; max is 32-bits.',
integer: x
};
var error = new Error('Integer too large; max is 32-bits.');
error.integer = x;
throw error;
};

/**
Expand All @@ -799,7 +794,7 @@ asn1.derToInteger = function(bytes) {

var n = bytes.length() * 8;
if(n > 32) {
throw {message: 'Integer too large; max is 32-bits.'};
throw new Error('Integer too large; max is 32-bits.');
}
return bytes.getSignedInt(n);
};
Expand Down
2 changes: 1 addition & 1 deletion js/hmac.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ hmac.create = function() {
if(md in forge.md.algorithms) {
_md = forge.md.algorithms[md].create();
} else {
throw 'Unknown hash algorithm "' + md + '"';
throw new Error('Unknown hash algorithm "' + md + '"');
}
} else {
// store message digest
Expand Down
43 changes: 19 additions & 24 deletions js/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,9 @@ http.createClient = function(options) {
window.location.protocol + '//' + window.location.host);
var url = http.parseUrl(options.url);
if(!url) {
throw {
message: 'Invalid url.',
details: {url: options.url}
};
var error = new Error('Invalid url.');
error.details = {url: options.url};
throw error;
}

// default to 1 connection
Expand Down Expand Up @@ -681,21 +680,19 @@ http.createClient = function(options) {

// do secure check
if(cookie.secure !== client.secure) {
throw {
message: 'Http client url scheme is incompatible ' +
'with cookie secure flag.',
url: client.url,
cookie: cookie
};
var error = new Error('Http client url scheme is incompatible ' +
'with cookie secure flag.');
error.url = client.url;
error.cookie = cookie;
throw error;
}
// make sure url host is within cookie.domain
if(!http.withinCookieDomain(client.url, cookie)) {
throw {
message: 'Http client url host is incompatible with ' +
'cookie domain.',
url: client.url,
cookie: cookie
};
var error = new Error('Http client url scheme is incompatible ' +
'with cookie secure flag.');
error.url = client.url;
error.cookie = cookie;
throw error;
}

// add new cookie
Expand Down Expand Up @@ -1052,10 +1049,9 @@ http.createResponse = function() {
response.message = tmp.slice(2).join(' ');
} else {
// invalid header
throw {
message: 'Invalid http response header.',
details: {'line': line}
};
var error = new Error('Invalid http response header.');
error.details = {'line': line};
throw error;
}
} else if(line.length === 0) {
// handle final line, end of header
Expand Down Expand Up @@ -1182,10 +1178,9 @@ http.createResponse = function() {
response.body = response.body || '';
_readChunkedBody(b);
} else {
throw {
message: 'Unknown Transfer-Encoding.',
details: {'transferEncoding' : transferEncoding}
};
var error = new Error('Unknown Transfer-Encoding.');
error.details = {'transferEncoding' : transferEncoding};
throw error;
}
} else if((contentLength !== null && contentLength < 0) ||
(contentLength === null &&
Expand Down
133 changes: 58 additions & 75 deletions js/pbe.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,9 @@ pki.encryptPrivateKeyInfo = function(obj, password, options) {
cipherFn = forge.des.createEncryptionCipher;
break;
default:
throw {
message: 'Cannot encrypt private key. Unknown encryption algorithm.',
algorithm: options.algorithm
};
var error = new Error('Cannot encrypt private key. Unknown encryption algorithm.');
error.algorithm = options.algorithm;
throw error;
}

// encrypt private key using pbe SHA-1 and AES/DES
Expand Down Expand Up @@ -298,10 +297,9 @@ pki.encryptPrivateKeyInfo = function(obj, password, options) {
])
]);
} else {
throw {
message: 'Cannot encrypt private key. Unknown encryption algorithm.',
algorithm: options.algorithm
};
var error = new Error('Cannot encrypt private key. Unknown encryption algorithm.');
error.algorithm = options.algorithm;
throw error;
}

// EncryptedPrivateKeyInfo
Expand Down Expand Up @@ -330,11 +328,10 @@ pki.decryptPrivateKeyInfo = function(obj, password) {
var capture = {};
var errors = [];
if(!asn1.validate(obj, encryptedPrivateKeyValidator, capture, errors)) {
throw {
message: 'Cannot read encrypted private key. ' +
'ASN.1 object is not a supported EncryptedPrivateKeyInfo.',
errors: errors
};
var error = new Error('Cannot read encrypted private key. ' +
'ASN.1 object is not a supported EncryptedPrivateKeyInfo.');
error.errors = errors;
throw error;
}

// get cipher
Expand Down Expand Up @@ -381,17 +378,14 @@ pki.encryptedPrivateKeyFromPem = function(pem) {
var msg = forge.pem.decode(pem)[0];

if(msg.type !== 'ENCRYPTED PRIVATE KEY') {
throw {
message: 'Could not convert encrypted private key from PEM; PEM header ' +
'type is "ENCRYPTED PRIVATE KEY".',
headerType: msg.type
};
var error = new Error('Could not convert encrypted private key from PEM; ' +
'PEM header type is "ENCRYPTED PRIVATE KEY".');
error.headerType = msg.type;
throw error;
}
if(msg.procType && msg.procType.type === 'ENCRYPTED') {
throw {
message: 'Could not convert encrypted private key from PEM; ' +
'PEM is encrypted.'
};
throw new Error('Could not convert encrypted private key from PEM; ' +
'PEM is encrypted.');
}

// convert DER to ASN.1 object
Expand Down Expand Up @@ -471,11 +465,10 @@ pki.encryptRsaPrivateKey = function(rsaKey, password, options) {
cipherFn = forge.des.createEncryptionCipher;
break;
default:
throw {
message: 'Could not encrypt RSA private key; unsupported encryption ' +
'algorithm "' + options.algorithm + '".',
algorithm: options.algorithm
};
var error = new Error('Could not encrypt RSA private key; unsupported ' +
'encryption algorithm "' + options.algorithm + '".');
error.algorithm = options.algorithm;
throw error;
}

// encrypt private key using OpenSSL legacy key derivation
Expand Down Expand Up @@ -516,11 +509,10 @@ pki.decryptRsaPrivateKey = function(pem, password) {
if(msg.type !== 'ENCRYPTED PRIVATE KEY' &&
msg.type !== 'PRIVATE KEY' &&
msg.type !== 'RSA PRIVATE KEY') {
throw {
message: 'Could not convert private key from PEM; PEM header type is ' +
'not "ENCRYPTED PRIVATE KEY", "PRIVATE KEY", or "RSA PRIVATE KEY".',
headerType: msg.type
};
var error = new Error('Could not convert private key from PEM; PEM header type ' +
'is not "ENCRYPTED PRIVATE KEY", "PRIVATE KEY", or "RSA PRIVATE KEY".');
error.headerType = error;
throw error;
}

if(msg.procType && msg.procType.type === 'ENCRYPTED') {
Expand Down Expand Up @@ -566,11 +558,10 @@ pki.decryptRsaPrivateKey = function(pem, password) {
};
break;
default:
throw {
message: 'Could not decrypt private key; unsupported encryption ' +
'algorithm "' + msg.dekInfo.algorithm + '".',
algorithm: msg.dekInfo.algorithm
};
var error = new Error('Could not decrypt private key; unsupported ' +
'encryption algorithm "' + msg.dekInfo.algorithm + '".');
error.algorithm = msg.dekInfo.algorithm;
throw error;
}

// use OpenSSL legacy key derivation
Expand Down Expand Up @@ -731,15 +722,14 @@ pki.pbe.getCipher = function(oid, params, password) {
return pki.pbe.getCipherForPKCS12PBE(oid, params, password);

default:
throw {
message: 'Cannot read encrypted PBE data block. Unsupported OID.',
oid: oid,
supportedOids: [
'pkcs5PBES2',
'pbeWithSHAAnd3-KeyTripleDES-CBC',
'pbewithSHAAnd40BitRC2-CBC'
]
};
var error = new Error('Cannot read encrypted PBE data block. Unsupported OID.');
error.oid = oid;
error.supportedOids = [
'pkcs5PBES2',
'pbeWithSHAAnd3-KeyTripleDES-CBC',
'pbewithSHAAnd40BitRC2-CBC'
];
throw error;
}
};

Expand All @@ -760,37 +750,33 @@ pki.pbe.getCipherForPBES2 = function(oid, params, password) {
var capture = {};
var errors = [];
if(!asn1.validate(params, PBES2AlgorithmsValidator, capture, errors)) {
throw {
message: 'Cannot read password-based-encryption algorithm ' +
'parameters. ASN.1 object is not a supported ' +
'EncryptedPrivateKeyInfo.',
errors: errors
};
var error = new Error('Cannot read password-based-encryption algorithm ' +
'parameters. ASN.1 object is not a supported EncryptedPrivateKeyInfo.');
error.errors = errors;
throw error;
}

// check oids
oid = asn1.derToOid(capture.kdfOid);
if(oid !== pki.oids['pkcs5PBKDF2']) {
throw {
message: 'Cannot read encrypted private key. ' +
'Unsupported key derivation function OID.',
oid: oid,
supportedOids: ['pkcs5PBKDF2']
};
var error = new Error('Cannot read encrypted private key. ' +
'Unsupported key derivation function OID.');
error.oid = oid;
error.supportedOids = ['pkcs5PBKDF2'];
throw error;
}
oid = asn1.derToOid(capture.encOid);
if(oid !== pki.oids['aes128-CBC'] &&
oid !== pki.oids['aes192-CBC'] &&
oid !== pki.oids['aes256-CBC'] &&
oid !== pki.oids['des-EDE3-CBC'] &&
oid !== pki.oids['desCBC']) {
throw {
message: 'Cannot read encrypted private key. ' +
'Unsupported encryption scheme OID.',
oid: oid,
supportedOids: [
'aes128-CBC', 'aes192-CBC', 'aes256-CBC', 'des-EDE3-CBC', 'desCBC']
};
var error = new Error('Cannot read encrypted private key. ' +
'Unsupported encryption scheme OID.');
error.oid = oid;
error.supportedOids = [
'aes128-CBC', 'aes192-CBC', 'aes256-CBC', 'des-EDE3-CBC', 'desCBC'];
throw error;
}

// set PBE params
Expand Down Expand Up @@ -847,12 +833,10 @@ pki.pbe.getCipherForPKCS12PBE = function(oid, params, password) {
var capture = {};
var errors = [];
if(!asn1.validate(params, pkcs12PbeParamsValidator, capture, errors)) {
throw {
message: 'Cannot read password-based-encryption algorithm ' +
'parameters. ASN.1 object is not a supported ' +
'EncryptedPrivateKeyInfo.',
errors: errors
};
var error = new Error('Cannot read password-based-encryption algorithm ' +
'parameters. ASN.1 object is not a supported EncryptedPrivateKeyInfo.');
error.errors = errors;
throw error;
}

var salt = forge.util.createBuffer(capture.salt);
Expand All @@ -878,10 +862,9 @@ pki.pbe.getCipherForPKCS12PBE = function(oid, params, password) {
break;

default:
throw {
message: 'Cannot read PKCS #12 PBE data block. Unsupported OID.',
oid: oid
};
var error = new Error('Cannot read PKCS #12 PBE data block. Unsupported OID.');
error.oid = oid;
throw error;
}

var key = pki.pbe.generatePkcs12Key(password, salt, 1, count, dkLen);
Expand Down
4 changes: 1 addition & 3 deletions js/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ forge.pbkdf2 = pkcs5.pbkdf2 = function(p, s, c, dkLen, md) {
/* 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
stop. */
if(dkLen > (0xFFFFFFFF * hLen)) {
throw {
message: 'Derived key is too long.'
};
throw new Error('Derived key is too long.');
}

/* 2. Let len be the number of hLen-octet blocks in the derived key,
Expand Down
Loading