comparison rsa.c @ 1409:c721e8c42d2a

add m_mp_free_multi, be more careful freeing when failing to load keys
author Matt Johnston <matt@ucc.asn.au>
date Fri, 26 May 2017 21:08:43 +0800
parents 553c6bb80265
children 798854f62430 06d52bcb8094
comparison
equal deleted inserted replaced
1407:8d9bad0a1c86 1409:c721e8c42d2a
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:
74 if (ret == DROPBEAR_FAILURE) { 74 if (ret == DROPBEAR_FAILURE) {
75 m_free(key->e); 75 m_mp_free_multi(&key->e, &key->n, NULL);
76 m_free(key->n);
77 } 76 }
78 return ret; 77 return ret;
79 } 78 }
80 79
81 /* Same as buf_get_rsa_pub_key, but reads private bits at the end. 80 /* Same as buf_get_rsa_pub_key, but reads private bits at the end.
119 } 118 }
120 119
121 ret = DROPBEAR_SUCCESS; 120 ret = DROPBEAR_SUCCESS;
122 out: 121 out:
123 if (ret == DROPBEAR_FAILURE) { 122 if (ret == DROPBEAR_FAILURE) {
124 m_free(key->d); 123 m_mp_free_multi(&key->d, &key->p, &key->q, NULL);
125 m_free(key->p);
126 m_free(key->q);
127 } 124 }
128 TRACE(("leave buf_get_rsa_priv_key")) 125 TRACE(("leave buf_get_rsa_priv_key"))
129 return ret; 126 return ret;
130 } 127 }
131 128
137 134
138 if (key == NULL) { 135 if (key == NULL) {
139 TRACE2(("leave rsa_key_free: key == NULL")) 136 TRACE2(("leave rsa_key_free: key == NULL"))
140 return; 137 return;
141 } 138 }
142 if (key->d) { 139 m_mp_free_multi(&key->d, &key->e, &key->p, &key->q, &key->n, NULL);
143 mp_clear(key->d);
144 m_free(key->d);
145 }
146 if (key->e) {
147 mp_clear(key->e);
148 m_free(key->e);
149 }
150 if (key->n) {
151 mp_clear(key->n);
152 m_free(key->n);
153 }
154 if (key->p) {
155 mp_clear(key->p);
156 m_free(key->p);
157 }
158 if (key->q) {
159 mp_clear(key->q);
160 m_free(key->q);
161 }
162 m_free(key); 140 m_free(key);
163 TRACE2(("leave rsa_key_free")) 141 TRACE2(("leave rsa_key_free"))
164 } 142 }
165 143
166 /* Put the public rsa key into the buffer in the required format: 144 /* Put the public rsa key into the buffer in the required format: