1+ #include "clar.h"
12#include "clar_libgit2.h"
23
34#include "path.h"
67#include "remote.h"
78#include "futils.h"
89#include "refs.h"
10+ #include "str.h"
11+ #include "streams/openssl.h"
12+
13+ #ifdef GIT_OPENSSL
14+ # include <openssl/ssl.h>
15+ # include <openssl/err.h>
16+ # include <openssl/x509v3.h>
17+ #endif
918
1019/*
11- * Certificate one is in the `certs` folder; certificate two is in the
12- * `self-signed.pem` file.
20+ * Certificates for https://test.libgit2.org/ are in the `certs` folder.
1321 */
22+ #define CUSTOM_CERT_DIR "certs"
23+
1424#define CUSTOM_CERT_ONE_URL "https://test.libgit2.org:1443/anonymous/test.git"
15- #define CUSTOM_CERT_ONE_PATH "certs "
25+ #define CUSTOM_CERT_ONE_PATH "one "
1626
1727#define CUSTOM_CERT_TWO_URL "https://test.libgit2.org:2443/anonymous/test.git"
18- #define CUSTOM_CERT_TWO_FILE "self-signed.pem"
28+ #define CUSTOM_CERT_TWO_FILE "two.pem"
29+
30+ #define CUSTOM_CERT_THREE_URL "https://test.libgit2.org:3443/anonymous/test.git"
31+ #define CUSTOM_CERT_THREE_FILE "three.pem.raw"
1932
2033#if (GIT_OPENSSL || GIT_MBEDTLS )
2134static git_repository * g_repo ;
22- static int initialized = false;
2335#endif
2436
2537void test_online_customcert__initialize (void )
2638{
2739#if (GIT_OPENSSL || GIT_MBEDTLS )
28- g_repo = NULL ;
40+ git_str path = GIT_STR_INIT , file = GIT_STR_INIT ;
41+ char cwd [GIT_PATH_MAX ];
2942
30- if (!initialized ) {
31- git_str path = GIT_STR_INIT , file = GIT_STR_INIT ;
32- char cwd [GIT_PATH_MAX ];
43+ g_repo = NULL ;
3344
34- cl_fixture_sandbox (CUSTOM_CERT_ONE_PATH );
35- cl_fixture_sandbox (CUSTOM_CERT_TWO_FILE );
45+ cl_fixture_sandbox (CUSTOM_CERT_DIR );
3646
37- cl_must_pass (p_getcwd (cwd , GIT_PATH_MAX ));
38- cl_git_pass (git_str_joinpath (& path , cwd , CUSTOM_CERT_ONE_PATH ));
39- cl_git_pass (git_str_joinpath (& file , cwd , CUSTOM_CERT_TWO_FILE ));
47+ cl_must_pass (p_getcwd (cwd , GIT_PATH_MAX ));
48+ cl_git_pass (git_str_join_n (& path , '/' , 3 , cwd , CUSTOM_CERT_DIR , CUSTOM_CERT_ONE_PATH ));
49+ cl_git_pass (git_str_join_n (& file , '/' , 3 , cwd , CUSTOM_CERT_DIR , CUSTOM_CERT_TWO_FILE ));
4050
41- cl_git_pass (git_libgit2_opts (GIT_OPT_SET_SSL_CERT_LOCATIONS ,
42- file .ptr , path .ptr ));
43- initialized = true;
51+ cl_git_pass (git_libgit2_opts (GIT_OPT_SET_SSL_CERT_LOCATIONS ,
52+ file .ptr , path .ptr ));
4453
45- git_str_dispose (& file );
46- git_str_dispose (& path );
47- }
54+ git_str_dispose (& file );
55+ git_str_dispose (& path );
4856#endif
4957}
5058
@@ -57,8 +65,11 @@ void test_online_customcert__cleanup(void)
5765 }
5866
5967 cl_fixture_cleanup ("./cloned" );
60- cl_fixture_cleanup (CUSTOM_CERT_ONE_PATH );
61- cl_fixture_cleanup (CUSTOM_CERT_TWO_FILE );
68+ cl_fixture_cleanup (CUSTOM_CERT_DIR );
69+ #endif
70+
71+ #ifdef GIT_OPENSSL
72+ git_openssl__reset_context ();
6273#endif
6374}
6475
@@ -77,3 +88,34 @@ void test_online_customcert__path(void)
7788 cl_assert (git_fs_path_exists ("./cloned/master.txt" ));
7889#endif
7990}
91+
92+ void test_online_customcert__raw_x509 (void )
93+ {
94+ #ifdef GIT_OPENSSL
95+ X509 * x509_cert = NULL ;
96+ char cwd [GIT_PATH_MAX ];
97+ git_str raw_file = GIT_STR_INIT ,
98+ raw_file_data = GIT_STR_INIT ,
99+ raw_cert = GIT_STR_INIT ;
100+ const unsigned char * raw_cert_bytes = NULL ;
101+
102+ cl_must_pass (p_getcwd (cwd , GIT_PATH_MAX ));
103+
104+ cl_git_pass (git_str_join_n (& raw_file , '/' , 3 , cwd , CUSTOM_CERT_DIR , CUSTOM_CERT_THREE_FILE ));
105+
106+ cl_git_pass (git_futils_readbuffer (& raw_file_data , git_str_cstr (& raw_file )));
107+ cl_git_pass (git_str_decode_base64 (& raw_cert , git_str_cstr (& raw_file_data ), git_str_len (& raw_file_data )));
108+
109+ raw_cert_bytes = (const unsigned char * )git_str_cstr (& raw_cert );
110+ x509_cert = d2i_X509 (NULL , & raw_cert_bytes , git_str_len (& raw_cert ));
111+ cl_git_pass (git_libgit2_opts (GIT_OPT_ADD_SSL_X509_CERT , x509_cert ));
112+ X509_free (x509_cert );
113+
114+ cl_git_pass (git_clone (& g_repo , CUSTOM_CERT_THREE_URL , "./cloned" , NULL ));
115+ cl_assert (git_fs_path_exists ("./cloned/master.txt" ));
116+
117+ git_str_dispose (& raw_cert );
118+ git_str_dispose (& raw_file_data );
119+ git_str_dispose (& raw_file );
120+ #endif
121+ }
0 commit comments