summaryrefslogtreecommitdiffstats
path: root/crypto/x509/x509aset.c
blob: 3d7e5fbb21dc8f5e2247a34617abefa8d9bc7efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/err.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include "x509_acert.h"

static int replace_gentime(ASN1_STRING **dest, const ASN1_GENERALIZEDTIME *src)
{
    ASN1_STRING *s;

    if (src->type != V_ASN1_GENERALIZEDTIME)
        return 0;

    if (*dest == src)
        return 1;

    s = ASN1_STRING_dup(src);
    if (s == NULL) {
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
        return 0;
    }

    ASN1_STRING_free(*dest);
    *dest = s;

    return 1;
}

static int replace_dirName(GENERAL_NAMES **names, const X509_NAME *dirName)
{
    GENERAL_NAME *gen_name = NULL;
    STACK_OF(GENERAL_NAME) *new_names = NULL;
    X509_NAME *name_copy;

    if ((name_copy = X509_NAME_dup(dirName)) == NULL) {
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
        goto err;
    }

    if ((new_names = sk_GENERAL_NAME_new_null()) == NULL) {
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
        goto err;
    }

    if ((gen_name = GENERAL_NAME_new()) == NULL) {
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
        goto err;
    }

    if (sk_GENERAL_NAME_push(new_names, gen_name) <= 0) {
        ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
        goto err;
    }

    GENERAL_NAME_set0_value(gen_name, GEN_DIRNAME, name_copy);

    GENERAL_NAMES_free(*names);
    *names = new_names;

    return 1;

err:
    GENERAL_NAME_free(gen_name);
    sk_GENERAL_NAME_free(new_names);
    X509_NAME_free(name_copy);
    return 0;
}

int OSSL_OBJECT_DIGEST_INFO_set1_digest(OSSL_OBJECT_DIGEST_INFO *o,
                                        int digestedObjectType,
                                        X509_ALGOR *digestAlgorithm,
                                        ASN1_BIT_STRING *digest)
{

    if (ASN1_ENUMERATED_set(&o->digestedObjectType, digestedObjectType) <= 0)
        return 0;

    if (X509_ALGOR_copy(&o->digestAlgorithm, digestAlgorithm) <= 0)
        return 0;

    if (ASN1_STRING_copy(&o->objectDigest, digest) <= 0)
        return 0;

    return 1;
}

int OSSL_ISSUER_SERIAL_set1_issuer(OSSL_ISSUER_SERIAL *isss,
                                   const X509_NAME *issuer)
{
    return replace_dirName(&isss->issuer, issuer);
}

int OSSL_ISSUER_SERIAL_set1_serial(OSSL_ISSUER_SERIAL *isss,
                                   const ASN1_INTEGER *serial)
{
    return ASN1_STRING_copy(&isss->serial, serial);
}

int OSSL_ISSUER_SERIAL_set1_issuerUID(OSSL_ISSUER_SERIAL *isss,
                                      const ASN1_BIT_STRING *uid)
{
    ASN1_BIT_STRING_free(isss->issuerUID);
    isss->issuerUID = ASN1_STRING_dup(uid);
    if (isss->issuerUID == NULL) {
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
        return 0;
    }
    return 1;
}

int X509_ACERT_set_version(X509_ACERT *x, long version)
{
    return ASN1_INTEGER_set(&x->acinfo->version, version);
}

void X509_ACERT_set0_holder_entityName(X509_ACERT *x, GENERAL_NAMES *names)
{
    GENERAL_NAMES_free(x->acinfo->holder.entityName);
    x->acinfo->holder.entityName = names;
}

void X509_ACERT_set0_holder_baseCertId(X509_ACERT *x,
                                       OSSL_ISSUER_SERIAL *isss)
{
    OSSL_ISSUER_SERIAL_free(x->acinfo->holder.baseCertificateID);
    x->acinfo->holder.baseCertificateID = isss;
}

void X509_ACERT_set0_holder_digest(X509_ACERT *x,
                                   OSSL_OBJECT_DIGEST_INFO *dinfo)
{
    OSSL_OBJECT_DIGEST_INFO_free(x->acinfo->holder.objectDigestInfo);
    x->acinfo->holder.objectDigestInfo = dinfo;
}

int X509_ACERT_set1_issuerName(X509_ACERT *x, const X509_NAME *name)
{
    X509_ACERT_ISSUER_V2FORM *v2Form;

    v2Form = x->acinfo->issuer.u.v2Form;

    /* only v2Form is supported, so always create that version */
    if (v2Form == NULL) {
        v2Form = X509_ACERT_ISSUER_V2FORM_new();
        if (v2Form == NULL) {
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
            return 0;
        }
        x->acinfo->issuer.u.v2Form = v2Form;
        x->acinfo->issuer.type = X509_ACERT_ISSUER_V2;
    }

    return replace_dirName(&v2Form->issuerName, name);
}

int X509_ACERT_set1_serialNumber(X509_ACERT *x, const ASN1_INTEGER *serial)
{
    return ASN1_STRING_copy(&x->acinfo->serialNumber, serial);
}

int X509_ACERT_set1_notBefore(X509_ACERT *x, const ASN1_GENERALIZEDTIME *time)
{
    return replace_gentime(&x->acinfo->validityPeriod.notBefore, time);
}

int X509_ACERT_set1_notAfter(X509_ACERT *x, const ASN1_GENERALIZEDTIME *time)
{
    return replace_gentime(&x->acinfo->validityPeriod.notAfter, time);
}