Debian Codegraph @

doc/case-study/openssl.md

raw
1 # Demo of ESSTRA Usage on OpenSSL Package
2
3 In this demo, we will explain how to use ESSTRA with a popular open-source package
4 "OpenSSL". This document is structured as a step-by-step guide, allowing you to easily
5 understand the basic operations of ESSTRA by following each step in order.
6
7 First, we will build the OpenSSL package using the ESSTRA Core to generate the ELF
8 files. These ELF files include metadata containing information about all the source files
9 involved in the compilation, such as absolute paths and checksums. We will verify this using
10 the ESSTRA Utility.
11
12 Next, we will demonstrate how to use the feature of the ESSTRA Utility that adds
13 license information of source files to the metadata. In this demo, we will
14 use the open-source software license analysis tool
15 [FOSSology](https://github.com/fossology/fossology)
16 to scan the source code for licenses and generate a license information
17 file. We will then update the metadata using the ESSTRA Utility based on this
18 information and verify that the license information has been correctly added to
19 the metadata.
20
21 The operational procedures for the license analysis tool FOSSology are also explained in this
22 guide.
23
24 Finally, we calculate the effectiveness of ESSTRA in license analysis activities.
25
26 ## ESSTRA Build Instructions
27
28 ### Build and Install ESSTRA
29
30 Before building the ESSTRA Core (`esstracore.so`) GCC plugin, you need to install a package on
31 your system. For Debian/Ubuntu, first check the version of GCC:
32
33 ```sh
34 $ gcc --version
35 gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
36 ...
37 ```
38
39 In this case, the major version is 11, so install the package `gcc-11-plugin-dev`:
40
41 ```sh
42 $ sudo apt install gcc-11-plugin-dev
43 ```
44
45 Next, run `make` in the top directory:
46
47 ```sh
48 $ make
49 ```
50
51 If there are no errors, the build is complete. Then, run:
52
53 ```sh
54 $ sudo make install
55 ```
56
57 and `esstracore.so` will be installed in `/usr/local/lib/gcc/<gcc-arch>/<gcc-major-version>/plugin/`.
58
59 ### Install a Spec File
60
61 ```sh
62 $ sudo make install-specs
63 ```
64
65 This command installs a
66 [GCC spec file](https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html)
67 on your system which enables the option:
68
69 * `-fplugin=/usr/local/lib/gcc/<gcc-arch>/<gcc-major-version>/plugin/esstracore.so`
70
71 by default.
72
73 This enables `esstracore.so` to be invoked whenever `gcc/g++` is used.
74
75 To disable this, type:
76
77 ```sh
78 $ sudo make uninstall-specs
79 ```
80
81 to remove the spec file.
82
83 ## Build OpenSSL Source with ESSTRA Core
84
85 ### Download the Source
86
87 Download the source of OpenSSL from <https://github.com/openssl/openssl/releases/latest>.
88 The version, at the time of writing, is 3.4.1, which is used for the demo.
89
90 ```bash
91 $ wget https://github.com/openssl/openssl/releases/download/openssl-3.4.1/openssl-3.4.1.tar.gz
92 $ tar -xf openssl-3.4.1.tar.gz
93 ```
94
95 ### Build OpenSSL
96
97 We will follow the
98 [official build instructions](https://github.com/openssl/openssl/blob/ddd7ecb04bcea5c13be3c73f3dc1a101087cdf24/INSTALL.md)
99 to build OpenSSL.
100
101 #### Install Prerequisites
102
103 For Debian/Ubuntu, the following command installs the required prerequisites mentioned in the
104 [documentation](https://github.com/openssl/openssl/blob/ddd7ecb04bcea5c13be3c73f3dc1a101087cdf24/INSTALL.md):
105
106 ```sh
107 $ sudo apt install -y perl build-essential
108 ```
109
110 #### Build OpenSSL
111
112 ```sh
113 $ cd openssl-3.4.1
114 openssl-3.4.1$ ./Configure no-tests
115 openssl-3.4.1$ make
116 ```
117
118 ### Find ELF Files Generated During the Build
119
120 We will use `BuildID` to find the ELF files.
121 A `BuildID` is a unique identifier assigned to a specific build of a binary, embedded within the ELF file itself.
122
123 ```sh
124 openssl-3.4.1$ find ./ -type f -exec sh -c "file '{}' | grep -q 'BuildID'" \; -print
125 ./libssl.so.3
126 ./libcrypto.so.3
127 ./util/quicserver
128 ./engines/ossltest.so
129 ./engines/padlock.so
130 ./engines/loader_attic.so
131 ./engines/afalg.so
132 ./engines/capi.so
133 ./engines/dasync.so
134 ./apps/openssl
135 ./providers/legacy.so
136
137 ```
138
139 ### Run ESSTRA Util Script to Get Source Files Used in the ELF
140
141 #### Prerequisite
142
143 Since the ESSTRA Utility depends on the [PyYAML](https://pyyaml.org/)
144 module to handle YAML data, you may need to install it by typing:
145
146 ```sh
147 $ pip install pyyaml
148 or
149 $ sudo apt install python3-yaml
150 ```
151
152 #### Run Util Script on the Generated ELF Files.
153
154 For the demonstration, we will use a single ELF file `openssl-3.4.1/apps/openssl` for
155 simplicity.
156
157 The information embedded by ESSTRA increases the size of the ELF files. We can reduce the size
158 of ELF files by eliminating duplication in the embedded information as below:
159
160 ```sh
161 $ du -sh ../openssl-3.4.1/apps/openssl
162 2.6M ../openssl-3.4.1/apps/openssl
163
164 $ esstra shrink ../openssl-3.4.1/apps/openssl
165 * processing '../openssl-3.4.1/apps/openssl'...
166 * done.
167
168 $ du -sh ../openssl-3.4.1/apps/openssl
169 1.3M ../openssl-3.4.1/apps/openssl
170 ```
171
172 Next, display the information embedded by ESSTRA:
173
174 ```sh
175 $ esstra show ../openssl-3.4.1/apps/openssl
176 ```
177
178 This will give an output as follows:
179
180 ```yaml
181 #
182 # BinaryFileName: ../openssl-3.4.1/apps/openssl
183 # BinaryPath: /home/openssl-3.4.1/apps/openssl
184 #
185 SourceFiles:
186 /home/openssl-3.4.1/apps:
187 - File: info.c
188 SHA1: 51aa1d9f96d789cd3fe178fd67ca758c4ab877ee
189 - File: kdf.c
190 SHA1: 49ba6552b654317b940599cfb964e7c0397c7121
191 - File: list.c
192 SHA1: 13ec3cdb556dfd35d723bd460c3253718f5358d4
193 - File: mac.c
194 SHA1: c295c80b647ff6a6667caa603f89ad436c29fd93
195 - File: nseq.c
196 SHA1: e4b69588cb1ebba0d084ea3f44bc2cd3ff9ff0a5
197 - File: ocsp.c
198 SHA1: 097dd05d023351fa1a418bf2988b544de77225f3
199 - File: openssl.c
200 SHA1: def2983007e42be1f31ebb31df23f3f51266ce71
201 - File: passwd.c
202 SHA1: 06bfce3822d37ec832f3eb35f4caa7f8e72cd2de
203 - File: pkcs12.c
204 SHA1: 0caa984c8de334109570a7a8c9bf927ba5386077
205 - File: pkcs7.c
206 SHA1: 5b6f326f2b8e53a0a6a20b6e37f69453a9cea3ce
207 - File: pkcs8.c
208 SHA1: 3c394e3f4567b67bd5b1c469d5ef14b7e00dab8b
209 - File: pkey.c
210 SHA1: 7e0fa21b1ee1d8e4e64e78ab91393a77c048f305
211 :
212 (snip)
213 :
214 ```
215
216 > [!NOTE]
217 > You can provide multiple ELF files as arguments to the ESSTRA Utility.
218
219 The complete output in YAML format can be found in
220 [yaml-report](./output-examples/openssl/esstra_scan_on_openssl_elf.yaml).
221
222 ## License Analysis Using FOSSology
223
224 The ESSTRA Utility includes a feature that adds license information to the metadata of each
225 file.
226 Below are the steps to add the license information of the OpenSSL source files to the metadata
227 of the binaries/ELFs (generated during build) using this feature.
228
229 As a preliminary step, you need to create a document in the
230 [SPDX 2.3 tag-value format](https://spdx.github.io/spdx-spec/v2.3/)
231 that includes the license information of the source files.
232 Here, we will demonstrate how to use the open-source software license analysis tool
233 [FOSSology](https://github.com/fossology/fossology) to achieve this.
234
235 ### Starting the FOSSology Server
236
237 We will use the FOSSology container image provided on
238 [Docker Hub](https://hub.docker.com/).
239
240 We will be using version 4.1.0 for this demo.
241
242 Pull the FOSSology container image with the following command:
243
244 ```sh
245 $ docker pull fossology/fossology:4.1.0
246 ```
247
248 Once the image is successful pulled, start the FOSSology server with the following command:
249
250 ```sh
251 $ docker run -p 8081:80 fossology/fossology:4.1.0
252 ```
253
254 This will allow you to access the FOSSology server via port 8081 on the
255 host. Open a web browser and go to:
256
257 * `http://<host_ip_addr>:8081/repo`
258
259 When accessing FOSSology, make sure to append `/repo` to the URL.
260
261 Then the FOSSology startup screen will appear in your browser.
262
263 ### Analyzing Source Code with FOSSology
264
265 Refer to
266 [this guide](https://github.com/sony/esstra/blob/main/samples/hello/README_FOSSOLOGY.md)
267 for a step-by-step manual approach to analyze the source code with FOSSology and generate a
268 report.
269
270 Here, we will use another approach to analyze the source code with FOSSology and generate a
271 report using the tool [FOSSology.REST.shell](https://github.com/fossology/FOSSology.REST.shell)
272
273 #### Download FOSSology.REST.shell and Install its Prerequisites
274
275 ```sh
276 $ git clone https://github.com/fossology/FOSSology.REST.shell.git
277 $ apt install -y jq curl
278 ```
279
280 #### Upload and Scan the OpenSSL Source Files
281
282 ```sh
283 FOSSology.REST.shell$ ./upload-rest.sh -n fossy -p fossy -r http://localhost:8081/repo/api/v1 -i ../openssl-3.4.1.tar.gz
284 Rest Client: Version 1.3
285 :
286 (snip)
287 :
288 Upload file: ../openssl-3.4.1.tar.gz
289
290 - Upload ID: 2
291 - Job ID : 5
292 - Group ID : 3
293 - Job ETA : 0
294
295 Unpack job: started
296 :
297 (snip)
298 :
299 ```
300
301 We will use the Upload ID provided in the log to download the report.
302
303 #### Download the Scan Report
304
305 ```sh
306 FOSSology.REST.shell$ ./download-rest.sh -F spdx2tv -r http://localhost:8081/repo/api/v1 -u 2 -n fossy -p fossy
307 :
308 (snip)
309 :
310 ======================================
311 === Download report
312 ======================================
313
314 Output directory: /home/FOSSology.REST.shell
315 Downloading file
316 Downloaded file: /home/FOSSology.REST.shell/SPDX2TV_openssl-3.4.1.tar.gz.spdx
317
318 ======================================
319 === End
320 ======================================
321 ```
322
323 Below is a portion of the downloaded file
324 [`SPDX2TV_openssl-3.4.1.tar.gz.spdx`](./output-examples/openssl/SPDX2TV_openssl-3.4.1.tar.gz.spdx):
325
326 ```yaml
327 SPDXVersion: SPDX-2.2
328 DataLicense: CC0-1.0
329
330 ##-------------------------
331 ## Document Information
332 ##-------------------------
333
334 DocumentNamespace: http://1f9d9e80ed71/repo/SPDX2TV_openssl-3.4.1.tar.gz.spdx
335 DocumentName: /srv/fossology/repository/report
336 SPDXID: SPDXRef-DOCUMENT
337
338 ##-------------------------
339 ## Creation Information
340 ##-------------------------
341
342 Creator: Tool: spdx2
343 Creator: Person: fossy (y)
344 CreatorComment: <text>
345 This document was created using license information and a generator from Fossology.
346 </text>
347 Created: 2025-03-05T08:31:25Z
348 LicenseListVersion: 2.6
349
350 ##-------------------------
351 ## Package Information
352 ##-------------------------
353
354
355 PackageName: openssl-3.4.1.tar.gz
356 PackageFileName: openssl-3.4.1.tar.gz
357
358 :
359 (snip)
360 :
361 ```
362
363 ## Adding License Information to Metadata
364
365 To add license information to the metadata in the binary using the ESSTRA Utility
366 with the
367 [`SPDX2TV_openssl-3.4.1.tar.gz.spdx`](./output-examples/openssl/SPDX2TV_openssl-3.4.1.tar.gz.spdx)
368 file downloaded from FOSSology, run the following command:
369
370 ```sh
371 $ esstra update ../openssl-3.4.1/apps/openssl -i SPDX2TV_openssl-3.4.1.tar.gz.spdx
372 * processing '../openssl-3.4.1/apps/openssl'...
373 * done.
374 ```
375
376 If no errors occur, the process is successful. To display the metadata content
377 of the ELF `openssl-3.4.1/apps/openssl`, run:
378
379 ```sh
380 $ esstra show ../openssl-3.4.1/apps/openssl
381 ```
382
383 The result will be as follows:
384
385 ```yaml
386 #
387 # BinaryFileName: ../openssl-3.4.1/apps/openssl
388 # BinaryPath: /home/openssl-3.4.1/apps/openssl
389 #
390 SourceFiles:
391 /home/openssl-3.4.1/apps:
392 - File: info.c
393 LicenseInfo:
394 - OpenSSL
395 - Apache-2.0
396 SHA1: 51aa1d9f96d789cd3fe178fd67ca758c4ab877ee
397 - File: kdf.c
398 LicenseInfo:
399 - Apache-2.0
400 - OpenSSL
401 SHA1: 49ba6552b654317b940599cfb964e7c0397c7121
402 - File: list.c
403 LicenseInfo:
404 - OpenSSL
405 - Apache-2.0
406 SHA1: 13ec3cdb556dfd35d723bd460c3253718f5358d4
407 - File: mac.c
408 LicenseInfo:
409 - OpenSSL
410 - Apache-2.0
411 SHA1: c295c80b647ff6a6667caa603f89ad436c29fd93
412 - File: nseq.c
413 LicenseInfo:
414 - Apache-2.0
415 - OpenSSL
416 ```
417
418 The complete output of the ESSTRA Utility, including license information, can be found
419 [here](./output-examples/openssl/esstra_show_openssl_result_with_license_info.yaml).
420
421 Please note that the file
422 [`SPDX2TV_openssl-3.4.1.tar.gz.spdx`](./output-examples/openssl/SPDX2TV_openssl-3.4.1.tar.gz.spdx),
423 generated previously by FOSSology, only contains license information
424 for the files present in the
425 [OpenSSL repository](https://github.com/openssl/openssl).
426 Therefore, license information will not be assigned to files
427 other than OpenSSL source files in the metadata of the ELF `./openssl-3.4.1/apps/openssl`.
428
429 To add license information for those files, you can use FOSSology or similar tools to identify
430 their licenses and generate an SPDX tag-value format file.
431 By passing the file to the ESSTRA Utility, you can add license information to the metadata in
432 the binary.
433
434 ## Analyze Effectiveness of ESSTRA for OpenSSL
435
436 One way to measure effectiveness is to consider the reduction in the number of files that
437 require manual review. This reduction not only saves time but also enhances quality by reducing
438 human error.
439
440 ### Get All Files in the Source Directory
441
442 ```sh
443 openssl-3.4.1$ find ./ -type f | wc -l
444 9625
445 ```
446
447 ### Calculate Unique Source Files Given by the ESSTRA
448
449 Run the ESSTRA Utility on all the generated ELF files and redirect output to a file.
450
451 ```sh
452 openssl-3.4.1$ esstra show $(find ./ -type f -exec sh -c "file '{}' | grep -q 'BuildID'" \; -print) >../esstra_scan_result.yaml
453 ```
454
455 The generated output above contains the details of all the source files used during the build,
456 including the system files.
457 To find the unique OpenSSL source files used during the build, we exclude all non-OpenSSL
458 source files.
459
460 Below, we split the details of all ELFs in `esstra_scan_result.yaml` into separate YAML files
461 for each ELF and count the 'SHA1' under the OpenSSL source directory to get unique source files
462 used in all the ELFs.
463
464 ```sh
465 $ awk '/^SourceFiles:/{
466 if(output) close(output);
467 output = "elf_output_" ++i ".yaml"
468 }
469 output {
470 print > output
471 }' esstra_scan_result.yaml
472
473 $ ls elf_output_*
474 elf_output_1.yaml elf_output_11.yaml elf_output_2.yaml elf_output_4.yaml elf_output_6.yaml elf_output_8.yaml
475 elf_output_10.yaml elf_output_12.yaml elf_output_3.yaml elf_output_5.yaml elf_output_7.yaml elf_output_9.yaml
476
477 $ yq '(.SourceFiles | to_entries | map(select(.key == "/home/openssl-3.4.1/*") | .value) | flatten | unique_by(.SHA1) | .[].SHA1)' elf_output_* | sort -u | wc -l
478 1606
479 ```
480
481 Thus, `1606` unique OpenSSL source files were used during the build to generate the ELF files.
482
483 ### Calculating Effectiveness of ESSTRA
484
485 **The percentage of source files reported by ESSTRA can be calculated as follows:**
486
487 ```math
488 \frac{\text{count of unique source files}}{\text{count of all source files}} \times 100
489 = (1606 / 9625) * 100
490 = 16.685 %
491 ```
492
493 This means that out of all the files, only **16.685%** were used in the generated ELFs.
494
495 ## Summary
496
497 In this demo, we first compiled the OpenSSL source files using the ESSTRA Core
498 to generate the ELFs and confirmed that the metadata in the generated ELF
499 includes information about all the files involved in the compilation.
500
501 Next, we used the ESSTRA Utility to add license information to the metadata of the generated ELF.
502 To generate the license information, we demonstrated how to use the
503 open-source license analysis tool
504 [FOSSology](https://github.com/fossology/fossology)
505 to scan the licenses of all the files in the
506 [OpenSSL repository](https://github.com/openssl/openssl)
507 and generate an SPDX 2.3 tag-value format file.
508
509 Finally, we analyzed the effectiveness of ESSTRA in the license analysis of the ELFs.