2 * Copyright notice from original mutt:
3 * Copyright (C) 2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
25 #include "mutt_socket.h"
26 #include "mutt_curses.h"
31 static int MuttNssInitialized = 0;
33 /* internal data struct we use with the CONNECTION. this is where NSS-specific
34 * data gets stuffed so that the main mutt_socket.h doesn't have to be
42 /* nss callback to grab the user's password. */
43 static char *mutt_nss_password_func (PK11SlotInfo * slot, PRBool retry,
49 static int mutt_nss_error (const char *call)
51 mutt_error (_("%s failed (error %d)"), call, PR_GetError ());
55 /* initialize the NSS library for use. must be called prior to any other
56 * functions in this module.
58 static int mutt_nss_init (void)
60 if (!MuttNssInitialized) {
61 PK11_SetPasswordFunc (mutt_nss_password_func);
62 if (NSS_Init (SslCertFile) == SECFailure)
63 return mutt_nss_error ("NSS_Init");
65 /* always use strong crypto. */
66 if (NSS_SetDomesticPolicy () == SECFailure)
67 return mutt_nss_error ("NSS_SetDomesticPolicy");
69 /* intialize the session cache */
70 SSL_ClearSessionCache ();
72 MuttNssInitialized = 1;
77 /* convert from int64 to a readable string and print on the screen */
78 static void mutt_nss_pretty_time (int64 usecs)
84 PR_ExplodeTime (usecs, PR_LocalTimeParameters, &ex);
88 t.tm_hour = ex.tm_hour;
89 t.tm_mday = ex.tm_mday;
90 t.tm_mon = ex.tm_month;
91 t.tm_year = ex.tm_year - 1900; /* PRExplodedTime uses the absolute year */
92 t.tm_wday = ex.tm_wday;
93 t.tm_yday = ex.tm_yday;
95 strfcpy (timebuf, asctime (&t), sizeof (timebuf));
96 timebuf[mutt_strlen (timebuf) - 1] = 0;
101 /* this function is called by the default hook CERT_AuthCertificate when it
102 * can't verify a cert based upon the contents of the user's certificate
103 * database. we are given the option to override the decision and accept
106 static SECStatus mutt_nss_bad_cert (void *arg, PRFileDesc * fd)
109 CERTCertificate *cert, *issuer;
110 unsigned char hash[16];
113 int64 not_before, not_after;
117 /* first lets see why this certificate failed. we only want to override
118 * in the case where the cert was not found.
120 err = PR_GetError ();
121 mutt_error (_("SSL_AuthCertificate failed (error %d)"), err);
123 /* fetch the cert in question */
124 cert = SSL_PeerCertificate (fd);
131 addstr (CERT_NameToAscii (&cert->issuer));
134 addstr ("Subject: ");
135 addstr (CERT_NameToAscii (&cert->subject));
140 CERT_GetCertTimes (cert, ¬_before, ¬_after);
141 mutt_nss_pretty_time (not_before);
143 mutt_nss_pretty_time (not_after);
147 addstr ("Fingerprint: ");
149 /* calculate the MD5 hash of the raw certificate */
150 HASH_HashBuf (HASH_AlgMD5, hash, cert->derCert.data, cert->derCert.len);
151 for (i = 0; i < 16; i++) {
152 printw ("%0x", hash[i]);
157 mvaddstr (LINES - 3, 0, "Signature: ");
160 /* find the cert which signed this cert */
161 issuer = CERT_FindCertByName ((CERTCertDBHandle *) arg, &cert->derIssuer);
163 /* verify the sig (only) if we have the issuer cert handy */
165 CERT_VerifySignedData (&cert->signatureWrap, issuer, PR_Now (), NULL)
172 SETCOLOR (MT_COLOR_STATUS);
173 memset (status, ' ', sizeof (status) - 1);
174 if (COLS < sizeof (status))
175 status[COLS - 1] = 0;
177 status[sizeof (status) - 1] = 0;
178 memcpy (status, "--- SSL Certificate Check",
179 sizeof ("--- SSL Certificate Check") - 1);
182 SETCOLOR (MT_COLOR_NORMAL);
185 mvaddstr (LINES - 1, 0, "(r)eject, accept (o)nce, (a)lways accept?");
192 else if (ascii_tolower (ch.ch) == 'r') {
196 else if (ascii_tolower (ch.ch) == 'o') {
200 else if (ascii_tolower (ch.ch) == 'a') {
201 /* push this certificate onto the user's certificate store so it
202 * automatically becomes valid next time we see it
205 /* set this certificate as a valid peer for SSL-auth ONLY. */
206 CERT_DecodeTrustString (&trust, "P,,");
208 CERT_AddTempCertToPerm (cert, NULL, &trust);
215 /* SSL_PeerCertificate() returns a copy with an updated ref count, so
216 * we have to destroy our copy here.
218 CERT_DestroyCertificate (cert);
223 static int mutt_nss_socket_open (CONNECTION * con)
225 mutt_nss_t *sockdata;
229 memset (&addr, 0, sizeof (addr));
230 addr.inet.family = AF_INET;
231 addr.inet.port = PR_htons (con->account.port);
232 he = gethostbyname (con->account.host);
234 mutt_error (_("Unable to find ip for host %s"), con->account.host);
237 addr.inet.ip = *((int *) he->h_addr_list[0]);
239 sockdata = safe_calloc (1, sizeof (mutt_nss_t));
242 sockdata->fd = PR_NewTCPSocket ();
243 if (sockdata->fd == NULL) {
244 mutt_error (_("PR_NewTCPSocket failed."));
247 /* make this a SSL socket */
248 sockdata->fd = SSL_ImportFD (NULL, sockdata->fd);
250 /* set SSL version options based upon user's preferences */
251 if (!option (OPTTLSV1))
252 SSL_OptionSet (sockdata->fd, SSL_ENABLE_TLS, PR_FALSE);
254 if (!option (OPTSSLV2))
255 SSL_OptionSet (sockdata->fd, SSL_ENABLE_SSL2, PR_FALSE);
257 if (!option (OPTSSLV3))
258 SSL_OptionSet (sockdata->fd, SSL_ENABLE_SSL3, PR_FALSE);
260 /* set the host we were attempting to connect to in order to verify
261 * the name in the certificate we get back.
263 if (SSL_SetURL (sockdata->fd, con->account.host)) {
264 mutt_nss_error ("SSL_SetURL");
268 /* we don't need no stinking pin. we don't authenticate ourself
271 SSL_SetPKCS11PinArg (sockdata->fd, 0);
273 sockdata->db = CERT_GetDefaultCertDB ();
275 /* use the default supplied hook. it takes an argument to our
276 * certificate database. the manual lies, you can't really specify
277 * NULL for the callback to get the default!
279 SSL_AuthCertificateHook (sockdata->fd, SSL_AuthCertificate, sockdata->db);
280 /* set the callback to be used when SSL_AuthCertificate() fails. this
281 * allows us to override and insert the cert back into the db
283 SSL_BadCertHook (sockdata->fd, mutt_nss_bad_cert, sockdata->db);
285 if (PR_Connect (sockdata->fd, &addr, PR_INTERVAL_NO_TIMEOUT) ==
287 mutt_error (_("Unable to connect to host %s"), con->account.host);
291 /* store the extra info in the CONNECTION struct for later use. */
292 con->sockdata = sockdata;
294 /* HACK. some of the higher level calls in mutt_socket.c depend on this
295 * being >0 when we are in the connected state. we just set this to
296 * an arbitrary value to avoid hitting that bug, since we neve have the
306 /* we get here when we had an oops. clean up the mess. */
310 PR_Close (sockdata->fd);
312 CERT_ClosePermCertDB (sockdata->db);
318 static int mutt_nss_socket_close (CONNECTION * con)
320 mutt_nss_t *sockdata = (mutt_nss_t *) con->sockdata;
322 if (PR_Close (sockdata->fd) == PR_FAILURE)
326 CERT_ClosePermCertDB (sockdata->db);
327 /* free up the memory we used for this connection specific to NSS. */
328 FREE (&con->sockdata);
332 static int mutt_nss_socket_read (CONNECTION * conn, char *buf, size_t len)
334 return PR_Read (((mutt_nss_t *) conn->sockdata)->fd, buf, len);
338 mutt_nss_socket_write (CONNECTION * con, const char *buf, size_t count)
340 return PR_Write (((mutt_nss_t *) con->sockdata)->fd, buf, count);
343 /* initialize a new connection for use with NSS */
344 int mutt_nss_socket_setup (CONNECTION * con)
346 if (mutt_nss_init ())
348 con->conn_open = mutt_nss_socket_open;
349 con->conn_read = mutt_nss_socket_read;
350 con->conn_write = mutt_nss_socket_write;
351 con->conn_close = mutt_nss_socket_close;