@@ -565,6 +565,56 @@ public void testCertManagerLoadCA() throws Exception {
565565 }
566566 }
567567
568+ /* The buffer load/verify calls must parse exactly sz bytes from the start
569+ * of the array. A sz beyond the array is rejected instead of being
570+ * ignored and the whole backing array parsed. */
571+ @ Test
572+ public void testCertManagerBufferHonorsSz () throws Exception {
573+
574+ WolfSSLCertManager cm = null ;
575+
576+ try {
577+ byte [] caCertPem =
578+ Files .readAllBytes (new File (ocspRootCaCert ).toPath ());
579+ byte [] peerPem = Files .readAllBytes (new File (serverCert ).toPath ());
580+
581+ cm = new WolfSSLCertManager ();
582+
583+ /* Exact length is the normal, supported call. */
584+ assertEquals ("exact-length load should succeed" ,
585+ WolfSSL .SSL_SUCCESS ,
586+ cm .CertManagerLoadCABuffer (caCertPem , caCertPem .length ,
587+ WolfSSL .SSL_FILETYPE_PEM ));
588+
589+ assertEquals ("load sz past end of array must be rejected" ,
590+ WolfSSL .BAD_FUNC_ARG ,
591+ cm .CertManagerLoadCABuffer (caCertPem , caCertPem .length + 1 ,
592+ WolfSSL .SSL_FILETYPE_PEM ));
593+
594+ assertEquals ("verify sz past end of array must be rejected" ,
595+ WolfSSL .BAD_FUNC_ARG ,
596+ cm .CertManagerVerifyBuffer (peerPem , peerPem .length + 1 ,
597+ WolfSSL .SSL_FILETYPE_PEM ));
598+
599+ /* A shorter sz must parse only the first sz bytes, not the whole
600+ * array. Half the PEM is malformed, so the load must fail. The
601+ * old code ignored sz and parsed the full array, succeeding. */
602+ assertTrue ("a shorter sz must not parse the whole array" ,
603+ cm .CertManagerLoadCABuffer (caCertPem , caCertPem .length / 2 ,
604+ WolfSSL .SSL_FILETYPE_PEM ) != WolfSSL .SSL_SUCCESS );
605+
606+ /* sz of zero parses nothing and must not succeed. */
607+ assertTrue ("sz of zero must not succeed" ,
608+ cm .CertManagerLoadCABuffer (caCertPem , 0 ,
609+ WolfSSL .SSL_FILETYPE_PEM ) != WolfSSL .SSL_SUCCESS );
610+
611+ } finally {
612+ if (cm != null ) {
613+ cm .free ();
614+ }
615+ }
616+ }
617+
568618 @ Test
569619 public void testCertManagerCheckOCSPResponse ()
570620 throws Exception {
0 commit comments