diff options
| author | jsing <> | 2022-03-14 16:23:29 +0000 |
|---|---|---|
| committer | jsing <> | 2022-03-14 16:23:29 +0000 |
| commit | 0e1a01640cd83662aa9f1a5c203f51d41f1ec36a (patch) | |
| tree | b6570fea4c5090bc3a5b571d3adfc4fd4fdc5575 /src | |
| parent | 80a47514c89065d34f61afd4698b0f8182c45d60 (diff) | |
| download | openbsd-0e1a01640cd83662aa9f1a5c203f51d41f1ec36a.tar.gz openbsd-0e1a01640cd83662aa9f1a5c203f51d41f1ec36a.tar.bz2 openbsd-0e1a01640cd83662aa9f1a5c203f51d41f1ec36a.zip | |
First pass clean up of ASN1_STRING code.
Use consistent variable names (astr/src) rather than 'a', 'bs', 'str', 'v'
or 'x', add some whitespace and remove some unneeded parentheses.
ok inoguchi@ tb@
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/asn1/a_string.c | 161 |
1 files changed, 87 insertions, 74 deletions
diff --git a/src/lib/libcrypto/asn1/a_string.c b/src/lib/libcrypto/asn1/a_string.c index 6d227f3688..2b5840e9e0 100644 --- a/src/lib/libcrypto/asn1/a_string.c +++ b/src/lib/libcrypto/asn1/a_string.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: a_string.c,v 1.4 2021/12/25 13:17:48 jsing Exp $ */ | 1 | /* $OpenBSD: a_string.c,v 1.5 2022/03/14 16:23:29 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 | * |
| @@ -56,11 +56,11 @@ | |||
| 56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
| 57 | */ | 57 | */ |
| 58 | 58 | ||
| 59 | #include <stdio.h> | 59 | #include <limits.h> |
| 60 | #include <stdlib.h> | ||
| 60 | #include <string.h> | 61 | #include <string.h> |
| 61 | 62 | ||
| 62 | #include <openssl/asn1.h> | 63 | #include <openssl/asn1.h> |
| 63 | #include <openssl/buffer.h> | ||
| 64 | #include <openssl/err.h> | 64 | #include <openssl/err.h> |
| 65 | 65 | ||
| 66 | #include "asn1_locl.h" | 66 | #include "asn1_locl.h" |
| @@ -68,31 +68,33 @@ | |||
| 68 | ASN1_STRING * | 68 | ASN1_STRING * |
| 69 | ASN1_STRING_new(void) | 69 | ASN1_STRING_new(void) |
| 70 | { | 70 | { |
| 71 | return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); | 71 | return ASN1_STRING_type_new(V_ASN1_OCTET_STRING); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | ASN1_STRING * | 74 | ASN1_STRING * |
| 75 | ASN1_STRING_type_new(int type) | 75 | ASN1_STRING_type_new(int type) |
| 76 | { | 76 | { |
| 77 | ASN1_STRING *a; | 77 | ASN1_STRING *astr; |
| 78 | 78 | ||
| 79 | if ((a = calloc(1, sizeof(ASN1_STRING))) == NULL) { | 79 | if ((astr = calloc(1, sizeof(ASN1_STRING))) == NULL) { |
| 80 | ASN1error(ERR_R_MALLOC_FAILURE); | 80 | ASN1error(ERR_R_MALLOC_FAILURE); |
| 81 | return NULL; | 81 | return NULL; |
| 82 | } | 82 | } |
| 83 | a->type = type; | 83 | astr->type = type; |
| 84 | 84 | ||
| 85 | return a; | 85 | return astr; |
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | void | 88 | void |
| 89 | ASN1_STRING_free(ASN1_STRING *a) | 89 | ASN1_STRING_free(ASN1_STRING *astr) |
| 90 | { | 90 | { |
| 91 | if (a == NULL) | 91 | if (astr == NULL) |
| 92 | return; | 92 | return; |
| 93 | if (a->data != NULL && !(a->flags & ASN1_STRING_FLAG_NDEF)) | 93 | |
| 94 | freezero(a->data, a->length); | 94 | if (astr->data != NULL && !(astr->flags & ASN1_STRING_FLAG_NDEF)) |
| 95 | free(a); | 95 | freezero(astr->data, astr->length); |
| 96 | |||
| 97 | free(astr); | ||
| 96 | } | 98 | } |
| 97 | 99 | ||
| 98 | int | 100 | int |
| @@ -111,68 +113,72 @@ ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) | |||
| 111 | } | 113 | } |
| 112 | 114 | ||
| 113 | int | 115 | int |
| 114 | ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) | 116 | ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *src) |
| 115 | { | 117 | { |
| 116 | if (str == NULL) | 118 | if (src == NULL) |
| 117 | return 0; | 119 | return 0; |
| 118 | if (!ASN1_STRING_set(dst, str->data, str->length)) | 120 | |
| 121 | if (!ASN1_STRING_set(dst, src->data, src->length)) | ||
| 119 | return 0; | 122 | return 0; |
| 120 | dst->type = str->type; | 123 | |
| 121 | dst->flags = str->flags; | 124 | dst->type = src->type; |
| 125 | dst->flags = src->flags & ~ASN1_STRING_FLAG_NDEF; | ||
| 126 | |||
| 122 | return 1; | 127 | return 1; |
| 123 | } | 128 | } |
| 124 | 129 | ||
| 125 | ASN1_STRING * | 130 | ASN1_STRING * |
| 126 | ASN1_STRING_dup(const ASN1_STRING *str) | 131 | ASN1_STRING_dup(const ASN1_STRING *src) |
| 127 | { | 132 | { |
| 128 | ASN1_STRING *ret; | 133 | ASN1_STRING *astr; |
| 129 | 134 | ||
| 130 | if (!str) | 135 | if (src == NULL) |
| 131 | return NULL; | 136 | return NULL; |
| 132 | ret = ASN1_STRING_new(); | 137 | |
| 133 | if (!ret) | 138 | if ((astr = ASN1_STRING_new()) == NULL) |
| 134 | return NULL; | 139 | return NULL; |
| 135 | if (!ASN1_STRING_copy(ret, str)) { | 140 | if (!ASN1_STRING_copy(astr, src)) { |
| 136 | ASN1_STRING_free(ret); | 141 | ASN1_STRING_free(astr); |
| 137 | return NULL; | 142 | return NULL; |
| 138 | } | 143 | } |
| 139 | return ret; | 144 | return astr; |
| 140 | } | 145 | } |
| 141 | 146 | ||
| 142 | int | 147 | int |
| 143 | ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) | 148 | ASN1_STRING_set(ASN1_STRING *astr, const void *_data, int len) |
| 144 | { | 149 | { |
| 145 | const char *data = _data; | 150 | const char *data = _data; |
| 146 | 151 | ||
| 147 | if (len < 0) { | 152 | if (len < 0) { |
| 148 | if (data == NULL) | 153 | if (data == NULL) |
| 149 | return (0); | 154 | return 0; |
| 150 | else | 155 | else |
| 151 | len = strlen(data); | 156 | len = strlen(data); |
| 152 | } | 157 | } |
| 153 | if ((str->length < len) || (str->data == NULL)) { | 158 | if ((astr->length < len) || (astr->data == NULL)) { |
| 154 | unsigned char *tmp; | 159 | unsigned char *tmp; |
| 155 | tmp = realloc(str->data, len + 1); | 160 | tmp = realloc(astr->data, len + 1); |
| 156 | if (tmp == NULL) { | 161 | if (tmp == NULL) { |
| 157 | ASN1error(ERR_R_MALLOC_FAILURE); | 162 | ASN1error(ERR_R_MALLOC_FAILURE); |
| 158 | return (0); | 163 | return 0; |
| 159 | } | 164 | } |
| 160 | str->data = tmp; | 165 | astr->data = tmp; |
| 161 | } | 166 | } |
| 162 | str->length = len; | 167 | astr->length = len; |
| 163 | if (data != NULL) { | 168 | if (data != NULL) { |
| 164 | memmove(str->data, data, len); | 169 | memmove(astr->data, data, len); |
| 165 | } | 170 | } |
| 166 | str->data[str->length] = '\0'; | 171 | astr->data[astr->length] = '\0'; |
| 167 | return (1); | 172 | |
| 173 | return 1; | ||
| 168 | } | 174 | } |
| 169 | 175 | ||
| 170 | void | 176 | void |
| 171 | ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) | 177 | ASN1_STRING_set0(ASN1_STRING *astr, void *data, int len) |
| 172 | { | 178 | { |
| 173 | freezero(str->data, str->length); | 179 | freezero(astr->data, astr->length); |
| 174 | str->data = data; | 180 | astr->data = data; |
| 175 | str->length = len; | 181 | astr->length = len; |
| 176 | } | 182 | } |
| 177 | 183 | ||
| 178 | void | 184 | void |
| @@ -182,47 +188,49 @@ asn1_add_error(const unsigned char *address, int offset) | |||
| 182 | } | 188 | } |
| 183 | 189 | ||
| 184 | int | 190 | int |
| 185 | ASN1_STRING_length(const ASN1_STRING *x) | 191 | ASN1_STRING_length(const ASN1_STRING *astr) |
| 186 | { | 192 | { |
| 187 | return (x->length); | 193 | return astr->length; |
| 188 | } | 194 | } |
| 189 | 195 | ||
| 190 | void | 196 | void |
| 191 | ASN1_STRING_length_set(ASN1_STRING *x, int len) | 197 | ASN1_STRING_length_set(ASN1_STRING *astr, int len) |
| 192 | { | 198 | { |
| 193 | x->length = len; | 199 | /* This is dangerous and unfixable. */ |
| 200 | astr->length = len; | ||
| 194 | } | 201 | } |
| 195 | 202 | ||
| 196 | int | 203 | int |
| 197 | ASN1_STRING_type(const ASN1_STRING *x) | 204 | ASN1_STRING_type(const ASN1_STRING *astr) |
| 198 | { | 205 | { |
| 199 | return (x->type); | 206 | return astr->type; |
| 200 | } | 207 | } |
| 201 | 208 | ||
| 202 | unsigned char * | 209 | unsigned char * |
| 203 | ASN1_STRING_data(ASN1_STRING *x) | 210 | ASN1_STRING_data(ASN1_STRING *astr) |
| 204 | { | 211 | { |
| 205 | return (x->data); | 212 | return astr->data; |
| 206 | } | 213 | } |
| 207 | 214 | ||
| 208 | const unsigned char * | 215 | const unsigned char * |
| 209 | ASN1_STRING_get0_data(const ASN1_STRING *x) | 216 | ASN1_STRING_get0_data(const ASN1_STRING *astr) |
| 210 | { | 217 | { |
| 211 | return (x->data); | 218 | return astr->data; |
| 212 | } | 219 | } |
| 213 | 220 | ||
| 214 | int | 221 | int |
| 215 | ASN1_STRING_print(BIO *bp, const ASN1_STRING *v) | 222 | ASN1_STRING_print(BIO *bp, const ASN1_STRING *astr) |
| 216 | { | 223 | { |
| 217 | int i, n; | 224 | int i, n; |
| 218 | char buf[80]; | 225 | char buf[80]; |
| 219 | const char *p; | 226 | const char *p; |
| 220 | 227 | ||
| 221 | if (v == NULL) | 228 | if (astr == NULL) |
| 222 | return (0); | 229 | return 0; |
| 230 | |||
| 223 | n = 0; | 231 | n = 0; |
| 224 | p = (const char *)v->data; | 232 | p = (const char *)astr->data; |
| 225 | for (i = 0; i < v->length; i++) { | 233 | for (i = 0; i < astr->length; i++) { |
| 226 | if ((p[i] > '~') || ((p[i] < ' ') && | 234 | if ((p[i] > '~') || ((p[i] < ' ') && |
| 227 | (p[i] != '\n') && (p[i] != '\r'))) | 235 | (p[i] != '\n') && (p[i] != '\r'))) |
| 228 | buf[n] = '.'; | 236 | buf[n] = '.'; |
| @@ -231,14 +239,16 @@ ASN1_STRING_print(BIO *bp, const ASN1_STRING *v) | |||
| 231 | n++; | 239 | n++; |
| 232 | if (n >= 80) { | 240 | if (n >= 80) { |
| 233 | if (BIO_write(bp, buf, n) <= 0) | 241 | if (BIO_write(bp, buf, n) <= 0) |
| 234 | return (0); | 242 | return 0; |
| 235 | n = 0; | 243 | n = 0; |
| 236 | } | 244 | } |
| 237 | } | 245 | } |
| 238 | if (n > 0) | 246 | if (n > 0) { |
| 239 | if (BIO_write(bp, buf, n) <= 0) | 247 | if (BIO_write(bp, buf, n) <= 0) |
| 240 | return (0); | 248 | return 0; |
| 241 | return (1); | 249 | } |
| 250 | |||
| 251 | return 1; | ||
| 242 | } | 252 | } |
| 243 | 253 | ||
| 244 | /* | 254 | /* |
| @@ -251,11 +261,12 @@ ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in) | |||
| 251 | ASN1_STRING stmp, *str = &stmp; | 261 | ASN1_STRING stmp, *str = &stmp; |
| 252 | int mbflag, ret; | 262 | int mbflag, ret; |
| 253 | 263 | ||
| 254 | if (!in) | 264 | if (in == NULL) |
| 255 | return -1; | 265 | return -1; |
| 256 | 266 | ||
| 257 | if ((mbflag = asn1_tag2charwidth(in->type)) == -1) | 267 | if ((mbflag = asn1_tag2charwidth(in->type)) == -1) |
| 258 | return -1; | 268 | return -1; |
| 269 | |||
| 259 | mbflag |= MBSTRING_FLAG; | 270 | mbflag |= MBSTRING_FLAG; |
| 260 | 271 | ||
| 261 | stmp.data = NULL; | 272 | stmp.data = NULL; |
| @@ -269,41 +280,41 @@ ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in) | |||
| 269 | } | 280 | } |
| 270 | 281 | ||
| 271 | int | 282 | int |
| 272 | i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type) | 283 | i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *astr, int type) |
| 273 | { | 284 | { |
| 274 | int i, n = 0; | 285 | int i, n = 0; |
| 275 | static const char h[] = "0123456789ABCDEF"; | 286 | static const char h[] = "0123456789ABCDEF"; |
| 276 | char buf[2]; | 287 | char buf[2]; |
| 277 | 288 | ||
| 278 | if (a == NULL) | 289 | if (astr == NULL) |
| 279 | return (0); | 290 | return 0; |
| 280 | 291 | ||
| 281 | if (a->length == 0) { | 292 | if (astr->length == 0) { |
| 282 | if (BIO_write(bp, "0", 1) != 1) | 293 | if (BIO_write(bp, "0", 1) != 1) |
| 283 | goto err; | 294 | goto err; |
| 284 | n = 1; | 295 | n = 1; |
| 285 | } else { | 296 | } else { |
| 286 | for (i = 0; i < a->length; i++) { | 297 | for (i = 0; i < astr->length; i++) { |
| 287 | if ((i != 0) && (i % 35 == 0)) { | 298 | if ((i != 0) && (i % 35 == 0)) { |
| 288 | if (BIO_write(bp, "\\\n", 2) != 2) | 299 | if (BIO_write(bp, "\\\n", 2) != 2) |
| 289 | goto err; | 300 | goto err; |
| 290 | n += 2; | 301 | n += 2; |
| 291 | } | 302 | } |
| 292 | buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; | 303 | buf[0] = h[((unsigned char)astr->data[i] >> 4) & 0x0f]; |
| 293 | buf[1] = h[((unsigned char)a->data[i]) & 0x0f]; | 304 | buf[1] = h[((unsigned char)astr->data[i]) & 0x0f]; |
| 294 | if (BIO_write(bp, buf, 2) != 2) | 305 | if (BIO_write(bp, buf, 2) != 2) |
| 295 | goto err; | 306 | goto err; |
| 296 | n += 2; | 307 | n += 2; |
| 297 | } | 308 | } |
| 298 | } | 309 | } |
| 299 | return (n); | 310 | return n; |
| 300 | 311 | ||
| 301 | err: | 312 | err: |
| 302 | return (-1); | 313 | return -1; |
| 303 | } | 314 | } |
| 304 | 315 | ||
| 305 | int | 316 | int |
| 306 | a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size) | 317 | a2i_ASN1_STRING(BIO *bp, ASN1_STRING *astr, char *buf, int size) |
| 307 | { | 318 | { |
| 308 | int ret = 0; | 319 | int ret = 0; |
| 309 | int i, j, k, m, n, again, bufsize; | 320 | int i, j, k, m, n, again, bufsize; |
| @@ -380,13 +391,15 @@ a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size) | |||
| 380 | else | 391 | else |
| 381 | break; | 392 | break; |
| 382 | } | 393 | } |
| 383 | bs->length = num; | 394 | astr->length = num; |
| 384 | bs->data = s; | 395 | astr->data = s; |
| 385 | return (1); | 396 | |
| 397 | return 1; | ||
| 386 | 398 | ||
| 387 | err_sl: | 399 | err_sl: |
| 388 | ASN1error(ASN1_R_SHORT_LINE); | 400 | ASN1error(ASN1_R_SHORT_LINE); |
| 389 | err: | 401 | err: |
| 390 | free(s); | 402 | free(s); |
| 391 | return (ret); | 403 | |
| 404 | return ret; | ||
| 392 | } | 405 | } |
