ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
regex.c File Reference

PCRE2-based regex patterns for cryptographic parsing. More...

Go to the source code of this file.

Functions

bool crypto_regex_match_known_hosts (const char *line, char **ip_port_out, char **key_type_out, char **hex_key_out, char **comment_out)
bool crypto_regex_match_public_key (const char *line, char **base64_key_out, char **comment_out)
bool crypto_regex_extract_pem_base64 (const char *file_content, char **base64_data_out)
bool crypto_regex_extract_gpg_keygrip (const char *line, char **keygrip_out)

Detailed Description

PCRE2-based regex patterns for cryptographic parsing.

Implements regex-based parsing for SSH formats using PCRE2. Compiles each pattern for the duration of a match.

Definition in file regex.c.

Function Documentation

◆ crypto_regex_extract_gpg_keygrip()

bool crypto_regex_extract_gpg_keygrip ( const char * line,
char ** keygrip_out )

Extract GPG keygrip from colon-delimited GPG output

Format: grp:::::::::D52FF935FBA59609EE65E1685287828242A1EA1A: Where the keygrip is a 40-character hexadecimal string after 8 colon-delimited empty fields

Parameters
lineInput line from GPG output (must start with "grp:")
keygrip_outOutput: allocated string with 40-char hex keygrip
Returns
true if line matches GPG keygrip format, false otherwise
Note
Caller must free output string with SAFE_FREE()
Returns false if regex is not available (caller should use fallback manual parsing)

Definition at line 245 of file regex.c.

245 {
246 if (!line || !keygrip_out) {
247 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
248 return false;
249 }
250
251 pcre2_code *regex = crypto_regex_get_gpg_keygrip();
252 if (!regex) {
253 /* Regex not available - caller should use fallback manual parsing */
254 return false;
255 }
256
257 defer(crypto_regex_free_code(regex));
258 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
259 if (!match_data) {
260 return false;
261 }
262
263 defer(crypto_regex_free_match_data(match_data));
264 int rc = crypto_regex_match(regex, (PCRE2_SPTR)line, strlen(line), match_data);
265
266 if (rc < 0) {
267 return false;
268 }
269
270 /* Extract keygrip from named group */
271 *keygrip_out = asciichat_pcre2_extract_named_group(regex, match_data, "keygrip", line);
272
273 return *keygrip_out != NULL;
274}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM
char * asciichat_pcre2_extract_named_group(pcre2_code *regex, pcre2_match_data *match_data, const char *group_name, const char *subject)
Extract named substring from PCRE2 match data.
Definition pcre2.c:34

References asciichat_pcre2_extract_named_group(), ERROR_INVALID_PARAM, and SET_ERRNO.

Referenced by gpg_get_public_key().

◆ crypto_regex_extract_pem_base64()

bool crypto_regex_extract_pem_base64 ( const char * file_content,
char ** base64_data_out )

Extract base64 data from OpenSSH private key PEM format

Format: --—BEGIN OPENSSH PRIVATE KEY--— <multiline base64 data> --—END OPENSSH PRIVATE KEY--—

Parameters
file_contentFull contents of PEM key file
base64_data_outOutput: allocated string with base64 data (includes whitespace)
Returns
true if PEM structure found and extracted, false otherwise
Note
Caller must free output string with SAFE_FREE()
Output includes newlines and whitespace (caller should remove as needed)

Definition at line 214 of file regex.c.

214 {
215 if (!file_content || !base64_data_out) {
216 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
217 return false;
218 }
219
220 pcre2_code *regex = crypto_regex_get_openssh_pem();
221 if (!regex) {
222 SET_ERRNO(ERROR_INVALID_STATE, "Invalid validator state");
223 return false;
224 }
225
226 defer(crypto_regex_free_code(regex));
227 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
228 if (!match_data) {
229 return false;
230 }
231
232 defer(crypto_regex_free_match_data(match_data));
233 int rc = crypto_regex_match(regex, (PCRE2_SPTR)file_content, strlen(file_content), match_data);
234
235 if (rc < 0) {
236 return false;
237 }
238
239 /* Extract base64 data */
240 *base64_data_out = asciichat_pcre2_extract_named_group(regex, match_data, "base64_data", file_content);
241
242 return *base64_data_out != NULL;
243}
@ ERROR_INVALID_STATE

References asciichat_pcre2_extract_named_group(), ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.

◆ crypto_regex_match_known_hosts()

bool crypto_regex_match_known_hosts ( const char * line,
char ** ip_port_out,
char ** key_type_out,
char ** hex_key_out,
char ** comment_out )

