Blame SOURCES/0050-FIPS-enable-pkcs12-mac.patch

bf760f
diff -up openssl-3.0.1/crypto/pkcs12/p12_key.c.pkc12_fips openssl-3.0.1/crypto/pkcs12/p12_key.c
bf760f
--- openssl-3.0.1/crypto/pkcs12/p12_key.c.pkc12_fips	2022-02-21 12:35:24.829893907 +0100
bf760f
+++ openssl-3.0.1/crypto/pkcs12/p12_key.c	2022-02-21 13:01:22.711622967 +0100
bf760f
@@ -85,17 +85,41 @@ int PKCS12_key_gen_uni_ex(unsigned char
bf760f
     EVP_KDF *kdf;
bf760f
     EVP_KDF_CTX *ctx;
bf760f
     OSSL_PARAM params[6], *p = params;
bf760f
+    char *adjusted_propq = NULL;
bf760f
 
bf760f
     if (n <= 0)
bf760f
         return 0;
bf760f
 
bf760f
-    kdf = EVP_KDF_fetch(libctx, "PKCS12KDF", propq);
bf760f
-    if (kdf == NULL)
bf760f
+    if (ossl_get_kernel_fips_flag()) {
bf760f
+        const char *nofips = "-fips";
bf760f
+        size_t len = propq ? strlen(propq) + 1 + strlen(nofips) + 1 :
bf760f
+                                                 strlen(nofips) + 1;
bf760f
+        char *ptr = NULL;
bf760f
+
bf760f
+        adjusted_propq = OPENSSL_zalloc(len);
bf760f
+        if (adjusted_propq != NULL) {
bf760f
+            ptr = adjusted_propq;
bf760f
+            if (propq) {
bf760f
+                memcpy(ptr, propq, strlen(propq));
bf760f
+                ptr += strlen(propq);
bf760f
+                *ptr = ',';
bf760f
+                ptr++;
bf760f
+            }
bf760f
+            memcpy(ptr, nofips, strlen(nofips));
bf760f
+        }
bf760f
+    }
bf760f
+
bf760f
+    kdf = adjusted_propq ? EVP_KDF_fetch(libctx, "PKCS12KDF", adjusted_propq) : EVP_KDF_fetch(libctx, "PKCS12KDF", propq);
bf760f
+    if (kdf == NULL) {
bf760f
+        OPENSSL_free(adjusted_propq);
bf760f
         return 0;
bf760f
+    }
bf760f
     ctx = EVP_KDF_CTX_new(kdf);
bf760f
     EVP_KDF_free(kdf);
bf760f
-    if (ctx == NULL)
bf760f
+    if (ctx == NULL) {
bf760f
+        OPENSSL_free(adjusted_propq);
bf760f
         return 0;
bf760f
+    }
bf760f
 
bf760f
     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
bf760f
                                             (char *)EVP_MD_get0_name(md_type),
bf760f
@@ -127,6 +149,7 @@ int PKCS12_key_gen_uni_ex(unsigned char
bf760f
         } OSSL_TRACE_END(PKCS12_KEYGEN);
bf760f
     }
bf760f
     EVP_KDF_CTX_free(ctx);
bf760f
+    OPENSSL_free(adjusted_propq);
bf760f
     return res;
bf760f
 }
bf760f
 
bf760f
diff -up openssl-3.0.1/apps/pkcs12.c.pkc12_fips_apps openssl-3.0.1/apps/pkcs12.c
bf760f
--- openssl-3.0.1/apps/pkcs12.c.pkc12_fips_apps	2022-02-21 16:37:07.908923682 +0100
bf760f
+++ openssl-3.0.1/apps/pkcs12.c	2022-02-21 17:38:44.555345633 +0100
bf760f
@@ -765,15 +765,34 @@ int pkcs12_main(int argc, char **argv)
bf760f
     }
bf760f
     if (macver) {
bf760f
         EVP_KDF *pkcs12kdf;
bf760f
+        char *adjusted_propq = NULL;
bf760f
+        const char *nofips = "-fips";
bf760f
+        size_t len = app_get0_propq() ? strlen(app_get0_propq()) + 1 + strlen(nofips) + 1 :
bf760f
+                                                            strlen(nofips) + 1;
bf760f
+        char *ptr = NULL;
bf760f
+
bf760f
+        adjusted_propq = OPENSSL_zalloc(len);
bf760f
+        if (adjusted_propq != NULL) {
bf760f
+                ptr = adjusted_propq;
bf760f
+                if (app_get0_propq()) {
bf760f
+                    memcpy(ptr, app_get0_propq(), strlen(app_get0_propq()));
bf760f
+                    ptr += strlen(app_get0_propq());
bf760f
+                    *ptr = ',';
bf760f
+                    ptr++;
bf760f
+                }
bf760f
+                memcpy(ptr, nofips, strlen(nofips));
bf760f
+        }
bf760f
 
bf760f
         pkcs12kdf = EVP_KDF_fetch(app_get0_libctx(), "PKCS12KDF",
bf760f
-                                  app_get0_propq());
bf760f
+                                  adjusted_propq ? adjusted_propq : app_get0_propq());
bf760f
         if (pkcs12kdf == NULL) {
bf760f
             BIO_printf(bio_err, "Error verifying PKCS12 MAC; no PKCS12KDF support.\n");
bf760f
             BIO_printf(bio_err, "Use -nomacver if MAC verification is not required.\n");
bf760f
+            OPENSSL_free(adjusted_propq);
bf760f
             goto end;
bf760f
         }
bf760f
         EVP_KDF_free(pkcs12kdf);
bf760f
+        OPENSSL_free(adjusted_propq);
bf760f
         /* If we enter empty password try no password first */
bf760f
         if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
bf760f
             /* If mac and crypto pass the same set it to NULL too */