Debian Codegraph @

v3/lints/etsi/lint_qcstatem_qcpds_valid.go

raw symbols SCIP
1 /*
2 * ZLint Copyright 2024 Regents of the University of Michigan
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
6 * of the License at http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15 package etsi
16
17 import (
18 "fmt"
19 "strings"
20
21 "github.com/zmap/zcrypto/encoding/asn1"
22 "github.com/zmap/zcrypto/x509"
23 "github.com/zmap/zlint/v3/lint"
24 "github.com/zmap/zlint/v3/util"
25 )
26
27 type qcStatemQcPdsValid struct{}
28
29 func init() {
30 lint.RegisterCertificateLint(&lint.CertificateLint{
31 LintMetadata: lint.LintMetadata{
32 Name: "e_qcstatem_qcpds_valid",
33 Description: "Checks that a QC Statement of the type id-etsi-qcs-QcPDS has the correct form",
34 Citation: "ETSI EN 319 412 - 5 V2.2.1 (2017 - 11) / Section 4.3.4",
35 Source: lint.EtsiEsi,
36 EffectiveDate: util.EtsiEn319_412_5_V2_2_1_Date,
37 },
38 Lint: NewQcStatemQcPdsValid,
39 })
40 }
41
42 func NewQcStatemQcPdsValid() lint.LintInterface {
43 return &qcStatemQcPdsValid{}
44 }
45
46 func (this *qcStatemQcPdsValid) getStatementOid() *asn1.ObjectIdentifier {
47 return &util.IdEtsiQcsQcEuPDS
48 }
49
50 func (l *qcStatemQcPdsValid) CheckApplies(c *x509.Certificate) bool {
51 if !util.IsExtInCert(c, util.QcStateOid) {
52 return false
53 }
54 if util.ParseQcStatem(util.GetExtFromCert(c, util.QcStateOid).Value, *l.getStatementOid()).IsPresent() {
55 return true
56 }
57 return false
58 }
59
60 func isInList(s string, list []string) bool {
61 for _, i := range list {
62 if strings.Compare(i, s) == 0 {
63 return true
64 }
65 }
66 return false
67 }
68
69 func (l *qcStatemQcPdsValid) Execute(c *x509.Certificate) *lint.LintResult {
70 errString := ""
71 ext := util.GetExtFromCert(c, util.QcStateOid)
72 s := util.ParseQcStatem(ext.Value, *l.getStatementOid())
73 errString += s.GetErrorInfo()
74 if len(errString) == 0 {
75 codeList := make([]string, 0)
76 foundEn := false
77 pds := s.(util.EtsiQcPds)
78 if len(pds.PdsLocations) == 0 {
79 util.AppendToStringSemicolonDelim(&errString, "PDS list is empty")
80 }
81 for i, loc := range pds.PdsLocations {
82 if len(loc.Language) != 2 {
83 util.AppendToStringSemicolonDelim(&errString, fmt.Sprintf("PDS location %d has a language code with an invalid length", i))
84 }
85 if strings.Compare(strings.ToLower(loc.Language), "en") == 0 {
86 foundEn = true
87 }
88 if isInList(strings.ToLower(loc.Language), codeList) {
89 util.AppendToStringSemicolonDelim(&errString, "country code '"+loc.Language+"' appears multiple times")
90 }
91 codeList = append(codeList, loc.Language)
92
93 }
94 if !foundEn {
95 util.AppendToStringSemicolonDelim(&errString, "no english PDS present")
96 }
97 }
98 if len(errString) == 0 {
99 return &lint.LintResult{Status: lint.Pass}
100 } else {
101 return &lint.LintResult{Status: lint.Error, Details: errString}
102 }
103 }