Skip to content

Commit 3320372

Browse files
davidbensamuel40791765
authored andcommitted
Use X509_get0_pubkey to simplify things slightly
Also X509_REQ_check_private_key didn't handle unknown key type case. Clean those up and align with X509_check_private_key. Change-Id: I7b16f85662943e4b226221a01e1092cf62afc643 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/65051 Reviewed-by: Bob Beck <[email protected]> Commit-Queue: David Benjamin <[email protected]> (cherry picked from commit c1c7f0929f18fabc74004242d3e4ce03f54511d0)
1 parent 0917e72 commit 3320372

File tree

6 files changed

+61
-84
lines changed

6 files changed

+61
-84
lines changed

crypto/x509/t_req.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ int X509_REQ_print_fp(FILE *fp, X509_REQ *x) {
8080
int X509_REQ_print_ex(BIO *bio, X509_REQ *x, unsigned long nmflags,
8181
unsigned long cflag) {
8282
long l;
83-
EVP_PKEY *pkey;
8483
STACK_OF(X509_ATTRIBUTE) *sk;
8584
char mlch = ' ';
8685

@@ -127,13 +126,12 @@ int X509_REQ_print_ex(BIO *bio, X509_REQ *x, unsigned long nmflags,
127126
goto err;
128127
}
129128

130-
pkey = X509_REQ_get_pubkey(x);
129+
const EVP_PKEY *pkey = X509_REQ_get0_pubkey(x);
131130
if (pkey == NULL) {
132131
BIO_printf(bio, "%12sUnable to load Public Key\n", "");
133132
ERR_print_errors(bio);
134133
} else {
135134
EVP_PKEY_print_public(bio, pkey, 16, NULL);
136-
EVP_PKEY_free(pkey);
137135
}
138136
}
139137

crypto/x509/t_x509.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,12 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,
212212
return 0;
213213
}
214214

215-
EVP_PKEY *pkey = X509_get_pubkey(x);
215+
const EVP_PKEY *pkey = X509_get0_pubkey(x);
216216
if (pkey == NULL) {
217217
BIO_printf(bp, "%12sUnable to load Public Key\n", "");
218218
ERR_print_errors(bp);
219219
} else {
220220
EVP_PKEY_print_public(bp, pkey, 16, NULL);
221-
EVP_PKEY_free(pkey);
222221
}
223222
}
224223

crypto/x509/x509_cmp.c

+17-24
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ X509 *X509_find_by_subject(const STACK_OF(X509) *sk, X509_NAME *name) {
219219
}
220220

221221
EVP_PKEY *X509_get0_pubkey(const X509 *x) {
222-
if ((x == NULL) || (x->cert_info == NULL)) {
223-
return NULL;
224-
}
225-
return (X509_PUBKEY_get0(x->cert_info->key));
222+
if (x == NULL) {
223+
return NULL;
224+
}
225+
return X509_PUBKEY_get0(x->cert_info->key);
226226
}
227227

