ocsp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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 = NULL;
  490. CertStatus* certStatus = NULL;
  491. WOLFSSL_CERT_MANAGER* cm = NULL;
  492. int ret = -1;
  493. DerBuffer* derCert = NULL;
  494. #ifdef WOLFSSL_SMALL_STACK
  495. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof(*cert), NULL,
  496. DYNAMIC_TYPE_TMP_BUFFER);
  497. if (cert == NULL)
  498. return 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. ret = AllocDer(&derCert, issuer->derCert->length,
  509. issuer->derCert->type, NULL);
  510. if (ret == 0) {
  511. /* AddCA() frees the buffer. */
  512. XMEMCPY(derCert->buffer, issuer->derCert->buffer,
  513. issuer->derCert->length);
  514. ret = AddCA(cm, &derCert, WOLFSSL_USER_CA, 1);
  515. if (ret != WOLFSSL_SUCCESS) {
  516. goto out;
  517. }
  518. }
  519. ret = -1;
  520. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(WOLFSSL_OCSP_CERTID), NULL,
  521. DYNAMIC_TYPE_OPENSSL);
  522. if (certId == NULL)
  523. goto out;
  524. certStatus = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  525. DYNAMIC_TYPE_OPENSSL);
  526. if (certStatus == NULL)
  527. goto out;
  528. XMEMSET(certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  529. XMEMSET(certStatus, 0, sizeof(CertStatus));
  530. certId->status = certStatus;
  531. certId->ownStatus = 1;
  532. InitDecodedCert(cert, subject->derCert->buffer,
  533. subject->derCert->length, NULL);
  534. if (ParseCertRelative(cert, CERT_TYPE, VERIFY_OCSP, cm) != 0) {
  535. goto out;
  536. }
  537. else {
  538. XMEMCPY(certId->issuerHash, cert->issuerHash, OCSP_DIGEST_SIZE);
  539. XMEMCPY(certId->issuerKeyHash, cert->issuerKeyHash, OCSP_DIGEST_SIZE);
  540. XMEMCPY(certId->status->serial, cert->serial, cert->serialSz);
  541. certId->status->serialSz = cert->serialSz;
  542. FreeDecodedCert(cert);
  543. }
  544. ret = 0;
  545. out:
  546. if (cm != NULL)
  547. wolfSSL_CertManagerFree(cm);
  548. #ifdef WOLFSSL_SMALL_STACK
  549. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  550. #endif
  551. if (ret != 0) {
  552. if (certId != NULL)
  553. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  554. if (certStatus)
  555. XFREE(certStatus, NULL, DYNAMIC_TYPE_OPENSSL);
  556. return NULL;
  557. } else
  558. return certId;
  559. }
  560. void wolfSSL_OCSP_BASICRESP_free(WOLFSSL_OCSP_BASICRESP* basicResponse)
  561. {
  562. wolfSSL_OCSP_RESPONSE_free(basicResponse);
  563. }
  564. /* Signature verified in DecodeBasicOcspResponse.
  565. * But no store available to verify certificate. */
  566. int wolfSSL_OCSP_basic_verify(WOLFSSL_OCSP_BASICRESP *bs,
  567. WOLF_STACK_OF(WOLFSSL_X509) *certs, WOLFSSL_X509_STORE *st, unsigned long flags)
  568. {
  569. int ret;
  570. #ifdef WOLFSSL_SMALL_STACK
  571. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof(*cert), NULL,
  572. DYNAMIC_TYPE_TMP_BUFFER);
  573. if (cert == NULL)
  574. return WOLFSSL_FAILURE;
  575. #else
  576. DecodedCert cert[1];
  577. #endif
  578. (void)certs;
  579. if (flags & OCSP_NOVERIFY) {
  580. ret = WOLFSSL_SUCCESS;
  581. goto out;
  582. }
  583. #ifdef OPENSSL_EXTRA
  584. if (bs->verifyError != OCSP_VERIFY_ERROR_NONE) {
  585. ret = WOLFSSL_FAILURE;
  586. goto out;
  587. }
  588. #endif
  589. ret = WOLFSSL_SUCCESS;
  590. InitDecodedCert(cert, bs->cert, bs->certSz, NULL);
  591. if (ParseCertRelative(cert, CERT_TYPE, VERIFY, st->cm) < 0)
  592. ret = WOLFSSL_FAILURE;
  593. FreeDecodedCert(cert);
  594. out:
  595. #ifdef WOLFSSL_SMALL_STACK
  596. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  597. #endif
  598. return ret;
  599. }
  600. void wolfSSL_OCSP_RESPONSE_free(OcspResponse* response)
  601. {
  602. if (response == NULL)
  603. return;
  604. if (response->single != NULL) {
  605. FreeOcspEntry(response->single, NULL);
  606. XFREE(response->single, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  607. }
  608. if (response->source != NULL)
  609. XFREE(response->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  610. XFREE(response, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  611. }
  612. #ifndef NO_BIO
  613. OcspResponse* wolfSSL_d2i_OCSP_RESPONSE_bio(WOLFSSL_BIO* bio,
  614. OcspResponse** response)
  615. {
  616. byte* data;
  617. byte* p;
  618. int len;
  619. int dataAlloced = 0;
  620. OcspResponse* ret = NULL;
  621. if (bio == NULL)
  622. return NULL;
  623. if (bio->type == WOLFSSL_BIO_MEMORY) {
  624. len = wolfSSL_BIO_get_mem_data(bio, &data);
  625. if (len <= 0 || data == NULL) {
  626. return NULL;
  627. }
  628. }
  629. #ifndef NO_FILESYSTEM
  630. else if (bio->type == WOLFSSL_BIO_FILE) {
  631. long fcur;
  632. long flen;
  633. if (bio->ptr == NULL)
  634. return NULL;
  635. fcur = XFTELL((XFILE)bio->ptr);
  636. if (fcur < 0)
  637. return NULL;
  638. if(XFSEEK((XFILE)bio->ptr, 0, SEEK_END) != 0)
  639. return NULL;
  640. flen = XFTELL((XFILE)bio->ptr);
  641. if (flen < 0)
  642. return NULL;
  643. if (XFSEEK((XFILE)bio->ptr, fcur, SEEK_SET) != 0)
  644. return NULL;
  645. /* check calculated length */
  646. fcur = flen - fcur;
  647. if (fcur > MAX_WOLFSSL_FILE_SIZE || fcur <= 0)
  648. return NULL;
  649. data = (byte*)XMALLOC(fcur, 0, DYNAMIC_TYPE_TMP_BUFFER);
  650. if (data == NULL)
  651. return NULL;
  652. dataAlloced = 1;
  653. len = wolfSSL_BIO_read(bio, (char *)data, (int)flen);
  654. }
  655. #endif
  656. else
  657. return NULL;
  658. if (len > 0) {
  659. p = data;
  660. ret = wolfSSL_d2i_OCSP_RESPONSE(response, (const unsigned char **)&p,
  661. len);
  662. }
  663. if (dataAlloced)
  664. XFREE(data, 0, DYNAMIC_TYPE_TMP_BUFFER);
  665. return ret;
  666. }
  667. #endif /* !NO_BIO */
  668. OcspResponse* wolfSSL_d2i_OCSP_RESPONSE(OcspResponse** response,
  669. const unsigned char** data, int len)
  670. {
  671. OcspResponse *resp = NULL;
  672. word32 idx = 0;
  673. int length = 0;
  674. if (data == NULL)
  675. return NULL;
  676. if (response != NULL)
  677. resp = *response;
  678. if (resp == NULL) {
  679. resp = (OcspResponse*)XMALLOC(sizeof(OcspResponse), NULL,
  680. DYNAMIC_TYPE_OCSP_REQUEST);
  681. if (resp == NULL)
  682. return NULL;
  683. XMEMSET(resp, 0, sizeof(OcspResponse));
  684. }
  685. resp->source = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  686. if (resp->source == NULL) {
  687. XFREE(resp, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  688. return NULL;
  689. }
  690. resp->single = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  691. DYNAMIC_TYPE_OCSP_ENTRY);
  692. if (resp->single == NULL) {
  693. XFREE(resp->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  694. XFREE(resp, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  695. return NULL;
  696. }
  697. XMEMSET(resp->single, 0, sizeof(OcspEntry));
  698. resp->single->status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  699. DYNAMIC_TYPE_OCSP_STATUS);
  700. resp->single->ownStatus = 1;
  701. if (resp->single->status == NULL) {
  702. XFREE(resp->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  703. XFREE(resp->single, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  704. XFREE(resp, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
  705. return NULL;
  706. }
  707. XMEMSET(resp->single->status, 0, sizeof(CertStatus));
  708. XMEMCPY(resp->source, *data, len);
  709. resp->maxIdx = len;
  710. if (OcspResponseDecode(resp, NULL, NULL, 1) != 0) {
  711. wolfSSL_OCSP_RESPONSE_free(resp);
  712. return NULL;
  713. }
  714. if (GetSequence(*data, &idx, &length, len) >= 0)
  715. (*data) += idx + length;
  716. return resp;
  717. }
  718. int wolfSSL_i2d_OCSP_RESPONSE(OcspResponse* response,
  719. unsigned char** data)
  720. {
  721. if (data == NULL)
  722. return response->maxIdx;
  723. XMEMCPY(*data, response->source, response->maxIdx);
  724. return response->maxIdx;
  725. }
  726. int wolfSSL_OCSP_response_status(OcspResponse *response)
  727. {
  728. return response->responseStatus;
  729. }
  730. const char *wolfSSL_OCSP_response_status_str(long s)
  731. {
  732. switch (s) {
  733. case OCSP_SUCCESSFUL:
  734. return "successful";
  735. case OCSP_MALFORMED_REQUEST:
  736. return "malformedrequest";
  737. case OCSP_INTERNAL_ERROR:
  738. return "internalerror";
  739. case OCSP_TRY_LATER:
  740. return "trylater";
  741. case OCSP_SIG_REQUIRED:
  742. return "sigrequired";
  743. case OCSP_UNAUTHORIZED:
  744. return "unauthorized";
  745. default:
  746. return "(UNKNOWN)";
  747. }
  748. }
  749. WOLFSSL_OCSP_BASICRESP* wolfSSL_OCSP_response_get1_basic(OcspResponse* response)
  750. {
  751. WOLFSSL_OCSP_BASICRESP* bs;
  752. bs = (WOLFSSL_OCSP_BASICRESP*)XMALLOC(sizeof(WOLFSSL_OCSP_BASICRESP), NULL,
  753. DYNAMIC_TYPE_OCSP_REQUEST);
  754. if (bs == NULL)
  755. return NULL;
  756. XMEMCPY(bs, response, sizeof(OcspResponse));
  757. bs->single = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  758. DYNAMIC_TYPE_OCSP_ENTRY);
  759. bs->source = (byte*)XMALLOC(bs->maxIdx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  760. if (bs->single == NULL || bs->source == NULL) {
  761. if (bs->single) XFREE(bs->single, NULL, DYNAMIC_TYPE_OCSP_ENTRY);
  762. if (bs->source) XFREE(bs->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  763. wolfSSL_OCSP_RESPONSE_free(bs);
  764. bs = NULL;
  765. }
  766. else {
  767. XMEMCPY(bs->single, response->single, sizeof(OcspEntry));
  768. XMEMCPY(bs->source, response->source, response->maxIdx);
  769. bs->single->ownStatus = 0;
  770. }
  771. return bs;
  772. }
  773. OcspRequest* wolfSSL_OCSP_REQUEST_new(void)
  774. {
  775. OcspRequest* request;
  776. request = (OcspRequest*)XMALLOC(sizeof(OcspRequest), NULL,
  777. DYNAMIC_TYPE_OPENSSL);
  778. if (request != NULL)
  779. XMEMSET(request, 0, sizeof(OcspRequest));
  780. return request;
  781. }
  782. void wolfSSL_OCSP_REQUEST_free(OcspRequest* request)
  783. {
  784. FreeOcspRequest(request);
  785. XFREE(request, NULL, DYNAMIC_TYPE_OPENSSL);
  786. }
  787. int wolfSSL_i2d_OCSP_REQUEST(OcspRequest* request, unsigned char** data)
  788. {
  789. int size;
  790. size = EncodeOcspRequest(request, NULL, 0);
  791. if (size <= 0 || data == NULL)
  792. return size;
  793. return EncodeOcspRequest(request, *data, size);
  794. }
  795. WOLFSSL_OCSP_ONEREQ* wolfSSL_OCSP_request_add0_id(OcspRequest *req,
  796. WOLFSSL_OCSP_CERTID *cid)
  797. {
  798. if (req == NULL || cid == NULL || cid->status == NULL)
  799. return NULL;
  800. XMEMCPY(req->issuerHash, cid->issuerHash, KEYID_SIZE);
  801. XMEMCPY(req->issuerKeyHash, cid->issuerKeyHash, KEYID_SIZE);
  802. if (cid->status->serialSz > req->serialSz) {
  803. if (req->serial != NULL)
  804. XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP);
  805. req->serial = (byte*)XMALLOC(cid->status->serialSz,
  806. req->heap, DYNAMIC_TYPE_OCSP_REQUEST);
  807. if (req->serial == NULL)
  808. return NULL;
  809. }
  810. XMEMCPY(req->serial, cid->status->serial, cid->status->serialSz);
  811. req->serialSz = cid->status->serialSz;
  812. return req;
  813. }
  814. WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_CERTID_dup(WOLFSSL_OCSP_CERTID* id)
  815. {
  816. WOLFSSL_OCSP_CERTID* certId;
  817. if (id == NULL)
  818. return NULL;
  819. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(WOLFSSL_OCSP_CERTID),
  820. NULL, DYNAMIC_TYPE_OPENSSL);
  821. if (certId) {
  822. XMEMCPY(certId, id, sizeof(WOLFSSL_OCSP_CERTID));
  823. }
  824. return certId;
  825. }
  826. #endif
  827. #if defined(OPENSSL_ALL) || defined(APACHE_HTTPD) || defined(WOLFSSL_HAPROXY)
  828. #ifndef NO_BIO
  829. int wolfSSL_i2d_OCSP_REQUEST_bio(WOLFSSL_BIO* out,
  830. WOLFSSL_OCSP_REQUEST *req)
  831. {
  832. int size = -1;
  833. unsigned char* data = NULL;
  834. WOLFSSL_ENTER("wolfSSL_i2d_OCSP_REQUEST_bio");
  835. if (out == NULL || req == NULL)
  836. return WOLFSSL_FAILURE;
  837. size = wolfSSL_i2d_OCSP_REQUEST(req, NULL);
  838. if (size > 0) {
  839. data = (unsigned char*) XMALLOC(size, out->heap,
  840. DYNAMIC_TYPE_TMP_BUFFER);
  841. }
  842. if (data != NULL) {
  843. size = wolfSSL_i2d_OCSP_REQUEST(req, &data);
  844. }
  845. if (size <= 0) {
  846. XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
  847. return WOLFSSL_FAILURE;
  848. }
  849. if (wolfSSL_BIO_write(out, data, size) == (int)size) {
  850. XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
  851. return WOLFSSL_SUCCESS;
  852. }
  853. XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
  854. return WOLFSSL_FAILURE;
  855. }
  856. #endif /* !NO_BIO */
  857. int wolfSSL_i2d_OCSP_CERTID(WOLFSSL_OCSP_CERTID* id, unsigned char** data)
  858. {
  859. if (id == NULL || data == NULL)
  860. return WOLFSSL_FAILURE;
  861. if (*data != NULL) {
  862. XMEMCPY(*data, id->rawCertId, id->rawCertIdSize);
  863. *data = *data + id->rawCertIdSize;
  864. }
  865. else {
  866. *data = (unsigned char*)XMALLOC(id->rawCertIdSize, NULL, DYNAMIC_TYPE_OPENSSL);
  867. if (*data == NULL) {
  868. return WOLFSSL_FAILURE;
  869. }
  870. XMEMCPY(*data, id->rawCertId, id->rawCertIdSize);
  871. }
  872. return id->rawCertIdSize;
  873. }
  874. const WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_SINGLERESP_get0_id(const WOLFSSL_OCSP_SINGLERESP *single)
  875. {
  876. return single;
  877. }
  878. /**
  879. * Compare two WOLFSSL_OCSP_CERTID objects
  880. * @param a
  881. * @param b
  882. * @return 0 on success and when objects have the same id otherwise either
  883. * the id's don't match or an error occurred
  884. */
  885. int wolfSSL_OCSP_id_cmp(WOLFSSL_OCSP_CERTID *a, WOLFSSL_OCSP_CERTID *b)
  886. {
  887. int ret = 0;
  888. if (a == NULL || b == NULL)
  889. return WOLFSSL_FATAL_ERROR;
  890. ret = a->hashAlgoOID != b->hashAlgoOID;
  891. if (ret == 0)
  892. ret = XMEMCMP(a->issuerHash, b->issuerHash, OCSP_DIGEST_SIZE);
  893. if (ret == 0)
  894. ret = XMEMCMP(a->issuerKeyHash, b->issuerKeyHash, OCSP_DIGEST_SIZE);
  895. if (ret == 0) {
  896. if (a->status != NULL && b->status != NULL) {
  897. if (a->status->serialSz == b->status->serialSz)
  898. ret = XMEMCMP(a->status->serial, b->status->serial,
  899. a->status->serialSz);
  900. else
  901. ret = -1;
  902. }
  903. else if (a->status != b->status) {
  904. /* If either is not null then return non-zero */
  905. ret = -1;
  906. }
  907. }
  908. return ret;
  909. }
  910. int wolfSSL_OCSP_single_get0_status(WOLFSSL_OCSP_SINGLERESP *single,
  911. int *reason,
  912. WOLFSSL_ASN1_TIME **revtime,
  913. WOLFSSL_ASN1_TIME **thisupd,
  914. WOLFSSL_ASN1_TIME **nextupd)
  915. {
  916. if (single == NULL)
  917. return WOLFSSL_FAILURE;
  918. if (thisupd != NULL)
  919. *thisupd = &single->status->thisDateParsed;
  920. if (nextupd != NULL)
  921. *nextupd = &single->status->nextDateParsed;
  922. if (reason != NULL)
  923. *reason = 0;
  924. if (revtime != NULL)
  925. *revtime = NULL;
  926. return single->status->status;
  927. }
  928. int wolfSSL_OCSP_resp_count(WOLFSSL_OCSP_BASICRESP *bs)
  929. {
  930. WOLFSSL_OCSP_SINGLERESP* single;
  931. int count = 0;
  932. if (bs == NULL)
  933. return WOLFSSL_FAILURE;
  934. single = bs->single;
  935. while(single != NULL)
  936. {
  937. ++count;
  938. single = single->next;
  939. }
  940. return count;
  941. }
  942. WOLFSSL_OCSP_SINGLERESP* wolfSSL_OCSP_resp_get0(WOLFSSL_OCSP_BASICRESP *bs, int idx)
  943. {
  944. WOLFSSL_OCSP_SINGLERESP* single;
  945. int currIdx = 0;
  946. if (bs == NULL)
  947. return NULL;
  948. single = bs->single;
  949. while(single != NULL && currIdx != idx)
  950. {
  951. single = single->next;
  952. ++currIdx;
  953. }
  954. return single;
  955. }
  956. #endif /* OPENSSL_ALL || APACHE_HTTPD || WOLFSSL_HAPROXY */
  957. #ifdef OPENSSL_EXTRA
  958. #ifndef NO_WOLFSSL_STUB
  959. int wolfSSL_OCSP_REQUEST_add_ext(OcspRequest* req, WOLFSSL_X509_EXTENSION* ext,
  960. int idx)
  961. {
  962. WOLFSSL_STUB("wolfSSL_OCSP_REQUEST_add_ext");
  963. (void)req;
  964. (void)ext;
  965. (void)idx;
  966. return WOLFSSL_FATAL_ERROR;
  967. }
  968. #endif
  969. #ifndef NO_WOLFSSL_STUB
  970. OcspResponse* wolfSSL_OCSP_response_create(int status,
  971. WOLFSSL_OCSP_BASICRESP* bs)
  972. {
  973. WOLFSSL_STUB("wolfSSL_OCSP_response_create");
  974. (void)status;
  975. (void)bs;
  976. return NULL;
  977. }
  978. #endif
  979. #ifndef NO_WOLFSSL_STUB
  980. const char* wolfSSL_OCSP_crl_reason_str(long s)
  981. {
  982. WOLFSSL_STUB("wolfSSL_OCSP_crl_reason_str");
  983. (void)s;
  984. return NULL;
  985. }
  986. #endif
  987. /* Returns elements of an OCSP_CERTID struct. Currently only supports
  988. * returning the serial number, and returns an error if user requests
  989. * any of name, pmd, and/or keyHash.
  990. * Return 1 on success, 0 on failure */
  991. int wolfSSL_OCSP_id_get0_info(WOLFSSL_ASN1_STRING **name,
  992. WOLFSSL_ASN1_OBJECT **pmd, WOLFSSL_ASN1_STRING **keyHash,
  993. WOLFSSL_ASN1_INTEGER **serial, WOLFSSL_OCSP_CERTID *cid)
  994. {
  995. int i = 0;
  996. WOLFSSL_ASN1_INTEGER* ser;
  997. WOLFSSL_ENTER("wolfSSL_OCSP_id_get0_info");
  998. if (cid == NULL)
  999. return 0;
  1000. /* build up ASN1_INTEGER for serial */
  1001. if (serial != NULL) {
  1002. ser = wolfSSL_ASN1_INTEGER_new();
  1003. if (ser == NULL)
  1004. return 0;
  1005. if (cid->status->serialSz > (WOLFSSL_ASN1_INTEGER_MAX - 2)) {
  1006. /* allocate data buffer, +2 for type and length */
  1007. ser->data = (unsigned char*)XMALLOC(cid->status->serialSz + 2, NULL,
  1008. DYNAMIC_TYPE_OPENSSL);
  1009. if (ser->data == NULL) {
  1010. wolfSSL_ASN1_INTEGER_free(ser);
  1011. return 0;
  1012. }
  1013. ser->dataMax = cid->status->serialSz + 2;
  1014. ser->isDynamic = 1;
  1015. } else {
  1016. /* Use array instead of dynamic memory */
  1017. ser->data = ser->intData;
  1018. ser->dataMax = WOLFSSL_ASN1_INTEGER_MAX;
  1019. }
  1020. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  1021. /* Serial number starts at 0 index of ser->data */
  1022. XMEMCPY(&ser->data[i], cid->status->serial, cid->status->serialSz);
  1023. ser->length = cid->status->serialSz;
  1024. #else
  1025. ser->data[i++] = ASN_INTEGER;
  1026. i += SetLength(cid->status->serialSz, ser->data + i);
  1027. XMEMCPY(&ser->data[i], cid->status->serial, cid->status->serialSz);
  1028. ser->length = i + cid->status->serialSz;
  1029. #endif
  1030. cid->status->serialInt = ser;
  1031. *serial = ser;
  1032. }
  1033. /* Not needed for Apache, return error if user is requesting */
  1034. if (name != NULL || pmd != NULL || keyHash != NULL) {
  1035. if (name != NULL)
  1036. *name = NULL;
  1037. if (pmd != NULL)
  1038. *pmd = NULL;
  1039. if (keyHash != NULL)
  1040. *keyHash = NULL;
  1041. return 0;
  1042. }
  1043. return 1;
  1044. }
  1045. int wolfSSL_OCSP_request_add1_nonce(OcspRequest* req, unsigned char* val,
  1046. int sz)
  1047. {
  1048. WC_RNG rng;
  1049. WOLFSSL_ENTER("wolfSSL_OCSP_request_add1_nonce");
  1050. if (req == NULL || sz > MAX_OCSP_NONCE_SZ) {
  1051. WOLFSSL_MSG("Bad parameter");
  1052. return WOLFSSL_FAILURE;
  1053. }
  1054. if (sz <= 0)
  1055. sz = MAX_OCSP_NONCE_SZ;
  1056. if (val != NULL) {
  1057. XMEMCPY(req->nonce, val, sz);
  1058. }
  1059. else {
  1060. if (
  1061. #ifndef HAVE_FIPS
  1062. wc_InitRng_ex(&rng, req->heap, INVALID_DEVID)
  1063. #else
  1064. wc_InitRng(&rng)
  1065. #endif
  1066. != 0) {
  1067. WOLFSSL_MSG("RNG init failed");
  1068. return WOLFSSL_FAILURE;
  1069. }
  1070. if (wc_RNG_GenerateBlock(&rng, req->nonce, sz) != 0) {
  1071. WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
  1072. wc_FreeRng(&rng);
  1073. return WOLFSSL_FAILURE;
  1074. }
  1075. wc_FreeRng(&rng);
  1076. }
  1077. req->nonceSz = sz;
  1078. return WOLFSSL_SUCCESS;
  1079. }
  1080. /* Returns result of OCSP nonce comparison. Return values:
  1081. * 1 - nonces are both present and equal
  1082. * 2 - both nonces are absent
  1083. * 3 - nonce only present in response
  1084. * -1 - nonce only present in request
  1085. * 0 - both nonces present and equal
  1086. */
  1087. int wolfSSL_OCSP_check_nonce(OcspRequest* req, WOLFSSL_OCSP_BASICRESP* bs)
  1088. {
  1089. byte* reqNonce = NULL;
  1090. byte* rspNonce = NULL;
  1091. int reqNonceSz = 0;
  1092. int rspNonceSz = 0;
  1093. WOLFSSL_ENTER("wolfSSL_OCSP_check_nonce");
  1094. if (req != NULL) {
  1095. reqNonce = req->nonce;
  1096. reqNonceSz = req->nonceSz;
  1097. }
  1098. if (bs != NULL) {
  1099. rspNonce = bs->nonce;
  1100. rspNonceSz = bs->nonceSz;
  1101. }
  1102. /* nonce absent in both req and rsp */
  1103. if (reqNonce == NULL && rspNonce == NULL)
  1104. return 2;
  1105. /* nonce present in rsp only */
  1106. if (reqNonce == NULL && rspNonce != NULL)
  1107. return 3;
  1108. /* nonce present in req only */
  1109. if (reqNonce != NULL && rspNonce == NULL)
  1110. return -1;
  1111. /* nonces are present and equal, return 1. Extra NULL check for fixing
  1112. scan-build warning. */
  1113. if (reqNonceSz == rspNonceSz && reqNonce && rspNonce) {
  1114. if (XMEMCMP(reqNonce, rspNonce, reqNonceSz) == 0)
  1115. return 1;
  1116. }
  1117. /* nonces are present but not equal */
  1118. return 0;
  1119. }
  1120. #endif /* OPENSSL_EXTRA */
  1121. #else /* HAVE_OCSP */
  1122. #ifdef _MSC_VER
  1123. /* 4206 warning for blank file */
  1124. #pragma warning(disable: 4206)
  1125. #endif
  1126. #endif /* HAVE_OCSP */
  1127. #endif /* WOLFCRYPT_ONLY */