summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/cvss/cvss.dtx
blob: 522be904cb12521b214b29265de16fc19d5ca51e (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
% \iffalse meta-comment
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
cvss --- A Package to compute and display CVSS base scores
E-mail: pierre@vivegnis.be
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

The LaTeX package allows a user to compute the CVSS base score for
an input CVSS vector, as well as other artifacts (CVSS level etc)
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
cvss --- A package to compute and display CVSS base scores
E-mail: pierre@vivegnis.be
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

\endpreamble
\postamble

Copyright (C) 2022 by Pierre VIVEGNIS <pierre@vivegnis.be>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version.  The latest version of this license is in the file:

http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
Pierre VIVEGNIS.

This work consists of the file  cvss.dtx
and the derived files           cvss.ins,
                                cvss.pdf and
                                cvss.sty.

\endpostamble
\usedir{tex/latex/cvss}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/cvss}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/cvss}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{cvss}[2022/11/03 First Release]
%</package>
%<*driver>
\documentclass{l3doc}
\usepackage{\jobname}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\setlength{\parindent}{0pt}
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \begin{documentation}
%
%\GetFileInfo{\jobname.sty}
%
%\title{
%  The \textsf{cvss} package\thanks{
%    This file describes version \fileversion, last revised \filedate.
%  }
%}
%\author{
%  Pierre VIVEGNIS\thanks{E-mail: pierre@vivegnis.be}
%}
%\date{Released \filedate}
%
%\maketitle
% \tableofcontents
%
% \section{Introduction}
%
% The \textsf{cvss} package allows the user to compute CVSS3.1 base scores and use them in documents. The Common Vulnerability Scoring System (CVSS) is an open framework for communicating the characteristics and severity of software vulnerabilities. CVSS consists of three metric groups: Base, Temporal, and Environmental.
%
% This packages only deal with Base score. Temporal and Environental scores will be part of a future release.
%
% More information can be found at \texttt{https://www.first.org/cvss/specification-document}.
%
% \section{Acknowledgements}
%
% I want to thank Alexander Lill who first created a cvss project in \LaTeX (available at \texttt{https://github.com/AlexanderLill/cvss3tex}).
%
% \section{Usage}
%
% The goal of this package is to compute the CVSS base score for an input CVSS vector, and to give the user macro to output it in 3 different forms
% \begin{itemize}
%     \item The CVSS \textbf{score} (fron 0.0 to 10)
%     \item the \textbf{level} (None, Info, Low, Medium, High or Critical)
%     \item the \textbf{colored level}
%     \item the \textbf{tag} which is a colored frame around the level
% \end{itemize}
%
% All macros are expandable, which makes them usable in any context.
%
% The macros of this packages are divided in 2 categories: 
% \begin{itemize}
%      \item \textbf{direct macros} : that will take as input the CVSS base score and give you the result
%   \item \textbf{indirect macros} : that are intermediary, in the way that they only compute a form based on the precedent one.
% \end{itemize}
%
% \subsection{Direct Macros}
% \begin{function}{\cvssScore}
     %   \begin{syntax}
          %     \cs{cvssScore} \Arg{CVSS string}
          %   \end{syntax}
% This is the main macro of this package, responsible for computing the base CVSS 3.1 score of an \marg{input vector} (without \texttt{CVSS3.1/}). The output of this macro is a floating point CVSS score, for example \texttt{5.4}.
% \end{function}
%
%   \noindent\llap{\bfseries \textcolor{red}{Important!}\qquad}\indent
%   \textbf{The CVSS vector string must be stripped from the \texttt{CVSS3.1/}!}
%
%
% \begin{verbatim}
% \cvssScore{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}
% \end{verbatim}
%
% This will output the following CVSS base score: \texttt{\cvssScore{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}}
%
%
% \begin{function}{\cvssScorepretty}
     %   \begin{syntax}
          %     \cs{cvssScorepretty} \Arg{CVSS string}
          %   \end{syntax}
     % This macro will print a \textbf{colored} base CVSS 3.1 score of an \marg{input vector} (without \texttt{CVSS3.1/}). The output of this macro is a floating point CVSS score.
     % \end{function}
%
% \begin{verbatim}
% \cvssScorepretty{AV:N/AC:H/PR:H/UI:R/S:U/C:H/I:L/A:N}
% \end{verbatim}
%
% This will output the following CVSS score: \cvssScorepretty{AV:N/AC:H/PR:H/UI:R/S:U/C:H/I:L/A:N}
%
%
% \begin{function}{\cvssLevel}
     %   \begin{syntax}
     %     \cs{cvssLevel} \Arg{CVSS string}
     %   \end{syntax}
% This macro will output the CVSS level from an \marg{input vector} (without \texttt{CVSS3.1/}), for example \texttt{Info}.
% \end{function}
%
% \begin{verbatim}
% \cvssLevel{AV:A/AC:H/PR:H/UI:R/S:U/C:H/I:L/A:N}
% \end{verbatim}
%
% This will output the following CVSS level: \cvssLevel{AV:A/AC:H/PR:H/UI:R/S:U/C:H/I:L/A:N}
%
%
% \begin{function}{\cvssLevelpretty}
     %   \begin{syntax}
     %     \cs{cvssLevelpretty} \Arg{CVSS string}
     %   \end{syntax}
% This macro will output the \textbf{colored} CVSS level from an \marg{input vector} (without \texttt{CVSS3.1/}).
% \end{function}
%
% \begin{verbatim}
% \cvssLevelpretty{AV:A/AC:H/PR:H/UI:R/S:U/C:L/I:L/A:N}
% \end{verbatim}
%
% This will output the following CVSS level: \cvssLevelpretty{AV:A/AC:H/PR:H/UI:R/S:U/C:L/I:L/A:N}
%
%
% \begin{function}{\cvssTag}
     %   \begin{syntax}
     %     \cs{cvssTag} \Arg{CVSS string}
     %   \end{syntax}
% This macro will output a colored tag with the CVSS level inside, from an \marg{input vector} (without \texttt{CVSS3.1/}).
% \end{function}
%
% \begin{verbatim}
% \cvssTag{AV:A/AC:H/PR:H/UI:R/S:U/C:N/I:N/A:N}
% \end{verbatim}
%
% This will output the following CVSS level: \cvssTag{AV:A/AC:H/PR:H/UI:R/S:U/C:N/I:N/A:N}.
%
%
% \begin{function}{\cvssPrint}
     %   \begin{syntax}
          %     \cs{cvssPrint} \Arg{CVSS string}
          %   \end{syntax}
     % This macro will print all details of a CVSS string: colored level, score, and hyperlink to FIRST calculator, from an \marg{input vector} (without \texttt{CVSS3.1/}).
     % \end{function}
%
% \begin{verbatim}
% \cvssPrint{AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H}
% \end{verbatim}
%
% This will output the following CVSS level:
%
% \cvssPrint{AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H}
%
%
%
% \subsection{Indirect Macros}
%
% \changes{v1.0}{2022/11/03}{First public release}
%
% \begin{function}{\category}
     %   \begin{syntax}
     %     \cs{category} \Arg{CVSS score}
     %   \end{syntax}
% This macro will output the CVSS category (None, Info, Low, Medium, High or Critical) based on the input CVSS vector passed as argument \marg{numerical score}. The mandatory argument is a floating point CVSS score, for example \texttt{5.4}.
% \end{function}
%
% \begin{verbatim}
% \category{9.9}
% \end{verbatim}
%
% This will output the following scope: \category{9.9}.
%
%
% \begin{function}{\cvssFrame}
     %   \begin{syntax}
          %     \cs{cvssFrame} \Arg{CVSS score}
          %   \end{syntax}
     % This macro will output a CVSS tag based on a CVSS \textbf{level} passed as argument. The mandatory argument must be one of the defined CVSS levels (None, Info, Low, Medium, High or Critical), for example \texttt{Info}.
     % \end{function}
%
% \begin{verbatim}
% \cvssFrame{High}
% \end{verbatim}
%
% This will output the following tag: \cvssFrame{High}.
%
%

% \section{Examples}
%
% \subsection{Direct Form}

%\begin{minipage}{.75\textwidth}
     % \begin{verbatim}
%\\cvssScore{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}
     % \end{verbatim}
     %\end{minipage}%
%\begin{minipage}{0.25\textwidth}
%\cvssScore{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}
     %\end{minipage}

%\begin{minipage}{.75\textwidth}
     % \begin{verbatim}
%\\cvssLevel{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}
     % \end{verbatim}
     %\end{minipage}%
%\begin{minipage}{0.25\textwidth}
%\cvssLevel{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}
     %\end{minipage}


%\begin{minipage}{.75\textwidth}
     % \begin{verbatim}
%\\cvssLevelpretty{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:H/A:H}
     % \end{verbatim}
     %\end{minipage}%
%\begin{minipage}{0.25\textwidth}
%\cvssLevelpretty{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:H/A:H}
     %\end{minipage}

%\begin{minipage}{.75\textwidth}
     % \begin{verbatim}
%\\cvssTag{AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H}
     % \end{verbatim}
     %\end{minipage}%
%\begin{minipage}{0.25\textwidth}
%\cvssTag{AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H}
     %\end{minipage}

%
% The vuln has a  \textbf{\cvssLevel{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}}-level and we can output it inline.
%\subsection{Imbricated Form}

%\begin{minipage}{.5\textwidth}
     % \begin{verbatim}
     %\cvssFrame{Low}
     % \end{verbatim}
     %\end{minipage}%
%\begin{minipage}{0.5\textwidth}
     %\cvssFrame{Low}
%\end{minipage}

%\begin{minipage}{.5\textwidth}
%     \begin{verbatim}
     %\category{9.9}
%     \end{verbatim}
%\end{minipage}%
%\begin{minipage}{0.5\textwidth}
     %\category{9.9}
%\end{minipage}
%
% We can even combine them:
% \begin{verbatim}
%\\category{\cvssScore{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}}
% \end{verbatim}
%
%And this outputs: \category{\cvssScore{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}}
%
% \begin{verbatim}
%\\cvssFrame{\category{\cvssScore{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}}}
% \end{verbatim}
% And the result is: %\cvssFrame{\category{\cvssScore{AV:L/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N}}}
%
%\subsection{Test Computations}
% \begin{verbatim}
% Should be 7.3: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:L}
%
% Should be 8.3: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:L/A:L}
%
% Should be 9.9: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:L/A:H}
%
% Should be 9.9: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:H/A:L}
%
% Should be 7.2: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:L/A:N}
%
% Should be 7.1: \cvssScore{AV:A/AC:L/PR:N/UI:N/S:C/C:L/I:L/A:L}
%
% Should be 5.8: \cvssScore{AV:A/AC:H/PR:N/UI:N/S:C/C:L/I:L/A:L}
%
% Should be 5.5: \cvssScore{AV:A/AC:H/PR:L/UI:N/S:C/C:L/I:L/A:L}
%
% Should be 5.1: \cvssScore{AV:A/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L}
%
% Should be 4.3: \cvssScore{AV:A/AC:H/PR:L/UI:R/S:U/C:L/I:L/A:L}
%
% Should be 2.4: \cvssScore{AV:N/AC:L/PR:H/UI:R/S:U/C:L/I:N/A:N}
%
% Should be 0.0: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:N/I:N/A:N}
% \end{verbatim}
%
% And the results of the computations:
%
%Should be 7.3: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:L}
%
%Should be 8.3: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:L/A:L}%
%
%Should be 9.9: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:L/A:H}%
%
%Should be 9.9: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:H/A:L}%
%
%Should be 7.2: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:L/I:L/A:N}%
%
%Should be 7.1: \cvssScore{AV:A/AC:L/PR:N/UI:N/S:C/C:L/I:L/A:L}%
%
%Should be 5.8: \cvssScore{AV:A/AC:H/PR:N/UI:N/S:C/C:L/I:L/A:L}%
%
%Should be 5.5: \cvssScore{AV:A/AC:H/PR:L/UI:N/S:C/C:L/I:L/A:L}%
%
%Should be 5.1: \cvssScore{AV:A/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L}%
%
%Should be 4.3: \cvssScore{AV:A/AC:H/PR:L/UI:R/S:U/C:L/I:L/A:L}%
%
%Should be 2.4: \cvssScore{AV:N/AC:L/PR:H/UI:R/S:U/C:L/I:N/A:N}
%
%Should be 0.0: \cvssScore{AV:N/AC:L/PR:N/UI:N/S:C/C:N/I:N/A:N}
%
% \end{documentation}
%\newpage
% \StopEventually{\setlength\IndexMin{200pt}  \PrintIndex  }
% \begin{implementation}
% \section{Implementation}
% \subsection{Initial set up}
%
% Load the essential support (\pkg{expl3}, \pkg{tcolorbox}, \pkg{xstring} and \pkg{hyperref}).
%    \begin{macrocode}
\RequirePackage{expl3}
\RequirePackage[skins]{tcolorbox}
\tcbuselibrary{xparse}
\RequirePackage{xstring}
\RequirePackage{hyperref}
%    \end{macrocode}
%
% Then, we define the thresholds:
%    \begin{macrocode}
% These are the thresholds
\def\scoreLow{0.1}
\def\scoreMed{4.0}
\def\scoreHigh{7.0}
\def\scoreCrit{9.0}
%    \end{macrocode}
% And finally the colors for each level (taken from the FIRST CVSS calulator website\footnote{Available at \url{https://www.first.org/cvss/calculator/3.1}})
%    \begin{macrocode}
\definecolor{color@cvss@None}{RGB}{83, 170, 51}
\definecolor{color@cvss@Low}{RGB}{255, 203, 13}
\definecolor{color@cvss@Medium}{RGB}{249, 160, 9}
\definecolor{color@cvss@High}{RGB}{223, 61, 3}
\definecolor{color@cvss@Critical}{RGB}{204, 5, 0}
%    \end{macrocode}
%
% \subsection{Round up function}
%
% First we defined the \texttt{roundup} function\footnote{This function was inspired by the following posts: \url{https://tex.stackexchange.com/a/615358/28926}} according to the precision mentionned by FIRST (\url{https://www.first.org/cvss/specification-document Appendix A}).
% .
%    \begin{macrocode}
\ExplSyntaxOn
% 
\cs_new:Npn \__CVSS_roundup:n #1 {
         \fp_eval:n { ceil(#1,1) }
         \fp_compare:nT { ceil(#1,1)=ceil(#1,0) } {.0}
}
%    \end{macrocode}
%
% \subsection{Error messages}
% We define some error message to help with the troubleshooting
%
%    \begin{macrocode}
\msg_new:nnn { CVSS } { invalid-option }{ Value~'#2'~invalid~for~#1~#3.}
\msg_new:nnn { CVSS } { invalid-structure } { CVSS~metric~#1~is~not~correct~(#2)~#3.}
\msg_new:nnn { CVSS } { invalid-length } { CVSS~vector~"#1"~is~badly~formatted~#2.}
%    \end{macrocode}%
%
%
% \subsection{CVSS metrics parsing}
%
% Then we can define the numerical values for each of the CVSS metric (Attack Vector, Attack Complexity, ...).
% This is done by checking the string value of the argument, and outputting the correpsondant value.
% For each function, a error message is thrown if the value is not one acceptable for that metric.
%
% \subsubsection{Attack Vector}
% The value for the Attack Vector can only by either \texttt{N} (None), \texttt{A} (Adjacent), \texttt{L} (Local) or \texttt{P} (Physical).
%
%\begin{macro}{\__CVSS_parseAV}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_parseAV:n #1
{
     \str_case_e:nnF {#1}
     {
          { N } { 0.85 } % Network
          { A } { 0.62 } % Adjacent
          { L } { 0.55 } % Local
          { P } { 0.2 }  % Physical
     }
     { \msg_error:nnxxx { CVSS } { invalid-option } { parseAV } {#1} {\msg_line_context:} }
}
%    \end{macrocode}%
%\end{macro}
%
% \subsubsection{Attack Complexity}
% The value for the Attack Complexity metric can only by either \texttt{L} (Low) or \texttt{H} (High).
%
%\begin{macro}{\__CVSS_parseAC}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_parseAC:n #1
{
     \str_case_e:nnF {#1}
     {
          { H } { 0.44 }     % High
          { L } { 0.77 }     % Low
          
     }
     { \msg_error:nnxxx { CVSS } { invalid-option } { parseAC } {#1} {\msg_line_context:} }
}
%    \end{macrocode}
%\end{macro}
%
% \subsubsection{Privileges Required}
% The value for the Privilged Required metric can only by either \texttt{N} (None), \texttt{L} (Low) or \texttt{H} (High).
% Hoever since the computation is different wheter the Scope is changed or not, we've defined 2 functions.
%
% 3 Internal macros are thus used, one per choice (Scope unchanged and Scope change), plus the function to choose which one to take into account.
%
%\begin{macro}{\__CVSS_parsePRScopeUnchanged}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_parsePRScopeUnchanged:n #1
{
     \str_case_e:nnF {#1}
     {
          { N } { 0.85 } % None
          { L } { 0.62 } % Low
          { H } { 0.27 } % High
     }
     { \msg_error:nnxxx { CVSS } { invalid-option } { parsePRScopeUnchanged } {#1} {\msg_line_context:} }
}
%    \end{macrocode}%
%\end{macro}
%\begin{macro}{\__CVSS_parsePRScopeChanged}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_parsePRScopeChanged:n #1
{
     \str_case_e:nnF {#1}
     {
          { N } { 0.85 } % None
          { L } { 0.68 } % Low
          { H } { 0.50 } % High
     }
     { \msg_error:nnxxx { CVSS } { invalid-option } { parsePRScopeChanged } {#1} {\msg_line_context:} }
}
%    \end{macrocode}%
%\end{macro}
%\begin{macro}{\__CVSS_parsePR}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_parsePR:nn #1#2
{
     % #1 Privilege Required
     % #2 Scope
     \str_case_e:nnF {#2}
     {
          { U } { \exp_args:Ne \__CVSS_parsePRScopeUnchanged:n {#1} }
          { C } { \exp_args:Ne \__CVSS_parsePRScopeChanged:n {#1} }
     }
     { \msg_error:nnxxx { CVSS } { invalid-option } { parsePR } {#1} {\msg_line_context:} }
}
%    \end{macrocode}%
%\end{macro}
%
% \subsubsection{User Interaction}
% The value for the User Interaction metric can only by either \texttt{N} (None) or \texttt{R} (Required).
%
%\begin{macro}{\__CVSS_parseUI}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_parseUI:n #1
{
     \str_case_e:nnF {#1}
     {
          { N } { 0.85 } % None
          { R } { 0.62 } % Required
     }
     { \msg_error:nnxxx { CVSS } { invalid-option } { parseUI } {#1} {\msg_line_context:} }
}

%    \end{macrocode}
%\end{macro}
%
% \subsubsection{Confidentiality, Integrity and Availability}
% The value for the Confidentiality, Integrity or Availability metrics can only by either \texttt{N} (None), \texttt{L} (Low) or \texttt{H} (High).
% Since the values are the same for the 3 metrics, we've grouped them together.
%
%\begin{macro}{\__CVSS_parseCIA}
%    \begin{macrocode}

\cs_new:Npn \__CVSS_parseCIA:n #1
{
     \str_case_e:nnF {#1}
     {
          { H } { 0.56 }
          { L } { 0.22 }
          { N } { 0.00 }
     }
     { \msg_error:nnxxx { CVSS } { invalid-option } { parseCIA } {#1} {\msg_line_context:} }
}
%    \end{macrocode}
%\end{macro}
%
% \subsection{CVSS computation}
% \subsubsection{Impact Sub Score (ISS)}
% The value for the Impact Sub-Score (ISS) is conmputed from the Confidentiality, Availability and Integrity values, as follows
%\begin{equation}
%     ISS =      1 - \Bigr[ (1 - \text{Confidentiality}) \times (1 - \text{Integrity}) \times (1 - \text{Availability}) \Bigr]
%\end{equation}
%
% This equation is then translated into \TeX code :
%
%\begin{macro}{\__CVSS_calcISS}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_calcISS:nnn #1#2#3
{
     % #1 Confidentiality Impact %High H, Low L, None N
     % #2 Integrity Impact %High H, Low L, None N
     % #3 Availability Impact %High H, Low L, None N
     1 - ( (1 - (\__CVSS_parseCIA:n {#1})) * (1 - (\__CVSS_parseCIA:n {#2})) * (1 - (\__CVSS_parseCIA:n {#3})) ) 
}
%    \end{macrocode}
%\end{macro}
%
% \subsubsection{Impact}
% The calculations for the impact depends whether the scope is changed or not, and will be computed differently:
%     \begin{equation}
%     \text{Impact} \rightarrow  
%     \begin{cases} 
%          \text{Scope Unchanged}  & 6.42 \times ISS\\
%          \text{Scope Changed}  & 7.52 \times (ISS - 0.029) - 3.25 \times (ISS - 0.02)^{15}\\
%     \end{cases}
%     \end{equation}
%
% This gives the following implementation:
%
%\begin{macro}{\__CVSS_calcImpact}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_calcImpact:nn #1#2
{
     % #1 = Scope
     % #2 = ISS
     % Scope Unchanged 6.42 × ISS
     % Scope Changed 7.52 × [ISS-0.029] - 3.25 × [ISS-0.02]15
     \str_case_e:nnF {#1}
     {
          { U } { \fp_eval:n { 6.42 * (#2) } } % Scope UNCHANGED
          { C } { \fp_eval:n { 7.52 * ( (#2) - 0.029 ) - 3.25 * ( (#2) - 0.02 )^15 } } % Scope CHANGED
     }
     { \msg_error:nnxxx { CVSS } { invalid-option } { calcISC } {#1} {\msg_line_context:} }
}%
%    \end{macrocode}
%\end{macro}
%
% \subsubsection{Exploitability}
% The equation to compute the exploitability is the following:
%     \begin{equation}
%          8.22 \times \text{AttackVector} \times \text{AttackComplexity} \times \text{PrivilegesRequired} \times \text{UserInteraction}
%     \end{equation}
%
% This gives the following implementation:
%
%\begin{macro}{\__CVSS_calcExploitability}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_calcExploitability:nnnnn #1#2#3#4#5
{
     % #1 Attack Vector
     % #2 Attack Complexity
     % #3 Privileges Required
     % #4 User Interaction
     % #5 Scope
     % 8.22 × AttackVector × AttackComplexity × PrivilegeRequired × UserInteraction
     8.22 * (\__CVSS_parseAV:n {#1}) * (\__CVSS_parseAC:n {#2}) * (\__CVSS_parsePR:nn {#3}{#5}) * (\__CVSS_parseUI:n {#4})%
}
%    \end{macrocode}
%\end{macro}
%
% \subsubsection{CVSS Base Score}
% Now that all the pre-requisites are calculated, we can compute the CVSS base score as follows:
%     \begin{equation}
%     \text{Base Score} =
%     \begin{cases}
%          0 & \text{if Impact $\geq 0$ } \\
%          Roundup \Bigr( min \bigr[(\text{Impact} + \text{Exploitability}), 10\bigr]\Bigr) & \text{if Scope is Unchanged} \\
%          Roundup \Bigr(min \bigr[1.08 \times (\text{Impact} + \text{Exploitability}), 10\bigr]\Bigr) & \text{if Scope is changed}
%     \end{cases}
%\end{equation}
%
% This gives the following implementation:
%
%\begin{macro}{\__CVSS_cvssBaseScore}
%    \begin{macrocode}
\cs_new:Npn \__CVSS_cvssBaseScore:nnnnnnnn #1#2#3#4#5#6#7#8 {
     % #1 Attack Vector %Network N, Adjacent A, Local L, Physical P
     % #2 Attack Complexity %Low L, High H
     % #3 Privileges Required %None N, Low L, High H
     % #4 User Interaction %None N, Required R
     % #5 Scope %Unchanged U, Changed C
     % #6 Confidentiality Impact %High H, Low L, None N
     % #7 Integrity Impact %High H, Low L, None N
     % #8 Availability Impact %High H, Low L, None N
     %
     \fp_compare:nTF { \exp_args:Ne \__CVSS_calcImpact:nn {#5}{\exp_args:Ne \__CVSS_calcISS:nnn {#6}{#7}{#8}} <= 0 }
     % IF ISC <=0
     {
          % ISC <=0
          0.0
     }{
          % ISC > 0
          \str_case_e:nnF {#5}
          {
               { U } { % SCOPE UNCHANGED
                    \fp_eval:n { \__CVSS_roundup:n { min( ((\__CVSS_calcImpact:nn {#5}{\__CVSS_calcISS:nnn {#6}{#7}{#8}}) + (\__CVSS_calcExploitability:nnnnn {#1}{#2}{#3}{#4}{#5})), 10) } }%
               }
               { C } { % SCOPE CHANGED
                    \fp_eval:n { \__CVSS_roundup:n { min( (1.08 * ((\__CVSS_calcImpact:nn {#5}{\__CVSS_calcISS:nnn {#6}{#7}{#8}}) + (\__CVSS_calcExploitability:nnnnn {#1}{#2}{#3}{#4}{#5}))), 10) } }%
               }
          }
          { \msg_error:nnxxx { CVSS } { invalid-option } { parseScope } {#1} {\msg_line_context:} }
     }%
}
%    \end{macrocode}
%\end{macro}
%
% \subsubsection{CVSS Base Score}
% Now we can use a macro to check the validity of the CVSS string and \textbf{finally} call \texttt{\textbackslash{}__CVSS_cvssBaseScore} internally. 
% This is the most important macro of this whole package, and is expandable.
%
%\begin{macro}{\cvssScore}
%    \begin{macrocode}
\NewExpandableDocumentCommand \cvssScore { m }{%
     
     % Check that there are 35 chars
     \int_compare:nNnTF { \str_count_ignore_spaces:n {#1} } = {35}{}{
          \msg_error:nnxx{CVSS}{invalid-length}{#1}{\msg_line_context:}
     }
     % Check AV value
     \str_if_eq:eeTF {\str_range:nnn {#1} {1} {3}} {AV:} 
     {} {
          \msg_error:nnxxx{CVSS}{invalid-structure}{AV}{\str_range:nnn {#1} {1} {3}}{\msg_line_context:}
     }
     
     % Check AC value
     \str_if_eq:eeTF {\str_range:nnn {#1} {5} {8}} {/AC:} 
     {} {
          \msg_error:nnxxx{CVSS}{invalid-structure}{AC}{\str_range:nnn {#1} {5} {8}}{\msg_line_context:}
     }
     
     
     % Check PR value
     \str_if_eq:eeTF {\str_range:nnn {#1} {10} {13}} {/PR:} 
     {} {
          \msg_error:nnxxx{CVSS}{invalid-structure}{PR}{\str_range:nnn {#1} {10} {13}}{\msg_line_context:}
     }
     
     % Check UI value
     \str_if_eq:eeTF {\str_range:nnn {#1} {15} {18}} {/UI:} 
     {} {
          \msg_error:nnxxx{CVSS}{invalid-structure}{UI}{\str_range:nnn {#1} {15} {18}}{\msg_line_context:}
     }
     
     % Check S value
     \str_if_eq:eeTF {\str_range:nnn {#1} {20} {22}} {/S:} 
     {} {
          \msg_error:nnxxx{CVSS}{invalid-structure}{S}{\str_range:nnn {#1} {20} {22}}{\msg_line_context:}
     }
     
     % Check I value
     \str_if_eq:eeTF {\str_range:nnn {#1} {24} {26}} {/C:} 
     {} {
          \msg_error:nnxxx{CVSS}{invalid-structure}{C}{\str_range:nnn {#1} {24} {26}}{\msg_line_context:}
     }
     
     % Check I value
     \str_if_eq:eeTF {\str_range:nnn {#1} {28} {30}} {/I:} 
     {} {
          \msg_error:nnxxx{CVSS}{invalid-structure}{I}{\str_range:nnn {#1} {28} {30}}{\msg_line_context:}
     }
     
     % Check A value
     \str_if_eq:eeTF {\str_range:nnn {#1} {32} {34}} {/A:} 
     {} {
          \msg_error:nnxxx{CVSS}{invalid-structure}{A}{\str_range:nnn {#1} {32} {34}}{\msg_line_context:}
     }
     
     \exp_args:Ne \__CVSS_cvssBaseScore:nnnnnnnn
     { \str_use:N \str_item_ignore_spaces:nn { #1 }{ 4 } }
     { \str_use:N \str_item_ignore_spaces:nn { #1 }{ 9 } }
     { \str_use:N \str_item_ignore_spaces:nn { #1 }{ 14 } }
     { \str_use:N \str_item_ignore_spaces:nn { #1 }{ 19 } }
     { \str_use:N \str_item_ignore_spaces:nn { #1 }{ 23 } }
     { \str_use:N \str_item_ignore_spaces:nn { #1 }{ 27 } }
     { \str_use:N \str_item_ignore_spaces:nn { #1 }{ 31 } }
     { \str_use:N \str_item_ignore_spaces:nn { #1 }{ 35 } }
     
}%
\ExplSyntaxOff
%    \end{macrocode}
%\end{macro}
%
% \subsection{CVSS levels}
% Since we can compute the numerical score of a given CVSS string, we can now get the classification of a CVSS vector using the 
% FIRST terminology :
%\begin{table}[!ht]
%     \centering
%     \begin{tabular}{cc}
%          \hline
%          \textbf{Rating} & \textbf{CVSS Score} \\ \hline
%          None & $0.0$ \\ 
%          Low & $0.1 - 3.9$ \\
%          Medium & $4.0 - 6.9$ \\ 
%          High & $7.0 - 8.9$ \\
%          Critical & $9.0 - 10.0$ \\ \hline
%     \end{tabular}
%\end{table}
%
% Then we can build our switch case to assign a level to the numerical CVSS score
%\begin{macro}{\category}
% This macro will output a CVSS level based on the numerical CVSS score.
%    \begin{macrocode}
\ExplSyntaxOn
\NewExpandableDocumentCommand \category { m }{%
     \fp_compare:nNnTF {#1}<{\scoreLow}{None}
     {
          \fp_compare:nNnTF{#1}<{\scoreMed}{Low}
          {
               \fp_compare:nNnTF{#1}<{\scoreHigh}{Medium}
               {
                    \fp_compare:nNnTF{#1}<{\scoreCrit}{High}
                    {Critical}
               }%
          }%
     }%
}%
\ExplSyntaxOff
%    \end{macrocode}
% We can even have a colored version of the score.
%\begin{macro}{\cvssScorepretty}
% This macro will output the \textbf{colored} CVSS level based on the CVSS vector.
%    \begin{macrocode}
\newcommand{\cvssScorepretty}[1]{%
     \def\CVSScategory{\category{\cvssScore{#1}}}%
     \textcolor{color@cvss@\CVSScategory}{\cvssScore{#1}}%
}%
%    \end{macrocode}
%\end{macro}
%\end{macro}
% We have also built a macro that will output the CVSS level based on the CVSS string, that combines \texttt{\textbackslash{}cvssScore} and \texttt{\textbackslash{}category}:
%\begin{macro}{\cvssLevel}
% This macro will output a CVSS level based on the numerical CVSS score.
%    \begin{macrocode}
\newcommand{\cvssLevel}[1]{%
     \def\CVSSscore{\cvssScore{#1}}%
     \category{\CVSSscore}%
}%
%    \end{macrocode}
%\end{macro}
% And we can even have a colored version of this level.
%\begin{macro}{\cvssLevelpretty}
% This macro will output the \textbf{colored} CVSS level based on the numerical CVSS score.
%    \begin{macrocode}
\newcommand{\cvssLevelpretty}[1]{%
     \def\CVSScategory{\category{\cvssScore{#1}}}%
     \textcolor{color@cvss@\CVSScategory}{\CVSScategory}%
}%
%    \end{macrocode}
%\end{macro}
% \subsection{Fancy prints}
% \subsubsection{Framed CVSS Level}
% For nice display of the CVSS score we created also tags, that can be used to highlight the CVSS score.
%\begin{macro}{\cvssFrame}
% First, we define \texttt{cvssFrame}, a type of \texttt{tcolorbox} we are going to use:
%    \begin{macrocode}
\DeclareTotalTCBox{\cvssFrame}{m}{
     enhanced,nobeforeafter,
     tcbox raise base,
     boxrule=0.4pt,
     top=0mm,bottom=0mm,right=1mm,left=1mm,
     arc=1pt,
     boxsep=2pt,
     colframe=color@cvss@#1,
     colback=tcbcolframe,
     coltext=black,
}{#1}%

\MakeRobust\cvssFrame
%    \end{macrocode}
%\end{macro}
%
% Then we can call this box in conjunction with \texttt{cvssScore}.
%\begin{macro}{\cvssTag}
% This macro will output the \textbf{colored} CVSS level based on the numerical CVSS score.
%    \begin{macrocode}
\newcommand{\cvssTag}[1]{%
     \def\CVSSscore{\cvssScore{#1}}%
     \cvssFrame{\category{\CVSSscore}}%
}%
%    \end{macrocode}
%\end{macro}
%
% \subsubsection{Full CVSS display}
% We can even have a nice all-in display of the category, the scrore and a hyperlink to the FIRST calculator using a combination of all the functions we've defined:
%\begin{macro}{\cvssPrint}
% This macro will output the \textbf{colored} CVSS level based on the numerical CVSS score.
%    \begin{macrocode}     
\newcommand{\cvssPrint}[1]{%
     \def\CVSSscore{\cvssScore{#1}}
     \cvssFrame{\category{\CVSSscore}} \quad \CVSSscore \quad%
     \href{https://www.first.org/cvss/calculator/3.1\#CVSS:3.1/#1}{CVSS:3.1/#1}
}%
%    \end{macrocode}
%\end{macro}

% \end{implementation}
% \Finale
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%