ocsp.c 45 KB

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