diff options
author | djm@openbsd.org <djm@openbsd.org> | 2022-10-28 02:39:29 +0200 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2022-10-28 03:46:58 +0200 |
commit | 262647c2e920492ca57f1b9320d74f4a0f6e482b (patch) | |
tree | 1ce89f627b8230d5ce9611ebe698e5b3f4338fa1 /ssh-rsa.c | |
parent | upstream: refactor and simplify sshkey_read() (diff) | |
download | openssh-262647c2e920492ca57f1b9320d74f4a0f6e482b.tar.xz openssh-262647c2e920492ca57f1b9320d74f4a0f6e482b.zip |
upstream: factor out key generation
feedback/ok markus@
OpenBSD-Commit-ID: 5b4211bff4de8d9adb84bc72857a8c42c44e7ceb
Diffstat (limited to 'ssh-rsa.c')
-rw-r--r-- | ssh-rsa.c | 31 |
1 files changed, 30 insertions, 1 deletions
@@ -1,4 +1,4 @@ -/* $OpenBSD: ssh-rsa.c,v 1.71 2022/10/28 00:37:24 djm Exp $ */ +/* $OpenBSD: ssh-rsa.c,v 1.72 2022/10/28 00:39:29 djm Exp $ */ /* * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org> * @@ -104,6 +104,34 @@ ssh_rsa_serialize_public(const struct sshkey *key, struct sshbuf *b, return 0; } +static int +ssh_rsa_generate(struct sshkey *k, int bits) +{ + RSA *private = NULL; + BIGNUM *f4 = NULL; + int ret = SSH_ERR_INTERNAL_ERROR; + + if (bits < SSH_RSA_MINIMUM_MODULUS_SIZE || + bits > SSHBUF_MAX_BIGNUM * 8) + return SSH_ERR_KEY_LENGTH; + if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) { + ret = SSH_ERR_ALLOC_FAIL; + goto out; + } + if (!BN_set_word(f4, RSA_F4) || + !RSA_generate_key_ex(private, bits, f4, NULL)) { + ret = SSH_ERR_LIBCRYPTO_ERROR; + goto out; + } + k->rsa = private; + private = NULL; + ret = 0; + out: + RSA_free(private); + BN_free(f4); + return ret; +} + static const char * rsa_hash_alg_ident(int hash_alg) { @@ -518,6 +546,7 @@ static const struct sshkey_impl_funcs sshkey_rsa_funcs = { /* .cleanup = */ ssh_rsa_cleanup, /* .equal = */ ssh_rsa_equal, /* .ssh_serialize_public = */ ssh_rsa_serialize_public, + /* .generate = */ ssh_rsa_generate, }; const struct sshkey_impl sshkey_rsa_impl = { |