Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /* |
michael@0 | 6 | * Code for dealing with x.509 v3 CRL Distribution Point extension. |
michael@0 | 7 | */ |
michael@0 | 8 | #include "genname.h" |
michael@0 | 9 | #include "certt.h" |
michael@0 | 10 | #include "secerr.h" |
michael@0 | 11 | |
michael@0 | 12 | SEC_ASN1_MKSUB(SEC_AnyTemplate) |
michael@0 | 13 | SEC_ASN1_MKSUB(SEC_BitStringTemplate) |
michael@0 | 14 | |
michael@0 | 15 | extern void PrepareBitStringForEncoding (SECItem *bitMap, SECItem *value); |
michael@0 | 16 | |
michael@0 | 17 | static const SEC_ASN1Template FullNameTemplate[] = { |
michael@0 | 18 | {SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0, |
michael@0 | 19 | offsetof (CRLDistributionPoint,derFullName), |
michael@0 | 20 | CERT_GeneralNamesTemplate} |
michael@0 | 21 | }; |
michael@0 | 22 | |
michael@0 | 23 | static const SEC_ASN1Template RelativeNameTemplate[] = { |
michael@0 | 24 | {SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 1, |
michael@0 | 25 | offsetof (CRLDistributionPoint,distPoint.relativeName), |
michael@0 | 26 | CERT_RDNTemplate} |
michael@0 | 27 | }; |
michael@0 | 28 | |
michael@0 | 29 | static const SEC_ASN1Template DistributionPointNameTemplate[] = { |
michael@0 | 30 | { SEC_ASN1_CHOICE, |
michael@0 | 31 | offsetof(CRLDistributionPoint, distPointType), NULL, |
michael@0 | 32 | sizeof(CRLDistributionPoint) }, |
michael@0 | 33 | { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 0, |
michael@0 | 34 | offsetof (CRLDistributionPoint, derFullName), |
michael@0 | 35 | CERT_GeneralNamesTemplate, generalName }, |
michael@0 | 36 | { SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_CONSTRUCTED | 1, |
michael@0 | 37 | offsetof (CRLDistributionPoint, distPoint.relativeName), |
michael@0 | 38 | CERT_RDNTemplate, relativeDistinguishedName }, |
michael@0 | 39 | { 0 } |
michael@0 | 40 | }; |
michael@0 | 41 | |
michael@0 | 42 | static const SEC_ASN1Template CRLDistributionPointTemplate[] = { |
michael@0 | 43 | { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CRLDistributionPoint) }, |
michael@0 | 44 | { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | |
michael@0 | 45 | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | SEC_ASN1_XTRN | 0, |
michael@0 | 46 | offsetof(CRLDistributionPoint,derDistPoint), |
michael@0 | 47 | SEC_ASN1_SUB(SEC_AnyTemplate)}, |
michael@0 | 48 | { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 1, |
michael@0 | 49 | offsetof(CRLDistributionPoint,bitsmap), |
michael@0 | 50 | SEC_ASN1_SUB(SEC_BitStringTemplate) }, |
michael@0 | 51 | { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | |
michael@0 | 52 | SEC_ASN1_CONSTRUCTED | 2, |
michael@0 | 53 | offsetof(CRLDistributionPoint, derCrlIssuer), |
michael@0 | 54 | CERT_GeneralNamesTemplate}, |
michael@0 | 55 | { 0 } |
michael@0 | 56 | }; |
michael@0 | 57 | |
michael@0 | 58 | const SEC_ASN1Template CERTCRLDistributionPointsTemplate[] = { |
michael@0 | 59 | {SEC_ASN1_SEQUENCE_OF, 0, CRLDistributionPointTemplate} |
michael@0 | 60 | }; |
michael@0 | 61 | |
michael@0 | 62 | SECStatus |
michael@0 | 63 | CERT_EncodeCRLDistributionPoints (PLArenaPool *arena, |
michael@0 | 64 | CERTCrlDistributionPoints *value, |
michael@0 | 65 | SECItem *derValue) |
michael@0 | 66 | { |
michael@0 | 67 | CRLDistributionPoint **pointList, *point; |
michael@0 | 68 | PLArenaPool *ourPool = NULL; |
michael@0 | 69 | SECStatus rv = SECSuccess; |
michael@0 | 70 | |
michael@0 | 71 | PORT_Assert (derValue); |
michael@0 | 72 | PORT_Assert (value && value->distPoints); |
michael@0 | 73 | |
michael@0 | 74 | do { |
michael@0 | 75 | ourPool = PORT_NewArena (SEC_ASN1_DEFAULT_ARENA_SIZE); |
michael@0 | 76 | if (ourPool == NULL) { |
michael@0 | 77 | rv = SECFailure; |
michael@0 | 78 | break; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | pointList = value->distPoints; |
michael@0 | 82 | while (*pointList) { |
michael@0 | 83 | point = *pointList; |
michael@0 | 84 | point->derFullName = NULL; |
michael@0 | 85 | point->derDistPoint.data = NULL; |
michael@0 | 86 | |
michael@0 | 87 | switch (point->distPointType) { |
michael@0 | 88 | case generalName: |
michael@0 | 89 | point->derFullName = cert_EncodeGeneralNames |
michael@0 | 90 | (ourPool, point->distPoint.fullName); |
michael@0 | 91 | |
michael@0 | 92 | if (!point->derFullName || |
michael@0 | 93 | !SEC_ASN1EncodeItem (ourPool, &point->derDistPoint, |
michael@0 | 94 | point, FullNameTemplate)) |
michael@0 | 95 | rv = SECFailure; |
michael@0 | 96 | break; |
michael@0 | 97 | |
michael@0 | 98 | case relativeDistinguishedName: |
michael@0 | 99 | if (!SEC_ASN1EncodeItem(ourPool, &point->derDistPoint, |
michael@0 | 100 | point, RelativeNameTemplate)) |
michael@0 | 101 | rv = SECFailure; |
michael@0 | 102 | break; |
michael@0 | 103 | |
michael@0 | 104 | /* distributionPointName is omitted */ |
michael@0 | 105 | case 0: break; |
michael@0 | 106 | |
michael@0 | 107 | default: |
michael@0 | 108 | PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID); |
michael@0 | 109 | rv = SECFailure; |
michael@0 | 110 | break; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | if (rv != SECSuccess) |
michael@0 | 114 | break; |
michael@0 | 115 | |
michael@0 | 116 | if (point->reasons.data) |
michael@0 | 117 | PrepareBitStringForEncoding (&point->bitsmap, &point->reasons); |
michael@0 | 118 | |
michael@0 | 119 | if (point->crlIssuer) { |
michael@0 | 120 | point->derCrlIssuer = cert_EncodeGeneralNames |
michael@0 | 121 | (ourPool, point->crlIssuer); |
michael@0 | 122 | if (!point->derCrlIssuer) { |
michael@0 | 123 | rv = SECFailure; |
michael@0 | 124 | break; |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | ++pointList; |
michael@0 | 128 | } |
michael@0 | 129 | if (rv != SECSuccess) |
michael@0 | 130 | break; |
michael@0 | 131 | if (!SEC_ASN1EncodeItem(arena, derValue, value, |
michael@0 | 132 | CERTCRLDistributionPointsTemplate)) { |
michael@0 | 133 | rv = SECFailure; |
michael@0 | 134 | break; |
michael@0 | 135 | } |
michael@0 | 136 | } while (0); |
michael@0 | 137 | PORT_FreeArena (ourPool, PR_FALSE); |
michael@0 | 138 | return rv; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | CERTCrlDistributionPoints * |
michael@0 | 142 | CERT_DecodeCRLDistributionPoints (PLArenaPool *arena, SECItem *encodedValue) |
michael@0 | 143 | { |
michael@0 | 144 | CERTCrlDistributionPoints *value = NULL; |
michael@0 | 145 | CRLDistributionPoint **pointList, *point; |
michael@0 | 146 | SECStatus rv = SECSuccess; |
michael@0 | 147 | SECItem newEncodedValue; |
michael@0 | 148 | |
michael@0 | 149 | PORT_Assert (arena); |
michael@0 | 150 | do { |
michael@0 | 151 | value = PORT_ArenaZNew(arena, CERTCrlDistributionPoints); |
michael@0 | 152 | if (value == NULL) { |
michael@0 | 153 | rv = SECFailure; |
michael@0 | 154 | break; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | /* copy the DER into the arena, since Quick DER returns data that points |
michael@0 | 158 | into the DER input, which may get freed by the caller */ |
michael@0 | 159 | rv = SECITEM_CopyItem(arena, &newEncodedValue, encodedValue); |
michael@0 | 160 | if (rv != SECSuccess) |
michael@0 | 161 | break; |
michael@0 | 162 | |
michael@0 | 163 | rv = SEC_QuickDERDecodeItem(arena, &value->distPoints, |
michael@0 | 164 | CERTCRLDistributionPointsTemplate, &newEncodedValue); |
michael@0 | 165 | if (rv != SECSuccess) |
michael@0 | 166 | break; |
michael@0 | 167 | |
michael@0 | 168 | pointList = value->distPoints; |
michael@0 | 169 | while (NULL != (point = *pointList)) { |
michael@0 | 170 | |
michael@0 | 171 | /* get the data if the distributionPointName is not omitted */ |
michael@0 | 172 | if (point->derDistPoint.data != NULL) { |
michael@0 | 173 | rv = SEC_QuickDERDecodeItem(arena, point, |
michael@0 | 174 | DistributionPointNameTemplate, &(point->derDistPoint)); |
michael@0 | 175 | if (rv != SECSuccess) |
michael@0 | 176 | break; |
michael@0 | 177 | |
michael@0 | 178 | switch (point->distPointType) { |
michael@0 | 179 | case generalName: |
michael@0 | 180 | point->distPoint.fullName = |
michael@0 | 181 | cert_DecodeGeneralNames(arena, point->derFullName); |
michael@0 | 182 | rv = point->distPoint.fullName ? SECSuccess : SECFailure; |
michael@0 | 183 | break; |
michael@0 | 184 | |
michael@0 | 185 | case relativeDistinguishedName: |
michael@0 | 186 | break; |
michael@0 | 187 | |
michael@0 | 188 | default: |
michael@0 | 189 | PORT_SetError (SEC_ERROR_EXTENSION_VALUE_INVALID); |
michael@0 | 190 | rv = SECFailure; |
michael@0 | 191 | break; |
michael@0 | 192 | } /* end switch */ |
michael@0 | 193 | if (rv != SECSuccess) |
michael@0 | 194 | break; |
michael@0 | 195 | } /* end if */ |
michael@0 | 196 | |
michael@0 | 197 | /* Get the reason code if it's not omitted in the encoding */ |
michael@0 | 198 | if (point->bitsmap.data != NULL) { |
michael@0 | 199 | SECItem bitsmap = point->bitsmap; |
michael@0 | 200 | DER_ConvertBitString(&bitsmap); |
michael@0 | 201 | rv = SECITEM_CopyItem(arena, &point->reasons, &bitsmap); |
michael@0 | 202 | if (rv != SECSuccess) |
michael@0 | 203 | break; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | /* Get the crl issuer name if it's not omitted in the encoding */ |
michael@0 | 207 | if (point->derCrlIssuer != NULL) { |
michael@0 | 208 | point->crlIssuer = cert_DecodeGeneralNames(arena, |
michael@0 | 209 | point->derCrlIssuer); |
michael@0 | 210 | if (!point->crlIssuer) |
michael@0 | 211 | break; |
michael@0 | 212 | } |
michael@0 | 213 | ++pointList; |
michael@0 | 214 | } /* end while points remain */ |
michael@0 | 215 | } while (0); |
michael@0 | 216 | return (rv == SECSuccess ? value : NULL); |
michael@0 | 217 | } |