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