if (!cc)
return EINVAL;
- if(!cert || !key) {
- err = EINVAL;
- set_error(glb_cc, err, posix_error, "invalid parameter value"
+ if(!cert) {
+ set_error(glb_cc, EINVAL, posix_error, "invalid parameter value"
" (canl_set_ctx_own_cert)");
return err;
}
- do_set_ctx_own_cert(glb_cc, cert, chain, key);
-
+ err = do_set_ctx_own_cert(glb_cc, cert, chain, key);
if(err) {
update_error(glb_cc, "can't set cert or key to context"
" (canl_set_ctx_own_cert)");
}
return err;
}
+
+//TODO callback and userdata process
+int canl_set_ctx_own_cert_file(canl_ctx cc, char *cert, char *key,
+ canl_password_callback cb, void *userdata)
+{
+ glb_ctx *glb_cc = (glb_ctx*) cc;
+ int err = 0;
+
+ if (!cc)
+ return EINVAL;
+ if(!cert ) {
+ set_error(glb_cc, EINVAL, posix_error, "invalid parameter value"
+ " (canl_set_ctx_own_cert_file)");
+ return EINVAL;
+ }
+
+ err = do_set_ctx_own_cert_file(glb_cc, cert, key);
+ if(err) {
+ update_error(glb_cc, "can't set cert or key to context"
+ " (canl_set_ctx_own_cert_file)");
+ }
+ return err;
+}
#include "canl_locl.h"
static int set_cert(glb_ctx *cc, X509 *cert);
+static int set_key_file(glb_ctx *cc, char *key);
+static int set_cert_file(glb_ctx *cc, char *cert);
//TODO just stub
int do_set_ctx_own_cert(glb_ctx *cc, canl_x509 cert, canl_stack_of_x509 chain,
return err;
}
-//int authn_set_ctx_own_cert_file(auth_ctx ac, char *cert, char *key, authn_password_callback cb, void *userdata);
+//TODO cert
+int do_set_ctx_own_cert_file(glb_ctx *cc, char *cert, char *key)
+{
+ /* otherwise the private key is in cert file*/
+ if (key)
+ set_key_file(cc, key);
+
+ return 0;
+}
+
+static int set_key_file(glb_ctx *cc, char *key)
+{
+ int err = 0;
+ FILE * key_file = NULL;
+
+ if (!cc->cert_key){
+ cc->cert_key = (cert_key_store *) calloc(1, sizeof(*(cc->cert_key)));
+ if (!cc->cert_key) {
+ err = ENOMEM;
+ set_error(cc, err, posix_error, "not enought memory for the"
+ " certificate storage (set_key_file)");
+ return ENOMEM;
+ }
+ }
+
+ if (cc->cert_key->key) {
+ EVP_PKEY_free(cc->cert_key->key);
+ cc->cert_key->key = NULL;
+ }
+/* cc->cert_key->key = EVP_PKEY_new(void);
+ if (!cc->cert_key->key) {
+ err = ERR_get_error();
+ set_error(cc, err, ssl_error, "not enough memory for"
+ " key storage (set_key_file)");
+ return err;
+ }
+*/
+ key_file = fopen(key, "rb");
+ if (!key_file) {
+ err = errno;
+ set_error(cc, err, posix_error, "cannot open file with key"
+ " (set_key_file)");
+ return err;
+ }
+ /*TODO NULL NULL, callback and user data*/
+ cc->cert_key->key = PEM_read_PrivateKey(key_file, NULL, NULL, NULL);
+ if (!cc->cert_key->key) {
+ err = ERR_get_error();
+ set_error(cc, err, ssl_error, "error while writing key to context"
+ " (set_key_file)");
+ goto end;
+ }
+
+end:
+ fclose(key_file);
+ return err;
+}
+
+static int set_cert_file(glb_ctx *cc, char *cert)
+{
+ int err = 0;
+ FILE * cert_file = NULL;
+
+ if (!cc->cert_key){
+ cc->cert_key = (cert_key_store *) calloc(1, sizeof(*(cc->cert_key)));
+ if (!cc->cert_key) {
+ err = ENOMEM;
+ set_error(cc, err, posix_error, "not enought memory for the"
+ " certificate storage (set_cert_file)");
+ return ENOMEM;
+ }
+ }
+
+ if (cc->cert_key->cert) {
+ X509_free(cc->cert_key->cert);
+ cc->cert_key->cert = NULL;
+ }
+/* cc->cert_key->cert = EVP_PKEY_new(void);
+ if (!cc->cert_key->cert) {
+ err = ERR_get_error();
+ set_error(cc, err, ssl_error, "not enough memory for"
+ " key storage (set_key_file)");
+ return err;
+ }
+*/
+ cert_file = fopen(cert, "rb");
+ if (!cert_file) {
+ err = errno;
+ set_error(cc, err, posix_error, "cannot open file with cert"
+ " (set_key_file)");
+ return err;
+ }
+ /*TODO NULL NULL, callback and user data*/
+ cc->cert_key->cert = PEM_read_X509(cert_file, NULL, NULL, NULL);
+ if (!cc->cert_key->cert) {
+ err = ERR_get_error();
+ set_error(cc, err, ssl_error, "error while writing certificate"
+ " to context (set_key_file)");
+ goto end;
+ }
+
+end:
+ fclose(cert_file);
+ return err;
+}