More removal of $Id$ tags
[citadel.git] / citadel / modules / crypto / serv_crypto.c
index 70d9aab9473ce7577fdafa051a4dbf4ddb957150..e962d7f01c772886ba03d584270eadf166888f11 100644 (file)
@@ -1,4 +1,20 @@
-/* $Id$ */
+/*
+ * Copyright (c) 1987-2009 by the citadel.org team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
 
 #include <string.h>
 #include <unistd.h>
@@ -243,17 +259,23 @@ void init_ssl(void)
                                        */
 
                                        X509_NAME_add_entry_by_txt(name, "O",
-                                               MBSTRING_ASC, config.c_humannode, -1, -1, 0);
+                                                                  MBSTRING_ASC, 
+                                                                  (unsigned char*) config.c_humannode,
+                                                                  -1, -1, 0);
 
                                        X509_NAME_add_entry_by_txt(name, "OU",
-                                               MBSTRING_ASC, "Citadel server", -1, -1, 0);
+                                                                  MBSTRING_ASC, 
+                                                                  (unsigned const char*)"Citadel server",
+                                                                  -1, -1, 0);
 
                                        /* X509_NAME_add_entry_by_txt(name, "CN",
                                                MBSTRING_ASC, config.c_fqdn, -1, -1, 0);
                                        */
 
-                                       X509_NAME_add_entry_by_txt(name, "CN",
-                                               MBSTRING_ASC, "*", -1, -1, 0);
+                                       X509_NAME_add_entry_by_txt(name, 
+                                                                  "CN",
+                                                                  MBSTRING_ASC, 
+                                                                  (const unsigned char *)"*", -1, -1, 0);
                                
                                        X509_REQ_set_subject_name(req, name);
 
@@ -370,7 +392,7 @@ void init_ssl(void)
 /*
  * client_write_ssl() Send binary data to the client encrypted.
  */
