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