View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2019 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.dependency.naming;
19  
20  import org.apache.commons.lang3.builder.CompareToBuilder;
21  import org.apache.commons.lang3.builder.EqualsBuilder;
22  import org.apache.commons.lang3.builder.HashCodeBuilder;
23  import org.jetbrains.annotations.NotNull;
24  import org.owasp.dependencycheck.dependency.Confidence;
25  import us.springett.parsers.cpe.Cpe;
26  import us.springett.parsers.cpe.CpeBuilder;
27  import us.springett.parsers.cpe.exceptions.CpeValidationException;
28  import us.springett.parsers.cpe.values.Part;
29  
30  /**
31   * A CPE Identifier for a dependency object.
32   *
33   * @author Jeremy Long
34   */
35  public class CpeIdentifier implements Identifier {
36  
37      /**
38       * The serial version UID for serialization.
39       */
40      private static final long serialVersionUID = 2901855131887281680L;
41  
42      /**
43       * The CPE identifier.
44       */
45      private final Cpe cpe;
46      /**
47       * The confidence that this is the correct identifier.
48       */
49      private Confidence confidence;
50      /**
51       * The URL for the identifier.
52       */
53      private String url;
54      /**
55       * Notes about the vulnerability. Generally used for suppression
56       * information.
57       */
58      private String notes;
59  
60      /**
61       * Constructs a new CPE Identifier from a CPE object with the given
62       * confidence.
63       *
64       * @param cpe the CPE value
65       * @param confidence the confidence in the identifiers match
66       */
67      public CpeIdentifier(Cpe cpe, Confidence confidence) {
68          this.cpe = cpe;
69          this.confidence = confidence;
70          this.url = null;
71      }
72  
73      /**
74       * Constructs a new CPE Identifier from a CPE object with the given
75       * confidence.
76       *
77       * @param cpe the CPE value
78       * @param url the URL for the identifier
79       * @param confidence the confidence in the identifiers match
80       */
81      public CpeIdentifier(Cpe cpe, String url, Confidence confidence) {
82          this.cpe = cpe;
83          this.confidence = confidence;
84          this.url = url;
85      }
86  
87      /**
88       * Constructs a new CPE Identifier from a CPE object with the given
89       * confidence.
90       *
91       * @param vendor the vendor
92       * @param product the product name
93       * @param version the version
94       * @param confidence the confidence in the identifiers match
95       * @throws CpeValidationException thrown if there is an error converting the
96       * vendor, product, and version into a CPE object
97       */
98      public CpeIdentifier(String vendor, String product, String version, Confidence confidence) throws CpeValidationException {
99          final CpeBuilder builder = new CpeBuilder();
100         this.cpe = builder.part(Part.APPLICATION).vendor(vendor).product(product).version(version).build();
101         this.confidence = confidence;
102     }
103 
104     /**
105      * Returns the CPE object.
106      *
107      * @return the CPE object
108      */
109     public Cpe getCpe() {
110         return cpe;
111     }
112 
113     @Override
114     public Confidence getConfidence() {
115         return confidence;
116     }
117 
118     @Override
119     public String getNotes() {
120         return notes;
121     }
122 
123     @Override
124     public String getUrl() {
125         return url;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public void setConfidence(Confidence confidence) {
133         this.confidence = confidence;
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public void setUrl(String url) {
141         this.url = url;
142     }
143 
144     @Override
145     public void setNotes(String notes) {
146         this.notes = notes;
147     }
148 
149     @Override
150     public String getValue() {
151         return cpe.toCpe23FS();
152     }
153 
154     /**
155      * Returns the CPE 2.3 formatted string.
156      *
157      * @return the CPE 2.3 formatted string
158      */
159     @Override
160     public String toString() {
161         return cpe.toCpe23FS();
162     }
163 
164     @Override
165     public int hashCode() {
166         return new HashCodeBuilder(95, 183)
167                 .append(this.cpe)
168                 .append(this.confidence)
169                 .append(this.url)
170                 .append(this.notes)
171                 .toHashCode();
172     }
173 
174     @Override
175     public boolean equals(Object obj) {
176         if (obj == null || !(obj instanceof CpeIdentifier)) {
177             return false;
178         }
179         if (this == obj) {
180             return true;
181         }
182         final CpeIdentifier other = (CpeIdentifier) obj;
183         return new EqualsBuilder().append(cpe, other.cpe)
184                 .append(this.confidence, other.confidence)
185                 .append(this.url, other.url)
186                 .append(this.notes, other.notes).isEquals();
187     }
188 
189     @Override
190     public int compareTo(@NotNull Identifier o) {
191         if (o instanceof CpeIdentifier) {
192             final CpeIdentifier other = (CpeIdentifier) o;
193             return new CompareToBuilder()
194                     .append(this.cpe, other.cpe)
195                     .append(this.url, other.getUrl())
196                     .append(this.confidence, other.getConfidence())
197                     .toComparison();
198 
199         }
200         return new CompareToBuilder()
201                 .append(this.toString(), o.toString())
202                 .append(this.url, o.getUrl())
203                 .append(this.confidence, o.getConfidence())
204                 .toComparison();
205     }
206 }