Skip to content
Open
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
67 changes: 67 additions & 0 deletions src/pkcs12.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,73 @@ Handle<Value> extract_p12(const Arguments &args) {

#endif

Handle<Value> build_to_p12(char *p12path, char *inputcert, char* inputpriv, Local<Array> &inputarraycerts, char* passwd)
{
HandleScope scope;
X509 *cert;
STACK_OF(X509) *arraycert;
PKCS12 *p12;
FILE *fp;
EVP_PKEY *priv;
BIO *bio;


OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
bio = BIO_new(BIO_s_mem());
if (BIO_puts(bio, inputcert) < 0)
{
ThrowException(Exception::Error(String::New("Cannot load input certificate in BIO")));
return scope.Close(exports);
}
cert = PEM_read_bio_X509(bio, NULL, 0, NULL);
if (!cert)
{
BIO_free(bio);
bio = BIO_new(BIO_s_file());

if (!BIO_read_filename(bio, inputcert)) {
ThrowException(Exception::Error(String::New("Cert: File doesn't exist.")));
return scope.Close(exports);
}

// Try reading the bio again with the file in it.
cert = PEM_read_bio_X509(bio, NULL, 0, NULL);

if (cert == NULL) {
ThrowException(Exception::Error(String::New("Unable to parse certificate.")));
return scope.Close(exports);
}
}
BIO_free(bio);
bio = BIO_new(BIO_s_mem());
if (BIO_puts(bio, inputpriv) < 0)
{
ThrowException(Exception::Error(String::New("Cannot load input privatekey in BIO")));
return scope.Close(exports);
}
priv = d2i_PrivateKey_bio(bio, NULL);
if (!priv)
{
BIO_free(bio);
bio = BIO_new(BIO_s_file());

if (!BIO_read_filename(bio, inputpriv)) {
ThrowException(Exception::Error(String::New("Private Key: File doesn't exist.")));
return scope.Close(exports);
}

priv = d2i_PrivateKey_bio(bio, NULL);
if (priv == NULL) {
ThrowException(Exception::Error(String::New("Unable to parse private key.")));
return scope.Close(exports);
}
}

scope.Close(Undefined());
}


Handle<Value> extract_from_p12(char *data, char* password) {
HandleScope scope;
Handle<Object> exports(Object::New());
Expand Down