| 1 |
/* |
| 2 |
* Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include "internal/cryptlib.h" |
| 12 |
#include <openssl/asn1.h> |
| 13 |
#include <openssl/asn1t.h> |
| 14 |
#include "crypto/asn1.h" |
| 15 |
|
| 16 |
int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len) |
| 17 |
{ |
| 18 |
ASN1_STRING *os; |
| 19 |
|
| 20 |
if ((os = ASN1_OCTET_STRING_new()) == NULL) |
| 21 |
return 0; |
| 22 |
if (!ASN1_OCTET_STRING_set(os, data, len)) { |
| 23 |
ASN1_OCTET_STRING_free(os); |
| 24 |
return 0; |
| 25 |
} |
| 26 |
ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os); |
| 27 |
return 1; |
| 28 |
} |
| 29 |
|
| 30 |
/* int max_len: for returned value |
| 31 |
* if passing NULL in data, nothing is copied but the necessary length |
| 32 |
* for it is returned. |
| 33 |
*/ |
| 34 |
int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len) |
| 35 |
{ |
| 36 |
int ret, num; |
| 37 |
const unsigned char *p; |
| 38 |
|
| 39 |
if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) { |
| 40 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG); |
| 41 |
return -1; |
| 42 |
} |
| 43 |
p = ASN1_STRING_get0_data(a->value.octet_string); |
| 44 |
ret = ASN1_STRING_length(a->value.octet_string); |
| 45 |
if (ret < max_len) |
| 46 |
num = ret; |
| 47 |
else |
| 48 |
num = max_len; |
| 49 |
if (num > 0 && data != NULL) |
| 50 |
memcpy(data, p, num); |
| 51 |
return ret; |
| 52 |
} |
| 53 |
|
| 54 |
static ossl_inline void asn1_type_init_oct(ASN1_OCTET_STRING *oct, |
| 55 |
unsigned char *data, int len) |
| 56 |
{ |
| 57 |
oct->data = data; |
| 58 |
oct->type = V_ASN1_OCTET_STRING; |
| 59 |
oct->length = len; |
| 60 |
oct->flags = 0; |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* This function copies 'anum' to 'num' and the data of 'oct' to 'data'. |
| 65 |
* If the length of 'data' > 'max_len', copies only the first 'max_len' |
| 66 |
* bytes, but returns the full length of 'oct'; this allows distinguishing |
| 67 |
* whether all the data was copied. |
| 68 |
*/ |
| 69 |
static int asn1_type_get_int_oct(ASN1_OCTET_STRING *oct, int32_t anum, |
| 70 |
long *num, unsigned char *data, int max_len) |
| 71 |
{ |
| 72 |
int ret = ASN1_STRING_length(oct), n; |
| 73 |
|
| 74 |
if (num != NULL) |
| 75 |
*num = anum; |
| 76 |
|
| 77 |
if (max_len > ret) |
| 78 |
n = ret; |
| 79 |
else |
| 80 |
n = max_len; |
| 81 |
|
| 82 |
if (data != NULL) |
| 83 |
memcpy(data, ASN1_STRING_get0_data(oct), n); |
| 84 |
|
| 85 |
return ret; |
| 86 |
} |
| 87 |
|
| 88 |
typedef struct { |
| 89 |
int32_t num; |
| 90 |
ASN1_OCTET_STRING *oct; |
| 91 |
} asn1_int_oct; |
| 92 |
|
| 93 |
ASN1_SEQUENCE(asn1_int_oct) = { |
| 94 |
ASN1_EMBED(asn1_int_oct, num, INT32), |
| 95 |
ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING) |
| 96 |
} static_ASN1_SEQUENCE_END(asn1_int_oct) |
| 97 |
|
| 98 |
DECLARE_ASN1_ITEM(asn1_int_oct) |
| 99 |
|
| 100 |
int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data, |
| 101 |
int len) |
| 102 |
{ |
| 103 |
asn1_int_oct atmp; |
| 104 |
ASN1_OCTET_STRING oct; |
| 105 |
|
| 106 |
atmp.num = num; |
| 107 |
atmp.oct = &oct; |
| 108 |
asn1_type_init_oct(&oct, data, len); |
| 109 |
|
| 110 |
if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a)) |
| 111 |
return 1; |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* This function decodes an int-octet sequence and copies the integer to 'num' |
| 117 |
* and the data of octet to 'data'. |
| 118 |
* If the length of 'data' > 'max_len', copies only the first 'max_len' |
| 119 |
* bytes, but returns the full length of 'oct'; this allows distinguishing |
| 120 |
* whether all the data was copied. |
| 121 |
*/ |
| 122 |
int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num, |
| 123 |
unsigned char *data, int max_len) |
| 124 |
{ |
| 125 |
asn1_int_oct *atmp = NULL; |
| 126 |
int ret = -1; |
| 127 |
|
| 128 |
if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) { |
| 129 |
goto err; |
| 130 |
} |
| 131 |
|
| 132 |
atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a); |
| 133 |
|
| 134 |
if (atmp == NULL) |
| 135 |
goto err; |
| 136 |
|
| 137 |
ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len); |
| 138 |
|
| 139 |
if (ret == -1) { |
| 140 |
err: |
| 141 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG); |
| 142 |
} |
| 143 |
M_ASN1_free_of(atmp, asn1_int_oct); |
| 144 |
return ret; |
| 145 |
} |
| 146 |
|
| 147 |
typedef struct { |
| 148 |
ASN1_OCTET_STRING *oct; |
| 149 |
int32_t num; |
| 150 |
} asn1_oct_int; |
| 151 |
|
| 152 |
/* |
| 153 |
* Defined in RFC 5084 - |
| 154 |
* Section 2. "Content-Authenticated Encryption Algorithms" |
| 155 |
*/ |
| 156 |
ASN1_SEQUENCE(asn1_oct_int) = { |
| 157 |
ASN1_SIMPLE(asn1_oct_int, oct, ASN1_OCTET_STRING), |
| 158 |
ASN1_EMBED(asn1_oct_int, num, INT32) |
| 159 |
} static_ASN1_SEQUENCE_END(asn1_oct_int) |
| 160 |
|
| 161 |
DECLARE_ASN1_ITEM(asn1_oct_int) |
| 162 |
|
| 163 |
int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num, |
| 164 |
unsigned char *data, int len) |
| 165 |
{ |
| 166 |
asn1_oct_int atmp; |
| 167 |
ASN1_OCTET_STRING oct; |
| 168 |
|
| 169 |
atmp.num = num; |
| 170 |
atmp.oct = &oct; |
| 171 |
asn1_type_init_oct(&oct, data, len); |
| 172 |
|
| 173 |
if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_oct_int), &atmp, &a)) |
| 174 |
return 1; |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
/* |
| 179 |
* This function decodes an octet-int sequence and copies the data of octet |
| 180 |
* to 'data' and the integer to 'num'. |
| 181 |
* If the length of 'data' > 'max_len', copies only the first 'max_len' |
| 182 |
* bytes, but returns the full length of 'oct'; this allows distinguishing |
| 183 |
* whether all the data was copied. |
| 184 |
*/ |
| 185 |
int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num, |
| 186 |
unsigned char *data, int max_len) |
| 187 |
{ |
| 188 |
asn1_oct_int *atmp = NULL; |
| 189 |
int ret = -1; |
| 190 |
|
| 191 |
if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) |
| 192 |
goto err; |
| 193 |
|
| 194 |
atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_oct_int), a); |
| 195 |
|
| 196 |
if (atmp == NULL) |
| 197 |
goto err; |
| 198 |
|
| 199 |
ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len); |
| 200 |
|
| 201 |
if (ret == -1) { |
| 202 |
err: |
| 203 |
ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG); |
| 204 |
} |
| 205 |
M_ASN1_free_of(atmp, asn1_oct_int); |
| 206 |
return ret; |
| 207 |
} |