Copyright consolidation 04/10
[openssl.git] / crypto / pkcs7 / pk7_attr.c
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 {
23     ASN1_STRING *seq;
24
25     if ((seq = ASN1_STRING_new()) == NULL) {
26         PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP, ERR_R_MALLOC_FAILURE);
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);
33 }
34
35 STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
36 {
37     ASN1_TYPE *cap;
38     const unsigned char *p;
39
40     cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
41     if (cap == NULL || (cap->type != V_ASN1_SEQUENCE))
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 }
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 {
52     X509_ALGOR *alg;
53
54     if ((alg = X509_ALGOR_new()) == NULL) {
55         PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP, ERR_R_MALLOC_FAILURE);
56         return 0;
57     }
58     ASN1_OBJECT_free(alg->algorithm);
59     alg->algorithm = OBJ_nid2obj(nid);
60     if (arg > 0) {
61         ASN1_INTEGER *nbit;
62         if ((alg->parameter = ASN1_TYPE_new()) == NULL) {
63             PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP, ERR_R_MALLOC_FAILURE);
64             return 0;
65         }
66         if ((nbit = ASN1_INTEGER_new()) == NULL) {
67             PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP, ERR_R_MALLOC_FAILURE);
68             return 0;
69         }
70         if (!ASN1_INTEGER_set(nbit, arg)) {
71             PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP, ERR_R_MALLOC_FAILURE);
72             return 0;
73         }
74         alg->parameter->value.integer = nbit;
75         alg->parameter->type = V_ASN1_INTEGER;
76     }
77     sk_X509_ALGOR_push(sk, alg);
78     return 1;
79 }
80
81 int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid)
82 {
83     if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType))
84         return 0;
85     if (!coid)
86         coid = OBJ_nid2obj(NID_pkcs7_data);
87     return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
88                                       V_ASN1_OBJECT, coid);
89 }
90
91 int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t)
92 {
93     if (t == NULL && (t = X509_gmtime_adj(NULL, 0)) == NULL) {
94         PKCS7err(PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME,
95                  ERR_R_MALLOC_FAILURE);
96         return 0;
97     }
98     return PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime,
99                                       V_ASN1_UTCTIME, t);
100 }
101
102 int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
103                              const unsigned char *md, int mdlen)
104 {
105     ASN1_OCTET_STRING *os;
106     os = ASN1_OCTET_STRING_new();
107     if (os == NULL)
108         return 0;
109     if (!ASN1_STRING_set(os, md, mdlen)
110         || !PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest,
111                                        V_ASN1_OCTET_STRING, os)) {
112         ASN1_OCTET_STRING_free(os);
113         return 0;
114     }
115     return 1;
116 }