nss.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2008, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. /*
  24. * Source file for all NSS-specific code for the TLS/SSL layer. No code
  25. * but sslgen.c should ever call or use these functions.
  26. */
  27. #include "setup.h"
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #ifdef HAVE_SYS_SOCKET_H
  32. #include <sys/socket.h>
  33. #endif
  34. #include "urldata.h"
  35. #include "sendf.h"
  36. #include "formdata.h" /* for the boundary function */
  37. #include "url.h" /* for the ssl config check function */
  38. #include "connect.h"
  39. #include "strequal.h"
  40. #include "select.h"
  41. #include "sslgen.h"
  42. #include "strequal.h"
  43. #define _MPRINTF_REPLACE /* use the internal *printf() functions */
  44. #include <curl/mprintf.h>
  45. #ifdef USE_NSS
  46. #include "nssg.h"
  47. #include <nspr.h>
  48. #include <nss.h>
  49. #include <ssl.h>
  50. #include <sslerr.h>
  51. #include <secerr.h>
  52. #include <secmod.h>
  53. #include <sslproto.h>
  54. #include <prtypes.h>
  55. #include <pk11pub.h>
  56. #include <prio.h>
  57. #include <secitem.h>
  58. #include <secport.h>
  59. #include <certdb.h>
  60. #include "memory.h"
  61. #include "easyif.h" /* for Curl_convert_from_utf8 prototype */
  62. /* The last #include file should be: */
  63. #include "memdebug.h"
  64. #define SSL_DIR "/etc/pki/nssdb"
  65. /* enough to fit the string "PEM Token #[0|1]" */
  66. #define SLOTSIZE 13
  67. PRFileDesc *PR_ImportTCPSocket(PRInt32 osfd);
  68. int initialized = 0;
  69. #define HANDSHAKE_TIMEOUT 30
  70. typedef struct {
  71. PRInt32 retryCount;
  72. struct SessionHandle *data;
  73. } pphrase_arg_t;
  74. typedef struct {
  75. const char *name;
  76. int num;
  77. PRInt32 version; /* protocol version valid for this cipher */
  78. } cipher_s;
  79. #define PK11_SETATTRS(x,id,v,l) (x)->type = (id); \
  80. (x)->pValue=(v); (x)->ulValueLen = (l)
  81. #define CERT_NewTempCertificate __CERT_NewTempCertificate
  82. enum sslversion { SSL2 = 1, SSL3 = 2, TLS = 4 };
  83. #define NUM_OF_CIPHERS sizeof(cipherlist)/sizeof(cipherlist[0])
  84. static const cipher_s cipherlist[] = {
  85. /* SSL2 cipher suites */
  86. {"rc4", SSL_EN_RC4_128_WITH_MD5, SSL2},
  87. {"rc4-md5", SSL_EN_RC4_128_WITH_MD5, SSL2},
  88. {"rc4export", SSL_EN_RC4_128_EXPORT40_WITH_MD5, SSL2},
  89. {"rc2", SSL_EN_RC2_128_CBC_WITH_MD5, SSL2},
  90. {"rc2export", SSL_EN_RC2_128_CBC_EXPORT40_WITH_MD5, SSL2},
  91. {"des", SSL_EN_DES_64_CBC_WITH_MD5, SSL2},
  92. {"desede3", SSL_EN_DES_192_EDE3_CBC_WITH_MD5, SSL2},
  93. /* SSL3/TLS cipher suites */
  94. {"rsa_rc4_128_md5", SSL_RSA_WITH_RC4_128_MD5, SSL3 | TLS},
  95. {"rsa_rc4_128_sha", SSL_RSA_WITH_RC4_128_SHA, SSL3 | TLS},
  96. {"rsa_3des_sha", SSL_RSA_WITH_3DES_EDE_CBC_SHA, SSL3 | TLS},
  97. {"rsa_des_sha", SSL_RSA_WITH_DES_CBC_SHA, SSL3 | TLS},
  98. {"rsa_rc4_40_md5", SSL_RSA_EXPORT_WITH_RC4_40_MD5, SSL3 | TLS},
  99. {"rsa_rc2_40_md5", SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5, SSL3 | TLS},
  100. {"rsa_null_md5", SSL_RSA_WITH_NULL_MD5, SSL3 | TLS},
  101. {"rsa_null_sha", SSL_RSA_WITH_NULL_SHA, SSL3 | TLS},
  102. {"fips_3des_sha", SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA, SSL3 | TLS},
  103. {"fips_des_sha", SSL_RSA_FIPS_WITH_DES_CBC_SHA, SSL3 | TLS},
  104. {"fortezza", SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA, SSL3 | TLS},
  105. {"fortezza_rc4_128_sha", SSL_FORTEZZA_DMS_WITH_RC4_128_SHA, SSL3 | TLS},
  106. {"fortezza_null", SSL_FORTEZZA_DMS_WITH_NULL_SHA, SSL3 | TLS},
  107. /* TLS 1.0: Exportable 56-bit Cipher Suites. */
  108. {"rsa_des_56_sha", TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA, SSL3 | TLS},
  109. {"rsa_rc4_56_sha", TLS_RSA_EXPORT1024_WITH_RC4_56_SHA, SSL3 | TLS},
  110. /* AES ciphers. */
  111. {"rsa_aes_128_sha", TLS_RSA_WITH_AES_128_CBC_SHA, SSL3 | TLS},
  112. {"rsa_aes_256_sha", TLS_RSA_WITH_AES_256_CBC_SHA, SSL3 | TLS},
  113. #ifdef NSS_ENABLE_ECC
  114. /* ECC ciphers. */
  115. {"ecdh_ecdsa_null_sha", TLS_ECDH_ECDSA_WITH_NULL_SHA, TLS},
  116. {"ecdh_ecdsa_rc4_128_sha", TLS_ECDH_ECDSA_WITH_RC4_128_SHA, TLS},
  117. {"ecdh_ecdsa_3des_sha", TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS},
  118. {"ecdh_ecdsa_aes_128_sha", TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS},
  119. {"ecdh_ecdsa_aes_256_sha", TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, TLS},
  120. {"ecdhe_ecdsa_null_sha", TLS_ECDHE_ECDSA_WITH_NULL_SHA, TLS},
  121. {"ecdhe_ecdsa_rc4_128_sha", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS},
  122. {"ecdhe_ecdsa_3des_sha", TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS},
  123. {"ecdhe_ecdsa_aes_128_sha", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS},
  124. {"ecdhe_ecdsa_aes_256_sha", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS},
  125. {"ecdh_rsa_null_sha", TLS_ECDH_RSA_WITH_NULL_SHA, TLS},
  126. {"ecdh_rsa_128_sha", TLS_ECDH_RSA_WITH_RC4_128_SHA, TLS},
  127. {"ecdh_rsa_3des_sha", TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, TLS},
  128. {"ecdh_rsa_aes_128_sha", TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS},
  129. {"ecdh_rsa_aes_256_sha", TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, TLS},
  130. {"echde_rsa_null", TLS_ECDHE_RSA_WITH_NULL_SHA, TLS},
  131. {"ecdhe_rsa_rc4_128_sha", TLS_ECDHE_RSA_WITH_RC4_128_SHA, TLS},
  132. {"ecdhe_rsa_3des_sha", TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, TLS},
  133. {"ecdhe_rsa_aes_128_sha", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS},
  134. {"ecdhe_rsa_aes_256_sha", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS},
  135. {"ecdh_anon_null_sha", TLS_ECDH_anon_WITH_NULL_SHA, TLS},
  136. {"ecdh_anon_rc4_128sha", TLS_ECDH_anon_WITH_RC4_128_SHA, TLS},
  137. {"ecdh_anon_3des_sha", TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA, TLS},
  138. {"ecdh_anon_aes_128_sha", TLS_ECDH_anon_WITH_AES_128_CBC_SHA, TLS},
  139. {"ecdh_anon_aes_256_sha", TLS_ECDH_anon_WITH_AES_256_CBC_SHA, TLS},
  140. #endif
  141. };
  142. #ifdef HAVE_PK11_CREATEGENERICOBJECT
  143. static const char* pem_library = "libnsspem.so";
  144. #endif
  145. SECMODModule* mod = NULL;
  146. static SECStatus set_ciphers(struct SessionHandle *data, PRFileDesc * model,
  147. char *cipher_list)
  148. {
  149. unsigned int i;
  150. PRBool cipher_state[NUM_OF_CIPHERS];
  151. PRBool found;
  152. char *cipher;
  153. SECStatus rv;
  154. /* First disable all ciphers. This uses a different max value in case
  155. * NSS adds more ciphers later we don't want them available by
  156. * accident
  157. */
  158. for(i=0; i<SSL_NumImplementedCiphers; i++) {
  159. SSL_CipherPrefSet(model, SSL_ImplementedCiphers[i], SSL_NOT_ALLOWED);
  160. }
  161. /* Set every entry in our list to false */
  162. for(i=0; i<NUM_OF_CIPHERS; i++) {
  163. cipher_state[i] = PR_FALSE;
  164. }
  165. cipher = cipher_list;
  166. while(cipher_list && (cipher_list[0])) {
  167. while((*cipher) && (ISSPACE(*cipher)))
  168. ++cipher;
  169. if((cipher_list = strchr(cipher, ','))) {
  170. *cipher_list++ = '\0';
  171. }
  172. found = PR_FALSE;
  173. for(i=0; i<NUM_OF_CIPHERS; i++) {
  174. if(strequal(cipher, cipherlist[i].name)) {
  175. cipher_state[i] = PR_TRUE;
  176. found = PR_TRUE;
  177. break;
  178. }
  179. }
  180. if(found == PR_FALSE) {
  181. failf(data, "Unknown cipher in list: %s", cipher);
  182. return SECFailure;
  183. }
  184. if(cipher_list) {
  185. cipher = cipher_list;
  186. }
  187. }
  188. /* Finally actually enable the selected ciphers */
  189. for(i=0; i<NUM_OF_CIPHERS; i++) {
  190. rv = SSL_CipherPrefSet(model, cipherlist[i].num, cipher_state[i]);
  191. if(rv != SECSuccess) {
  192. failf(data, "Unknown cipher in cipher list");
  193. return SECFailure;
  194. }
  195. }
  196. return SECSuccess;
  197. }
  198. /*
  199. * Determine whether the nickname passed in is a filename that needs to
  200. * be loaded as a PEM or a regular NSS nickname.
  201. *
  202. * returns 1 for a file
  203. * returns 0 for not a file (NSS nickname)
  204. */
  205. static int is_file(const char *filename)
  206. {
  207. struct stat st;
  208. if(filename == NULL)
  209. return 0;
  210. if(stat(filename, &st) == 0)
  211. if(S_ISREG(st.st_mode))
  212. return 1;
  213. return 0;
  214. }
  215. static int
  216. nss_load_cert(const char *filename, PRBool cacert)
  217. {
  218. #ifdef HAVE_PK11_CREATEGENERICOBJECT
  219. CK_SLOT_ID slotID;
  220. PK11SlotInfo * slot = NULL;
  221. PK11GenericObject *rv;
  222. CK_ATTRIBUTE *attrs;
  223. CK_ATTRIBUTE theTemplate[20];
  224. CK_BBOOL cktrue = CK_TRUE;
  225. CK_BBOOL ckfalse = CK_FALSE;
  226. CK_OBJECT_CLASS objClass = CKO_CERTIFICATE;
  227. char *slotname = NULL;
  228. #endif
  229. CERTCertificate *cert;
  230. char *nickname = NULL;
  231. char *n = NULL;
  232. /* If there is no slash in the filename it is assumed to be a regular
  233. * NSS nickname.
  234. */
  235. if(is_file(filename)) {
  236. n = strrchr(filename, '/');
  237. if(n)
  238. n++;
  239. if(!mod)
  240. return 1;
  241. }
  242. else {
  243. /* A nickname from the NSS internal database */
  244. if(cacert)
  245. return 0; /* You can't specify an NSS CA nickname this way */
  246. nickname = strdup(filename);
  247. goto done;
  248. }
  249. #ifdef HAVE_PK11_CREATEGENERICOBJECT
  250. attrs = theTemplate;
  251. /* All CA and trust objects go into slot 0. Other slots are used
  252. * for storing certificates. With each new user certificate we increment
  253. * the slot count. We only support 1 user certificate right now.
  254. */
  255. if(cacert)
  256. slotID = 0;
  257. else
  258. slotID = 1;
  259. slotname = (char *)malloc(SLOTSIZE);
  260. nickname = (char *)malloc(PATH_MAX);
  261. snprintf(slotname, SLOTSIZE, "PEM Token #%ld", slotID);
  262. snprintf(nickname, PATH_MAX, "PEM Token #%ld:%s", slotID, n);
  263. slot = PK11_FindSlotByName(slotname);
  264. if(!slot) {
  265. free(slotname);
  266. free(nickname);
  267. return 0;
  268. }
  269. PK11_SETATTRS(attrs, CKA_CLASS, &objClass, sizeof(objClass) );
  270. attrs++;
  271. PK11_SETATTRS(attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) );
  272. attrs++;
  273. PK11_SETATTRS(attrs, CKA_LABEL, (unsigned char *)filename,
  274. strlen(filename)+1);
  275. attrs++;
  276. if(cacert) {
  277. PK11_SETATTRS(attrs, CKA_TRUST, &cktrue, sizeof(CK_BBOOL) );
  278. }
  279. else {
  280. PK11_SETATTRS(attrs, CKA_TRUST, &ckfalse, sizeof(CK_BBOOL) );
  281. }
  282. attrs++;
  283. /* This load the certificate in our PEM module into the appropriate
  284. * slot.
  285. */
  286. rv = PK11_CreateGenericObject(slot, theTemplate, 4, PR_FALSE /* isPerm */);
  287. PK11_FreeSlot(slot);
  288. free(slotname);
  289. if(rv == NULL) {
  290. free(nickname);
  291. return 0;
  292. }
  293. #else
  294. /* We don't have PK11_CreateGenericObject but a file-based cert was passed
  295. * in. We need to fail.
  296. */
  297. return 0;
  298. #endif
  299. done:
  300. /* Double-check that the certificate or nickname requested exists in
  301. * either the token or the NSS certificate database.
  302. */
  303. if(!cacert) {
  304. cert = PK11_FindCertFromNickname((char *)nickname, NULL);
  305. /* An invalid nickname was passed in */
  306. if(cert == NULL) {
  307. free(nickname);
  308. PR_SetError(SEC_ERROR_UNKNOWN_CERT, 0);
  309. return 0;
  310. }
  311. CERT_DestroyCertificate(cert);
  312. }
  313. free(nickname);
  314. return 1;
  315. }
  316. static int nss_load_crl(char* crlfilename, PRBool ascii)
  317. {
  318. PRFileDesc *infile;
  319. PRStatus prstat;
  320. PRFileInfo info;
  321. PRInt32 nb;
  322. int rv;
  323. SECItem crlDER;
  324. CERTSignedCrl *crl=NULL;
  325. PK11SlotInfo *slot=NULL;
  326. infile = PR_Open(crlfilename,PR_RDONLY,0);
  327. if (!infile) {
  328. return 0;
  329. }
  330. crlDER.data = NULL;
  331. prstat = PR_GetOpenFileInfo(infile,&info);
  332. if (prstat!=PR_SUCCESS)
  333. return 0;
  334. if (ascii) {
  335. SECItem filedata;
  336. char *asc,*body;
  337. filedata.data = NULL;
  338. if (!SECITEM_AllocItem(NULL,&filedata,info.size))
  339. return 0;
  340. nb = PR_Read(infile,filedata.data,info.size);
  341. if (nb!=info.size)
  342. return 0;
  343. asc = (char*)filedata.data;
  344. if (!asc)
  345. return 0;
  346. body=strstr(asc,"-----BEGIN");
  347. if (body != NULL) {
  348. char *trailer=NULL;
  349. asc = body;
  350. body = PORT_Strchr(asc,'\n');
  351. if (!body)
  352. body = PORT_Strchr(asc,'\r');
  353. if (body)
  354. trailer = strstr(++body,"-----END");
  355. if (trailer!=NULL)
  356. *trailer='\0';
  357. else
  358. return 0;
  359. }
  360. else {
  361. body = asc;
  362. }
  363. rv = ATOB_ConvertAsciiToItem(&crlDER,body);
  364. PORT_Free(filedata.data);
  365. if (rv)
  366. return 0;
  367. }
  368. else {
  369. if (!SECITEM_AllocItem(NULL,&crlDER,info.size))
  370. return 0;
  371. nb = PR_Read(infile,crlDER.data,info.size);
  372. if (nb!=info.size)
  373. return 0;
  374. }
  375. slot = PK11_GetInternalKeySlot();
  376. crl = PK11_ImportCRL(slot,&crlDER,
  377. NULL,SEC_CRL_TYPE,
  378. NULL,CRL_IMPORT_DEFAULT_OPTIONS,
  379. NULL,(CRL_DECODE_DEFAULT_OPTIONS|
  380. CRL_DECODE_DONT_COPY_DER));
  381. if (slot) PK11_FreeSlot(slot);
  382. if (!crl) return 0;
  383. SEC_DestroyCrl(crl);
  384. return 1;
  385. }
  386. static int nss_load_key(struct connectdata *conn, char *key_file)
  387. {
  388. #ifdef HAVE_PK11_CREATEGENERICOBJECT
  389. PK11SlotInfo * slot = NULL;
  390. PK11GenericObject *rv;
  391. CK_ATTRIBUTE *attrs;
  392. CK_ATTRIBUTE theTemplate[20];
  393. CK_BBOOL cktrue = CK_TRUE;
  394. CK_OBJECT_CLASS objClass = CKO_PRIVATE_KEY;
  395. CK_SLOT_ID slotID;
  396. char *slotname = NULL;
  397. pphrase_arg_t *parg = NULL;
  398. attrs = theTemplate;
  399. /* FIXME: grok the various file types */
  400. slotID = 1; /* hardcoded for now */
  401. slotname = (char *)malloc(SLOTSIZE);
  402. snprintf(slotname, SLOTSIZE, "PEM Token #%ld", slotID);
  403. slot = PK11_FindSlotByName(slotname);
  404. free(slotname);
  405. if(!slot)
  406. return 0;
  407. PK11_SETATTRS(attrs, CKA_CLASS, &objClass, sizeof(objClass) ); attrs++;
  408. PK11_SETATTRS(attrs, CKA_TOKEN, &cktrue, sizeof(CK_BBOOL) ); attrs++;
  409. PK11_SETATTRS(attrs, CKA_LABEL, (unsigned char *)key_file,
  410. strlen(key_file)+1); attrs++;
  411. /* When adding an encrypted key the PKCS#11 will be set as removed */
  412. rv = PK11_CreateGenericObject(slot, theTemplate, 3, PR_FALSE /* isPerm */);
  413. if(rv == NULL) {
  414. PR_SetError(SEC_ERROR_BAD_KEY, 0);
  415. return 0;
  416. }
  417. /* This will force the token to be seen as re-inserted */
  418. SECMOD_WaitForAnyTokenEvent(mod, 0, 0);
  419. PK11_IsPresent(slot);
  420. parg = (pphrase_arg_t *) malloc(sizeof(*parg));
  421. parg->retryCount = 0;
  422. parg->data = conn->data;
  423. /* parg is initialized in nss_Init_Tokens() */
  424. if(PK11_Authenticate(slot, PR_TRUE, parg) != SECSuccess) {
  425. free(parg);
  426. return 0;
  427. }
  428. free(parg);
  429. return 1;
  430. #else
  431. /* If we don't have PK11_CreateGenericObject then we can't load a file-based
  432. * key.
  433. */
  434. (void)conn; /* unused */
  435. (void)key_file; /* unused */
  436. return 0;
  437. #endif
  438. }
  439. static int display_error(struct connectdata *conn, PRInt32 err,
  440. const char *filename)
  441. {
  442. switch(err) {
  443. case SEC_ERROR_BAD_PASSWORD:
  444. failf(conn->data, "Unable to load client key: Incorrect password");
  445. return 1;
  446. case SEC_ERROR_UNKNOWN_CERT:
  447. failf(conn->data, "Unable to load certificate %s", filename);
  448. return 1;
  449. default:
  450. break;
  451. }
  452. return 0; /* The caller will print a generic error */
  453. }
  454. static int cert_stuff(struct connectdata *conn, char *cert_file, char *key_file)
  455. {
  456. struct SessionHandle *data = conn->data;
  457. int rv = 0;
  458. if(cert_file) {
  459. rv = nss_load_cert(cert_file, PR_FALSE);
  460. if(!rv) {
  461. if(!display_error(conn, PR_GetError(), cert_file))
  462. failf(data, "Unable to load client cert %d.", PR_GetError());
  463. return 0;
  464. }
  465. }
  466. if(key_file || (is_file(cert_file))) {
  467. if(key_file)
  468. rv = nss_load_key(conn, key_file);
  469. else
  470. /* In case the cert file also has the key */
  471. rv = nss_load_key(conn, cert_file);
  472. if(!rv) {
  473. if(!display_error(conn, PR_GetError(), key_file))
  474. failf(data, "Unable to load client key %d.", PR_GetError());
  475. return 0;
  476. }
  477. }
  478. return 1;
  479. }
  480. static char * nss_get_password(PK11SlotInfo * slot, PRBool retry, void *arg)
  481. {
  482. pphrase_arg_t *parg;
  483. parg = (pphrase_arg_t *) arg;
  484. (void)slot; /* unused */
  485. if(retry > 2)
  486. return NULL;
  487. if(parg->data->set.str[STRING_KEY_PASSWD])
  488. return (char *)PORT_Strdup((char *)parg->data->set.str[STRING_KEY_PASSWD]);
  489. else
  490. return NULL;
  491. }
  492. /* No longer ask for the password, parg has been freed */
  493. static char * nss_no_password(PK11SlotInfo *slot, PRBool retry, void *arg)
  494. {
  495. (void)slot; /* unused */
  496. (void)retry; /* unused */
  497. (void)arg; /* unused */
  498. return NULL;
  499. }
  500. static SECStatus nss_Init_Tokens(struct connectdata * conn)
  501. {
  502. PK11SlotList *slotList;
  503. PK11SlotListElement *listEntry;
  504. SECStatus ret, status = SECSuccess;
  505. pphrase_arg_t *parg = NULL;
  506. parg = (pphrase_arg_t *) malloc(sizeof(*parg));
  507. parg->retryCount = 0;
  508. parg->data = conn->data;
  509. PK11_SetPasswordFunc(nss_get_password);
  510. slotList =
  511. PK11_GetAllTokens(CKM_INVALID_MECHANISM, PR_FALSE, PR_TRUE, NULL);
  512. for(listEntry = PK11_GetFirstSafe(slotList);
  513. listEntry; listEntry = listEntry->next) {
  514. PK11SlotInfo *slot = listEntry->slot;
  515. if(PK11_NeedLogin(slot) && PK11_NeedUserInit(slot)) {
  516. if(slot == PK11_GetInternalKeySlot()) {
  517. failf(conn->data, "The NSS database has not been initialized");
  518. }
  519. else {
  520. failf(conn->data, "The token %s has not been initialized",
  521. PK11_GetTokenName(slot));
  522. }
  523. PK11_FreeSlot(slot);
  524. continue;
  525. }
  526. ret = PK11_Authenticate(slot, PR_TRUE, parg);
  527. if(SECSuccess != ret) {
  528. if(PR_GetError() == SEC_ERROR_BAD_PASSWORD)
  529. infof(conn->data, "The password for token '%s' is incorrect\n",
  530. PK11_GetTokenName(slot));
  531. status = SECFailure;
  532. break;
  533. }
  534. parg->retryCount = 0; /* reset counter to 0 for the next token */
  535. PK11_FreeSlot(slot);
  536. }
  537. free(parg);
  538. return status;
  539. }
  540. static SECStatus BadCertHandler(void *arg, PRFileDesc *sock)
  541. {
  542. SECStatus success = SECSuccess;
  543. struct connectdata *conn = (struct connectdata *)arg;
  544. PRErrorCode err = PR_GetError();
  545. CERTCertificate *cert = NULL;
  546. char *subject, *issuer;
  547. if(conn->data->set.ssl.certverifyresult!=0)
  548. return success;
  549. conn->data->set.ssl.certverifyresult=err;
  550. cert = SSL_PeerCertificate(sock);
  551. subject = CERT_NameToAscii(&cert->subject);
  552. issuer = CERT_NameToAscii(&cert->issuer);
  553. CERT_DestroyCertificate(cert);
  554. switch(err) {
  555. case SEC_ERROR_CA_CERT_INVALID:
  556. infof(conn->data, "Issuer certificate is invalid: '%s'\n", issuer);
  557. if(conn->data->set.ssl.verifypeer)
  558. success = SECFailure;
  559. break;
  560. case SEC_ERROR_UNTRUSTED_ISSUER:
  561. if(conn->data->set.ssl.verifypeer)
  562. success = SECFailure;
  563. infof(conn->data, "Certificate is signed by an untrusted issuer: '%s'\n",
  564. issuer);
  565. break;
  566. case SSL_ERROR_BAD_CERT_DOMAIN:
  567. if(conn->data->set.ssl.verifypeer)
  568. success = SECFailure;
  569. infof(conn->data, "common name: %s (does not match '%s')\n",
  570. subject, conn->host.dispname);
  571. break;
  572. case SEC_ERROR_EXPIRED_CERTIFICATE:
  573. if(conn->data->set.ssl.verifypeer)
  574. success = SECFailure;
  575. infof(conn->data, "Remote Certificate has expired.\n");
  576. break;
  577. default:
  578. if(conn->data->set.ssl.verifypeer)
  579. success = SECFailure;
  580. infof(conn->data, "Bad certificate received. Subject = '%s', "
  581. "Issuer = '%s'\n", subject, issuer);
  582. break;
  583. }
  584. if(success == SECSuccess)
  585. infof(conn->data, "SSL certificate verify ok.\n");
  586. PR_Free(subject);
  587. PR_Free(issuer);
  588. return success;
  589. }
  590. /**
  591. * Inform the application that the handshake is complete.
  592. */
  593. static SECStatus HandshakeCallback(PRFileDesc *sock, void *arg)
  594. {
  595. (void)sock;
  596. (void)arg;
  597. return SECSuccess;
  598. }
  599. static void display_conn_info(struct connectdata *conn, PRFileDesc *sock)
  600. {
  601. SSLChannelInfo channel;
  602. SSLCipherSuiteInfo suite;
  603. CERTCertificate *cert;
  604. char *subject, *issuer, *common_name;
  605. PRExplodedTime printableTime;
  606. char timeString[256];
  607. PRTime notBefore, notAfter;
  608. if(SSL_GetChannelInfo(sock, &channel, sizeof channel) ==
  609. SECSuccess && channel.length == sizeof channel &&
  610. channel.cipherSuite) {
  611. if(SSL_GetCipherSuiteInfo(channel.cipherSuite,
  612. &suite, sizeof suite) == SECSuccess) {
  613. infof(conn->data, "SSL connection using %s\n", suite.cipherSuiteName);
  614. }
  615. }
  616. infof(conn->data, "Server certificate:\n");
  617. cert = SSL_PeerCertificate(sock);
  618. subject = CERT_NameToAscii(&cert->subject);
  619. issuer = CERT_NameToAscii(&cert->issuer);
  620. common_name = CERT_GetCommonName(&cert->subject);
  621. infof(conn->data, "\tsubject: %s\n", subject);
  622. CERT_GetCertTimes(cert, &notBefore, &notAfter);
  623. PR_ExplodeTime(notBefore, PR_GMTParameters, &printableTime);
  624. PR_FormatTime(timeString, 256, "%b %d %H:%M:%S %Y GMT", &printableTime);
  625. infof(conn->data, "\tstart date: %s\n", timeString);
  626. PR_ExplodeTime(notAfter, PR_GMTParameters, &printableTime);
  627. PR_FormatTime(timeString, 256, "%b %d %H:%M:%S %Y GMT", &printableTime);
  628. infof(conn->data, "\texpire date: %s\n", timeString);
  629. infof(conn->data, "\tcommon name: %s\n", common_name);
  630. infof(conn->data, "\tissuer: %s\n", issuer);
  631. PR_Free(subject);
  632. PR_Free(issuer);
  633. PR_Free(common_name);
  634. CERT_DestroyCertificate(cert);
  635. return;
  636. }
  637. /**
  638. *
  639. * Check that the Peer certificate's issuer certificate matches the one found
  640. * by issuer_nickname. This is not exactly the way OpenSSL and GNU TLS do the
  641. * issuer check, so we provide comments that mimic the OpenSSL
  642. * X509_check_issued function (in x509v3/v3_purp.c)
  643. */
  644. static SECStatus check_issuer_cert(PRFileDesc *sock,
  645. char* issuer_nickname)
  646. {
  647. CERTCertificate *cert,*cert_issuer,*issuer;
  648. SECStatus res=SECSuccess;
  649. void *proto_win = NULL;
  650. /*
  651. PRArenaPool *tmpArena = NULL;
  652. CERTAuthKeyID *authorityKeyID = NULL;
  653. SECITEM *caname = NULL;
  654. */
  655. cert = SSL_PeerCertificate(sock);
  656. cert_issuer = CERT_FindCertIssuer(cert,PR_Now(),certUsageObjectSigner);
  657. proto_win = SSL_RevealPinArg(sock);
  658. issuer = NULL;
  659. issuer = PK11_FindCertFromNickname(issuer_nickname, proto_win);
  660. if ((!cert_issuer) || (!issuer))
  661. res = SECFailure;
  662. else if (SECITEM_CompareItem(&cert_issuer->derCert,
  663. &issuer->derCert)!=SECEqual)
  664. res = SECFailure;
  665. CERT_DestroyCertificate(cert);
  666. CERT_DestroyCertificate(issuer);
  667. CERT_DestroyCertificate(cert_issuer);
  668. return res;
  669. }
  670. /**
  671. *
  672. * Callback to pick the SSL client certificate.
  673. */
  674. static SECStatus SelectClientCert(void *arg, PRFileDesc *sock,
  675. struct CERTDistNamesStr *caNames,
  676. struct CERTCertificateStr **pRetCert,
  677. struct SECKEYPrivateKeyStr **pRetKey)
  678. {
  679. CERTCertificate *cert;
  680. SECKEYPrivateKey *privKey;
  681. char *nickname = (char *)arg;
  682. void *proto_win = NULL;
  683. SECStatus secStatus = SECFailure;
  684. PK11SlotInfo *slot;
  685. (void)caNames;
  686. proto_win = SSL_RevealPinArg(sock);
  687. if(!nickname)
  688. return secStatus;
  689. cert = PK11_FindCertFromNickname(nickname, proto_win);
  690. if(cert) {
  691. if(!strncmp(nickname, "PEM Token", 9)) {
  692. CK_SLOT_ID slotID = 1; /* hardcoded for now */
  693. char * slotname = (char *)malloc(SLOTSIZE);
  694. snprintf(slotname, SLOTSIZE, "PEM Token #%ld", slotID);
  695. slot = PK11_FindSlotByName(slotname);
  696. privKey = PK11_FindPrivateKeyFromCert(slot, cert, NULL);
  697. PK11_FreeSlot(slot);
  698. free(slotname);
  699. if(privKey) {
  700. secStatus = SECSuccess;
  701. }
  702. }
  703. else {
  704. privKey = PK11_FindKeyByAnyCert(cert, proto_win);
  705. if(privKey)
  706. secStatus = SECSuccess;
  707. }
  708. }
  709. if(secStatus == SECSuccess) {
  710. *pRetCert = cert;
  711. *pRetKey = privKey;
  712. }
  713. else {
  714. if(cert)
  715. CERT_DestroyCertificate(cert);
  716. }
  717. return secStatus;
  718. }
  719. /**
  720. * Global SSL init
  721. *
  722. * @retval 0 error initializing SSL
  723. * @retval 1 SSL initialized successfully
  724. */
  725. int Curl_nss_init(void)
  726. {
  727. if(!initialized)
  728. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
  729. /* We will actually initialize NSS later */
  730. return 1;
  731. }
  732. /* Global cleanup */
  733. void Curl_nss_cleanup(void)
  734. {
  735. NSS_Shutdown();
  736. initialized = 0;
  737. }
  738. /*
  739. * This function uses SSL_peek to determine connection status.
  740. *
  741. * Return codes:
  742. * 1 means the connection is still in place
  743. * 0 means the connection has been closed
  744. * -1 means the connection status is unknown
  745. */
  746. int
  747. Curl_nss_check_cxn(struct connectdata *conn)
  748. {
  749. int rc;
  750. char buf;
  751. rc =
  752. PR_Recv(conn->ssl[FIRSTSOCKET].handle, (void *)&buf, 1, PR_MSG_PEEK,
  753. PR_SecondsToInterval(1));
  754. if(rc > 0)
  755. return 1; /* connection still in place */
  756. if(rc == 0)
  757. return 0; /* connection has been closed */
  758. return -1; /* connection status unknown */
  759. }
  760. /*
  761. * This function is called when an SSL connection is closed.
  762. */
  763. void Curl_nss_close(struct connectdata *conn, int sockindex)
  764. {
  765. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  766. if(connssl->handle) {
  767. PR_Close(connssl->handle);
  768. if(connssl->client_nickname != NULL) {
  769. free(connssl->client_nickname);
  770. connssl->client_nickname = NULL;
  771. }
  772. connssl->handle = NULL;
  773. }
  774. }
  775. /*
  776. * This function is called when the 'data' struct is going away. Close
  777. * down everything and free all resources!
  778. */
  779. int Curl_nss_close_all(struct SessionHandle *data)
  780. {
  781. (void)data;
  782. return 0;
  783. }
  784. CURLcode Curl_nss_connect(struct connectdata *conn, int sockindex)
  785. {
  786. PRInt32 err;
  787. PRFileDesc *model = NULL;
  788. PRBool ssl2, ssl3, tlsv1;
  789. struct SessionHandle *data = conn->data;
  790. curl_socket_t sockfd = conn->sock[sockindex];
  791. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  792. SECStatus rv;
  793. #ifdef HAVE_PK11_CREATEGENERICOBJECT
  794. char *configstring = NULL;
  795. #endif
  796. char *certDir = NULL;
  797. int curlerr;
  798. curlerr = CURLE_SSL_CONNECT_ERROR;
  799. if (connssl->state == ssl_connection_complete)
  800. return CURLE_OK;
  801. /* FIXME. NSS doesn't support multiple databases open at the same time. */
  802. if(!initialized) {
  803. initialized = 1;
  804. certDir = getenv("SSL_DIR"); /* Look in $SSL_DIR */
  805. if(!certDir) {
  806. struct stat st;
  807. if(stat(SSL_DIR, &st) == 0)
  808. if(S_ISDIR(st.st_mode)) {
  809. certDir = (char *)SSL_DIR;
  810. }
  811. }
  812. if(!certDir) {
  813. rv = NSS_NoDB_Init(NULL);
  814. }
  815. else {
  816. rv = NSS_Initialize(certDir, NULL, NULL, "secmod.db",
  817. NSS_INIT_READONLY);
  818. }
  819. if(rv != SECSuccess) {
  820. infof(conn->data, "Unable to initialize NSS database\n");
  821. curlerr = CURLE_SSL_CACERT_BADFILE;
  822. goto error;
  823. }
  824. NSS_SetDomesticPolicy();
  825. #ifdef HAVE_PK11_CREATEGENERICOBJECT
  826. configstring = (char *)malloc(PATH_MAX);
  827. PR_snprintf(configstring, PATH_MAX, "library=%s name=PEM", pem_library);
  828. mod = SECMOD_LoadUserModule(configstring, NULL, PR_FALSE);
  829. free(configstring);
  830. if(!mod || !mod->loaded) {
  831. if(mod) {
  832. SECMOD_DestroyModule(mod);
  833. mod = NULL;
  834. }
  835. infof(data, "WARNING: failed to load NSS PEM library %s. Using OpenSSL "
  836. "PEM certificates will not work.\n", pem_library);
  837. }
  838. #endif
  839. }
  840. model = PR_NewTCPSocket();
  841. if(!model)
  842. goto error;
  843. model = SSL_ImportFD(NULL, model);
  844. if(SSL_OptionSet(model, SSL_SECURITY, PR_TRUE) != SECSuccess)
  845. goto error;
  846. if(SSL_OptionSet(model, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) != SECSuccess)
  847. goto error;
  848. if(SSL_OptionSet(model, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) != SECSuccess)
  849. goto error;
  850. ssl2 = ssl3 = tlsv1 = PR_FALSE;
  851. switch (data->set.ssl.version) {
  852. default:
  853. case CURL_SSLVERSION_DEFAULT:
  854. ssl3 = tlsv1 = PR_TRUE;
  855. break;
  856. case CURL_SSLVERSION_TLSv1:
  857. tlsv1 = PR_TRUE;
  858. break;
  859. case CURL_SSLVERSION_SSLv2:
  860. ssl2 = PR_TRUE;
  861. break;
  862. case CURL_SSLVERSION_SSLv3:
  863. ssl3 = PR_TRUE;
  864. break;
  865. }
  866. if(SSL_OptionSet(model, SSL_ENABLE_SSL2, ssl2) != SECSuccess)
  867. goto error;
  868. if(SSL_OptionSet(model, SSL_ENABLE_SSL3, ssl3) != SECSuccess)
  869. goto error;
  870. if(SSL_OptionSet(model, SSL_ENABLE_TLS, tlsv1) != SECSuccess)
  871. goto error;
  872. if(SSL_OptionSet(model, SSL_V2_COMPATIBLE_HELLO, ssl2) != SECSuccess)
  873. goto error;
  874. if(data->set.ssl.cipher_list) {
  875. if(set_ciphers(data, model, data->set.ssl.cipher_list) != SECSuccess) {
  876. curlerr = CURLE_SSL_CIPHER;
  877. goto error;
  878. }
  879. }
  880. data->set.ssl.certverifyresult=0; /* not checked yet */
  881. if(SSL_BadCertHook(model, (SSLBadCertHandler) BadCertHandler, conn)
  882. != SECSuccess) {
  883. goto error;
  884. }
  885. if(SSL_HandshakeCallback(model, (SSLHandshakeCallback) HandshakeCallback,
  886. NULL) != SECSuccess)
  887. goto error;
  888. if(!data->set.ssl.verifypeer)
  889. /* skip the verifying of the peer */
  890. ;
  891. else if(data->set.ssl.CAfile) {
  892. int rc = nss_load_cert(data->set.ssl.CAfile, PR_TRUE);
  893. if(!rc) {
  894. curlerr = CURLE_SSL_CACERT_BADFILE;
  895. goto error;
  896. }
  897. }
  898. else if(data->set.ssl.CApath) {
  899. struct stat st;
  900. PRDir *dir;
  901. PRDirEntry *entry;
  902. if(stat(data->set.ssl.CApath, &st) == -1) {
  903. curlerr = CURLE_SSL_CACERT_BADFILE;
  904. goto error;
  905. }
  906. if(S_ISDIR(st.st_mode)) {
  907. int rc;
  908. dir = PR_OpenDir(data->set.ssl.CApath);
  909. do {
  910. entry = PR_ReadDir(dir, PR_SKIP_BOTH | PR_SKIP_HIDDEN);
  911. if(entry) {
  912. char fullpath[PATH_MAX];
  913. snprintf(fullpath, sizeof(fullpath), "%s/%s", data->set.ssl.CApath,
  914. entry->name);
  915. rc = nss_load_cert(fullpath, PR_TRUE);
  916. /* FIXME: check this return value! */
  917. }
  918. /* This is purposefully tolerant of errors so non-PEM files
  919. * can be in the same directory */
  920. } while(entry != NULL);
  921. PR_CloseDir(dir);
  922. }
  923. }
  924. infof(data,
  925. " CAfile: %s\n"
  926. " CApath: %s\n",
  927. data->set.ssl.CAfile ? data->set.ssl.CAfile : "none",
  928. data->set.ssl.CApath ? data->set.ssl.CApath : "none");
  929. if (data->set.ssl.CRLfile) {
  930. int rc = nss_load_crl(data->set.ssl.CRLfile, PR_FALSE);
  931. if (!rc) {
  932. curlerr = CURLE_SSL_CRL_BADFILE;
  933. goto error;
  934. }
  935. infof(data,
  936. " CRLfile: %s\n",
  937. data->set.ssl.CRLfile ? data->set.ssl.CRLfile : "none");
  938. }
  939. if(data->set.str[STRING_CERT]) {
  940. char *n;
  941. char *nickname;
  942. nickname = (char *)malloc(PATH_MAX);
  943. if(is_file(data->set.str[STRING_CERT])) {
  944. n = strrchr(data->set.str[STRING_CERT], '/');
  945. if(n) {
  946. n++; /* skip last slash */
  947. snprintf(nickname, PATH_MAX, "PEM Token #%ld:%s", 1, n);
  948. }
  949. }
  950. else {
  951. strncpy(nickname, data->set.str[STRING_CERT], PATH_MAX);
  952. nickname[PATH_MAX-1]=0; /* make sure this is zero terminated */
  953. }
  954. if(nss_Init_Tokens(conn) != SECSuccess) {
  955. free(nickname);
  956. goto error;
  957. }
  958. if(!cert_stuff(conn, data->set.str[STRING_CERT],
  959. data->set.str[STRING_KEY])) {
  960. /* failf() is already done in cert_stuff() */
  961. free(nickname);
  962. return CURLE_SSL_CERTPROBLEM;
  963. }
  964. connssl->client_nickname = strdup(nickname);
  965. if(SSL_GetClientAuthDataHook(model,
  966. (SSLGetClientAuthData) SelectClientCert,
  967. (void *)connssl) !=
  968. SECSuccess) {
  969. curlerr = CURLE_SSL_CERTPROBLEM;
  970. goto error;
  971. }
  972. free(nickname);
  973. PK11_SetPasswordFunc(nss_no_password);
  974. }
  975. else
  976. connssl->client_nickname = NULL;
  977. /* Import our model socket onto the existing file descriptor */
  978. connssl->handle = PR_ImportTCPSocket(sockfd);
  979. connssl->handle = SSL_ImportFD(model, connssl->handle);
  980. if(!connssl->handle)
  981. goto error;
  982. PR_Close(model); /* We don't need this any more */
  983. /* Force handshake on next I/O */
  984. SSL_ResetHandshake(connssl->handle, /* asServer */ PR_FALSE);
  985. SSL_SetURL(connssl->handle, conn->host.name);
  986. /* Force the handshake now */
  987. if(SSL_ForceHandshakeWithTimeout(connssl->handle,
  988. PR_SecondsToInterval(HANDSHAKE_TIMEOUT))
  989. != SECSuccess) {
  990. if(conn->data->set.ssl.certverifyresult!=0)
  991. curlerr = CURLE_SSL_CACERT;
  992. goto error;
  993. }
  994. connssl->state = ssl_connection_complete;
  995. display_conn_info(conn, connssl->handle);
  996. if (data->set.str[STRING_SSL_ISSUERCERT]) {
  997. char *n;
  998. char *nickname;
  999. nickname = (char *)malloc(PATH_MAX);
  1000. if(is_file(data->set.str[STRING_SSL_ISSUERCERT])) {
  1001. n = strrchr(data->set.str[STRING_SSL_ISSUERCERT], '/');
  1002. if (n) {
  1003. n++; /* skip last slash */
  1004. snprintf(nickname, PATH_MAX, "PEM Token #%ld:%s", 1, n);
  1005. }
  1006. }
  1007. else {
  1008. strncpy(nickname, data->set.str[STRING_SSL_ISSUERCERT], PATH_MAX);
  1009. nickname[PATH_MAX-1]=0; /* make sure this is zero terminated */
  1010. }
  1011. if (check_issuer_cert(connssl->handle, nickname) == SECFailure) {
  1012. infof(data,"SSL certificate issuer check failed\n");
  1013. free(nickname);
  1014. curlerr = CURLE_SSL_ISSUER_ERROR;
  1015. goto error;
  1016. }
  1017. else {
  1018. infof(data, "SSL certificate issuer check ok\n");
  1019. }
  1020. }
  1021. return CURLE_OK;
  1022. error:
  1023. err = PR_GetError();
  1024. infof(data, "NSS error %d\n", err);
  1025. if(model)
  1026. PR_Close(model);
  1027. return curlerr;
  1028. }
  1029. /* return number of sent (non-SSL) bytes */
  1030. int Curl_nss_send(struct connectdata *conn, /* connection data */
  1031. int sockindex, /* socketindex */
  1032. const void *mem, /* send this data */
  1033. size_t len) /* amount to write */
  1034. {
  1035. PRInt32 err;
  1036. struct SessionHandle *data = conn->data;
  1037. PRInt32 timeout;
  1038. int rc;
  1039. if(data->set.timeout)
  1040. timeout = PR_MillisecondsToInterval(data->set.timeout);
  1041. else
  1042. timeout = PR_MillisecondsToInterval(DEFAULT_CONNECT_TIMEOUT);
  1043. rc = PR_Send(conn->ssl[sockindex].handle, mem, (int)len, 0, timeout);
  1044. if(rc < 0) {
  1045. err = PR_GetError();
  1046. if(err == PR_IO_TIMEOUT_ERROR) {
  1047. failf(data, "SSL connection timeout");
  1048. return CURLE_OPERATION_TIMEDOUT;
  1049. }
  1050. failf(conn->data, "SSL write: error %d", err);
  1051. return -1;
  1052. }
  1053. return rc; /* number of bytes */
  1054. }
  1055. /*
  1056. * If the read would block we return -1 and set 'wouldblock' to TRUE.
  1057. * Otherwise we return the amount of data read. Other errors should return -1
  1058. * and set 'wouldblock' to FALSE.
  1059. */
  1060. ssize_t Curl_nss_recv(struct connectdata * conn, /* connection data */
  1061. int num, /* socketindex */
  1062. char *buf, /* store read data here */
  1063. size_t buffersize, /* max amount to read */
  1064. bool * wouldblock)
  1065. {
  1066. ssize_t nread;
  1067. struct SessionHandle *data = conn->data;
  1068. PRInt32 timeout;
  1069. if(data->set.timeout)
  1070. timeout = PR_SecondsToInterval(data->set.timeout);
  1071. else
  1072. timeout = PR_MillisecondsToInterval(DEFAULT_CONNECT_TIMEOUT);
  1073. nread = PR_Recv(conn->ssl[num].handle, buf, (int)buffersize, 0, timeout);
  1074. *wouldblock = FALSE;
  1075. if(nread < 0) {
  1076. /* failed SSL read */
  1077. PRInt32 err = PR_GetError();
  1078. if(err == PR_WOULD_BLOCK_ERROR) {
  1079. *wouldblock = TRUE;
  1080. return -1; /* basically EWOULDBLOCK */
  1081. }
  1082. if(err == PR_IO_TIMEOUT_ERROR) {
  1083. failf(data, "SSL connection timeout");
  1084. return CURLE_OPERATION_TIMEDOUT;
  1085. }
  1086. failf(conn->data, "SSL read: errno %d", err);
  1087. return -1;
  1088. }
  1089. return nread;
  1090. }
  1091. size_t Curl_nss_version(char *buffer, size_t size)
  1092. {
  1093. return snprintf(buffer, size, "NSS/%s", NSS_VERSION);
  1094. }
  1095. #endif /* USE_NSS */