summaryrefslogtreecommitdiff
path: root/key.c
diff options
context:
space:
mode:
Diffstat (limited to 'key.c')
-rw-r--r--key.c31
1 files changed, 22 insertions, 9 deletions
diff --git a/key.c b/key.c
index 196092de5..c71bf5b0a 100644
--- a/key.c
+++ b/key.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: key.c,v 1.93 2010/09/09 10:45:45 djm Exp $ */ 1/* $OpenBSD: key.c,v 1.94 2010/10/28 11:22:09 djm Exp $ */
2/* 2/*
3 * read_bignum(): 3 * read_bignum():
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -1053,12 +1053,8 @@ key_ecdsa_bits_to_nid(int bits)
1053} 1053}
1054 1054
1055#ifdef OPENSSL_HAS_ECC 1055#ifdef OPENSSL_HAS_ECC
1056/*
1057 * This is horrid, but OpenSSL's PEM_read_PrivateKey seems not to restore
1058 * the EC_GROUP nid when loading a key...
1059 */
1060int 1056int
1061key_ecdsa_group_to_nid(const EC_GROUP *g) 1057key_ecdsa_key_to_nid(EC_KEY *k)
1062{ 1058{
1063 EC_GROUP *eg; 1059 EC_GROUP *eg;
1064 int nids[] = { 1060 int nids[] = {
@@ -1067,23 +1063,39 @@ key_ecdsa_group_to_nid(const EC_GROUP *g)
1067 NID_secp521r1, 1063 NID_secp521r1,
1068 -1 1064 -1
1069 }; 1065 };
1066 int nid;
1070 u_int i; 1067 u_int i;
1071 BN_CTX *bnctx; 1068 BN_CTX *bnctx;
1069 const EC_GROUP *g = EC_KEY_get0_group(k);
1072 1070
1071 /*
1072 * The group may be stored in a ASN.1 encoded private key in one of two
1073 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1074 * or explicit group parameters encoded into the key blob. Only the
1075 * "named group" case sets the group NID for us, but we can figure
1076 * it out for the other case by comparing against all the groups that
1077 * are supported.
1078 */
1079 if ((nid = EC_GROUP_get_curve_name(g)) > 0)
1080 return nid;
1073 if ((bnctx = BN_CTX_new()) == NULL) 1081 if ((bnctx = BN_CTX_new()) == NULL)
1074 fatal("%s: BN_CTX_new() failed", __func__); 1082 fatal("%s: BN_CTX_new() failed", __func__);
1075 for (i = 0; nids[i] != -1; i++) { 1083 for (i = 0; nids[i] != -1; i++) {
1076 if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL) 1084 if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL)
1077 fatal("%s: EC_GROUP_new_by_curve_name failed", 1085 fatal("%s: EC_GROUP_new_by_curve_name failed",
1078 __func__); 1086 __func__);
1079 if (EC_GROUP_cmp(g, eg, bnctx) == 0) { 1087 if (EC_GROUP_cmp(g, eg, bnctx) == 0)
1080 EC_GROUP_free(eg);
1081 break; 1088 break;
1082 }
1083 EC_GROUP_free(eg); 1089 EC_GROUP_free(eg);
1084 } 1090 }
1085 BN_CTX_free(bnctx); 1091 BN_CTX_free(bnctx);
1086 debug3("%s: nid = %d", __func__, nids[i]); 1092 debug3("%s: nid = %d", __func__, nids[i]);
1093 if (nids[i] != -1) {
1094 /* Use the group with the NID attached */
1095 EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
1096 if (EC_KEY_set_group(k, eg) != 1)
1097 fatal("%s: EC_KEY_set_group", __func__);
1098 }
1087 return nids[i]; 1099 return nids[i];
1088} 1100}
1089 1101
@@ -1098,6 +1110,7 @@ ecdsa_generate_private_key(u_int bits, int *nid)
1098 fatal("%s: EC_KEY_new_by_curve_name failed", __func__); 1110 fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
1099 if (EC_KEY_generate_key(private) != 1) 1111 if (EC_KEY_generate_key(private) != 1)
1100 fatal("%s: EC_KEY_generate_key failed", __func__); 1112 fatal("%s: EC_KEY_generate_key failed", __func__);
1113 EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
1101 return private; 1114 return private;
1102} 1115}
1103#endif /* OPENSSL_HAS_ECC */ 1116#endif /* OPENSSL_HAS_ECC */