]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
2039c421 | 2 | * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved. |
cfcefcbe | 3 | * |
2039c421 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
cfcefcbe DSH |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 | 12 | #include <openssl/asn1.h> |
cfcefcbe DSH |
13 | |
14 | /* ASN1 packing and unpacking functions */ | |
15 | ||
ecbe0781 | 16 | ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct) |
09ab755c | 17 | { |
0f113f3e MC |
18 | ASN1_STRING *octmp; |
19 | ||
69e2bd32 | 20 | if (oct == NULL || *oct == NULL) { |
75ebbd9a | 21 | if ((octmp = ASN1_STRING_new()) == NULL) { |
0f113f3e MC |
22 | ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE); |
23 | return NULL; | |
24 | } | |
69e2bd32 | 25 | } else { |
0f113f3e | 26 | octmp = *oct; |
69e2bd32 | 27 | } |
0f113f3e | 28 | |
b548a1f1 RS |
29 | OPENSSL_free(octmp->data); |
30 | octmp->data = NULL; | |
0f113f3e | 31 | |
75ebbd9a | 32 | if ((octmp->length = ASN1_item_i2d(obj, &octmp->data, it)) == 0) { |
0f113f3e | 33 | ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR); |
69e2bd32 | 34 | goto err; |
0f113f3e | 35 | } |
69e2bd32 | 36 | if (octmp->data == NULL) { |
0f113f3e | 37 | ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE); |
69e2bd32 | 38 | goto err; |
0f113f3e | 39 | } |
69e2bd32 MC |
40 | |
41 | if (oct != NULL && *oct == NULL) | |
42 | *oct = octmp; | |
43 | ||
0f113f3e | 44 | return octmp; |
69e2bd32 MC |
45 | err: |
46 | if (oct == NULL || *oct == NULL) | |
47 | ASN1_STRING_free(octmp); | |
48 | return NULL; | |
09ab755c DSH |
49 | } |
50 | ||
51 | /* Extract an ASN1 object from an ASN1_STRING */ | |
52 | ||
ecbe0781 | 53 | void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it) |
09ab755c | 54 | { |
0f113f3e MC |
55 | const unsigned char *p; |
56 | void *ret; | |
09ab755c | 57 | |
0f113f3e | 58 | p = oct->data; |
75ebbd9a | 59 | if ((ret = ASN1_item_d2i(NULL, &p, oct->length, it)) == NULL) |
0f113f3e MC |
60 | ASN1err(ASN1_F_ASN1_ITEM_UNPACK, ASN1_R_DECODE_ERROR); |
61 | return ret; | |
09ab755c | 62 | } |