Repository navigation
Expand file tree
/
Copy pathsslutils.sql.in
More file actions
138 lines (126 loc) · 5.84 KB
/
Copy pathsslutils.sql.in
File metadata and controls
138 lines (126 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
BEGIN;
CREATE OR REPLACE FUNCTION sslutils_version()
RETURNS text
AS '$libdir/sslutils', 'sslutils_version'
LANGUAGE C IMMUTABLE;
COMMENT ON FUNCTION sslutils_version() IS 'Returns the current version of sslutils';
CREATE OR REPLACE FUNCTION openssl_rsa_generate_key(integer)
RETURNS text
AS '$libdir/sslutils', 'openssl_rsa_generate_key'
LANGUAGE C VOLATILE STRICT SECURITY DEFINER
SET search_path = pg_catalog, pg_temp;
COMMENT ON FUNCTION openssl_rsa_generate_key(integer) IS 'Generates the RSA Private Key.
-- Parameter:
-- param1 : Number of bits.';
CREATE OR REPLACE FUNCTION openssl_rsa_key_to_csr(text, text, text, text, text, text, text)
RETURNS text
AS '$libdir/sslutils', 'openssl_rsa_key_to_csr'
LANGUAGE C VOLATILE STRICT SECURITY DEFINER
SET search_path = pg_catalog, pg_temp;
COMMENT ON FUNCTION openssl_rsa_key_to_csr(text, text, text, text, text, text, text) IS 'Generates the Certificate Signing Request (CSR).
-- Parameters:
-- param1 : rsa key
-- param2 : CN or common name e.g. agentN
-- param3 : C or Country
-- param4 : ST or State
-- param5 : L or Location (City)
-- param6 : OU or Organization Unit
-- param7 : email.';
CREATE OR REPLACE FUNCTION openssl_csr_to_crt(text, text, text, integer DEFAULT NULL)
RETURNS text
AS '$libdir/sslutils', 'openssl_csr_to_crt'
LANGUAGE C VOLATILE SECURITY DEFINER
SET search_path = pg_catalog, pg_temp;
COMMENT ON FUNCTION openssl_csr_to_crt(text, text, text, integer) IS 'Generates the CA/self signed certificate.
-- Parameters:
-- param1 : csr or certificate signing request
-- param2 : Path to CA certificate OR NULL If CA self signed certificate is required.
-- param3 : Path to CA private key OR Path to self private key, If param2 is NULL.
-- param4 : The validity (in days) for the generated certificate.';
CREATE OR REPLACE FUNCTION openssl_rsa_generate_crl(text, text, integer DEFAULT NULL)
RETURNS text
AS '$libdir/sslutils', 'openssl_rsa_generate_crl'
LANGUAGE C VOLATILE SECURITY DEFINER
SET search_path = pg_catalog, pg_temp;
COMMENT ON FUNCTION openssl_rsa_generate_crl(text, text, integer) IS 'Generates the Certificate Revocation List (CRL).
-- Parameters:
-- param1 : Path to CA certificate.
-- param2 : Path to CA private key.
-- param3 : The validity (in days) for the generated CRL.';
CREATE OR REPLACE FUNCTION openssl_is_crt_expire_on(text, timestamptz)
RETURNS integer
AS '$libdir/sslutils', 'openssl_is_crt_expire_on'
LANGUAGE C VOLATILE SECURITY DEFINER
SET search_path = pg_catalog, pg_temp;
COMMENT ON FUNCTION openssl_is_crt_expire_on(text, timestamptz) IS 'Compare certificate expiry on given time.
-- Parameters:
-- param1 : Path to certificate.
-- param2 : time to compare with end date';
CREATE OR REPLACE FUNCTION openssl_revoke_certificate(text, text, integer DEFAULT NULL)
RETURNS text
AS '$libdir/sslutils', 'openssl_revoke_certificate'
LANGUAGE C VOLATILE SECURITY DEFINER
SET search_path = pg_catalog, pg_temp;
COMMENT ON FUNCTION openssl_revoke_certificate(text, text, integer) IS 'Revoke Certificate and add to Certificate Revocation List (CRL).
-- Parameters:
-- param1 : Path to client certificate to be revoked.
-- param2 : CRL file name specified in postgres config file.
-- param3 : The validity (in days) for the revoked certificate.';
CREATE OR REPLACE FUNCTION openssl_get_crt_expiry_date(text)
RETURNS timestamptz
AS '$libdir/sslutils', 'openssl_get_crt_expiry_date'
LANGUAGE C VOLATILE SECURITY DEFINER
SET search_path = pg_catalog, pg_temp;
COMMENT ON FUNCTION openssl_get_crt_expiry_date(text) IS 'Return the expiry date of the given certificate.
-- Parameters:
-- param1 : Path to certificate.';
DO $sslutils$
DECLARE
owner_role pg_catalog.pg_roles%ROWTYPE;
signature text;
BEGIN
SELECT * INTO owner_role FROM pg_catalog.pg_roles
WHERE rolname = 'sslutils_owner';
IF NOT FOUND THEN
CREATE ROLE sslutils_owner NOLOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE
INHERIT NOREPLICATION;
ELSE
IF owner_role.rolcanlogin OR owner_role.rolsuper
OR owner_role.rolcreatedb OR owner_role.rolcreaterole
OR owner_role.rolreplication OR owner_role.rolbypassrls
OR NOT owner_role.rolinherit
OR EXISTS (SELECT FROM pg_catalog.pg_auth_members
WHERE roleid = owner_role.oid)
OR EXISTS (SELECT FROM pg_catalog.pg_auth_members AS membership
JOIN pg_catalog.pg_roles AS granted
ON granted.oid = membership.roleid
WHERE membership.member = owner_role.oid
AND granted.rolname NOT IN
('pg_read_server_files', 'pg_write_server_files')) THEN
RAISE EXCEPTION 'sslutils_owner must be an INHERIT, nonadministrative NOLOGIN role with no members and only file-role memberships';
END IF;
END IF;
IF pg_catalog.current_setting('server_version_num')::integer >= 110000 THEN
GRANT pg_read_server_files, pg_write_server_files TO sslutils_owner;
END IF;
FOREACH signature IN ARRAY ARRAY[
'openssl_rsa_generate_key(integer)',
'openssl_rsa_key_to_csr(text,text,text,text,text,text,text)',
'openssl_csr_to_crt(text,text,text,integer)',
'openssl_rsa_generate_crl(text,text,integer)',
'openssl_is_crt_expire_on(text,timestamptz)',
'openssl_revoke_certificate(text,text,integer)',
'openssl_get_crt_expiry_date(text)'
]
LOOP
EXECUTE 'ALTER FUNCTION ' || signature ||
' SECURITY DEFINER VOLATILE SET search_path = pg_catalog, pg_temp';
EXECUTE 'REVOKE ALL ON FUNCTION ' || signature || ' FROM PUBLIC';
EXECUTE 'ALTER FUNCTION ' || signature || ' OWNER TO sslutils_owner';
IF EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'pem_agent') THEN
EXECUTE 'GRANT EXECUTE ON FUNCTION ' || signature || ' TO pem_agent';
END IF;
END LOOP;
END
$sslutils$;
COMMIT;