]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/asn_pack.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / asn1 / asn_pack.c
CommitLineData
0f113f3e 1/*
2039c421 2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
cfcefcbe 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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 16ASN1_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) {
9311d0c4 22 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
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) {
9311d0c4 33 ERR_raise(ERR_LIB_ASN1, ASN1_R_ENCODE_ERROR);
69e2bd32 34 goto err;
0f113f3e 35 }
69e2bd32 36 if (octmp->data == NULL) {
9311d0c4 37 ERR_raise(ERR_LIB_ASN1, 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
0c800648 53void *ASN1_item_unpack(const 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)
9311d0c4 60 ERR_raise(ERR_LIB_ASN1, ASN1_R_DECODE_ERROR);
0f113f3e 61 return ret;
09ab755c 62}