| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck.data.cpe; |
| 20 | |
|
| 21 | |
import java.io.IOException; |
| 22 | |
import java.io.UnsupportedEncodingException; |
| 23 | |
import java.net.URLEncoder; |
| 24 | |
import java.sql.SQLException; |
| 25 | |
import java.util.ArrayList; |
| 26 | |
import java.util.Collections; |
| 27 | |
import java.util.List; |
| 28 | |
import java.util.Set; |
| 29 | |
import java.util.StringTokenizer; |
| 30 | |
import java.util.logging.Level; |
| 31 | |
import java.util.logging.Logger; |
| 32 | |
import org.apache.lucene.document.Document; |
| 33 | |
import org.apache.lucene.index.CorruptIndexException; |
| 34 | |
import org.apache.lucene.queryparser.classic.ParseException; |
| 35 | |
import org.apache.lucene.search.ScoreDoc; |
| 36 | |
import org.apache.lucene.search.TopDocs; |
| 37 | |
import org.owasp.dependencycheck.Engine; |
| 38 | |
import org.owasp.dependencycheck.analyzer.AnalysisException; |
| 39 | |
import org.owasp.dependencycheck.analyzer.AnalysisPhase; |
| 40 | |
import org.owasp.dependencycheck.data.lucene.LuceneUtils; |
| 41 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 42 | |
import org.owasp.dependencycheck.dependency.Evidence; |
| 43 | |
import org.owasp.dependencycheck.dependency.Evidence.Confidence; |
| 44 | |
import org.owasp.dependencycheck.dependency.EvidenceCollection; |
| 45 | |
import org.owasp.dependencycheck.analyzer.Analyzer; |
| 46 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 47 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 48 | |
import org.owasp.dependencycheck.dependency.Identifier; |
| 49 | |
import org.owasp.dependencycheck.dependency.VulnerableSoftware; |
| 50 | |
import org.owasp.dependencycheck.utils.DependencyVersion; |
| 51 | |
import org.owasp.dependencycheck.utils.DependencyVersionUtil; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 30 | public class CPEAnalyzer implements Analyzer { |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
static final int MAX_QUERY_RESULTS = 25; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
static final String WEIGHTING_BOOST = "^5"; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
static final String CLEANSE_CHARACTER_RX = "[^A-Za-z0-9 ._-]"; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
static final String CLEANSE_NONALPHA_RX = "[^A-Za-z]*"; |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
static final int STRING_BUILDER_BUFFER = 20; |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private Index cpe; |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private CveDB cve; |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
public void open() throws IOException, DatabaseException { |
| 103 | 24 | cpe = new Index(); |
| 104 | 24 | cpe.open(); |
| 105 | 24 | cve = new CveDB(); |
| 106 | |
try { |
| 107 | 24 | cve.open(); |
| 108 | 0 | } catch (SQLException ex) { |
| 109 | 0 | Logger.getLogger(CPEAnalyzer.class.getName()).log(Level.FINE, null, ex); |
| 110 | 0 | throw new DatabaseException("Unable to open the cve db", ex); |
| 111 | 0 | } catch (ClassNotFoundException ex) { |
| 112 | 0 | Logger.getLogger(CPEAnalyzer.class.getName()).log(Level.FINE, null, ex); |
| 113 | 0 | throw new DatabaseException("Unable to open the cve db", ex); |
| 114 | 24 | } |
| 115 | 24 | } |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
@Override |
| 121 | |
public void close() { |
| 122 | 24 | cpe.close(); |
| 123 | 24 | cve.close(); |
| 124 | 24 | } |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public boolean isOpen() { |
| 132 | 22 | return (cpe != null) && cpe.isOpen(); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
@Override |
| 141 | |
protected void finalize() throws Throwable { |
| 142 | 13 | super.finalize(); |
| 143 | 13 | if (isOpen()) { |
| 144 | 0 | close(); |
| 145 | |
} |
| 146 | 13 | } |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
protected void determineCPE(Dependency dependency) throws CorruptIndexException, IOException, ParseException { |
| 159 | 27 | Confidence vendorConf = Confidence.HIGHEST; |
| 160 | 27 | Confidence productConf = Confidence.HIGHEST; |
| 161 | |
|
| 162 | 27 | String vendors = addEvidenceWithoutDuplicateTerms("", dependency.getVendorEvidence(), vendorConf); |
| 163 | 27 | String products = addEvidenceWithoutDuplicateTerms("", dependency.getProductEvidence(), productConf); |
| 164 | |
|
| 165 | |
|
| 166 | 27 | int ctr = 0; |
| 167 | |
do { |
| 168 | 108 | if (!vendors.isEmpty() && !products.isEmpty()) { |
| 169 | 108 | final List<IndexEntry> entries = searchCPE(vendors, products, dependency.getProductEvidence().getWeighting(), |
| 170 | |
dependency.getVendorEvidence().getWeighting()); |
| 171 | |
|
| 172 | 108 | for (IndexEntry e : entries) { |
| 173 | 468 | if (verifyEntry(e, dependency)) { |
| 174 | |
|
| 175 | 39 | final String vendor = e.getVendor(); |
| 176 | 39 | final String product = e.getProduct(); |
| 177 | |
|
| 178 | 39 | determineIdentifiers(dependency, vendor, product); |
| 179 | 468 | } |
| 180 | |
} |
| 181 | |
} |
| 182 | |
|
| 183 | 108 | vendorConf = reduceConfidence(vendorConf); |
| 184 | 108 | if (dependency.getVendorEvidence().contains(vendorConf)) { |
| 185 | |
|
| 186 | 102 | vendors = addEvidenceWithoutDuplicateTerms(vendors, dependency.getVendorEvidence(), vendorConf); |
| 187 | |
} |
| 188 | 108 | productConf = reduceConfidence(productConf); |
| 189 | 108 | if (dependency.getProductEvidence().contains(productConf)) { |
| 190 | |
|
| 191 | 108 | products = addEvidenceWithoutDuplicateTerms(products, dependency.getProductEvidence(), productConf); |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | 108 | } while ((++ctr) < 4); |
| 196 | 27 | } |
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
private String addEvidenceWithoutDuplicateTerms(final String text, final EvidenceCollection ec, Confidence confidenceFilter) { |
| 210 | 264 | final String txt = (text == null) ? "" : text; |
| 211 | 264 | final StringBuilder sb = new StringBuilder(txt.length() + (20 * ec.size())); |
| 212 | 264 | sb.append(' ').append(txt).append(' '); |
| 213 | 264 | for (Evidence e : ec.iterator(confidenceFilter)) { |
| 214 | 672 | String value = e.getValue(); |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | 672 | if (value.startsWith("http://")) { |
| 219 | 48 | value = value.substring(7).replaceAll("\\.", " "); |
| 220 | |
} |
| 221 | 672 | if (value.startsWith("https://")) { |
| 222 | 0 | value = value.substring(8).replaceAll("\\.", " "); |
| 223 | |
} |
| 224 | 672 | if (sb.indexOf(" " + value + " ") < 0) { |
| 225 | 429 | sb.append(value).append(' '); |
| 226 | |
} |
| 227 | 672 | } |
| 228 | 264 | return sb.toString().trim(); |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
private Confidence reduceConfidence(final Confidence c) { |
| 239 | 216 | if (c == Confidence.HIGHEST) { |
| 240 | 54 | return Confidence.HIGH; |
| 241 | 162 | } else if (c == Confidence.HIGH) { |
| 242 | 54 | return Confidence.MEDIUM; |
| 243 | |
} else { |
| 244 | 108 | return Confidence.LOW; |
| 245 | |
} |
| 246 | |
} |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
protected List<IndexEntry> searchCPE(String vendor, String product, |
| 267 | |
Set<String> vendorWeightings, Set<String> productWeightings) |
| 268 | |
throws CorruptIndexException, IOException, ParseException { |
| 269 | 111 | final ArrayList<IndexEntry> ret = new ArrayList<IndexEntry>(MAX_QUERY_RESULTS); |
| 270 | |
|
| 271 | 111 | final String searchString = buildSearch(vendor, product, vendorWeightings, productWeightings); |
| 272 | 111 | if (searchString == null) { |
| 273 | 0 | return ret; |
| 274 | |
} |
| 275 | |
|
| 276 | 111 | final TopDocs docs = cpe.search(searchString, MAX_QUERY_RESULTS); |
| 277 | 2820 | for (ScoreDoc d : docs.scoreDocs) { |
| 278 | 2709 | final Document doc = cpe.getDocument(d.doc); |
| 279 | 2709 | final IndexEntry entry = new IndexEntry(); |
| 280 | 2709 | entry.setVendor(doc.get(Fields.VENDOR)); |
| 281 | 2709 | entry.setProduct(doc.get(Fields.PRODUCT)); |
| 282 | 2709 | entry.setSearchScore(d.score); |
| 283 | 2709 | if (!ret.contains(entry)) { |
| 284 | 474 | ret.add(entry); |
| 285 | |
} |
| 286 | |
} |
| 287 | 111 | return ret; |
| 288 | |
} |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
protected String buildSearch(String vendor, String product, |
| 307 | |
Set<String> vendorWeighting, Set<String> productWeightings) { |
| 308 | 123 | final String v = vendor; |
| 309 | 123 | final String p = product; |
| 310 | 123 | final StringBuilder sb = new StringBuilder(v.length() + p.length() |
| 311 | |
+ Fields.PRODUCT.length() + Fields.VENDOR.length() + STRING_BUILDER_BUFFER); |
| 312 | |
|
| 313 | 123 | if (!appendWeightedSearch(sb, Fields.PRODUCT, p, productWeightings)) { |
| 314 | 0 | return null; |
| 315 | |
} |
| 316 | 123 | sb.append(" AND "); |
| 317 | 123 | if (!appendWeightedSearch(sb, Fields.VENDOR, v, vendorWeighting)) { |
| 318 | 0 | return null; |
| 319 | |
} |
| 320 | 123 | return sb.toString(); |
| 321 | |
} |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
private boolean appendWeightedSearch(StringBuilder sb, String field, String searchText, Set<String> weightedText) { |
| 338 | 246 | sb.append(" ").append(field).append(":( "); |
| 339 | |
|
| 340 | 246 | final String cleanText = cleanseText(searchText); |
| 341 | |
|
| 342 | 246 | if ("".equals(cleanText)) { |
| 343 | 0 | return false; |
| 344 | |
} |
| 345 | |
|
| 346 | 246 | if (weightedText == null || weightedText.isEmpty()) { |
| 347 | 36 | LuceneUtils.appendEscapedLuceneQuery(sb, cleanText); |
| 348 | |
} else { |
| 349 | 210 | final StringTokenizer tokens = new StringTokenizer(cleanText); |
| 350 | 2628 | while (tokens.hasMoreElements()) { |
| 351 | 2418 | final String word = tokens.nextToken(); |
| 352 | 2418 | String temp = null; |
| 353 | 2418 | for (String weighted : weightedText) { |
| 354 | 4968 | final String weightedStr = cleanseText(weighted); |
| 355 | 4968 | if (equalsIgnoreCaseAndNonAlpha(word, weightedStr)) { |
| 356 | 492 | temp = LuceneUtils.escapeLuceneQuery(word) + WEIGHTING_BOOST; |
| 357 | 492 | if (!word.equalsIgnoreCase(weightedStr)) { |
| 358 | 24 | temp += " " + LuceneUtils.escapeLuceneQuery(weightedStr) + WEIGHTING_BOOST; |
| 359 | |
} |
| 360 | |
} |
| 361 | 4968 | } |
| 362 | 2418 | if (temp == null) { |
| 363 | 1926 | temp = LuceneUtils.escapeLuceneQuery(word); |
| 364 | |
} |
| 365 | 2418 | sb.append(" ").append(temp); |
| 366 | 2418 | } |
| 367 | |
} |
| 368 | 246 | sb.append(" ) "); |
| 369 | 246 | return true; |
| 370 | |
} |
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
private String cleanseText(String text) { |
| 380 | 5214 | return text.replaceAll(CLEANSE_CHARACTER_RX, " "); |
| 381 | |
} |
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
private boolean equalsIgnoreCaseAndNonAlpha(String l, String r) { |
| 392 | 4968 | if (l == null || r == null) { |
| 393 | 0 | return false; |
| 394 | |
} |
| 395 | |
|
| 396 | 4968 | final String left = l.replaceAll(CLEANSE_NONALPHA_RX, ""); |
| 397 | 4968 | final String right = r.replaceAll(CLEANSE_NONALPHA_RX, ""); |
| 398 | 4968 | return left.equalsIgnoreCase(right); |
| 399 | |
} |
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
private boolean verifyEntry(final IndexEntry entry, final Dependency dependency) { |
| 411 | 468 | boolean isValid = false; |
| 412 | |
|
| 413 | 468 | if (collectionContainsString(dependency.getProductEvidence(), entry.getProduct()) |
| 414 | |
&& collectionContainsString(dependency.getVendorEvidence(), entry.getVendor())) { |
| 415 | |
|
| 416 | 39 | isValid = true; |
| 417 | |
} |
| 418 | 468 | return isValid; |
| 419 | |
} |
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
private boolean collectionContainsString(EvidenceCollection ec, String text) { |
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | 513 | final String[] words = text.split("[\\s_-]"); |
| 443 | 513 | final List<String> list = new ArrayList<String>(); |
| 444 | 513 | String tempWord = null; |
| 445 | 1410 | for (String word : words) { |
| 446 | |
|
| 447 | |
|
| 448 | 897 | if (tempWord != null) { |
| 449 | 48 | list.add(tempWord + word); |
| 450 | 48 | tempWord = null; |
| 451 | 849 | } else if (word.length() <= 2) { |
| 452 | 51 | tempWord = word; |
| 453 | |
} else { |
| 454 | 798 | list.add(word); |
| 455 | |
} |
| 456 | |
} |
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | 513 | boolean contains = true; |
| 461 | 513 | for (String word : list) { |
| 462 | 846 | contains &= ec.containsUsedString(word); |
| 463 | |
} |
| 464 | 513 | return contains; |
| 465 | |
} |
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
@Override |
| 477 | |
public void analyze(Dependency dependency, Engine engine) throws AnalysisException { |
| 478 | |
try { |
| 479 | 15 | determineCPE(dependency); |
| 480 | 0 | } catch (CorruptIndexException ex) { |
| 481 | 0 | throw new AnalysisException("CPE Index is corrupt.", ex); |
| 482 | 0 | } catch (IOException ex) { |
| 483 | 0 | throw new AnalysisException("Failure opening the CPE Index.", ex); |
| 484 | 0 | } catch (ParseException ex) { |
| 485 | 0 | throw new AnalysisException("Unable to parse the generated Lucene query for this dependency.", ex); |
| 486 | 15 | } |
| 487 | 15 | } |
| 488 | |
|
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
|
| 494 | |
@Override |
| 495 | |
public Set<String> getSupportedExtensions() { |
| 496 | 17289 | return null; |
| 497 | |
} |
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
@Override |
| 505 | |
public String getName() { |
| 506 | 0 | return "CPE Analyzer"; |
| 507 | |
} |
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | |
@Override |
| 516 | |
public boolean supportsExtension(String extension) { |
| 517 | 0 | return true; |
| 518 | |
} |
| 519 | |
|
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
|
| 524 | |
|
| 525 | |
@Override |
| 526 | |
public AnalysisPhase getAnalysisPhase() { |
| 527 | 3 | return AnalysisPhase.IDENTIFIER_ANALYSIS; |
| 528 | |
} |
| 529 | |
|
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
@Override |
| 536 | |
public void initialize() throws Exception { |
| 537 | 0 | this.open(); |
| 538 | 0 | } |
| 539 | |
|
| 540 | |
|
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
|
| 548 | |
|
| 549 | |
|
| 550 | |
|
| 551 | |
|
| 552 | |
private void determineIdentifiers(Dependency dependency, String vendor, String product) throws UnsupportedEncodingException { |
| 553 | 39 | final Set<VulnerableSoftware> cpes = cve.getCPEs(vendor, product); |
| 554 | 39 | DependencyVersion bestGuess = new DependencyVersion("-"); |
| 555 | 39 | Confidence bestGuessConf = null; |
| 556 | 39 | final List<IdentifierMatch> collected = new ArrayList<IdentifierMatch>(); |
| 557 | 195 | for (Confidence conf : Confidence.values()) { |
| 558 | 156 | for (Evidence evidence : dependency.getVersionEvidence().iterator(conf)) { |
| 559 | 126 | final DependencyVersion evVer = DependencyVersionUtil.parseVersion(evidence.getValue()); |
| 560 | 126 | if (evVer == null) { |
| 561 | 0 | continue; |
| 562 | |
} |
| 563 | 126 | for (VulnerableSoftware vs : cpes) { |
| 564 | |
DependencyVersion dbVer; |
| 565 | 2757 | if (vs.getRevision() != null && !vs.getRevision().isEmpty()) { |
| 566 | 495 | dbVer = DependencyVersionUtil.parseVersion(vs.getVersion() + "." + vs.getRevision()); |
| 567 | |
} else { |
| 568 | 2262 | dbVer = DependencyVersionUtil.parseVersion(vs.getVersion()); |
| 569 | |
} |
| 570 | 2757 | if (dbVer == null |
| 571 | |
|| evVer.equals(dbVer)) { |
| 572 | 87 | final String url = String.format("http://web.nvd.nist.gov/view/vuln/search?cpe=%s", URLEncoder.encode(vs.getName(), "UTF-8")); |
| 573 | 87 | final IdentifierMatch match = new IdentifierMatch("cpe", vs.getName(), url, IdentifierConfidence.EXACT_MATCH, conf); |
| 574 | 87 | collected.add(match); |
| 575 | 87 | } else { |
| 576 | |
|
| 577 | 2670 | if (evVer.getVersionParts().size() <= dbVer.getVersionParts().size() |
| 578 | |
&& evVer.matchesAtLeastThreeLevels(dbVer)) { |
| 579 | 195 | if (bestGuessConf == null || bestGuessConf.compareTo(conf) > 0) { |
| 580 | 15 | if (bestGuess.getVersionParts().size() < dbVer.getVersionParts().size()) { |
| 581 | 15 | bestGuess = dbVer; |
| 582 | 15 | bestGuessConf = conf; |
| 583 | |
} |
| 584 | |
} |
| 585 | |
} |
| 586 | |
} |
| 587 | 2757 | } |
| 588 | 126 | if (bestGuessConf == null || bestGuessConf.compareTo(conf) > 0) { |
| 589 | 24 | if (bestGuess.getVersionParts().size() < evVer.getVersionParts().size()) { |
| 590 | 24 | bestGuess = evVer; |
| 591 | 24 | bestGuessConf = conf; |
| 592 | |
} |
| 593 | |
} |
| 594 | 126 | } |
| 595 | |
} |
| 596 | 39 | final String cpeName = String.format("cpe:/a:%s:%s:%s", vendor, product, bestGuess.toString()); |
| 597 | 39 | final String url = null; |
| 598 | 39 | if (bestGuessConf == null) { |
| 599 | 0 | bestGuessConf = Confidence.LOW; |
| 600 | |
} |
| 601 | 39 | final IdentifierMatch match = new IdentifierMatch("cpe", cpeName, url, IdentifierConfidence.BEST_GUESS, bestGuessConf); |
| 602 | 39 | collected.add(match); |
| 603 | |
|
| 604 | 39 | Collections.sort(collected); |
| 605 | 39 | final IdentifierConfidence bestIdentifierQuality = collected.get(0).getConfidence(); |
| 606 | 39 | final Confidence bestEvidenceQuality = collected.get(0).getEvidenceConfidence(); |
| 607 | 39 | for (IdentifierMatch m : collected) { |
| 608 | 126 | if (bestIdentifierQuality.equals(m.getConfidence()) |
| 609 | |
&& bestEvidenceQuality.equals(m.getEvidenceConfidence())) { |
| 610 | 48 | dependency.addIdentifier(m.getIdentifier()); |
| 611 | |
} |
| 612 | |
} |
| 613 | 39 | } |
| 614 | |
|
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | 9 | private enum IdentifierConfidence { |
| 619 | |
|
| 620 | |
|
| 621 | |
|
| 622 | |
|
| 623 | 3 | EXACT_MATCH, |
| 624 | |
|
| 625 | |
|
| 626 | |
|
| 627 | 3 | BEST_GUESS |
| 628 | |
} |
| 629 | |
|
| 630 | |
|
| 631 | |
|
| 632 | |
|
| 633 | |
|
| 634 | 117 | private static class IdentifierMatch implements Comparable<IdentifierMatch> { |
| 635 | |
|
| 636 | |
|
| 637 | |
|
| 638 | |
|
| 639 | |
|
| 640 | |
|
| 641 | |
|
| 642 | |
|
| 643 | |
|
| 644 | |
|
| 645 | |
|
| 646 | |
|
| 647 | 126 | IdentifierMatch(String type, String value, String url, IdentifierConfidence identifierConfidence, Confidence evidenceConfidence) { |
| 648 | 126 | this.identifier = new Identifier(type, value, url); |
| 649 | 126 | this.confidence = identifierConfidence; |
| 650 | 126 | this.evidenceConfidence = evidenceConfidence; |
| 651 | 126 | } |
| 652 | |
|
| 653 | |
|
| 654 | |
|
| 655 | |
|
| 656 | |
private Confidence evidenceConfidence; |
| 657 | |
|
| 658 | |
|
| 659 | |
|
| 660 | |
|
| 661 | |
|
| 662 | |
|
| 663 | |
public Confidence getEvidenceConfidence() { |
| 664 | 135 | return evidenceConfidence; |
| 665 | |
} |
| 666 | |
|
| 667 | |
|
| 668 | |
|
| 669 | |
|
| 670 | |
|
| 671 | |
|
| 672 | |
public void setEvidenceConfidence(Confidence evidenceConfidence) { |
| 673 | 0 | this.evidenceConfidence = evidenceConfidence; |
| 674 | 0 | } |
| 675 | |
|
| 676 | |
|
| 677 | |
|
| 678 | |
private IdentifierConfidence confidence; |
| 679 | |
|
| 680 | |
|
| 681 | |
|
| 682 | |
|
| 683 | |
|
| 684 | |
|
| 685 | |
public IdentifierConfidence getConfidence() { |
| 686 | 165 | return confidence; |
| 687 | |
} |
| 688 | |
|
| 689 | |
|
| 690 | |
|
| 691 | |
|
| 692 | |
|
| 693 | |
|
| 694 | |
public void setConfidence(IdentifierConfidence confidence) { |
| 695 | 0 | this.confidence = confidence; |
| 696 | 0 | } |
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | |
private Identifier identifier; |
| 701 | |
|
| 702 | |
|
| 703 | |
|
| 704 | |
|
| 705 | |
|
| 706 | |
|
| 707 | |
public Identifier getIdentifier() { |
| 708 | 48 | return identifier; |
| 709 | |
} |
| 710 | |
|
| 711 | |
|
| 712 | |
|
| 713 | |
|
| 714 | |
|
| 715 | |
|
| 716 | |
public void setIdentifier(Identifier identifier) { |
| 717 | 0 | this.identifier = identifier; |
| 718 | 0 | } |
| 719 | |
|
| 720 | |
|
| 721 | |
|
| 722 | |
|
| 723 | |
|
| 724 | |
|
| 725 | |
|
| 726 | |
|
| 727 | |
@Override |
| 728 | |
public String toString() { |
| 729 | 0 | return "IdentifierMatch{" + "evidenceConfidence=" + evidenceConfidence |
| 730 | |
+ ", confidence=" + confidence + ", identifier=" + identifier + '}'; |
| 731 | |
} |
| 732 | |
|
| 733 | |
|
| 734 | |
|
| 735 | |
|
| 736 | |
|
| 737 | |
|
| 738 | |
@Override |
| 739 | |
public int hashCode() { |
| 740 | 0 | int hash = 5; |
| 741 | 0 | hash = 97 * hash + (this.evidenceConfidence != null ? this.evidenceConfidence.hashCode() : 0); |
| 742 | 0 | hash = 97 * hash + (this.confidence != null ? this.confidence.hashCode() : 0); |
| 743 | 0 | hash = 97 * hash + (this.identifier != null ? this.identifier.hashCode() : 0); |
| 744 | 0 | return hash; |
| 745 | |
} |
| 746 | |
|
| 747 | |
|
| 748 | |
|
| 749 | |
|
| 750 | |
|
| 751 | |
|
| 752 | |
|
| 753 | |
@Override |
| 754 | |
public boolean equals(Object obj) { |
| 755 | 0 | if (obj == null) { |
| 756 | 0 | return false; |
| 757 | |
} |
| 758 | 0 | if (getClass() != obj.getClass()) { |
| 759 | 0 | return false; |
| 760 | |
} |
| 761 | 0 | final IdentifierMatch other = (IdentifierMatch) obj; |
| 762 | 0 | if (this.evidenceConfidence != other.evidenceConfidence) { |
| 763 | 0 | return false; |
| 764 | |
} |
| 765 | 0 | if (this.confidence != other.confidence) { |
| 766 | 0 | return false; |
| 767 | |
} |
| 768 | 0 | if (this.identifier != other.identifier && (this.identifier == null || !this.identifier.equals(other.identifier))) { |
| 769 | 0 | return false; |
| 770 | |
} |
| 771 | 0 | return true; |
| 772 | |
} |
| 773 | |
|
| 774 | |
|
| 775 | |
|
| 776 | |
|
| 777 | |
|
| 778 | |
|
| 779 | |
|
| 780 | |
|
| 781 | |
|
| 782 | |
@Override |
| 783 | |
public int compareTo(IdentifierMatch o) { |
| 784 | 117 | int conf = this.confidence.compareTo(o.confidence); |
| 785 | 117 | if (conf == 0) { |
| 786 | 87 | conf = this.evidenceConfidence.compareTo(o.evidenceConfidence); |
| 787 | 87 | if (conf == 0) { |
| 788 | 33 | conf = identifier.compareTo(o.identifier); |
| 789 | |
} |
| 790 | |
} |
| 791 | 117 | return conf; |
| 792 | |
} |
| 793 | |
} |
| 794 | |
} |