-
Notifications
You must be signed in to change notification settings - Fork 1
/
cms-sign-pkcs11.c
157 lines (123 loc) · 3.36 KB
/
cms-sign-pkcs11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (c) 2023 Embetrix.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/cms.h>
#include <openssl/engine.h>
#include <openssl/conf.h>
#include <openssl/err.h>
static EVP_PKEY *read_private_key(const char *private_key_name)
{
EVP_PKEY *private_key = NULL;
ENGINE *eng = NULL;
BIO *b = NULL;
if (!strncmp(private_key_name, "pkcs11:", 7)) {
ENGINE_load_builtin_engines();
eng = ENGINE_by_id("pkcs11");
if (!eng) {
fprintf(stderr, "Error: Load PKCS#11 ENGINE\n");
return NULL;
}
if (!ENGINE_init(eng)) {
fprintf(stderr, "Error: ENGINE_init\n");
return NULL;
}
private_key = ENGINE_load_private_key(eng, private_key_name,
NULL, NULL);
}
else {
b = BIO_new_file(private_key_name, "rb");
if (!b)
goto out;
private_key = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);
BIO_free(b);
}
out:
if (!private_key)
fprintf(stderr, "Error: failed loading private key %s\n", private_key_name);
if (eng)
ENGINE_free(eng);
return private_key;
}
int main(int argc, char **argv)
{
int ret = -1;
BIO *in = NULL, *out = NULL, *cbio = NULL;
X509 *scert = NULL;
CMS_ContentInfo *cms = NULL;
EVP_PKEY *sprivkey = NULL;
int flags = CMS_BINARY | CMS_PARTIAL | CMS_DETACHED ;
if (argc != 5) {
printf("Usage: %s <sign cert> <sign key id> <input> <signature output>\n", argv[0]);
return ret;
}
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
ERR_clear_error();
/* Read in signing key */
sprivkey = read_private_key(argv[2]);
if (!sprivkey)
goto out;
/* Read in signing certificate */
cbio = BIO_new_file(argv[1], "r");
if (!cbio)
goto out;
scert = PEM_read_bio_X509(cbio, NULL, 0, NULL);
if (!scert)
goto out;
/* open content to be signed */
in = BIO_new_file(argv[3], "r");
if (!in)
goto out;
/* sign content */
cms = CMS_sign(NULL, NULL, NULL, in, flags);
if (!cms)
goto out;
CMS_SignerInfo *si;
si = CMS_add1_signer(cms, scert, sprivkey, EVP_sha256(), flags);
if (!si)
goto out;
if (!CMS_final(cms, in, NULL, flags))
goto out;
/* create the signature */
out = BIO_new_file(argv[4], "w");
if (!out)
goto out;
if (!i2d_CMS_bio_stream(out, cms, in, flags))
goto out;
ret = 0;
out:
if (ret) {
fprintf(stderr, "Error Signing Data\n");
ERR_print_errors_fp(stderr);
}
if (cms)
CMS_ContentInfo_free(cms);
if (scert)
X509_free(scert);
if (sprivkey)
EVP_PKEY_free(sprivkey);
if (in)
BIO_free(in);
if (out)
BIO_free(out);
if (cbio)
BIO_free(cbio);
return ret;
}