summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libcrypto/ec/ec_asn1.c75
1 files changed, 49 insertions, 26 deletions
diff --git a/src/lib/libcrypto/ec/ec_asn1.c b/src/lib/libcrypto/ec/ec_asn1.c
index 5f8a6c344e..09aa947b71 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.94 2024/10/29 04:57:33 tb Exp $ */ 1/* $OpenBSD: ec_asn1.c,v 1.95 2024/10/30 06:11:50 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -561,6 +561,50 @@ EC_PRIVATEKEY_free(EC_PRIVATEKEY *a)
561} 561}
562 562
563static int 563static int
564ec_point_to_asn1_string_type(const EC_GROUP *group, const EC_POINT *point,
565 int form, int type, ASN1_STRING **out_astr)
566{
567 ASN1_STRING *astr = NULL;
568 unsigned char *buf = NULL;
569 size_t len = 0;
570 int ret = 0;
571
572 if (*out_astr != NULL && ASN1_STRING_type(*out_astr) != type)
573 goto err;
574
575 if (!ec_point_to_octets(group, point, form, &buf, &len, NULL))
576 goto err;
577
578 if ((astr = *out_astr) == NULL)
579 astr = ASN1_STRING_type_new(type);
580 if (astr == NULL)
581 goto err;
582
583 ASN1_STRING_set0(astr, buf, len);
584 buf = NULL;
585 len = 0;
586
587 *out_astr = astr;
588 astr = NULL;
589
590 ret = 1;
591
592 err:
593 ASN1_STRING_free(astr);
594 freezero(buf, len);
595
596 return ret;
597}
598
599static int
600ec_point_to_asn1_octet_string(const EC_GROUP *group, const EC_POINT *point,
601 int form, ASN1_OCTET_STRING **out_aos)
602{
603 return ec_point_to_asn1_string_type(group, point, form,
604 V_ASN1_OCTET_STRING, out_aos);
605}
606
607static int
564ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field) 608ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field)
565{ 609{
566 BIGNUM *p = NULL; 610 BIGNUM *p = NULL;
@@ -719,12 +763,10 @@ static ECPARAMETERS *
719ec_asn1_group2parameters(const EC_GROUP *group) 763ec_asn1_group2parameters(const EC_GROUP *group)
720{ 764{
721 int ok = 0; 765 int ok = 0;
722 size_t len = 0;
723 ECPARAMETERS *ret = NULL; 766 ECPARAMETERS *ret = NULL;
724 const BIGNUM *order, *cofactor; 767 const BIGNUM *order, *cofactor;
725 unsigned char *buffer = NULL;
726 const EC_POINT *point = NULL; 768 const EC_POINT *point = NULL;
727 point_conversion_form_t form; 769 uint8_t form;
728 770
729 if ((ret = ECPARAMETERS_new()) == NULL) { 771 if ((ret = ECPARAMETERS_new()) == NULL) {
730 ECerror(ERR_R_MALLOC_FAILURE); 772 ECerror(ERR_R_MALLOC_FAILURE);
@@ -749,29 +791,11 @@ ec_asn1_group2parameters(const EC_GROUP *group)
749 ECerror(EC_R_UNDEFINED_GENERATOR); 791 ECerror(EC_R_UNDEFINED_GENERATOR);
750 goto err; 792 goto err;
751 } 793 }
752 form = EC_GROUP_get_point_conversion_form(group);
753 794
754 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL); 795 form = EC_GROUP_get_point_conversion_form(group);
755 if (len == 0) { 796 if (!ec_point_to_asn1_octet_string(group, point, form, &ret->base))
756 ECerror(ERR_R_EC_LIB);
757 goto err;
758 }
759 if ((buffer = malloc(len)) == NULL) {
760 ECerror(ERR_R_MALLOC_FAILURE);
761 goto err;
762 }
763 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
764 ECerror(ERR_R_EC_LIB);
765 goto err;
766 }
767 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
768 ECerror(ERR_R_MALLOC_FAILURE);
769 goto err;
770 }
771 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
772 ECerror(ERR_R_ASN1_LIB);
773 goto err; 797 goto err;
774 } 798
775 if ((order = EC_GROUP_get0_order(group)) == NULL) { 799 if ((order = EC_GROUP_get0_order(group)) == NULL) {
776 ECerror(ERR_R_EC_LIB); 800 ECerror(ERR_R_EC_LIB);
777 goto err; 801 goto err;
@@ -804,7 +828,6 @@ ec_asn1_group2parameters(const EC_GROUP *group)
804 ECPARAMETERS_free(ret); 828 ECPARAMETERS_free(ret);
805 ret = NULL; 829 ret = NULL;
806 } 830 }
807 free(buffer);
808 return (ret); 831 return (ret);
809} 832}
810 833