ocsp.c 40 KB

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