summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authortb <>2024-10-31 15:26:05 +0000
committertb <>2024-10-31 15:26:05 +0000
commit2f2f748fde73822038414204b26d3d5f3b07d5ba (patch)
tree96394e3a6d7b3176f0f83b321128355e5bd46c59 /src/lib
parent3c4e7375bde814ea2663e65bf0caa42fe3b4a05d (diff)
downloadopenbsd-2f2f748fde73822038414204b26d3d5f3b07d5ba.tar.gz
openbsd-2f2f748fde73822038414204b26d3d5f3b07d5ba.tar.bz2
openbsd-2f2f748fde73822038414204b26d3d5f3b07d5ba.zip
Clean up the mess in i2d_EC_PRIVATEKEY()
Use a few local variables to make the checks at the start slightly less unappealing. Use those to simplify the conditionals a bit and avoid a particularly silly exit code. ok is set unless ret is 0, so what do you think 'return (ok ? ret : 0);' returns? By the way, ret < 0 is an error as well. While most of the stuff in this file could use a lot more cleanup, I think the first layer of cockroaches has been exterminated and there's even some faint golden glimmer between the turds. Let's shelve the biohazard warnings for now. ok jsing
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/ec/ec_asn1.c52
1 files changed, 36 insertions, 16 deletions
diff --git a/src/lib/libcrypto/ec/ec_asn1.c b/src/lib/libcrypto/ec/ec_asn1.c
index 50e089a063..8c99773512 100644
--- a/src/lib/libcrypto/ec/ec_asn1.c
+++ b/src/lib/libcrypto/ec/ec_asn1.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_asn1.c,v 1.103 2024/10/31 15:07:49 tb Exp $ */ 1/* $OpenBSD: ec_asn1.c,v 1.104 2024/10/31 15:26:05 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -1293,46 +1293,66 @@ LCRYPTO_ALIAS(d2i_ECPrivateKey);
1293int 1293int
1294i2d_ECPrivateKey(EC_KEY *ec_key, unsigned char **out) 1294i2d_ECPrivateKey(EC_KEY *ec_key, unsigned char **out)
1295{ 1295{
1296 int ret = 0, ok = 0;
1297 EC_PRIVATEKEY *ec_privatekey = NULL; 1296 EC_PRIVATEKEY *ec_privatekey = NULL;
1297 const EC_GROUP *group;
1298 const BIGNUM *private_key;
1299 const EC_POINT *public_key = NULL;
1300 int ret = 0;
1298 1301
1299 if (ec_key == NULL || ec_key->group == NULL || ec_key->priv_key == NULL || 1302 if (ec_key == NULL) {
1300 (!(ec_key->enc_flag & EC_PKEY_NO_PUBKEY) && ec_key->pub_key == NULL)) {
1301 ECerror(ERR_R_PASSED_NULL_PARAMETER); 1303 ECerror(ERR_R_PASSED_NULL_PARAMETER);
1302 goto err; 1304 goto err;
1303 } 1305 }
1306 if ((group = EC_KEY_get0_group(ec_key)) == NULL) {
1307 ECerror(EC_R_MISSING_PARAMETERS);
1308 goto err;
1309 }
1310 if ((private_key = EC_KEY_get0_private_key(ec_key)) == NULL) {
1311 ECerror(EC_R_KEYS_NOT_SET);
1312 goto err;
1313 }
1314 if ((ec_key->enc_flag & EC_PKEY_NO_PUBKEY) == 0) {
1315 if ((public_key = EC_KEY_get0_public_key(ec_key)) == NULL) {
1316 ECerror(EC_R_KEYS_NOT_SET);
1317 goto err;
1318 }
1319 }
1320
1304 if ((ec_privatekey = EC_PRIVATEKEY_new()) == NULL) { 1321 if ((ec_privatekey = EC_PRIVATEKEY_new()) == NULL) {
1305 ECerror(ERR_R_MALLOC_FAILURE); 1322 ECerror(ERR_R_MALLOC_FAILURE);
1306 goto err; 1323 goto err;
1307 } 1324 }
1308 ec_privatekey->version = ec_key->version; 1325 ec_privatekey->version = ec_key->version;
1309 1326
1310 if (!ec_asn1_encode_private_key(ec_key->group, ec_key->priv_key, 1327 if (!ec_asn1_encode_private_key(group, private_key, ec_privatekey->privateKey))
1311 ec_privatekey->privateKey))
1312 goto err; 1328 goto err;
1313 if (!(ec_key->enc_flag & EC_PKEY_NO_PARAMETERS)) { 1329 if ((ec_key->enc_flag & EC_PKEY_NO_PARAMETERS) == 0) {
1314 ECPKPARAMETERS *parameters; 1330 ECPKPARAMETERS *parameters;
1315 1331
1316 if ((parameters = ec_asn1_group2pkparameters(ec_key->group)) == NULL) { 1332 if ((parameters = ec_asn1_group2pkparameters(group)) == NULL) {
1317 ECerror(ERR_R_EC_LIB); 1333 ECerror(ERR_R_EC_LIB);
1318 goto err; 1334 goto err;
1319 } 1335 }
1320 ec_privatekey->parameters = parameters; 1336 ec_privatekey->parameters = parameters;
1321 } 1337 }
1322 if (!(ec_key->enc_flag & EC_PKEY_NO_PUBKEY) && ec_key->pub_key != NULL) { 1338 if (public_key != NULL) {
1323 if (!ec_point_to_asn1_bit_string(ec_key->group, ec_key->pub_key, 1339 uint8_t form;
1324 ec_key->conv_form, &ec_privatekey->publicKey)) 1340
1341 form = EC_KEY_get_conv_form(ec_key);
1342 if (!ec_point_to_asn1_bit_string(group, public_key, form,
1343 &ec_privatekey->publicKey))
1325 goto err; 1344 goto err;
1326 } 1345 }
1327 if ((ret = i2d_EC_PRIVATEKEY(ec_privatekey, out)) == 0) { 1346
1347 if ((ret = i2d_EC_PRIVATEKEY(ec_privatekey, out)) <= 0) {
1328 ECerror(ERR_R_EC_LIB); 1348 ECerror(ERR_R_EC_LIB);
1329 goto err; 1349 goto err;
1330 } 1350 }
1331 ok = 1; 1351
1332 err: 1352 err:
1333 if (ec_privatekey) 1353 EC_PRIVATEKEY_free(ec_privatekey);
1334 EC_PRIVATEKEY_free(ec_privatekey); 1354
1335 return (ok ? ret : 0); 1355 return ret;
1336} 1356}
1337LCRYPTO_ALIAS(i2d_ECPrivateKey); 1357LCRYPTO_ALIAS(i2d_ECPrivateKey);
1338 1358