1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 #ifndef INCLUDE_git_cert_h__
8 #define INCLUDE_git_cert_h__
9 
10 #include "common.h"
11 
12 /**
13  * @file git2/cert.h
14  * @brief Git certificate objects
15  * @defgroup git_cert Certificate objects
16  * @ingroup Git
17  * @{
18  */
19 GIT_BEGIN_DECL
20 
21 /**
22  * Type of host certificate structure that is passed to the check callback
23  */
24 typedef enum git_cert_t {
25 	/**
26 	 * No information about the certificate is available. This may
27 	 * happen when using curl.
28 	 */
29 	GIT_CERT_NONE,
30 	/**
31 	 * The `data` argument to the callback will be a pointer to
32 	 * the DER-encoded data.
33 	 */
34 	GIT_CERT_X509,
35 	/**
36 	 * The `data` argument to the callback will be a pointer to a
37 	 * `git_cert_hostkey` structure.
38 	 */
39 	GIT_CERT_HOSTKEY_LIBSSH2,
40 	/**
41 	 * The `data` argument to the callback will be a pointer to a
42 	 * `git_strarray` with `name:content` strings containing
43 	 * information about the certificate. This is used when using
44 	 * curl.
45 	 */
46 	GIT_CERT_STRARRAY,
47 } git_cert_t;
48 
49 /**
50  * Parent type for `git_cert_hostkey` and `git_cert_x509`.
51  */
52 struct git_cert {
53 	/**
54 	 * Type of certificate. A `GIT_CERT_` value.
55 	 */
56 	git_cert_t cert_type;
57 };
58 
59 /**
60  * Callback for the user's custom certificate checks.
61  *
62  * @param cert The host certificate
63  * @param valid Whether the libgit2 checks (OpenSSL or WinHTTP) think
64  * this certificate is valid
65  * @param host Hostname of the host libgit2 connected to
66  * @param payload Payload provided by the caller
67  * @return 0 to proceed with the connection, < 0 to fail the connection
68  *         or > 0 to indicate that the callback refused to act and that
69  *         the existing validity determination should be honored
70  */
71 typedef int GIT_CALLBACK(git_transport_certificate_check_cb)(git_cert *cert, int valid, const char *host, void *payload);
72 
73 /**
74  * Type of SSH host fingerprint
75  */
76 typedef enum {
77 	/** MD5 is available */
78 	GIT_CERT_SSH_MD5 = (1 << 0),
79 	/** SHA-1 is available */
80 	GIT_CERT_SSH_SHA1 = (1 << 1),
81 	/** SHA-256 is available */
82 	GIT_CERT_SSH_SHA256 = (1 << 2),
83 	/** Raw hostkey is available */
84 	GIT_CERT_SSH_RAW = (1 << 3),
85 } git_cert_ssh_t;
86 
87 typedef enum {
88 	/** The raw key is of an unknown type. */
89 	GIT_CERT_SSH_RAW_TYPE_UNKNOWN = 0,
90 	/** The raw key is an RSA key. */
91 	GIT_CERT_SSH_RAW_TYPE_RSA = 1,
92 	/** The raw key is a DSS key. */
93 	GIT_CERT_SSH_RAW_TYPE_DSS = 2,
94 } git_cert_ssh_raw_type_t;
95 
96 /**
97  * Hostkey information taken from libssh2
98  */
99 typedef struct {
100 	git_cert parent; /**< The parent cert */
101 
102 	/**
103 	 * A bitmask containing the available fields.
104 	 */
105 	git_cert_ssh_t type;
106 
107 	/**
108 	 * Hostkey hash. If `type` has `GIT_CERT_SSH_MD5` set, this will
109 	 * have the MD5 hash of the hostkey.
110 	 */
111 	unsigned char hash_md5[16];
112 
113 	/**
114 	 * Hostkey hash. If `type` has `GIT_CERT_SSH_SHA1` set, this will
115 	 * have the SHA-1 hash of the hostkey.
116 	 */
117 	unsigned char hash_sha1[20];
118 
119 	/**
120 	 * Hostkey hash. If `type` has `GIT_CERT_SSH_SHA256` set, this will
121 	 * have the SHA-256 hash of the hostkey.
122 	 */
123 	unsigned char hash_sha256[32];
124 
125 	/**
126 	 * Raw hostkey type. If `type` has `GIT_CERT_SSH_RAW` set, this will
127 	 * have the type of the raw hostkey.
128 	 */
129 	git_cert_ssh_raw_type_t raw_type;
130 
131 	/**
132 	 * Pointer to the raw hostkey. If `type` has `GIT_CERT_SSH_RAW` set,
133 	 * this will have the raw contents of the hostkey.
134 	 */
135 	const char *hostkey;
136 
137 	/**
138 	 * Raw hostkey length. If `type` has `GIT_CERT_SSH_RAW` set, this will
139 	 * have the length of the raw contents of the hostkey.
140 	 */
141 	size_t hostkey_len;
142 } git_cert_hostkey;
143 
144 /**
145  * X.509 certificate information
146  */
147 typedef struct {
148 	git_cert parent; /**< The parent cert */
149 
150 	/**
151 	 * Pointer to the X.509 certificate data
152 	 */
153 	void *data;
154 
155 	/**
156 	 * Length of the memory block pointed to by `data`.
157 	 */
158 	size_t len;
159 } git_cert_x509;
160 
161 /** @} */
162 GIT_END_DECL
163 #endif
164