pgcrypto: Add function to check FIPS mode
authorDaniel Gustafsson <[email protected]>
Fri, 24 Jan 2025 13:18:40 +0000 (14:18 +0100)
committerDaniel Gustafsson <[email protected]>
Fri, 24 Jan 2025 13:18:40 +0000 (14:18 +0100)
This adds a SQL callable function for reading and returning the status
of FIPS configuration of OpenSSL.  If OpenSSL is operating with FIPS
enabled it will return true, otherwise false.  As this adds a function
to the SQL file, bump the extension version to 1.4.

Author: Daniel Gustafsson <[email protected]>
Reviewed-by: Joe Conway <[email protected]>
Discussion: https://postgr.es/m/8f979145-e206-475a-a31b-73c977a4134c@joeconway.com

contrib/pgcrypto/Makefile
contrib/pgcrypto/meson.build
contrib/pgcrypto/openssl.c
contrib/pgcrypto/pgcrypto--1.3--1.4.sql [new file with mode: 0644]
contrib/pgcrypto/pgcrypto.c
contrib/pgcrypto/pgcrypto.control
contrib/pgcrypto/px.h
doc/src/sgml/pgcrypto.sgml

index 85f1c94681356f6e5ab6372bca5534ee6030a1b5..d40216dd4209ee5be6a567414caff18093c5d88b 100644 (file)
@@ -36,7 +36,7 @@ MODULE_big    = pgcrypto
 
 EXTENSION = pgcrypto
 DATA = pgcrypto--1.3.sql pgcrypto--1.2--1.3.sql pgcrypto--1.1--1.2.sql \
-   pgcrypto--1.0--1.1.sql
+   pgcrypto--1.0--1.1.sql pgcrypto--1.3--1.4.sql
 PGFILEDESC = "pgcrypto - cryptographic functions"
 
 REGRESS = init md5 sha1 hmac-md5 hmac-sha1 blowfish rijndael \
index 0bcbe4cfe5a7b7ad708e380eb352d8d37f87ac2a..7a4e8e76d64c20e8a5810b7c14180f93dec3ccd3 100644 (file)
@@ -93,6 +93,7 @@ install_data(
   'pgcrypto--1.1--1.2.sql',
   'pgcrypto--1.2--1.3.sql',
   'pgcrypto--1.3.sql',
+  'pgcrypto--1.3--1.4.sql',
   'pgcrypto.control',
   kwargs: contrib_data_args,
 )
index 448db331a0f1e7e2f4b14e0a547ce385f4100b69..e6870c72c9a79b35c18dc506f60f455488b9a7b1 100644 (file)
@@ -794,3 +794,30 @@ ResOwnerReleaseOSSLCipher(Datum res)
 {
    free_openssl_cipher((OSSLCipher *) DatumGetPointer(res));
 }
+
+/*
+ * CheckFIPSMode
+ *
+ * Returns the FIPS mode of the underlying OpenSSL installation.
+ */
+bool
+CheckFIPSMode(void)
+{
+   int         fips_enabled = 0;
+
+   /*
+    * EVP_default_properties_is_fips_enabled was added in OpenSSL 3.0, before
+    * that FIPS_mode() was used to test for FIPS being enabled.  The last
+    * upstream OpenSSL version before 3.0 which supported FIPS was 1.0.2, but
+    * there are forks of 1.1.1 which are FIPS validated so we still need to
+    * test with FIPS_mode() even though we don't support 1.0.2.
+    */
+   fips_enabled =
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+       EVP_default_properties_is_fips_enabled(NULL);
+#else
+       FIPS_mode();
+#endif
+
+   return (fips_enabled == 1);
+}
diff --git a/contrib/pgcrypto/pgcrypto--1.3--1.4.sql b/contrib/pgcrypto/pgcrypto--1.3--1.4.sql
new file mode 100644 (file)
index 0000000..b942bde
--- /dev/null
@@ -0,0 +1,9 @@
+/* contrib/pgcrypto/pgcrypto--1.3--1.4.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION pgcrypto UPDATE TO '1.4'" to load this file. \quit
+
+CREATE FUNCTION fips_mode()
+RETURNS bool
+AS 'MODULE_PATHNAME', 'pg_check_fipsmode'
+LANGUAGE C VOLATILE STRICT PARALLEL SAFE;
index ebd76eed702e262a1bc399c9da243a02b3f54f9c..ee2a010e402fae7729f6a9a26b3d2a4103f6be32 100644 (file)
@@ -450,6 +450,14 @@ pg_random_uuid(PG_FUNCTION_ARGS)
    return gen_random_uuid(fcinfo);
 }
 
+PG_FUNCTION_INFO_V1(pg_check_fipsmode);
+
+Datum
+pg_check_fipsmode(PG_FUNCTION_ARGS)
+{
+   PG_RETURN_BOOL(CheckFIPSMode());
+}
+
 static void *
 find_provider(text *name,
              PFN provider_lookup,
index d2151d3bc4be5b9513fa93ba1b4f2eb3d84aefb2..fcdd0b46f5b2da35b1bff19264fdaad623a09d79 100644 (file)
@@ -1,6 +1,6 @@
 # pgcrypto extension
 comment = 'cryptographic functions'
-default_version = '1.3'
+default_version = '1.4'
 module_pathname = '$libdir/pgcrypto'
 relocatable = true
 trusted = true
index 471bb4ec7274c216977854dd386a990f44b46ea6..c2c2fc31245d6630b6e4c2d910d0a101c175dd10 100644 (file)
@@ -182,6 +182,8 @@ void        px_set_debug_handler(void (*handler) (const char *));
 
 void       px_memset(void *ptr, int c, size_t len);
 
+bool       CheckFIPSMode(void);
+
 #ifdef PX_DEBUG
 void       px_debug(const char *fmt,...) pg_attribute_printf(1, 2);
 #else
index 396c67f0cdef2350c6c8d26d0a93ee8121a91e0a..838d7532a526ef260a6beac26c9fa8fcce1c967a 100644 (file)
@@ -1149,6 +1149,22 @@ gen_random_uuid() returns uuid
   </para>
  </sect2>
 
+ <sect2 id="pgcrypto-openssl-support-funcs">
+  <title>OpenSSL Support Functions</title>
+
+  <indexterm>
+   <primary>fips_mode</primary>
+  </indexterm>
+
+<synopsis>
+fips_mode() returns boolean
+</synopsis>
+  <para>
+   Returns <literal>true</literal> if <productname>OpenSSL</productname> is
+   running with FIPS mode enabled, otherwise <literal>false</literal>.
+  </para>
+ </sect2>
+
  <sect2 id="pgcrypto-notes">
   <title>Notes</title>