diff options
| author | tb <> | 2023-08-10 15:11:16 +0000 |
|---|---|---|
| committer | tb <> | 2023-08-10 15:11:16 +0000 |
| commit | 613ea14100348433bc126c36a2fe25f5d35cd216 (patch) | |
| tree | 8b2adeb365ee4ac1a769735df0410ecf7de69dc7 /src/lib/libcrypto/dh | |
| parent | 94d6db7aae41374a07274a3db71eb29dfc627d30 (diff) | |
| download | openbsd-613ea14100348433bc126c36a2fe25f5d35cd216.tar.gz openbsd-613ea14100348433bc126c36a2fe25f5d35cd216.tar.bz2 openbsd-613ea14100348433bc126c36a2fe25f5d35cd216.zip | |
Clean up {dh,dsa}_pub_encode()
This brings these two messy functions into more usual shape. There is a
lot more that can be done in here. It is a step in the right direction.
ok jsing
Diffstat (limited to 'src/lib/libcrypto/dh')
| -rw-r--r-- | src/lib/libcrypto/dh/dh_ameth.c | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/src/lib/libcrypto/dh/dh_ameth.c b/src/lib/libcrypto/dh/dh_ameth.c index 12f2db7b8e..3898db89f0 100644 --- a/src/lib/libcrypto/dh/dh_ameth.c +++ b/src/lib/libcrypto/dh/dh_ameth.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: dh_ameth.c,v 1.31 2023/08/10 09:43:51 tb Exp $ */ | 1 | /* $OpenBSD: dh_ameth.c,v 1.32 2023/08/10 15:11:16 tb Exp $ */ |
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
| 3 | * project 2006. | 3 | * project 2006. |
| 4 | */ | 4 | */ |
| @@ -130,48 +130,49 @@ err: | |||
| 130 | static int | 130 | static int |
| 131 | dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) | 131 | dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) |
| 132 | { | 132 | { |
| 133 | DH *dh; | 133 | const DH *dh = pkey->pkey.dh; |
| 134 | int ptype; | 134 | ASN1_STRING *str = NULL; |
| 135 | unsigned char *penc = NULL; | 135 | int ptype = V_ASN1_SEQUENCE; |
| 136 | int penclen; | ||
| 137 | ASN1_STRING *str; | ||
| 138 | ASN1_INTEGER *pub_key = NULL; | 136 | ASN1_INTEGER *pub_key = NULL; |
| 137 | ASN1_OBJECT *aobj; | ||
| 138 | unsigned char *data = NULL, *penc = NULL; | ||
| 139 | int datalen = 0, penclen = 0; | ||
| 139 | 140 | ||
| 140 | dh=pkey->pkey.dh; | 141 | if ((datalen = i2d_DHparams(dh, &data)) <= 0) { |
| 141 | |||
| 142 | str = ASN1_STRING_new(); | ||
| 143 | if (str == NULL) { | ||
| 144 | DHerror(ERR_R_MALLOC_FAILURE); | 142 | DHerror(ERR_R_MALLOC_FAILURE); |
| 143 | datalen = 0; | ||
| 145 | goto err; | 144 | goto err; |
| 146 | } | 145 | } |
| 147 | 146 | if ((str = ASN1_STRING_new()) == NULL) { | |
| 148 | str->length = i2d_DHparams(dh, &str->data); | ||
| 149 | if (str->length <= 0) { | ||
| 150 | DHerror(ERR_R_MALLOC_FAILURE); | 147 | DHerror(ERR_R_MALLOC_FAILURE); |
| 151 | goto err; | 148 | goto err; |
| 152 | } | 149 | } |
| 153 | ptype = V_ASN1_SEQUENCE; | 150 | ASN1_STRING_set0(str, data, datalen); |
| 151 | data = NULL; | ||
| 152 | datalen = 0; | ||
| 154 | 153 | ||
| 155 | pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL); | 154 | if ((pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL)) == NULL) |
| 156 | if (!pub_key) | ||
| 157 | goto err; | 155 | goto err; |
| 158 | 156 | if ((penclen = i2d_ASN1_INTEGER(pub_key, &penc)) <= 0) { | |
| 159 | penclen = i2d_ASN1_INTEGER(pub_key, &penc); | 157 | DHerror(ERR_R_MALLOC_FAILURE); |
| 160 | 158 | penclen = 0; | |
| 159 | goto err; | ||
| 160 | } | ||
| 161 | ASN1_INTEGER_free(pub_key); | 161 | ASN1_INTEGER_free(pub_key); |
| 162 | pub_key = NULL; | ||
| 162 | 163 | ||
| 163 | if (penclen <= 0) { | 164 | if ((aobj = OBJ_nid2obj(EVP_PKEY_DH)) == NULL) |
| 164 | DHerror(ERR_R_MALLOC_FAILURE); | 165 | goto err; |
| 166 | if (!X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen)) | ||
| 165 | goto err; | 167 | goto err; |
| 166 | } | ||
| 167 | 168 | ||
| 168 | if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DH), ptype, | 169 | return 1; |
| 169 | (void *)str, penc, penclen)) | ||
| 170 | return 1; | ||
| 171 | 170 | ||
| 172 | err: | 171 | err: |
| 173 | free(penc); | ||
| 174 | ASN1_STRING_free(str); | 172 | ASN1_STRING_free(str); |
| 173 | ASN1_INTEGER_free(pub_key); | ||
| 174 | freezero(data, datalen); | ||
| 175 | freezero(penc, penclen); | ||
| 175 | 176 | ||
| 176 | return 0; | 177 | return 0; |
| 177 | } | 178 | } |
