ocsp.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /* ocsp.c
  2. *
  3. * Copyright (C) 2006-2021 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. WOLFSSL_LOCAL 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. ret = CompareOcspReqResp(ocspRequest, ocspResponse);
  262. if (ret != 0) {
  263. goto end;
  264. }
  265. }
  266. if (responseBuffer) {
  267. responseBuffer->buffer = (byte*)XMALLOC(responseSz, ocsp->cm->heap,
  268. DYNAMIC_TYPE_TMP_BUFFER);
  269. if (responseBuffer->buffer) {
  270. responseBuffer->length = responseSz;
  271. XMEMCPY(responseBuffer->buffer, response, responseSz);
  272. }
  273. }
  274. ret = xstat2err(ocspResponse->single->status->status);
  275. if (ret == 0) {
  276. validated = 1;
  277. }
  278. if (wc_LockMutex(&ocsp->ocspLock) != 0) {
  279. ret = BAD_MUTEX_E;
  280. goto end;
  281. }
  282. if (status != NULL) {
  283. if (status->rawOcspResponse) {
  284. XFREE(status->rawOcspResponse, ocsp->cm->heap,
  285. DYNAMIC_TYPE_OCSP_STATUS);
  286. }
  287. /* Replace existing certificate entry with updated */
  288. newSingle->status->next = status->next;
  289. XMEMCPY(status, newSingle->status, sizeof(CertStatus));
  290. }
  291. else {
  292. /* Save new certificate entry */
  293. status = (CertStatus*)XMALLOC(sizeof(CertStatus),
  294. ocsp->cm->heap, DYNAMIC_TYPE_OCSP_STATUS);
  295. if (status != NULL) {
  296. XMEMCPY(status, newSingle->status, sizeof(CertStatus));
  297. status->next = entry->status;
  298. entry->status = status;
  299. entry->ownStatus = 1;
  300. entry->totalStatus++;
  301. }
  302. }
  303. if (status && responseBuffer && responseBuffer->buffer) {
  304. status->rawOcspResponse = (byte*)XMALLOC(responseBuffer->length,
  305. ocsp->cm->heap,
  306. DYNAMIC_TYPE_OCSP_STATUS);
  307. if (status->rawOcspResponse) {
  308. status->rawOcspResponseSz = responseBuffer->length;
  309. XMEMCPY(status->rawOcspResponse, responseBuffer->buffer,
  310. responseBuffer->length);
  311. }
  312. }
  313. wc_UnLockMutex(&ocsp->ocspLock);
  314. end:
  315. if (ret == 0 && validated == 1) {
  316. WOLFSSL_MSG("New OcspResponse validated");
  317. } else if (ret != OCSP_CERT_REVOKED) {
  318. ret = OCSP_LOOKUP_FAIL;
  319. }
  320. #ifdef WOLFSSL_SMALL_STACK
  321. XFREE(newStatus, NULL, DYNAMIC_TYPE_OCSP_STATUS);
  322. XFREE(newSingle, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  323. XFREE(ocspResponse, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  324. #endif
  325. return ret;
  326. }
  327. /* 0 on success */
  328. int CheckOcspRequest(WOLFSSL_OCSP* ocsp, OcspRequest* ocspRequest,
  329. buffer* responseBuffer)
  330. {
  331. OcspEntry* entry = NULL;
  332. CertStatus* status = NULL;
  333. byte* request = NULL;
  334. int requestSz = 2048;
  335. int responseSz = 0;
  336. byte* response = NULL;
  337. const char* url = NULL;
  338. int urlSz = 0;
  339. int ret = -1;
  340. WOLFSSL* ssl;
  341. void* ioCtx;
  342. WOLFSSL_ENTER("CheckOcspRequest");
  343. if (ocsp == NULL || ocspRequest == NULL)
  344. return BAD_FUNC_ARG;
  345. if (responseBuffer) {
  346. responseBuffer->buffer = NULL;
  347. responseBuffer->length = 0;
  348. }
  349. ret = GetOcspEntry(ocsp, ocspRequest, &entry);
  350. if (ret != 0)
  351. return ret;
  352. ret = GetOcspStatus(ocsp, ocspRequest, entry, &status, responseBuffer);
  353. if (ret != OCSP_INVALID_STATUS)
  354. return ret;
  355. /* get SSL and IOCtx */
  356. ssl = (WOLFSSL*)ocspRequest->ssl;
  357. ioCtx = (ssl && ssl->ocspIOCtx != NULL) ?
  358. ssl->ocspIOCtx : ocsp->cm->ocspIOCtx;
  359. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  360. if (ocsp->statusCb != NULL && ssl != NULL) {
  361. ret = ocsp->statusCb(ssl, ioCtx);
  362. if (ret == 0) {
  363. ret = wolfSSL_get_ocsp_response(ssl, &response);
  364. ret = CheckOcspResponse(ocsp, response, ret, responseBuffer, status,
  365. entry, NULL);
  366. if (response != NULL)
  367. XFREE(response, NULL, DYNAMIC_TYPE_OPENSSL);
  368. return ret;
  369. }
  370. WOLFSSL_LEAVE("CheckOcspRequest", ocsp->error);
  371. return OCSP_LOOKUP_FAIL;
  372. }
  373. #endif
  374. if (ocsp->cm->ocspUseOverrideURL) {
  375. url = ocsp->cm->ocspOverrideURL;
  376. if (url != NULL && url[0] != '\0')
  377. urlSz = (int)XSTRLEN(url);
  378. else
  379. return OCSP_NEED_URL;
  380. }
  381. else if (ocspRequest->urlSz != 0 && ocspRequest->url != NULL) {
  382. url = (const char *)ocspRequest->url;
  383. urlSz = ocspRequest->urlSz;
  384. }
  385. else {
  386. /* cert doesn't have extAuthInfo, assuming CERT_GOOD */
  387. WOLFSSL_MSG("Cert has no OCSP URL, assuming CERT_GOOD");
  388. return 0;
  389. }
  390. request = (byte*)XMALLOC(requestSz, ocsp->cm->heap, DYNAMIC_TYPE_OCSP);
  391. if (request == NULL) {
  392. WOLFSSL_LEAVE("CheckCertOCSP", MEMORY_ERROR);
  393. if (responseBuffer) {
  394. XFREE(responseBuffer->buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  395. responseBuffer->buffer = NULL;
  396. }
  397. return MEMORY_ERROR;
  398. }
  399. requestSz = EncodeOcspRequest(ocspRequest, request, requestSz);
  400. if (requestSz > 0 && ocsp->cm->ocspIOCb) {
  401. responseSz = ocsp->cm->ocspIOCb(ioCtx, url, urlSz,
  402. request, requestSz, &response);
  403. }
  404. if (responseSz == WOLFSSL_CBIO_ERR_WANT_READ) {
  405. ret = OCSP_WANT_READ;
  406. }
  407. XFREE(request, ocsp->cm->heap, DYNAMIC_TYPE_OCSP);
  408. if (responseSz >= 0 && response) {
  409. ret = CheckOcspResponse(ocsp, response, responseSz, responseBuffer, status,
  410. entry, ocspRequest);
  411. }
  412. if (response != NULL && ocsp->cm->ocspRespFreeCb)
  413. ocsp->cm->ocspRespFreeCb(ioCtx, response);
  414. /* Keep responseBuffer in the case of getting to response check. Caller
  415. * should free responseBuffer after checking OCSP return value in "ret" */
  416. WOLFSSL_LEAVE("CheckOcspRequest", ret);
  417. return ret;
  418. }
  419. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  420. defined(WOLFSSL_APACHE_HTTPD) || defined(HAVE_LIGHTY)
  421. int wolfSSL_OCSP_resp_find_status(WOLFSSL_OCSP_BASICRESP *bs,
  422. WOLFSSL_OCSP_CERTID* id, int* status, int* reason,
  423. WOLFSSL_ASN1_TIME** revtime, WOLFSSL_ASN1_TIME** thisupd,
  424. WOLFSSL_ASN1_TIME** nextupd)
  425. {
  426. WOLFSSL_OCSP_SINGLERESP* single;
  427. if (bs == NULL || id == NULL)
  428. return WOLFSSL_FAILURE;
  429. single = bs->single;
  430. while (single != NULL) {
  431. if ((XMEMCMP(single->status->serial, id->status->serial, single->status->serialSz) == 0)
  432. && (XMEMCMP(single->issuerHash, id->issuerHash, OCSP_DIGEST_SIZE) == 0)
  433. && (XMEMCMP(single->issuerKeyHash, id->issuerKeyHash, OCSP_DIGEST_SIZE) == 0)) {
  434. break;
  435. }
  436. single = single->next;
  437. }
  438. if (single == NULL)
  439. return WOLFSSL_FAILURE;
  440. if (status != NULL)
  441. *status = single->status->status;
  442. if (thisupd != NULL)
  443. *thisupd = &single->status->thisDateParsed;
  444. if (nextupd != NULL)
  445. *nextupd = &single->status->nextDateParsed;
  446. /* TODO: Not needed for Nginx or httpd */
  447. if (reason != NULL)
  448. *reason = 0;
  449. if (revtime != NULL)
  450. *revtime = NULL;
  451. return WOLFSSL_SUCCESS;
  452. }
  453. const char *wolfSSL_OCSP_cert_status_str(long s)
  454. {
  455. switch (s) {
  456. case CERT_GOOD:
  457. return "good";
  458. case CERT_REVOKED:
  459. return "revoked";
  460. case CERT_UNKNOWN:
  461. return "unknown";
  462. default:
  463. return "(UNKNOWN)";
  464. }
  465. }
  466. int wolfSSL_OCSP_check_validity(WOLFSSL_ASN1_TIME* thisupd,
  467. WOLFSSL_ASN1_TIME* nextupd, long sec, long maxsec)
  468. {
  469. (void)thisupd;
  470. (void)nextupd;
  471. (void)sec;
  472. (void)maxsec;
  473. /* Dates validated in DecodeSingleResponse. */
  474. return WOLFSSL_SUCCESS;
  475. }
  476. void wolfSSL_OCSP_CERTID_free(WOLFSSL_OCSP_CERTID* certId)
  477. {
  478. FreeOcspEntry(certId, NULL);
  479. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  480. }
  481. WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_cert_to_id(
  482. const WOLFSSL_EVP_MD *dgst, const WOLFSSL_X509 *subject,
  483. const WOLFSSL_X509 *issuer)
  484. {
  485. WOLFSSL_OCSP_CERTID* certId;
  486. CertStatus* certStatus;
  487. DecodedCert cert;
  488. WOLFSSL_CERT_MANAGER* cm;
  489. int ret;
  490. DerBuffer* derCert = NULL;
  491. (void)dgst;
  492. cm = wolfSSL_CertManagerNew();
  493. if (cm == NULL
  494. || subject == NULL || subject->derCert == NULL
  495. || issuer == NULL || issuer->derCert == NULL)
  496. return NULL;
  497. ret = AllocDer(&derCert, issuer->derCert->length,
  498. issuer->derCert->type, NULL);
  499. if (ret == 0) {
  500. /* AddCA() frees the buffer. */
  501. XMEMCPY(derCert->buffer, issuer->derCert->buffer,
  502. issuer->derCert->length);
  503. ret = AddCA(cm, &derCert, WOLFSSL_USER_CA, 1);
  504. if (ret != WOLFSSL_SUCCESS) {
  505. wolfSSL_CertManagerFree(cm);
  506. return NULL;
  507. }
  508. }
  509. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(WOLFSSL_OCSP_CERTID), NULL,
  510. DYNAMIC_TYPE_OPENSSL);
  511. certStatus = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  512. DYNAMIC_TYPE_OPENSSL);
  513. if (certId == NULL || certStatus == NULL) {
  514. if (certId)
  515. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  516. if (certStatus)
  517. XFREE(certStatus, NULL, DYNAMIC_TYPE_OPENSSL);
  518. certId = NULL;
  519. }
  520. if (certId != NULL) {
  521. XMEMSET(certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  522. XMEMSET(certStatus, 0, sizeof(CertStatus));
  523. certId->status = certStatus;
  524. certId->ownStatus = 1;
  525. InitDecodedCert(&cert, subject->derCert->buffer,
  526. subject->derCert->length, NULL);
  527. if (ParseCertRelative(&cert, CERT_TYPE, VERIFY_OCSP, cm) != 0) {
  528. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  529. certId = NULL;
  530. }
  531. else {
  532. XMEMCPY(certId->issuerHash, cert.issuerHash, OCSP_DIGEST_SIZE);
  533. XMEMCPY(certId->issuerKeyHash, cert.issuerKeyHash, OCSP_DIGEST_SIZE);
  534. XMEMCPY(certId->status->serial, cert.serial, cert.serialSz);
  535. certId->status->serialSz = cert.serialSz;
  536. }
  537. FreeDecodedCert(&cert);
  538. }
  539. wolfSSL_CertManagerFree(cm);
  540. return certId;
  541. }
  542. void wolfSSL_OCSP_BASICRESP_free(WOLFSSL_OCSP_BASICRESP* basicResponse)
  543. {
  544. wolfSSL_OCSP_RESPONSE_free(basicResponse);
  545. }
  546. /* Signature verified in DecodeBasicOcspResponse.
  547. * But no store available to verify certificate. */
  548. int wolfSSL_OCSP_basic_verify(WOLFSSL_OCSP_BASICRESP *bs,
  549. WOLF_STACK_OF(WOLFSSL_X509) *certs, WOLFSSL_X509_STORE *st, unsigned long flags)
  550. {
  551. DecodedCert cert;
  552. int ret = WOLFSSL_SUCCESS;
  553. (void)certs;
  554. if (flags & OCSP_NOVERIFY)
  555. return WOLFSSL_SUCCESS;
  556. #ifdef OPENSSL_EXTRA
  557. if (bs->verifyError != OCSP_VERIFY_ERROR_NONE)
  558. return WOLFSSL_FAILURE;
  559. #endif
  560. InitDecodedCert(&cert, bs->cert, bs->certSz, NULL);
  561. if (ParseCertRelative(&cert, CERT_TYPE, VERIFY, st->cm) < 0)
  562. ret = WOLFSSL_FAILURE;
  563. FreeDecodedCert(&cert);
  564. return ret;
  565. }
  566. void wolfSSL_OCSP_RESPONSE_free(OcspResponse* response)
  567. {
  568. if (response == NULL)
  569. return;
  570. if (response->single != NULL) {
  571. FreeOcspEntry(response->single, NULL);
  572. XFREE(response->single, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  573. }
  574. if (response->source != NULL)
  575. XFREE(response->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  576. XFREE(response, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  577. }
  578. #ifndef NO_BIO
  579. OcspResponse* wolfSSL_d2i_OCSP_RESPONSE_bio(WOLFSSL_BIO* bio,
  580. OcspResponse** response)
  581. {
  582. byte* data;
  583. byte* p;
  584. int len;
  585. int dataAlloced = 0;
  586. OcspResponse* ret = NULL;
  587. if (bio == NULL)
  588. return NULL;
  589. if (bio->type == WOLFSSL_BIO_MEMORY) {
  590. len = wolfSSL_BIO_get_mem_data(bio, &data);
  591. if (len <= 0 || data == NULL) {
  592. return NULL;
  593. }
  594. }
  595. #ifndef NO_FILESYSTEM
  596. else if (bio->type == WOLFSSL_BIO_FILE) {
  597. long fcur;
  598. long flen;
  599. if (bio->ptr == NULL)
  600. return NULL;
  601. fcur = XFTELL((XFILE)bio->ptr);
  602. if (fcur < 0)
  603. return NULL;
  604. if(XFSEEK((XFILE)bio->ptr, 0, SEEK_END) != 0)
  605. return NULL;
  606. flen = XFTELL((XFILE)bio->ptr);
  607. if (flen < 0)
  608. return NULL;
  609. if (XFSEEK((XFILE)bio->ptr, fcur, SEEK_SET) != 0)
  610. return NULL;
  611. /* check calculated length */
  612. fcur = flen - fcur;
  613. if (fcur > MAX_WOLFSSL_FILE_SIZE || fcur <= 0)
  614. return NULL;
  615. data = (byte*)XMALLOC(fcur, 0, DYNAMIC_TYPE_TMP_BUFFER);
  616. if (data == NULL)
  617. return NULL;
  618. dataAlloced = 1;
  619. len = wolfSSL_BIO_read(bio, (char *)data, (int)flen);
  620. }
  621. #endif
  622. else
  623. return NULL;
  624. if (len > 0) {
  625. p = data;
  626. ret = wolfSSL_d2i_OCSP_RESPONSE(response, (const unsigned char **)&p,
  627. len);
  628. }
  629. if (dataAlloced)
  630. XFREE(data, 0, DYNAMIC_TYPE_TMP_BUFFER);
  631. return ret;
  632. }
  633. #endif /* !NO_BIO */
  634. OcspResponse* wolfSSL_d2i_OCSP_RESPONSE(OcspResponse** response,
  635. const unsigned char** data, int len)
  636. {
  637. OcspResponse *resp = NULL;
  638. word32 idx = 0;
  639. int length = 0;
  640. if (data == NULL)
  641. return NULL;
  642. if (response != NULL)
  643. resp = *response;
  644. if (resp == NULL) {
  645. resp = (OcspResponse*)XMALLOC(sizeof(OcspResponse), NULL,
  646. DYNAMIC_TYPE_OCSP_REQUEST);
  647. if (resp == NULL)
  648. return NULL;
  649. XMEMSET(resp, 0, sizeof(OcspResponse));
  650. }
  651. resp->source = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  652. if (resp->source == NULL) {
  653. XFREE(resp, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  654. return NULL;
  655. }
  656. resp->single = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  657. DYNAMIC_TYPE_OCSP_ENTRY);
  658. if (resp->single == NULL) {
  659. XFREE(resp->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  660. XFREE(resp, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  661. return NULL;
  662. }
  663. XMEMSET(resp->single, 0, sizeof(OcspEntry));
  664. resp->single->status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  665. DYNAMIC_TYPE_OCSP_STATUS);
  666. resp->single->ownStatus = 1;
  667. if (resp->single->status == NULL) {
  668. XFREE(resp->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  669. XFREE(resp->single, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  670. XFREE(resp, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  671. return NULL;
  672. }
  673. XMEMSET(resp->single->status, 0, sizeof(CertStatus));
  674. XMEMCPY(resp->source, *data, len);
  675. resp->maxIdx = len;
  676. if (OcspResponseDecode(resp, NULL, NULL, 1) != 0) {
  677. wolfSSL_OCSP_RESPONSE_free(resp);
  678. return NULL;
  679. }
  680. if (GetSequence(*data, &idx, &length, len) >= 0)
  681. (*data) += idx + length;
  682. return resp;
  683. }
  684. int wolfSSL_i2d_OCSP_RESPONSE(OcspResponse* response,
  685. unsigned char** data)
  686. {
  687. if (data == NULL)
  688. return response->maxIdx;
  689. XMEMCPY(*data, response->source, response->maxIdx);
  690. return response->maxIdx;
  691. }
  692. int wolfSSL_OCSP_response_status(OcspResponse *response)
  693. {
  694. return response->responseStatus;
  695. }
  696. const char *wolfSSL_OCSP_response_status_str(long s)
  697. {
  698. switch (s) {
  699. case OCSP_SUCCESSFUL:
  700. return "successful";
  701. case OCSP_MALFORMED_REQUEST:
  702. return "malformedrequest";
  703. case OCSP_INTERNAL_ERROR:
  704. return "internalerror";
  705. case OCSP_TRY_LATER:
  706. return "trylater";
  707. case OCSP_SIG_REQUIRED:
  708. return "sigrequired";
  709. case OCSP_UNAUTHORIZED:
  710. return "unauthorized";
  711. default:
  712. return "(UNKNOWN)";
  713. }
  714. }
  715. WOLFSSL_OCSP_BASICRESP* wolfSSL_OCSP_response_get1_basic(OcspResponse* response)
  716. {
  717. WOLFSSL_OCSP_BASICRESP* bs;
  718. bs = (WOLFSSL_OCSP_BASICRESP*)XMALLOC(sizeof(WOLFSSL_OCSP_BASICRESP), NULL,
  719. DYNAMIC_TYPE_OCSP_REQUEST);
  720. if (bs == NULL)
  721. return NULL;
  722. XMEMCPY(bs, response, sizeof(OcspResponse));
  723. bs->single = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  724. DYNAMIC_TYPE_OCSP_ENTRY);
  725. bs->source = (byte*)XMALLOC(bs->maxIdx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  726. if (bs->single == NULL || bs->source == NULL) {
  727. if (bs->single) XFREE(bs->single, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  728. if (bs->source) XFREE(bs->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  729. wolfSSL_OCSP_RESPONSE_free(bs);
  730. bs = NULL;
  731. }
  732. else {
  733. XMEMCPY(bs->single, response->single, sizeof(OcspEntry));
  734. XMEMCPY(bs->source, response->source, response->maxIdx);
  735. bs->single->ownStatus = 0;
  736. }
  737. return bs;
  738. }
  739. OcspRequest* wolfSSL_OCSP_REQUEST_new(void)
  740. {
  741. OcspRequest* request;
  742. request = (OcspRequest*)XMALLOC(sizeof(OcspRequest), NULL,
  743. DYNAMIC_TYPE_OPENSSL);
  744. if (request != NULL)
  745. XMEMSET(request, 0, sizeof(OcspRequest));
  746. return request;
  747. }
  748. void wolfSSL_OCSP_REQUEST_free(OcspRequest* request)
  749. {
  750. FreeOcspRequest(request);
  751. XFREE(request, NULL, DYNAMIC_TYPE_OPENSSL);
  752. }
  753. int wolfSSL_i2d_OCSP_REQUEST(OcspRequest* request, unsigned char** data)
  754. {
  755. int size;
  756. size = EncodeOcspRequest(request, NULL, 0);
  757. if (size <= 0 || data == NULL)
  758. return size;
  759. return EncodeOcspRequest(request, *data, size);
  760. }
  761. WOLFSSL_OCSP_ONEREQ* wolfSSL_OCSP_request_add0_id(OcspRequest *req,
  762. WOLFSSL_OCSP_CERTID *cid)
  763. {
  764. if (req == NULL || cid == NULL || cid->status == NULL)
  765. return NULL;
  766. XMEMCPY(req->issuerHash, cid->issuerHash, KEYID_SIZE);
  767. XMEMCPY(req->issuerKeyHash, cid->issuerKeyHash, KEYID_SIZE);
  768. if (cid->status->serialSz > req->serialSz) {
  769. if (req->serial != NULL)
  770. XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP);
  771. req->serial = (byte*)XMALLOC(cid->status->serialSz,
  772. req->heap, DYNAMIC_TYPE_OCSP_REQUEST);
  773. if (req->serial == NULL)
  774. return NULL;
  775. }
  776. XMEMCPY(req->serial, cid->status->serial, cid->status->serialSz);
  777. req->serialSz = cid->status->serialSz;
  778. return req;
  779. }
  780. WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_CERTID_dup(WOLFSSL_OCSP_CERTID* id)
  781. {
  782. WOLFSSL_OCSP_CERTID* certId;
  783. if (id == NULL)
  784. return NULL;
  785. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(WOLFSSL_OCSP_CERTID),
  786. NULL, DYNAMIC_TYPE_OPENSSL);
  787. if (certId) {
  788. XMEMCPY(certId, id, sizeof(WOLFSSL_OCSP_CERTID));
  789. }
  790. return certId;
  791. }
  792. #endif
  793. #if defined(OPENSSL_ALL) || defined(APACHE_HTTPD) || defined(WOLFSSL_HAPROXY)
  794. #ifndef NO_BIO
  795. int wolfSSL_i2d_OCSP_REQUEST_bio(WOLFSSL_BIO* out,
  796. WOLFSSL_OCSP_REQUEST *req)
  797. {
  798. int size = -1;
  799. unsigned char* data = NULL;
  800. WOLFSSL_ENTER("wolfSSL_i2d_OCSP_REQUEST_bio");
  801. if (out == NULL || req == NULL)
  802. return WOLFSSL_FAILURE;
  803. size = wolfSSL_i2d_OCSP_REQUEST(req, NULL);
  804. if (size > 0) {
  805. data = (unsigned char*) XMALLOC(size, out->heap,
  806. DYNAMIC_TYPE_TMP_BUFFER);
  807. }
  808. if (data != NULL) {
  809. size = wolfSSL_i2d_OCSP_REQUEST(req, &data);
  810. }
  811. if (size <= 0) {
  812. XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
  813. return WOLFSSL_FAILURE;
  814. }
  815. if (wolfSSL_BIO_write(out, data, size) == (int)size) {
  816. XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
  817. return WOLFSSL_SUCCESS;
  818. }
  819. XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
  820. return WOLFSSL_FAILURE;
  821. }
  822. #endif /* !NO_BIO */
  823. int wolfSSL_i2d_OCSP_CERTID(WOLFSSL_OCSP_CERTID* id, unsigned char** data)
  824. {
  825. if (id == NULL || data == NULL)
  826. return WOLFSSL_FAILURE;
  827. if (*data != NULL) {
  828. XMEMCPY(*data, id->rawCertId, id->rawCertIdSize);
  829. *data = *data + id->rawCertIdSize;
  830. }
  831. else {
  832. *data = (unsigned char*)XMALLOC(id->rawCertIdSize, NULL, DYNAMIC_TYPE_OPENSSL);
  833. if (*data == NULL) {
  834. return WOLFSSL_FAILURE;
  835. }
  836. XMEMCPY(*data, id->rawCertId, id->rawCertIdSize);
  837. }
  838. return id->rawCertIdSize;
  839. }
  840. const WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_SINGLERESP_get0_id(const WOLFSSL_OCSP_SINGLERESP *single)
  841. {
  842. return single;
  843. }
  844. /**
  845. * Compare two WOLFSSL_OCSP_CERTID objects
  846. * @param a
  847. * @param b
  848. * @return 0 on success and when objects have the same id otherwise either
  849. * the id's don't match or an error occurred
  850. */
  851. int wolfSSL_OCSP_id_cmp(WOLFSSL_OCSP_CERTID *a, WOLFSSL_OCSP_CERTID *b)
  852. {
  853. int ret = 0;
  854. if (a == NULL || b == NULL)
  855. return WOLFSSL_FATAL_ERROR;
  856. ret = a->hashAlgoOID != b->hashAlgoOID;
  857. if (ret == 0)
  858. ret = XMEMCMP(a->issuerHash, b->issuerHash, OCSP_DIGEST_SIZE);
  859. if (ret == 0)
  860. ret = XMEMCMP(a->issuerKeyHash, b->issuerKeyHash, OCSP_DIGEST_SIZE);
  861. if (ret == 0) {
  862. if (a->status != NULL && b->status != NULL) {
  863. if (a->status->serialSz == b->status->serialSz)
  864. ret = XMEMCMP(a->status->serial, b->status->serial,
  865. a->status->serialSz);
  866. else
  867. ret = -1;
  868. }
  869. else if (a->status != b->status) {
  870. /* If either is not null then return non-zero */
  871. ret = -1;
  872. }
  873. }
  874. return ret;
  875. }
  876. int wolfSSL_OCSP_single_get0_status(WOLFSSL_OCSP_SINGLERESP *single,
  877. int *reason,
  878. WOLFSSL_ASN1_TIME **revtime,
  879. WOLFSSL_ASN1_TIME **thisupd,
  880. WOLFSSL_ASN1_TIME **nextupd)
  881. {
  882. if (single == NULL)
  883. return WOLFSSL_FAILURE;
  884. if (thisupd != NULL)
  885. *thisupd = &single->status->thisDateParsed;
  886. if (nextupd != NULL)
  887. *nextupd = &single->status->nextDateParsed;
  888. if (reason != NULL)
  889. *reason = 0;
  890. if (revtime != NULL)
  891. *revtime = NULL;
  892. return single->status->status;
  893. }
  894. int wolfSSL_OCSP_resp_count(WOLFSSL_OCSP_BASICRESP *bs)
  895. {
  896. WOLFSSL_OCSP_SINGLERESP* single;
  897. int count = 0;
  898. if (bs == NULL)
  899. return WOLFSSL_FAILURE;
  900. single = bs->single;
  901. while(single != NULL)
  902. {
  903. ++count;
  904. single = single->next;
  905. }
  906. return count;
  907. }
  908. WOLFSSL_OCSP_SINGLERESP* wolfSSL_OCSP_resp_get0(WOLFSSL_OCSP_BASICRESP *bs, int idx)
  909. {
  910. WOLFSSL_OCSP_SINGLERESP* single;
  911. int currIdx = 0;
  912. if (bs == NULL)
  913. return NULL;
  914. single = bs->single;
  915. while(single != NULL && currIdx != idx)
  916. {
  917. single = single->next;
  918. ++currIdx;
  919. }
  920. return single;
  921. }
  922. #endif /* OPENSSL_ALL || APACHE_HTTPD */
  923. #ifdef OPENSSL_EXTRA
  924. #ifndef NO_WOLFSSL_STUB
  925. int wolfSSL_OCSP_REQUEST_add_ext(OcspRequest* req, WOLFSSL_X509_EXTENSION* ext,
  926. int idx)
  927. {
  928. WOLFSSL_STUB("wolfSSL_OCSP_REQUEST_add_ext");
  929. (void)req;
  930. (void)ext;
  931. (void)idx;
  932. return WOLFSSL_FATAL_ERROR;
  933. }
  934. #endif
  935. #ifndef NO_WOLFSSL_STUB
  936. OcspResponse* wolfSSL_OCSP_response_create(int status,
  937. WOLFSSL_OCSP_BASICRESP* bs)
  938. {
  939. WOLFSSL_STUB("wolfSSL_OCSP_response_create");
  940. (void)status;
  941. (void)bs;
  942. return NULL;
  943. }
  944. #endif
  945. #ifndef NO_WOLFSSL_STUB
  946. const char* wolfSSL_OCSP_crl_reason_str(long s)
  947. {
  948. WOLFSSL_STUB("wolfSSL_OCSP_crl_reason_str");
  949. (void)s;
  950. return NULL;
  951. }
  952. #endif
  953. /* Returns elements of an OCSP_CERTID struct. Currently only supports
  954. * returning the serial number, and returns an error if user requests
  955. * any of name, pmd, and/or keyHash.
  956. * Return 1 on success, 0 on failure */
  957. int wolfSSL_OCSP_id_get0_info(WOLFSSL_ASN1_STRING **name,
  958. WOLFSSL_ASN1_OBJECT **pmd, WOLFSSL_ASN1_STRING **keyHash,
  959. WOLFSSL_ASN1_INTEGER **serial, WOLFSSL_OCSP_CERTID *cid)
  960. {
  961. int i = 0;
  962. WOLFSSL_ASN1_INTEGER* ser;
  963. WOLFSSL_ENTER("wolfSSL_OCSP_id_get0_info");
  964. if (cid == NULL)
  965. return 0;
  966. /* build up ASN1_INTEGER for serial */
  967. if (serial != NULL) {
  968. ser = wolfSSL_ASN1_INTEGER_new();
  969. if (ser == NULL)
  970. return 0;
  971. if (cid->status->serialSz > (WOLFSSL_ASN1_INTEGER_MAX - 2)) {
  972. /* allocate data buffer, +2 for type and length */
  973. ser->data = (unsigned char*)XMALLOC(cid->status->serialSz + 2, NULL,
  974. DYNAMIC_TYPE_OPENSSL);
  975. if (ser->data == NULL) {
  976. wolfSSL_ASN1_INTEGER_free(ser);
  977. return 0;
  978. }
  979. ser->dataMax = cid->status->serialSz + 2;
  980. ser->isDynamic = 1;
  981. } else {
  982. /* Use array instead of dynamic memory */
  983. ser->data = ser->intData;
  984. ser->dataMax = WOLFSSL_ASN1_INTEGER_MAX;
  985. }
  986. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  987. /* Serial number starts at 0 index of ser->data */
  988. XMEMCPY(&ser->data[i], cid->status->serial, cid->status->serialSz);
  989. ser->length = cid->status->serialSz;
  990. #else
  991. ser->data[i++] = ASN_INTEGER;
  992. i += SetLength(cid->status->serialSz, ser->data + i);
  993. XMEMCPY(&ser->data[i], cid->status->serial, cid->status->serialSz);
  994. ser->length = i + cid->status->serialSz;
  995. #endif
  996. cid->status->serialInt = ser;
  997. *serial = ser;
  998. }
  999. /* Not needed for Apache, return error if user is requesting */
  1000. if (name != NULL || pmd != NULL || keyHash != NULL) {
  1001. if (name != NULL)
  1002. *name = NULL;
  1003. if (pmd != NULL)
  1004. *pmd = NULL;
  1005. if (keyHash != NULL)
  1006. *keyHash = NULL;
  1007. return 0;
  1008. }
  1009. return 1;
  1010. }
  1011. #ifndef NO_WOLFSSL_STUB
  1012. int wolfSSL_OCSP_request_add1_nonce(OcspRequest* req, unsigned char* val,
  1013. int sz)
  1014. {
  1015. WOLFSSL_STUB("wolfSSL_OCSP_request_add1_nonce");
  1016. (void)req;
  1017. (void)val;
  1018. (void)sz;
  1019. return WOLFSSL_FATAL_ERROR;
  1020. }
  1021. #endif
  1022. /* Returns result of OCSP nonce comparison. Return values:
  1023. * 1 - nonces are both present and equal
  1024. * 2 - both nonces are absent
  1025. * 3 - nonce only present in response
  1026. * -1 - nonce only present in request
  1027. * 0 - both nonces present and equal
  1028. */
  1029. int wolfSSL_OCSP_check_nonce(OcspRequest* req, WOLFSSL_OCSP_BASICRESP* bs)
  1030. {
  1031. byte* reqNonce = NULL;
  1032. byte* rspNonce = NULL;
  1033. int reqNonceSz = 0;
  1034. int rspNonceSz = 0;
  1035. WOLFSSL_ENTER("wolfSSL_OCSP_check_nonce");
  1036. if (req != NULL) {
  1037. reqNonce = req->nonce;
  1038. reqNonceSz = req->nonceSz;
  1039. }
  1040. if (bs != NULL) {
  1041. rspNonce = bs->nonce;
  1042. rspNonceSz = bs->nonceSz;
  1043. }
  1044. /* nonce absent in both req and rsp */
  1045. if (reqNonce == NULL && rspNonce == NULL)
  1046. return 2;
  1047. /* nonce present in rsp only */
  1048. if (reqNonce == NULL && rspNonce != NULL)
  1049. return 3;
  1050. /* nonce present in req only */
  1051. if (reqNonce != NULL && rspNonce == NULL)
  1052. return -1;
  1053. /* nonces are present and equal, return 1. Extra NULL check for fixing
  1054. scan-build warning. */
  1055. if (reqNonceSz == rspNonceSz && reqNonce && rspNonce) {
  1056. if (XMEMCMP(reqNonce, rspNonce, reqNonceSz) == 0)
  1057. return 1;
  1058. }
  1059. /* nonces are present but not equal */
  1060. return 0;
  1061. }
  1062. #endif /* OPENSSL_EXTRA */
  1063. #else /* HAVE_OCSP */
  1064. #ifdef _MSC_VER
  1065. /* 4206 warning for blank file */
  1066. #pragma warning(disable: 4206)
  1067. #endif
  1068. #endif /* HAVE_OCSP */
  1069. #endif /* WOLFCRYPT_ONLY */