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 
CBB_finish_i2d(CBB * cbb,uint8_t ** outp)28 int CBB_finish_i2d(CBB *cbb, uint8_t **outp) {
29   assert(!cbb->is_child);
30   assert(cbb->u.base.can_resize);
31 
32   uint8_t *der;
33   size_t der_len;
34   if (!CBB_finish(cbb, &der, &der_len)) {
35     CBB_cleanup(cbb);
36     return -1;
37   }
38   if (der_len > INT_MAX) {
39     OPENSSL_free(der);
40     return -1;
41   }
42   if (outp != NULL) {
43     if (*outp == NULL) {
44       *outp = der;
45       der = NULL;
46     } else {
47       OPENSSL_memcpy(*outp, der, der_len);
48       *outp += der_len;
49     }
50   }
51   OPENSSL_free(der);
52   return (int)der_len;
53 }
54