Coverage Report

Created: 2026-07-19 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/pkcs7/pk7_attr.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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
8
 */
9
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <openssl/bio.h>
13
#include <openssl/asn1.h>
14
#include <openssl/asn1t.h>
15
#include <openssl/pem.h>
16
#include <openssl/pkcs7.h>
17
#include <openssl/x509.h>
18
#include <openssl/err.h>
19
20
#include <crypto/asn1.h>
21
22
int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
23
    STACK_OF(X509_ALGOR) *cap)
24
0
{
25
0
    ASN1_STRING *seq;
26
27
0
    if ((seq = ASN1_STRING_new()) == NULL) {
28
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
29
0
        return 0;
30
0
    }
31
0
    seq->length = ASN1_item_i2d((ASN1_VALUE *)cap, &seq->data,
32
0
        ASN1_ITEM_rptr(X509_ALGORS));
33
0
    if (ASN1_STRING_length_ex(seq) == 0 || ASN1_STRING_get0_data(seq) == NULL) {
34
0
        ASN1_STRING_free(seq);
35
0
        return 1;
36
0
    }
37
0
    if (!PKCS7_add_signed_attribute(si, NID_SMIMECapabilities,
38
0
            V_ASN1_SEQUENCE, seq)) {
39
0
        ASN1_STRING_free(seq);
40
0
        return 0;
41
0
    }
42
0
    return 1;
43
0
}
44
45
STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
46
0
{
47
0
    const ASN1_TYPE *cap;
48
0
    const unsigned char *p;
49
0
    size_t len;
50
51
0
    cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
52
0
    if (cap == NULL || (cap->type != V_ASN1_SEQUENCE))
53
0
        return NULL;
54
0
    p = ASN1_STRING_get0_data(cap->value.sequence);
55
0
    len = ASN1_STRING_length_ex(cap->value.sequence);
56
0
    if (len > INT_MAX)
57
0
        return NULL;
58
0
    return (STACK_OF(X509_ALGOR) *)
59
0
        ASN1_item_d2i(NULL, &p, (int)len, ASN1_ITEM_rptr(X509_ALGORS));
60
0
}
61
62
/* Basic smime-capabilities OID and optional integer arg */
63
int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
64
0
{
65
0
    ASN1_INTEGER *nbit = NULL;
66
0
    X509_ALGOR *alg;
67
68
0
    if ((alg = X509_ALGOR_new()) == NULL) {
69
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
70
0
        return 0;
71
0
    }
72
0
    ASN1_OBJECT_free(alg->algorithm);
73
0
    alg->algorithm = OBJ_nid2obj(nid);
74
0
    if (arg > 0) {
75
0
        if ((alg->parameter = ASN1_TYPE_new()) == NULL) {
76
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
77
0
            goto err;
78
0
        }
79
0
        if ((nbit = ASN1_INTEGER_new()) == NULL) {
80
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
81
0
            goto err;
82
0
        }
83
0
        if (!ASN1_INTEGER_set(nbit, arg)) {
84
0
            ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
85
0
            goto err;
86
0
        }
87
0
        alg->parameter->value.integer = nbit;
88
0
        alg->parameter->type = V_ASN1_INTEGER;
89
0
        nbit = NULL;
90
0
    }
91
0
    if (!sk_X509_ALGOR_push(sk, alg)) {
92
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
93
0
        goto err;
94
0
    }
95
0
    return 1;
96
0
err:
97
0
    ASN1_INTEGER_free(nbit);
98
0
    X509_ALGOR_free(alg);
99
0
    return 0;
100
0
}
101
102
int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid)
103
0
{
104
0
    if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType))
105
0
        return 0;
106
0
    if (!coid)
107
0
        coid = OBJ_nid2obj(NID_pkcs7_data);
108
0
    return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
109
0
        V_ASN1_OBJECT, coid);
110
0
}
111
112
int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t)
113
0
{
114
0
    ASN1_TIME *tmp = NULL;
115
116
0
    if (t == NULL && (tmp = t = X509_gmtime_adj(NULL, 0)) == NULL) {
117
0
        ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
118
0
        return 0;
119
0
    }
120
0
    if (!PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime,
121
0
            V_ASN1_UTCTIME, t)) {
122
0
        ASN1_TIME_free(tmp);
123
0
        return 0;
124
0
    }
125
0
    return 1;
126
0
}
127
128
int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
129
    const unsigned char *md, int mdlen)
130
0
{
131
0
    ASN1_OCTET_STRING *os;
132
0
    os = ASN1_OCTET_STRING_new();
133
0
    if (os == NULL)
134
0
        return 0;
135
0
    if (!ASN1_STRING_set_data(os, md, mdlen)
136
0
        || !PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest,
137
0
            V_ASN1_OCTET_STRING, os)) {
138
0
        ASN1_OCTET_STRING_free(os);
139
0
        return 0;
140
0
    }
141
0
    return 1;
142
0
}