| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.data.update.xml; |
| 19 | |
|
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.HashMap; |
| 22 | |
import java.util.List; |
| 23 | |
import java.util.Map; |
| 24 | |
import org.owasp.dependencycheck.dependency.VulnerableSoftware; |
| 25 | |
import org.xml.sax.Attributes; |
| 26 | |
import org.xml.sax.SAXException; |
| 27 | |
import org.xml.sax.SAXNotSupportedException; |
| 28 | |
import org.xml.sax.helpers.DefaultHandler; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 1 | public class NvdCve12Handler extends DefaultHandler { |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
private static final String CURRENT_SCHEMA_VERSION = "1.2"; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
private String vulnerability; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
private List<VulnerableSoftware> software; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
private String vendor; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private String product; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 1 | private boolean skip = false; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | 1 | private boolean hasPreviousVersion = false; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 1 | private final Element current = new Element(); |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
private Map<String, List<VulnerableSoftware>> vulnerabilities; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public Map<String, List<VulnerableSoftware>> getVulnerabilities() { |
| 82 | 1 | return vulnerabilities; |
| 83 | |
} |
| 84 | |
|
| 85 | |
@Override |
| 86 | |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
| 87 | 1222 | current.setNode(qName); |
| 88 | 1222 | if (current.isEntryNode()) { |
| 89 | 27 | vendor = null; |
| 90 | 27 | product = null; |
| 91 | 27 | hasPreviousVersion = false; |
| 92 | 27 | final String reject = attributes.getValue("reject"); |
| 93 | 27 | skip = "1".equals(reject); |
| 94 | 27 | if (!skip) { |
| 95 | 26 | vulnerability = attributes.getValue("name"); |
| 96 | 26 | software = new ArrayList<VulnerableSoftware>(); |
| 97 | |
} else { |
| 98 | 1 | vulnerability = null; |
| 99 | 1 | software = null; |
| 100 | |
} |
| 101 | 27 | } else if (!skip && current.isProdNode()) { |
| 102 | |
|
| 103 | 52 | vendor = attributes.getValue("vendor"); |
| 104 | 52 | product = attributes.getValue("name"); |
| 105 | 1143 | } else if (!skip && current.isVersNode()) { |
| 106 | 761 | final String prev = attributes.getValue("prev"); |
| 107 | 761 | if (prev != null && "1".equals(prev)) { |
| 108 | 1 | hasPreviousVersion = true; |
| 109 | 1 | final String edition = attributes.getValue("edition"); |
| 110 | 1 | final String num = attributes.getValue("num"); |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | 1 | String cpe = "cpe:/a:" + vendor + ":" + product; |
| 116 | 1 | if (num != null) { |
| 117 | 1 | cpe += ":" + num; |
| 118 | |
} |
| 119 | 1 | if (edition != null) { |
| 120 | 0 | cpe += ":" + edition; |
| 121 | |
} |
| 122 | 1 | final VulnerableSoftware vs = new VulnerableSoftware(); |
| 123 | 1 | vs.setCpe(cpe); |
| 124 | 1 | vs.setPreviousVersion(prev); |
| 125 | 1 | software.add(vs); |
| 126 | |
} |
| 127 | 761 | } else if (current.isNVDNode()) { |
| 128 | 1 | final String nvdVer = attributes.getValue("nvd_xml_version"); |
| 129 | 1 | if (!CURRENT_SCHEMA_VERSION.equals(nvdVer)) { |
| 130 | 0 | throw new SAXNotSupportedException("Schema version " + nvdVer + " is not supported"); |
| 131 | |
} |
| 132 | 1 | vulnerabilities = new HashMap<String, List<VulnerableSoftware>>(); |
| 133 | |
} |
| 134 | 1222 | } |
| 135 | |
|
| 136 | |
@Override |
| 137 | |
public void endElement(String uri, String localName, String qName) throws SAXException { |
| 138 | 1222 | current.setNode(qName); |
| 139 | 1222 | if (current.isEntryNode()) { |
| 140 | 27 | if (!skip && hasPreviousVersion) { |
| 141 | 1 | vulnerabilities.put(vulnerability, software); |
| 142 | |
} |
| 143 | 27 | vulnerability = null; |
| 144 | 27 | software = null; |
| 145 | |
} |
| 146 | 1222 | } |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | 1 | protected static class Element { |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public static final String NVD = "nvd"; |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
public static final String ENTRY = "entry"; |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public static final String VULN_SOFTWARE = "vuln_soft"; |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
public static final String PROD = "prod"; |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public static final String VERS = "vers"; |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
private String node; |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public String getNode() { |
| 185 | 0 | return this.node; |
| 186 | |
} |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
public void setNode(String node) { |
| 194 | 2444 | this.node = node; |
| 195 | 2444 | } |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
public boolean isNVDNode() { |
| 203 | 382 | return NVD.equals(node); |
| 204 | |
} |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public boolean isEntryNode() { |
| 212 | 2444 | return ENTRY.equals(node); |
| 213 | |
} |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
public boolean isVulnSoftwareNode() { |
| 221 | 0 | return VULN_SOFTWARE.equals(node); |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
public boolean isProdNode() { |
| 230 | 1192 | return PROD.equals(node); |
| 231 | |
} |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public boolean isVersNode() { |
| 239 | 1140 | return VERS.equals(node); |
| 240 | |
} |
| 241 | |
} |
| 242 | |
|
| 243 | |
} |