-void client_write_ssl(char *buf, int nbytes)
+void client_write_ssl(const char *buf, int nbytes)
 {
        int retval;
        int nremain;
@@ -408,62 +430,199 @@ void client_write_ssl(char *buf, int nbytes)
 
 
 /*
- * client_read_ssl() - read data from the encrypted layer.
+ * read data from the encrypted layer.
  */
-int client_read_ssl(char *buf, int bytes, int timeout)
+int client_read_sslbuffer(StrBuf *buf, int timeout)
 {
-#if 0
-       fd_set rfds;
-       struct timeval tv;
-       int retval;
-       int s;
-#endif
-       int len, rlen;
+       char sbuf[16384]; /* OpenSSL communicates in 16k blocks, so let's speak its native tongue. */
+       int rlen;
        char junk[1];
+       SSL *pssl = CC->ssl;
 
-       len = 0;
-       while (len < bytes) {
-#if 0
-               /*
-                * This code is disabled because we don't need it when
-                * using blocking reads (which we are). -IO
-                */
-               FD_ZERO(&rfds);
-               s = BIO_get_fd(CC->ssl->rbio, NULL);
-               FD_SET(s, &rfds);
-               tv.tv_sec = timeout;
-               tv.tv_usec = 0;
-
-               retval = select(s + 1, &rfds, NULL, NULL, &tv);
+       if (pssl == NULL) return(-1);
 
-               if (FD_ISSET(s, &rfds) == 0) {
-                       return (0);
-               }
-
-#endif
-               if (SSL_want_read(CC->ssl)) {
-                       if ((SSL_write(CC->ssl, junk, 0)) < 1) {
-                               CtdlLogPrintf(CTDL_DEBUG, "SSL_write in client_read: %s\n", ERR_reason_error_string(ERR_get_error()));
+       while (1) {
+               if (SSL_want_read(pssl)) {
+                       if ((SSL_write(pssl, junk, 0)) < 1) {
+                               CtdlLogPrintf(CTDL_DEBUG, "SSL_write in client_read\n");
                        }
                }
-               rlen = SSL_read(CC->ssl, &buf[len], bytes - len);
+               rlen = SSL_read(pssl, sbuf, sizeof(sbuf));
                if (rlen < 1) {
                        long errval;
 
-                       errval = SSL_get_error(CC->ssl, rlen);
-                       if (errval == SSL_ERROR_WANT_READ ||
-                           errval == SSL_ERROR_WANT_WRITE) {
+                       errval = SSL_get_error(pssl, rlen);
+                       if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
                                sleep(1);
                                continue;
                        }
                        CtdlLogPrintf(CTDL_DEBUG, "SSL_read got error %ld\n", errval);
                        endtls();
-                       return (client_read_to
-                               (&buf[len], bytes - len, timeout));
+                       return (-1);
+               }
+               StrBufAppendBufPlain(buf, sbuf, rlen, 0);
+               return rlen;
+       }
+       return (0);
+}
+
+int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int timeout)
+{
+       CitContext *CCC = CC;
+       const char *pos = NULL;
+       const char *pLF;
+       int len, rlen, retlen;
+       int nSuccessLess = 0;
+       const char *pch = NULL;
+       
+       retlen = 0;
+       if ((Line == NULL) ||
+           (Pos == NULL) ||
+           (IOBuf == NULL))
+       {
+               if (Pos != NULL)
+                       *Pos = NULL;
+//             *Error = ErrRBLF_PreConditionFailed;
+               return -1;
+       }
+
+       pos = *Pos;
+       if ((StrLength(IOBuf) > 0) && 
+           (pos != NULL) && 
+           (pos < ChrPtr(IOBuf) + StrLength(IOBuf))) 
+       {
+               pch = pos;
+               pch = strchr(pch, '\n');
+               
+               if (pch == NULL) {
+                       StrBufAppendBufPlain(Line, pos, 
+                                            StrLength(IOBuf) - (pos - ChrPtr(IOBuf)), 0);
+                       FlushStrBuf(IOBuf);
+                       pos = *Pos = NULL;
+               }
+               else {
+                       int n = 0;
+                       if ((pch > ChrPtr(IOBuf)) && 
+                           (*(pch - 1) == '\r')) {
+                               n = 1;
+                       }
+                       StrBufAppendBufPlain(Line, pos, 
+                                            (pch - pos - n), 0);
+
+                       if (StrLength(IOBuf) <= (pch - ChrPtr(IOBuf) + 1)) {
+                               FlushStrBuf(IOBuf);
+                               pos = *Pos = NULL;
+                       }
+                       else 
+                               *Pos = pch + 1;
+                       return StrLength(Line);
+               }
+       }
+
+       pLF = NULL;
+       while ((nSuccessLess < timeout) && 
+              (pLF == NULL) &&
+              (CCC->ssl != NULL)) {
+
+               rlen = client_read_sslbuffer(IOBuf, timeout);
+               if (rlen < 1) {
+//                     *Error = strerror(errno);
+//                     close(*fd);
+//                     *fd = -1;
+                       return -1;
+               }
+               else if (rlen > 0) {
+                       pLF = strchr(ChrPtr(IOBuf), '\n');
+               }
+       }
+       *Pos = NULL;
+       if (pLF != NULL) {
+               pos = ChrPtr(IOBuf);
+               len = pLF - pos;
+               if (len > 0 && (*(pLF - 1) == '\r') )
+                       len --;
+               StrBufAppendBufPlain(Line, pos, len, 0);
+               if (pLF + 1 >= ChrPtr(IOBuf) + StrLength(IOBuf))
+               {
+                       FlushStrBuf(IOBuf);
+               }
+               else 
+                       *Pos = pLF + 1;
+               return StrLength(Line);
+       }
+//     *Error = ErrRBLF_NotEnoughSentFromServer;
+       return -1;
+}
+
+
+
+int client_read_sslblob(StrBuf *Target, long bytes, int timeout)
+{
+       long baselen;
+       long RemainRead;
+       int retval = 0;
+       CitContext *CCC = CC;
+
+       baselen = StrLength(Target);
+
+       if (StrLength(CCC->ReadBuf) > 0)
+       {
+               long RemainLen;
+               long TotalLen;
+               const char *pchs;
+
+               if (CCC->Pos == NULL)
+                       CCC->Pos = ChrPtr(CCC->ReadBuf);
+               pchs = ChrPtr(CCC->ReadBuf);
+               TotalLen = StrLength(CCC->ReadBuf);
+               RemainLen = TotalLen - (pchs - CCC->Pos);
+               if (RemainLen > bytes)
+                       RemainLen = bytes;
+               if (RemainLen > 0)
+               {
+                       StrBufAppendBufPlain(Target, 
+                                            CCC->Pos, 
+                                            RemainLen, 0);
+                       CCC->Pos += RemainLen;
+               }
+               if ((ChrPtr(CCC->ReadBuf) + StrLength(CCC->ReadBuf)) <= CCC->Pos)
+               {
+                       CCC->Pos = NULL;
+                       FlushStrBuf(CCC->ReadBuf);
+               }
+       }
+
+       if (StrLength(Target) >= bytes + baselen)
+               return 1;
+
+       CCC->Pos = NULL;
+
+       while ((StrLength(Target) < bytes + baselen) &&
+              (retval >= 0))
+       {
+               retval = client_read_sslbuffer(CCC->ReadBuf, timeout);
+               if (retval >= 0) {
+                       RemainRead = bytes - (StrLength (Target) - baselen);
+                       if (RemainRead > StrLength(CCC->ReadBuf))
+                       {
+                               StrBufAppendBufPlain(
+                                       Target, 
+                                       ChrPtr(CCC->ReadBuf), 
+                                       RemainRead, 0);
+                               CCC->Pos = ChrPtr(CCC->ReadBuf) + RemainRead;
+                               break;
+                       }
+                       StrBufAppendBuf(Target, CCC->ReadBuf, 0); /* todo: Buf > bytes? */
+                       FlushStrBuf(CCC->ReadBuf);
+               }
+               else 
+               {
+                       FlushStrBuf(CCC->ReadBuf);
+                       return -1;
+       
                }
-               len += rlen;
        }
-       return (1);
+       return 1;
 }