1294491Sdelphij/* crypto/x509/x509_set.c */
2294491Sdelphij/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3294491Sdelphij * All rights reserved.
4294491Sdelphij *
5294491Sdelphij * This package is an SSL implementation written
6294491Sdelphij * by Eric Young (eay@cryptsoft.com).
7294491Sdelphij * The implementation was written so as to conform with Netscapes SSL.
8294491Sdelphij *
9294491Sdelphij * This library is free for commercial and non-commercial use as long as
10294491Sdelphij * the following conditions are aheared to.  The following conditions
11294491Sdelphij * apply to all code found in this distribution, be it the RC4, RSA,
12294491Sdelphij * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13294491Sdelphij * included with this distribution is covered by the same copyright terms
14294491Sdelphij * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15294491Sdelphij *
16294491Sdelphij * Copyright remains Eric Young's, and as such any Copyright notices in
17294491Sdelphij * the code are not to be removed.
18294491Sdelphij * If this package is used in a product, Eric Young should be given attribution
19294491Sdelphij * as the author of the parts of the library used.
20294491Sdelphij * This can be in the form of a textual message at program startup or
21294491Sdelphij * in documentation (online or textual) provided with the package.
22294491Sdelphij *
23294491Sdelphij * Redistribution and use in source and binary forms, with or without
24294491Sdelphij * modification, are permitted provided that the following conditions
25294491Sdelphij * are met:
26294491Sdelphij * 1. Redistributions of source code must retain the copyright
27294491Sdelphij *    notice, this list of conditions and the following disclaimer.
28294491Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
29294491Sdelphij *    notice, this list of conditions and the following disclaimer in the
30294491Sdelphij *    documentation and/or other materials provided with the distribution.
31294491Sdelphij * 3. All advertising materials mentioning features or use of this software
32294491Sdelphij *    must display the following acknowledgement:
33294491Sdelphij *    "This product includes cryptographic software written by
34294491Sdelphij *     Eric Young (eay@cryptsoft.com)"
35294491Sdelphij *    The word 'cryptographic' can be left out if the rouines from the library
36294491Sdelphij *    being used are not cryptographic related :-).
37294491Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
38294491Sdelphij *    the apps directory (application code) you must include an acknowledgement:
39294491Sdelphij *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40294491Sdelphij *
41294491Sdelphij * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42294491Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43294491Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44294491Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45294491Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46294491Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47294491Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48294491Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49294491Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50294491Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51294491Sdelphij * SUCH DAMAGE.
52294491Sdelphij *
53294491Sdelphij * The licence and distribution terms for any publically available version or
54294491Sdelphij * derivative of this code cannot be changed.  i.e. this code cannot simply be
55294491Sdelphij * copied and put under another distribution licence
56294491Sdelphij * [including the GNU Public Licence.]
57294491Sdelphij */
58294491Sdelphij
59294491Sdelphij#include <stdio.h>
60294491Sdelphij#include "cryptlib.h"
61294491Sdelphij#include <openssl/asn1.h>
62294491Sdelphij#include <openssl/objects.h>
63294491Sdelphij#include <openssl/evp.h>
64294491Sdelphij#include <openssl/x509.h>
65294491Sdelphij
66294491Sdelphijint X509_set_version(X509 *x, long version)
67294491Sdelphij{
68294491Sdelphij    if (x == NULL)
69294491Sdelphij        return (0);
70294491Sdelphij    if (x->cert_info->version == NULL) {
71294491Sdelphij        if ((x->cert_info->version = M_ASN1_INTEGER_new()) == NULL)
72294491Sdelphij            return (0);
73294491Sdelphij    }
74294491Sdelphij    return (ASN1_INTEGER_set(x->cert_info->version, version));
75294491Sdelphij}
76294491Sdelphij
77294491Sdelphijint X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
78294491Sdelphij{
79294491Sdelphij    ASN1_INTEGER *in;
80294491Sdelphij
81294491Sdelphij    if (x == NULL)
82294491Sdelphij        return (0);
83294491Sdelphij    in = x->cert_info->serialNumber;
84294491Sdelphij    if (in != serial) {
85294491Sdelphij        in = M_ASN1_INTEGER_dup(serial);
86294491Sdelphij        if (in != NULL) {
87294491Sdelphij            M_ASN1_INTEGER_free(x->cert_info->serialNumber);
88294491Sdelphij            x->cert_info->serialNumber = in;
89294491Sdelphij        }
90    }
91    return (in != NULL);
92}
93
94int X509_set_issuer_name(X509 *x, X509_NAME *name)
95{
96    if ((x == NULL) || (x->cert_info == NULL))
97        return (0);
98    return (X509_NAME_set(&x->cert_info->issuer, name));
99}
100
101int X509_set_subject_name(X509 *x, X509_NAME *name)
102{
103    if ((x == NULL) || (x->cert_info == NULL))
104        return (0);
105    return (X509_NAME_set(&x->cert_info->subject, name));
106}
107
108int X509_set_notBefore(X509 *x, const ASN1_TIME *tm)
109{
110    ASN1_TIME *in;
111
112    if ((x == NULL) || (x->cert_info->validity == NULL))
113        return (0);
114    in = x->cert_info->validity->notBefore;
115    if (in != tm) {
116        in = M_ASN1_TIME_dup(tm);
117        if (in != NULL) {
118            M_ASN1_TIME_free(x->cert_info->validity->notBefore);
119            x->cert_info->validity->notBefore = in;
120        }
121    }
122    return (in != NULL);
123}
124
125int X509_set_notAfter(X509 *x, const ASN1_TIME *tm)
126{
127    ASN1_TIME *in;
128
129    if ((x == NULL) || (x->cert_info->validity == NULL))
130        return (0);
131    in = x->cert_info->validity->notAfter;
132    if (in != tm) {
133        in = M_ASN1_TIME_dup(tm);
134        if (in != NULL) {
135            M_ASN1_TIME_free(x->cert_info->validity->notAfter);
136            x->cert_info->validity->notAfter = in;
137        }
138    }
139    return (in != NULL);
140}
141
142int X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
143{
144    if ((x == NULL) || (x->cert_info == NULL))
145        return (0);
146    return (X509_PUBKEY_set(&(x->cert_info->key), pkey));
147}
148