| 1 |
/* Copyright (c) 2016, Google Inc. |
| 2 |
* |
| 3 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
* purpose with or without fee is hereby granted, provided that the above |
| 5 |
* copyright notice and this permission notice appear in all copies. |
| 6 |
* |
| 7 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 |
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 |
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 |
|
| 15 |
|
| 16 |
#include <openssl/bytestring.h> |
| 17 |
|
| 18 |
#include <assert.h> |
| 19 |
#include <limits.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <openssl/mem.h> |
| 23 |
|
| 24 |
#include "internal.h" |
| 25 |
|
| 26 |
|
| 27 |
int CBB_finish_i2d(CBB *cbb, uint8_t **outp) { |
| 28 |
assert(cbb->base->can_resize); |
| 29 |
|
| 30 |
uint8_t *der; |
| 31 |
size_t der_len; |
| 32 |
if (!CBB_finish(cbb, &der, &der_len)) { |
| 33 |
CBB_cleanup(cbb); |
| 34 |
return -1; |
| 35 |
} |
| 36 |
if (der_len > INT_MAX) { |
| 37 |
OPENSSL_free(der); |
| 38 |
return -1; |
| 39 |
} |
| 40 |
if (outp != NULL) { |
| 41 |
if (*outp == NULL) { |
| 42 |
*outp = der; |
| 43 |
der = NULL; |
| 44 |
} else { |
| 45 |
memcpy(*outp, der, der_len); |
| 46 |
*outp += der_len; |
| 47 |
} |
| 48 |
} |
| 49 |
OPENSSL_free(der); |
| 50 |
return (int)der_len; |
| 51 |
} |