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"
28 static int MuttNssInitialized = 0;
30 /* internal data struct we use with the CONNECTION. this is where NSS-specific
31 * data gets stuffed so that the main mutt_socket.h doesn't have to be
39 /* nss callback to grab the user's password. */
40 static char *mutt_nss_password_func (PK11SlotInfo * slot, PRBool retry,
46 static int mutt_nss_error (const char *call)
48 mutt_error (_("%s failed (error %d)"), call, PR_GetError ());
52 /* initialize the NSS library for use. must be called prior to any other
53 * functions in this module.
55 static int mutt_nss_init (void)
57 if (!MuttNssInitialized) {
58 PK11_SetPasswordFunc (mutt_nss_password_func);
59 if (NSS_Init (SslCertFile) == SECFailure)
60 return mutt_nss_error ("NSS_Init");
62 /* always use strong crypto. */
63 if (NSS_SetDomesticPolicy () == SECFailure)
64 return mutt_nss_error ("NSS_SetDomesticPolicy");
66 /* intialize the session cache */
67 SSL_ClearSessionCache ();
69 MuttNssInitialized = 1;
74 /* convert from int64 to a readable string and print on the screen */
75 static void mutt_nss_pretty_time (int64 usecs)
81 PR_ExplodeTime (usecs, PR_LocalTimeParameters, &ex);
85 t.tm_hour = ex.tm_hour;
86 t.tm_mday = ex.tm_mday;
87 t.tm_mon = ex.tm_month;
88 t.tm_year = ex.tm_year - 1900; /* PRExplodedTime uses the absolute year */
89 t.tm_wday = ex.tm_wday;
90 t.tm_yday = ex.tm_yday;
92 strfcpy (timebuf, asctime (&t), sizeof (timebuf));
93 timebuf[mutt_strlen (timebuf) - 1] = 0;
98 /* this function is called by the default hook CERT_AuthCertificate when it
99 * can't verify a cert based upon the contents of the user's certificate
100 * database. we are given the option to override the decision and accept
103 static SECStatus mutt_nss_bad_cert (void *arg, PRFileDesc * fd)
106 CERTCertificate *cert, *issuer;
107 unsigned char hash[16];
110 int64 not_before, not_after;
114 /* first lets see why this certificate failed. we only want to override
115 * in the case where the cert was not found.
117 err = PR_GetError ();
118 mutt_error (_("SSL_AuthCertificate failed (error %d)"), err);
120 /* fetch the cert in question */
121 cert = SSL_PeerCertificate (fd);
128 addstr (CERT_NameToAscii (&cert->issuer));
131 addstr ("Subject: ");
132 addstr (CERT_NameToAscii (&cert->subject));
137 CERT_GetCertTimes (cert, ¬_before, ¬_after);
138 mutt_nss_pretty_time (not_before);
140 mutt_nss_pretty_time (not_after);
144 addstr ("Fingerprint: ");
146 /* calculate the MD5 hash of the raw certificate */
147 HASH_HashBuf (HASH_AlgMD5, hash, cert->derCert.data, cert->derCert.len);
148 for (i = 0; i < 16; i++) {
149 printw ("%0x", hash[i]);
154 mvaddstr (LINES - 3, 0, "Signature: ");
157 /* find the cert which signed this cert */
158 issuer = CERT_FindCertByName ((CERTCertDBHandle *) arg, &cert->derIssuer);
160 /* verify the sig (only) if we have the issuer cert handy */
162 CERT_VerifySignedData (&cert->signatureWrap, issuer, PR_Now (), NULL)
169 SETCOLOR (MT_COLOR_STATUS);
170 memset (status, ' ', sizeof (status) - 1);
171 if (COLS < sizeof (status))
172 status[COLS - 1] = 0;
174 status[sizeof (status) - 1] = 0;
175 memcpy (status, "--- SSL Certificate Check",
176 sizeof ("--- SSL Certificate Check") - 1);
179 SETCOLOR (MT_COLOR_NORMAL);
182 mvaddstr (LINES - 1, 0, "(r)eject, accept (o)nce, (a)lways accept?");
189 else if (ascii_tolower (ch.ch) == 'r') {
193 else if (ascii_tolower (ch.ch) == 'o') {
197 else if (ascii_tolower (ch.ch) == 'a') {
198 /* push this certificate onto the user's certificate store so it
199 * automatically becomes valid next time we see it
202 /* set this certificate as a valid peer for SSL-auth ONLY. */
203 CERT_DecodeTrustString (&trust, "P,,");
205 CERT_AddTempCertToPerm (cert, NULL, &trust);
212 /* SSL_PeerCertificate() returns a copy with an updated ref count, so
213 * we have to destroy our copy here.
215 CERT_DestroyCertificate (cert);
220 static int mutt_nss_socket_open (CONNECTION * con)
222 mutt_nss_t *sockdata;
226 memset (&addr, 0, sizeof (addr));
227 addr.inet.family = AF_INET;
228 addr.inet.port = PR_htons (con->account.port);
229 he = gethostbyname (con->account.host);
231 mutt_error (_("Unable to find ip for host %s"), con->account.host);
234 addr.inet.ip = *((int *) he->h_addr_list[0]);
236 sockdata = safe_calloc (1, sizeof (mutt_nss_t));
239 sockdata->fd = PR_NewTCPSocket ();
240 if (sockdata->fd == NULL) {
241 mutt_error (_("PR_NewTCPSocket failed."));
244 /* make this a SSL socket */
245 sockdata->fd = SSL_ImportFD (NULL, sockdata->fd);
247 /* set SSL version options based upon user's preferences */
248 if (!option (OPTTLSV1))
249 SSL_OptionSet (sockdata->fd, SSL_ENABLE_TLS, PR_FALSE);
251 if (!option (OPTSSLV2))
252 SSL_OptionSet (sockdata->fd, SSL_ENABLE_SSL2, PR_FALSE);
254 if (!option (OPTSSLV3))
255 SSL_OptionSet (sockdata->fd, SSL_ENABLE_SSL3, PR_FALSE);
257 /* set the host we were attempting to connect to in order to verify
258 * the name in the certificate we get back.
260 if (SSL_SetURL (sockdata->fd, con->account.host)) {
261 mutt_nss_error ("SSL_SetURL");
265 /* we don't need no stinking pin. we don't authenticate ourself
268 SSL_SetPKCS11PinArg (sockdata->fd, 0);
270 sockdata->db = CERT_GetDefaultCertDB ();
272 /* use the default supplied hook. it takes an argument to our
273 * certificate database. the manual lies, you can't really specify
274 * NULL for the callback to get the default!
276 SSL_AuthCertificateHook (sockdata->fd, SSL_AuthCertificate, sockdata->db);
277 /* set the callback to be used when SSL_AuthCertificate() fails. this
278 * allows us to override and insert the cert back into the db
280 SSL_BadCertHook (sockdata->fd, mutt_nss_bad_cert, sockdata->db);
282 if (PR_Connect (sockdata->fd, &addr, PR_INTERVAL_NO_TIMEOUT) ==
284 mutt_error (_("Unable to connect to host %s"), con->account.host);
288 /* store the extra info in the CONNECTION struct for later use. */
289 con->sockdata = sockdata;
291 /* HACK. some of the higher level calls in mutt_socket.c depend on this
292 * being >0 when we are in the connected state. we just set this to
293 * an arbitrary value to avoid hitting that bug, since we neve have the
303 /* we get here when we had an oops. clean up the mess. */
307 PR_Close (sockdata->fd);
309 CERT_ClosePermCertDB (sockdata->db);
315 static int mutt_nss_socket_close (CONNECTION * con)
317 mutt_nss_t *sockdata = (mutt_nss_t *) con->sockdata;
319 if (PR_Close (sockdata->fd) == PR_FAILURE)
323 CERT_ClosePermCertDB (sockdata->db);
324 /* free up the memory we used for this connection specific to NSS. */
325 FREE (&con->sockdata);
329 static int mutt_nss_socket_read (CONNECTION * conn, char *buf, size_t len)
331 return PR_Read (((mutt_nss_t *) conn->sockdata)->fd, buf, len);
335 mutt_nss_socket_write (CONNECTION * con, const char *buf, size_t count)
337 return PR_Write (((mutt_nss_t *) con->sockdata)->fd, buf, count);
340 /* initialize a new connection for use with NSS */
341 int mutt_nss_socket_setup (CONNECTION * con)
343 if (mutt_nss_init ())
345 con->conn_open = mutt_nss_socket_open;
346 con->conn_read = mutt_nss_socket_read;
347 con->conn_write = mutt_nss_socket_write;
348 con->conn_close = mutt_nss_socket_close;