Coverage Report

Created: 2026-11-12 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/lib/x509/pkcs7-attrs.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2015 Red Hat, Inc.
3
 *
4
 * Author: Nikos Mavrogiannopoulos
5
 *
6
 * This file is part of GnuTLS.
7
 *
8
 * The GnuTLS is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation; either version 2.1 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
20
 *
21
 */
22
23
/* Functions that relate on PKCS7 attribute setting.
24
 */
25
26
#include "gnutls_int.h"
27
28
#include "datum.h"
29
#include "global.h"
30
#include "errors.h"
31
#include "common.h"
32
#include "x509_b64.h"
33
#include <gnutls/abstract.h>
34
#include <gnutls/pkcs7.h>
35
36
/**
37
 * gnutls_pkcs7_add_attr:
38
 * @list: A list of existing attributes or pointer to %NULL for the first one
39
 * @oid: the OID of the attribute to be set
40
 * @data: the raw (DER-encoded) data of the attribute to be set
41
 * @flags: zero or %GNUTLS_PKCS7_ATTR_ENCODE_OCTET_STRING
42
 *
43
 * This function will set a PKCS #7 attribute in the provided list.
44
 * If this function fails, the previous list would be deallocated.
45
 *
46
 * Note that any attributes set with this function must either be
47
 * DER or BER encoded, unless a special flag is present.
48
 *
49
 * Returns: On success, the new list head, otherwise %NULL.
50
 *
51
 * Since: 3.4.2
52
 **/
53
int gnutls_pkcs7_add_attr(gnutls_pkcs7_attrs_t *list, const char *oid,
54
        gnutls_datum_t *data, unsigned flags)
55
0
{
56
0
  int ret;
57
0
  gnutls_pkcs7_attrs_st *r;
58
59
0
  r = gnutls_calloc(1, sizeof(gnutls_pkcs7_attrs_st));
60
0
  if (r == NULL)
61
0
    goto fail;
62
63
0
  if (flags & GNUTLS_PKCS7_ATTR_ENCODE_OCTET_STRING) {
64
0
    ret = _gnutls_x509_encode_string(ASN1_ETYPE_OCTET_STRING,
65
0
             data->data, data->size,
66
0
             &r->data);
67
0
  } else {
68
0
    ret = _gnutls_set_datum(&r->data, data->data, data->size);
69
0
  }
70
0
  if (ret < 0)
71
0
    goto fail;
72
73
0
  r->oid = gnutls_strdup(oid);
74
0
  if (r->oid == NULL)
75
0
    goto fail;
76
77
0
  r->next = *list;
78
0
  *list = r;
79
80
0
  return 0;
81
0
fail:
82
0
  if (r) {
83
0
    gnutls_free(r->data.data);
84
0
    gnutls_free(r);
85
0
  }
86
0
  gnutls_pkcs7_attrs_deinit(*list);
87
0
  return GNUTLS_E_MEMORY_ERROR;
88
0
}
89
90
/**
91
 * gnutls_pkcs7_get_attr:
92
 * @list: A list of existing attributes or %NULL for the first one
93
 * @idx: the index of the attribute to get
94
 * @oid: the OID of the attribute (read-only)
95
 * @data: the raw data of the attribute
96
 * @flags: zero or %GNUTLS_PKCS7_ATTR_ENCODE_OCTET_STRING
97
 *
98
 * This function will get a PKCS #7 attribute from the provided list.
99
 * The OID is a constant string, but data will be allocated and must be
100
 * deinitialized by the caller.
101
 *
102
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
103
 *   negative error value. %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE is returned
104
 *   if there are no data in the current index.
105
 *
106
 * Since: 3.4.2
107
 **/
108
int gnutls_pkcs7_get_attr(gnutls_pkcs7_attrs_t list, unsigned idx, char **oid,
109
        gnutls_datum_t *data, unsigned flags)
110
0
{
111
0
  unsigned i;
112
0
  gnutls_pkcs7_attrs_st *p = list;
113
0
  int ret;
114
115
0
  for (i = 0; i < idx; i++) {
116
0
    p = p->next;
117
0
    if (p == NULL)
118
0
      break;
119
0
  }
120
121
0
  if (p == NULL)
122
0
    return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
123
124
0
  *oid = p->oid;
125
126
0
  if (flags & GNUTLS_PKCS7_ATTR_ENCODE_OCTET_STRING) {
127
0
    ret = _gnutls_x509_decode_string(ASN1_ETYPE_OCTET_STRING,
128
0
             p->data.data, p->data.size,
129
0
             data, 1);
130
0
  } else {
131
0
    ret = _gnutls_set_datum(data, p->data.data, p->data.size);
132
0
  }
133
0
  if (ret < 0)
134
0
    return gnutls_assert_val(ret);
135
136
0
  return 0;
137
0
}
138
139
/**
140
 * gnutls_pkcs7_attrs_deinit:
141
 * @list: A list of existing attributes
142
 *
143
 * This function will clear a PKCS #7 attribute list.
144
 *
145
 * Since: 3.4.2
146
 **/
147
void gnutls_pkcs7_attrs_deinit(gnutls_pkcs7_attrs_t list)
148
19.6k
{
149
19.6k
  gnutls_pkcs7_attrs_st *r = list, *next;
150
151
19.6k
  while (r) {
152
0
    next = r->next;
153
154
0
    gnutls_free(r->data.data);
155
0
    gnutls_free(r->oid);
156
    gnutls_free(r);
157
0
    r = next;
158
0
  }
159
19.6k
}