summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2022-06-25 16:15:18 +0000
committerjsing <>2022-06-25 16:15:18 +0000
commit1d19a6a87bdf14afc50458119a6f47ca1a457aca (patch)
treee24f4023b782fb3ac866ff68d41b7cd10522ac52 /src
parent3ab41ace1e3881927ef311ca2f0e8f2a30cf07a1 (diff)
downloadopenbsd-1d19a6a87bdf14afc50458119a6f47ca1a457aca.tar.gz
openbsd-1d19a6a87bdf14afc50458119a6f47ca1a457aca.tar.bz2
openbsd-1d19a6a87bdf14afc50458119a6f47ca1a457aca.zip
Reuse ASN1_INTEGER functions for ASN1_ENUMERATED_{get,set}()
Instead of having a separate get/set implementation, reuse the ASN1_INTEGER code. Also prepare to provide ASN1_ENUMERATED_{get,set}_int64(). ok beck@ tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/asn1/a_enum.c109
-rw-r--r--src/lib/libcrypto/asn1/asn1.h6
2 files changed, 59 insertions, 56 deletions
diff --git a/src/lib/libcrypto/asn1/a_enum.c b/src/lib/libcrypto/asn1/a_enum.c
index 007a421849..11868cef37 100644
--- a/src/lib/libcrypto/asn1/a_enum.c
+++ b/src/lib/libcrypto/asn1/a_enum.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_enum.c,v 1.23 2021/12/25 13:17:48 jsing Exp $ */ 1/* $OpenBSD: a_enum.c,v 1.24 2022/06/25 16:15:18 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -57,7 +57,7 @@
57 */ 57 */
58 58
59#include <limits.h> 59#include <limits.h>
60#include <stdio.h> 60#include <string.h>
61 61
62#include <openssl/asn1.h> 62#include <openssl/asn1.h>
63#include <openssl/asn1t.h> 63#include <openssl/asn1t.h>
@@ -65,6 +65,9 @@
65#include <openssl/buffer.h> 65#include <openssl/buffer.h>
66#include <openssl/err.h> 66#include <openssl/err.h>
67 67
68#include "asn1_locl.h"
69#include "bytestring.h"
70
68/* 71/*
69 * Code for ENUMERATED type: identical to INTEGER apart from a different tag. 72 * Code for ENUMERATED type: identical to INTEGER apart from a different tag.
70 * for comments on encoding see a_int.c 73 * for comments on encoding see a_int.c
@@ -82,6 +85,16 @@ ASN1_ENUMERATED_new(void)
82 return (ASN1_ENUMERATED *)ASN1_item_new(&ASN1_ENUMERATED_it); 85 return (ASN1_ENUMERATED *)ASN1_item_new(&ASN1_ENUMERATED_it);
83} 86}
84 87
88static void
89asn1_aenum_clear(ASN1_ENUMERATED *aenum)
90{
91 freezero(aenum->data, aenum->length);
92
93 memset(aenum, 0, sizeof(*aenum));
94
95 aenum->type = V_ASN1_ENUMERATED;
96}
97
85void 98void
86ASN1_ENUMERATED_free(ASN1_ENUMERATED *a) 99ASN1_ENUMERATED_free(ASN1_ENUMERATED *a)
87{ 100{
@@ -89,73 +102,59 @@ ASN1_ENUMERATED_free(ASN1_ENUMERATED *a)
89} 102}
90 103
91int 104int
92ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) 105ASN1_ENUMERATED_get_int64(int64_t *out_val, const ASN1_ENUMERATED *aenum)
93{ 106{
94 int j, k; 107 CBS cbs;
95 unsigned int i; 108
96 unsigned char buf[sizeof(long) + 1]; 109 *out_val = 0;
97 long d; 110
98 111 if (aenum == NULL || aenum->length < 0)
99 a->type = V_ASN1_ENUMERATED; 112 return 0;
100 if (a->length < (int)(sizeof(long) + 1)) { 113
101 free(a->data); 114 if (aenum->type != V_ASN1_ENUMERATED &&
102 a->data = calloc(1, sizeof(long) + 1); 115 aenum->type != V_ASN1_NEG_ENUMERATED) {
103 } 116 ASN1error(ASN1_R_WRONG_INTEGER_TYPE);
104 if (a->data == NULL) { 117 return 0;
105 ASN1error(ERR_R_MALLOC_FAILURE);
106 return (0);
107 }
108 d = v;
109 if (d < 0) {
110 d = -d;
111 a->type = V_ASN1_NEG_ENUMERATED;
112 } 118 }
113 119
114 for (i = 0; i < sizeof(long); i++) { 120 CBS_init(&cbs, aenum->data, aenum->length);
115 if (d == 0) 121
116 break; 122 return asn1_aint_get_int64(&cbs, (aenum->type == V_ASN1_NEG_ENUMERATED),
117 buf[i] = (int)d & 0xff; 123 out_val);
118 d >>= 8; 124}
125
126int
127ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *aenum, int64_t val)
128{
129 asn1_aenum_clear(aenum);
130
131 if (val < 0) {
132 aenum->type = V_ASN1_NEG_ENUMERATED;
133 val = -val;
119 } 134 }
120 j = 0; 135
121 for (k = i - 1; k >= 0; k--) 136 return asn1_aint_set_uint64((uint64_t)val, &aenum->data, &aenum->length);
122 a->data[j++] = buf[k];
123 a->length = j;
124 return (1);
125} 137}
126 138
127long 139long
128ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a) 140ASN1_ENUMERATED_get(const ASN1_ENUMERATED *aenum)
129{ 141{
130 int neg = 0, i; 142 int64_t val;
131 unsigned long r = 0;
132 143
133 if (a == NULL) 144 if (!ASN1_ENUMERATED_get_int64(&val, aenum))
134 return (0L);
135 i = a->type;
136 if (i == V_ASN1_NEG_ENUMERATED)
137 neg = 1;
138 else if (i != V_ASN1_ENUMERATED)
139 return -1; 145 return -1;
140 146 if (val < LONG_MIN || val > LONG_MAX) {
141 if (a->length > (int)sizeof(long)) { 147 /* hmm... a bit ugly, return all ones */
142 /* hmm... a bit ugly */
143 return -1; 148 return -1;
144 } 149 }
145 if (a->data == NULL)
146 return 0;
147
148 for (i = 0; i < a->length; i++) {
149 r <<= 8;
150 r |= (unsigned char)a->data[i];
151 }
152 150
153 if (r > LONG_MAX) 151 return (long)val;
154 return -1; 152}
155 153
156 if (neg) 154int
157 return -(long)r; 155ASN1_ENUMERATED_set(ASN1_ENUMERATED *aenum, long val)
158 return (long)r; 156{
157 return ASN1_ENUMERATED_set_int64(aenum, val);
159} 158}
160 159
161ASN1_ENUMERATED * 160ASN1_ENUMERATED *
diff --git a/src/lib/libcrypto/asn1/asn1.h b/src/lib/libcrypto/asn1/asn1.h
index d6adb0d22e..0db0b1d8fe 100644
--- a/src/lib/libcrypto/asn1/asn1.h
+++ b/src/lib/libcrypto/asn1/asn1.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1.h,v 1.63 2022/06/25 15:39:12 jsing Exp $ */ 1/* $OpenBSD: asn1.h,v 1.64 2022/06/25 16:15:18 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -759,6 +759,10 @@ long ASN1_INTEGER_get(const ASN1_INTEGER *a);
759ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai); 759ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai);
760BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn); 760BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn);
761 761
762#ifdef LIBRESSL_INTERNAL
763int ASN1_ENUMERATED_get_int64(int64_t *out_val, const ASN1_ENUMERATED *aenum);
764int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *aenum, int64_t val);
765#endif
762int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v); 766int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v);
763long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a); 767long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a);
764ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai); 768ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai);