|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <string.h>
|
|
Packit Service |
084de1 |
#include <openssl/pem.h> /* PEM_def_callback() */
|
|
Packit Service |
084de1 |
#include "internal/thread_once.h"
|
|
Packit Service |
084de1 |
#include "ui_local.h"
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifndef BUFSIZ
|
|
Packit Service |
084de1 |
#define BUFSIZ 256
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt,
|
|
Packit Service |
084de1 |
int verify)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
char buff[BUFSIZ];
|
|
Packit Service |
084de1 |
int ret;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret =
|
|
Packit Service |
084de1 |
UI_UTIL_read_pw(buf, buff, (length > BUFSIZ) ? BUFSIZ : length,
|
|
Packit Service |
084de1 |
prompt, verify);
|
|
Packit Service |
084de1 |
OPENSSL_cleanse(buff, BUFSIZ);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt,
|
|
Packit Service |
084de1 |
int verify)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int ok = 0;
|
|
Packit Service |
084de1 |
UI *ui;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (size < 1)
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ui = UI_new();
|
|
Packit Service |
084de1 |
if (ui != NULL) {
|
|
Packit Service |
084de1 |
ok = UI_add_input_string(ui, prompt, 0, buf, 0, size - 1);
|
|
Packit Service |
084de1 |
if (ok >= 0 && verify)
|
|
Packit Service |
084de1 |
ok = UI_add_verify_string(ui, prompt, 0, buff, 0, size - 1, buf);
|
|
Packit Service |
084de1 |
if (ok >= 0)
|
|
Packit Service |
084de1 |
ok = UI_process(ui);
|
|
Packit Service |
084de1 |
UI_free(ui);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (ok > 0)
|
|
Packit Service |
084de1 |
ok = 0;
|
|
Packit Service |
084de1 |
return ok;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Wrapper around pem_password_cb, a method to help older APIs use newer
|
|
Packit Service |
084de1 |
* ones.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
struct pem_password_cb_data {
|
|
Packit Service |
084de1 |
pem_password_cb *cb;
|
|
Packit Service |
084de1 |
int rwflag;
|
|
Packit Service |
084de1 |
};
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static void ui_new_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
|
|
Packit Service |
084de1 |
int idx, long argl, void *argp)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Do nothing, the data is allocated externally and assigned later with
|
|
Packit Service |
084de1 |
* CRYPTO_set_ex_data()
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int ui_dup_method_data(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
|
|
Packit Service |
084de1 |
void *from_d, int idx, long argl, void *argp)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
void **pptr = (void **)from_d;
|
|
Packit Service |
084de1 |
if (*pptr != NULL)
|
|
Packit Service |
084de1 |
*pptr = OPENSSL_memdup(*pptr, sizeof(struct pem_password_cb_data));
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static void ui_free_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
|
|
Packit Service |
084de1 |
int idx, long argl, void *argp)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
OPENSSL_free(ptr);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static CRYPTO_ONCE get_index_once = CRYPTO_ONCE_STATIC_INIT;
|
|
Packit Service |
084de1 |
static int ui_method_data_index = -1;
|
|
Packit Service |
084de1 |
DEFINE_RUN_ONCE_STATIC(ui_method_data_index_init)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
ui_method_data_index = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD,
|
|
Packit Service |
084de1 |
0, NULL, ui_new_method_data,
|
|
Packit Service |
084de1 |
ui_dup_method_data,
|
|
Packit Service |
084de1 |
ui_free_method_data);
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int ui_open(UI *ui)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
static int ui_read(UI *ui, UI_STRING *uis)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
switch (UI_get_string_type(uis)) {
|
|
Packit Service |
084de1 |
case UIT_PROMPT:
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
char result[PEM_BUFSIZE + 1];
|
|
Packit Service |
084de1 |
const struct pem_password_cb_data *data =
|
|
Packit Service |
084de1 |
UI_method_get_ex_data(UI_get_method(ui), ui_method_data_index);
|
|
Packit Service |
084de1 |
int maxsize = UI_get_result_maxsize(uis);
|
|
Packit Service |
084de1 |
int len = data->cb(result,
|
|
Packit Service |
084de1 |
maxsize > PEM_BUFSIZE ? PEM_BUFSIZE : maxsize,
|
|
Packit Service |
084de1 |
data->rwflag, UI_get0_user_data(ui));
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (len >= 0)
|
|
Packit Service |
084de1 |
result[len] = '\0';
|
|
Packit Service |
084de1 |
if (len <= 0)
|
|
Packit Service |
084de1 |
return len;
|
|
Packit Service |
084de1 |
if (UI_set_result_ex(ui, uis, result, len) >= 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
case UIT_VERIFY:
|
|
Packit Service |
084de1 |
case UIT_NONE:
|
|
Packit Service |
084de1 |
case UIT_BOOLEAN:
|
|
Packit Service |
084de1 |
case UIT_INFO:
|
|
Packit Service |
084de1 |
case UIT_ERROR:
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
static int ui_write(UI *ui, UI_STRING *uis)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
static int ui_close(UI *ui)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
UI_METHOD *UI_UTIL_wrap_read_pem_callback(pem_password_cb *cb, int rwflag)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
struct pem_password_cb_data *data = NULL;
|
|
Packit Service |
084de1 |
UI_METHOD *ui_method = NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL
|
|
Packit Service |
084de1 |
|| (ui_method = UI_create_method("PEM password callback wrapper")) == NULL
|
|
Packit Service |
084de1 |
|| UI_method_set_opener(ui_method, ui_open) < 0
|
|
Packit Service |
084de1 |
|| UI_method_set_reader(ui_method, ui_read) < 0
|
|
Packit Service |
084de1 |
|| UI_method_set_writer(ui_method, ui_write) < 0
|
|
Packit Service |
084de1 |
|| UI_method_set_closer(ui_method, ui_close) < 0
|
|
Packit Service |
084de1 |
|| !RUN_ONCE(&get_index_once, ui_method_data_index_init)
|
|
Packit Service |
084de1 |
|| UI_method_set_ex_data(ui_method, ui_method_data_index, data) < 0) {
|
|
Packit Service |
084de1 |
UI_destroy_method(ui_method);
|
|
Packit Service |
084de1 |
OPENSSL_free(data);
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
data->rwflag = rwflag;
|
|
Packit Service |
084de1 |
data->cb = cb != NULL ? cb : PEM_def_callback;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return ui_method;
|
|
Packit Service |
084de1 |
}
|