Match SSH known_hosts line and extract components

Format: <IP:port> <keytype> <hex_key> [comment] Example: 192.0.2.1:8080 x25519 0123456789abcdef... ascii-chat

Parameters
lineInput line from known_hosts file
ip_port_outOutput: allocated string with IP:port (e.g., "192.0.2.1:8080")
key_type_outOutput: allocated string with key type (e.g., "x25519", "no-identity")
hex_key_outOutput: allocated string with 64-char hex key (may be NULL for no-identity)
comment_outOutput: allocated string with comment (may be NULL)
Returns
true if line matches known_hosts format, false otherwise
Note
Caller must free all non-NULL output strings with SAFE_FREE()
Returns false if any output pointer is NULL

Definition at line 132 of file regex.c.

132 {
133 if (!line || !ip_port_out || !key_type_out || !hex_key_out || !comment_out) {
134 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
135 return false;
136 }
137
138 pcre2_code *regex = crypto_regex_get_known_hosts();
139 if (!regex) {
140 SET_ERRNO(ERROR_INVALID_STATE, "Invalid validator state");
141 return false;
142 }
143
144 defer(crypto_regex_free_code(regex));
145 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
146 if (!match_data) {
147 return false;
148 }
149
150 defer(crypto_regex_free_match_data(match_data));
151 int rc = crypto_regex_match(regex, (PCRE2_SPTR)line, strlen(line), match_data);
152
153 if (rc < 0) {
154 return false;
155 }
156
157 /* Extract named groups */
158 *ip_port_out = asciichat_pcre2_extract_named_group(regex, match_data, "ip_port", line);
159 *key_type_out = asciichat_pcre2_extract_named_group(regex, match_data, "key_type", line);
160 *hex_key_out = asciichat_pcre2_extract_named_group(regex, match_data, "hex_key", line);
161 *comment_out = asciichat_pcre2_extract_named_group(regex, match_data, "comment", line);
162
163 /* Verify we got required fields */
164 if (!*ip_port_out || !*key_type_out) {
165 SAFE_FREE(*ip_port_out);
166 SAFE_FREE(*key_type_out);
167 SAFE_FREE(*hex_key_out);
168 SAFE_FREE(*comment_out);
169 return false;
170 }
171
172 return true;
173}
#define SAFE_FREE(ptr)

References asciichat_pcre2_extract_named_group(), ERROR_INVALID_PARAM, ERROR_INVALID_STATE, SAFE_FREE, and SET_ERRNO.

Referenced by check_known_host_no_identity(), and check_known_host_with_key().

◆ crypto_regex_match_public_key()

bool crypto_regex_match_public_key ( const char * line,
char ** base64_key_out,
char ** comment_out )

Match SSH Ed25519 public key line and extract base64

Format: ssh-ed25519 <base64_key> [comment] Example: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... test-key

Parameters
lineInput line from SSH public key file
base64_key_outOutput: allocated string with base64-encoded key
comment_outOutput: allocated string with comment (may be NULL)
Returns
true if line matches SSH Ed25519 format, false otherwise
Note
Caller must free output strings with SAFE_FREE()
Case-insensitive: matches "SSH-ED25519" or "ssh-ed25519"

Definition at line 175 of file regex.c.

175 {
176 if (!line || !base64_key_out || !comment_out) {
177 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
178 return false;
179 }
180
181 pcre2_code *regex = crypto_regex_get_ssh_public_key();
182 if (!regex) {
183 SET_ERRNO(ERROR_INVALID_STATE, "Invalid validator state");
184 return false;
185 }
186
187 defer(crypto_regex_free_code(regex));
188 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
189 if (!match_data) {
190 return false;
191 }
192
193 defer(crypto_regex_free_match_data(match_data));
194 int rc = crypto_regex_match(regex, (PCRE2_SPTR)line, strlen(line), match_data);
195
196 if (rc < 0) {
197 return false;
198 }
199
200 /* Extract named groups */
201 *base64_key_out = asciichat_pcre2_extract_named_group(regex, match_data, "base64_key", line);
202 *comment_out = asciichat_pcre2_extract_named_group(regex, match_data, "comment", line);
203
204 /* Verify we got required field */
205 if (!*base64_key_out) {
206 SAFE_FREE(*base64_key_out);
207 SAFE_FREE(*comment_out);
208 return false;
209 }
210
211 return true;
212}

References asciichat_pcre2_extract_named_group(), ERROR_INVALID_PARAM, ERROR_INVALID_STATE, SAFE_FREE, and SET_ERRNO.

Referenced by parse_ssh_ed25519_line().