1
0

ocsp.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. /* ocsp.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* Name change compatibility layer no longer needs to be included here */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. /*
  27. * WOLFSSL_NO_OCSP_ISSUER_CHAIN_CHECK:
  28. * Disable looking for an authorized responder in the verification path of
  29. * the issuer. This will make the authorized responder only look at the
  30. * OCSP response signer and direct issuer.
  31. */
  32. #ifndef WOLFCRYPT_ONLY
  33. #ifdef HAVE_OCSP
  34. #include <wolfssl/error-ssl.h>
  35. #include <wolfssl/ocsp.h>
  36. #include <wolfssl/internal.h>
  37. #ifdef NO_INLINE
  38. #include <wolfssl/wolfcrypt/misc.h>
  39. #else
  40. #define WOLFSSL_MISC_INCLUDED
  41. #include <wolfcrypt/src/misc.c>
  42. #endif
  43. int InitOCSP(WOLFSSL_OCSP* ocsp, WOLFSSL_CERT_MANAGER* cm)
  44. {
  45. WOLFSSL_ENTER("InitOCSP");
  46. ForceZero(ocsp, sizeof(WOLFSSL_OCSP));
  47. if (wc_InitMutex(&ocsp->ocspLock) != 0)
  48. return BAD_MUTEX_E;
  49. ocsp->cm = cm;
  50. return 0;
  51. }
  52. static int InitOcspEntry(OcspEntry* entry, OcspRequest* request)
  53. {
  54. WOLFSSL_ENTER("InitOcspEntry");
  55. ForceZero(entry, sizeof(OcspEntry));
  56. XMEMCPY(entry->issuerHash, request->issuerHash, OCSP_DIGEST_SIZE);
  57. XMEMCPY(entry->issuerKeyHash, request->issuerKeyHash, OCSP_DIGEST_SIZE);
  58. return 0;
  59. }
  60. static void FreeOcspEntry(OcspEntry* entry, void* heap)
  61. {
  62. CertStatus *status, *next;
  63. if (entry == NULL || !entry->ownStatus)
  64. return;
  65. WOLFSSL_ENTER("FreeOcspEntry");
  66. for (status = entry->status; status; status = next) {
  67. next = status->next;
  68. if (status->rawOcspResponse)
  69. XFREE(status->rawOcspResponse, heap, DYNAMIC_TYPE_OCSP_STATUS);
  70. #ifdef OPENSSL_EXTRA
  71. if (status->serialInt) {
  72. if (status->serialInt->isDynamic) {
  73. XFREE(status->serialInt->data, NULL, DYNAMIC_TYPE_OPENSSL);
  74. }
  75. XFREE(status->serialInt, NULL, DYNAMIC_TYPE_OPENSSL);
  76. }
  77. status->serialInt = NULL;
  78. #endif
  79. XFREE(status, heap, DYNAMIC_TYPE_OCSP_STATUS);
  80. }
  81. (void)heap;
  82. }
  83. void FreeOCSP(WOLFSSL_OCSP* ocsp, int dynamic)
  84. {
  85. OcspEntry *entry, *next;
  86. WOLFSSL_ENTER("FreeOCSP");
  87. for (entry = ocsp->ocspList; entry; entry = next) {
  88. next = entry->next;
  89. FreeOcspEntry(entry, ocsp->cm->heap);
  90. XFREE(entry, ocsp->cm->heap, DYNAMIC_TYPE_OCSP_ENTRY);
  91. }
  92. wc_FreeMutex(&ocsp->ocspLock);
  93. if (dynamic)
  94. XFREE(ocsp, ocsp->cm->heap, DYNAMIC_TYPE_OCSP);
  95. }
  96. static int xstat2err(int st)
  97. {
  98. switch (st) {
  99. case CERT_GOOD:
  100. return 0;
  101. case CERT_REVOKED:
  102. return OCSP_CERT_REVOKED;
  103. default:
  104. return OCSP_CERT_UNKNOWN;
  105. }
  106. }
  107. int CheckCertOCSP_ex(WOLFSSL_OCSP* ocsp, DecodedCert* cert, WOLFSSL* ssl)
  108. {
  109. int ret = OCSP_LOOKUP_FAIL;
  110. #ifdef WOLFSSL_SMALL_STACK
  111. OcspRequest* ocspRequest;
  112. #else
  113. OcspRequest ocspRequest[1];
  114. #endif
  115. WOLFSSL_ENTER("CheckCertOCSP");
  116. #ifdef WOLFSSL_SMALL_STACK
  117. ocspRequest = (OcspRequest*)XMALLOC(sizeof(OcspRequest), NULL,
  118. DYNAMIC_TYPE_TMP_BUFFER);
  119. if (ocspRequest == NULL) {
  120. WOLFSSL_LEAVE("CheckCertOCSP", MEMORY_ERROR);
  121. return MEMORY_E;
  122. }
  123. #endif
  124. if (InitOcspRequest(ocspRequest, cert, ocsp->cm->ocspSendNonce,
  125. ocsp->cm->heap) == 0) {
  126. ocspRequest->ssl = ssl;
  127. ret = CheckOcspRequest(ocsp, ocspRequest, NULL, NULL);
  128. FreeOcspRequest(ocspRequest);
  129. }
  130. #ifdef WOLFSSL_SMALL_STACK
  131. XFREE(ocspRequest, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  132. #endif
  133. WOLFSSL_LEAVE("CheckCertOCSP", ret);
  134. return ret;
  135. }
  136. int CheckCertOCSP(WOLFSSL_OCSP* ocsp, DecodedCert* cert)
  137. {
  138. return CheckCertOCSP_ex(ocsp, cert, NULL);
  139. }
  140. static int GetOcspEntry(WOLFSSL_OCSP* ocsp, OcspRequest* request,
  141. OcspEntry** entry)
  142. {
  143. WOLFSSL_ENTER("GetOcspEntry");
  144. *entry = NULL;
  145. if (wc_LockMutex(&ocsp->ocspLock) != 0) {
  146. WOLFSSL_LEAVE("CheckCertOCSP", BAD_MUTEX_E);
  147. return BAD_MUTEX_E;
  148. }
  149. for (*entry = ocsp->ocspList; *entry; *entry = (*entry)->next)
  150. if (XMEMCMP((*entry)->issuerHash, request->issuerHash,
  151. OCSP_DIGEST_SIZE) == 0
  152. && XMEMCMP((*entry)->issuerKeyHash, request->issuerKeyHash,
  153. OCSP_DIGEST_SIZE) == 0)
  154. break;
  155. if (*entry == NULL) {
  156. *entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry),
  157. ocsp->cm->heap, DYNAMIC_TYPE_OCSP_ENTRY);
  158. if (*entry) {
  159. InitOcspEntry(*entry, request);
  160. (*entry)->next = ocsp->ocspList;
  161. ocsp->ocspList = *entry;
  162. }
  163. }
  164. wc_UnLockMutex(&ocsp->ocspLock);
  165. return *entry ? 0 : MEMORY_ERROR;
  166. }
  167. /* Mallocs responseBuffer->buffer and is up to caller to free on success
  168. *
  169. * Returns OCSP status
  170. */
  171. static int GetOcspStatus(WOLFSSL_OCSP* ocsp, OcspRequest* request,
  172. OcspEntry* entry, CertStatus** status, buffer* responseBuffer,
  173. void* heap)
  174. {
  175. int ret = OCSP_INVALID_STATUS;
  176. WOLFSSL_ENTER("GetOcspStatus");
  177. (void)heap;
  178. *status = NULL;
  179. if (wc_LockMutex(&ocsp->ocspLock) != 0) {
  180. WOLFSSL_LEAVE("CheckCertOCSP", BAD_MUTEX_E);
  181. return BAD_MUTEX_E;
  182. }
  183. for (*status = entry->status; *status; *status = (*status)->next)
  184. if ((*status)->serialSz == request->serialSz
  185. && !XMEMCMP((*status)->serial, request->serial, (*status)->serialSz))
  186. break;
  187. if (responseBuffer && *status && !(*status)->rawOcspResponse) {
  188. /* force fetching again */
  189. ret = OCSP_INVALID_STATUS;
  190. }
  191. else if (*status) {
  192. #ifndef NO_ASN_TIME
  193. if (XVALIDATE_DATE((*status)->thisDate,
  194. (*status)->thisDateFormat, BEFORE)
  195. && ((*status)->nextDate[0] != 0)
  196. && XVALIDATE_DATE((*status)->nextDate,
  197. (*status)->nextDateFormat, AFTER))
  198. #endif
  199. {
  200. ret = xstat2err((*status)->status);
  201. if (responseBuffer) {
  202. responseBuffer->buffer = (byte*)XMALLOC(
  203. (*status)->rawOcspResponseSz, heap,
  204. DYNAMIC_TYPE_TMP_BUFFER);
  205. if (responseBuffer->buffer) {
  206. responseBuffer->length = (*status)->rawOcspResponseSz;
  207. XMEMCPY(responseBuffer->buffer,
  208. (*status)->rawOcspResponse,
  209. (*status)->rawOcspResponseSz);
  210. }
  211. }
  212. }
  213. }
  214. wc_UnLockMutex(&ocsp->ocspLock);
  215. return ret;
  216. }
  217. /* Check that the response for validity. Store result in status.
  218. *
  219. * ocsp Context object for OCSP status.
  220. * response OCSP response message data.
  221. * responseSz Length of OCSP response message data.
  222. * reponseBuffer Buffer object to return the response with.
  223. * status The certificate status object.
  224. * entry The OCSP entry for this certificate.
  225. * ocspRequest Request corresponding to response.
  226. * heap Heap hint used for responseBuffer
  227. * returns OCSP_LOOKUP_FAIL when the response is bad and 0 otherwise.
  228. */
  229. int CheckOcspResponse(WOLFSSL_OCSP *ocsp, byte *response, int responseSz,
  230. WOLFSSL_BUFFER_INFO *responseBuffer, CertStatus *status,
  231. OcspEntry *entry, OcspRequest *ocspRequest, void* heap)
  232. {
  233. #ifdef WOLFSSL_SMALL_STACK
  234. CertStatus* newStatus;
  235. OcspEntry* newSingle;
  236. OcspResponse* ocspResponse;
  237. #else
  238. CertStatus newStatus[1];
  239. OcspEntry newSingle[1];
  240. OcspResponse ocspResponse[1];
  241. #endif
  242. int ret;
  243. int validated = 0; /* ocsp validation flag */
  244. (void)heap;
  245. #ifdef WOLFSSL_SMALL_STACK
  246. newStatus = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  247. DYNAMIC_TYPE_OCSP_STATUS);
  248. newSingle = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  249. DYNAMIC_TYPE_OCSP_ENTRY);
  250. ocspResponse = (OcspResponse*)XMALLOC(sizeof(OcspResponse), NULL,
  251. DYNAMIC_TYPE_OCSP_REQUEST);
  252. if (newStatus == NULL || newSingle == NULL || ocspResponse == NULL) {
  253. if (newStatus) XFREE(newStatus, NULL, DYNAMIC_TYPE_OCSP_STATUS);
  254. if (newSingle) XFREE(newSingle, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  255. if (ocspResponse) XFREE(ocspResponse, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  256. WOLFSSL_LEAVE("CheckCertOCSP", MEMORY_ERROR);
  257. return MEMORY_E;
  258. }
  259. #endif
  260. InitOcspResponse(ocspResponse, newSingle, newStatus, response, responseSz,
  261. ocsp->cm->heap);
  262. ret = OcspResponseDecode(ocspResponse, ocsp->cm, ocsp->cm->heap, 0);
  263. if (ret != 0) {
  264. ocsp->error = ret;
  265. WOLFSSL_LEAVE("OcspResponseDecode failed", ocsp->error);
  266. goto end;
  267. }
  268. if (ocspResponse->responseStatus != OCSP_SUCCESSFUL) {
  269. WOLFSSL_MSG("OcspResponse status bad");
  270. goto end;
  271. }
  272. if (ocspRequest != NULL) {
  273. /* Has the chance to bubble up response changing ocspResponse->single to
  274. no longer be pointing at newSingle */
  275. ret = CompareOcspReqResp(ocspRequest, ocspResponse);
  276. if (ret != 0) {
  277. goto end;
  278. }
  279. }
  280. if (responseBuffer) {
  281. responseBuffer->buffer = (byte*)XMALLOC(responseSz, heap,
  282. DYNAMIC_TYPE_TMP_BUFFER);
  283. if (responseBuffer->buffer) {
  284. responseBuffer->length = responseSz;
  285. XMEMCPY(responseBuffer->buffer, response, responseSz);
  286. }
  287. }
  288. ret = xstat2err(ocspResponse->single->status->status);
  289. if (ret == 0) {
  290. validated = 1;
  291. }
  292. if (wc_LockMutex(&ocsp->ocspLock) != 0) {
  293. ret = BAD_MUTEX_E;
  294. goto end;
  295. }
  296. if (status != NULL) {
  297. if (status->rawOcspResponse) {
  298. XFREE(status->rawOcspResponse, ocsp->cm->heap,
  299. DYNAMIC_TYPE_OCSP_STATUS);
  300. }
  301. /* Replace existing certificate entry with updated */
  302. ocspResponse->single->status->next = status->next;
  303. XMEMCPY(status, ocspResponse->single->status, sizeof(CertStatus));
  304. }
  305. else {
  306. /* Save new certificate entry */
  307. status = (CertStatus*)XMALLOC(sizeof(CertStatus),
  308. ocsp->cm->heap, DYNAMIC_TYPE_OCSP_STATUS);
  309. if (status != NULL) {
  310. XMEMCPY(status, ocspResponse->single->status, sizeof(CertStatus));
  311. status->next = entry->status;
  312. entry->status = status;
  313. entry->ownStatus = 1;
  314. entry->totalStatus++;
  315. }
  316. }
  317. if (status && responseBuffer && responseBuffer->buffer) {
  318. status->rawOcspResponse = (byte*)XMALLOC(responseBuffer->length,
  319. ocsp->cm->heap,
  320. DYNAMIC_TYPE_OCSP_STATUS);
  321. if (status->rawOcspResponse) {
  322. status->rawOcspResponseSz = responseBuffer->length;
  323. XMEMCPY(status->rawOcspResponse, responseBuffer->buffer,
  324. responseBuffer->length);
  325. }
  326. }
  327. wc_UnLockMutex(&ocsp->ocspLock);
  328. end:
  329. if (ret == 0 && validated == 1) {
  330. WOLFSSL_MSG("New OcspResponse validated");
  331. } else if ((ret == ocsp->error) && (ocspResponse->single->status->status == CERT_UNKNOWN)) {
  332. WOLFSSL_MSG("OCSP unknown");
  333. ret = OCSP_CERT_UNKNOWN;
  334. } else if (ret != OCSP_CERT_REVOKED) {
  335. WOLFSSL_MSG("OCSP lookup failure");
  336. ret = OCSP_LOOKUP_FAIL;
  337. }
  338. FreeOcspResponse(ocspResponse);
  339. #ifdef WOLFSSL_SMALL_STACK
  340. XFREE(newStatus, NULL, DYNAMIC_TYPE_OCSP_STATUS);
  341. XFREE(newSingle, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  342. XFREE(ocspResponse, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  343. #endif
  344. return ret;
  345. }
  346. /* 0 on success */
  347. /* allow user to override the maximum request size at build-time */
  348. #ifndef OCSP_MAX_REQUEST_SZ
  349. #define OCSP_MAX_REQUEST_SZ 2048
  350. #endif
  351. int CheckOcspRequest(WOLFSSL_OCSP* ocsp, OcspRequest* ocspRequest,
  352. buffer* responseBuffer, void* heap)
  353. {
  354. OcspEntry* entry = NULL;
  355. CertStatus* status = NULL;
  356. byte* request = NULL;
  357. int requestSz = OCSP_MAX_REQUEST_SZ;
  358. int responseSz = 0;
  359. byte* response = NULL;
  360. const char* url = NULL;
  361. int urlSz = 0;
  362. int ret = -1;
  363. WOLFSSL* ssl;
  364. void* ioCtx;
  365. WOLFSSL_ENTER("CheckOcspRequest");
  366. if (ocsp == NULL || ocspRequest == NULL)
  367. return BAD_FUNC_ARG;
  368. if (responseBuffer) {
  369. responseBuffer->buffer = NULL;
  370. responseBuffer->length = 0;
  371. }
  372. ret = GetOcspEntry(ocsp, ocspRequest, &entry);
  373. if (ret != 0)
  374. return ret;
  375. ret = GetOcspStatus(ocsp, ocspRequest, entry, &status, responseBuffer,
  376. heap);
  377. if (ret != OCSP_INVALID_STATUS)
  378. return ret;
  379. if (responseBuffer) {
  380. XFREE(responseBuffer->buffer, heap, DYNAMIC_TYPE_TMP_BUFFER);
  381. responseBuffer->buffer = NULL;
  382. }
  383. /* get SSL and IOCtx */
  384. ssl = (WOLFSSL*)ocspRequest->ssl;
  385. ioCtx = (ssl && ssl->ocspIOCtx != NULL) ?
  386. ssl->ocspIOCtx : ocsp->cm->ocspIOCtx;
  387. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  388. if (ocsp->statusCb != NULL && ssl != NULL) {
  389. WOLFSSL_MSG("Calling ocsp->statusCb");
  390. ret = ocsp->statusCb(ssl, ioCtx);
  391. switch (ret) {
  392. case SSL_TLSEXT_ERR_OK:
  393. ret = wolfSSL_get_ocsp_response(ssl, &response);
  394. ret = CheckOcspResponse(ocsp, response, ret, responseBuffer,
  395. status, entry, NULL, heap);
  396. if (response != NULL)
  397. XFREE(response, NULL, DYNAMIC_TYPE_OPENSSL);
  398. break;
  399. case SSL_TLSEXT_ERR_NOACK:
  400. ret = OCSP_LOOKUP_FAIL;
  401. break;
  402. case SSL_TLSEXT_ERR_ALERT_FATAL:
  403. default:
  404. WOLFSSL_LEAVE("CheckOcspRequest", ocsp->error);
  405. ret = WOLFSSL_FATAL_ERROR;
  406. break;
  407. }
  408. WOLFSSL_LEAVE("CheckOcspRequest", ret);
  409. return ret;
  410. }
  411. #endif
  412. if (ocsp->cm->ocspUseOverrideURL) {
  413. url = ocsp->cm->ocspOverrideURL;
  414. if (url != NULL && url[0] != '\0')
  415. urlSz = (int)XSTRLEN(url);
  416. else
  417. return OCSP_NEED_URL;
  418. }
  419. else if (ocspRequest->urlSz != 0 && ocspRequest->url != NULL) {
  420. url = (const char *)ocspRequest->url;
  421. urlSz = ocspRequest->urlSz;
  422. }
  423. else {
  424. /* cert doesn't have extAuthInfo, assuming CERT_GOOD */
  425. WOLFSSL_MSG("Cert has no OCSP URL, assuming CERT_GOOD");
  426. return 0;
  427. }
  428. request = (byte*)XMALLOC(requestSz, ocsp->cm->heap, DYNAMIC_TYPE_OCSP);
  429. if (request == NULL) {
  430. WOLFSSL_LEAVE("CheckCertOCSP", MEMORY_ERROR);
  431. return MEMORY_ERROR;
  432. }
  433. requestSz = EncodeOcspRequest(ocspRequest, request, requestSz);
  434. if (requestSz > 0 && ocsp->cm->ocspIOCb) {
  435. responseSz = ocsp->cm->ocspIOCb(ioCtx, url, urlSz,
  436. request, requestSz, &response);
  437. }
  438. if (responseSz == WOLFSSL_CBIO_ERR_WANT_READ) {
  439. ret = OCSP_WANT_READ;
  440. }
  441. XFREE(request, ocsp->cm->heap, DYNAMIC_TYPE_OCSP);
  442. if (responseSz >= 0 && response) {
  443. ret = CheckOcspResponse(ocsp, response, responseSz, responseBuffer, status,
  444. entry, ocspRequest, heap);
  445. }
  446. if (response != NULL && ocsp->cm->ocspRespFreeCb)
  447. ocsp->cm->ocspRespFreeCb(ioCtx, response);
  448. /* Keep responseBuffer in the case of getting to response check. Caller
  449. * should free responseBuffer after checking OCSP return value in "ret" */
  450. WOLFSSL_LEAVE("CheckOcspRequest", ret);
  451. return ret;
  452. }
  453. #ifndef WOLFSSL_NO_OCSP_ISSUER_CHAIN_CHECK
  454. static int CheckOcspResponderChain(OcspEntry* single, DecodedCert *cert,
  455. void* vp) {
  456. /* Attempt to build a chain up to cert's issuer */
  457. WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
  458. Signer* ca = NULL;
  459. Signer* prev = NULL;
  460. int passed = 0;
  461. /*
  462. * Relation between certs:
  463. * CA
  464. * / \
  465. * intermediate(s) cert in OCSP response
  466. * | with OCSP key usage ext
  467. * issuer of cert
  468. * in OCSP request
  469. */
  470. /* End loop if no more issuers found or if we have found a self
  471. * signed cert (ca == prev) */
  472. for (ca = GetCAByName(cm, single->issuerHash); ca != NULL && ca != prev;
  473. prev = ca, ca = GetCAByName(cm, ca->issuerNameHash)) {
  474. if (XMEMCMP(cert->issuerHash, ca->issuerNameHash,
  475. OCSP_DIGEST_SIZE) == 0) {
  476. WOLFSSL_MSG("\tOCSP Response signed by authorized "
  477. "responder delegated by issuer "
  478. "(found in chain)");
  479. passed = 1;
  480. break;
  481. }
  482. }
  483. return passed;
  484. }
  485. #endif
  486. /**
  487. * Enforce https://www.rfc-editor.org/rfc/rfc6960#section-4.2.2.2
  488. * @param bs The basic response to verify
  489. * @param cert The decoded bs->cert
  490. * @return
  491. */
  492. int CheckOcspResponder(OcspResponse *bs, DecodedCert *cert, void* vp)
  493. {
  494. int ret = 0;
  495. OcspEntry* single;
  496. /* Both evaluate to enum values so can't use a pre-processor check */
  497. WOLFSSL_ASSERT_EQ(OCSP_DIGEST_SIZE, SIGNER_DIGEST_SIZE);
  498. (void)vp;
  499. WOLFSSL_ENTER("CheckOcspResponder");
  500. /* In the future if this API is used more then it could be beneficial to
  501. * implement calling InitDecodedCert and ParseCertRelative here
  502. * automatically when cert == NULL. */
  503. if (bs == NULL || cert == NULL)
  504. return BAD_FUNC_ARG;
  505. /* Traverse the list and check that the cert has the authority to provide
  506. * an OCSP response for each entry. */
  507. for (single = bs->single; single != NULL; single = single->next) {
  508. int passed = 0;
  509. if (XMEMCMP(cert->subjectHash, single->issuerHash, OCSP_DIGEST_SIZE)
  510. == 0) {
  511. WOLFSSL_MSG("\tOCSP Response signed by issuer");
  512. passed = 1;
  513. }
  514. else if ((cert->extExtKeyUsage & EXTKEYUSE_OCSP_SIGN) != 0) {
  515. if (XMEMCMP(cert->issuerHash, single->issuerHash,
  516. OCSP_DIGEST_SIZE) == 0) {
  517. WOLFSSL_MSG("\tOCSP Response signed by authorized responder "
  518. "delegated by issuer");
  519. passed = 1;
  520. }
  521. #ifndef WOLFSSL_NO_OCSP_ISSUER_CHAIN_CHECK
  522. else if (vp != NULL) {
  523. passed = CheckOcspResponderChain(single, cert, vp);
  524. }
  525. #endif
  526. }
  527. if (!passed) {
  528. WOLFSSL_MSG("\tOCSP Responder not authorized");
  529. #ifdef OPENSSL_EXTRA
  530. bs->verifyError = OCSP_BAD_ISSUER;
  531. #endif
  532. ret = BAD_OCSP_RESPONDER;
  533. break;
  534. }
  535. }
  536. return ret;
  537. }
  538. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  539. defined(WOLFSSL_APACHE_HTTPD) || defined(HAVE_LIGHTY)
  540. int wolfSSL_OCSP_resp_find_status(WOLFSSL_OCSP_BASICRESP *bs,
  541. WOLFSSL_OCSP_CERTID* id, int* status, int* reason,
  542. WOLFSSL_ASN1_TIME** revtime, WOLFSSL_ASN1_TIME** thisupd,
  543. WOLFSSL_ASN1_TIME** nextupd)
  544. {
  545. WOLFSSL_OCSP_SINGLERESP* single;
  546. if (bs == NULL || id == NULL)
  547. return WOLFSSL_FAILURE;
  548. single = bs->single;
  549. while (single != NULL) {
  550. if ((XMEMCMP(single->status->serial, id->status->serial, single->status->serialSz) == 0)
  551. && (XMEMCMP(single->issuerHash, id->issuerHash, OCSP_DIGEST_SIZE) == 0)
  552. && (XMEMCMP(single->issuerKeyHash, id->issuerKeyHash, OCSP_DIGEST_SIZE) == 0)) {
  553. break;
  554. }
  555. single = single->next;
  556. }
  557. if (single == NULL)
  558. return WOLFSSL_FAILURE;
  559. if (status != NULL)
  560. *status = single->status->status;
  561. if (thisupd != NULL)
  562. *thisupd = &single->status->thisDateParsed;
  563. if (nextupd != NULL)
  564. *nextupd = &single->status->nextDateParsed;
  565. /* TODO: Not needed for Nginx or httpd */
  566. if (reason != NULL)
  567. *reason = 0;
  568. if (revtime != NULL)
  569. *revtime = NULL;
  570. return WOLFSSL_SUCCESS;
  571. }
  572. const char *wolfSSL_OCSP_cert_status_str(long s)
  573. {
  574. switch (s) {
  575. case CERT_GOOD:
  576. return "good";
  577. case CERT_REVOKED:
  578. return "revoked";
  579. case CERT_UNKNOWN:
  580. return "unknown";
  581. default:
  582. return "(UNKNOWN)";
  583. }
  584. }
  585. int wolfSSL_OCSP_check_validity(WOLFSSL_ASN1_TIME* thisupd,
  586. WOLFSSL_ASN1_TIME* nextupd, long sec, long maxsec)
  587. {
  588. (void)thisupd;
  589. (void)nextupd;
  590. (void)sec;
  591. (void)maxsec;
  592. /* Dates validated in DecodeSingleResponse. */
  593. return WOLFSSL_SUCCESS;
  594. }
  595. void wolfSSL_OCSP_CERTID_free(WOLFSSL_OCSP_CERTID* certId)
  596. {
  597. FreeOcspEntry(certId, NULL);
  598. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  599. }
  600. WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_cert_to_id(
  601. const WOLFSSL_EVP_MD *dgst, const WOLFSSL_X509 *subject,
  602. const WOLFSSL_X509 *issuer)
  603. {
  604. WOLFSSL_OCSP_CERTID* certId = NULL;
  605. CertStatus* certStatus = NULL;
  606. WOLFSSL_CERT_MANAGER* cm = NULL;
  607. int ret = -1;
  608. DerBuffer* derCert = NULL;
  609. #ifdef WOLFSSL_SMALL_STACK
  610. DecodedCert *cert = NULL;
  611. #else
  612. DecodedCert cert[1];
  613. #endif
  614. (void)dgst;
  615. cm = wolfSSL_CertManagerNew();
  616. if (cm == NULL
  617. || subject == NULL || subject->derCert == NULL
  618. || issuer == NULL || issuer->derCert == NULL)
  619. goto out;
  620. #ifdef WOLFSSL_SMALL_STACK
  621. cert = (DecodedCert *)XMALLOC(sizeof(*cert), cm->heap, DYNAMIC_TYPE_DCERT);
  622. if (cert == NULL)
  623. goto out;
  624. #endif
  625. ret = AllocDer(&derCert, issuer->derCert->length,
  626. issuer->derCert->type, NULL);
  627. if (ret == 0) {
  628. /* AddCA() frees the buffer. */
  629. XMEMCPY(derCert->buffer, issuer->derCert->buffer,
  630. issuer->derCert->length);
  631. ret = AddCA(cm, &derCert, WOLFSSL_USER_CA, 1);
  632. if (ret != WOLFSSL_SUCCESS) {
  633. goto out;
  634. }
  635. derCert = NULL;
  636. }
  637. ret = -1;
  638. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(WOLFSSL_OCSP_CERTID),
  639. cm->heap, DYNAMIC_TYPE_OPENSSL);
  640. if (certId == NULL)
  641. goto out;
  642. certStatus = (CertStatus*)XMALLOC(sizeof(CertStatus), cm->heap,
  643. DYNAMIC_TYPE_OPENSSL);
  644. if (certStatus == NULL)
  645. goto out;
  646. XMEMSET(certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  647. XMEMSET(certStatus, 0, sizeof(CertStatus));
  648. certId->status = certStatus;
  649. certId->ownStatus = 1;
  650. InitDecodedCert(cert, subject->derCert->buffer,
  651. subject->derCert->length, NULL);
  652. if (ParseCertRelative(cert, CERT_TYPE, VERIFY_OCSP, cm) != 0) {
  653. FreeDecodedCert(cert);
  654. goto out;
  655. }
  656. else {
  657. XMEMCPY(certId->issuerHash, cert->issuerHash, OCSP_DIGEST_SIZE);
  658. XMEMCPY(certId->issuerKeyHash, cert->issuerKeyHash, OCSP_DIGEST_SIZE);
  659. XMEMCPY(certId->status->serial, cert->serial, cert->serialSz);
  660. certId->status->serialSz = cert->serialSz;
  661. FreeDecodedCert(cert);
  662. }
  663. ret = 0;
  664. out:
  665. if (ret != 0) {
  666. if (derCert != NULL)
  667. FreeDer(&derCert);
  668. if (certId != NULL) {
  669. XFREE(certId, cm->heap, DYNAMIC_TYPE_OPENSSL);
  670. certId = NULL;
  671. }
  672. if (certStatus)
  673. XFREE(certStatus, cm->heap, DYNAMIC_TYPE_OPENSSL);
  674. }
  675. #ifdef WOLFSSL_SMALL_STACK
  676. if (cert != NULL)
  677. XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
  678. #endif
  679. if (cm != NULL)
  680. wolfSSL_CertManagerFree(cm);
  681. return certId;
  682. }
  683. void wolfSSL_OCSP_BASICRESP_free(WOLFSSL_OCSP_BASICRESP* basicResponse)
  684. {
  685. wolfSSL_OCSP_RESPONSE_free(basicResponse);
  686. }
  687. /* Signature verified in DecodeBasicOcspResponse.
  688. * But no store available to verify certificate. */
  689. int wolfSSL_OCSP_basic_verify(WOLFSSL_OCSP_BASICRESP *bs,
  690. WOLF_STACK_OF(WOLFSSL_X509) *certs, WOLFSSL_X509_STORE *st, unsigned long flags)
  691. {
  692. int ret = WOLFSSL_FAILURE;
  693. #ifdef WOLFSSL_SMALL_STACK
  694. DecodedCert *cert;
  695. #else
  696. DecodedCert cert[1];
  697. #endif
  698. byte certInit = 0;
  699. int idx;
  700. (void)certs;
  701. if (flags & OCSP_NOVERIFY)
  702. return WOLFSSL_SUCCESS;
  703. #ifdef WOLFSSL_SMALL_STACK
  704. cert = (DecodedCert *)
  705. XMALLOC(sizeof(*cert), (st && st->cm) ? st->cm->heap : NULL,
  706. DYNAMIC_TYPE_DCERT);
  707. if (cert == NULL)
  708. return WOLFSSL_FAILURE;
  709. #endif
  710. #ifdef OPENSSL_EXTRA
  711. if (bs->verifyError != OCSP_VERIFY_ERROR_NONE)
  712. goto out;
  713. #endif
  714. if (flags & OCSP_TRUSTOTHER) {
  715. for (idx = 0; idx < wolfSSL_sk_X509_num(certs); idx++) {
  716. WOLFSSL_X509* x = wolfSSL_sk_X509_value(certs, idx);
  717. int derSz = 0;
  718. const byte* der = wolfSSL_X509_get_der(x, &derSz);
  719. if (derSz == (int)bs->certSz && XMEMCMP(bs->cert, der, derSz) == 0) {
  720. ret = WOLFSSL_SUCCESS;
  721. goto out;
  722. }
  723. }
  724. }
  725. InitDecodedCert(cert, bs->cert, bs->certSz, NULL);
  726. certInit = 1;
  727. if (ParseCertRelative(cert, CERT_TYPE, VERIFY, st->cm) < 0)
  728. goto out;
  729. if (!(flags & OCSP_NOCHECKS)) {
  730. if (CheckOcspResponder(bs, cert, st->cm) != 0)
  731. goto out;
  732. }
  733. ret = WOLFSSL_SUCCESS;
  734. out:
  735. if (certInit)
  736. FreeDecodedCert(cert);
  737. #ifdef WOLFSSL_SMALL_STACK
  738. XFREE(cert, (st && st->cm) ? st->cm->heap : NULL, DYNAMIC_TYPE_DCERT);
  739. #endif
  740. return ret;
  741. }
  742. void wolfSSL_OCSP_RESPONSE_free(OcspResponse* response)
  743. {
  744. if (response == NULL)
  745. return;
  746. if (response->single != NULL) {
  747. FreeOcspEntry(response->single, NULL);
  748. XFREE(response->single, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  749. }
  750. if (response->source != NULL)
  751. XFREE(response->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  752. XFREE(response, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  753. }
  754. #ifndef NO_BIO
  755. OcspResponse* wolfSSL_d2i_OCSP_RESPONSE_bio(WOLFSSL_BIO* bio,
  756. OcspResponse** response)
  757. {
  758. byte* data;
  759. byte* p;
  760. int len;
  761. int dataAlloced = 0;
  762. OcspResponse* ret = NULL;
  763. if (bio == NULL)
  764. return NULL;
  765. if (bio->type == WOLFSSL_BIO_MEMORY) {
  766. len = wolfSSL_BIO_get_mem_data(bio, &data);
  767. if (len <= 0 || data == NULL) {
  768. return NULL;
  769. }
  770. }
  771. #ifndef NO_FILESYSTEM
  772. else if (bio->type == WOLFSSL_BIO_FILE) {
  773. long fcur;
  774. long flen;
  775. if (bio->ptr == NULL)
  776. return NULL;
  777. fcur = XFTELL((XFILE)bio->ptr);
  778. if (fcur < 0)
  779. return NULL;
  780. if(XFSEEK((XFILE)bio->ptr, 0, SEEK_END) != 0)
  781. return NULL;
  782. flen = XFTELL((XFILE)bio->ptr);
  783. if (flen < 0)
  784. return NULL;
  785. if (XFSEEK((XFILE)bio->ptr, fcur, SEEK_SET) != 0)
  786. return NULL;
  787. /* check calculated length */
  788. fcur = flen - fcur;
  789. if (fcur > MAX_WOLFSSL_FILE_SIZE || fcur <= 0)
  790. return NULL;
  791. data = (byte*)XMALLOC(fcur, 0, DYNAMIC_TYPE_TMP_BUFFER);
  792. if (data == NULL)
  793. return NULL;
  794. dataAlloced = 1;
  795. len = wolfSSL_BIO_read(bio, (char *)data, (int)flen);
  796. }
  797. #endif
  798. else
  799. return NULL;
  800. if (len > 0) {
  801. p = data;
  802. ret = wolfSSL_d2i_OCSP_RESPONSE(response, (const unsigned char **)&p,
  803. len);
  804. }
  805. if (dataAlloced)
  806. XFREE(data, 0, DYNAMIC_TYPE_TMP_BUFFER);
  807. return ret;
  808. }
  809. #endif /* !NO_BIO */
  810. OcspResponse* wolfSSL_d2i_OCSP_RESPONSE(OcspResponse** response,
  811. const unsigned char** data, int len)
  812. {
  813. OcspResponse *resp = NULL;
  814. word32 idx = 0;
  815. int length = 0;
  816. int ret;
  817. if (data == NULL)
  818. return NULL;
  819. if (response != NULL)
  820. resp = *response;
  821. if (resp == NULL) {
  822. resp = (OcspResponse*)XMALLOC(sizeof(OcspResponse), NULL,
  823. DYNAMIC_TYPE_OCSP_REQUEST);
  824. if (resp == NULL)
  825. return NULL;
  826. XMEMSET(resp, 0, sizeof(OcspResponse));
  827. }
  828. resp->source = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  829. if (resp->source == NULL) {
  830. XFREE(resp, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  831. return NULL;
  832. }
  833. resp->single = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  834. DYNAMIC_TYPE_OCSP_ENTRY);
  835. if (resp->single == NULL) {
  836. XFREE(resp->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  837. XFREE(resp, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  838. return NULL;
  839. }
  840. XMEMSET(resp->single, 0, sizeof(OcspEntry));
  841. resp->single->status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  842. DYNAMIC_TYPE_OCSP_STATUS);
  843. resp->single->ownStatus = 1;
  844. if (resp->single->status == NULL) {
  845. XFREE(resp->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  846. XFREE(resp->single, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  847. XFREE(resp, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  848. return NULL;
  849. }
  850. XMEMSET(resp->single->status, 0, sizeof(CertStatus));
  851. XMEMCPY(resp->source, *data, len);
  852. resp->maxIdx = len;
  853. ret = OcspResponseDecode(resp, NULL, NULL, 1);
  854. if (ret != 0 && ret != ASN_OCSP_CONFIRM_E) {
  855. /* for just converting from a DER to an internal structure the CA may
  856. * not yet be known to this function for signature verification */
  857. wolfSSL_OCSP_RESPONSE_free(resp);
  858. return NULL;
  859. }
  860. if (GetSequence(*data, &idx, &length, len) >= 0)
  861. (*data) += idx + length;
  862. return resp;
  863. }
  864. int wolfSSL_i2d_OCSP_RESPONSE(OcspResponse* response,
  865. unsigned char** data)
  866. {
  867. if (data == NULL)
  868. return response->maxIdx;
  869. XMEMCPY(*data, response->source, response->maxIdx);
  870. return response->maxIdx;
  871. }
  872. int wolfSSL_OCSP_response_status(OcspResponse *response)
  873. {
  874. return response->responseStatus;
  875. }
  876. const char *wolfSSL_OCSP_response_status_str(long s)
  877. {
  878. switch (s) {
  879. case OCSP_SUCCESSFUL:
  880. return "successful";
  881. case OCSP_MALFORMED_REQUEST:
  882. return "malformedrequest";
  883. case OCSP_INTERNAL_ERROR:
  884. return "internalerror";
  885. case OCSP_TRY_LATER:
  886. return "trylater";
  887. case OCSP_SIG_REQUIRED:
  888. return "sigrequired";
  889. case OCSP_UNAUTHORIZED:
  890. return "unauthorized";
  891. default:
  892. return "(UNKNOWN)";
  893. }
  894. }
  895. WOLFSSL_OCSP_BASICRESP* wolfSSL_OCSP_response_get1_basic(OcspResponse* response)
  896. {
  897. WOLFSSL_OCSP_BASICRESP* bs;
  898. bs = (WOLFSSL_OCSP_BASICRESP*)XMALLOC(sizeof(WOLFSSL_OCSP_BASICRESP), NULL,
  899. DYNAMIC_TYPE_OCSP_REQUEST);
  900. if (bs == NULL)
  901. return NULL;
  902. XMEMCPY(bs, response, sizeof(OcspResponse));
  903. bs->single = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  904. DYNAMIC_TYPE_OCSP_ENTRY);
  905. bs->source = (byte*)XMALLOC(bs->maxIdx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  906. if (bs->single == NULL || bs->source == NULL) {
  907. if (bs->single) {
  908. XFREE(bs->single, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  909. bs->single = NULL;
  910. }
  911. wolfSSL_OCSP_RESPONSE_free(bs);
  912. bs = NULL;
  913. }
  914. else {
  915. XMEMCPY(bs->single, response->single, sizeof(OcspEntry));
  916. XMEMCPY(bs->source, response->source, response->maxIdx);
  917. bs->single->ownStatus = 0;
  918. }
  919. return bs;
  920. }
  921. OcspRequest* wolfSSL_OCSP_REQUEST_new(void)
  922. {
  923. OcspRequest* request;
  924. request = (OcspRequest*)XMALLOC(sizeof(OcspRequest), NULL,
  925. DYNAMIC_TYPE_OPENSSL);
  926. if (request != NULL)
  927. XMEMSET(request, 0, sizeof(OcspRequest));
  928. return request;
  929. }
  930. void wolfSSL_OCSP_REQUEST_free(OcspRequest* request)
  931. {
  932. FreeOcspRequest(request);
  933. XFREE(request, NULL, DYNAMIC_TYPE_OPENSSL);
  934. }
  935. int wolfSSL_i2d_OCSP_REQUEST(OcspRequest* request, unsigned char** data)
  936. {
  937. int size;
  938. size = EncodeOcspRequest(request, NULL, 0);
  939. if (size <= 0 || data == NULL)
  940. return size;
  941. return EncodeOcspRequest(request, *data, size);
  942. }
  943. WOLFSSL_OCSP_ONEREQ* wolfSSL_OCSP_request_add0_id(OcspRequest *req,
  944. WOLFSSL_OCSP_CERTID *cid)
  945. {
  946. if (req == NULL || cid == NULL || cid->status == NULL)
  947. return NULL;
  948. if (req->cid != NULL)
  949. wolfSSL_OCSP_CERTID_free((WOLFSSL_OCSP_CERTID*)req->cid);
  950. /* Keep to free */
  951. req->cid = (void*)cid;
  952. XMEMCPY(req->issuerHash, cid->issuerHash, KEYID_SIZE);
  953. XMEMCPY(req->issuerKeyHash, cid->issuerKeyHash, KEYID_SIZE);
  954. if (cid->status->serialSz > req->serialSz) {
  955. if (req->serial != NULL)
  956. XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP);
  957. req->serial = (byte*)XMALLOC(cid->status->serialSz,
  958. req->heap, DYNAMIC_TYPE_OCSP_REQUEST);
  959. if (req->serial == NULL)
  960. return NULL;
  961. }
  962. XMEMCPY(req->serial, cid->status->serial, cid->status->serialSz);
  963. req->serialSz = cid->status->serialSz;
  964. return req;
  965. }
  966. WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_CERTID_dup(WOLFSSL_OCSP_CERTID* id)
  967. {
  968. WOLFSSL_OCSP_CERTID* certId;
  969. if (id == NULL)
  970. return NULL;
  971. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(WOLFSSL_OCSP_CERTID),
  972. NULL, DYNAMIC_TYPE_OPENSSL);
  973. if (certId) {
  974. XMEMCPY(certId, id, sizeof(WOLFSSL_OCSP_CERTID));
  975. }
  976. return certId;
  977. }
  978. #endif
  979. #if defined(OPENSSL_ALL) || defined(APACHE_HTTPD) || defined(WOLFSSL_HAPROXY)
  980. #ifndef NO_BIO
  981. int wolfSSL_i2d_OCSP_REQUEST_bio(WOLFSSL_BIO* out,
  982. WOLFSSL_OCSP_REQUEST *req)
  983. {
  984. int size = -1;
  985. unsigned char* data = NULL;
  986. WOLFSSL_ENTER("wolfSSL_i2d_OCSP_REQUEST_bio");
  987. if (out == NULL || req == NULL)
  988. return WOLFSSL_FAILURE;
  989. size = wolfSSL_i2d_OCSP_REQUEST(req, NULL);
  990. if (size > 0) {
  991. data = (unsigned char*) XMALLOC(size, out->heap,
  992. DYNAMIC_TYPE_TMP_BUFFER);
  993. }
  994. if (data != NULL) {
  995. size = wolfSSL_i2d_OCSP_REQUEST(req, &data);
  996. }
  997. if (size <= 0) {
  998. XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
  999. return WOLFSSL_FAILURE;
  1000. }
  1001. if (wolfSSL_BIO_write(out, data, size) == (int)size) {
  1002. XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1003. return WOLFSSL_SUCCESS;
  1004. }
  1005. XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1006. return WOLFSSL_FAILURE;
  1007. }
  1008. #endif /* !NO_BIO */
  1009. int wolfSSL_i2d_OCSP_CERTID(WOLFSSL_OCSP_CERTID* id, unsigned char** data)
  1010. {
  1011. if (id == NULL || data == NULL)
  1012. return WOLFSSL_FAILURE;
  1013. if (*data != NULL) {
  1014. XMEMCPY(*data, id->rawCertId, id->rawCertIdSize);
  1015. *data = *data + id->rawCertIdSize;
  1016. }
  1017. else {
  1018. *data = (unsigned char*)XMALLOC(id->rawCertIdSize, NULL, DYNAMIC_TYPE_OPENSSL);
  1019. if (*data == NULL) {
  1020. return WOLFSSL_FAILURE;
  1021. }
  1022. XMEMCPY(*data, id->rawCertId, id->rawCertIdSize);
  1023. }
  1024. return id->rawCertIdSize;
  1025. }
  1026. WOLFSSL_OCSP_CERTID* wolfSSL_d2i_OCSP_CERTID(WOLFSSL_OCSP_CERTID** cidOut,
  1027. const unsigned char** derIn,
  1028. int length)
  1029. {
  1030. WOLFSSL_OCSP_CERTID *cid = NULL;
  1031. if ((cidOut != NULL) && (derIn != NULL) && (*derIn != NULL) &&
  1032. (length > 0)) {
  1033. cid = *cidOut;
  1034. /* If a NULL is passed we allocate the memory for the caller. */
  1035. if (cid == NULL) {
  1036. cid = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*cid), NULL,
  1037. DYNAMIC_TYPE_OPENSSL);
  1038. }
  1039. else if (cid->rawCertId != NULL) {
  1040. XFREE(cid->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  1041. cid->rawCertId = NULL;
  1042. cid->rawCertIdSize = 0;
  1043. }
  1044. if (cid != NULL) {
  1045. cid->rawCertId = (byte*)XMALLOC(length + 1, NULL, DYNAMIC_TYPE_OPENSSL);
  1046. if (cid->rawCertId != NULL) {
  1047. XMEMCPY(cid->rawCertId, *derIn, length);
  1048. cid->rawCertIdSize = length;
  1049. /* Per spec. advance past the data that is being returned
  1050. * to the caller. */
  1051. *cidOut = cid;
  1052. *derIn = *derIn + length;
  1053. return cid;
  1054. }
  1055. }
  1056. }
  1057. if ((cid != NULL) && ((cidOut == NULL) || (cid != *cidOut))) {
  1058. XFREE(cid, NULL, DYNAMIC_TYPE_OPENSSL);
  1059. }
  1060. return NULL;
  1061. }
  1062. const WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_SINGLERESP_get0_id(const WOLFSSL_OCSP_SINGLERESP *single)
  1063. {
  1064. return single;
  1065. }
  1066. /**
  1067. * Compare two WOLFSSL_OCSP_CERTID objects
  1068. * @param a
  1069. * @param b
  1070. * @return 0 on success and when objects have the same id otherwise either
  1071. * the id's don't match or an error occurred
  1072. */
  1073. int wolfSSL_OCSP_id_cmp(WOLFSSL_OCSP_CERTID *a, WOLFSSL_OCSP_CERTID *b)
  1074. {
  1075. int ret = 0;
  1076. if (a == NULL || b == NULL)
  1077. return WOLFSSL_FATAL_ERROR;
  1078. ret = a->hashAlgoOID != b->hashAlgoOID;
  1079. if (ret == 0)
  1080. ret = XMEMCMP(a->issuerHash, b->issuerHash, OCSP_DIGEST_SIZE);
  1081. if (ret == 0)
  1082. ret = XMEMCMP(a->issuerKeyHash, b->issuerKeyHash, OCSP_DIGEST_SIZE);
  1083. if (ret == 0) {
  1084. if (a->status != NULL && b->status != NULL) {
  1085. if (a->status->serialSz == b->status->serialSz)
  1086. ret = XMEMCMP(a->status->serial, b->status->serial,
  1087. a->status->serialSz);
  1088. else
  1089. ret = -1;
  1090. }
  1091. else if (a->status != b->status) {
  1092. /* If either is not null then return non-zero */
  1093. ret = -1;
  1094. }
  1095. }
  1096. return ret;
  1097. }
  1098. int wolfSSL_OCSP_single_get0_status(WOLFSSL_OCSP_SINGLERESP *single,
  1099. int *reason,
  1100. WOLFSSL_ASN1_TIME **revtime,
  1101. WOLFSSL_ASN1_TIME **thisupd,
  1102. WOLFSSL_ASN1_TIME **nextupd)
  1103. {
  1104. if (single == NULL)
  1105. return WOLFSSL_FAILURE;
  1106. if (thisupd != NULL)
  1107. *thisupd = &single->status->thisDateParsed;
  1108. if (nextupd != NULL)
  1109. *nextupd = &single->status->nextDateParsed;
  1110. if (reason != NULL)
  1111. *reason = 0;
  1112. if (revtime != NULL)
  1113. *revtime = NULL;
  1114. return single->status->status;
  1115. }
  1116. int wolfSSL_OCSP_resp_count(WOLFSSL_OCSP_BASICRESP *bs)
  1117. {
  1118. WOLFSSL_OCSP_SINGLERESP* single;
  1119. int count = 0;
  1120. if (bs == NULL)
  1121. return WOLFSSL_FAILURE;
  1122. single = bs->single;
  1123. while(single != NULL)
  1124. {
  1125. ++count;
  1126. single = single->next;
  1127. }
  1128. return count;
  1129. }
  1130. WOLFSSL_OCSP_SINGLERESP* wolfSSL_OCSP_resp_get0(WOLFSSL_OCSP_BASICRESP *bs, int idx)
  1131. {
  1132. WOLFSSL_OCSP_SINGLERESP* single;
  1133. int currIdx = 0;
  1134. if (bs == NULL)
  1135. return NULL;
  1136. single = bs->single;
  1137. while(single != NULL && currIdx != idx)
  1138. {
  1139. single = single->next;
  1140. ++currIdx;
  1141. }
  1142. return single;
  1143. }
  1144. #endif /* OPENSSL_ALL || APACHE_HTTPD || WOLFSSL_HAPROXY */
  1145. #ifdef OPENSSL_EXTRA
  1146. #ifndef NO_WOLFSSL_STUB
  1147. int wolfSSL_OCSP_REQUEST_add_ext(OcspRequest* req, WOLFSSL_X509_EXTENSION* ext,
  1148. int idx)
  1149. {
  1150. WOLFSSL_STUB("wolfSSL_OCSP_REQUEST_add_ext");
  1151. (void)req;
  1152. (void)ext;
  1153. (void)idx;
  1154. return WOLFSSL_FATAL_ERROR;
  1155. }
  1156. #endif
  1157. #ifndef NO_WOLFSSL_STUB
  1158. OcspResponse* wolfSSL_OCSP_response_create(int status,
  1159. WOLFSSL_OCSP_BASICRESP* bs)
  1160. {
  1161. WOLFSSL_STUB("wolfSSL_OCSP_response_create");
  1162. (void)status;
  1163. (void)bs;
  1164. return NULL;
  1165. }
  1166. #endif
  1167. #ifndef NO_WOLFSSL_STUB
  1168. const char* wolfSSL_OCSP_crl_reason_str(long s)
  1169. {
  1170. WOLFSSL_STUB("wolfSSL_OCSP_crl_reason_str");
  1171. (void)s;
  1172. return NULL;
  1173. }
  1174. #endif
  1175. /* Returns elements of an OCSP_CERTID struct. Currently only supports
  1176. * returning the serial number, and returns an error if user requests
  1177. * any of name, pmd, and/or keyHash.
  1178. * Return 1 on success, 0 on failure */
  1179. int wolfSSL_OCSP_id_get0_info(WOLFSSL_ASN1_STRING **name,
  1180. WOLFSSL_ASN1_OBJECT **pmd, WOLFSSL_ASN1_STRING **keyHash,
  1181. WOLFSSL_ASN1_INTEGER **serial, WOLFSSL_OCSP_CERTID *cid)
  1182. {
  1183. WOLFSSL_ENTER("wolfSSL_OCSP_id_get0_info");
  1184. if (cid == NULL)
  1185. return 0;
  1186. /* build up ASN1_INTEGER for serial */
  1187. if (serial != NULL) {
  1188. int i = 0;
  1189. WOLFSSL_ASN1_INTEGER* ser;
  1190. ser = wolfSSL_ASN1_INTEGER_new();
  1191. if (ser == NULL)
  1192. return 0;
  1193. if (cid->status->serialSz > (WOLFSSL_ASN1_INTEGER_MAX - 2)) {
  1194. /* allocate data buffer, +2 for type and length */
  1195. ser->data = (unsigned char*)XMALLOC(cid->status->serialSz + 2, NULL,
  1196. DYNAMIC_TYPE_OPENSSL);
  1197. if (ser->data == NULL) {
  1198. wolfSSL_ASN1_INTEGER_free(ser);
  1199. return 0;
  1200. }
  1201. ser->dataMax = cid->status->serialSz + 2;
  1202. ser->isDynamic = 1;
  1203. } else {
  1204. /* Use array instead of dynamic memory */
  1205. ser->data = ser->intData;
  1206. ser->dataMax = WOLFSSL_ASN1_INTEGER_MAX;
  1207. }
  1208. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  1209. /* Serial number starts at 0 index of ser->data */
  1210. XMEMCPY(&ser->data[i], cid->status->serial, cid->status->serialSz);
  1211. ser->length = cid->status->serialSz;
  1212. #else
  1213. ser->data[i++] = ASN_INTEGER;
  1214. i += SetLength(cid->status->serialSz, ser->data + i);
  1215. XMEMCPY(&ser->data[i], cid->status->serial, cid->status->serialSz);
  1216. ser->length = i + cid->status->serialSz;
  1217. #endif
  1218. cid->status->serialInt = ser;
  1219. *serial = ser;
  1220. }
  1221. /* Not needed for Apache, return error if user is requesting */
  1222. if (name != NULL || pmd != NULL || keyHash != NULL) {
  1223. if (name != NULL)
  1224. *name = NULL;
  1225. if (pmd != NULL)
  1226. *pmd = NULL;
  1227. if (keyHash != NULL)
  1228. *keyHash = NULL;
  1229. return 0;
  1230. }
  1231. return 1;
  1232. }
  1233. int wolfSSL_OCSP_request_add1_nonce(OcspRequest* req, unsigned char* val,
  1234. int sz)
  1235. {
  1236. WC_RNG rng;
  1237. WOLFSSL_ENTER("wolfSSL_OCSP_request_add1_nonce");
  1238. if (req == NULL || sz > MAX_OCSP_NONCE_SZ) {
  1239. WOLFSSL_MSG("Bad parameter");
  1240. return WOLFSSL_FAILURE;
  1241. }
  1242. if (sz <= 0)
  1243. sz = MAX_OCSP_NONCE_SZ;
  1244. if (val != NULL) {
  1245. XMEMCPY(req->nonce, val, sz);
  1246. }
  1247. else {
  1248. if (
  1249. #ifndef HAVE_FIPS
  1250. wc_InitRng_ex(&rng, req->heap, INVALID_DEVID)
  1251. #else
  1252. wc_InitRng(&rng)
  1253. #endif
  1254. != 0) {
  1255. WOLFSSL_MSG("RNG init failed");
  1256. return WOLFSSL_FAILURE;
  1257. }
  1258. if (wc_RNG_GenerateBlock(&rng, req->nonce, sz) != 0) {
  1259. WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
  1260. wc_FreeRng(&rng);
  1261. return WOLFSSL_FAILURE;
  1262. }
  1263. wc_FreeRng(&rng);
  1264. }
  1265. req->nonceSz = sz;
  1266. return WOLFSSL_SUCCESS;
  1267. }
  1268. /* Returns result of OCSP nonce comparison. Return values:
  1269. * 1 - nonces are both present and equal
  1270. * 2 - both nonces are absent
  1271. * 3 - nonce only present in response
  1272. * -1 - nonce only present in request
  1273. * 0 - both nonces present and equal
  1274. */
  1275. int wolfSSL_OCSP_check_nonce(OcspRequest* req, WOLFSSL_OCSP_BASICRESP* bs)
  1276. {
  1277. byte* reqNonce = NULL;
  1278. byte* rspNonce = NULL;
  1279. int reqNonceSz = 0;
  1280. int rspNonceSz = 0;
  1281. WOLFSSL_ENTER("wolfSSL_OCSP_check_nonce");
  1282. if (req != NULL) {
  1283. reqNonce = req->nonce;
  1284. reqNonceSz = req->nonceSz;
  1285. }
  1286. if (bs != NULL) {
  1287. rspNonce = bs->nonce;
  1288. rspNonceSz = bs->nonceSz;
  1289. }
  1290. /* nonce absent in both req and rsp */
  1291. if (reqNonce == NULL && rspNonce == NULL)
  1292. return 2;
  1293. /* nonce present in rsp only */
  1294. if (reqNonce == NULL && rspNonce != NULL)
  1295. return 3;
  1296. /* nonce present in req only */
  1297. if (reqNonce != NULL && rspNonce == NULL)
  1298. return -1;
  1299. /* nonces are present and equal, return 1. Extra NULL check for fixing
  1300. scan-build warning. */
  1301. if (reqNonceSz == rspNonceSz && reqNonce && rspNonce) {
  1302. if (XMEMCMP(reqNonce, rspNonce, reqNonceSz) == 0)
  1303. return 1;
  1304. }
  1305. /* nonces are present but not equal */
  1306. return 0;
  1307. }
  1308. #endif /* OPENSSL_EXTRA */
  1309. #else /* HAVE_OCSP */
  1310. #ifdef _MSC_VER
  1311. /* 4206 warning for blank file */
  1312. #pragma warning(disable: 4206)
  1313. #endif
  1314. #endif /* HAVE_OCSP */
  1315. #endif /* WOLFCRYPT_ONLY */