

CREATE TABLE identities (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL,
  data BLOB NOT NULL,
  UNIQUE (type, data)
);


CREATE TABLE child_configs (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  lifetime INTEGER NOT NULL DEFAULT '1500',
  rekeytime INTEGER NOT NULL DEFAULT '1200',
  jitter INTEGER NOT NULL DEFAULT '60',
  updown TEXT DEFAULT NULL,
  hostaccess INTEGER NOT NULL DEFAULT '0',
  mode INTEGER NOT NULL DEFAULT '2',
  start_action INTEGER NOT NULL DEFAULT '0',
  dpd_action INTEGER NOT NULL DEFAULT '0',
  close_action INTEGER NOT NULL DEFAULT '0',
  ipcomp INTEGER NOT NULL DEFAULT '0',
  reqid INTEGER NOT NULL DEFAULT '0'
);
DROP INDEX IF EXISTS child_configs_name;
CREATE INDEX child_configs_name ON child_configs (
  name
);


CREATE TABLE child_config_traffic_selector (
  child_cfg INTEGER NOT NULL,
  traffic_selector INTEGER NOT NULL,
  kind INTEGER NOT NULL
);
DROP INDEX IF EXISTS child_config_traffic_selector_all;
CREATE INDEX child_config_traffic_selector_all ON child_config_traffic_selector (
  child_cfg, traffic_selector
);

CREATE TABLE proposals (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  proposal TEXT NOT NULL
);


CREATE TABLE child_config_proposal (
  child_cfg INTEGER NOT NULL,
  prio INTEGER NOT NULL,
  prop INTEGER NOT NULL
);


CREATE TABLE ike_configs (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  certreq INTEGER NOT NULL DEFAULT '1',
  force_encap INTEGER NOT NULL DEFAULT '0',
  local TEXT NOT NULL,
  remote TEXT NOT NULL
);


CREATE TABLE ike_config_proposal (
  ike_cfg INTEGER NOT NULL,
  prio INTEGER NOT NULL,
  prop INTEGER NOT NULL
);


CREATE TABLE peer_configs (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  ike_version INTEGER NOT NULL DEFAULT '2',
  ike_cfg INTEGER NOT NULL,
  local_id TEXT NOT NULL,
  remote_id TEXT NOT NULL,
  cert_policy INTEGER NOT NULL DEFAULT '1',
  uniqueid INTEGER NOT NULL DEFAULT '0',
  auth_method INTEGER NOT NULL DEFAULT '1',
  eap_type INTEGER NOT NULL DEFAULT '0',
  eap_vendor INTEGER NOT NULL DEFAULT '0',
  keyingtries INTEGER NOT NULL DEFAULT '3',
  rekeytime INTEGER NOT NULL DEFAULT '7200',
  reauthtime INTEGER NOT NULL DEFAULT '0',
  jitter INTEGER NOT NULL DEFAULT '180',
  overtime INTEGER NOT NULL DEFAULT '300',
  mobike INTEGER NOT NULL DEFAULT '1',
  dpd_delay INTEGER NOT NULL DEFAULT '120',
  virtual TEXT DEFAULT NULL,
  pool TEXT DEFAULT NULL,
  mediation INTEGER NOT NULL DEFAULT '0',
  mediated_by INTEGER NOT NULL DEFAULT '0',
  peer_id INTEGER NOT NULL DEFAULT '0'
);
DROP INDEX IF EXISTS peer_configs_name;
CREATE INDEX peer_configs_name ON peer_configs (
  name
);


CREATE TABLE peer_config_child_config (
  peer_cfg INTEGER NOT NULL,
  child_cfg INTEGER NOT NULL,
  PRIMARY KEY (peer_cfg, child_cfg)
);


CREATE TABLE traffic_selectors (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL DEFAULT '7',
  protocol INTEGER NOT NULL DEFAULT '0',
  start_addr BLOB DEFAULT NULL,
  end_addr BLOB DEFAULT NULL,
  start_port INTEGER NOT NULL DEFAULT '0',
  end_port INTEGER NOT NULL DEFAULT '65535'
);


CREATE TABLE certificates (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL,
  keytype INTEGER NOT NULL,
  data BLOB NOT NULL
);


CREATE TABLE certificate_identity (
  certificate INTEGER NOT NULL,
  identity INTEGER NOT NULL,
  PRIMARY KEY (certificate, identity)
);


CREATE TABLE private_keys (
  id INTEGER NOT NULL  PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL,
  data BLOB NOT NULL
);


