ssh
src/keygen.c@0:c4152c628df5, 2019-11-25 (annotated)
- Committer:
- sPymbed
- Date:
- Mon Nov 25 14:24:05 2019 +0000
- Revision:
- 0:c4152c628df5
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sPymbed | 0:c4152c628df5 | 1 | /* keygen.c |
sPymbed | 0:c4152c628df5 | 2 | * |
sPymbed | 0:c4152c628df5 | 3 | * Copyright (C) 2014-2016 wolfSSL Inc. |
sPymbed | 0:c4152c628df5 | 4 | * |
sPymbed | 0:c4152c628df5 | 5 | * This file is part of wolfSSH. |
sPymbed | 0:c4152c628df5 | 6 | * |
sPymbed | 0:c4152c628df5 | 7 | * wolfSSH is free software; you can redistribute it and/or modify |
sPymbed | 0:c4152c628df5 | 8 | * it under the terms of the GNU General Public License as published by |
sPymbed | 0:c4152c628df5 | 9 | * the Free Software Foundation; either version 3 of the License, or |
sPymbed | 0:c4152c628df5 | 10 | * (at your option) any later version. |
sPymbed | 0:c4152c628df5 | 11 | * |
sPymbed | 0:c4152c628df5 | 12 | * wolfSSH is distributed in the hope that it will be useful, |
sPymbed | 0:c4152c628df5 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
sPymbed | 0:c4152c628df5 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
sPymbed | 0:c4152c628df5 | 15 | * GNU General Public License for more details. |
sPymbed | 0:c4152c628df5 | 16 | * |
sPymbed | 0:c4152c628df5 | 17 | * You should have received a copy of the GNU General Public License |
sPymbed | 0:c4152c628df5 | 18 | * along with wolfSSH. If not, see <http://www.gnu.org/licenses/>. |
sPymbed | 0:c4152c628df5 | 19 | */ |
sPymbed | 0:c4152c628df5 | 20 | |
sPymbed | 0:c4152c628df5 | 21 | |
sPymbed | 0:c4152c628df5 | 22 | /* |
sPymbed | 0:c4152c628df5 | 23 | * The keygen module contains utility functions wrapping the wolfCrypt |
sPymbed | 0:c4152c628df5 | 24 | * key generation functions to product SSH friendly keys. |
sPymbed | 0:c4152c628df5 | 25 | */ |
sPymbed | 0:c4152c628df5 | 26 | |
sPymbed | 0:c4152c628df5 | 27 | |
sPymbed | 0:c4152c628df5 | 28 | #ifdef HAVE_CONFIG_H |
sPymbed | 0:c4152c628df5 | 29 | #include <config.h> |
sPymbed | 0:c4152c628df5 | 30 | #endif |
sPymbed | 0:c4152c628df5 | 31 | |
sPymbed | 0:c4152c628df5 | 32 | #include <wolfssl/options.h> |
sPymbed | 0:c4152c628df5 | 33 | #include <wolfcrypt/random.h> |
sPymbed | 0:c4152c628df5 | 34 | #include <wolfcrypt/rsa.h> |
sPymbed | 0:c4152c628df5 | 35 | #include <wolfssh/error.h> |
sPymbed | 0:c4152c628df5 | 36 | #include <wolfssh/keygen.h> |
sPymbed | 0:c4152c628df5 | 37 | #include <wolfssh/log.h> |
sPymbed | 0:c4152c628df5 | 38 | |
sPymbed | 0:c4152c628df5 | 39 | #ifdef WOLFSSH_KEYGEN |
sPymbed | 0:c4152c628df5 | 40 | |
sPymbed | 0:c4152c628df5 | 41 | #ifdef NO_INLINE |
sPymbed | 0:c4152c628df5 | 42 | #include <wolfssh/misc.h> |
sPymbed | 0:c4152c628df5 | 43 | #else |
sPymbed | 0:c4152c628df5 | 44 | #define WOLFSSH_MISC_INCLUDED |
sPymbed | 0:c4152c628df5 | 45 | #include "src/misc.c" |
sPymbed | 0:c4152c628df5 | 46 | #endif |
sPymbed | 0:c4152c628df5 | 47 | |
sPymbed | 0:c4152c628df5 | 48 | |
sPymbed | 0:c4152c628df5 | 49 | int wolfSSH_MakeRsaKey(byte* out, word32 outSz, |
sPymbed | 0:c4152c628df5 | 50 | word32 size, word32 e) |
sPymbed | 0:c4152c628df5 | 51 | { |
sPymbed | 0:c4152c628df5 | 52 | int ret = WS_SUCCESS; |
sPymbed | 0:c4152c628df5 | 53 | WC_RNG rng; |
sPymbed | 0:c4152c628df5 | 54 | |
sPymbed | 0:c4152c628df5 | 55 | WLOG(WS_LOG_DEBUG, "Entering wolfSSH_MakeRsaKey()"); |
sPymbed | 0:c4152c628df5 | 56 | |
sPymbed | 0:c4152c628df5 | 57 | if (wc_InitRng(&rng) != 0) { |
sPymbed | 0:c4152c628df5 | 58 | WLOG(WS_LOG_DEBUG, "Couldn't create RNG"); |
sPymbed | 0:c4152c628df5 | 59 | ret = WS_CRYPTO_FAILED; |
sPymbed | 0:c4152c628df5 | 60 | } |
sPymbed | 0:c4152c628df5 | 61 | |
sPymbed | 0:c4152c628df5 | 62 | if (ret == WS_SUCCESS) { |
sPymbed | 0:c4152c628df5 | 63 | RsaKey key; |
sPymbed | 0:c4152c628df5 | 64 | |
sPymbed | 0:c4152c628df5 | 65 | if (wc_InitRsaKey(&key, NULL) != 0) |
sPymbed | 0:c4152c628df5 | 66 | ret = WS_CRYPTO_FAILED; |
sPymbed | 0:c4152c628df5 | 67 | |
sPymbed | 0:c4152c628df5 | 68 | if (ret == WS_SUCCESS) { |
sPymbed | 0:c4152c628df5 | 69 | if (wc_MakeRsaKey(&key, size, e, &rng) != 0) { |
sPymbed | 0:c4152c628df5 | 70 | WLOG(WS_LOG_DEBUG, "RSA key generation failed"); |
sPymbed | 0:c4152c628df5 | 71 | ret = WS_CRYPTO_FAILED; |
sPymbed | 0:c4152c628df5 | 72 | } |
sPymbed | 0:c4152c628df5 | 73 | } |
sPymbed | 0:c4152c628df5 | 74 | |
sPymbed | 0:c4152c628df5 | 75 | if (ret == WS_SUCCESS) { |
sPymbed | 0:c4152c628df5 | 76 | int keySz; |
sPymbed | 0:c4152c628df5 | 77 | |
sPymbed | 0:c4152c628df5 | 78 | keySz = wc_RsaKeyToDer(&key, out, outSz); |
sPymbed | 0:c4152c628df5 | 79 | if (keySz < 0) { |
sPymbed | 0:c4152c628df5 | 80 | WLOG(WS_LOG_DEBUG, "RSA key to DER failed"); |
sPymbed | 0:c4152c628df5 | 81 | ret = WS_CRYPTO_FAILED; |
sPymbed | 0:c4152c628df5 | 82 | } |
sPymbed | 0:c4152c628df5 | 83 | else |
sPymbed | 0:c4152c628df5 | 84 | ret = keySz; |
sPymbed | 0:c4152c628df5 | 85 | } |
sPymbed | 0:c4152c628df5 | 86 | |
sPymbed | 0:c4152c628df5 | 87 | if (wc_FreeRsaKey(&key) != 0) { |
sPymbed | 0:c4152c628df5 | 88 | WLOG(WS_LOG_DEBUG, "RSA key free failed"); |
sPymbed | 0:c4152c628df5 | 89 | ret = WS_CRYPTO_FAILED; |
sPymbed | 0:c4152c628df5 | 90 | } |
sPymbed | 0:c4152c628df5 | 91 | |
sPymbed | 0:c4152c628df5 | 92 | if (wc_FreeRng(&rng) != 0) { |
sPymbed | 0:c4152c628df5 | 93 | WLOG(WS_LOG_DEBUG, "Couldn't free RNG"); |
sPymbed | 0:c4152c628df5 | 94 | ret = WS_CRYPTO_FAILED; |
sPymbed | 0:c4152c628df5 | 95 | } |
sPymbed | 0:c4152c628df5 | 96 | } |
sPymbed | 0:c4152c628df5 | 97 | |
sPymbed | 0:c4152c628df5 | 98 | WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_MakeRsaKey(), ret = %d", ret); |
sPymbed | 0:c4152c628df5 | 99 | return ret; |
sPymbed | 0:c4152c628df5 | 100 | } |
sPymbed | 0:c4152c628df5 | 101 | |
sPymbed | 0:c4152c628df5 | 102 | #endif |