Codebase list openssl / effdcf6
Allow DH_set0_key with only private key. The pub_key field for DH isn't actually used in DH_compute_key at all. (Note the peer public key is passed in as as BIGNUM.) It's mostly there so the caller may extract it from DH_generate_key. It doesn't particularly need to be present if filling in a DH from external parameters. The check in DH_set0_key conflicts with adding OpenSSL 1.1.0 to Node. Their public API is a thin wrapper over the old OpenSSL one: https://nodejs.org/api/crypto.html#crypto_class_diffiehellman They have separate setPrivateKey and setPublicKey methods, so the public key may be set last or not at all. In 1.0.2, either worked fine since operations on DH objects generally didn't use the public key. (Like with OpenSSL, Node's setPublicKey method is also largely a no-op, but so it goes.) In 1.1.0, DH_set0_key prevents create a private-key-only DH object. (cherry picked from commit d58ad9a2a287d1c0bc99ba63c997eed88cc161b5) Reviewed-by: Rich Salz <rsalz@openssl.org> Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de> (Merged from https://github.com/openssl/openssl/pull/4425) David Benjamin authored 6 years ago Bernd Edlinger committed 6 years ago
3 changed file(s) with 36 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
229229
230230 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
231231 {
232 /* If the field pub_key in dh is NULL, the corresponding input
233 * parameters MUST be non-NULL. The priv_key field may
234 * be left NULL.
235 */
236 if (dh->pub_key == NULL && pub_key == NULL)
237 return 0;
238
239232 if (pub_key != NULL) {
240233 BN_free(dh->pub_key);
241234 dh->pub_key = pub_key;
4747 be. The values point to the internal representation of the public key and
4848 private key values. This memory should not be freed directly.
4949
50 The public and private key values can be set using DH_set0_key(). The public
51 key must be non-NULL the first time this function is called on a given DH
52 object. The private key may be NULL. On subsequent calls, either may be NULL,
53 which means the corresponding DH field is left untouched. As for DH_set0_pqg()
54 this function transfers the memory management of the key values to the DH
55 object, and therefore they should not be freed directly after this function has
56 been called.
50 The public and private key values can be set using DH_set0_key(). Either
51 parameter may be NULL, which means the corresponding DH field is left
52 untouched. As with DH_set0_pqg() this function transfers the memory management
53 of the key values to the DH object, and therefore they should not be freed
54 directly after this function has been called.
5755
5856 DH_set_flags() sets the flags in the B<flags> parameter on the DH object.
5957 Multiple flags can be passed in one go (bitwise ORed together). Any flags that
3939 BN_GENCB *_cb = NULL;
4040 DH *a = NULL;
4141 DH *b = NULL;
42 DH *c = NULL;
4243 const BIGNUM *ap = NULL, *ag = NULL, *apub_key = NULL, *priv_key = NULL;
4344 const BIGNUM *bpub_key = NULL;
44 BIGNUM *bp = NULL, *bg = NULL;
45 BIGNUM *bp = NULL, *bg = NULL, *cpriv_key = NULL;
4546 char buf[12] = {0};
4647 unsigned char *abuf = NULL;
4748 unsigned char *bbuf = NULL;
48 int i, alen, blen, aout, bout;
49 unsigned char *cbuf = NULL;
50 int i, alen, blen, clen, aout, bout, cout;
4951 int ret = 1;
5052 BIO *out = NULL;
5153
113115 BN_print(out, bpub_key);
114116 BIO_puts(out, "\n");
115117
118 /* Also test with a private-key-only copy of |b|. */
119 if ((c = DHparams_dup(b)) == NULL
120 || (cpriv_key = BN_dup(priv_key)) == NULL
121 || !DH_set0_key(c, NULL, cpriv_key))
122 goto err;
123 cpriv_key = NULL;
124
116125 alen = DH_size(a);
117126 abuf = OPENSSL_malloc(alen);
118127 if (abuf == NULL)
140149 BIO_puts(out, buf);
141150 }
142151 BIO_puts(out, "\n");
143 if ((aout < 4) || (bout != aout) || (memcmp(abuf, bbuf, aout) != 0)) {
152
153 clen = DH_size(c);
154 cbuf = OPENSSL_malloc(clen);
155 if (cbuf == NULL)
156 goto err;
157
158 cout = DH_compute_key(cbuf, apub_key, c);
159
160 BIO_puts(out, "key3 =");
161 for (i = 0; i < cout; i++) {
162 sprintf(buf, "%02X", cbuf[i]);
163 BIO_puts(out, buf);
164 }
165 BIO_puts(out, "\n");
166
167 if ((aout < 4) || (bout != aout) || (memcmp(abuf, bbuf, aout) != 0)
168 || (cout != aout) || (memcmp(abuf, cbuf, aout) != 0)) {
144169 fprintf(stderr, "Error in DH routines\n");
145170 ret = 1;
146171 } else
153178
154179 OPENSSL_free(abuf);
155180 OPENSSL_free(bbuf);
181 OPENSSL_free(cbuf);
156182 DH_free(b);
157183 DH_free(a);
184 DH_free(c);
158185 BN_free(bp);
159186 BN_free(bg);
187 BN_free(cpriv_key);
160188 BN_GENCB_free(_cb);
161189 BIO_free(out);
162190