ascii-chat 0.11.33
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 262 of file regex.c.

262 {
263 if (!line || !keygrip_out) {
264 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
265 return false;
266 }
267
268 pcre2_code *regex = crypto_regex_get_gpg_keygrip();
269 if (!regex) {
270 /* Regex not available - caller should use fallback manual parsing */
271 return false;
272 }
273
274 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
275 if (!match_data) {
276 return false;
277 }
278
279 /* Perform JIT match (falls back to interpreted if JIT unavailable) */
280 int rc = pcre2_jit_match(regex, (PCRE2_SPTR)line, strlen(line), 0, /* startoffset */
281 0, /* options */
282 match_data, NULL); /* mcontext */
283
284 if (rc < 0) {
285 pcre2_match_data_free(match_data);
286 return false;
287 }
288
289 /* Extract keygrip from named group */
290 *keygrip_out = asciichat_pcre2_extract_named_group(regex, match_data, "keygrip", line);
291
292 pcre2_match_data_free(match_data);
293
294 return *keygrip_out != NULL;
295}
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error 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:269

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

227 {
228 if (!file_content || !base64_data_out) {
229 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
230 return false;
231 }
232
233 pcre2_code *regex = crypto_regex_get_openssh_pem();
234 if (!regex) {
235 SET_ERRNO(ERROR_INVALID_STATE, "Invalid validator state");
236 return false;
237 }
238
239 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
240 if (!match_data) {
241 return false;
242 }
243
244 /* Perform JIT match (falls back to interpreted if JIT unavailable) */
245 int rc = pcre2_jit_match(regex, (PCRE2_SPTR)file_content, strlen(file_content), 0, /* startoffset */
246 0, /* options */
247 match_data, NULL); /* mcontext */
248
249 if (rc < 0) {
250 pcre2_match_data_free(match_data);
251 return false;
252 }
253
254 /* Extract base64 data */
255 *base64_data_out = asciichat_pcre2_extract_named_group(regex, match_data, "base64_data", file_content);
256
257 pcre2_match_data_free(match_data);
258
259 return *base64_data_out != NULL;
260}
@ ERROR_INVALID_STATE

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

Referenced by parse_ssh_private_key().

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

137 {
138 if (!line || !ip_port_out || !key_type_out || !hex_key_out || !comment_out) {
139 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
140 return false;
141 }
142
143 pcre2_code *regex = crypto_regex_get_known_hosts();
144 if (!regex) {
145 SET_ERRNO(ERROR_INVALID_STATE, "Invalid validator state");
146 return false;
147 }
148
149 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
150 if (!match_data) {
151 return false;
152 }
153
154 /* Perform JIT match (falls back to interpreted if JIT unavailable) */
155 int rc = pcre2_jit_match(regex, (PCRE2_SPTR)line, strlen(line), 0, /* startoffset */
156 0, /* options */
157 match_data, NULL); /* mcontext */
158
159 if (rc < 0) {
160 pcre2_match_data_free(match_data);
161 return false;
162 }
163
164 /* Extract named groups */
165 *ip_port_out = asciichat_pcre2_extract_named_group(regex, match_data, "ip_port", line);
166 *key_type_out = asciichat_pcre2_extract_named_group(regex, match_data, "key_type", line);
167 *hex_key_out = asciichat_pcre2_extract_named_group(regex, match_data, "hex_key", line);
168 *comment_out = asciichat_pcre2_extract_named_group(regex, match_data, "comment", line);
169
170 pcre2_match_data_free(match_data);
171
172 /* Verify we got required fields */
173 if (!*ip_port_out || !*key_type_out) {
174 SAFE_FREE(*ip_port_out);
175 SAFE_FREE(*key_type_out);
176 SAFE_FREE(*hex_key_out);
177 SAFE_FREE(*comment_out);
178 return false;
179 }
180
181 return true;
182}
#define SAFE_FREE(ptr)
Definition common.h:376

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

Referenced by check_known_host(), and check_known_host_no_identity().

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

184 {
185 if (!line || !base64_key_out || !comment_out) {
186 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
187 return false;
188 }
189
190 pcre2_code *regex = crypto_regex_get_ssh_public_key();
191 if (!regex) {
192 SET_ERRNO(ERROR_INVALID_STATE, "Invalid validator state");
193 return false;
194 }
195
196 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
197 if (!match_data) {
198 return false;
199 }
200
201 /* Perform JIT match (falls back to interpreted if JIT unavailable) */
202 int rc = pcre2_jit_match(regex, (PCRE2_SPTR)line, strlen(line), 0, /* startoffset */
203 0, /* options */
204 match_data, NULL); /* mcontext */
205
206 if (rc < 0) {
207 pcre2_match_data_free(match_data);
208 return false;
209 }
210
211 /* Extract named groups */
212 *base64_key_out = asciichat_pcre2_extract_named_group(regex, match_data, "base64_key", line);
213 *comment_out = asciichat_pcre2_extract_named_group(regex, match_data, "comment", line);
214
215 pcre2_match_data_free(match_data);
216
217 /* Verify we got required field */
218 if (!*base64_key_out) {
219 SAFE_FREE(*base64_key_out);
220 SAFE_FREE(*comment_out);
221 return false;
222 }
223
224 return true;
225}

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

Referenced by parse_ssh_ed25519_line().