Coverage Report

Created: 2026-08-13 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dropbear/src/gensignkey.c
Line
Count
Source
1
#include "includes.h"
2
#include "dbutil.h"
3
#include "buffer.h"
4
#include "ecdsa.h"
5
#include "genrsa.h"
6
#include "gendss.h"
7
#include "gened25519.h"
8
#include "signkey.h"
9
#include "dbrandom.h"
10
11
/* returns 0 on failure */
12
static int get_default_bits(enum signkey_type keytype)
13
0
{
14
0
  switch (keytype) {
15
0
#if DROPBEAR_RSA
16
0
    case DROPBEAR_SIGNKEY_RSA:
17
0
      return DROPBEAR_DEFAULT_RSA_SIZE;
18
0
#endif
19
0
#if DROPBEAR_DSS
20
0
    case DROPBEAR_SIGNKEY_DSS:
21
      /* DSS for SSH only defines 1024 bits */
22
0
      return 1024;
23
0
#endif
24
0
#if DROPBEAR_ECDSA
25
0
    case DROPBEAR_SIGNKEY_ECDSA_KEYGEN:
26
0
      return ECDSA_DEFAULT_SIZE;
27
0
    case DROPBEAR_SIGNKEY_ECDSA_NISTP521:
28
0
      return 521;
29
0
    case DROPBEAR_SIGNKEY_ECDSA_NISTP384:
30
0
      return 384;
31
0
    case DROPBEAR_SIGNKEY_ECDSA_NISTP256:
32
0
      return 256;
33
0
#endif
34
0
#if DROPBEAR_ED25519
35
0
    case DROPBEAR_SIGNKEY_ED25519:
36
0
      return 256;
37
0
#endif
38
0
    default:
39
0
      return 0;
40
0
  }
41
0
}
42
43
0
int signkey_generate_get_bits(enum signkey_type keytype, int bits) {
44
0
  if (bits == 0)
45
0
  {
46
0
    bits = get_default_bits(keytype);
47
0
  }
48
0
  return bits;
49
0
}
50
51
/* if skip_exist is set it will silently return if the key file exists */
52
int signkey_generate(enum signkey_type keytype, int bits, const char* filename, int skip_exist)
53
0
{
54
0
  sign_key * key = NULL;
55
0
  buffer *buf = NULL;
56
0
  char *fn_temp = NULL;
57
0
  int ret = DROPBEAR_FAILURE;
58
0
  bits = signkey_generate_get_bits(keytype, bits);
59
60
  /* now we can generate the key */
61
0
  key = new_sign_key();
62
63
0
  seedrandom();
64
65
0
  switch(keytype) {
66
0
#if DROPBEAR_RSA
67
0
    case DROPBEAR_SIGNKEY_RSA:
68
0
      key->rsakey = gen_rsa_priv_key(bits);
69
0
      break;
70
0
#endif
71
0
#if DROPBEAR_DSS
72
0
    case DROPBEAR_SIGNKEY_DSS:
73
0
      key->dsskey = gen_dss_priv_key(bits);
74
0
      break;
75
0
#endif
76
0
#if DROPBEAR_ECDSA
77
0
    case DROPBEAR_SIGNKEY_ECDSA_KEYGEN:
78
0
    case DROPBEAR_SIGNKEY_ECDSA_NISTP521:
79
0
    case DROPBEAR_SIGNKEY_ECDSA_NISTP384:
80
0
    case DROPBEAR_SIGNKEY_ECDSA_NISTP256:
81
0
      {
82
0
        ecc_key *ecckey = gen_ecdsa_priv_key(bits);
83
0
        keytype = ecdsa_signkey_type(ecckey);
84
0
        *signkey_key_ptr(key, keytype) = ecckey;
85
0
      }
86
0
      break;
87
0
#endif
88
0
#if DROPBEAR_ED25519
89
0
    case DROPBEAR_SIGNKEY_ED25519:
90
0
      key->ed25519key = gen_ed25519_priv_key(bits);
91
0
      break;
92
0
#endif
93
0
    default:
94
0
      dropbear_exit("Internal error");
95
0
  }
96
97
0
  seedrandom();
98
99
0
  buf = buf_new(MAX_PRIVKEY_SIZE); 
100
101
0
  buf_put_priv_key(buf, key, keytype);
102
0
  sign_key_free(key);
103
0
  key = NULL;
104
0
  buf_setpos(buf, 0);
105
106
0
  fn_temp = m_malloc(strlen(filename) + 30);
107
0
  snprintf(fn_temp, strlen(filename)+30, "%s.tmp%d", filename, getpid());
108
0
  ret = buf_writefile(buf, fn_temp, 0);
109
110
0
  if (ret == DROPBEAR_FAILURE) {
111
0
    goto out;
112
0
  }
113
114
0
  if (link(fn_temp, filename) < 0) {
115
    /* If generating keys on connection (skipexist) it's OK to get EEXIST 
116
    - we probably just lost a race with another connection to generate the key */
117
0
    if (!(skip_exist && errno == EEXIST)) {
118
0
      if (errno == EPERM || errno == EACCES || errno == ENOSYS) {
119
        /* Non-atomic fallback when hard-links not allowed or unsupported */
120
0
        buf_setpos(buf, 0);
121
0
        ret = buf_writefile(buf, filename, skip_exist);
122
0
      } else {
123
0
        dropbear_log(LOG_ERR, "Failed moving key file to %s: %s", filename,
124
0
          strerror(errno));
125
0
        ret = DROPBEAR_FAILURE;
126
0
      }
127
128
0
      goto out;
129
0
    }
130
0
  }
131
132
  /* ensure directory update is flushed to disk, otherwise we can end up
133
  with zero-byte hostkey files if the power goes off */
134
0
  fsync_parent_dir(filename);
135
136
0
out:
137
0
  if (buf) {
138
0
    buf_burn_free(buf);
139
0
  }
140
  
141
0
  if (fn_temp) {
142
0
    unlink(fn_temp);
143
0
    m_free(fn_temp);
144
0
  }
145
146
0
  return ret;
147
0
}