

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 ( /* C=CH, O=strongSwan Project, CN=strongSwan Root CA */
  9, X'3047310b3009060355040613024348311b3019060355040a13127374726f6e675377616e2050726f6a656374311b3019060355040313127374726f6e675377616e20526f6f74204341'
);

INSERT INTO identities (
  type, data
) VALUES ( /* subjkey of 'C=CH, O=strongSwan Project, CN=strongSwan Root CA' */
  11, X'288a0bd0d28511dae59ffa160ab021d708922d67'
);

INSERT INTO identities (
  type, data
) VALUES ( /* keyid of 'C=CH, O=strongSwan Project, CN=strongSwan Root CA' */
  11, X'91f8e4475f0bc75a388624bcf0a56fe6a45f0c2a'
);

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

INSERT INTO identities (
  type, data
) VALUES ( /* sun.strongswan.org */
  2, X'73756e2e7374726f6e677377616e2e6f7267'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* subjkey of 'C=CH, O=strongSwan Project, CN=sun.strongswan.org' */
  11, X'823451a9fb1c25adaea981081174c5eb42372e23'
 );

/* Certificates */

INSERT INTO certificates (
   type, keytype, data
) VALUES ( /* C=CH, O=strongSwan Project, CN=strongSwan Root CA */
  1, 1, X'30820455308202bda00302010202083e9346d3b12d6782300d06092a864886f70d01010b05003047310b3009060355040613024348311b3019060355040a13127374726f6e675377616e2050726f6a656374311b3019060355040313127374726f6e675377616e20526f6f74204341301e170d3235313130363030313332365a170d3335313130373030313332365a3047310b3009060355040613024348311b3019060355040a13127374726f6e675377616e2050726f6a656374311b3019060355040313127374726f6e675377616e20526f6f74204341308201a2300d06092a864886f70d01010105000382018f003082018a0282018100997a7e790296b1dfe697090d480043ab51d6deb0a4415223a2dcdd7cfee9f2aba271ed293f12691ba6fd3bf3b477e7dfa4e57778dca316075725056c0a35cc273aed5411b04663998f04eccb8066082d615f15e0588469a595acadfa8fcfeaa5bacc09192c4a4c45792672e291a4da45708561ac6b8c5ed5aaf5a25761c6357d18875b0391cbd4c6265351c814bea4fcc9fbd3e606794f02d89326162ab86e9194f30840e920c192c2d97129e8e93e08543ba8c4cb2c057b9e64215b5ec93a5cd8ef040b3e4df77e617cb4c79afa94385e2c90de44910d4fa112db3deef80589851ba090405c70a061ac752391963eb68c837bfbb8a5ead5f30b54c797591c4ab42904c4930994452b9b776f983c7e3debe5b1ce5b029e5daab6e720e7bc54d4dac75a702cbd809e695b6d9916cac2279a5c04ba6eaab70646274efb99600604604b836542140b43020ad6b2c33c2f8326fd4a34391e2011092d321e84820425bb6562428138ad630cf12ce66de2d85b33151ec43e4504c722f1384e80accbb10203010001a345304330120603551d130101ff040830060101ff020101300e0603551d0f0101ff040403020106301d0603551d0e04160414288a0bd0d28511dae59ffa160ab021d708922d67300d06092a864886f70d01010b050003820181007ccad9c8210802532b184e5fbd23a15cf6830ca783c5cefd1485ea7dd9e5a339074c6e089193d41d4402f31f260be503ab54aa5fc710261ca72455c3aec5f66224ccac23c57b901aaf8df4d878151082765ae6ed854204ec6342dcf6c091852d094ceb528beb7699a1e58d13f3072aa432e74a72ceee4ef57d6deb5963ddaeef15cbec267247a05c1cf2824a5a774bcf31e75f480e17b24568ba547d04ca9a905797cffee0fa309044389b5e23c2c2cb08c18f42a049b5f613f9c75c9cb42d57dc2181877dc8f7351a6d2c1ad81eba12d56b52f96609b696bb6e01ddec0efd893cca413f6b99216930ac4f0e152ac1773f18016323b408b8fc5e0b0acb46a515647cfe6678268ee85637381c8a0497559261e03205e057fcc3303fd0c9cb9183cb1de89c6ea5e7cd95aeb8b5a4413420035ff233d876d4480c0202462184a9a462afcd6502dd1b1badfd590609281fd68e0daf5ad62e2789c92763e843f0cb8cf5fdca9e847533c791a54ae574373e1f4fbc966d90f7fcc994859c7971050f7f'
);

