1 /*
2 * Copyright 2003-2022 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 "e_os.h"
11 #include <string.h>
12 #include <limits.h>
13 #include <openssl/crypto.h>
14 #include "crypto/ctype.h"
15 #include "internal/cryptlib.h"
16 #include "internal/thread_once.h"
17
18 #define DEFAULT_SEPARATOR ':'
19 #define CH_ZERO '\0'
20
CRYPTO_strdup(const char * str,const char * file,int line)21 char *CRYPTO_strdup(const char *str, const char* file, int line)
22 {
23 char *ret;
24
25 if (str == NULL)
26 return NULL;
27 ret = CRYPTO_malloc(strlen(str) + 1, file, line);
28 if (ret != NULL)
29 strcpy(ret, str);
30 return ret;
31 }
32
CRYPTO_strndup(const char * str,size_t s,const char * file,int line)33 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
34 {
35 size_t maxlen;
36 char *ret;
37
38 if (str == NULL)
39 return NULL;
40
41 maxlen = OPENSSL_strnlen(str, s);
42
43 ret = CRYPTO_malloc(maxlen + 1, file, line);
44 if (ret) {
45 memcpy(ret, str, maxlen);
46 ret[maxlen] = CH_ZERO;
47 }
48 return ret;
49 }
50
CRYPTO_memdup(const void * data,size_t siz,const char * file,int line)51 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
52 {
53 void *ret;
54
55 if (data == NULL || siz >= INT_MAX)
56 return NULL;
57
58 ret = CRYPTO_malloc(siz, file, line);
59 if (ret == NULL) {
60 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
61 return NULL;
62 }
63 return memcpy(ret, data, siz);
64 }
65
OPENSSL_strnlen(const char * str,size_t maxlen)66 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
67 {
68 const char *p;
69
70 for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
71
72 return p - str;
73 }
74
OPENSSL_strlcpy(char * dst,const char * src,size_t size)75 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
76 {
77 size_t l = 0;
78 for (; size > 1 && *src; size--) {
79 *dst++ = *src++;
80 l++;
81 }
82 if (size)
83 *dst = CH_ZERO;
84 return l + strlen(src);
85 }
86
OPENSSL_strlcat(char * dst,const char * src,size_t size)87 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
88 {
89 size_t l = 0;
90 for (; size > 0 && *dst; size--, dst++)
91 l++;
92 return l + OPENSSL_strlcpy(dst, src, size);
93 }
94
OPENSSL_hexchar2int(unsigned char c)95 int OPENSSL_hexchar2int(unsigned char c)
96 {
97 #ifdef CHARSET_EBCDIC
98 c = os_toebcdic[c];
99 #endif
100
101 switch (c) {
102 case '0':
103 return 0;
104 case '1':
105 return 1;
106 case '2':
107 return 2;
108 case '3':
109 return 3;
110 case '4':
111 return 4;
112 case '5':
113 return 5;
114 case '6':
115 return 6;
116 case '7':
117 return 7;
118 case '8':
119 return 8;
120 case '9':
121 return 9;
122 case 'a': case 'A':
123 return 0x0A;
124 case 'b': case 'B':
125 return 0x0B;
126 case 'c': case 'C':
127 return 0x0C;
128 case 'd': case 'D':
129 return 0x0D;
130 case 'e': case 'E':
131 return 0x0E;
132 case 'f': case 'F':
133 return 0x0F;
134 }
135 return -1;
136 }
137
hexstr2buf_sep(unsigned char * buf,size_t buf_n,size_t * buflen,const char * str,const char sep)138 static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
139 const char *str, const char sep)
140 {
141 unsigned char *q;
142 unsigned char ch, cl;
143 int chi, cli;
144 const unsigned char *p;
145 size_t cnt;
146
147 for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
148 ch = *p++;
149 /* A separator of CH_ZERO means there is no separator */
150 if (ch == sep && sep != CH_ZERO)
151 continue;
152 cl = *p++;
153 if (!cl) {
154 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
155 return 0;
156 }
157 cli = OPENSSL_hexchar2int(cl);
158 chi = OPENSSL_hexchar2int(ch);
159 if (cli < 0 || chi < 0) {
160 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
161 return 0;
162 }
163 cnt++;
164 if (q != NULL) {
165 if (cnt > buf_n) {
166 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
167 return 0;
168 }
169 *q++ = (unsigned char)((chi << 4) | cli);
170 }
171 }
172
173 if (buflen != NULL)
174 *buflen = cnt;
175 return 1;
176 }
177
178 /*
179 * Given a string of hex digits convert to a buffer
180 */
OPENSSL_hexstr2buf_ex(unsigned char * buf,size_t buf_n,size_t * buflen,const char * str,const char sep)181 int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
182 const char *str, const char sep)
183 {
184 return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
185 }
186
ossl_hexstr2buf_sep(const char * str,long * buflen,const char sep)187 unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
188 const char sep)
189 {
190 unsigned char *buf;
191 size_t buf_n, tmp_buflen;
192
193 buf_n = strlen(str);
194 if (buf_n <= 1) {
195 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
196 return NULL;
197 }
198 buf_n /= 2;
199 if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
200 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
201 return NULL;
202 }
203
204 if (buflen != NULL)
205 *buflen = 0;
206 tmp_buflen = 0;
207 if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
208 if (buflen != NULL)
209 *buflen = (long)tmp_buflen;
210 return buf;
211 }
212 OPENSSL_free(buf);
213 return NULL;
214 }
215
OPENSSL_hexstr2buf(const char * str,long * buflen)216 unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
217 {
218 return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
219 }
220
buf2hexstr_sep(char * str,size_t str_n,size_t * strlength,const unsigned char * buf,size_t buflen,const char sep)221 static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
222 const unsigned char *buf, size_t buflen,
223 const char sep)
224 {
225 static const char hexdig[] = "0123456789ABCDEF";
226 const unsigned char *p;
227 char *q;
228 size_t i;
229 int has_sep = (sep != CH_ZERO);
230 size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
231
232 if (strlength != NULL)
233 *strlength = len;
234 if (str == NULL)
235 return 1;
236
237 if (str_n < (unsigned long)len) {
238 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
239 return 0;
240 }
241
242 q = str;
243 for (i = 0, p = buf; i < buflen; i++, p++) {
244 *q++ = hexdig[(*p >> 4) & 0xf];
245 *q++ = hexdig[*p & 0xf];
246 if (has_sep)
247 *q++ = sep;
248 }
249 if (has_sep)
250 --q;
251 *q = CH_ZERO;
252
253 #ifdef CHARSET_EBCDIC
254 ebcdic2ascii(str, str, q - str - 1);
255 #endif
256 return 1;
257 }
258
OPENSSL_buf2hexstr_ex(char * str,size_t str_n,size_t * strlength,const unsigned char * buf,size_t buflen,const char sep)259 int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
260 const unsigned char *buf, size_t buflen,
261 const char sep)
262 {
263 return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
264 }
265
ossl_buf2hexstr_sep(const unsigned char * buf,long buflen,char sep)266 char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
267 {
268 char *tmp;
269 size_t tmp_n;
270
271 if (buflen == 0)
272 return OPENSSL_zalloc(1);
273
274 tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
275 if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) {
276 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
277 return NULL;
278 }
279
280 if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
281 return tmp;
282 OPENSSL_free(tmp);
283 return NULL;
284 }
285
286
287 /*
288 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
289 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
290 * on EBCDIC machines)
291 */
OPENSSL_buf2hexstr(const unsigned char * buf,long buflen)292 char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
293 {
294 return ossl_buf2hexstr_sep(buf, buflen, ':');
295 }
296
openssl_strerror_r(int errnum,char * buf,size_t buflen)297 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
298 {
299 #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
300 return !strerror_s(buf, buflen, errnum);
301 #elif defined(_GNU_SOURCE)
302 char *err;
303
304 /*
305 * GNU strerror_r may not actually set buf.
306 * It can return a pointer to some (immutable) static string in which case
307 * buf is left unused.
308 */
309 err = strerror_r(errnum, buf, buflen);
310 if (err == NULL || buflen == 0)
311 return 0;
312 /*
313 * If err is statically allocated, err != buf and we need to copy the data.
314 * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
315 * since src and dest are not annotated with __restrict and the function
316 * reads src byte for byte and writes to dest.
317 * If err == buf we do not have to copy anything.
318 */
319 if (err != buf)
320 OPENSSL_strlcpy(buf, err, buflen);
321 return 1;
322 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
323 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
324 /*
325 * We can use "real" strerror_r. The OpenSSL version differs in that it
326 * gives 1 on success and 0 on failure for consistency with other OpenSSL
327 * functions. Real strerror_r does it the other way around
328 */
329 return !strerror_r(errnum, buf, buflen);
330 #else
331 char *err;
332
333 /* Fall back to non-thread safe strerror()...its all we can do */
334 if (buflen < 2)
335 return 0;
336 err = strerror(errnum);
337 /* Can this ever happen? */
338 if (err == NULL)
339 return 0;
340 OPENSSL_strlcpy(buf, err, buflen);
341 return 1;
342 #endif
343 }
344
OPENSSL_strcasecmp(const char * s1,const char * s2)345 int OPENSSL_strcasecmp(const char *s1, const char *s2)
346 {
347 int t;
348
349 while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
350 if (*s1++ == '\0')
351 return 0;
352 return t;
353 }
354
OPENSSL_strncasecmp(const char * s1,const char * s2,size_t n)355 int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
356 {
357 int t;
358 size_t i;
359
360 for (i = 0; i < n; i++)
361 if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
362 return t;
363 else if (*s1++ == '\0')
364 return 0;
365 return 0;
366 }
367