<commit_before>var forge = require("node-forge");

var APNKey = require("../../lib/credentials/APNKey");
var APNCertificate = require("../../lib/credentials/APNCertificate");

function apnCredentialsFromPkcs12(p12Data, passphrase) {
	if (!p12Data) {
		return;
	}

	var asn1 = forge.asn1.fromDer(p12Data.toString("binary"), false);
	var pkcs12;
	try {
		pkcs12 = decryptPkcs12FromAsn1(asn1, passphrase);
	}
	catch(e) {
		if (e.message.match("Invalid password")) {
			throw new Error("unable to parse credentials, incorrect passphrase");
		}
		else {
			throw new Error("unable to parse credentials, not a PFX/P12 file");
		}
	}

	var credentials = { "key": null, "certificates": []};
	for(var i in pkcs12.safeContents) {
		var safeContents = pkcs12.safeContents[i];

		for(var j in safeContents.safeBags) {
			var safeBag = safeContents.safeBags[j];

			if(safeBag.type === forge.pki.oids.pkcs8ShroudedKeyBag) {
				if(credentials.key) {
					throw new Error("multiple keys found in PFX/P12 file");
				}
				credentials.key = new APNKey(safeBag.key);
			}
			else if(safeBag.type === forge.pki.oids.certBag) {
				credentials.certificates.push(new APNCertificate(safeBag.cert));
			}
		}
	}

	return credentials;
}

function decryptPkcs12FromAsn1(asn1, passphrase) {
	try {
		return forge.pkcs12.pkcs12FromAsn1(asn1, false, passphrase);
	}
	catch (e) {
		// OpenSSL-exported files need an empty string, if no password was specified 
		// during export.
		if (passphrase) {
			throw e;
		}
		return forge.pkcs12.pkcs12FromAsn1(asn1, false, "");
	}
}

module.exports = apnCredentialsFromPkcs12;<commit_msg>Shouldn't use functions before they're defined.<commit_after>var forge = require("node-forge");

var APNKey = require("../../lib/credentials/APNKey");
var APNCertificate = require("../../lib/credentials/APNCertificate");

function decryptPkcs12FromAsn1(asn1, passphrase) {
	try {
		return forge.pkcs12.pkcs12FromAsn1(asn1, false, passphrase);
	}
	catch (e) {
		// OpenSSL-exported files need an empty string, if no password was specified 
		// during export.
		if (passphrase) {
			throw e;
		}
		return forge.pkcs12.pkcs12FromAsn1(asn1, false, "");
	}
}

function apnCredentialsFromPkcs12(p12Data, passphrase) {
	if (!p12Data) {
		return;
	}

	var asn1 = forge.asn1.fromDer(p12Data.toString("binary"), false);
	var pkcs12;
	try {
		pkcs12 = decryptPkcs12FromAsn1(asn1, passphrase);
	}
	catch(e) {
		if (e.message.match("Invalid password")) {
			throw new Error("unable to parse credentials, incorrect passphrase");
		}
		else {
			throw new Error("unable to parse credentials, not a PFX/P12 file");
		}
	}

	var credentials = { "key": null, "certificates": []};
	for(var i in pkcs12.safeContents) {
		var safeContents = pkcs12.safeContents[i];

		for(var j in safeContents.safeBags) {
			var safeBag = safeContents.safeBags[j];

			if(safeBag.type === forge.pki.oids.pkcs8ShroudedKeyBag) {
				if(credentials.key) {
					throw new Error("multiple keys found in PFX/P12 file");
				}
				credentials.key = new APNKey(safeBag.key);
			}
			else if(safeBag.type === forge.pki.oids.certBag) {
				credentials.certificates.push(new APNCertificate(safeBag.cert));
			}
		}
	}

	return credentials;
}

module.exports = apnCredentialsFromPkcs12;
{'fraction_non_alphanumeric': 0.13726095003084515, 'fraction_numerical': 0.027760641579272053, 'mean_word_length': 4.012364760432766, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4015273', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1203, 'n_words': 244}