auth_http.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (c) 2007 iptelorg GmbH
00005  *
00006  * This file is part of SIP-router, a free SIP server.
00007  *
00008  * SIP-router is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * SIP-router is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <curl/curl.h>
00034 #include <curl/easy.h>
00035 #include <openssl/pem.h>
00036 #include <openssl/err.h>
00037 #include <openssl/x509.h>
00038 #include <openssl/bio.h>
00039 
00040 
00041 #include "../../mem/mem.h"
00042 #include "../../data_lump.h"
00043 
00044 #include "auth_identity.h"
00045 
00046 size_t curlmem_cb(void *ptr, size_t size, size_t nmemb, void *data)
00047 {
00048         size_t irealsize = size * nmemb;
00049 
00050         /* too big certificate */
00051         if (((str*)data)->len + irealsize >= CERTIFICATE_LENGTH)
00052                 return 0;
00053 
00054         memcpy(&(((str*)data)->s[((str*)data)->len]), ptr, irealsize);
00055         ((str*)data)->len+=irealsize;
00056 
00057         return irealsize;
00058 }
00059 
00060 int download_cer(str *suri, CURL *hcurl)
00061 {
00062         CURLcode iRes;
00063         long lerr=200;
00064         char snulled[CERTIFICATE_URL_LENGTH], *snulledptr=NULL;
00065         int iRet=0;
00066 
00067         if (suri->len < sizeof(snulled)) {
00068                 memcpy(snulled, suri->s, suri->len);
00069                 snulled[suri->len]=0;
00070         } else {
00071                 /* +1 for the terminating \0 byte */
00072                 if (!(snulledptr=pkg_malloc(suri->len + 1))) {
00073                         LOG(L_ERR, "AUTH_IDENTITY:download_cer: Not enough memory error\n");
00074                         return -1;
00075                 }
00076                 memcpy(snulledptr, suri->s, suri->len);
00077                 snulledptr[suri->len]=0;
00078         }
00079 
00080         do {
00081                 if ((iRes=curl_easy_setopt(hcurl,
00082                                                                    CURLOPT_URL,
00083                                                                    snulledptr ? snulledptr : snulled))!=0) {
00084                         LOG(L_ERR,
00085                                 "AUTH_IDENTITY:download_cer: Unable to set the url of certificate: %s\n",
00086                                 curl_easy_strerror(iRes));
00087                         iRet=-2;
00088                         break;
00089                 }
00090 
00091                 if ((iRes=curl_easy_perform(hcurl))!=0) {
00092                         LOG(L_ERR,
00093                                 "AUTH_IDENTITY:download_cer: Error while downloading certificate '%s'\n",
00094                                 curl_easy_strerror(iRes));
00095                         iRet=-3;
00096                         break;
00097                 }
00098 
00099                 curl_easy_getinfo(hcurl,CURLINFO_RESPONSE_CODE,&lerr);
00100                 if (lerr/100 != 2) {
00101                         LOG(L_ERR, "AUTH_IDENTITY:download_cer: Bad HTTP response: %ld\n", lerr );
00102                         iRet=-4;
00103                 }
00104         } while (0);
00105 
00106         if (snulledptr)
00107                 pkg_free(snulledptr);
00108 
00109         return iRet;
00110 }