228228
EVP_PKEY *X509_get_pubkey(const X509 *x) {
229-
if ((x == NULL) || (x->cert_info == NULL)) {
229+
if (x == NULL) {
230230
return NULL;
231231
}
232232
return X509_PUBKEY_get(x->cert_info->key);
@@ -239,36 +239,29 @@ ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x) {
239239
return x->cert_info->key->public_key;
240240
}
241241

242-
int X509_check_private_key(X509 *x, const EVP_PKEY *k) {
243-
EVP_PKEY *xk;
244-
int ret;
245-
246-
xk = X509_get_pubkey(x);
242+
int X509_check_private_key(const X509 *x, const EVP_PKEY *k) {
243+
const EVP_PKEY *xk = X509_get0_pubkey(x);
244+
if (xk == NULL) {
245+
return 0;
246+
}
247247

248-
if (xk) {
249-
ret = EVP_PKEY_cmp(xk, k);
250-
} else {
251-
ret = -2;
248+
int ret = EVP_PKEY_cmp(xk, k);
249+
if (ret > 0) {
250+
return 1;
252251
}
253252

254253
switch (ret) {
255-
case 1:
256-
break;
257254
case 0:
258255
OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
259-
break;
256+
return 0;
260257
case -1:
261258
OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
262-
break;
259+
return 0;
263260
case -2:
264261
OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
262+
return 0;
265263
}
266-
if (xk) {
267-
EVP_PKEY_free(xk);
268-
}
269-
if (ret > 0) {
270-
return 1;
271-
}
264+
272265
return 0;
273266
}
274267

crypto/x509/x509_req.c

+21-18
Original file line numberDiff line numberDiff line change
@@ -77,46 +77,49 @@ X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req) {
7777
}
7878

7979
EVP_PKEY *X509_REQ_get_pubkey(const X509_REQ *req) {
80-
if ((req == NULL) || (req->req_info == NULL)) {
80+
if (req == NULL || req->req_info == NULL) {
8181
OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
8282
return NULL;
8383
}
84-
return (X509_PUBKEY_get(req->req_info->pubkey));
84+
return X509_PUBKEY_get(req->req_info->pubkey);
8585
}
8686

8787
EVP_PKEY *X509_REQ_get0_pubkey(const X509_REQ *req) {
88-
if ((req == NULL) || (req->req_info == NULL)) {
88+
if (req == NULL || req->req_info == NULL) {
8989
OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
9090
return NULL;
9191
}
92-
return (X509_PUBKEY_get0(req->req_info->pubkey));
92+
return X509_PUBKEY_get0(req->req_info->pubkey);
9393
}
9494

95-
int X509_REQ_check_private_key(X509_REQ *x, const EVP_PKEY *k) {
96-
EVP_PKEY *xk = NULL;
97-
int ok = 0;
95+
int X509_REQ_check_private_key(const X509_REQ *x, const EVP_PKEY *k) {
96+
const EVP_PKEY *xk = X509_REQ_get0_pubkey(x);
97+
if (xk == NULL) {
98+
return 0;
99+
}
100+
101+
int ret = EVP_PKEY_cmp(xk, k);
102+
if (ret > 0) {
103+
return 1;
104+
}
98105

99-
xk = X509_REQ_get_pubkey(x);
100-
switch (EVP_PKEY_cmp(xk, k)) {
101-
case 1:
102-
ok = 1;
103-
break;
106+
switch (ret) {
104107
case 0:
105108
OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
106-
break;
109+
return 0;
107110
case -1:
108111
OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
109-
break;
112+
return 0;
110113
case -2:
111114
if (EVP_PKEY_id(k) == EVP_PKEY_EC) {
112115
OPENSSL_PUT_ERROR(X509, ERR_R_EC_LIB);
113-
break;
116+
} else {
117+
OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
114118
}
115-
OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
119+
return 0;
116120
}
117121

118-
EVP_PKEY_free(xk);
119-
return ok;
122+
return 0;
120123
}
121124

122125
int X509_REQ_extension_nid(int req_nid) {

crypto/x509/x509_vfy.c

+18-35
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,6 @@ static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x) {
11821182
// Check CRL validity
11831183
static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) {
11841184
X509 *issuer = NULL;
1185-
EVP_PKEY *ikey = NULL;
1186-
int ok = 0;
11871185
int cnum = ctx->error_depth;
11881186
int chnum = (int)sk_X509_num(ctx->chain) - 1;
11891187
// if we have an alternative CRL issuer cert use that
@@ -1200,9 +1198,8 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) {
12001198
// If not self signed, can't check signature
12011199
if (!x509_check_issued_with_callback(ctx, issuer, issuer)) {
12021200
ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1203-
ok = ctx->verify_cb(0, ctx);
1204-
if (!ok) {
1205-
goto err;
1201+
if (!ctx->verify_cb(0, ctx)) {
1202+
return 0;
12061203
}
12071204
}
12081205
}
@@ -1212,61 +1209,50 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) {
12121209
if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
12131210
!(issuer->ex_kusage & X509v3_KU_CRL_SIGN)) {
12141211
ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1215-
ok = ctx->verify_cb(0, ctx);
1216-
if (!ok) {
1217-
goto err;
1212+
if (!ctx->verify_cb(0, ctx)) {
1213+
return 0;
12181214
}
12191215
}
12201216

12211217
if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
12221218
ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1223-
ok = ctx->verify_cb(0, ctx);
1224-
if (!ok) {
1225-
goto err;
1219+
if (!ctx->verify_cb(0, ctx)) {
1220+
return 0;
12261221
}
12271222
}
12281223

12291224
if (crl->idp_flags & IDP_INVALID) {
12301225
ctx->error = X509_V_ERR_INVALID_EXTENSION;
1231-
ok = ctx->verify_cb(0, ctx);
1232-
if (!ok) {
1233-
goto err;
1226+
if (!ctx->verify_cb(0, ctx)) {
1227+
return 0;
12341228
}
12351229
}
12361230

12371231
if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1238-
ok = check_crl_time(ctx, crl, 1);
1239-
if (!ok) {
1240-
goto err;
1232+
if (!check_crl_time(ctx, crl, 1)) {
1233+
return 0;
12411234
}
12421235
}
12431236

12441237
// Attempt to get issuer certificate public key
1245-
ikey = X509_get_pubkey(issuer);
1246-
1238+
EVP_PKEY *ikey = X509_get0_pubkey(issuer);
12471239
if (!ikey) {
12481240
ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1249-
ok = ctx->verify_cb(0, ctx);
1250-
if (!ok) {
1251-
goto err;
1241+
if (!ctx->verify_cb(0, ctx)) {
1242+
return 0;
12521243
}
12531244
} else {
12541245
// Verify CRL signature
12551246
if (X509_CRL_verify(crl, ikey) <= 0) {
12561247
ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1257-
ok = ctx->verify_cb(0, ctx);
1258-
if (!ok) {
1259-
goto err;
1248+
if (!ctx->verify_cb(0, ctx)) {
1249+
return 0;
12601250
}
12611251
}
12621252
}
12631253
}
12641254

1265-
ok = 1;
1266-
1267-
err:
1268-
EVP_PKEY_free(ikey);
1269-
return ok;
1255+
return 1;
12701256
}
12711257

12721258
// Check certificate against CRL
@@ -1388,7 +1374,6 @@ int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x509, int suppress_error) {
13881374
static int internal_verify(X509_STORE_CTX *ctx) {
13891375
int ok = 0;
13901376
X509 *xs, *xi;
1391-
EVP_PKEY *pkey = NULL;
13921377

13931378
int n = (int)sk_X509_num(ctx->chain);
13941379
ctx->error_depth = n - 1;
@@ -1422,7 +1407,8 @@ static int internal_verify(X509_STORE_CTX *ctx) {
14221407
// explicitly asked for. It doesn't add any security and just wastes
14231408
// time.
14241409
if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) {
1425-
if ((pkey = X509_get_pubkey(xi)) == NULL) {
1410+
EVP_PKEY *pkey = X509_get0_pubkey(xi);
1411+
if (pkey == NULL) {
14261412
ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
14271413
ctx->current_cert = xi;
14281414
ok = ctx->verify_cb(0, ctx);
@@ -1434,12 +1420,9 @@ static int internal_verify(X509_STORE_CTX *ctx) {
14341420
ctx->current_cert = xs;
14351421
ok = ctx->verify_cb(0, ctx);
14361422
if (!ok) {
1437-
EVP_PKEY_free(pkey);
14381423
goto end;
14391424
}
14401425
}
1441-
EVP_PKEY_free(pkey);
1442-
pkey = NULL;
14431426
}
14441427

14451428
check_cert:

include/openssl/x509.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ OPENSSL_EXPORT ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x509);
219219

220220
// X509_check_private_key returns one if |x509|'s public key matches |pkey| and
221221
// zero otherwise.
222-
OPENSSL_EXPORT int X509_check_private_key(X509 *x509, const EVP_PKEY *pkey);
222+
OPENSSL_EXPORT int X509_check_private_key(const X509 *x509,
223+
const EVP_PKEY *pkey);
223224

224225
// X509_get0_uids sets |*out_issuer_uid| to a non-owning pointer to the
225226
// issuerUID field of |x509|, or NULL if |x509| has no issuerUID. It similarly
@@ -1163,7 +1164,7 @@ OPENSSL_EXPORT EVP_PKEY *X509_REQ_get_pubkey(const X509_REQ *req);
11631164

11641165
// X509_REQ_check_private_key returns one if |req|'s public key matches |pkey|
11651166
// and zero otherwise.
1166-
OPENSSL_EXPORT int X509_REQ_check_private_key(X509_REQ *req,
1167+
OPENSSL_EXPORT int X509_REQ_check_private_key(const X509_REQ *req,
11671168
const EVP_PKEY *pkey);
11681169

11691170
// X509_REQ_get_attr_count returns the number of attributes in |req|.

0 commit comments

Comments
 (0)