{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FIPS-140 example\n",
    "\n",
    "This notebook illustrates basic functionality with the `FIPSDataset` class that holds FIPS 140 dataset.\n",
    "\n",
    "Note that there exists a front end to this functionality at [sec-certs.org/fips](https://sec-certs.org/fips/). Before reinventing the wheel, it's good idea to check our web. Maybe you don't even need to run the code, but just use our web instead. \n",
    "\n",
    "For full API documentation of the `FIPSDataset` class go to the [dataset](../../api/dataset) docs.\n",
    "\n",
    "If you would like to examine the FIPS-140 \"Implementations Under Test\" or \"Modules In Process\" queues, check out the [FIPS IUT](fips_iut.ipynb) and [FIPS MIP](fips_mip.ipynb) example notebooks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sec_certs.dataset.fips import FIPSDataset\n",
    "from sec_certs.sample import FIPSCertificate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get fresh dataset snapshot from mirror"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dset: FIPSDataset = FIPSDataset.from_web()\n",
    "print(len(dset))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Do some basic dataset serialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dump dataset into json and load it back\n",
    "dset.to_json(\"./fips_dataset.json\")\n",
    "new_dset = FIPSDataset.from_json(\"./fips_dataset.json\")\n",
    "assert dset == new_dset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple dataset manipulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get certificates from a single manufacturer\n",
    "cisco_certs = [cert for cert in dset if \"Cisco\" in cert.manufacturer]\n",
    "\n",
    "# Get certificates with some CVE\n",
    "vulnerable_certs = [cert for cert in dset if cert.heuristics.related_cves]\n",
    "\n",
    "# Show CVE ids of some vulnerable certificate\n",
    "print(f\"{vulnerable_certs[0].heuristics.related_cves=}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dissect a single certificate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Select a certificate and print some attributes\n",
    "cert: FIPSCertificate = dset[\"542cacae1d41132a\"]\n",
    "\n",
    "print(f\"{cert.web_data.module_name=}\")\n",
    "print(f\"{cert.heuristics.cpe_matches=}\")\n",
    "print(f\"{cert.web_data.level=}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Serialize single certificate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "cert.to_json(\"./cert.json\")\n",
    "new_cert: FIPSCertificate = FIPSCertificate.from_json(\"./cert.json\")\n",
    "assert new_cert == cert"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create new dataset and fully process it\n",
    "\n",
    "```{warning}\n",
    "It's not good idea to run this from notebook. It may take several hours to finish. We recommend using `from_web()` or turning this into a Python script.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dset = FIPSDataset()\n",
    "dset.get_certs_from_web()\n",
    "dset.process_auxiliary_datasets()\n",
    "dset.download_all_artifacts()\n",
    "dset.convert_all_pdfs()\n",
    "dset.analyze_certificates()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Advanced usage\n",
    "There are more notebooks available showcasing more advanced usage of the tool.\n",
    "\n",
    "```{toctree}\n",
    ":caption: Other\n",
    ":hidden: True\n",
    ":maxdepth: 1\n",
    "Temporal trends <../fips/temporal_trends.ipynb>\n",
    "Vulnerabilities <../fips/vulnerabilities.ipynb>\n",
    "References <../fips/references.ipynb>\n",
    "IUT and MIP <../fips/in_process.ipynb>\n",
    "```\n",
    "\n",
    "  - Examine [temporal trends](../fips/temporal_trends.ipynb) in the FIPS-140 ecosystem.\n",
    "  - Analyze [vulnerabilities](../fips/vulnerabilities.ipynb) of FIPS-140 certified items.\n",
    "  - Study [references](../fips/references.ipynb) between FIPS-140 certificates.\n",
    "  - Analyze the FIPS-140 [IUT and MIP](../fips/in_process.ipynb) queues."
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "6386d1612879d92d026c363e7667e428bc38d86c5a080d58c3d70e7cd43df67d"
  },
  "kernelspec": {
   "display_name": "Python 3.8.1 ('certsvenv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
