diff --git a/src/pkcs12.cc b/src/pkcs12.cc index 5dc81fa..f5d5173 100644 --- a/src/pkcs12.cc +++ b/src/pkcs12.cc @@ -47,6 +47,73 @@ Handle extract_p12(const Arguments &args) { #endif +Handle build_to_p12(char *p12path, char *inputcert, char* inputpriv, Local &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 extract_from_p12(char *data, char* password) { HandleScope scope; Handle exports(Object::New());