ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
regex.h 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.

Unified module for regex-based parsing of SSH-related formats:

  • SSH known_hosts file entries
  • SSH Ed25519 public key lines
  • OpenSSH private key PEM format

Uses PCRE2

Definition in file regex.h.

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 247 of file regex.c.

247 {
248 if (!line || !keygrip_out) {
249 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
250 return false;
251 }
252
253 pcre2_code *regex = crypto_regex_get_gpg_keygrip();
254 if (!regex) {
255 /* Regex not available - caller should use fallback manual parsing */
256 return false;
257 }
258
259 defer(crypto_regex_free_code(regex));
260 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
261 if (!match_data) {
262 return false;
263 }
264
265 defer(crypto_regex_free_match_data(match_data));
266 int rc = crypto_regex_match(regex, (PCRE2_SPTR)line, strlen(line), match_data);
267
268 if (rc < 0) {
269 return false;
270 }
271
272 /* Extract keygrip from named group */
273 *keygrip_out = asciichat_pcre2_extract_named_group(regex, match_data, "keygrip", line);
274
275 return *keygrip_out != NULL;
276}
#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 216 of file regex.c.

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

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

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 177 of file regex.c.

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

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

Referenced by parse_ssh_ed25519_line().