CREATE TABLE private_key_identity (
  private_key INTEGER NOT NULL,
  identity INTEGER NOT NULL,
  PRIMARY KEY (private_key, identity)
);


CREATE TABLE shared_secrets (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL,
  data BLOB NOT NULL
);


CREATE TABLE shared_secret_identity (
  shared_secret INTEGER NOT NULL,
  identity INTEGER NOT NULL,
  PRIMARY KEY (shared_secret, identity)
);


CREATE TABLE certificate_authorities (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  certificate INTEGER NOT NULL
);


CREATE TABLE certificate_distribution_points (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  ca INTEGER NOT NULL,
  type INTEGER NOT NULL,
  uri TEXT NOT NULL
);


CREATE TABLE pools (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL UNIQUE,
  start BLOB NOT NULL,
  end BLOB NOT NULL,
  timeout INTEGER NOT NULL
);

CREATE TABLE addresses (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  pool INTEGER NOT NULL,
  address BLOB NOT NULL,
  identity INTEGER NOT NULL DEFAULT 0,
  acquired INTEGER NOT NULL DEFAULT 0,
  released INTEGER NOT NULL DEFAULT 1
);
DROP INDEX IF EXISTS addresses_pool;
CREATE INDEX addresses_pool ON addresses (
  pool
);
DROP INDEX IF EXISTS addresses_address;
CREATE INDEX addresses_address ON addresses (
  address
);
DROP INDEX IF EXISTS addresses_identity;
CREATE INDEX addresses_identity ON addresses (
  identity
);

CREATE TABLE leases (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  address INTEGER NOT NULL,
  identity INTEGER NOT NULL,
  acquired INTEGER NOT NULL,
  released INTEGER NOT NULL
);

CREATE TABLE attribute_pools (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL
);

CREATE TABLE attributes (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  identity INTEGER NOT NULL DEFAULT 0,
  pool INTEGER NOT NULL DEFAULT 0,
  type INTEGER NOT NULL,
  value BLOB NOT NULL
);
DROP INDEX IF EXISTS attributes_identity;
CREATE INDEX attributes_identity ON attributes (
  identity
);
DROP INDEX IF EXISTS attributes_pool;
CREATE INDEX attributes_pool ON attributes (
  pool
);

CREATE TABLE ike_sas (
  local_spi BLOB NOT NULL PRIMARY KEY,
  remote_spi BLOB NOT NULL,
  id INTEGER NOT NULL,
  initiator INTEGER NOT NULL,
  local_id_type INTEGER NOT NULL,
  local_id_data BLOB DEFAULT NULL,
  remote_id_type INTEGER NOT NULL,
  remote_id_data BLOB DEFAULT NULL,
  host_family INTEGER NOT NULL,
  local_host_data BLOB NOT NULL,
  remote_host_data BLOB NOT NULL,
  created INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE logs (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  local_spi BLOB NOT NULL,
  signal INTEGER NOT NULL,
  level INTEGER NOT NULL,
  msg TEXT NOT NULL,
  time INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP
);

/* Identities */

INSERT INTO identities (
  type, data
) VALUES ( /* dave@strongswan.org */
  3, X'64617665407374726f6e677377616e2e6f7267'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* moon.strongswan.org */
  2, X'6d6f6f6e2e7374726f6e677377616e2e6f7267'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* subjkey of dave@strongswan.org */
  11, X'516f2a5affb28c278a5b3044f84fab6b42e4e8d1'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* subjkey of moon.strongswan.org */
  11, X'4b712ee9ad4bb4d4ce6d5c52eff8074be41ec9e9'
 );

/* Certificates */

INSERT INTO certificates (
   type, keytype, data
) VALUES ( /* dave@strongswan.org */
  6, 1, X'308201a2300d06092a864886f70d01010105000382018f003082018a0282018100ac0930765adb727a1d7f092a8bc02fa88754cd355279c5cf5b1129bf22b1607736a42ad468dec35753669f2672f97b645f00ecd8909675a0d85ace8914073d7f76acf2f8655b212bf05b84dae81218f34c87843a63ff358797eb94c59e291f5daa780d6fe2016bbfcb9a47e1aec2f67f1f45a6fb89478ef3e59f735e0eb42d6700346e57c59baa4f4f8b868552eed0f6a2c29c36d70284ffccdc20db75f66516b0e52666b6a36b673474c252d32c5350ac45a2168ea1d03f4f37801dfc56e9489508d4da92323c3036a80d91ba8b3edeb88ffe06989a8a3906fc1161c14bb306ade5ba3abe61a79e9f6ed5d03995dc2792e5769dcb118b606013ca6a13dff1221f36bddee4ae08191d4f3c5063c970570d0d7018ac158ce9a6f211855feb557f7b8df61287e69deccd92d3e986ab3506beb1111215a5990d60c2f5c2cfcc2fac0a9d30120e306309111f11929f4cc1b5cf480c99598440ebf41a287857d44c01596cded752d38f8465601339793740836530c3530814e61d749a84a9e60dca1d0203010001'
);

INSERT INTO certificates (
   type, keytype, data
) VALUES ( /* moon.strongswan.org */
  6, 1, X'308201a2300d06092a864886f70d01010105000382018f003082018a0282018100bd640cea0aaecb276ae66468a51855fc927272ebcfeb7b2de00736a4bff5af585be8db7e971e7dee4b2d758fdaec13fac84bba9722bf0057856270d095c9c655519f0ec7cf2ac442e4559fa996827a6fb0a51f289597191e23b23fb066c8930f855bead4ebd64906295ec16063cf0ba9da8664f1a8ab48070993ee87cfbdb5d0aab600ebf64b12311c8a72415c9c19aad141758615559b562c8a328b52fb86b4f0dbfe31426341fd717aaf2b9fe134f9f471aec0afff8bf2870b2691d26714640236d795ec8dc1605ae6f3a430123bf333bfd0f3d1ffe5ea98e4a9495dd50203cb7c6c8a0a82b2f7fa7bbce9b4f538650ae09ff2967675994a1d07ee07ea0651bd2a87ea8ee5a708ba775bace3cd166aab090376a3fa13d86edf913fc9b604c7debb4f2488ff1e1ae8f11a3654add2b2f5613c6e252242206c68624311ac6c6d0d42ede0744f9a13a627f72f966f0de46e52ec2d64d094ab743cb6103cef17da64986e8dcbd219b8306b031317d816c338cbe22776bcb1f0c6b8d7c9247f9ab10203010001'
);

INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  1, 1
);
$INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  1, 3
);

INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  2, 2
);

INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  2, 4
);

/* Private Keys */

INSERT INTO private_keys (
   type, data
) VALUES ( /* key of dave@strongswan.org */
  1, X'308206e30201000282018100ac0930765adb727a1d7f092a8bc02fa88754cd355279c5cf5b1129bf22b1607736a42ad468dec35753669f2672f97b645f00ecd8909675a0d85ace8914073d7f76acf2f8655b212bf05b84dae81218f34c87843a63ff358797eb94c59e291f5daa780d6fe2016bbfcb9a47e1aec2f67f1f45a6fb89478ef3e59f735e0eb42d6700346e57c59baa4f4f8b868552eed0f6a2c29c36d70284ffccdc20db75f66516b0e52666b6a36b673474c252d32c5350ac45a2168ea1d03f4f37801dfc56e9489508d4da92323c3036a80d91ba8b3edeb88ffe06989a8a3906fc1161c14bb306ade5ba3abe61a79e9f6ed5d03995dc2792e5769dcb118b606013ca6a13dff1221f36bddee4ae08191d4f3c5063c970570d0d7018ac158ce9a6f211855feb557f7b8df61287e69deccd92d3e986ab3506beb1111215a5990d60c2f5c2cfcc2fac0a9d30120e306309111f11929f4cc1b5cf480c99598440ebf41a287857d44c01596cded752d38f8465601339793740836530c3530814e61d749a84a9e60dca1d0203010001028201800ef248c56cf50b8d44251bd3984b0263a6c7a82bd2f6651db3f5ef45bdb561c1ff10cf4474b759e055e220bf4896429c00a1b0c775f4647ed7728d9172432e1acb6190f0776f2ded712ca503c1cd84b66aa136cc933b2d2d927633643744050770693812f989e71070422a0442588a42ec217335aa57cf0f2dead1ad4bce129dbec7a4b337d0b8eb6567f95acef99455168f48342984fcaf7409c749af30d0d7e256de4f1132172c386dfbce1203beaf4fc59da4a3bab79585d936bbcedc7182ff8131de2db310258725baacedcfdfe63d3d3e775acb3b098619367f68a30dafeedb2f01d4314c553f878cba95b7def25851dc498679fe4791a8ebd369c334a80fcb9672303baa37f9fd6051b4384ce0dd1d5cf9cf18643118950ddd56d6bd03c854ca26e2aa4420bf95254606934f32923f38cbcfdd386440c4f25c1b065a7bbdaed914165bd5d3a04e77bb3b3a2ed70fdcfc47d01a2f49df84d2f3092bc3f75bf3a75f22091d1af6d0318033a8dce71ded27d954ee6c65b647793ca96ab30f0281c100d987c050255808468cac2ff43f9f18ca562c1e76b5d384499bfab1c647bc16cfa49a50e8a2e698e5d5acb45fec8efe507a4afd4d30faae74d3787bcb29612f6961d1039411064748862f0e8133c1fbb1764fa6ba43cb1348cdc0ce1eb7d8d131f21d76cd334fb3dc0f8feb93aef2fb66e6c34a9cb4e6c42934d89b208f0d7f1ce2031c13bb8746ac79e8fd6b0905e07d31db0ed9157cb3c4488427ee72587e842c050f79f97a5317dc949b58495a17177fff0e5567354b9a976db3648adae6c70281c100ca75c55345260389c773a087547986bf7c00ed7356c5d4f244540d6220ee018eb91fbed9d01b013d2c9b43e90038774dfbb8c5d22cc7ee17a6b122e383d064f8606f175957528c0263e2656ff75361fb0d67cf4bbed2fe5895797afc284e0d391253c493e493d9ee67b4c59e277343e7405c1f546a12c8dd8e17f7f50bd00f2905bb0d7c095532a812f6433281c378904794069c232d9bb468173a8d52b1f3b33d33818453aa38244554d323d96c730f954d24ac9a8d6e20f04a4421eb8d53fb0281c07536d307dcb78cee070b1963f18c571d6045b1bca1c971f500199c058dcdea34c52b6a019115d4b9b3e3b5e6226002e576a045d17c5cf7f877a2ea81d4b04b8deb69671637fe3c16f64710f7c9ddf333f2de84a6c5b4fd9533597b37ce3504b477142d59f116378292d8284691f4d846e07dc70c9de5e339990246c4ca90a937f5095bedbe14ec9e2faabe3318cf93e1a225b9e07bd590406a1adc317e67f4f45f8bf284f54b56cefacb10d207e485a0e2159bb1a5457091c29b18194b411d2d0281c0626ec5e4494f45131c949bec8fe691dbd137463af86a30b1250554886c082b5bafa7be8b798157a5dd6513ac59be1a4b20e261d3609047b2da096f33fc673a6003ca9f2881ab11852bf8eb667771e3c6426b413ecb651be207b24b580364874b431cd50b2e6bafde182fac90cb0f6069334256bd1ce14b2564ab4493b00d950155d6eea46a89309ac4b554505f04a13e4c7eac84071a6a8458c34a65615a4168d88d8d1fbfe9976813093daa455053d4b4bb0fe49733c1e263d147b4d22821370281c1008fff09d04bbcf413fe17c68668f37b402015b54ae5a54d2bc57e86c43b0a5d136ce8e9237f33b799a2f8be9ac7cab43b5da3fc1b359c00cb622e771c347325b460de26cb7be0772c137b2c960c3339dd3ffd5efae7af5a9e91f9a9e85062584d7bbf92fec2c893addc2a8aaad30956b901f42ba41bcfb655d687930739a812e83545825a343358d1909177a453b50cf05523e05eac29953ba03548fcf5ab0a6f2b7e48a342712d56621cee4aa97f59c578da376a27b9b25204b389e9c6f8d28f'
);

INSERT INTO private_key_identity (
  private_key, identity
) VALUES (
  1, 1
);

INSERT INTO private_key_identity (
  private_key, identity
) VALUES (
  1, 3
);

/* Configurations */

INSERT INTO ike_configs (
  local, remote
) VALUES (
  '192.168.0.200', '192.168.0.1'
);

INSERT INTO peer_configs (
  name, ike_cfg, local_id, remote_id
) VALUES (
  'home', 1, 1, 2
);

INSERT INTO child_configs (
  name, updown
) VALUES (
  'home', '/usr/local/libexec/ipsec/_updown iptables'
);

INSERT INTO peer_config_child_config (
  peer_cfg, child_cfg
) VALUES (
  1, 1
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES ( /* 10.1.0.0/16 */
  7, X'0a010000', X'0a01ffff'
);

INSERT INTO traffic_selectors (
  type
) VALUES ( /* dynamic/32 */
  7
);

INSERT INTO child_config_traffic_selector (
  child_cfg, traffic_selector, kind
) VALUES (
  1, 1, 1
);

INSERT INTO child_config_traffic_selector (
	child_cfg, traffic_selector, kind
) VALUES (
  1, 2, 2
);

