1160814Ssimon/* pcy_data.c */
2296465Sdelphij/*
3296465Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4296465Sdelphij * 2004.
5160814Ssimon */
6160814Ssimon/* ====================================================================
7160814Ssimon * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
8160814Ssimon *
9160814Ssimon * Redistribution and use in source and binary forms, with or without
10160814Ssimon * modification, are permitted provided that the following conditions
11160814Ssimon * are met:
12160814Ssimon *
13160814Ssimon * 1. Redistributions of source code must retain the above copyright
14296465Sdelphij *    notice, this list of conditions and the following disclaimer.
15160814Ssimon *
16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17160814Ssimon *    notice, this list of conditions and the following disclaimer in
18160814Ssimon *    the documentation and/or other materials provided with the
19160814Ssimon *    distribution.
20160814Ssimon *
21160814Ssimon * 3. All advertising materials mentioning features or use of this
22160814Ssimon *    software must display the following acknowledgment:
23160814Ssimon *    "This product includes software developed by the OpenSSL Project
24160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25160814Ssimon *
26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27160814Ssimon *    endorse or promote products derived from this software without
28160814Ssimon *    prior written permission. For written permission, please contact
29160814Ssimon *    licensing@OpenSSL.org.
30160814Ssimon *
31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
33160814Ssimon *    permission of the OpenSSL Project.
34160814Ssimon *
35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
36160814Ssimon *    acknowledgment:
37160814Ssimon *    "This product includes software developed by the OpenSSL Project
38160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39160814Ssimon *
40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52160814Ssimon * ====================================================================
53160814Ssimon *
54160814Ssimon * This product includes cryptographic software written by Eric Young
55160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
56160814Ssimon * Hudson (tjh@cryptsoft.com).
57160814Ssimon *
58160814Ssimon */
59160814Ssimon
60160814Ssimon#include "cryptlib.h"
61160814Ssimon#include <openssl/x509.h>
62160814Ssimon#include <openssl/x509v3.h>
63160814Ssimon
64160814Ssimon#include "pcy_int.h"
65160814Ssimon
66160814Ssimon/* Policy Node routines */
67160814Ssimon
68160814Ssimonvoid policy_data_free(X509_POLICY_DATA *data)
69296465Sdelphij{
70296465Sdelphij    ASN1_OBJECT_free(data->valid_policy);
71296465Sdelphij    /* Don't free qualifiers if shared */
72296465Sdelphij    if (!(data->flags & POLICY_DATA_FLAG_SHARED_QUALIFIERS))
73296465Sdelphij        sk_POLICYQUALINFO_pop_free(data->qualifier_set, POLICYQUALINFO_free);
74296465Sdelphij    sk_ASN1_OBJECT_pop_free(data->expected_policy_set, ASN1_OBJECT_free);
75296465Sdelphij    OPENSSL_free(data);
76296465Sdelphij}
77160814Ssimon
78296465Sdelphij/*
79296465Sdelphij * Create a data based on an existing policy. If 'id' is NULL use the oid in
80296465Sdelphij * the policy, otherwise use 'id'. This behaviour covers the two types of
81296465Sdelphij * data in RFC3280: data with from a CertificatePolcies extension and
82296465Sdelphij * additional data with just the qualifiers of anyPolicy and ID from another
83296465Sdelphij * source.
84160814Ssimon */
85160814Ssimon
86296465SdelphijX509_POLICY_DATA *policy_data_new(POLICYINFO *policy, ASN1_OBJECT *id,
87296465Sdelphij                                  int crit)
88296465Sdelphij{
89296465Sdelphij    X509_POLICY_DATA *ret;
90296465Sdelphij    if (!policy && !id)
91296465Sdelphij        return NULL;
92296465Sdelphij    if (id) {
93296465Sdelphij        id = OBJ_dup(id);
94296465Sdelphij        if (!id)
95296465Sdelphij            return NULL;
96296465Sdelphij    }
97296465Sdelphij    ret = OPENSSL_malloc(sizeof(X509_POLICY_DATA));
98296465Sdelphij    if (!ret)
99296465Sdelphij        return NULL;
100296465Sdelphij    ret->expected_policy_set = sk_ASN1_OBJECT_new_null();
101296465Sdelphij    if (!ret->expected_policy_set) {
102296465Sdelphij        OPENSSL_free(ret);
103296465Sdelphij        if (id)
104296465Sdelphij            ASN1_OBJECT_free(id);
105296465Sdelphij        return NULL;
106296465Sdelphij    }
107160814Ssimon
108296465Sdelphij    if (crit)
109296465Sdelphij        ret->flags = POLICY_DATA_FLAG_CRITICAL;
110296465Sdelphij    else
111296465Sdelphij        ret->flags = 0;
112160814Ssimon
113296465Sdelphij    if (id)
114296465Sdelphij        ret->valid_policy = id;
115296465Sdelphij    else {
116296465Sdelphij        ret->valid_policy = policy->policyid;
117296465Sdelphij        policy->policyid = NULL;
118296465Sdelphij    }
119160814Ssimon
120296465Sdelphij    if (policy) {
121296465Sdelphij        ret->qualifier_set = policy->qualifiers;
122296465Sdelphij        policy->qualifiers = NULL;
123296465Sdelphij    } else
124296465Sdelphij        ret->qualifier_set = NULL;
125160814Ssimon
126296465Sdelphij    return ret;
127296465Sdelphij}
128