1/* crypto/x509/x509_set.c */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved.
| 1/* 2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
4 *
| 3 *
|
5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.]
| 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html
|
57 */ 58 59#include <stdio.h>
| 8 */ 9 10#include <stdio.h>
|
60#include "cryptlib.h"
| 11#include "internal/cryptlib.h"
|
61#include <openssl/asn1.h> 62#include <openssl/objects.h> 63#include <openssl/evp.h> 64#include <openssl/x509.h>
| 12#include <openssl/asn1.h> 13#include <openssl/objects.h> 14#include <openssl/evp.h> 15#include <openssl/x509.h>
|
| 16#include "internal/x509_int.h"
|
65 66int X509_set_version(X509 *x, long version) 67{ 68 if (x == NULL) 69 return (0); 70 if (version == 0) {
| 17 18int X509_set_version(X509 *x, long version) 19{ 20 if (x == NULL) 21 return (0); 22 if (version == 0) {
|
71 M_ASN1_INTEGER_free(x->cert_info->version); 72 x->cert_info->version = NULL;
| 23 ASN1_INTEGER_free(x->cert_info.version); 24 x->cert_info.version = NULL;
|
73 return (1); 74 }
| 25 return (1); 26 }
|
75 if (x->cert_info->version == NULL) { 76 if ((x->cert_info->version = M_ASN1_INTEGER_new()) == NULL)
| 27 if (x->cert_info.version == NULL) { 28 if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL)
|
77 return (0); 78 }
| 29 return (0); 30 }
|
79 return (ASN1_INTEGER_set(x->cert_info->version, version));
| 31 return (ASN1_INTEGER_set(x->cert_info.version, version));
|
80} 81 82int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial) 83{ 84 ASN1_INTEGER *in; 85 86 if (x == NULL)
| 32} 33 34int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial) 35{ 36 ASN1_INTEGER *in; 37 38 if (x == NULL)
|
87 return (0); 88 in = x->cert_info->serialNumber; 89 if (in != serial) { 90 in = M_ASN1_INTEGER_dup(serial); 91 if (in != NULL) { 92 M_ASN1_INTEGER_free(x->cert_info->serialNumber); 93 x->cert_info->serialNumber = in; 94 } 95 } 96 return (in != NULL);
| 39 return 0; 40 in = &x->cert_info.serialNumber; 41 if (in != serial) 42 return ASN1_STRING_copy(in, serial); 43 return 1;
|
97} 98 99int X509_set_issuer_name(X509 *x, X509_NAME *name) 100{
| 44} 45 46int X509_set_issuer_name(X509 *x, X509_NAME *name) 47{
|
101 if ((x == NULL) || (x->cert_info == NULL))
| 48 if (x == NULL)
|
102 return (0);
| 49 return (0);
|
103 return (X509_NAME_set(&x->cert_info->issuer, name));
| 50 return (X509_NAME_set(&x->cert_info.issuer, name));
|
104} 105 106int X509_set_subject_name(X509 *x, X509_NAME *name) 107{
| 51} 52 53int X509_set_subject_name(X509 *x, X509_NAME *name) 54{
|
108 if ((x == NULL) || (x->cert_info == NULL))
| 55 if (x == NULL)
|
109 return (0);
| 56 return (0);
|
110 return (X509_NAME_set(&x->cert_info->subject, name));
| 57 return (X509_NAME_set(&x->cert_info.subject, name));
|
111} 112
| 58} 59
|
113int X509_set_notBefore(X509 *x, const ASN1_TIME *tm)
| 60int x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm)
|
114{ 115 ASN1_TIME *in;
| 61{ 62 ASN1_TIME *in;
|
116 117 if ((x == NULL) || (x->cert_info->validity == NULL)) 118 return (0); 119 in = x->cert_info->validity->notBefore;
| 63 in = *ptm;
|
120 if (in != tm) {
| 64 if (in != tm) {
|
121 in = M_ASN1_TIME_dup(tm);
| 65 in = ASN1_STRING_dup(tm);
|
122 if (in != NULL) {
| 66 if (in != NULL) {
|
123 M_ASN1_TIME_free(x->cert_info->validity->notBefore); 124 x->cert_info->validity->notBefore = in;
| 67 ASN1_TIME_free(*ptm); 68 *ptm = in;
|
125 } 126 } 127 return (in != NULL); 128} 129
| 69 } 70 } 71 return (in != NULL); 72} 73
|
130int X509_set_notAfter(X509 *x, const ASN1_TIME *tm)
| 74int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm)
|
131{
| 75{
|
132 ASN1_TIME *in;
| 76 if (x == NULL) 77 return 0; 78 return x509_set1_time(&x->cert_info.validity.notBefore, tm); 79}
|
133
| 80
|
134 if ((x == NULL) || (x->cert_info->validity == NULL)) 135 return (0); 136 in = x->cert_info->validity->notAfter; 137 if (in != tm) { 138 in = M_ASN1_TIME_dup(tm); 139 if (in != NULL) { 140 M_ASN1_TIME_free(x->cert_info->validity->notAfter); 141 x->cert_info->validity->notAfter = in; 142 } 143 } 144 return (in != NULL);
| 81int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm) 82{ 83 if (x == NULL) 84 return 0; 85 return x509_set1_time(&x->cert_info.validity.notAfter, tm);
|
145} 146 147int X509_set_pubkey(X509 *x, EVP_PKEY *pkey) 148{
| 86} 87 88int X509_set_pubkey(X509 *x, EVP_PKEY *pkey) 89{
|
149 if ((x == NULL) || (x->cert_info == NULL))
| 90 if (x == NULL)
|
150 return (0);
| 91 return (0);
|
151 return (X509_PUBKEY_set(&(x->cert_info->key), pkey));
| 92 return (X509_PUBKEY_set(&(x->cert_info.key), pkey));
|
152}
| 93}
|
| 94 95int X509_up_ref(X509 *x) 96{ 97 int i; 98 99 if (CRYPTO_atomic_add(&x->references, 1, &i, x->lock) <= 0) 100 return 0; 101 102 REF_PRINT_COUNT("X509", x); 103 REF_ASSERT_ISNT(i < 2); 104 return ((i > 1) ? 1 : 0); 105} 106 107long X509_get_version(const X509 *x) 108{ 109 return ASN1_INTEGER_get(x->cert_info.version); 110} 111 112const ASN1_TIME *X509_get0_notBefore(const X509 *x) 113{ 114 return x->cert_info.validity.notBefore; 115} 116 117const ASN1_TIME *X509_get0_notAfter(const X509 *x) 118{ 119 return x->cert_info.validity.notAfter; 120} 121 122ASN1_TIME *X509_getm_notBefore(const X509 *x) 123{ 124 return x->cert_info.validity.notBefore; 125} 126 127ASN1_TIME *X509_getm_notAfter(const X509 *x) 128{ 129 return x->cert_info.validity.notAfter; 130} 131 132int X509_get_signature_type(const X509 *x) 133{ 134 return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm)); 135} 136 137X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x) 138{ 139 return x->cert_info.key; 140} 141 142const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x) 143{ 144 return x->cert_info.extensions; 145} 146 147void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid, 148 const ASN1_BIT_STRING **psuid) 149{ 150 if (piuid != NULL) 151 *piuid = x->cert_info.issuerUID; 152 if (psuid != NULL) 153 *psuid = x->cert_info.subjectUID; 154} 155 156const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x) 157{ 158 return &x->cert_info.signature; 159}
|
| |