| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/asn1.h> |
| 5 |
|
| 6 |
#include <limits.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/err.h> |
| 10 |
#include <openssl/mem.h> |
| 11 |
|
| 12 |
#include "../internal.h" |
| 13 |
#include "internal.h" |
| 14 |
|
| 15 |
|
| 16 |
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, |
| 17 |
ossl_ssize_t len) { |
| 18 |
return ASN1_STRING_set(x, d, len); |
| 19 |
} |
| 20 |
|
| 21 |
int asn1_bit_string_length(const ASN1_BIT_STRING *str, |
| 22 |
uint8_t *out_padding_bits) { |
| 23 |
int len = str->length; |
| 24 |
if (str->flags & ASN1_STRING_FLAG_BITS_LEFT) { |
| 25 |
// If the string is already empty, it cannot have padding bits. |
| 26 |
*out_padding_bits = len == 0 ? 0 : str->flags & 0x07; |
| 27 |
return len; |
| 28 |
} |
| 29 |
|
| 30 |
// TODO(https://crbug.com/boringssl/447): If we move this logic to |
| 31 |
// |ASN1_BIT_STRING_set_bit|, can we remove this representation? |
| 32 |
while (len > 0 && str->data[len - 1] == 0) { |
| 33 |
len--; |
| 34 |
} |
| 35 |
uint8_t padding_bits = 0; |
| 36 |
if (len > 0) { |
| 37 |
uint8_t last = str->data[len - 1]; |
| 38 |
assert(last != 0); |
| 39 |
for (; padding_bits < 7; padding_bits++) { |
| 40 |
if (last & (1 << padding_bits)) { |
| 41 |
break; |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
*out_padding_bits = padding_bits; |
| 46 |
return len; |
| 47 |
} |
| 48 |
|
| 49 |
int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, size_t *out) { |
| 50 |
uint8_t padding_bits; |
| 51 |
int len = asn1_bit_string_length(str, &padding_bits); |
| 52 |
if (padding_bits != 0) { |
| 53 |
return 0; |
| 54 |
} |
| 55 |
*out = len; |
| 56 |
return 1; |
| 57 |
} |
| 58 |
|
| 59 |
int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp) { |
| 60 |
if (a == NULL) { |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
uint8_t bits; |
| 65 |
int len = asn1_bit_string_length(a, &bits); |
| 66 |
if (len > INT_MAX - 1) { |
| 67 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW); |
| 68 |
return 0; |
| 69 |
} |
| 70 |
int ret = 1 + len; |
| 71 |
if (pp == NULL) { |
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
uint8_t *p = *pp; |
| 76 |
*(p++) = bits; |
| 77 |
OPENSSL_memcpy(p, a->data, len); |
| 78 |
if (len > 0) { |
| 79 |
p[len - 1] &= (0xff << bits); |
| 80 |
} |
| 81 |
p += len; |
| 82 |
*pp = p; |
| 83 |
return ret; |
| 84 |
} |
| 85 |
|
| 86 |
ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, |
| 87 |
const unsigned char **pp, long len) { |
| 88 |
ASN1_BIT_STRING *ret = NULL; |
| 89 |
const unsigned char *p; |
| 90 |
unsigned char *s; |
| 91 |
int padding; |
| 92 |
|
| 93 |
if (len < 1) { |
| 94 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT); |
| 95 |
goto err; |
| 96 |
} |
| 97 |
|
| 98 |
if (len > INT_MAX) { |
| 99 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG); |
| 100 |
goto err; |
| 101 |
} |
| 102 |
|
| 103 |
if ((a == NULL) || ((*a) == NULL)) { |
| 104 |
if ((ret = ASN1_BIT_STRING_new()) == NULL) { |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
} else { |
| 108 |
ret = (*a); |
| 109 |
} |
| 110 |
|
| 111 |
p = *pp; |
| 112 |
padding = *(p++); |
| 113 |
len--; |
| 114 |
if (padding > 7) { |
| 115 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT); |
| 116 |
goto err; |
| 117 |
} |
| 118 |
|
| 119 |
// Unused bits in a BIT STRING must be zero. |
| 120 |
uint8_t padding_mask = (1 << padding) - 1; |
| 121 |
if (padding != 0 && (len < 1 || (p[len - 1] & padding_mask) != 0)) { |
| 122 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_PADDING); |
| 123 |
goto err; |
| 124 |
} |
| 125 |
|
| 126 |
// We do this to preserve the settings. If we modify the settings, via |
| 127 |
// the _set_bit function, we will recalculate on output |
| 128 |
ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); // clear |
| 129 |
ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | padding); // set |
| 130 |
|
| 131 |
if (len > 0) { |
| 132 |
s = OPENSSL_memdup(p, len); |
| 133 |
if (s == NULL) { |
| 134 |
goto err; |
| 135 |
} |
| 136 |
p += len; |
| 137 |
} else { |
| 138 |
s = NULL; |
| 139 |
} |
| 140 |
|
| 141 |
ret->length = (int)len; |
| 142 |
OPENSSL_free(ret->data); |
| 143 |
ret->data = s; |
| 144 |
ret->type = V_ASN1_BIT_STRING; |
| 145 |
if (a != NULL) { |
| 146 |
(*a) = ret; |
| 147 |
} |
| 148 |
*pp = p; |
| 149 |
return ret; |
| 150 |
err: |
| 151 |
if ((ret != NULL) && ((a == NULL) || (*a != ret))) { |
| 152 |
ASN1_BIT_STRING_free(ret); |
| 153 |
} |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
|
| 157 |
// These next 2 functions from Goetz Babin-Ebell <[email protected]> |
| 158 |
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) { |
| 159 |
int w, v, iv; |
| 160 |
unsigned char *c; |
| 161 |
|
| 162 |
// n is an index, cannot be negative |
| 163 |
if (n < 0) { |
| 164 |
return 0; |
| 165 |
} |
| 166 |
w = n / 8; |
| 167 |
v = 1 << (7 - (n & 0x07)); |
| 168 |
iv = ~v; |
| 169 |
if (!value) { |
| 170 |
v = 0; |
| 171 |
} |
| 172 |
|
| 173 |
if (a == NULL) { |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); // clear, set on write |
| 178 |
|
| 179 |
if ((a->length < (w + 1)) || (a->data == NULL)) { |
| 180 |
if (!value) { |
| 181 |
return 1; // Don't need to set |
| 182 |
} |
| 183 |
if (a->data == NULL) { |
| 184 |
c = (unsigned char *)OPENSSL_malloc(w + 1); |
| 185 |
} else { |
| 186 |
c = (unsigned char *)OPENSSL_realloc(a->data, w + 1); |
| 187 |
} |
| 188 |
if (c == NULL) { |
| 189 |
return 0; |
| 190 |
} |
| 191 |
if (w + 1 - a->length > 0) { |
| 192 |
OPENSSL_memset(c + a->length, 0, w + 1 - a->length); |
| 193 |
} |
| 194 |
a->data = c; |
| 195 |
a->length = w + 1; |
| 196 |
} |
| 197 |
a->data[w] = ((a->data[w]) & iv) | v; |
| 198 |
while ((a->length > 0) && (a->data[a->length - 1] == 0)) { |
| 199 |
a->length--; |
| 200 |
} |
| 201 |
return 1; |
| 202 |
} |
| 203 |
|
| 204 |
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n) { |
| 205 |
int w, v; |
| 206 |
|
| 207 |
w = n / 8; |
| 208 |
v = 1 << (7 - (n & 0x07)); |
| 209 |
if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) { |
| 210 |
return 0; |
| 211 |
} |
| 212 |
return ((a->data[w] & v) != 0); |
| 213 |
} |
| 214 |
|
| 215 |
// Checks if the given bit string contains only bits specified by |
| 216 |
// the flags vector. Returns 0 if there is at least one bit set in 'a' |
| 217 |
// which is not specified in 'flags', 1 otherwise. |
| 218 |
// 'len' is the length of 'flags'. |
| 219 |
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, const unsigned char *flags, |
| 220 |
int flags_len) { |
| 221 |
int i, ok; |
| 222 |
// Check if there is one bit set at all. |
| 223 |
if (!a || !a->data) { |
| 224 |
return 1; |
| 225 |
} |
| 226 |
|
| 227 |
// Check each byte of the internal representation of the bit string. |
| 228 |
ok = 1; |
| 229 |
for (i = 0; i < a->length && ok; ++i) { |
| 230 |
unsigned char mask = i < flags_len ? ~flags[i] : 0xff; |
| 231 |
// We are done if there is an unneeded bit set. |
| 232 |
ok = (a->data[i] & mask) == 0; |
| 233 |
} |
| 234 |
return ok; |
| 235 |
} |