Skip to content

Commit eb2a571

Browse files
authored
Merge pull request #168 from rhenium/ky/pkey-check-sanity
[Bug #14087] x509cert, x509crl, x509req, ns_spki: check sanity of public key
2 parents 4cf2074 + 363f40f commit eb2a571

File tree

6 files changed

+38
-28
lines changed

6 files changed

+38
-28
lines changed

ext/openssl/ossl_ns_spki.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,13 @@ static VALUE
208208
ossl_spki_set_public_key(VALUE self, VALUE key)
209209
{
210210
NETSCAPE_SPKI *spki;
211+
EVP_PKEY *pkey;
211212

212213
GetSPKI(self, spki);
213-
if (!NETSCAPE_SPKI_set_pubkey(spki, GetPKeyPtr(key))) { /* NO NEED TO DUP */
214-
ossl_raise(eSPKIError, NULL);
215-
}
216-
214+
pkey = GetPKeyPtr(key);
215+
ossl_pkey_check_public_key(pkey);
216+
if (!NETSCAPE_SPKI_set_pubkey(spki, pkey))
217+
ossl_raise(eSPKIError, "NETSCAPE_SPKI_set_pubkey");
217218
return key;
218219
}
219220

@@ -307,17 +308,20 @@ static VALUE
307308
ossl_spki_verify(VALUE self, VALUE key)
308309
{
309310
NETSCAPE_SPKI *spki;
311+
EVP_PKEY *pkey;
310312

311313
GetSPKI(self, spki);
312-
switch (NETSCAPE_SPKI_verify(spki, GetPKeyPtr(key))) { /* NO NEED TO DUP */
313-
case 0:
314+
pkey = GetPKeyPtr(key);
315+
ossl_pkey_check_public_key(pkey);
316+
switch (NETSCAPE_SPKI_verify(spki, pkey)) {
317+
case 0:
318+
ossl_clear_error();
314319
return Qfalse;
315-
case 1:
320+
case 1:
316321
return Qtrue;
317-
default:
318-
ossl_raise(eSPKIError, NULL);
322+
default:
323+
ossl_raise(eSPKIError, "NETSCAPE_SPKI_verify");
319324
}
320-
return Qnil; /* dummy */
321325
}
322326

323327
/* Document-class: OpenSSL::Netscape::SPKI

ext/openssl/ossl_pkey.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,17 @@ ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self)
163163
return ossl_pkey_new(pkey);
164164
}
165165

166-
static void
167-
pkey_check_public_key(EVP_PKEY *pkey)
166+
void
167+
ossl_pkey_check_public_key(const EVP_PKEY *pkey)
168168
{
169169
void *ptr;
170170
const BIGNUM *n, *e, *pubkey;
171171

172172
if (EVP_PKEY_missing_parameters(pkey))
173173
ossl_raise(ePKeyError, "parameters missing");
174174

175-
ptr = EVP_PKEY_get0(pkey);
175+
/* OpenSSL < 1.1.0 takes non-const pointer */
176+
ptr = EVP_PKEY_get0((EVP_PKEY *)pkey);
176177
switch (EVP_PKEY_base_id(pkey)) {
177178
case EVP_PKEY_RSA:
178179
RSA_get0_key(ptr, &n, &e, NULL);
@@ -352,7 +353,7 @@ ossl_pkey_verify(VALUE self, VALUE digest, VALUE sig, VALUE data)
352353
int siglen, result;
353354

354355
GetPKey(self, pkey);
355-
pkey_check_public_key(pkey);
356+
ossl_pkey_check_public_key(pkey);
356357
md = GetDigestPtr(digest);
357358
StringValue(sig);
358359
siglen = RSTRING_LENINT(sig);

ext/openssl/ossl_pkey.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
4848
void ossl_generate_cb_stop(void *ptr);
4949

5050
VALUE ossl_pkey_new(EVP_PKEY *);
51+
void ossl_pkey_check_public_key(const EVP_PKEY *);
5152
EVP_PKEY *GetPKeyPtr(VALUE);
5253
EVP_PKEY *DupPKeyPtr(VALUE);
5354
EVP_PKEY *GetPrivPKeyPtr(VALUE);

ext/openssl/ossl_x509cert.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,18 +546,19 @@ ossl_x509_get_public_key(VALUE self)
546546

547547
/*
548548
* call-seq:
549-
* cert.public_key = key => key
549+
* cert.public_key = key
550550
*/
551551
static VALUE
552552
ossl_x509_set_public_key(VALUE self, VALUE key)
553553
{
554554
X509 *x509;
555+
EVP_PKEY *pkey;
555556

556557
GetX509(self, x509);
557-
if (!X509_set_pubkey(x509, GetPKeyPtr(key))) { /* DUPs pkey */
558-
ossl_raise(eX509CertError, NULL);
559-
}
560-
558+
pkey = GetPKeyPtr(key);
559+
ossl_pkey_check_public_key(pkey);
560+
if (!X509_set_pubkey(x509, pkey))
561+
ossl_raise(eX509CertError, "X509_set_pubkey");
561562
return key;
562563
}
563564

@@ -594,9 +595,9 @@ ossl_x509_verify(VALUE self, VALUE key)
594595
X509 *x509;
595596
EVP_PKEY *pkey;
596597

597-
pkey = GetPKeyPtr(key); /* NO NEED TO DUP */
598598
GetX509(self, x509);
599-
599+
pkey = GetPKeyPtr(key);
600+
ossl_pkey_check_public_key(pkey);
600601
switch (X509_verify(x509, pkey)) {
601602
case 1:
602603
return Qtrue;

ext/openssl/ossl_x509crl.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,12 @@ static VALUE
366366
ossl_x509crl_verify(VALUE self, VALUE key)
367367
{
368368
X509_CRL *crl;
369+
EVP_PKEY *pkey;
369370

370371
GetX509CRL(self, crl);
371-
switch (X509_CRL_verify(crl, GetPKeyPtr(key))) {
372+
pkey = GetPKeyPtr(key);
373+
ossl_pkey_check_public_key(pkey);
374+
switch (X509_CRL_verify(crl, pkey)) {
372375
case 1:
373376
return Qtrue;
374377
case 0:

ext/openssl/ossl_x509req.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,10 @@ ossl_x509req_set_public_key(VALUE self, VALUE key)
330330
EVP_PKEY *pkey;
331331

332332
GetX509Req(self, req);
333-
pkey = GetPKeyPtr(key); /* NO NEED TO DUP */
334-
if (!X509_REQ_set_pubkey(req, pkey)) {
335-
ossl_raise(eX509ReqError, NULL);
336-
}
337-
333+
pkey = GetPKeyPtr(key);
334+
ossl_pkey_check_public_key(pkey);
335+
if (!X509_REQ_set_pubkey(req, pkey))
336+
ossl_raise(eX509ReqError, "X509_REQ_set_pubkey");
338337
return key;
339338
}
340339

@@ -365,7 +364,8 @@ ossl_x509req_verify(VALUE self, VALUE key)
365364
EVP_PKEY *pkey;
366365

367366
GetX509Req(self, req);
368-
pkey = GetPKeyPtr(key); /* NO NEED TO DUP */
367+
pkey = GetPKeyPtr(key);
368+
ossl_pkey_check_public_key(pkey);
369369
switch (X509_REQ_verify(req, pkey)) {
370370
case 1:
371371
return Qtrue;

0 commit comments

Comments
 (0)