Debian Codegraph @

src/source/signature.c

raw symbols SCIP
1 /*
2 * Copyright 2021 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Author:
20 * Jan Černý <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30
31 #include <libxml/tree.h>
32 #include <libxml/xmlmemory.h>
33 #include <libxml/parser.h>
34
35 #include <libxslt/xslt.h>
36 #include <libxslt/security.h>
37
38 #include <xmlsec/xmlsec.h>
39 #include <xmlsec/xmltree.h>
40 #include <xmlsec/xmldsig.h>
41 #include <xmlsec/crypto.h>
42
43 #include "common/_error.h"
44 #include "common/debug_priv.h"
45 #include "oscap_source.h"
46 #include "oscap_source_priv.h"
47 #include "signature_priv.h"
48
49 struct oscap_signature_ctx {
50 const char *pubkey_pem; // path to the public key file in PEM format
51 const char *pubkey_cert_pem; // path to the public key certificate file in PEM format
52 };
53
54 struct oscap_signature_ctx *oscap_signature_ctx_new()
55 {
56 struct oscap_signature_ctx *ctx = malloc(sizeof(struct oscap_signature_ctx));
57 ctx->pubkey_pem = NULL;
58 ctx->pubkey_cert_pem = NULL;
59 return ctx;
60 }
61
62 void oscap_signature_ctx_set_pubkey_pem(struct oscap_signature_ctx *ctx, const char *path)
63 {
64 ctx->pubkey_pem = path;
65 }
66
67 void oscap_signature_ctx_set_pubkey_cert_pem(struct oscap_signature_ctx *ctx, const char *path)
68 {
69 ctx->pubkey_cert_pem = path;
70 }
71
72 void oscap_signature_ctx_free(struct oscap_signature_ctx *ctx)
73 {
74 free(ctx);
75 }
76
77 static int _mark_sds_id_elements(xmlDocPtr doc)
78 {
79 /* Adds id attibutes of ds:component and ds:data-stream to the list
80 * of globally known id attributes.
81 */
82 xmlNodePtr root = xmlDocGetRootElement(doc);
83 xmlNodePtr node = root->children;
84 for (; node != NULL; node = node->next) {
85 if (node->type != XML_ELEMENT_NODE)
86 continue;
87 const char *name = (const char *)node->name;
88 if (strcmp(name, "component") && strcmp(name, "data-stream"))
89 continue;
90 xmlAttrPtr id_attr = xmlHasProp(node, BAD_CAST "id");
91 if (id_attr == NULL || id_attr->children == NULL) {
92 oscap_seterr(OSCAP_EFAMILY_XML, "Element \"%s\" doesn't have an id attribute\n", name);
93 return -1;
94 }
95 xmlChar *id_attr_value = xmlNodeListGetString(node->doc, id_attr->children, 1);
96 if (id_attr_value == NULL) {
97 oscap_seterr(OSCAP_EFAMILY_XML, "Attribute \"id\" in element \"%s\" has no value\n", name);
98 return -1;
99 }
100 /* check that we don't have same ID already */
101 xmlAttrPtr tmp_attr = xmlGetID(node->doc, id_attr_value);
102 if (tmp_attr == NULL) {
103 xmlAddID(NULL, node->doc, id_attr_value, id_attr);
104 } else if (tmp_attr != id_attr) {
105 oscap_seterr(OSCAP_EFAMILY_XML, "Duplicate ID attribute \"%s\"\n", id_attr_value);
106 xmlFree(id_attr_value);
107 return -1;
108 }
109 xmlFree(id_attr_value);
110 }
111 return 0;
112 }
113
114 static int _oscap_signature_validate_doc(xmlDocPtr doc, oscap_document_type_t scap_type, struct oscap_signature_ctx *ctx, bool enforce_signature)
115 {
116 int res = -1;
117 xmlNodePtr node = NULL;
118 xmlSecDSigCtxPtr dsigCtx = NULL;
119 xmlSecKeysMngrPtr mngr = NULL;
120 xsltSecurityPrefsPtr xsltSecPrefs = NULL;
121
122 dI("Validating XML signature.");
123
124 /* Init libxslt */
125 /* disable everything */
126 xsltSecPrefs = xsltNewSecurityPrefs();
127 xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_READ_FILE, xsltSecurityForbid);
128 xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid);
129 xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid);
130 xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid);
131 xsltSetSecurityPrefs(xsltSecPrefs, XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid);
132 xsltSetDefaultSecurityPrefs(xsltSecPrefs);
133
134 /* Init xmlsec library */
135 if (xmlSecInit() < 0) {
136 oscap_seterr(OSCAP_EFAMILY_XML, "Xmlsec initialization failed.");
137 return(-1);
138 }
139
140 /* Check loaded library version */
141 if (xmlSecCheckVersion() != 1) {
142 oscap_seterr(OSCAP_EFAMILY_XML, "Loaded xmlsec library version is not compatible.");
143 return(-1);
144 }
145
146 /* Init crypto library */
147 if (xmlSecCryptoAppInit(NULL) < 0) {
148 oscap_seterr(OSCAP_EFAMILY_XML, "Crypto initialization failed.");
149 return(-1);
150 }
151
152 /* Init xmlsec-crypto library */
153 if (xmlSecCryptoInit() < 0) {
154 oscap_seterr(OSCAP_EFAMILY_XML, "Xmlsec-crypto initialization failed.");
155 return(-1);
156 }
157
158 /* find Signature node */
159 xmlNodePtr root = xmlDocGetRootElement(doc);
160 node = xmlSecFindNode(root, xmlSecNodeSignature, xmlSecDSigNs);
161 if (node == NULL) {
162 if (enforce_signature) {
163 oscap_seterr(OSCAP_EFAMILY_XML, "Signature not found");
164 goto cleanup;
165 }
166 /* Non-fatal error, datastreams without signatures are accepted */
167 dI("Signature node not found");
168 res = 0;
169 goto cleanup;
170 }
171
172 /* Adds id attibutes of ds:component and ds:data-stream as general id
173 * attributes
174 */
175 if (scap_type == OSCAP_DOCUMENT_SDS && _mark_sds_id_elements(doc) < 0) {
176 goto cleanup;
177 }
178
179 /* create and initialize keys manager */
180 mngr = xmlSecKeysMngrCreate();
181 if (mngr == NULL) {
182 oscap_seterr(OSCAP_EFAMILY_XML, "failed to create keys manager");
183 goto cleanup;
184 }
185 if (xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) {
186 oscap_seterr(OSCAP_EFAMILY_XML, "failed to initialize keys manager");
187 goto cleanup;
188 }
189
190 /* create signature context */
191 dsigCtx = xmlSecDSigCtxCreate(mngr);
192 if (dsigCtx == NULL) {
193 oscap_seterr(OSCAP_EFAMILY_XML, "failed to create signature context");
194 goto cleanup;
195 }
196
197 /* XMLSec 1.3 API Change: Enable KeyValue reading in the signature context */
198 xmlSecKeyDataId kv_data_id = xmlSecKeyDataIdListFindByName(xmlSecKeyDataIdsGet(), BAD_CAST "key-value", xmlSecKeyDataUsageAny);
199 xmlSecKeyDataId rsa_data_id = xmlSecKeyDataIdListFindByName(xmlSecKeyDataIdsGet(), BAD_CAST "rsa", xmlSecKeyDataUsageAny);
200
201 res = xmlSecPtrListAdd(&(dsigCtx->keyInfoReadCtx.enabledKeyData), (const xmlSecPtr)kv_data_id);
202 if (res < 0) {
203 oscap_seterr(OSCAP_EFAMILY_XML, "failed to enable key data: key-value");
204 goto cleanup;
205 }
206 res = xmlSecPtrListAdd(&(dsigCtx->keyInfoReadCtx.enabledKeyData), (const xmlSecPtr)rsa_data_id);
207 if (res < 0) {
208 oscap_seterr(OSCAP_EFAMILY_XML, "failed to enable key data: rsa");
209 goto cleanup;
210 }
211
212 /* Verify signature */
213 if (xmlSecDSigCtxVerify(dsigCtx, node) < 0) {
214 oscap_seterr(OSCAP_EFAMILY_XML, "Signature verification failed");
215 goto cleanup;
216 }
217
218 if (dsigCtx->status == xmlSecDSigStatusSucceeded) {
219 dI("Signature is OK");
220 res = 0;
221 } else {
222 dI("Signature is invalid.");
223 res = 1;
224 }
225
226 /* compare good/bad references */
227 int good = 0;
228 xmlSecDSigReferenceCtxPtr dsigRefCtx;
229 int size = xmlSecPtrListGetSize(&(dsigCtx->signedInfoReferences));
230 for (int i = good = 0; i < size; i++) {
231 dsigRefCtx = (xmlSecDSigReferenceCtxPtr)xmlSecPtrListGetItem(&(dsigCtx->signedInfoReferences), i);
232 if(dsigRefCtx == NULL) {
233 oscap_seterr(OSCAP_EFAMILY_XML, "Reference ctx is null");
234 goto cleanup;
235 }
236 if (dsigRefCtx->status == xmlSecDSigStatusSucceeded)
237 good++;
238 }
239 dI("SignedInfo references (ok/all): %d/%d", good, size);
240 if (good != size) {
241 res = 1;
242 }
243
244 /* compare good/bad manifests */
245 size = xmlSecPtrListGetSize(&(dsigCtx->manifestReferences));
246 for (int i = good = 0; i < size; i++) {
247 dsigRefCtx = (xmlSecDSigReferenceCtxPtr)xmlSecPtrListGetItem(&(dsigCtx->manifestReferences), i);
248 if (dsigRefCtx == NULL) {
249 oscap_seterr(OSCAP_EFAMILY_XML, "Reference ctx is null");
250 goto cleanup;
251 }
252 if (dsigRefCtx->status == xmlSecDSigStatusSucceeded)
253 good++;
254 }
255 dI("Manifests references (ok/all): %d/%d", good, size);
256 if (good != size) {
257 res = 1;
258 }
259 if (res == 0) {
260 printf("XML signature is valid.\n");
261 }
262
263 cleanup:
264 /* cleanup */
265 if (dsigCtx != NULL)
266 xmlSecDSigCtxDestroy(dsigCtx);
267
268 /* destroy keys manager */
269 if (mngr != NULL)
270 xmlSecKeysMngrDestroy(mngr);
271
272 /* Shutdown xmlsec-crypto library */
273 xmlSecCryptoShutdown();
274
275 /* Shutdown crypto library */
276 xmlSecCryptoAppShutdown();
277
278 /* Shutdown xmlsec library */
279 xmlSecShutdown();
280
281 /* Shutdown libxslt/libxml */
282 xsltFreeSecurityPrefs(xsltSecPrefs);
283
284 /* Unset the default XSLT security settings to prevent
285 * blockage of other oscap XSLT features (generate guide etc.)
286 */
287 xsltSetDefaultSecurityPrefs(NULL);
288
289 return res;
290 }
291
292 int oscap_signature_validate(struct oscap_source *source, struct oscap_signature_ctx *ctx, bool enforce_signature)
293 {
294 int ret = 0;
295 oscap_document_type_t scap_type = oscap_source_get_scap_type(source);
296 const char *origin = oscap_source_readable_origin(source);
297
298 if (scap_type == OSCAP_DOCUMENT_UNKNOWN) {
299 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unrecognized document type for: %s", origin);
300 ret = -1;
301 } else if (scap_type == OSCAP_DOCUMENT_SDS) {
302 xmlDocPtr doc = oscap_source_get_xmlDoc(source);
303 ret = _oscap_signature_validate_doc(doc, scap_type, ctx, enforce_signature);
304 } else {
305 const char *type_name = oscap_document_type_to_string(scap_type);
306 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported document type %s for XML signature validation: %s", type_name, origin);
307 ret = -1;
308 }
309 return ret;
310 }