lib:printer_driver: Retrieve Class value
[samba.git] / lib / mscat / mscat_pkcs7.c
1 /*
2  * Copyright (c) 2016      Andreas Schneider <asn@samba.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <errno.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include <util/debug.h>
26 #include <util/data_blob.h>
27
28 #include "mscat.h"
29 #include "mscat_private.h"
30
31 #define PKCS7_CTL_OBJID                "1.3.6.1.4.1.311.10.1"
32
33 static int mscat_pkcs7_cleanup(struct mscat_pkcs7 *mp7)
34 {
35         if (mp7->c != NULL) {
36                 gnutls_pkcs7_deinit(mp7->c);
37         }
38
39         return 0;
40 }
41
42 struct mscat_pkcs7 *mscat_pkcs7_init(TALLOC_CTX *mem_ctx)
43 {
44         struct mscat_pkcs7 *pkcs7;
45         int rc;
46
47         pkcs7 = talloc_zero(mem_ctx, struct mscat_pkcs7);
48         if (pkcs7 == NULL) {
49                 return NULL;
50         }
51         talloc_set_destructor(pkcs7, mscat_pkcs7_cleanup);
52
53         rc = gnutls_pkcs7_init(&pkcs7->c);
54         if (rc != 0) {
55                 talloc_free(pkcs7);
56                 return NULL;
57         }
58
59         return pkcs7;
60 }
61
62 static int mscat_read_file(TALLOC_CTX *mem_ctx,
63                            const char *filename,
64                            DATA_BLOB *pblob)
65 {
66         struct stat sb = {0};
67         size_t alloc_size;
68         size_t count;
69         DATA_BLOB blob;
70         FILE *fp;
71         int rc;
72
73         fp = fopen(filename, "r");
74         if (fp == NULL) {
75                 return -1;
76         }
77
78         rc = fstat(fileno(fp), &sb);
79         if (rc != 0) {
80                 goto error;
81         }
82
83         if (!S_ISREG(sb.st_mode)) {
84                 errno = EINVAL;
85                 goto error;
86         }
87         if (SIZE_MAX - 1 < (unsigned long)sb.st_size) {
88                 errno = ENOMEM;
89                 goto error;
90         }
91         alloc_size = sb.st_size + 1;
92
93         blob = data_blob_talloc_zero(mem_ctx, alloc_size);
94         if (blob.data == NULL) {
95                 goto error;
96         }
97
98         count = fread(blob.data, 1, blob.length, fp);
99         if (count != blob.length) {
100                 if (ferror(fp)) {
101                         goto error;
102                 }
103         }
104         blob.data[count] = '\0';
105         blob.length = count;
106         fclose(fp);
107
108         *pblob = blob;
109
110         return 0;
111 error:
112         data_blob_free(&blob);
113         fclose(fp);
114         return rc;
115 }
116
117 int mscat_pkcs7_import_catfile(struct mscat_pkcs7 *mp7,
118                                const char *catfile)
119 {
120         TALLOC_CTX *tmp_ctx;
121         gnutls_datum_t mscat_data = {
122                 .size = 0,
123         };
124         DATA_BLOB blob = {
125                 .length = 0,
126         };
127         int rc;
128
129         tmp_ctx = talloc_new(mp7);
130         if (tmp_ctx == NULL) {
131                 return -1;
132         }
133
134         rc = mscat_read_file(tmp_ctx,
135                              catfile,
136                              &blob);
137         if (rc == -1) {
138                 DBG_ERR("Failed to read catalog file '%s' - %s",
139                         catfile,
140                         strerror(errno));
141                 goto done;
142         }
143
144         mscat_data.data = blob.data;
145         mscat_data.size = blob.length;
146
147         rc = gnutls_pkcs7_import(mp7->c,
148                                  &mscat_data,
149                                  GNUTLS_X509_FMT_DER);
150         if (rc < 0) {
151                 DBG_ERR("Failed to import PKCS7 from '%s' - %s",
152                         catfile,
153                         gnutls_strerror(rc));
154                 goto done;
155         }
156
157         rc = 0;
158 done:
159         talloc_free(tmp_ctx);
160         return rc;
161 }
162
163 int mscat_pkcs7_verify(struct mscat_pkcs7 *mp7,
164                        const char *ca_file)
165 {
166         TALLOC_CTX *tmp_ctx = NULL;
167         gnutls_x509_trust_list_t tl = NULL;
168         gnutls_datum_t ca_data;
169         DATA_BLOB blob = {
170                 .length = 0,
171         };
172         uint32_t flags = 0;
173         const char *oid;
174         int count;
175         int cmp;
176         int rc;
177         int i;
178
179         oid = gnutls_pkcs7_get_embedded_data_oid(mp7->c);
180         if (oid == NULL) {
181                 DBG_ERR("Failed to get oid - %s",
182                         gnutls_strerror(errno));
183                 return -1;
184         }
185
186         cmp = strcmp(oid, PKCS7_CTL_OBJID);
187         if (cmp != 0) {
188                 DBG_ERR("Invalid oid in catalog file! oid: %s, expected: %s",
189                         oid,
190                         PKCS7_CTL_OBJID);
191                 return -1;
192         }
193
194         tmp_ctx = talloc_new(mp7);
195         if (tmp_ctx == NULL) {
196                 return -1;
197         }
198
199         rc = gnutls_x509_trust_list_init(&tl,
200                                          0); /* default size */
201         if (rc != 0) {
202                 DBG_ERR("Failed to create trust list - %s",
203                         gnutls_strerror(rc));
204                 goto done;
205         }
206
207
208         /* Load the system trust list */
209         rc = gnutls_x509_trust_list_add_system_trust(tl, 0, 0);
210         if (rc < 0) {
211                 DBG_ERR("Failed to add system trust list - %s",
212                         gnutls_strerror(rc));
213                 goto done;
214         }
215         DBG_INFO("Loaded %d CAs", rc);
216
217         if (ca_file != NULL) {
218                 rc = mscat_read_file(tmp_ctx,
219                                      ca_file,
220                                      &blob);
221                 if (rc != 0) {
222                         DBG_ERR("Failed to read CA file '%s' - %s",
223                                 ca_file,
224                                 strerror(errno));
225                         goto done;
226                 }
227
228                 ca_data.data = blob.data;
229                 ca_data.size = blob.length;
230
231                 rc = gnutls_x509_trust_list_add_trust_mem(tl,
232                                                           &ca_data,
233                                                           NULL, /* crls */
234                                                           GNUTLS_X509_FMT_DER,
235                                                           0, /* tl_flags */
236                                                           0); /* tl_vflags */
237                 if (rc < 0) {
238                         DBG_ERR("Failed to add '%s' to trust list - %s (%d)",
239                                 ca_file,
240                                 gnutls_strerror(rc),
241                                 rc);
242                         goto done;
243                 }
244                 DBG_INFO("Loaded %d additional CAs", rc);
245         }
246
247         /*
248          * Drivers often exist for quite some time, so it is possible that one
249          * of the certificates in the trust list expired.
250          * This is not a big deal, but we need to disable the time checks
251          * or the verification will fail.
252          */
253         flags = GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS|
254                 GNUTLS_VERIFY_DISABLE_TIME_CHECKS;
255
256 #if GNUTLS_VERSION_NUMBER >= 0x030600
257         /* The "Microsoft Root Authority" certificate uses SHA1 */
258         flags |= GNUTLS_VERIFY_ALLOW_SIGN_WITH_SHA1;
259 #endif
260
261         count = gnutls_pkcs7_get_signature_count(mp7->c);
262         if (count == 0) {
263                 DBG_ERR("Failed to verify catalog file, no signatures found");
264                 goto done;
265         }
266
267         for (i = 0; i < count; i++) {
268                 rc = gnutls_pkcs7_verify(mp7->c,
269                                          tl,
270                                          NULL, /* vdata */
271                                          0,    /* vdata_size */
272                                          i,    /* index */
273                                          NULL, /* data */
274                                          flags);   /* flags */
275                 if (rc < 0) {
276                         DBG_ERR("Failed to verify catalog file - %s (%d)",
277                                 gnutls_strerror(rc),
278                                 rc);
279                         goto done;
280                 }
281         }
282
283         rc = 0;
284 done:
285         gnutls_x509_trust_list_deinit(tl, 1);
286         talloc_free(tmp_ctx);
287         return rc;
288 }