crlv2.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * Code for dealing with x.509 v3 crl and crl entries extensions.
  6. */
  7. #include "cert.h"
  8. #include "secitem.h"
  9. #include "secoid.h"
  10. #include "secoidt.h"
  11. #include "secder.h"
  12. #include "secasn1.h"
  13. #include "certxutl.h"
  14. SECStatus
  15. CERT_FindCRLExtensionByOID(CERTCrl *crl, SECItem *oid, SECItem *value)
  16. {
  17. return (cert_FindExtensionByOID(crl->extensions, oid, value));
  18. }
  19. SECStatus
  20. CERT_FindCRLExtension(CERTCrl *crl, int tag, SECItem *value)
  21. {
  22. return (cert_FindExtension(crl->extensions, tag, value));
  23. }
  24. /* Callback to set extensions and adjust verison */
  25. static void
  26. SetCrlExts(void *object, CERTCertExtension **exts)
  27. {
  28. CERTCrl *crl = (CERTCrl *)object;
  29. crl->extensions = exts;
  30. DER_SetUInteger(crl->arena, &crl->version, SEC_CRL_VERSION_2);
  31. }
  32. void *
  33. CERT_StartCRLExtensions(CERTCrl *crl)
  34. {
  35. return (cert_StartExtensions((void *)crl, crl->arena, SetCrlExts));
  36. }
  37. static void
  38. SetCrlEntryExts(void *object, CERTCertExtension **exts)
  39. {
  40. CERTCrlEntry *crlEntry = (CERTCrlEntry *)object;
  41. crlEntry->extensions = exts;
  42. }
  43. void *
  44. CERT_StartCRLEntryExtensions(CERTCrl *crl, CERTCrlEntry *entry)
  45. {
  46. return (cert_StartExtensions(entry, crl->arena, SetCrlEntryExts));
  47. }
  48. SECStatus
  49. CERT_FindCRLNumberExten(PLArenaPool *arena, CERTCrl *crl,
  50. SECItem *value)
  51. {
  52. SECItem encodedExtenValue;
  53. SECItem *tmpItem = NULL;
  54. SECStatus rv;
  55. void *mark = NULL;
  56. encodedExtenValue.data = NULL;
  57. encodedExtenValue.len = 0;
  58. rv = cert_FindExtension(crl->extensions, SEC_OID_X509_CRL_NUMBER,
  59. &encodedExtenValue);
  60. if (rv != SECSuccess)
  61. return (rv);
  62. mark = PORT_ArenaMark(arena);
  63. tmpItem = SECITEM_ArenaDupItem(arena, &encodedExtenValue);
  64. if (tmpItem) {
  65. rv = SEC_QuickDERDecodeItem(arena, value,
  66. SEC_ASN1_GET(SEC_IntegerTemplate),
  67. tmpItem);
  68. } else {
  69. rv = SECFailure;
  70. }
  71. PORT_Free(encodedExtenValue.data);
  72. if (rv == SECFailure) {
  73. PORT_ArenaRelease(arena, mark);
  74. } else {
  75. PORT_ArenaUnmark(arena, mark);
  76. }
  77. return (rv);
  78. }
  79. SECStatus
  80. CERT_FindCRLEntryReasonExten(CERTCrlEntry *crlEntry,
  81. CERTCRLEntryReasonCode *value)
  82. {
  83. SECItem wrapperItem = { siBuffer, 0 };
  84. SECItem tmpItem = { siBuffer, 0 };
  85. SECStatus rv;
  86. PLArenaPool *arena = NULL;
  87. arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
  88. if (!arena) {
  89. return (SECFailure);
  90. }
  91. rv = cert_FindExtension(crlEntry->extensions, SEC_OID_X509_REASON_CODE,
  92. &wrapperItem);
  93. if (rv != SECSuccess) {
  94. goto loser;
  95. }
  96. rv = SEC_QuickDERDecodeItem(arena, &tmpItem,
  97. SEC_ASN1_GET(SEC_EnumeratedTemplate),
  98. &wrapperItem);
  99. if (rv != SECSuccess) {
  100. goto loser;
  101. }
  102. *value = (CERTCRLEntryReasonCode)DER_GetInteger(&tmpItem);
  103. loser:
  104. if (arena) {
  105. PORT_FreeArena(arena, PR_FALSE);
  106. }
  107. if (wrapperItem.data) {
  108. PORT_Free(wrapperItem.data);
  109. }
  110. return (rv);
  111. }
  112. SECStatus
  113. CERT_FindInvalidDateExten(CERTCrl *crl, PRTime *value)
  114. {
  115. SECItem encodedExtenValue;
  116. SECItem decodedExtenValue = { siBuffer, 0 };
  117. SECStatus rv;
  118. encodedExtenValue.data = decodedExtenValue.data = NULL;
  119. encodedExtenValue.len = decodedExtenValue.len = 0;
  120. rv = cert_FindExtension(crl->extensions, SEC_OID_X509_INVALID_DATE, &encodedExtenValue);
  121. if (rv != SECSuccess)
  122. return (rv);
  123. rv = SEC_ASN1DecodeItem(NULL, &decodedExtenValue,
  124. SEC_ASN1_GET(SEC_GeneralizedTimeTemplate),
  125. &encodedExtenValue);
  126. if (rv == SECSuccess)
  127. rv = DER_GeneralizedTimeToTime(value, &encodedExtenValue);
  128. PORT_Free(decodedExtenValue.data);
  129. PORT_Free(encodedExtenValue.data);
  130. return (rv);
  131. }