comparison rsa.c @ 1250:2bb4c662d1c2

more hard tab
author Francois Perrad <francois.perrad@gadz.org>
date Fri, 01 Jan 2016 15:02:09 +0100
parents c6346c63281b
children 750ec4ec4cbe
comparison
equal deleted inserted replaced
1249:c6346c63281b 1250:2bb4c662d1c2
45 * The key will have the same format as buf_put_rsa_key. 45 * The key will have the same format as buf_put_rsa_key.
46 * These should be freed with rsa_key_free. 46 * These should be freed with rsa_key_free.
47 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ 47 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
48 int buf_get_rsa_pub_key(buffer* buf, dropbear_rsa_key *key) { 48 int buf_get_rsa_pub_key(buffer* buf, dropbear_rsa_key *key) {
49 49
50 int ret = DROPBEAR_FAILURE; 50 int ret = DROPBEAR_FAILURE;
51 TRACE(("enter buf_get_rsa_pub_key")) 51 TRACE(("enter buf_get_rsa_pub_key"))
52 dropbear_assert(key != NULL); 52 dropbear_assert(key != NULL);
53 m_mp_alloc_init_multi(&key->e, &key->n, NULL); 53 m_mp_alloc_init_multi(&key->e, &key->n, NULL);
54 key->d = NULL; 54 key->d = NULL;
55 key->p = NULL; 55 key->p = NULL;
58 buf_incrpos(buf, 4+SSH_SIGNKEY_RSA_LEN); /* int + "ssh-rsa" */ 58 buf_incrpos(buf, 4+SSH_SIGNKEY_RSA_LEN); /* int + "ssh-rsa" */
59 59
60 if (buf_getmpint(buf, key->e) == DROPBEAR_FAILURE 60 if (buf_getmpint(buf, key->e) == DROPBEAR_FAILURE
61 || buf_getmpint(buf, key->n) == DROPBEAR_FAILURE) { 61 || buf_getmpint(buf, key->n) == DROPBEAR_FAILURE) {
62 TRACE(("leave buf_get_rsa_pub_key: failure")) 62 TRACE(("leave buf_get_rsa_pub_key: failure"))
63 goto out; 63 goto out;
64 } 64 }
65 65
66 if (mp_count_bits(key->n) < MIN_RSA_KEYLEN) { 66 if (mp_count_bits(key->n) < MIN_RSA_KEYLEN) {
67 dropbear_log(LOG_WARNING, "RSA key too short"); 67 dropbear_log(LOG_WARNING, "RSA key too short");
68 goto out; 68 goto out;
69 } 69 }
70 70
71 TRACE(("leave buf_get_rsa_pub_key: success")) 71 TRACE(("leave buf_get_rsa_pub_key: success"))
72 ret = DROPBEAR_SUCCESS; 72 ret = DROPBEAR_SUCCESS;
73 out: 73 out:
80 80
81 /* Same as buf_get_rsa_pub_key, but reads private bits at the end. 81 /* Same as buf_get_rsa_pub_key, but reads private bits at the end.
82 * Loads a private rsa key from a buffer 82 * Loads a private rsa key from a buffer
83 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ 83 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
84 int buf_get_rsa_priv_key(buffer* buf, dropbear_rsa_key *key) { 84 int buf_get_rsa_priv_key(buffer* buf, dropbear_rsa_key *key) {
85 int ret = DROPBEAR_FAILURE; 85 int ret = DROPBEAR_FAILURE;
86 86
87 TRACE(("enter buf_get_rsa_priv_key")) 87 TRACE(("enter buf_get_rsa_priv_key"))
88 dropbear_assert(key != NULL); 88 dropbear_assert(key != NULL);
89 89
90 if (buf_get_rsa_pub_key(buf, key) == DROPBEAR_FAILURE) { 90 if (buf_get_rsa_pub_key(buf, key) == DROPBEAR_FAILURE) {
97 key->q = NULL; 97 key->q = NULL;
98 98
99 m_mp_alloc_init_multi(&key->d, NULL); 99 m_mp_alloc_init_multi(&key->d, NULL);
100 if (buf_getmpint(buf, key->d) == DROPBEAR_FAILURE) { 100 if (buf_getmpint(buf, key->d) == DROPBEAR_FAILURE) {
101 TRACE(("leave buf_get_rsa_priv_key: d: ret == DROPBEAR_FAILURE")) 101 TRACE(("leave buf_get_rsa_priv_key: d: ret == DROPBEAR_FAILURE"))
102 goto out; 102 goto out;
103 } 103 }
104 104
105 if (buf->pos == buf->len) { 105 if (buf->pos == buf->len) {
106 /* old Dropbear private keys didn't keep p and q, so we will ignore them*/ 106 /* old Dropbear private keys didn't keep p and q, so we will ignore them*/
107 } else { 107 } else {
108 m_mp_alloc_init_multi(&key->p, &key->q, NULL); 108 m_mp_alloc_init_multi(&key->p, &key->q, NULL);
109 109
110 if (buf_getmpint(buf, key->p) == DROPBEAR_FAILURE) { 110 if (buf_getmpint(buf, key->p) == DROPBEAR_FAILURE) {
111 TRACE(("leave buf_get_rsa_priv_key: p: ret == DROPBEAR_FAILURE")) 111 TRACE(("leave buf_get_rsa_priv_key: p: ret == DROPBEAR_FAILURE"))
112 goto out; 112 goto out;
113 } 113 }
114 114
115 if (buf_getmpint(buf, key->q) == DROPBEAR_FAILURE) { 115 if (buf_getmpint(buf, key->q) == DROPBEAR_FAILURE) {
116 TRACE(("leave buf_get_rsa_priv_key: q: ret == DROPBEAR_FAILURE")) 116 TRACE(("leave buf_get_rsa_priv_key: q: ret == DROPBEAR_FAILURE"))
117 goto out; 117 goto out;
118 } 118 }
119 } 119 }
120 120
121 ret = DROPBEAR_SUCCESS; 121 ret = DROPBEAR_SUCCESS;
122 out: 122 out:
123 if (ret == DROPBEAR_FAILURE) { 123 if (ret == DROPBEAR_FAILURE) {
124 m_free(key->d); 124 m_free(key->d);
125 m_free(key->p); 125 m_free(key->p);
126 m_free(key->q); 126 m_free(key->q);
127 } 127 }
128 TRACE(("leave buf_get_rsa_priv_key")) 128 TRACE(("leave buf_get_rsa_priv_key"))
129 return ret; 129 return ret;
130 } 130 }
131 131
132 132
133 /* Clear and free the memory used by a public or private key */ 133 /* Clear and free the memory used by a public or private key */
134 void rsa_key_free(dropbear_rsa_key *key) { 134 void rsa_key_free(dropbear_rsa_key *key) {