Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/pkcs7/pk7_attr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
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
int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
21
                              STACK_OF(X509_ALGOR) *cap)
22
0
{
23
0
    ASN1_STRING *seq;
24
0
25
0
    if ((seq = ASN1_STRING_new()) == NULL) {
26
0
        PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP, ERR_R_MALLOC_FAILURE);
27
0
        return 0;
28
0
    }
29
0
    seq->length = ASN1_item_i2d((ASN1_VALUE *)cap, &seq->data,
30
0
                                ASN1_ITEM_rptr(X509_ALGORS));
31
0
    return PKCS7_add_signed_attribute(si, NID_SMIMECapabilities,
32
0
                                      V_ASN1_SEQUENCE, seq);
33
0
}
34
35
STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
36
0
{
37
0
    ASN1_TYPE *cap;
38
0
    const unsigned char *p;
39
0
40
0
    cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
41
0
    if (cap == NULL || (cap->type != V_ASN1_SEQUENCE))
42
0
        return NULL;
43
0
    p = cap->value.sequence->data;
44
0
    return (STACK_OF(X509_ALGOR) *)
45
0
        ASN1_item_d2i(NULL, &p, cap->value.sequence->length,
46
0
                      ASN1_ITEM_rptr(X509_ALGORS));
47
0
}
48
49
/* Basic smime-capabilities OID and optional integer arg */
50
int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
51
0
{
52
0
    ASN1_INTEGER *nbit = NULL;
53
0
    X509_ALGOR *alg;
54
0
55
0
    if ((alg = X509_ALGOR_new()) == NULL) {
56
0
        PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP, ERR_R_MALLOC_FAILURE);
57
0
        return 0;
58
0
    }
59
0
    ASN1_OBJECT_free(alg->algorithm);
60
0
    alg->algorithm = OBJ_nid2obj(nid);
61
0
    if (arg > 0) {
62
0
        if ((alg->parameter = ASN1_TYPE_new()) == NULL) {
63
0
            goto err;
64
0
        }
65
0
        if ((nbit = ASN1_INTEGER_new()) == NULL) {
66
0
            goto err;
67
0
        }
68
0
        if (!ASN1_INTEGER_set(nbit, arg)) {
69
0
            goto err;
70
0
        }
71
0
        alg->parameter->value.integer = nbit;
72
0
        alg->parameter->type = V_ASN1_INTEGER;
73
0
        nbit = NULL;
74
0
    }
75
0
    if (!sk_X509_ALGOR_push(sk, alg)) {
76
0
        goto err;
77
0
    }
78
0
    return 1;
79
0
err:
80
0
    PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP, ERR_R_MALLOC_FAILURE);
81
0
    ASN1_INTEGER_free(nbit);
82
0
    X509_ALGOR_free(alg);
83
0
    return 0;
84
0
}
85
86
int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid)
87
0
{
88
0
    if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType))
89
0
        return 0;
90
0
    if (!coid)
91
0
        coid = OBJ_nid2obj(NID_pkcs7_data);
92
0
    return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
93
0
                                      V_ASN1_OBJECT, coid);
94
0
}
95
96
int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t)
97
0
{
98
0
    if (t == NULL && (t = X509_gmtime_adj(NULL, 0)) == NULL) {
99
0
        PKCS7err(PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME,
100
0
                 ERR_R_MALLOC_FAILURE);
101
0
        return 0;
102
0
    }
103
0
    return PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime,
104
0
                                      V_ASN1_UTCTIME, t);
105
0
}
106
107
int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
108
                             const unsigned char *md, int mdlen)
109
0
{
110
0
    ASN1_OCTET_STRING *os;
111
0
    os = ASN1_OCTET_STRING_new();
112
0
    if (os == NULL)
113
0
        return 0;
114
0
    if (!ASN1_STRING_set(os, md, mdlen)
115
0
        || !PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest,
116
0
                                       V_ASN1_OCTET_STRING, os)) {
117
0
        ASN1_OCTET_STRING_free(os);
118
0
        return 0;
119
0
    }
120
0
    return 1;
121
0
}