2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include <openssl/evp.h>
14 #include <openssl/asn1.h>
15 #include <openssl/x509.h>
16 #include "internal/x509_int.h"
17 #include <openssl/objects.h>
18 #include <openssl/buffer.h>
20 X509 *X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey)
25 EVP_PKEY *pubkey = NULL;
27 if ((ret = X509_new()) == NULL) {
28 X509err(X509_F_X509_REQ_TO_X509, ERR_R_MALLOC_FAILURE);
32 /* duplicate the request */
35 if (sk_X509_ATTRIBUTE_num(r->req_info.attributes) != 0) {
36 if ((xi->version = ASN1_INTEGER_new()) == NULL)
38 if (!ASN1_INTEGER_set(xi->version, 2))
40 /*- xi->extensions=ri->attributes; <- bad, should not ever be done
41 ri->attributes=NULL; */
44 xn = X509_REQ_get_subject_name(r);
45 if (X509_set_subject_name(ret, xn) == 0)
47 if (X509_set_issuer_name(ret, xn) == 0)
50 if (X509_gmtime_adj(xi->validity.notBefore, 0) == NULL)
52 if (X509_gmtime_adj(xi->validity.notAfter, (long)60 * 60 * 24 * days) ==
56 pubkey = X509_REQ_get0_pubkey(r);
57 if (pubkey == NULL || !X509_set_pubkey(ret, pubkey))
60 if (!X509_sign(ret, pkey, EVP_md5()))