Merge commit 'dkg/master'
[monkeysphere.git] / src / keytrans / gnutls-helpers.h
1 /* Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net> */
2 /* Date: Fri, 04 Apr 2008 19:31:16 -0400 */
3 /* License: GPL v3 or later */
4
5
6 #include <gnutls/gnutls.h>
7 #include <gnutls/openpgp.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <stdarg.h>
16
17 /* Functions to help dealing with GnuTLS for monkeysphere key
18    translation projects: */
19
20 /* set everything up, including logging levels.  Return 0 on
21    success */
22 int init_gnutls();
23
24 /* logging and output functions: */
25
26 void err(int level, const char* fmt, ...);
27 void logfunc(int level, const char* string);
28
29 /* basic datum manipulations: */
30
31 void init_datum(gnutls_datum_t* d);
32 void copy_datum(gnutls_datum_t* dest, const gnutls_datum_t* src);
33 int compare_data(const gnutls_datum_t* a, const gnutls_datum_t* b);
34 void free_datum(gnutls_datum_t* d);
35 int write_datum_fd(int fd, const gnutls_datum_t* d);
36 int write_datum_fd_with_length(int fd, const gnutls_datum_t* d);
37 int write_data_fd_with_length(int fd, const gnutls_datum_t** d, unsigned int num);
38
39 /* set up a datum from a null-terminated string */
40 int datum_from_string(gnutls_datum_t* d, const char* str);
41
42 /* keyid manipulations: */
43 typedef unsigned char printable_keyid[16];
44
45 void init_keyid(gnutls_openpgp_keyid_t keyid);
46 void make_keyid_printable(printable_keyid out, gnutls_openpgp_keyid_t keyid);
47 void collapse_printable_keyid(gnutls_openpgp_keyid_t out, printable_keyid in);
48 int convert_string_to_keyid(gnutls_openpgp_keyid_t out, const char* str);
49 int convert_string_to_printable_keyid(printable_keyid out, const char* str);
50
51 /* you must have twice as many bytes in the out buffer as in the in buffer */
52 void hex_print_data(char* out, const unsigned char* in, size_t incount);
53
54 /* expects a null-terminated string as in, containing an even number
55    of hexadecimal characters.
56
57    returns length in *bits* of raw data as output. 
58
59    the out buffer must be at least half as long as in to hold the
60    output.  if out is NULL, no output will be generated, but the
61    length will still be returned.
62 */
63 unsigned int hexstring2bin(unsigned char* out, const char* in);
64
65 /* functions to get data into datum objects: */
66
67 /* read the passed-in string, store in a single datum */
68 int set_datum_string(gnutls_datum_t* d, const char* s);
69
70 /* read the passed-in file descriptor until EOF, store in a single
71    datum */
72 int set_datum_fd(gnutls_datum_t* d, int fd);
73
74 /* read the file indicated (by name) in the fname parameter.  store
75    its entire contents in a single datum. */
76 int set_datum_file(gnutls_datum_t* d, const char* fname);
77
78 /* set up file descriptor pipe for writing (child process pid gets
79    stored in pid, fd is returned)*/
80 int create_writing_pipe(pid_t* pid, const char* path, char* const argv[]);
81
82 /* return 0 if userid matches the monkeysphere spec for ssh host user IDs */
83 int validate_ssh_host_userid(const char* userid);
84
85 /* how many bytes will it take to write out this datum in OpenPGP MPI form? */
86 size_t get_openpgp_mpi_size(gnutls_datum_t* d);
87
88 /* write the MPI stored in gnutls_datum_t to file descriptor fd: */
89 int write_openpgp_mpi_to_fd(int fd, gnutls_datum_t* d);