1 /* $OpenBSD: x_long.c,v 1.21 2024/07/08 16:24:22 beck Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 2000. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #include <limits.h> 60 #include <string.h> 61 62 #include <openssl/asn1t.h> 63 #include <openssl/bn.h> 64 #include <openssl/err.h> 65 66 #include "asn1_local.h" 67 68 /* 69 * Custom primitive type for long handling. This converts between an 70 * ASN1_INTEGER and a long directly. 71 */ 72 73 static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it); 74 static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 75 static void long_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); 76 77 static int long_i2c(ASN1_VALUE **pval, unsigned char *content, int *putype, 78 const ASN1_ITEM *it); 79 static int long_c2i(ASN1_VALUE **pval, const unsigned char *content, int len, 80 int utype, char *free_content, const ASN1_ITEM *it); 81 static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, 82 int indent, const ASN1_PCTX *pctx); 83 84 static const ASN1_PRIMITIVE_FUNCS long_pf = { 85 .app_data = NULL, 86 .flags = 0, 87 .prim_new = long_new, 88 .prim_free = long_free, 89 .prim_clear = long_clear, 90 .prim_c2i = long_c2i, 91 .prim_i2c = long_i2c, 92 .prim_print = long_print, 93 }; 94 95 const ASN1_ITEM LONG_it = { 96 .itype = ASN1_ITYPE_PRIMITIVE, 97 .utype = V_ASN1_INTEGER, 98 .templates = NULL, 99 .tcount = 0, 100 .funcs = &long_pf, 101 .size = ASN1_LONG_UNDEF, 102 .sname = "LONG", 103 }; 104 LCRYPTO_ALIAS(LONG_it); 105 106 const ASN1_ITEM ZLONG_it = { 107 .itype = ASN1_ITYPE_PRIMITIVE, 108 .utype = V_ASN1_INTEGER, 109 .templates = NULL, 110 .tcount = 0, 111 .funcs = &long_pf, 112 .size = 0, 113 .sname = "ZLONG", 114 }; 115 LCRYPTO_ALIAS(ZLONG_it); 116 117 static void 118 long_get(ASN1_VALUE **pval, long *out_val) 119 { 120 memcpy(out_val, pval, sizeof(long)); 121 } 122 123 static void 124 long_set(ASN1_VALUE **pval, long val) 125 { 126 memcpy(pval, &val, sizeof(long)); 127 } 128 129 static int 130 long_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 131 { 132 long_clear(pval, it); 133 134 return 1; 135 } 136 137 static void 138 long_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 139 { 140 long_clear(pval, it); 141 } 142 143 static void 144 long_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) 145 { 146 /* Zero value. */ 147 long_set(pval, it->size); 148 } 149 150 static int 151 long_i2c(ASN1_VALUE **pval, unsigned char *content, int *putype, 152 const ASN1_ITEM *it) 153 { 154 ASN1_INTEGER *aint; 155 uint8_t **pp = NULL; 156 long val; 157 int ret = 0; 158 159 long_get(pval, &val); 160 161 /* 162 * The zero value for this type (stored in the overloaded it->size 163 * field) is considered to be invalid. 164 */ 165 if (val == it->size) 166 return -1; 167 168 if ((aint = ASN1_INTEGER_new()) == NULL) 169 goto err; 170 if (!ASN1_INTEGER_set_int64(aint, (int64_t)val)) 171 goto err; 172 if (content != NULL) 173 pp = &content; 174 ret = i2c_ASN1_INTEGER(aint, pp); 175 176 err: 177 ASN1_INTEGER_free(aint); 178 179 return ret; 180 } 181 182 static int 183 long_c2i(ASN1_VALUE **pval, const unsigned char *content, int len, int utype, 184 char *free_content, const ASN1_ITEM *it) 185 { 186 ASN1_INTEGER *aint = NULL; 187 const uint8_t **pp = NULL; 188 int64_t val = 0; 189 int ret = 0; 190 191 /* 192 * The original long_i2c() mishandled 0 values and encoded them as 193 * content with zero length, rather than a single zero byte. Permit 194 * zero length content here for backwards compatibility. 195 */ 196 if (len != 0) { 197 if (content != NULL) 198 pp = &content; 199 if (!c2i_ASN1_INTEGER(&aint, pp, len)) 200 goto err; 201 if (!ASN1_INTEGER_get_int64(&val, aint)) 202 goto err; 203 } 204 205 if (val < LONG_MIN || val > LONG_MAX) { 206 ASN1error(ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); 207 goto err; 208 } 209 210 /* 211 * The zero value for this type (stored in the overloaded it->size 212 * field) is considered to be invalid. 213 */ 214 if (val == (int64_t)it->size) { 215 ASN1error(ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); 216 goto err; 217 } 218 219 long_set(pval, (long)val); 220 221 ret = 1; 222 223 err: 224 ASN1_INTEGER_free(aint); 225 226 return ret; 227 } 228 229 static int 230 long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, 231 const ASN1_PCTX *pctx) 232 { 233 long val; 234 235 long_get(pval, &val); 236 237 if (BIO_printf(out, "%ld\n", val) <= 0) 238 return 0; 239 240 return 1; 241 } 242