1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Generic SSL / SMIME commands.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
9 * Gunnar Ritter. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Gunnar Ritter
22 * and his contributors.
23 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 typedef int avoid_empty_file_compiler_warning
;
48 ssl_set_vrfy_level(const char *uhp
)
53 ssl_vrfy_level
= VRFY_ASK
;
55 vrvar
= ac_alloc(l
+ 12);
56 memcpy(vrvar
, "ssl-verify-", 11);
57 memcpy(vrvar
+ 11, uhp
, l
+ 1);
59 if ((cp
= value(vrvar
)) == NULL
)
60 cp
= value("ssl-verify");
63 if (strcmp(cp
, "strict") == 0)
64 ssl_vrfy_level
= VRFY_STRICT
;
65 else if (strcmp(cp
, "ask") == 0)
66 ssl_vrfy_level
= VRFY_ASK
;
67 else if (strcmp(cp
, "warn") == 0)
68 ssl_vrfy_level
= VRFY_WARN
;
69 else if (strcmp(cp
, "ignore") == 0)
70 ssl_vrfy_level
= VRFY_IGNORE
;
72 fprintf(stderr
, tr(265,
73 "invalid value of ssl-verify: %s\n"), cp
);
82 switch (ssl_vrfy_level
) {
91 fprintf(stderr
, tr(264, "Continue (y/n)? "));
92 if (readline_restart(stdin
, &line
, &linesize
, 0) > 0 &&
109 ssl_method_string(const char *uhp
)
115 mtvar
= ac_alloc(l
+ 12);
116 memcpy(mtvar
, "ssl-method-", 11);
117 memcpy(mtvar
+ 11, uhp
, l
+ 1);
118 if ((cp
= value(mtvar
)) == NULL
)
119 cp
= value("ssl-method");
125 smime_split(FILE *ip
, FILE **hp
, FILE **bp
, long xcount
, int keep
)
127 /* TODO like i said quite often now, the entire SSL stuff really needs
128 * TODO a review ASAP; also in respect to correct resource release, and
129 * TODO especially in case of errors; S-nail is a program that REALLY
130 * TODO may run for a week or longer!!! */
132 char *savedfields
= NULL
;
133 size_t bufsize
, buflen
, cnt
, savedsize
= 0;
136 if ((*hp
= Ftemp(&hn
, "Rh", "w+", 0600, 1)) == NULL
)
140 if ((*bp
= Ftemp(&bn
, "Rb", "w+", 0600, 1)) == NULL
) {
148 buf
= smalloc(bufsize
= LINESIZE
);
149 savedfields
= smalloc(savedsize
= 1);
156 while (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, ip
, 0) != NULL
&&
158 if (ascncasecmp(buf
, "content-", 8) == 0) {
160 fputs("X-Encoded-", *hp
);
163 savedfields
= srealloc(savedfields
, savedsize
);
164 memcpy(savedfields
+ strlen(savedfields
),
167 fwrite(buf
, sizeof *buf
, buflen
, *hp
);
172 fgetline(&buf
, &bufsize
, &cnt
, &buflen
,
177 fwrite(buf
, sizeof *buf
, buflen
, *hp
);
182 fputs(savedfields
, *bp
);
184 while (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, ip
, 0) != NULL
)
185 fwrite(buf
, sizeof *buf
, buflen
, *bp
);
195 smime_sign_assemble(FILE *hp
, FILE *bp
, FILE *sp
)
201 if ((op
= Ftemp(&cp
, "Rs", "w+", 0600, 1)) == NULL
) {
207 boundary
= mime_create_boundary();
208 while ((c
= getc(hp
)) != EOF
) {
209 if (c
== '\n' && lastc
== '\n')
214 fprintf(op
, "Content-Type: multipart/signed;\n"
215 " protocol=\"application/x-pkcs7-signature\"; micalg=sha1;\n"
216 " boundary=\"%s\"\n\n", boundary
);
217 fprintf(op
, "This is an S/MIME signed message.\n\n--%s\n",
219 while ((c
= getc(bp
)) != EOF
)
221 fprintf(op
, "\n--%s\n", boundary
);
222 fputs("Content-Type: application/x-pkcs7-signature; "
223 "name=\"smime.p7s\"\n"
224 "Content-Transfer-Encoding: base64\n"
225 "Content-Disposition: attachment; filename=\"smime.p7s\"\n\n",
227 while ((c
= getc(sp
)) != EOF
) {
229 while ((c
= getc(sp
)) != EOF
&& c
!= '\n');
234 fprintf(op
, "\n--%s--\n", boundary
);
240 perror("signed output data");
249 smime_encrypt_assemble(FILE *hp
, FILE *yp
)
255 if ((op
= Ftemp(&cp
, "Rs", "w+", 0600, 1)) == NULL
) {
261 while ((c
= getc(hp
)) != EOF
) {
262 if (c
== '\n' && lastc
== '\n')
267 fprintf(op
, "Content-Type: application/x-pkcs7-mime; "
268 "name=\"smime.p7m\"\n"
269 "Content-Transfer-Encoding: base64\n"
270 "Content-Disposition: attachment; "
271 "filename=\"smime.p7m\"\n\n");
272 while ((c
= getc(yp
)) != EOF
) {
274 while ((c
= getc(yp
)) != EOF
&& c
!= '\n');
283 perror("encrypted output data");
292 smime_decrypt_assemble(struct message
*m
, FILE *hp
, FILE *bp
)
297 size_t bufsize
= 0, buflen
, cnt
;
298 long lines
= 0, octets
= 0;
302 x
= salloc(sizeof *x
);
305 fseek(mb
.mb_otf
, 0L, SEEK_END
);
306 offset
= ftell(mb
.mb_otf
);
308 while (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, hp
, 0) != NULL
) {
312 if ((cp
= thisfield(buf
, "content-transfer-encoding")) != NULL
)
313 if (ascncasecmp(cp
, "binary", 7) == 0)
315 fwrite(buf
, sizeof *buf
, buflen
, mb
.mb_otf
);
319 octets
+= mkdate(mb
.mb_otf
, "X-Decoding-Date");
322 while (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, bp
, 0) != NULL
) {
324 if (!binary
&& buf
[buflen
-1] == '\n' && buf
[buflen
-2] == '\r')
325 buf
[--buflen
-1] = '\n';
326 fwrite(buf
, sizeof *buf
, buflen
, mb
.mb_otf
);
330 else if (buf
[buflen
-1] == '\n')
335 while (!binary
&& lastnl
< 2) {
336 putc('\n', mb
.mb_otf
);
345 if (ferror(mb
.mb_otf
)) {
346 perror("decrypted output data");
349 x
->m_size
= x
->m_xsize
= octets
;
350 x
->m_lines
= x
->m_xlines
= lines
;
351 x
->m_block
= mailx_blockof(offset
);
352 x
->m_offset
= mailx_offsetof(offset
);
361 char *file
= NULL
, *str
= v
;
366 msgvec
= salloc((msgCount
+ 2) * sizeof *msgvec
);
367 if ((file
= laststring(str
, &f
, 1)) == NULL
) {
368 fprintf(stderr
, "No file to save certificate given.\n");
372 *msgvec
= first(0, MMNORM
);
377 "No messages to get certificates from.\n");
381 } else if (getmsglist(str
, msgvec
, 0) < 0)
386 fprintf(stderr
, "No applicable messages.\n");
389 if ((fp
= Fopen(file
, "a")) == NULL
) {
393 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++)
394 if (smime_certsave(&message
[*ip
-1], *ip
, fp
) != OKAY
)
398 printf("Certificate(s) saved.\n");
403 rfc2595_hostname_match(const char *host
, const char *pattern
)
405 if (pattern
[0] == '*' && pattern
[1] == '.') {
407 while (*host
&& *host
!= '.')
410 return asccasecmp(host
, pattern
) == 0 ? OKAY
: STOP
;
412 #endif /* HAVE_SSL */