1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <openssl/objects.h>
13e1051a39Sopenharmony_ci#include <openssl/asn1t.h>
14e1051a39Sopenharmony_ci#include <openssl/x509.h>
15e1051a39Sopenharmony_ci#include "x509_local.h"
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci/*-
18e1051a39Sopenharmony_ci * X509_ATTRIBUTE: this has the following form:
19e1051a39Sopenharmony_ci *
20e1051a39Sopenharmony_ci * typedef struct x509_attributes_st
21e1051a39Sopenharmony_ci *      {
22e1051a39Sopenharmony_ci *      ASN1_OBJECT *object;
23e1051a39Sopenharmony_ci *      STACK_OF(ASN1_TYPE) *set;
24e1051a39Sopenharmony_ci *      } X509_ATTRIBUTE;
25e1051a39Sopenharmony_ci *
26e1051a39Sopenharmony_ci */
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ciASN1_SEQUENCE(X509_ATTRIBUTE) = {
29e1051a39Sopenharmony_ci        ASN1_SIMPLE(X509_ATTRIBUTE, object, ASN1_OBJECT),
30e1051a39Sopenharmony_ci        ASN1_SET_OF(X509_ATTRIBUTE, set, ASN1_ANY)
31e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_ciIMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
34e1051a39Sopenharmony_ciIMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ciX509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
37e1051a39Sopenharmony_ci{
38e1051a39Sopenharmony_ci    X509_ATTRIBUTE *ret = NULL;
39e1051a39Sopenharmony_ci    ASN1_TYPE *val = NULL;
40e1051a39Sopenharmony_ci    ASN1_OBJECT *oid;
41e1051a39Sopenharmony_ci
42e1051a39Sopenharmony_ci    if ((oid = OBJ_nid2obj(nid)) == NULL)
43e1051a39Sopenharmony_ci        return NULL;
44e1051a39Sopenharmony_ci    if ((ret = X509_ATTRIBUTE_new()) == NULL)
45e1051a39Sopenharmony_ci        return NULL;
46e1051a39Sopenharmony_ci    ret->object = oid;
47e1051a39Sopenharmony_ci    if ((val = ASN1_TYPE_new()) == NULL)
48e1051a39Sopenharmony_ci        goto err;
49e1051a39Sopenharmony_ci    if (!sk_ASN1_TYPE_push(ret->set, val))
50e1051a39Sopenharmony_ci        goto err;
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ci    ASN1_TYPE_set(val, atrtype, value);
53e1051a39Sopenharmony_ci    return ret;
54e1051a39Sopenharmony_ci err:
55e1051a39Sopenharmony_ci    X509_ATTRIBUTE_free(ret);
56e1051a39Sopenharmony_ci    ASN1_TYPE_free(val);
57e1051a39Sopenharmony_ci    return NULL;
58e1051a39Sopenharmony_ci}
59