]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pkcs7/pk7_attr.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / pkcs7 / pk7_attr.c
CommitLineData
0f113f3e 1/*
454afd98 2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
68dbba98 3 *
b7617a3a 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
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
5a9a4b29
DSH
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <openssl/bio.h>
13#include <openssl/asn1.h>
3c07d3a3 14#include <openssl/asn1t.h>
5a9a4b29
DSH
15#include <openssl/pem.h>
16#include <openssl/pkcs7.h>
4d29312c 17#include <openssl/x509.h>
5a9a4b29
DSH
18#include <openssl/err.h>
19
0f113f3e
MC
20int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
21 STACK_OF(X509_ALGOR) *cap)
5a9a4b29 22{
0f113f3e 23 ASN1_STRING *seq;
75ebbd9a
RS
24
25 if ((seq = ASN1_STRING_new()) == NULL) {
9311d0c4 26 ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
27 return 0;
28 }
29 seq->length = ASN1_item_i2d((ASN1_VALUE *)cap, &seq->data,
30 ASN1_ITEM_rptr(X509_ALGORS));
31 return PKCS7_add_signed_attribute(si, NID_SMIMECapabilities,
32 V_ASN1_SEQUENCE, seq);
5a9a4b29
DSH
33}
34
4d29312c 35STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
0f113f3e
MC
36{
37 ASN1_TYPE *cap;
38 const unsigned char *p;
875a644a 39
0f113f3e 40 cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
75ebbd9a 41 if (cap == NULL || (cap->type != V_ASN1_SEQUENCE))
0f113f3e
MC
42 return NULL;
43 p = cap->value.sequence->data;
44 return (STACK_OF(X509_ALGOR) *)
45 ASN1_item_d2i(NULL, &p, cap->value.sequence->length,
46 ASN1_ITEM_rptr(X509_ALGORS));
47}
5a9a4b29
DSH
48
49/* Basic smime-capabilities OID and optional integer arg */
371acb22 50int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
5a9a4b29 51{
b77a8653 52 ASN1_INTEGER *nbit = NULL;
0f113f3e 53 X509_ALGOR *alg;
371acb22 54
75ebbd9a 55 if ((alg = X509_ALGOR_new()) == NULL) {
9311d0c4 56 ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
57 return 0;
58 }
59 ASN1_OBJECT_free(alg->algorithm);
60 alg->algorithm = OBJ_nid2obj(nid);
61 if (arg > 0) {
75ebbd9a 62 if ((alg->parameter = ASN1_TYPE_new()) == NULL) {
b77a8653 63 goto err;
0f113f3e 64 }
75ebbd9a 65 if ((nbit = ASN1_INTEGER_new()) == NULL) {
b77a8653 66 goto err;
0f113f3e
MC
67 }
68 if (!ASN1_INTEGER_set(nbit, arg)) {
b77a8653 69 goto err;
0f113f3e
MC
70 }
71 alg->parameter->value.integer = nbit;
72 alg->parameter->type = V_ASN1_INTEGER;
b77a8653 73 nbit = NULL;
0f113f3e 74 }
68efafc5 75 if (!sk_X509_ALGOR_push(sk, alg)) {
b77a8653 76 goto err;
68efafc5 77 }
0f113f3e 78 return 1;
b77a8653 79err:
9311d0c4 80 ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
b77a8653
F
81 ASN1_INTEGER_free(nbit);
82 X509_ALGOR_free(alg);
83 return 0;
5a9a4b29 84}
76fa8f18
DSH
85
86int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid)
0f113f3e
MC
87{
88 if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType))
89 return 0;
90 if (!coid)
91 coid = OBJ_nid2obj(NID_pkcs7_data);
92 return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
93 V_ASN1_OBJECT, coid);
94}
76fa8f18
DSH
95
96int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t)
0f113f3e 97{
75ebbd9a 98 if (t == NULL && (t = X509_gmtime_adj(NULL, 0)) == NULL) {
9311d0c4 99 ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
100 return 0;
101 }
102 return PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime,
103 V_ASN1_UTCTIME, t);
104}
76fa8f18
DSH
105
106int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
0f113f3e
MC
107 const unsigned char *md, int mdlen)
108{
109 ASN1_OCTET_STRING *os;
110 os = ASN1_OCTET_STRING_new();
90945fa3 111 if (os == NULL)
0f113f3e
MC
112 return 0;
113 if (!ASN1_STRING_set(os, md, mdlen)
114 || !PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest,
115 V_ASN1_OCTET_STRING, os)) {
116 ASN1_OCTET_STRING_free(os);
117 return 0;
118 }
119 return 1;
120}