Index: trunk/src/VBox/Runtime/common/crypto/pemfile.cpp
===================================================================
--- trunk/src/VBox/Runtime/common/crypto/pemfile.cpp	(revision 57358)
+++ trunk/src/VBox/Runtime/common/crypto/pemfile.cpp	(revision 57572)
@@ -125,6 +125,5 @@
                 uint8_t const  *pbSavedContent = pbContent;
                 size_t  const   cbSavedContent = cbContent;
-                uint32_t        iMarker = 0;
-                while (iMarker < cMarkers)
+                for (uint32_t iMarker = 0; iMarker < cMarkers; iMarker++)
                 {
                     pbContent = pbSavedContent;
@@ -143,11 +142,14 @@
                         cbContent -= cchWord;
 
-                        if (!cbContent || !RT_C_IS_BLANK(*pbContent))
-                            break;
-                        do
-                        {
-                            pbContent++;
-                            cbContent--;
-                        } while (cbContent > 0 && RT_C_IS_BLANK(*pbContent));
+                        if (!cbContent)
+                            break;
+                        if (RT_C_IS_BLANK(*pbContent))
+                            do
+                            {
+                                pbContent++;
+                                cbContent--;
+                            } while (cbContent > 0 && RT_C_IS_BLANK(*pbContent));
+                        else if (cWords > 1 || pbContent[0] != '-')
+                            break;
 
                         cWords--;
@@ -175,5 +177,5 @@
                                 if (poffEnd)
                                     *poffEnd = pbContent - pbStart;
-                                if (*ppMatch)
+                                if (ppMatch)
                                     *ppMatch = &paMarkers[iMarker];
                                 return true;
@@ -265,13 +267,18 @@
 {
     /*
-     * Assume a well formed PEM file contains only 7-bit ASCII and restricts
-     * itself to the following control characters:
+     * Well formed PEM files should probably only contain 7-bit ASCII and
+     * restrict thenselfs to the following control characters:
      *      tab, newline, return, form feed
+     *
+     * However, if we wan't to read PEM files which contains human readable
+     * certificate details before or after each base-64 section, we can't stick
+     * to 7-bit ASCII.  We could say it must be UTF-8, but that's probably to
+     * limited too.  So, we'll settle for detecting binary files by control
+     * characters alone (safe enough for DER encoded stuff, I think).
      */
     while (cbFile-- > 0)
     {
         uint8_t const b = *pbFile++;
-        if (   b >= 0x7f
-            || (b < 32 && b != '\t' && b != '\n' && b != '\r' && b != '\f') )
+        if (b < 32 && b != '\t' && b != '\n' && b != '\r' && b != '\f')
         {
             /* Ignore EOT (4), SUB (26) and NUL (0) at the end of the file. */
@@ -280,11 +287,13 @@
                     || (   cbFile == 1
                         && *pbFile == '\0')))
-                return true;
+                return false;
+
             if (b == 0 && cbFile == 0)
-                return true;
-            return false;
-        }
-    }
-    return true;
+                return false;
+
+            return true;
+        }
+    }
+    return false;
 }
 
@@ -328,5 +337,5 @@
                             PCRTCRPEMSECTION *ppSectionHead, PRTERRINFO pErrInfo)
 {
-    AssertReturn(!fFlags, VERR_INVALID_FLAGS);
+    AssertReturn(!(fFlags & ~RTCRPEMREADFILE_F_CONTINUE_ON_ENCODING_ERROR), VERR_INVALID_FLAGS);
 
     size_t      cbContent;
@@ -362,11 +371,18 @@
                     /* Decode the section. */
                     /** @todo copy the preamble as well. */
-                    rc = rtCrPemDecodeBase64(pbContent + offBegin, offEnd - offBegin,
-                                             (void **)&pSection->pbData, &pSection->cbData);
-                    if (RT_FAILURE(rc))
+                    int rc2 = rtCrPemDecodeBase64(pbContent + offBegin, offEnd - offBegin,
+                                                  (void **)&pSection->pbData, &pSection->cbData);
+                    if (RT_FAILURE(rc2))
                     {
                         pSection->pbData = NULL;
                         pSection->cbData = 0;
-                        break;
+                        if (   rc2 == VERR_INVALID_BASE64_ENCODING
+                            && (fFlags & RTCRPEMREADFILE_F_CONTINUE_ON_ENCODING_ERROR))
+                            rc = -rc2;
+                        else
+                        {
+                            rc = rc2;
+                            break;
+                        }
                     }
 
