xcrldist.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Distribution Point extension.
  6. */
  7. #include "genname.h"
  8. #include "certt.h"
  9. #include "secerr.h"
  10. SEC_ASN1_MKSUB(SEC_AnyTemplate)
  11. SEC_ASN1_MKSUB(SEC_BitStringTemplate)
  12. extern void PrepareBitStringForEncoding(SECItem *bitMap, SECItem *value);
  13. static const SEC_ASN1Template FullNameTemplate[] = {
  14. { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0,
  15. offsetof(CRLDistributionPoint, derFullName),
  16. CERT_GeneralNamesTemplate }
  17. };
  18. static const SEC_ASN1Template RelativeNameTemplate[] = {
  19. { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 1,
  20. offsetof(CRLDistributionPoint, distPoint.relativeName),
  21. CERT_RDNTemplate }
  22. };
  23. static const SEC_ASN1Template DistributionPointNameTemplate[] = {
  24. { SEC_ASN1_CHOICE,
  25. offsetof(CRLDistributionPoint, distPointType), NULL,
  26. sizeof(CRLDistributionPoint) },
  27. { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0,
  28. offsetof(CRLDistributionPoint, derFullName),
  29. CERT_GeneralNamesTemplate, generalName },
  30. { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 1,
  31. offsetof(CRLDistributionPoint, distPoint.relativeName),
  32. CERT_RDNTemplate, relativeDistinguishedName },
  33. { 0 }
  34. };
  35. static const SEC_ASN1Template CRLDistributionPointTemplate[] = {
  36. { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CRLDistributionPoint) },
  37. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC |
  38. SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | SEC_ASN1_XTRN | 0,
  39. offsetof(CRLDistributionPoint, derDistPoint),
  40. SEC_ASN1_SUB(SEC_AnyTemplate) },
  41. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 1,
  42. offsetof(CRLDistributionPoint, bitsmap),
  43. SEC_ASN1_SUB(SEC_BitStringTemplate) },
  44. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC |
  45. SEC_ASN1_CONSTRUCTED | 2,
  46. offsetof(CRLDistributionPoint, derCrlIssuer),
  47. CERT_GeneralNamesTemplate },
  48. { 0 }
  49. };
  50. const SEC_ASN1Template CERTCRLDistributionPointsTemplate[] = {
  51. { SEC_ASN1_SEQUENCE_OF, 0, CRLDistributionPointTemplate }
  52. };
  53. SECStatus
  54. CERT_EncodeCRLDistributionPoints(PLArenaPool *arena,
  55. CERTCrlDistributionPoints *value,
  56. SECItem *derValue)
  57. {
  58. CRLDistributionPoint **pointList, *point;
  59. PLArenaPool *ourPool = NULL;
  60. SECStatus rv = SECSuccess;
  61. PORT_Assert(derValue);
  62. PORT_Assert(value && value->distPoints);
  63. do {
  64. ourPool = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
  65. if (ourPool == NULL) {
  66. rv = SECFailure;
  67. break;
  68. }
  69. pointList = value->distPoints;
  70. while (*pointList) {
  71. point = *pointList;
  72. point->derFullName = NULL;
  73. point->derDistPoint.data = NULL;
  74. switch (point->distPointType) {
  75. case generalName:
  76. point->derFullName = cert_EncodeGeneralNames(ourPool, point->distPoint.fullName);
  77. if (!point->derFullName ||
  78. !SEC_ASN1EncodeItem(ourPool, &point->derDistPoint,
  79. point, FullNameTemplate))
  80. rv = SECFailure;
  81. break;
  82. case relativeDistinguishedName:
  83. if (!SEC_ASN1EncodeItem(ourPool, &point->derDistPoint,
  84. point, RelativeNameTemplate))
  85. rv = SECFailure;
  86. break;
  87. default:
  88. PORT_SetError(SEC_ERROR_EXTENSION_VALUE_INVALID);
  89. rv = SECFailure;
  90. break;
  91. }
  92. if (rv != SECSuccess)
  93. break;
  94. if (point->reasons.data)
  95. PrepareBitStringForEncoding(&point->bitsmap, &point->reasons);
  96. if (point->crlIssuer) {
  97. point->derCrlIssuer = cert_EncodeGeneralNames(ourPool, point->crlIssuer);
  98. if (!point->derCrlIssuer) {
  99. rv = SECFailure;
  100. break;
  101. }
  102. }
  103. ++pointList;
  104. }
  105. if (rv != SECSuccess)
  106. break;
  107. if (!SEC_ASN1EncodeItem(arena, derValue, value,
  108. CERTCRLDistributionPointsTemplate)) {
  109. rv = SECFailure;
  110. break;
  111. }
  112. } while (0);
  113. PORT_FreeArena(ourPool, PR_FALSE);
  114. return rv;
  115. }
  116. CERTCrlDistributionPoints *
  117. CERT_DecodeCRLDistributionPoints(PLArenaPool *arena, SECItem *encodedValue)
  118. {
  119. CERTCrlDistributionPoints *value = NULL;
  120. CRLDistributionPoint **pointList, *point;
  121. SECStatus rv = SECSuccess;
  122. SECItem newEncodedValue;
  123. PORT_Assert(arena);
  124. do {
  125. value = PORT_ArenaZNew(arena, CERTCrlDistributionPoints);
  126. if (value == NULL) {
  127. rv = SECFailure;
  128. break;
  129. }
  130. /* copy the DER into the arena, since Quick DER returns data that points
  131. into the DER input, which may get freed by the caller */
  132. rv = SECITEM_CopyItem(arena, &newEncodedValue, encodedValue);
  133. if (rv != SECSuccess)
  134. break;
  135. rv = SEC_QuickDERDecodeItem(arena, &value->distPoints,
  136. CERTCRLDistributionPointsTemplate, &newEncodedValue);
  137. if (rv != SECSuccess)
  138. break;
  139. pointList = value->distPoints;
  140. while (NULL != (point = *pointList)) {
  141. /* get the data if the distributionPointName is not omitted */
  142. if (point->derDistPoint.data != NULL) {
  143. rv = SEC_QuickDERDecodeItem(arena, point,
  144. DistributionPointNameTemplate, &(point->derDistPoint));
  145. if (rv != SECSuccess)
  146. break;
  147. switch (point->distPointType) {
  148. case generalName:
  149. point->distPoint.fullName =
  150. cert_DecodeGeneralNames(arena, point->derFullName);
  151. rv = point->distPoint.fullName ? SECSuccess : SECFailure;
  152. break;
  153. case relativeDistinguishedName:
  154. break;
  155. default:
  156. PORT_SetError(SEC_ERROR_EXTENSION_VALUE_INVALID);
  157. rv = SECFailure;
  158. break;
  159. } /* end switch */
  160. if (rv != SECSuccess)
  161. break;
  162. } /* end if */
  163. /* Get the reason code if it's not omitted in the encoding */
  164. if (point->bitsmap.data != NULL) {
  165. SECItem bitsmap = point->bitsmap;
  166. DER_ConvertBitString(&bitsmap);
  167. rv = SECITEM_CopyItem(arena, &point->reasons, &bitsmap);
  168. if (rv != SECSuccess)
  169. break;
  170. }
  171. /* Get the crl issuer name if it's not omitted in the encoding */
  172. if (point->derCrlIssuer != NULL) {
  173. point->crlIssuer = cert_DecodeGeneralNames(arena,
  174. point->derCrlIssuer);
  175. if (!point->crlIssuer)
  176. break;
  177. }
  178. ++pointList;
  179. } /* end while points remain */
  180. } while (0);
  181. return (rv == SECSuccess ? value : NULL);
  182. }