From dcaea3c5b4618c42b3b8e7d538a525b82c6214af Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Fri, 20 May 2016 18:08:26 +0200 Subject: [PATCH] key import/export password handling example Change-Id: I2b6da9307025e41e53dfa208c86c83bfc85ac22c --- examples/CMakeLists.txt | 1 + examples/key_password.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/key_password.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4cf4681..0d6f3e1 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -50,6 +50,7 @@ BUILD_EXAMPLE("yaca-example-encrypt-gcm-ccm" encrypt_aes_gcm_ccm.c) BUILD_EXAMPLE("yaca-example-sign" sign.c) BUILD_EXAMPLE("yaca-example-key-exchange" key_exchange.c) BUILD_EXAMPLE("yaca-example-key-impexp" key_import_export.c) +BUILD_EXAMPLE("yaca-example-key-password" key_password.c) INSTALL(FILES ${COMMON_SOURCES} DESTINATION ${EXAMPLES_DIR}) diff --git a/examples/key_password.c b/examples/key_password.c new file mode 100644 index 0000000..e3e20c2 --- /dev/null +++ b/examples/key_password.c @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Krzysztof Jackiewicz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#include +#include +#include +#include +#include +#include +#include "misc.h" +#include "../src/debug.h" + + +int main(int argc, char* argv[]) +{ + yaca_debug_set_error_cb(debug_func); + + yaca_key_h key = YACA_KEY_NULL; + char *k = NULL; + size_t kl; + int ret; + char *password = NULL; + + ret = yaca_init(); + if (ret != 0) + goto exit; + + ret = yaca_key_gen(&key, YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_1024BIT); + if (ret != 0) + goto exit; + + ret = read_stdin_line("encryption pass: ", &password); + if (ret != 0) + goto exit; + + ret = yaca_key_export(key, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, password, &k, &kl); + if (ret != 0) + goto exit; + + yaca_free(password); + yaca_key_free(key); + password = NULL; + key = YACA_KEY_NULL; + + ret = yaca_key_import(&key, YACA_KEY_TYPE_RSA_PRIV, NULL, k, kl); + if (ret == YACA_ERROR_PASSWORD_INVALID) { + ret = read_stdin_line("decryption pass: ", &password); + if (ret != 0) + goto exit; + + ret = yaca_key_import(&key, YACA_KEY_TYPE_RSA_PRIV, password, k, kl); + if (ret == YACA_ERROR_PASSWORD_INVALID) + printf("invalid password\n"); + + yaca_free(password); + password = NULL; + } + + if (ret != 0) + goto exit; + + yaca_free(k); + k = NULL; + + ret = yaca_key_export(key, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &k, &kl); + if (ret != 0) + goto exit; + + printf("%.*s", (int)kl, k); + +exit: + yaca_free(k); + yaca_free(password); + yaca_key_free(key); + + yaca_exit(); + + return 0; +} -- 2.7.4