ocsp.c 40 KB

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