Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var sig = pr.get('signature');
pki_data = PaymentProtocol.X509Certificates.decode(pki_data);
pki_data = pki_data.certificate;
ver.should.equal(1);
pki_type.should.equal('x509+sha256');
pki_data.length.should.equal(2);
if (is_browser) {
var type = 'SHA256';
var pem = PaymentProtocol.prototype._DERtoPEM(pki_data[0], 'CERTIFICATE');
var buf = pr.serializeForSig();
var jsrsaSig = new KJUR.crypto.Signature({
alg: type + 'withRSA',
prov: 'cryptojs/jsrsa'
});
var signedCert = pki_data[0];
var der = signedCert.toString('hex');
// var pem = PaymentProtocol.DERtoPEM(der, 'CERTIFICATE');
var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex(der, 'CERTIFICATE');
jsrsaSig.init(pem);
jsrsaSig.updateHex(buf.toString('hex'));
jsrsaSig.verify(sig.toString('hex')).should.equal(true);
} else {
var crypto = require('crypto');
var type = 'SHA256';
var pem = PaymentProtocol.DERtoPEM(pki_data[0], 'CERTIFICATE');
var buf = pr.serializeForSig();
var verifier = crypto.createVerify('RSA-' + type);
PaymentProtocol.prototype.x509Sign = function(key, returnTrust) {
var pki_type = this.get('pki_type');
var pki_data = this.get('pki_data'); // contains one or more x509 certs
pki_data = PaymentProtocol.X509Certificates.decode(pki_data);
pki_data = pki_data.certificate;
var type = pki_type !== 'none' ? pki_type.split('+')[1].toUpperCase() : pki_type;
var buf = this.serializeForSig();
var rsa = new KJUR.RSAKey();
rsa.readPrivateKeyFromPEMString(key.toString());
key = rsa;
var sig;
if (type !== 'none') {
var jsrsaSig = new KJUR.crypto.Signature({
alg: type + 'withRSA',
prov: 'cryptojs/jsrsa'
});
jsrsaSig.init(key);
jsrsaSig.updateHex(buf.toString('hex'));
sig = new Buffer(jsrsaSig.sign(), 'hex');
} else {
sig = '';
}
if (returnTrust) {
var cert = pki_data[pki_data.length - 1];
var der = cert.toString('hex');
verify(params) {
if (params.purpose === 'PASSWORD') {
let data = {purpose: params.purpose, verification_id: params.verification_id, code: params.code, password: params.password};
return this.api.axios.post(`/email_verifications/${params.verification_id}`, data)
}
let pwd = uuidv4().toLowerCase();
let ec = new KJUR.crypto.ECDSA({'curve': 'secp256r1'});
let pub = ec.generateKeyPairHex().ecpubhex;
let priv = KJUR.KEYUTIL.getPEM(ec, 'PKCS8PRV', pwd);
let data = {purpose: params.purpose, verification_id: params.verification_id, code: params.code, username: params.username, password: params.password, session_secret: this.fixed_schema_header + pub};
return this.api.axios.post(`/email_verifications/${params.verification_id}`, data).then((resp) => {
if (resp.error) {
return resp;
}
let data = resp.data;
Cookies.set('sid', pwd, { expires: 365 });
window.localStorage.setItem('token', priv);
window.localStorage.setItem('uid', data.user_id);
window.localStorage.setItem('sid', data.session_id);
window.localStorage.setItem('user', this.base64.encode(JSON.stringify(data)));
return resp;
});
}
verify(params) {
if (params.purpose === 'PASSWORD') {
let data = {purpose: params.purpose, verification_id: params.verification_id, code: params.code, password: params.password};
return this.api.axios.post(`/email_verifications/${params.verification_id}`, data)
}
let pwd = uuid().toLowerCase();
let ec = new KJUR.crypto.ECDSA({'curve': 'secp256r1'});
let pub = ec.generateKeyPairHex().ecpubhex;
let priv = KJUR.KEYUTIL.getPEM(ec, 'PKCS8PRV', pwd);
let data = {purpose: params.purpose, verification_id: params.verification_id, code: params.code, username: params.username, password: params.password, session_secret: this.fixed_schema_header + pub};
return this.api.axios.post(`/email_verifications/${params.verification_id}`, data).then((resp) => {
if (resp.error) {
return resp;
}
let data = resp.data;
Cookies.set('sid', pwd, { expires: 365 });
window.localStorage.setItem('token', priv);
window.localStorage.setItem('uid', data.user_id);
window.localStorage.setItem('sid', data.session_id);
window.localStorage.setItem('user', this.base64.encode(JSON.stringify(data)));
return resp;
});
}
function hashAccessToken(access_token) {
var hash = jsrsasign.crypto.Util.hashString(access_token, "sha256")
var left = hash.substr(0, hash.length / 2);
var left_b64u = jsrsasign.hextob64u(left);
return left_b64u;
}