1 // Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
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 #include <openssl/asn1.h>
16
17 #include <openssl/err.h>
18 #include <openssl/mem.h>
19
20
ASN1_item_pack(void * obj,const ASN1_ITEM * it,ASN1_STRING ** out)21 ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **out) {
22 uint8_t *new_data = NULL;
23 int len = ASN1_item_i2d(reinterpret_cast<ASN1_VALUE *>(obj), &new_data, it);
24 if (len <= 0) {
25 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ENCODE_ERROR);
26 return NULL;
27 }
28
29 ASN1_STRING *ret = NULL;
30 if (out == NULL || *out == NULL) {
31 ret = ASN1_STRING_new();
32 if (ret == NULL) {
33 OPENSSL_free(new_data);
34 return NULL;
35 }
36 } else {
37 ret = *out;
38 }
39
40 ASN1_STRING_set0(ret, new_data, len);
41 if (out != NULL) {
42 *out = ret;
43 }
44 return ret;
45 }
46
ASN1_item_unpack(const ASN1_STRING * oct,const ASN1_ITEM * it)47 void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it) {
48 const unsigned char *p = oct->data;
49 void *ret = ASN1_item_d2i(NULL, &p, oct->length, it);
50 if (ret == NULL || p != oct->data + oct->length) {
51 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
52 ASN1_item_free(reinterpret_cast<ASN1_VALUE *>(ret), it);
53 return NULL;
54 }
55 return ret;
56 }
57