sshbuf-getput-basic.c (294328) | sshbuf-getput-basic.c (294332) |
---|---|
1/* $OpenBSD: sshbuf-getput-basic.c,v 1.1 2014/04/30 05:29:56 djm Exp $ */ | 1/* $OpenBSD: sshbuf-getput-basic.c,v 1.4 2015/01/14 15:02:39 djm Exp $ */ |
2/* 3 * Copyright (c) 2011 Damien Miller 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --- 19 unchanged lines hidden (view full) --- 29int 30sshbuf_get(struct sshbuf *buf, void *v, size_t len) 31{ 32 const u_char *p = sshbuf_ptr(buf); 33 int r; 34 35 if ((r = sshbuf_consume(buf, len)) < 0) 36 return r; | 2/* 3 * Copyright (c) 2011 Damien Miller 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --- 19 unchanged lines hidden (view full) --- 29int 30sshbuf_get(struct sshbuf *buf, void *v, size_t len) 31{ 32 const u_char *p = sshbuf_ptr(buf); 33 int r; 34 35 if ((r = sshbuf_consume(buf, len)) < 0) 36 return r; |
37 if (v != NULL) | 37 if (v != NULL && len != 0) |
38 memcpy(v, p, len); 39 return 0; 40} 41 42int 43sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp) 44{ 45 const u_char *p = sshbuf_ptr(buf); --- 58 unchanged lines hidden (view full) --- 104 *lenp = 0; 105 if ((r = sshbuf_get_string_direct(buf, &val, &len)) < 0) 106 return r; 107 if (valp != NULL) { 108 if ((*valp = malloc(len + 1)) == NULL) { 109 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); 110 return SSH_ERR_ALLOC_FAIL; 111 } | 38 memcpy(v, p, len); 39 return 0; 40} 41 42int 43sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp) 44{ 45 const u_char *p = sshbuf_ptr(buf); --- 58 unchanged lines hidden (view full) --- 104 *lenp = 0; 105 if ((r = sshbuf_get_string_direct(buf, &val, &len)) < 0) 106 return r; 107 if (valp != NULL) { 108 if ((*valp = malloc(len + 1)) == NULL) { 109 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); 110 return SSH_ERR_ALLOC_FAIL; 111 } |
112 memcpy(*valp, val, len); | 112 if (len != 0) 113 memcpy(*valp, val, len); |
113 (*valp)[len] = '\0'; 114 } 115 if (lenp != NULL) 116 *lenp = len; 117 return 0; 118} 119 120int --- 74 unchanged lines hidden (view full) --- 195 } 196 if ((r = sshbuf_skip_string(buf)) != 0) 197 return -1; 198 if (valp != NULL) { 199 if ((*valp = malloc(len + 1)) == NULL) { 200 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); 201 return SSH_ERR_ALLOC_FAIL; 202 } | 114 (*valp)[len] = '\0'; 115 } 116 if (lenp != NULL) 117 *lenp = len; 118 return 0; 119} 120 121int --- 74 unchanged lines hidden (view full) --- 196 } 197 if ((r = sshbuf_skip_string(buf)) != 0) 198 return -1; 199 if (valp != NULL) { 200 if ((*valp = malloc(len + 1)) == NULL) { 201 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); 202 return SSH_ERR_ALLOC_FAIL; 203 } |
203 memcpy(*valp, p, len); | 204 if (len != 0) 205 memcpy(*valp, p, len); |
204 (*valp)[len] = '\0'; 205 } 206 if (lenp != NULL) 207 *lenp = (size_t)len; 208 return 0; 209} 210 211int --- 19 unchanged lines hidden (view full) --- 231int 232sshbuf_put(struct sshbuf *buf, const void *v, size_t len) 233{ 234 u_char *p; 235 int r; 236 237 if ((r = sshbuf_reserve(buf, len, &p)) < 0) 238 return r; | 206 (*valp)[len] = '\0'; 207 } 208 if (lenp != NULL) 209 *lenp = (size_t)len; 210 return 0; 211} 212 213int --- 19 unchanged lines hidden (view full) --- 233int 234sshbuf_put(struct sshbuf *buf, const void *v, size_t len) 235{ 236 u_char *p; 237 int r; 238 239 if ((r = sshbuf_reserve(buf, len, &p)) < 0) 240 return r; |
239 memcpy(p, v, len); | 241 if (len != 0) 242 memcpy(p, v, len); |
240 return 0; 241} 242 243int 244sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v) 245{ 246 return sshbuf_put(buf, sshbuf_ptr(v), sshbuf_len(v)); 247} --- 99 unchanged lines hidden (view full) --- 347 348 if (len > SSHBUF_SIZE_MAX - 4) { 349 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE")); 350 return SSH_ERR_NO_BUFFER_SPACE; 351 } 352 if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0) 353 return r; 354 POKE_U32(d, len); | 243 return 0; 244} 245 246int 247sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v) 248{ 249 return sshbuf_put(buf, sshbuf_ptr(v), sshbuf_len(v)); 250} --- 99 unchanged lines hidden (view full) --- 350 351 if (len > SSHBUF_SIZE_MAX - 4) { 352 SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE")); 353 return SSH_ERR_NO_BUFFER_SPACE; 354 } 355 if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0) 356 return r; 357 POKE_U32(d, len); |
355 memcpy(d + 4, v, len); | 358 if (len != 0) 359 memcpy(d + 4, v, len); |
356 return 0; 357} 358 359int 360sshbuf_put_cstring(struct sshbuf *buf, const char *v) 361{ | 360 return 0; 361} 362 363int 364sshbuf_put_cstring(struct sshbuf *buf, const char *v) 365{ |
362 return sshbuf_put_string(buf, (u_char *)v, strlen(v)); | 366 return sshbuf_put_string(buf, (u_char *)v, v == NULL ? 0 : strlen(v)); |
363} 364 365int 366sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v) 367{ 368 return sshbuf_put_string(buf, sshbuf_ptr(v), sshbuf_len(v)); 369} 370 --- 40 unchanged lines hidden (view full) --- 411 * avoid interpretation as a negative number. 412 */ 413 prepend = len > 0 && (s[0] & 0x80) != 0; 414 if ((r = sshbuf_reserve(buf, len + 4 + prepend, &d)) < 0) 415 return r; 416 POKE_U32(d, len + prepend); 417 if (prepend) 418 d[4] = 0; | 367} 368 369int 370sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v) 371{ 372 return sshbuf_put_string(buf, sshbuf_ptr(v), sshbuf_len(v)); 373} 374 --- 40 unchanged lines hidden (view full) --- 415 * avoid interpretation as a negative number. 416 */ 417 prepend = len > 0 && (s[0] & 0x80) != 0; 418 if ((r = sshbuf_reserve(buf, len + 4 + prepend, &d)) < 0) 419 return r; 420 POKE_U32(d, len + prepend); 421 if (prepend) 422 d[4] = 0; |
419 memcpy(d + 4 + prepend, s, len); | 423 if (len != 0) 424 memcpy(d + 4 + prepend, s, len); |
420 return 0; 421} | 425 return 0; 426} |
427 428int 429sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf, 430 const u_char **valp, size_t *lenp) 431{ 432 const u_char *d; 433 size_t len, olen; 434 int r; 435 436 if ((r = sshbuf_peek_string_direct(buf, &d, &olen)) < 0) 437 return r; 438 len = olen; 439 /* Refuse negative (MSB set) bignums */ 440 if ((len != 0 && (*d & 0x80) != 0)) 441 return SSH_ERR_BIGNUM_IS_NEGATIVE; 442 /* Refuse overlong bignums, allow prepended \0 to avoid MSB set */ 443 if (len > SSHBUF_MAX_BIGNUM + 1 || 444 (len == SSHBUF_MAX_BIGNUM + 1 && *d != 0)) 445 return SSH_ERR_BIGNUM_TOO_LARGE; 446 /* Trim leading zeros */ 447 while (len > 0 && *d == 0x00) { 448 d++; 449 len--; 450 } 451 if (valp != 0) 452 *valp = d; 453 if (lenp != NULL) 454 *lenp = len; 455 if (sshbuf_consume(buf, olen + 4) != 0) { 456 /* Shouldn't happen */ 457 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR")); 458 SSHBUF_ABORT(); 459 return SSH_ERR_INTERNAL_ERROR; 460 } 461 return 0; 462} |
|