

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'2d2d2d2d2d424547494e2043455254494649434154452d2d2d2d2d0a4d4949455654434341723267417749424167494950704e47303745745a3449774451594a4b6f5a496876634e4151454c42514177527a454c4d416b47413155450a42684d4351306778477a415a42674e5642416f54456e4e30636d39755a314e335957346755484a76616d566a644445624d426b474131554541784d53633352790a6232356e55336468626942536232393049454e424d423458445449314d5445774e6a41774d544d794e6c6f5844544d314d5445774e7a41774d544d794e6c6f770a527a454c4d416b474131554542684d4351306778477a415a42674e5642416f54456e4e30636d39755a314e335957346755484a76616d566a644445624d426b470a4131554541784d53633352796232356e55336468626942536232393049454e424d4949426f6a414e42676b71686b6947397730424151454641414f43415938410a4d49494269674b43415945416d58702b65514b5773642f6d6c776b4e53414244713148573372436b5156496a6f747a6466503770387175696365307050784a700a473662394f2f4f30642b6666704f5633654e796a466764584a515673436a584d4a7a727456424777526d4f5a6a7754737934426d4343316858785867574952700a705a5773726671507a2b716c7573774a4753784b544556354a6e4c696b61546152584346596178726a46375671765769563248474e583059683173446b6376550a78695a5455636755767154387966765435675a3554774c596b7959574b7268756b5a547a43454470494d475377746c784b656a70506768554f366a45797977460a6535356b4956746579547063324f3845437a354e39333568664c54486d7671554f4634736b4e35456b5131506f524c625065373442596d4647364351514678770a6f47477364534f526c6a36326a494e372b37696c3674587a433154486c316b6353725170424d5354435a52464b35743362356738666a33723562484f57774b650a586171323579446e764654553273646163437939674a35705732325a467372434a357063424c7075717263475269644f2b356c674267526753344e6c5168514c0a5177494b31724c4450432b444a76314b4e446b654942454a4c544965684949454a62746c596b4b424f4b316a44504573356d33693246737a46523745506b55450a78794c784f453641724d757841674d424141476a525442444d42494741315564457745422f7751494d415942416638434151457744675944565230504151482f0a42415144416745474d42304741315564446751574242516f69677651306f5552327557662b68594b73434858434a49745a7a414e42676b71686b6947397730420a4151734641414f4341594541664d725a79434549416c4d724745356676534f6858506144444b6544786337394649587166646e6c6f7a6b48544734496b5a50550a485551433878386d432b554471315371583863514a68796e4a465844727358325969544d7243504665354161723433303248675645494a3257756274685549450a37474e43335062416b59557443557a72556f767264706d68355930543877637170444c6e536e4c4f376b3731665733725757506472753856792b776d636b65670a58427a79676b7061643076504d65646653413458736b566f756c5239424d71616b4665587a2f37672b6a4351524469625869504377737349775939436f456d310a3968503578317963744331583343474268333349397a556162537761324236364574567255766c6d43626157753234423365774f2f596b38796b452f61356b680a61544373547734564b7346335078674259794f30434c6a385867734b7930616c465752382f6d5a344a6f376f566a633448496f456c31575359654179426542580a2f4d4d775039444a793547447978336f6e47366c35383256727269317045453049414e66386a5059647452494441494352694745716152697238316c417430620a473633395751594a4b422f576a673276577459754a346e4a4a32506f512f444c6a50583979703645645450486b61564b3558513350683950764a5a746b5066380a795a53466e486c784251392f0a2d2d2d2d2d454e442043455254494649434154452d2d2d2d2d0a'
);

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

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'[encoded data, 4910 characters]'
);

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

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

/* Proposals */

INSERT INTO proposals (
  proposal
) VALUES (
  'aes128-sha256-modp3072'
);

/* Configurations */

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

INSERT INTO ike_config_proposal (
  ike_cfg, prio, prop
) VALUES (
  1, 1, 1
);

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

INSERT INTO child_configs (
  name, updown, start_action, dpd_action
) VALUES (
  'net-1', '/usr/local/libexec/ipsec/_updown iptables', 1, 1
);

INSERT INTO child_configs (
  name, updown, start_action, dpd_action
) VALUES (
  'net-2', '/usr/local/libexec/ipsec/_updown iptables', 1, 1
);

INSERT INTO child_configs (
  name, updown, start_action, dpd_action
) VALUES (
  'net-3', '/usr/local/libexec/ipsec/_updown iptables', 0, 0
);

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

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

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

INSERT INTO child_config_proposal (
  child_cfg, prio, prop
) VALUES (
  1, 1, 1
);

INSERT INTO child_config_proposal (
  child_cfg, prio, prop
) VALUES (
  2, 1, 1
);

INSERT INTO child_config_proposal (
  child_cfg, prio, prop
) VALUES (
  3, 1, 1
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a010000', X'0a01000f'
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a010010', X'0a01001f'
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a010200', X'0a0103ff'
);

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

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a020200', X'0a0203ff'
);

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

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

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

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

INSERT INTO child_config_traffic_selector (
	child_cfg, traffic_selector, kind
) VALUES (
  3, 5, 0
);
