

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 ( /* carol@strongswan.org */
  3, X'6361726f6c407374726f6e677377616e2e6f7267'
 );

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

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

/* 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=carol@strongswan.org */
  1, 1, X'3082049d30820305a003020102020101300d06092a864886f70d01010b05003047310b3009060355040613024348311b3019060355040a13127374726f6e675377616e2050726f6a656374311b3019060355040313127374726f6e675377616e20526f6f74204341301e170d3235313130363030313332365a170d3333313130363030313332365a305c310b3009060355040613024348311b3019060355040a13127374726f6e675377616e2050726f6a6563743111300f060355040b13085265736561726368311d301b06035504030c146361726f6c407374726f6e677377616e2e6f7267308201a2300d06092a864886f70d01010105000382018f003082018a0282018100d7cbc3a0cbd23c004eef1df6e477f0fc307e9241eac63524e4ff7e799d84096e07d839baf139bad3ac9041eccc724040b012dd52de12f791edbdcb86b9ecc9657961503aef1f8e0fed8385d87368dda333eb69ab4eab75822026cc02891e033203ec12659275cd4aea9962490ffcdc33efc9d2a89c2c4bf15d199ebf675b6bf8cfb2272659ca8c52f50d43ef50fc8fad1ea82b27a4755757aaa321129388b415564cb3f3e955ea45da96512479258e16e513d162b10bcd209eda6069770064011581e9fc62d4777d2e689f57869da0e68f2aeae1cc0b4c730b9dd2f641a9881f7d871e6ea6e4d2533170b8d191f005bbd15dcc319572044343e2f142ddd3167ad307053f8d1b8a0033edb6ed0f94c17a646c84a5bf2361ff7ce9fab83583e61fd1c542c0de300176b4d0943d1c49e15ffb3c10041165dfaaed2ae26ceec79bf537fc58f8cb6360980004f538ce6d3ff3ab9c74f695b2683e34e2053ff7625375c94aa672faf86f56864db4d27b9967dd9449f22ade1ac19342e8cff3082d6c450203010001a37f307d301f0603551d23041830168014288a0bd0d28511dae59ffa160ab021d708922d67301f0603551d110418301681146361726f6c407374726f6e677377616e2e6f726730390603551d1f04323030302ea02ca02a8628687474703a2f2f63726c2e7374726f6e677377616e2e6f72672f7374726f6e677377616e2e63726c300d06092a864886f70d01010b050003820181005a80b96a795c4c72bd86289c5eca1e555c7241fcbef2f27e3fc0a9980f837037a00564d3911a1e9e403294a7b266428cae81a7c87b0a46364d9b3043754016af628c9b97979e2c54353a1434b92937201376dfb24d9f4be305c52bc72e31c92cfac47228d4c753096789d19055be01cc4401c59c673e49b46330cef81af3a4439c6f216aa85dda2debdfebe7f5642ea911673c83ab9ee84214766d044a9a378c93fdb4762a2cf098255f53126b4c42d1d6937dbbde4484fdda9eb0850417f8576a835336c83beda69050af4e93cf04e0d688b6fb8ae86a3e22df8425d3bf1ed527547e717d7d08a989673d548e71a595ad70a503c966c40fd540b481ecdb74e479c5e0dfb00b677652121d9c164bbbc3db216cd1dad66c5a261a4583118dc47dee3c76e4403f8eb0cfc9901aa5b3285fbe9a619979e7a21aa745e3291d01150929bd35130bed453d5ad77753a7021ab5dc3fea35037018da26164737d5e28e3b6a532fdd3ef01d8f9600300f2cddf70c76e9dba872096591993eb928665915b4'
);

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, 4
);

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

/* Private Keys */

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

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

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

/* Configurations */

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

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

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
);

