/src/boringssl/crypto/bytestring/asn1_compat.cc
Line | Count | Source |
1 | | // Copyright 2016 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
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 | | #include "../internal.h" |
26 | | |
27 | | |
28 | | using namespace bssl; |
29 | | |
30 | 72.9k | int bssl::CBB_finish_i2d(CBB *cbb, uint8_t **outp) { |
31 | 72.9k | assert(!cbb->is_child); |
32 | 72.9k | assert(cbb->u.base.can_resize); |
33 | | |
34 | 72.9k | uint8_t *der; |
35 | 72.9k | size_t der_len; |
36 | 72.9k | if (!CBB_finish(cbb, &der, &der_len)) { |
37 | 0 | CBB_cleanup(cbb); |
38 | 0 | return -1; |
39 | 0 | } |
40 | 72.9k | if (der_len > INT_MAX) { |
41 | 0 | OPENSSL_free(der); |
42 | 0 | return -1; |
43 | 0 | } |
44 | 72.9k | if (outp != nullptr) { |
45 | 49.7k | if (*outp == nullptr) { |
46 | 45.0k | *outp = der; |
47 | 45.0k | der = nullptr; |
48 | 45.0k | } else { |
49 | 4.64k | OPENSSL_memcpy(*outp, der, der_len); |
50 | 4.64k | *outp += der_len; |
51 | 4.64k | } |
52 | 49.7k | } |
53 | 72.9k | OPENSSL_free(der); |
54 | 72.9k | return (int)der_len; |
55 | 72.9k | } |