| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 |
| 18 | #include <openssl/asn1.h> |
| 19 | #include <openssl/asn1t.h> |
| 20 | #include <openssl/conf.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/obj.h> |
| 23 | #include <openssl/x509.h> |
| 24 |
| 25 | #include "ext_dat.h" |
| 26 | #include "internal.h" |
| 27 |
| 28 |
| 29 | static STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS( |
| 30 | const X509V3_EXT_METHOD *method, void *bcons, |
| 31 | STACK_OF(CONF_VALUE) *extlist); |
| 32 | static void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method, |
| 33 | const X509V3_CTX *ctx, |
| 34 | const STACK_OF(CONF_VALUE) *values); |
| 35 |
| 36 | const X509V3_EXT_METHOD v3_policy_constraints = { |
| 37 | NID_policy_constraints, |
| 38 | 0, |
| 39 | ASN1_ITEM_ref(POLICY_CONSTRAINTS), |
| 40 | 0, |
| 41 | 0, |
| 42 | 0, |
| 43 | 0, |
| 44 | 0, |
| 45 | 0, |
| 46 | i2v_POLICY_CONSTRAINTS, |
| 47 | v2i_POLICY_CONSTRAINTS, |
| 48 | NULL, |
| 49 | NULL, |
| 50 | NULL}; |
| 51 |
| 52 | ASN1_SEQUENCE(POLICY_CONSTRAINTS) = { |
| 53 | ASN1_IMP_OPT(POLICY_CONSTRAINTS, requireExplicitPolicy, ASN1_INTEGER, 0), |
| 54 | ASN1_IMP_OPT(POLICY_CONSTRAINTS, inhibitPolicyMapping, ASN1_INTEGER, 1), |
| 55 | } ASN1_SEQUENCE_END(POLICY_CONSTRAINTS) |
| 56 |
| 57 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_CONSTRAINTS) |
| 58 |
| 59 | static STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS( |
| 60 | const X509V3_EXT_METHOD *method, void *a, STACK_OF(CONF_VALUE) *extlist) { |
| 61 | const POLICY_CONSTRAINTS *pcons = reinterpret_cast<POLICY_CONSTRAINTS *>(a); |
| 62 | X509V3_add_value_int("Require Explicit Policy", pcons->requireExplicitPolicy, |
| 63 | &extlist); |
| 64 | X509V3_add_value_int("Inhibit Policy Mapping", pcons->inhibitPolicyMapping, |
| 65 | &extlist); |
| 66 | return extlist; |
| 67 | } |
| 68 |
| 69 | static void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method, |
| 70 | const X509V3_CTX *ctx, |
| 71 | const STACK_OF(CONF_VALUE) *values) { |
| 72 | POLICY_CONSTRAINTS *pcons = NULL; |
| 73 | if (!(pcons = POLICY_CONSTRAINTS_new())) { |
| 74 | return NULL; |
| 75 | } |
| 76 | for (size_t i = 0; i < sk_CONF_VALUE_num(values); i++) { |
| 77 | const CONF_VALUE *val = sk_CONF_VALUE_value(values, i); |
| 78 | if (!strcmp(val->name, "requireExplicitPolicy")) { |
| 79 | if (!X509V3_get_value_int(val, &pcons->requireExplicitPolicy)) { |
| 80 | goto err; |
| 81 | } |
| 82 | } else if (!strcmp(val->name, "inhibitPolicyMapping")) { |
| 83 | if (!X509V3_get_value_int(val, &pcons->inhibitPolicyMapping)) { |
| 84 | goto err; |
| 85 | } |
| 86 | } else { |
| 87 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NAME); |
| 88 | X509V3_conf_err(val); |
| 89 | goto err; |
| 90 | } |
| 91 | } |
| 92 | if (!pcons->inhibitPolicyMapping && !pcons->requireExplicitPolicy) { |
| 93 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_ILLEGAL_EMPTY_EXTENSION); |
| 94 | goto err; |
| 95 | } |
| 96 |
| 97 | return pcons; |
| 98 | err: |
| 99 | POLICY_CONSTRAINTS_free(pcons); |
| 100 | return NULL; |
| 101 | } |
| 102 |