ab73271facd61b542070653b1c9b8623935a2783
[users/jgh/exim.git] / src / src / pdkim / pdkim.h
1 /* $Cambridge: exim/src/src/pdkim/pdkim.h,v 1.1.2.1 2009/02/24 13:13:47 tom Exp $ */
2 /* pdkim.h */
3
4 #include "sha1.h"
5 #include "sha2.h"
6 #include "rsa.h"
7 #include "base64.h"
8
9 #define PDKIM_SIGNATURE_VERSION "1"
10 #define PDKIM_MAX_BODY_LINE_LEN 1024
11 #define PDKIM_DEBUG
12 #define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\
13                              "Message-ID:To:Cc:MIME-Version:Content-Type:"\
14                              "Content-Transfer-Encoding:Content-ID:"\
15                              "Content-Description:Resent-Date:Resent-From:"\
16                              "Resent-Sender:Resent-To:Resent-Cc:"\
17                              "Resent-Message-ID:In-Reply-To:References:"\
18                              "List-Id:List-Help:List-Unsubscribe:"\
19                              "List-Subscribe:List-Post:List-Owner:List-Archive"
20
21
22 /* Success / Error codes */
23 #define PDKIM_OK              0
24 #define PDKIM_FAIL            1
25 #define PDKIM_ERR_OOM         100
26 #define PDKIM_ERR_RSA_PRIVKEY 101
27 #define PDKIM_ERR_RSA_SIGNING 102
28 #define PDKIM_ERR_LONG_LINE   103
29
30
31 #ifdef PDKIM_DEBUG
32 void pdkim_quoteprint(FILE *, char *, int, int);
33 #endif
34
35
36 typedef struct pdkim_stringlist {
37   char *value;
38   void *next;
39 } pdkim_stringlist;
40 pdkim_stringlist *pdkim_append_stringlist(pdkim_stringlist *, char *);
41
42
43 #define PDKIM_STR_ALLOC_FRAG 256
44 typedef struct pdkim_str {
45   char         *str;
46   unsigned int  len;
47   unsigned int  allocated;
48 } pdkim_str;
49 pdkim_str *pdkim_strnew (char *);
50 char      *pdkim_strcat (pdkim_str *, char *);
51 char      *pdkim_strncat(pdkim_str *, char *, int);
52 void       pdkim_strfree(pdkim_str *);
53
54 #define PDKIM_QUERYMETHOD_DNS_TXT 0
55 /* extern char *pdkim_querymethods[]; */
56
57 #define PDKIM_ALGO_RSA_SHA256 0
58 #define PDKIM_ALGO_RSA_SHA1   1
59 /* extern char *pdkim_algos[]; */
60
61 #define PDKIM_CANON_SIMPLE   0
62 #define PDKIM_CANON_RELAXED  1
63 /* extern char *pdkim_canons[]; */
64
65
66 /* -------------------------------------------------------------------------- */
67 /* Public key as (usually) fetched from DNS */
68 typedef struct pdkim_pubkey {
69   char *version;                  /* v=  */
70   char *granularity;              /* g=  */
71
72   int num_hash_algos;
73   int **hash_algos;               /* h=  */
74
75   int keytype;                    /* k=  */
76   int srvtype;                    /* s=  */
77
78   char *notes;                    /* n=  */
79   char *key;                      /* p=  */
80
81   int testing;                    /* t=y */
82   int no_subdomaining;            /* t=s */
83 } pdkim_pubkey;
84
85 /* -------------------------------------------------------------------------- */
86 /* Signature as it appears in a DKIM-Signature header */
87 typedef struct pdkim_signature {
88
89   /* Bits stored in a DKIM signature header */
90   int version;                    /* v=   */
91   int algo;                       /* a=   */
92   int canon_headers;              /* c=x/ */
93   int canon_body;                 /* c=/x */
94   int querymethod;                /* q=   */
95
96   char *sigdata;                  /* b=   */
97   char *bodyhash;                 /* bh=  */
98
99   char *selector;                 /* s=   */
100   char *domain;                   /* d=   */
101   char *identity;                 /* i=   */
102
103   unsigned long created;          /* t=   */
104   unsigned long expires;          /* x=   */
105   unsigned long bodylength;       /* l=   */
106
107   char *headernames;              /* h=   */
108   char *copiedheaders;            /* z=   */
109
110   /* Public key used to verify this signature.
111      (Verification only) */
112   pdkim_pubkey pubkey;
113
114   /* Private RSA key used to create this signature */
115   char *rsa_privkey;
116
117   /* Header field names to include in the signature,
118      colon separated. When NULL, the recommended defaults
119      from RFC 4871 are used. */
120   char *sign_headers;
121
122   /* Per-signature helper variables */
123   sha1_context sha1_body;
124   sha2_context sha2_body;
125   unsigned long signed_body_bytes;
126   pdkim_stringlist *headers;
127
128   /* Verification specific */
129   int verify_result;
130
131   /* Pointer to next signature in list.
132      (Always NULL for signing) */
133   void *next;
134
135 } pdkim_signature;
136
137
138 /* -------------------------------------------------------------------------- */
139 /* Context to keep state between all operations */
140
141 #define PDKIM_MODE_SIGN     0
142 #define PDKIM_MODE_VERIFY   1
143 #define PDKIM_INPUT_NORMAL  0
144 #define PDKIM_INPUT_SMTP    1
145
146 typedef struct pdkim_ctx {
147
148   /* PDKIM_MODE_VERIFY or PDKIM_MODE_SIGN */
149   int mode;
150
151   /* PDKIM_INPUT_SMTP or PDKIM_INPUT_NORMAL */
152   int input_mode;
153
154   /* One (signing) or several chained (verification) signatures */
155   pdkim_signature *sig;
156
157   /* Coder's little helpers */
158   pdkim_str *cur_header;
159   char       linebuf[PDKIM_MAX_BODY_LINE_LEN];
160   int        linebuf_offset;
161   int        seen_lf;
162   int        seen_eod;
163   int        past_headers;
164   int        num_buffered_crlf;
165
166 #ifdef PDKIM_DEBUG
167   /* A FILE pointer. When not NULL, debug output will be generated
168     and sent to this stream */
169   FILE *debug_stream;
170 #endif
171
172 } pdkim_ctx;
173
174
175 int   header_name_match       (char *, char *);
176 char *pdkim_relax_header      (char *, int);
177
178 int   pdkim_update_bodyhash   (pdkim_ctx *, char *, int);
179 int   pdkim_finish_bodyhash   (pdkim_ctx *);
180
181 int   pdkim_bodyline_complete (pdkim_ctx *);
182 int   pdkim_header_complete   (pdkim_ctx *);
183
184 int   pdkim_feed              (pdkim_ctx *, char *data, int len);
185 int   pdkim_feed_finish       (pdkim_ctx *);
186
187 pdkim_str
188      *pdkim_create_header     (pdkim_ctx *, int);
189
190 pdkim_ctx
191      *pdkim_init_sign         (char *, char *, char *);
192
193 int   pdkim_set_optional      (pdkim_ctx *, char *, char *,
194                                int, int,
195                                unsigned long, int,
196                                unsigned long,
197                                unsigned long);
198
199 void  pdkim_free_sig          (pdkim_signature *);
200 void  pdkim_free_ctx          (pdkim_ctx *);
201
202
203 #ifdef PDKIM_DEBUG
204 void  pdkim_set_debug_stream  (pdkim_ctx *, FILE *);
205 #endif