ocsp.c 37 KB

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