INSERT INTO certificates (
   type, keytype, data
) VALUES ( /* C=CH, O=strongSwan Project, CN=sun.strongswan.org */
  1, 1, X'30820486308202eea003020102020104300d06092a864886f70d01010b05003047310b3009060355040613024348311b3019060355040a13127374726f6e675377616e2050726f6a656374311b3019060355040313127374726f6e675377616e20526f6f74204341301e170d3235313130363030313332365a170d3333313130363030313332365a3047310b3009060355040613024348311b3019060355040a13127374726f6e675377616e2050726f6a656374311b30190603550403131273756e2e7374726f6e677377616e2e6f7267308201a2300d06092a864886f70d01010105000382018f003082018a0282018100ad64df8d63e1d146727388fe6ced2a17a6a54839a1b413b4efe1568e50aecf4f6a4e1ff3a0e4aeb11b5b5ee8049fadfe6c100b327e9f9f6987d518c4effdef76b99723c78c64c3eea8802264f4e301160be47d378eb17250c7718ea1fc9392177a9152bfef962e9e72c47ed3b668ed7e4a2b778cb89da1a1d77afe830b280006bb0d73f0a9bde1c9127ef777d3572ff9a2280136003ffac737e5acb7d409942129542c0e609166a0a10cca38a3b9224041eca1b796d2da97e1d6a26d00e33464b46b172b1523411a14980eeafc797dbfd177081d9d85b5e0be15b35fd99858ec6a3b87793c21880b5d5ebbd930e471e25245d604cd318ac3bd1c396e11f650386b31a06a6e8556d582f71dc47384d4d7c719fdb5e3267f3ddeafc47725487176d8b26e11e242509fbd02896562072a8b7aa7170a3d5840d0fd907d1ef9c6cf5dbcb3144ae2df6e9efa580e8be8244b3e9f91ce84d930220affa48dbf735ca823e0902a7c6a9b538c567ef0f961f5faee6472661749e4d8b727d58667869404590203010001a37d307b301f0603551d23041830168014288a0bd0d28511dae59ffa160ab021d708922d67301d0603551d1104163014821273756e2e7374726f6e677377616e2e6f726730390603551d1f04323030302ea02ca02a8628687474703a2f2f63726c2e7374726f6e677377616e2e6f72672f7374726f6e677377616e2e63726c300d06092a864886f70d01010b0500038201810088384ed873e07991e9120701ce1daccefe7807846a7fd8c2a4513b4d3772d262997bb3b07f809a9e62723a9457a1b76d14e374c260d7f2a96f2d098692dd7f9d2c24dc7033126fa054ba3fd5536c583b83638b57faf69fb40f379a46a403e64dd7007b93839f4823a33ed83c10a42b1a0ee2448d4d3949a8943bd2eb6ddd033e9149f9b02768d4e3092cf9e18ed45dce386fb48e6923b0575d526fe4b3b1274a990c3d411f21294ee338f7ca62a5fe089bee8471df2c496fb13951c7c2caa8bbf5f53daf321f3b5558c6704484bbfd7cdab7f5125c92b732c8a5b5c2847eaba247347ace4cffa133dac3424c3b9710f512610e9279aca0af2a29884d982b64dcdcab117c9062c384fdc6e437056d8abf59d9c48b15d1405486bdf8a75802b8d76e7cb5fbb7c47971b4188ad389d743a42c55433d3112db067d01cb81166b93a791a546455e47c9bd4949ea9c112d83f2126ac4bffa2a62aa7ad05f1d0d09653683cb4d54a49d53ff55a6e79494f67f8c23496bb7584bb42fff04eaf0ec3e074a'
);

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

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

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

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

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

/* Private Keys */

