respcli.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* -*- Mode: C; tab-width: 8 -*-*/
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * This file will contain all routines needed by a client that has
  7. * to parse a CMMFCertRepContent structure and retirieve the appropriate
  8. * data.
  9. */
  10. #include "cmmf.h"
  11. #include "cmmfi.h"
  12. #include "crmf.h"
  13. #include "crmfi.h"
  14. #include "secitem.h"
  15. #include "secder.h"
  16. #include "secasn1.h"
  17. CMMFCertRepContent *
  18. CMMF_CreateCertRepContentFromDER(CERTCertDBHandle *db, const char *buf,
  19. long len)
  20. {
  21. PLArenaPool *poolp;
  22. CMMFCertRepContent *certRepContent;
  23. SECStatus rv;
  24. int i;
  25. poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
  26. if (poolp == NULL) {
  27. return NULL;
  28. }
  29. certRepContent = PORT_ArenaZNew(poolp, CMMFCertRepContent);
  30. if (certRepContent == NULL) {
  31. goto loser;
  32. }
  33. certRepContent->poolp = poolp;
  34. rv = SEC_ASN1Decode(poolp, certRepContent, CMMFCertRepContentTemplate,
  35. buf, len);
  36. if (rv != SECSuccess) {
  37. goto loser;
  38. }
  39. if (certRepContent->response != NULL) {
  40. for (i = 0; certRepContent->response[i] != NULL; i++) {
  41. rv = cmmf_decode_process_cert_response(poolp, db,
  42. certRepContent->response[i]);
  43. if (rv != SECSuccess) {
  44. goto loser;
  45. }
  46. }
  47. }
  48. certRepContent->isDecoded = PR_TRUE;
  49. return certRepContent;
  50. loser:
  51. PORT_FreeArena(poolp, PR_FALSE);
  52. return NULL;
  53. }
  54. long
  55. CMMF_CertResponseGetCertReqId(CMMFCertResponse *inCertResp)
  56. {
  57. PORT_Assert(inCertResp != NULL);
  58. if (inCertResp == NULL) {
  59. return -1;
  60. }
  61. return DER_GetInteger(&inCertResp->certReqId);
  62. }
  63. PRBool
  64. cmmf_CertRepContentIsIndexValid(CMMFCertRepContent *inCertRepContent,
  65. int inIndex)
  66. {
  67. int numResponses;
  68. PORT_Assert(inCertRepContent != NULL);
  69. numResponses = CMMF_CertRepContentGetNumResponses(inCertRepContent);
  70. return (PRBool)(inIndex >= 0 && inIndex < numResponses);
  71. }
  72. CMMFCertResponse *
  73. CMMF_CertRepContentGetResponseAtIndex(CMMFCertRepContent *inCertRepContent,
  74. int inIndex)
  75. {
  76. CMMFCertResponse *certResponse;
  77. SECStatus rv;
  78. PORT_Assert(inCertRepContent != NULL &&
  79. cmmf_CertRepContentIsIndexValid(inCertRepContent, inIndex));
  80. if (inCertRepContent == NULL ||
  81. !cmmf_CertRepContentIsIndexValid(inCertRepContent, inIndex)) {
  82. return NULL;
  83. }
  84. certResponse = PORT_ZNew(CMMFCertResponse);
  85. if (certResponse) {
  86. rv = cmmf_CopyCertResponse(NULL, certResponse,
  87. inCertRepContent->response[inIndex]);
  88. if (rv != SECSuccess) {
  89. CMMF_DestroyCertResponse(certResponse);
  90. certResponse = NULL;
  91. }
  92. }
  93. return certResponse;
  94. }
  95. CMMFPKIStatus
  96. CMMF_CertResponseGetPKIStatusInfoStatus(CMMFCertResponse *inCertResp)
  97. {
  98. PORT_Assert(inCertResp != NULL);
  99. if (inCertResp == NULL) {
  100. return cmmfNoPKIStatus;
  101. }
  102. return cmmf_PKIStatusInfoGetStatus(&inCertResp->status);
  103. }
  104. CERTCertificate *
  105. CMMF_CertResponseGetCertificate(CMMFCertResponse *inCertResp,
  106. CERTCertDBHandle *inCertdb)
  107. {
  108. PORT_Assert(inCertResp != NULL);
  109. if (inCertResp == NULL || inCertResp->certifiedKeyPair == NULL) {
  110. return NULL;
  111. }
  112. return cmmf_CertOrEncCertGetCertificate(
  113. &inCertResp->certifiedKeyPair->certOrEncCert, inCertdb);
  114. }
  115. CERTCertList *
  116. CMMF_CertRepContentGetCAPubs(CMMFCertRepContent *inCertRepContent)
  117. {
  118. PORT_Assert(inCertRepContent != NULL);
  119. if (inCertRepContent == NULL || inCertRepContent->caPubs == NULL) {
  120. return NULL;
  121. }
  122. return cmmf_MakeCertList(inCertRepContent->caPubs);
  123. }