summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec_asn1.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ec/ec_asn1.c')
-rw-r--r--src/lib/libcrypto/ec/ec_asn1.c220
1 files changed, 103 insertions, 117 deletions
diff --git a/src/lib/libcrypto/ec/ec_asn1.c b/src/lib/libcrypto/ec/ec_asn1.c
index 3234e7a6f2..dddf71c6e5 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.22 2016/03/20 16:50:29 krw Exp $ */ 1/* $OpenBSD: ec_asn1.c,v 1.23 2017/01/29 17:49:23 beck Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -96,7 +96,7 @@ EC_GROUP_get_trinomial_basis(const EC_GROUP * group, unsigned int *k)
96 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != 96 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
97 NID_X9_62_characteristic_two_field 97 NID_X9_62_characteristic_two_field
98 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0))) { 98 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] == 0))) {
99 ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 99 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
100 return 0; 100 return 0;
101 } 101 }
102 if (k) 102 if (k)
@@ -114,7 +114,7 @@ EC_GROUP_get_pentanomial_basis(const EC_GROUP * group, unsigned int *k1,
114 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) != 114 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
115 NID_X9_62_characteristic_two_field 115 NID_X9_62_characteristic_two_field
116 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0))) { 116 || !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0))) {
117 ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 117 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
118 return 0; 118 return 0;
119 } 119 }
120 if (k1) 120 if (k1)
@@ -696,29 +696,29 @@ ec_asn1_group2fieldid(const EC_GROUP * group, X9_62_FIELDID * field)
696 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); 696 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
697 /* set OID for the field */ 697 /* set OID for the field */
698 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) { 698 if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
699 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB); 699 ECerror(ERR_R_OBJ_LIB);
700 goto err; 700 goto err;
701 } 701 }
702 if (nid == NID_X9_62_prime_field) { 702 if (nid == NID_X9_62_prime_field) {
703 if ((tmp = BN_new()) == NULL) { 703 if ((tmp = BN_new()) == NULL) {
704 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 704 ECerror(ERR_R_MALLOC_FAILURE);
705 goto err; 705 goto err;
706 } 706 }
707 /* the parameters are specified by the prime number p */ 707 /* the parameters are specified by the prime number p */
708 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) { 708 if (!EC_GROUP_get_curve_GFp(group, tmp, NULL, NULL, NULL)) {
709 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB); 709 ECerror(ERR_R_EC_LIB);
710 goto err; 710 goto err;
711 } 711 }
712 /* set the prime number */ 712 /* set the prime number */
713 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL); 713 field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
714 if (field->p.prime == NULL) { 714 if (field->p.prime == NULL) {
715 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB); 715 ECerror(ERR_R_ASN1_LIB);
716 goto err; 716 goto err;
717 } 717 }
718 } else /* nid == NID_X9_62_characteristic_two_field */ 718 } else /* nid == NID_X9_62_characteristic_two_field */
719#ifdef OPENSSL_NO_EC2M 719#ifdef OPENSSL_NO_EC2M
720 { 720 {
721 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED); 721 ECerror(EC_R_GF2M_NOT_SUPPORTED);
722 goto err; 722 goto err;
723 } 723 }
724#else 724#else
@@ -730,7 +730,7 @@ ec_asn1_group2fieldid(const EC_GROUP * group, X9_62_FIELDID * field)
730 char_two = field->p.char_two; 730 char_two = field->p.char_two;
731 731
732 if (char_two == NULL) { 732 if (char_two == NULL) {
733 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 733 ECerror(ERR_R_MALLOC_FAILURE);
734 goto err; 734 goto err;
735 } 735 }
736 char_two->m = (long) EC_GROUP_get_degree(group); 736 char_two->m = (long) EC_GROUP_get_degree(group);
@@ -738,12 +738,12 @@ ec_asn1_group2fieldid(const EC_GROUP * group, X9_62_FIELDID * field)
738 field_type = EC_GROUP_get_basis_type(group); 738 field_type = EC_GROUP_get_basis_type(group);
739 739
740 if (field_type == 0) { 740 if (field_type == 0) {
741 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB); 741 ECerror(ERR_R_EC_LIB);
742 goto err; 742 goto err;
743 } 743 }
744 /* set base type OID */ 744 /* set base type OID */
745 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) { 745 if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
746 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB); 746 ECerror(ERR_R_OBJ_LIB);
747 goto err; 747 goto err;
748 } 748 }
749 if (field_type == NID_X9_62_tpBasis) { 749 if (field_type == NID_X9_62_tpBasis) {
@@ -754,12 +754,11 @@ ec_asn1_group2fieldid(const EC_GROUP * group, X9_62_FIELDID * field)
754 754
755 char_two->p.tpBasis = ASN1_INTEGER_new(); 755 char_two->p.tpBasis = ASN1_INTEGER_new();
756 if (!char_two->p.tpBasis) { 756 if (!char_two->p.tpBasis) {
757 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 757 ECerror(ERR_R_MALLOC_FAILURE);
758 goto err; 758 goto err;
759 } 759 }
760 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long) k)) { 760 if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long) k)) {
761 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, 761 ECerror(ERR_R_ASN1_LIB);
762 ERR_R_ASN1_LIB);
763 goto err; 762 goto err;
764 } 763 }
765 } else if (field_type == NID_X9_62_ppBasis) { 764 } else if (field_type == NID_X9_62_ppBasis) {
@@ -770,7 +769,7 @@ ec_asn1_group2fieldid(const EC_GROUP * group, X9_62_FIELDID * field)
770 769
771 char_two->p.ppBasis = X9_62_PENTANOMIAL_new(); 770 char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
772 if (!char_two->p.ppBasis) { 771 if (!char_two->p.ppBasis) {
773 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 772 ECerror(ERR_R_MALLOC_FAILURE);
774 goto err; 773 goto err;
775 } 774 }
776 /* set k? values */ 775 /* set k? values */
@@ -781,7 +780,7 @@ ec_asn1_group2fieldid(const EC_GROUP * group, X9_62_FIELDID * field)
781 /* for ONB the parameters are (asn1) NULL */ 780 /* for ONB the parameters are (asn1) NULL */
782 char_two->p.onBasis = ASN1_NULL_new(); 781 char_two->p.onBasis = ASN1_NULL_new();
783 if (!char_two->p.onBasis) { 782 if (!char_two->p.onBasis) {
784 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE); 783 ECerror(ERR_R_MALLOC_FAILURE);
785 goto err; 784 goto err;
786 } 785 }
787 } 786 }
@@ -809,7 +808,7 @@ ec_asn1_group2curve(const EC_GROUP * group, X9_62_CURVE * curve)
809 return 0; 808 return 0;
810 809
811 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) { 810 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
812 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); 811 ECerror(ERR_R_MALLOC_FAILURE);
813 goto err; 812 goto err;
814 } 813 }
815 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); 814 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
@@ -817,14 +816,14 @@ ec_asn1_group2curve(const EC_GROUP * group, X9_62_CURVE * curve)
817 /* get a and b */ 816 /* get a and b */
818 if (nid == NID_X9_62_prime_field) { 817 if (nid == NID_X9_62_prime_field) {
819 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) { 818 if (!EC_GROUP_get_curve_GFp(group, NULL, tmp_1, tmp_2, NULL)) {
820 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB); 819 ECerror(ERR_R_EC_LIB);
821 goto err; 820 goto err;
822 } 821 }
823 } 822 }
824#ifndef OPENSSL_NO_EC2M 823#ifndef OPENSSL_NO_EC2M
825 else { /* nid == NID_X9_62_characteristic_two_field */ 824 else { /* nid == NID_X9_62_characteristic_two_field */
826 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) { 825 if (!EC_GROUP_get_curve_GF2m(group, NULL, tmp_1, tmp_2, NULL)) {
827 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB); 826 ECerror(ERR_R_EC_LIB);
828 goto err; 827 goto err;
829 } 828 }
830 } 829 }
@@ -838,12 +837,11 @@ ec_asn1_group2curve(const EC_GROUP * group, X9_62_CURVE * curve)
838 len_1 = 1; 837 len_1 = 1;
839 } else { 838 } else {
840 if ((buffer_1 = malloc(len_1)) == NULL) { 839 if ((buffer_1 = malloc(len_1)) == NULL) {
841 ECerr(EC_F_EC_ASN1_GROUP2CURVE, 840 ECerror(ERR_R_MALLOC_FAILURE);
842 ERR_R_MALLOC_FAILURE);
843 goto err; 841 goto err;
844 } 842 }
845 if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) { 843 if ((len_1 = BN_bn2bin(tmp_1, buffer_1)) == 0) {
846 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB); 844 ECerror(ERR_R_BN_LIB);
847 goto err; 845 goto err;
848 } 846 }
849 a_buf = buffer_1; 847 a_buf = buffer_1;
@@ -855,12 +853,11 @@ ec_asn1_group2curve(const EC_GROUP * group, X9_62_CURVE * curve)
855 len_2 = 1; 853 len_2 = 1;
856 } else { 854 } else {
857 if ((buffer_2 = malloc(len_2)) == NULL) { 855 if ((buffer_2 = malloc(len_2)) == NULL) {
858 ECerr(EC_F_EC_ASN1_GROUP2CURVE, 856 ECerror(ERR_R_MALLOC_FAILURE);
859 ERR_R_MALLOC_FAILURE);
860 goto err; 857 goto err;
861 } 858 }
862 if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) { 859 if ((len_2 = BN_bn2bin(tmp_2, buffer_2)) == 0) {
863 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB); 860 ECerror(ERR_R_BN_LIB);
864 goto err; 861 goto err;
865 } 862 }
866 b_buf = buffer_2; 863 b_buf = buffer_2;
@@ -869,21 +866,21 @@ ec_asn1_group2curve(const EC_GROUP * group, X9_62_CURVE * curve)
869 /* set a and b */ 866 /* set a and b */
870 if (!ASN1_STRING_set(curve->a, a_buf, len_1) || 867 if (!ASN1_STRING_set(curve->a, a_buf, len_1) ||
871 !ASN1_STRING_set(curve->b, b_buf, len_2)) { 868 !ASN1_STRING_set(curve->b, b_buf, len_2)) {
872 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB); 869 ECerror(ERR_R_ASN1_LIB);
873 goto err; 870 goto err;
874 } 871 }
875 /* set the seed (optional) */ 872 /* set the seed (optional) */
876 if (group->seed) { 873 if (group->seed) {
877 if (!curve->seed) 874 if (!curve->seed)
878 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { 875 if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
879 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE); 876 ECerror(ERR_R_MALLOC_FAILURE);
880 goto err; 877 goto err;
881 } 878 }
882 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); 879 curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
883 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT; 880 curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT;
884 if (!ASN1_BIT_STRING_set(curve->seed, group->seed, 881 if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
885 (int) group->seed_len)) { 882 (int) group->seed_len)) {
886 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB); 883 ECerror(ERR_R_ASN1_LIB);
887 goto err; 884 goto err;
888 } 885 }
889 } else { 886 } else {
@@ -915,13 +912,12 @@ ec_asn1_group2parameters(const EC_GROUP * group, ECPARAMETERS * param)
915 point_conversion_form_t form; 912 point_conversion_form_t form;
916 913
917 if ((tmp = BN_new()) == NULL) { 914 if ((tmp = BN_new()) == NULL) {
918 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); 915 ECerror(ERR_R_MALLOC_FAILURE);
919 goto err; 916 goto err;
920 } 917 }
921 if (param == NULL) { 918 if (param == NULL) {
922 if ((ret = ECPARAMETERS_new()) == NULL) { 919 if ((ret = ECPARAMETERS_new()) == NULL) {
923 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, 920 ECerror(ERR_R_MALLOC_FAILURE);
924 ERR_R_MALLOC_FAILURE);
925 goto err; 921 goto err;
926 } 922 }
927 } else 923 } else
@@ -932,57 +928,57 @@ ec_asn1_group2parameters(const EC_GROUP * group, ECPARAMETERS * param)
932 928
933 /* set the fieldID */ 929 /* set the fieldID */
934 if (!ec_asn1_group2fieldid(group, ret->fieldID)) { 930 if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
935 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 931 ECerror(ERR_R_EC_LIB);
936 goto err; 932 goto err;
937 } 933 }
938 /* set the curve */ 934 /* set the curve */
939 if (!ec_asn1_group2curve(group, ret->curve)) { 935 if (!ec_asn1_group2curve(group, ret->curve)) {
940 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 936 ECerror(ERR_R_EC_LIB);
941 goto err; 937 goto err;
942 } 938 }
943 /* set the base point */ 939 /* set the base point */
944 if ((point = EC_GROUP_get0_generator(group)) == NULL) { 940 if ((point = EC_GROUP_get0_generator(group)) == NULL) {
945 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, EC_R_UNDEFINED_GENERATOR); 941 ECerror(EC_R_UNDEFINED_GENERATOR);
946 goto err; 942 goto err;
947 } 943 }
948 form = EC_GROUP_get_point_conversion_form(group); 944 form = EC_GROUP_get_point_conversion_form(group);
949 945
950 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL); 946 len = EC_POINT_point2oct(group, point, form, NULL, len, NULL);
951 if (len == 0) { 947 if (len == 0) {
952 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 948 ECerror(ERR_R_EC_LIB);
953 goto err; 949 goto err;
954 } 950 }
955 if ((buffer = malloc(len)) == NULL) { 951 if ((buffer = malloc(len)) == NULL) {
956 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); 952 ECerror(ERR_R_MALLOC_FAILURE);
957 goto err; 953 goto err;
958 } 954 }
959 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) { 955 if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
960 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 956 ECerror(ERR_R_EC_LIB);
961 goto err; 957 goto err;
962 } 958 }
963 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) { 959 if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
964 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); 960 ECerror(ERR_R_MALLOC_FAILURE);
965 goto err; 961 goto err;
966 } 962 }
967 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) { 963 if (!ASN1_OCTET_STRING_set(ret->base, buffer, len)) {
968 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB); 964 ECerror(ERR_R_ASN1_LIB);
969 goto err; 965 goto err;
970 } 966 }
971 /* set the order */ 967 /* set the order */
972 if (!EC_GROUP_get_order(group, tmp, NULL)) { 968 if (!EC_GROUP_get_order(group, tmp, NULL)) {
973 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); 969 ECerror(ERR_R_EC_LIB);
974 goto err; 970 goto err;
975 } 971 }
976 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order); 972 ret->order = BN_to_ASN1_INTEGER(tmp, ret->order);
977 if (ret->order == NULL) { 973 if (ret->order == NULL) {
978 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB); 974 ECerror(ERR_R_ASN1_LIB);
979 goto err; 975 goto err;
980 } 976 }
981 /* set the cofactor (optional) */ 977 /* set the cofactor (optional) */
982 if (EC_GROUP_get_cofactor(group, tmp, NULL)) { 978 if (EC_GROUP_get_cofactor(group, tmp, NULL)) {
983 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor); 979 ret->cofactor = BN_to_ASN1_INTEGER(tmp, ret->cofactor);
984 if (ret->cofactor == NULL) { 980 if (ret->cofactor == NULL) {
985 ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_ASN1_LIB); 981 ECerror(ERR_R_ASN1_LIB);
986 goto err; 982 goto err;
987 } 983 }
988 } 984 }
@@ -1006,8 +1002,7 @@ ec_asn1_group2pkparameters(const EC_GROUP * group, ECPKPARAMETERS * params)
1006 1002
1007 if (ret == NULL) { 1003 if (ret == NULL) {
1008 if ((ret = ECPKPARAMETERS_new()) == NULL) { 1004 if ((ret = ECPKPARAMETERS_new()) == NULL) {
1009 ECerr(EC_F_EC_ASN1_GROUP2PKPARAMETERS, 1005 ECerror(ERR_R_MALLOC_FAILURE);
1010 ERR_R_MALLOC_FAILURE);
1011 return NULL; 1006 return NULL;
1012 } 1007 }
1013 } else { 1008 } else {
@@ -1056,24 +1051,24 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1056 1051
1057 if (!params->fieldID || !params->fieldID->fieldType || 1052 if (!params->fieldID || !params->fieldID->fieldType ||
1058 !params->fieldID->p.ptr) { 1053 !params->fieldID->p.ptr) {
1059 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 1054 ECerror(EC_R_ASN1_ERROR);
1060 goto err; 1055 goto err;
1061 } 1056 }
1062 /* now extract the curve parameters a and b */ 1057 /* now extract the curve parameters a and b */
1063 if (!params->curve || !params->curve->a || 1058 if (!params->curve || !params->curve->a ||
1064 !params->curve->a->data || !params->curve->b || 1059 !params->curve->a->data || !params->curve->b ||
1065 !params->curve->b->data) { 1060 !params->curve->b->data) {
1066 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 1061 ECerror(EC_R_ASN1_ERROR);
1067 goto err; 1062 goto err;
1068 } 1063 }
1069 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL); 1064 a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
1070 if (a == NULL) { 1065 if (a == NULL) {
1071 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB); 1066 ECerror(ERR_R_BN_LIB);
1072 goto err; 1067 goto err;
1073 } 1068 }
1074 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL); 1069 b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
1075 if (b == NULL) { 1070 if (b == NULL) {
1076 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB); 1071 ECerror(ERR_R_BN_LIB);
1077 goto err; 1072 goto err;
1078 } 1073 }
1079 /* get the field parameters */ 1074 /* get the field parameters */
@@ -1081,7 +1076,7 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1081 if (tmp == NID_X9_62_characteristic_two_field) 1076 if (tmp == NID_X9_62_characteristic_two_field)
1082#ifdef OPENSSL_NO_EC2M 1077#ifdef OPENSSL_NO_EC2M
1083 { 1078 {
1084 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED); 1079 ECerror(EC_R_GF2M_NOT_SUPPORTED);
1085 goto err; 1080 goto err;
1086 } 1081 }
1087#else 1082#else
@@ -1092,11 +1087,11 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1092 1087
1093 field_bits = char_two->m; 1088 field_bits = char_two->m;
1094 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { 1089 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1095 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE); 1090 ECerror(EC_R_FIELD_TOO_LARGE);
1096 goto err; 1091 goto err;
1097 } 1092 }
1098 if ((p = BN_new()) == NULL) { 1093 if ((p = BN_new()) == NULL) {
1099 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE); 1094 ECerror(ERR_R_MALLOC_FAILURE);
1100 goto err; 1095 goto err;
1101 } 1096 }
1102 /* get the base type */ 1097 /* get the base type */
@@ -1106,13 +1101,13 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1106 long tmp_long; 1101 long tmp_long;
1107 1102
1108 if (!char_two->p.tpBasis) { 1103 if (!char_two->p.tpBasis) {
1109 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 1104 ECerror(EC_R_ASN1_ERROR);
1110 goto err; 1105 goto err;
1111 } 1106 }
1112 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis); 1107 tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);
1113 1108
1114 if (!(char_two->m > tmp_long && tmp_long > 0)) { 1109 if (!(char_two->m > tmp_long && tmp_long > 0)) {
1115 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_TRINOMIAL_BASIS); 1110 ECerror(EC_R_INVALID_TRINOMIAL_BASIS);
1116 goto err; 1111 goto err;
1117 } 1112 }
1118 /* create the polynomial */ 1113 /* create the polynomial */
@@ -1127,11 +1122,11 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1127 1122
1128 penta = char_two->p.ppBasis; 1123 penta = char_two->p.ppBasis;
1129 if (!penta) { 1124 if (!penta) {
1130 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 1125 ECerror(EC_R_ASN1_ERROR);
1131 goto err; 1126 goto err;
1132 } 1127 }
1133 if (!(char_two->m > penta->k3 && penta->k3 > penta->k2 && penta->k2 > penta->k1 && penta->k1 > 0)) { 1128 if (!(char_two->m > penta->k3 && penta->k3 > penta->k2 && penta->k2 > penta->k1 && penta->k1 > 0)) {
1134 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_PENTANOMIAL_BASIS); 1129 ECerror(EC_R_INVALID_PENTANOMIAL_BASIS);
1135 goto err; 1130 goto err;
1136 } 1131 }
1137 /* create the polynomial */ 1132 /* create the polynomial */
@@ -1146,10 +1141,10 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1146 if (!BN_set_bit(p, 0)) 1141 if (!BN_set_bit(p, 0))
1147 goto err; 1142 goto err;
1148 } else if (tmp == NID_X9_62_onBasis) { 1143 } else if (tmp == NID_X9_62_onBasis) {
1149 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED); 1144 ECerror(EC_R_NOT_IMPLEMENTED);
1150 goto err; 1145 goto err;
1151 } else { /* error */ 1146 } else { /* error */
1152 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 1147 ECerror(EC_R_ASN1_ERROR);
1153 goto err; 1148 goto err;
1154 } 1149 }
1155 1150
@@ -1161,40 +1156,39 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1161 /* we have a curve over a prime field */ 1156 /* we have a curve over a prime field */
1162 /* extract the prime number */ 1157 /* extract the prime number */
1163 if (!params->fieldID->p.prime) { 1158 if (!params->fieldID->p.prime) {
1164 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 1159 ECerror(EC_R_ASN1_ERROR);
1165 goto err; 1160 goto err;
1166 } 1161 }
1167 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL); 1162 p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
1168 if (p == NULL) { 1163 if (p == NULL) {
1169 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); 1164 ECerror(ERR_R_ASN1_LIB);
1170 goto err; 1165 goto err;
1171 } 1166 }
1172 if (BN_is_negative(p) || BN_is_zero(p)) { 1167 if (BN_is_negative(p) || BN_is_zero(p)) {
1173 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD); 1168 ECerror(EC_R_INVALID_FIELD);
1174 goto err; 1169 goto err;
1175 } 1170 }
1176 field_bits = BN_num_bits(p); 1171 field_bits = BN_num_bits(p);
1177 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { 1172 if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
1178 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE); 1173 ECerror(EC_R_FIELD_TOO_LARGE);
1179 goto err; 1174 goto err;
1180 } 1175 }
1181 /* create the EC_GROUP structure */ 1176 /* create the EC_GROUP structure */
1182 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL); 1177 ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
1183 } else { 1178 } else {
1184 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD); 1179 ECerror(EC_R_INVALID_FIELD);
1185 goto err; 1180 goto err;
1186 } 1181 }
1187 1182
1188 if (ret == NULL) { 1183 if (ret == NULL) {
1189 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB); 1184 ECerror(ERR_R_EC_LIB);
1190 goto err; 1185 goto err;
1191 } 1186 }
1192 /* extract seed (optional) */ 1187 /* extract seed (optional) */
1193 if (params->curve->seed != NULL) { 1188 if (params->curve->seed != NULL) {
1194 free(ret->seed); 1189 free(ret->seed);
1195 if (!(ret->seed = malloc(params->curve->seed->length))) { 1190 if (!(ret->seed = malloc(params->curve->seed->length))) {
1196 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, 1191 ECerror(ERR_R_MALLOC_FAILURE);
1197 ERR_R_MALLOC_FAILURE);
1198 goto err; 1192 goto err;
1199 } 1193 }
1200 memcpy(ret->seed, params->curve->seed->data, 1194 memcpy(ret->seed, params->curve->seed->data,
@@ -1202,7 +1196,7 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1202 ret->seed_len = params->curve->seed->length; 1196 ret->seed_len = params->curve->seed->length;
1203 } 1197 }
1204 if (!params->order || !params->base || !params->base->data) { 1198 if (!params->order || !params->base || !params->base->data) {
1205 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR); 1199 ECerror(EC_R_ASN1_ERROR);
1206 goto err; 1200 goto err;
1207 } 1201 }
1208 if ((point = EC_POINT_new(ret)) == NULL) 1202 if ((point = EC_POINT_new(ret)) == NULL)
@@ -1215,20 +1209,20 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1215 /* extract the ec point */ 1209 /* extract the ec point */
1216 if (!EC_POINT_oct2point(ret, point, params->base->data, 1210 if (!EC_POINT_oct2point(ret, point, params->base->data,
1217 params->base->length, NULL)) { 1211 params->base->length, NULL)) {
1218 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB); 1212 ECerror(ERR_R_EC_LIB);
1219 goto err; 1213 goto err;
1220 } 1214 }
1221 /* extract the order */ 1215 /* extract the order */
1222 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) { 1216 if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
1223 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); 1217 ECerror(ERR_R_ASN1_LIB);
1224 goto err; 1218 goto err;
1225 } 1219 }
1226 if (BN_is_negative(a) || BN_is_zero(a)) { 1220 if (BN_is_negative(a) || BN_is_zero(a)) {
1227 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER); 1221 ECerror(EC_R_INVALID_GROUP_ORDER);
1228 goto err; 1222 goto err;
1229 } 1223 }
1230 if (BN_num_bits(a) > (int) field_bits + 1) { /* Hasse bound */ 1224 if (BN_num_bits(a) > (int) field_bits + 1) { /* Hasse bound */
1231 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER); 1225 ECerror(EC_R_INVALID_GROUP_ORDER);
1232 goto err; 1226 goto err;
1233 } 1227 }
1234 /* extract the cofactor (optional) */ 1228 /* extract the cofactor (optional) */
@@ -1236,12 +1230,12 @@ ec_asn1_parameters2group(const ECPARAMETERS * params)
1236 BN_free(b); 1230 BN_free(b);
1237 b = NULL; 1231 b = NULL;
1238 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) { 1232 } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
1239 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB); 1233 ECerror(ERR_R_ASN1_LIB);
1240 goto err; 1234 goto err;
1241 } 1235 }
1242 /* set the generator, order and cofactor (if present) */ 1236 /* set the generator, order and cofactor (if present) */
1243 if (!EC_GROUP_set_generator(ret, point, a, b)) { 1237 if (!EC_GROUP_set_generator(ret, point, a, b)) {
1244 ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB); 1238 ECerror(ERR_R_EC_LIB);
1245 goto err; 1239 goto err;
1246 } 1240 }
1247 ok = 1; 1241 ok = 1;
@@ -1264,15 +1258,13 @@ ec_asn1_pkparameters2group(const ECPKPARAMETERS * params)
1264 int tmp = 0; 1258 int tmp = 0;
1265 1259
1266 if (params == NULL) { 1260 if (params == NULL) {
1267 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, 1261 ECerror(EC_R_MISSING_PARAMETERS);
1268 EC_R_MISSING_PARAMETERS);
1269 return NULL; 1262 return NULL;
1270 } 1263 }
1271 if (params->type == 0) {/* the curve is given by an OID */ 1264 if (params->type == 0) {/* the curve is given by an OID */
1272 tmp = OBJ_obj2nid(params->value.named_curve); 1265 tmp = OBJ_obj2nid(params->value.named_curve);
1273 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) { 1266 if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
1274 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, 1267 ECerror(EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
1275 EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
1276 return NULL; 1268 return NULL;
1277 } 1269 }
1278 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); 1270 EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);
@@ -1280,14 +1272,14 @@ ec_asn1_pkparameters2group(const ECPKPARAMETERS * params)
1280 * ECPARAMETERS structure */ 1272 * ECPARAMETERS structure */
1281 ret = ec_asn1_parameters2group(params->value.parameters); 1273 ret = ec_asn1_parameters2group(params->value.parameters);
1282 if (!ret) { 1274 if (!ret) {
1283 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB); 1275 ECerror(ERR_R_EC_LIB);
1284 return NULL; 1276 return NULL;
1285 } 1277 }
1286 EC_GROUP_set_asn1_flag(ret, 0x0); 1278 EC_GROUP_set_asn1_flag(ret, 0x0);
1287 } else if (params->type == 2) { /* implicitlyCA */ 1279 } else if (params->type == 2) { /* implicitlyCA */
1288 return NULL; 1280 return NULL;
1289 } else { 1281 } else {
1290 ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR); 1282 ECerror(EC_R_ASN1_ERROR);
1291 return NULL; 1283 return NULL;
1292 } 1284 }
1293 1285
@@ -1303,11 +1295,11 @@ d2i_ECPKParameters(EC_GROUP ** a, const unsigned char **in, long len)
1303 ECPKPARAMETERS *params = NULL; 1295 ECPKPARAMETERS *params = NULL;
1304 1296
1305 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) { 1297 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {
1306 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE); 1298 ECerror(EC_R_D2I_ECPKPARAMETERS_FAILURE);
1307 goto err; 1299 goto err;
1308 } 1300 }
1309 if ((group = ec_asn1_pkparameters2group(params)) == NULL) { 1301 if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
1310 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE); 1302 ECerror(EC_R_PKPARAMETERS2GROUP_FAILURE);
1311 goto err; 1303 goto err;
1312 } 1304 }
1313 1305
@@ -1327,11 +1319,11 @@ i2d_ECPKParameters(const EC_GROUP * a, unsigned char **out)
1327 int ret = 0; 1319 int ret = 0;
1328 ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL); 1320 ECPKPARAMETERS *tmp = ec_asn1_group2pkparameters(a, NULL);
1329 if (tmp == NULL) { 1321 if (tmp == NULL) {
1330 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE); 1322 ECerror(EC_R_GROUP2PKPARAMETERS_FAILURE);
1331 return 0; 1323 return 0;
1332 } 1324 }
1333 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) { 1325 if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
1334 ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE); 1326 ECerror(EC_R_I2D_ECPKPARAMETERS_FAILURE);
1335 ECPKPARAMETERS_free(tmp); 1327 ECPKPARAMETERS_free(tmp);
1336 return 0; 1328 return 0;
1337 } 1329 }
@@ -1348,17 +1340,17 @@ d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1348 EC_PRIVATEKEY *priv_key = NULL; 1340 EC_PRIVATEKEY *priv_key = NULL;
1349 1341
1350 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { 1342 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1351 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); 1343 ECerror(ERR_R_MALLOC_FAILURE);
1352 return NULL; 1344 return NULL;
1353 } 1345 }
1354 if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL) { 1346 if ((priv_key = d2i_EC_PRIVATEKEY(&priv_key, in, len)) == NULL) {
1355 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); 1347 ECerror(ERR_R_EC_LIB);
1356 EC_PRIVATEKEY_free(priv_key); 1348 EC_PRIVATEKEY_free(priv_key);
1357 return NULL; 1349 return NULL;
1358 } 1350 }
1359 if (a == NULL || *a == NULL) { 1351 if (a == NULL || *a == NULL) {
1360 if ((ret = EC_KEY_new()) == NULL) { 1352 if ((ret = EC_KEY_new()) == NULL) {
1361 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); 1353 ECerror(ERR_R_MALLOC_FAILURE);
1362 goto err; 1354 goto err;
1363 } 1355 }
1364 } else 1356 } else
@@ -1369,7 +1361,7 @@ d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1369 ret->group = ec_asn1_pkparameters2group(priv_key->parameters); 1361 ret->group = ec_asn1_pkparameters2group(priv_key->parameters);
1370 } 1362 }
1371 if (ret->group == NULL) { 1363 if (ret->group == NULL) {
1372 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); 1364 ECerror(ERR_R_EC_LIB);
1373 goto err; 1365 goto err;
1374 } 1366 }
1375 ret->version = priv_key->version; 1367 ret->version = priv_key->version;
@@ -1380,13 +1372,11 @@ d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1380 ASN1_STRING_length(priv_key->privateKey), 1372 ASN1_STRING_length(priv_key->privateKey),
1381 ret->priv_key); 1373 ret->priv_key);
1382 if (ret->priv_key == NULL) { 1374 if (ret->priv_key == NULL) {
1383 ECerr(EC_F_D2I_ECPRIVATEKEY, 1375 ECerror(ERR_R_BN_LIB);
1384 ERR_R_BN_LIB);
1385 goto err; 1376 goto err;
1386 } 1377 }
1387 } else { 1378 } else {
1388 ECerr(EC_F_D2I_ECPRIVATEKEY, 1379 ECerror(EC_R_MISSING_PRIVATE_KEY);
1389 EC_R_MISSING_PRIVATE_KEY);
1390 goto err; 1380 goto err;
1391 } 1381 }
1392 1382
@@ -1397,7 +1387,7 @@ d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1397 EC_POINT_clear_free(ret->pub_key); 1387 EC_POINT_clear_free(ret->pub_key);
1398 ret->pub_key = EC_POINT_new(ret->group); 1388 ret->pub_key = EC_POINT_new(ret->group);
1399 if (ret->pub_key == NULL) { 1389 if (ret->pub_key == NULL) {
1400 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); 1390 ECerror(ERR_R_EC_LIB);
1401 goto err; 1391 goto err;
1402 } 1392 }
1403 pub_oct = ASN1_STRING_data(priv_key->publicKey); 1393 pub_oct = ASN1_STRING_data(priv_key->publicKey);
@@ -1406,7 +1396,7 @@ d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1406 ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01); 1396 ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);
1407 if (!EC_POINT_oct2point(ret->group, ret->pub_key, 1397 if (!EC_POINT_oct2point(ret->group, ret->pub_key,
1408 pub_oct, pub_oct_len, NULL)) { 1398 pub_oct, pub_oct_len, NULL)) {
1409 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB); 1399 ECerror(ERR_R_EC_LIB);
1410 goto err; 1400 goto err;
1411 } 1401 }
1412 } 1402 }
@@ -1434,13 +1424,11 @@ i2d_ECPrivateKey(EC_KEY * a, unsigned char **out)
1434 EC_PRIVATEKEY *priv_key = NULL; 1424 EC_PRIVATEKEY *priv_key = NULL;
1435 1425
1436 if (a == NULL || a->group == NULL || a->priv_key == NULL) { 1426 if (a == NULL || a->group == NULL || a->priv_key == NULL) {
1437 ECerr(EC_F_I2D_ECPRIVATEKEY, 1427 ECerror(ERR_R_PASSED_NULL_PARAMETER);
1438 ERR_R_PASSED_NULL_PARAMETER);
1439 goto err; 1428 goto err;
1440 } 1429 }
1441 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { 1430 if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
1442 ECerr(EC_F_I2D_ECPRIVATEKEY, 1431 ECerror(ERR_R_MALLOC_FAILURE);
1443 ERR_R_MALLOC_FAILURE);
1444 goto err; 1432 goto err;
1445 } 1433 }
1446 priv_key->version = a->version; 1434 priv_key->version = a->version;
@@ -1448,30 +1436,28 @@ i2d_ECPrivateKey(EC_KEY * a, unsigned char **out)
1448 buf_len = (size_t) BN_num_bytes(a->priv_key); 1436 buf_len = (size_t) BN_num_bytes(a->priv_key);
1449 buffer = malloc(buf_len); 1437 buffer = malloc(buf_len);
1450 if (buffer == NULL) { 1438 if (buffer == NULL) {
1451 ECerr(EC_F_I2D_ECPRIVATEKEY, 1439 ECerror(ERR_R_MALLOC_FAILURE);
1452 ERR_R_MALLOC_FAILURE);
1453 goto err; 1440 goto err;
1454 } 1441 }
1455 if (!BN_bn2bin(a->priv_key, buffer)) { 1442 if (!BN_bn2bin(a->priv_key, buffer)) {
1456 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_BN_LIB); 1443 ECerror(ERR_R_BN_LIB);
1457 goto err; 1444 goto err;
1458 } 1445 }
1459 if (!ASN1_STRING_set(priv_key->privateKey, buffer, buf_len)) { 1446 if (!ASN1_STRING_set(priv_key->privateKey, buffer, buf_len)) {
1460 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB); 1447 ECerror(ERR_R_ASN1_LIB);
1461 goto err; 1448 goto err;
1462 } 1449 }
1463 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { 1450 if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) {
1464 if ((priv_key->parameters = ec_asn1_group2pkparameters( 1451 if ((priv_key->parameters = ec_asn1_group2pkparameters(
1465 a->group, priv_key->parameters)) == NULL) { 1452 a->group, priv_key->parameters)) == NULL) {
1466 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); 1453 ECerror(ERR_R_EC_LIB);
1467 goto err; 1454 goto err;
1468 } 1455 }
1469 } 1456 }
1470 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key != NULL) { 1457 if (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key != NULL) {
1471 priv_key->publicKey = ASN1_BIT_STRING_new(); 1458 priv_key->publicKey = ASN1_BIT_STRING_new();
1472 if (priv_key->publicKey == NULL) { 1459 if (priv_key->publicKey == NULL) {
1473 ECerr(EC_F_I2D_ECPRIVATEKEY, 1460 ECerror(ERR_R_MALLOC_FAILURE);
1474 ERR_R_MALLOC_FAILURE);
1475 goto err; 1461 goto err;
1476 } 1462 }
1477 tmp_len = EC_POINT_point2oct(a->group, a->pub_key, 1463 tmp_len = EC_POINT_point2oct(a->group, a->pub_key,
@@ -1480,7 +1466,7 @@ i2d_ECPrivateKey(EC_KEY * a, unsigned char **out)
1480 if (tmp_len > buf_len) { 1466 if (tmp_len > buf_len) {
1481 unsigned char *tmp_buffer = realloc(buffer, tmp_len); 1467 unsigned char *tmp_buffer = realloc(buffer, tmp_len);
1482 if (!tmp_buffer) { 1468 if (!tmp_buffer) {
1483 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE); 1469 ECerror(ERR_R_MALLOC_FAILURE);
1484 goto err; 1470 goto err;
1485 } 1471 }
1486 buffer = tmp_buffer; 1472 buffer = tmp_buffer;
@@ -1488,19 +1474,19 @@ i2d_ECPrivateKey(EC_KEY * a, unsigned char **out)
1488 } 1474 }
1489 if (!EC_POINT_point2oct(a->group, a->pub_key, 1475 if (!EC_POINT_point2oct(a->group, a->pub_key,
1490 a->conv_form, buffer, buf_len, NULL)) { 1476 a->conv_form, buffer, buf_len, NULL)) {
1491 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); 1477 ECerror(ERR_R_EC_LIB);
1492 goto err; 1478 goto err;
1493 } 1479 }
1494 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); 1480 priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1495 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT; 1481 priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1496 if (!ASN1_STRING_set(priv_key->publicKey, buffer, 1482 if (!ASN1_STRING_set(priv_key->publicKey, buffer,
1497 buf_len)) { 1483 buf_len)) {
1498 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_ASN1_LIB); 1484 ECerror(ERR_R_ASN1_LIB);
1499 goto err; 1485 goto err;
1500 } 1486 }
1501 } 1487 }
1502 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { 1488 if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
1503 ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB); 1489 ECerror(ERR_R_EC_LIB);
1504 goto err; 1490 goto err;
1505 } 1491 }
1506 ok = 1; 1492 ok = 1;
@@ -1515,7 +1501,7 @@ int
1515i2d_ECParameters(EC_KEY * a, unsigned char **out) 1501i2d_ECParameters(EC_KEY * a, unsigned char **out)
1516{ 1502{
1517 if (a == NULL) { 1503 if (a == NULL) {
1518 ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER); 1504 ECerror(ERR_R_PASSED_NULL_PARAMETER);
1519 return 0; 1505 return 0;
1520 } 1506 }
1521 return i2d_ECPKParameters(a->group, out); 1507 return i2d_ECPKParameters(a->group, out);
@@ -1527,19 +1513,19 @@ d2i_ECParameters(EC_KEY ** a, const unsigned char **in, long len)
1527 EC_KEY *ret; 1513 EC_KEY *ret;
1528 1514
1529 if (in == NULL || *in == NULL) { 1515 if (in == NULL || *in == NULL) {
1530 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER); 1516 ECerror(ERR_R_PASSED_NULL_PARAMETER);
1531 return NULL; 1517 return NULL;
1532 } 1518 }
1533 if (a == NULL || *a == NULL) { 1519 if (a == NULL || *a == NULL) {
1534 if ((ret = EC_KEY_new()) == NULL) { 1520 if ((ret = EC_KEY_new()) == NULL) {
1535 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE); 1521 ECerror(ERR_R_MALLOC_FAILURE);
1536 return NULL; 1522 return NULL;
1537 } 1523 }
1538 } else 1524 } else
1539 ret = *a; 1525 ret = *a;
1540 1526
1541 if (!d2i_ECPKParameters(&ret->group, in, len)) { 1527 if (!d2i_ECPKParameters(&ret->group, in, len)) {
1542 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB); 1528 ECerror(ERR_R_EC_LIB);
1543 if (a == NULL || *a != ret) 1529 if (a == NULL || *a != ret)
1544 EC_KEY_free(ret); 1530 EC_KEY_free(ret);
1545 return NULL; 1531 return NULL;
@@ -1560,17 +1546,17 @@ o2i_ECPublicKey(EC_KEY ** a, const unsigned char **in, long len)
1560 * sorry, but a EC_GROUP-structur is necessary to set the 1546 * sorry, but a EC_GROUP-structur is necessary to set the
1561 * public key 1547 * public key
1562 */ 1548 */
1563 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER); 1549 ECerror(ERR_R_PASSED_NULL_PARAMETER);
1564 return 0; 1550 return 0;
1565 } 1551 }
1566 ret = *a; 1552 ret = *a;
1567 if (ret->pub_key == NULL && 1553 if (ret->pub_key == NULL &&
1568 (ret->pub_key = EC_POINT_new(ret->group)) == NULL) { 1554 (ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
1569 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_MALLOC_FAILURE); 1555 ECerror(ERR_R_MALLOC_FAILURE);
1570 return 0; 1556 return 0;
1571 } 1557 }
1572 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) { 1558 if (!EC_POINT_oct2point(ret->group, ret->pub_key, *in, len, NULL)) {
1573 ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB); 1559 ECerror(ERR_R_EC_LIB);
1574 return 0; 1560 return 0;
1575 } 1561 }
1576 /* save the point conversion form */ 1562 /* save the point conversion form */
@@ -1586,7 +1572,7 @@ i2o_ECPublicKey(EC_KEY * a, unsigned char **out)
1586 int new_buffer = 0; 1572 int new_buffer = 0;
1587 1573
1588 if (a == NULL) { 1574 if (a == NULL) {
1589 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER); 1575 ECerror(ERR_R_PASSED_NULL_PARAMETER);
1590 return 0; 1576 return 0;
1591 } 1577 }
1592 buf_len = EC_POINT_point2oct(a->group, a->pub_key, 1578 buf_len = EC_POINT_point2oct(a->group, a->pub_key,
@@ -1598,14 +1584,14 @@ i2o_ECPublicKey(EC_KEY * a, unsigned char **out)
1598 1584
1599 if (*out == NULL) { 1585 if (*out == NULL) {
1600 if ((*out = malloc(buf_len)) == NULL) { 1586 if ((*out = malloc(buf_len)) == NULL) {
1601 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE); 1587 ECerror(ERR_R_MALLOC_FAILURE);
1602 return 0; 1588 return 0;
1603 } 1589 }
1604 new_buffer = 1; 1590 new_buffer = 1;
1605 } 1591 }
1606 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, 1592 if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
1607 *out, buf_len, NULL)) { 1593 *out, buf_len, NULL)) {
1608 ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB); 1594 ECerror(ERR_R_EC_LIB);
1609 if (new_buffer) { 1595 if (new_buffer) {
1610 free(*out); 1596 free(*out);
1611 *out = NULL; 1597 *out = NULL;