INSERT INTO private_keys (
   type, data
) VALUES ( /* key of 'C=CH, O=strongSwan Project, CN=sun.strongswan.org' */
  1, X'308206e20201000282018100ad64df8d63e1d146727388fe6ced2a17a6a54839a1b413b4efe1568e50aecf4f6a4e1ff3a0e4aeb11b5b5ee8049fadfe6c100b327e9f9f6987d518c4effdef76b99723c78c64c3eea8802264f4e301160be47d378eb17250c7718ea1fc9392177a9152bfef962e9e72c47ed3b668ed7e4a2b778cb89da1a1d77afe830b280006bb0d73f0a9bde1c9127ef777d3572ff9a2280136003ffac737e5acb7d409942129542c0e609166a0a10cca38a3b9224041eca1b796d2da97e1d6a26d00e33464b46b172b1523411a14980eeafc797dbfd177081d9d85b5e0be15b35fd99858ec6a3b87793c21880b5d5ebbd930e471e25245d604cd318ac3bd1c396e11f650386b31a06a6e8556d582f71dc47384d4d7c719fdb5e3267f3ddeafc47725487176d8b26e11e242509fbd02896562072a8b7aa7170a3d5840d0fd907d1ef9c6cf5dbcb3144ae2df6e9efa580e8be8244b3e9f91ce84d930220affa48dbf735ca823e0902a7c6a9b538c567ef0f961f5faee6472661749e4d8b727d586678694045902030100010282018042b2fcf3acaa5f105452712e3dc4755ea7b208b872a58cb71b57792e41871d2415667c1001a23638d6a72d38df1e7595db07c8e6e1a29175ccd48234af84e52aac75a143cf93ab7310ca4970c06ada2e1ae107f1e928052b61ad230ad8d8036898b3723b76f43476aafb3f363d82db68ab2b514f866ccc922f0b96ffddb8c359d3767754ea56aa95e85182ec4b15ccd4a035f751488353fa725c1d36919025b523eac70fb8c1f5d270613520d88a0cccdf0f219e379c608df047da2f481a1e3ac4bc761fe1de006b7e4bd58437820e59cef0c5558b13a01012ea75dd7f06d8a13735477c44c51fff6a894868836ec9d50210f57c2efbce0f0e4928d5a592d76fe2ba067534a9e8704ce5e9aab7382cc05418ff01149644c6d71cb3e502c22a527c8a7606b18e02fb94837cb7786ea0a1f78767c6f7a233f9a52c2e6cb59566c86bec81347ee0d359f4127ee5935b90d49f4eb0df1806132058a3eabde2aec1ecb19f64ed357b0773ae8e39ed9382e00453bdc1cca870be7b60a7d063e41d8c290281c100d38d26a8a23138eebd1760a3b093c719217d54084fbff9eb83d140403fba14e957a24363ecb1d3b4c55d5e4de25af7e11eb0b98bf1e8538f1640ded2218c8bb4c2bb17499d1a7bac1f6c68dc136df73a7344c17f6018a1386e9fe004c570a12f9a48b64c01b7d7da0b02abedcc49e41cd9d634647130b5641b9706941fb226129df8785c64ef78774833b6cf2f6e078863f10d0dbc93bb96df026d384a80fa6e68004886cead5a9a6c1dd7011eaab79609171fc14bed8aa2a1b19c5cd6ace8db0281c100d1d3544942003ede1e41dd9e83d39e8661e13f863d7e21cad4fa9dfa0a37630619b3b3a5ba54b498c6d4eb39808bbd071cd0e2e755c0763cc071cb6b6fa58726f6dbb47f1f9e1b17f325be2ec76f503c576a0d228d4bf5a66e7657d3cff0608250a95769d6d36aece5c71149788dc2b83a521f00deedc4b30a3ff0d628b7b39260e86a6839c3fd4697a235e6a869889ea6fd8c2cf3faf2f8581346ea2c7caa3dd5366f39b9029e1d37118b6b8b72df065996b99b9c3913e9ae0e74f606fec3db0281c100c77ddecbbd0f1fb9fa34965c26026508eccccbb67ec5f13e4772c6dd0f7b4186fdadb551007e7cbe863352dfad29cd80cbb9296fafb23f41ba6177fdbdc754632412db95fe815fc863658ec8a1c5dc12916a6ff1d6c3c31c7d2d6449fb8a0765e55ed6117cb949e282e98509dcd07d3d3baac483473e007fa388da9a49fd6bebb86dfbca5d2079803a6511e9f4b71481688582a2c686a5aa53286da85c919e170919e34ac49132d4564a195b23f926e064381b28e399e05716956f7718b5bb6b0281bf5bcc1913f3cc27b50587cb6d8e3f071bc3b6c813f2d33bf5813823e54092fabfb07eee19936b7b975b65cb835cb9c5f956a20ca328e10ad989583fa6c131a9ef6210bce6e0ee4aec2f7453c023808f99d6a48f1a8eb6acc22391a2db081e221ae12f350f18ed65da3eacd8e34bdab955e02e0d625425b1e8a29356ca5b06038a06dba7b1f193cd3b151be27907352490447ea8d501e8c863de2490e1e294a7e665616b47d5a94f837894346ad89768fb5e3692fd815b775ac4cb8e6014af610281c0383c2ec48b3dc9e8cb1f0b38cf41d937dba5b116df6d1a8873ebc97e880b3c85565cffc0046e7aad746322def41f5767be5c64a8c3e38a9204772d26c82ab20cafc484b7bb9c7137060c9c117c4627af9db661f63a52af3acf2aa8281ce499d76d3fc6d1de7700bd146ac2c2a1488f0044db234f5d7026f04dc7463c42b5b8a52ebc53d405cea3f64038b27fa158344f09df950e328b378ae3ab85421fbfb9618039f9c37c6e07abf0cc8f1dbc0eff5528fbadc979ccfefcb40acaddabee3858'
);

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

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

/* Configurations */

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

INSERT INTO peer_configs (
  name, ike_cfg, local_id, remote_id, mobike, dpd_delay
) VALUES (
  'net-net', 1, 5, 4, 0, 0
);

INSERT INTO child_configs (
  name, updown
) VALUES (
  'net-net', '/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 (
  7, X'0a010000', X'0a01ffff'
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a020000', X'0a02ffff'
);

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

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

