2016-05-18 02:52:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2016-05-18 02:52:22 +08:00
|
|
|
* Licensed under the OpenSSL license (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
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/asn1.h>
|
|
|
|
#include <openssl/x509.h>
|
2015-08-31 19:58:07 +08:00
|
|
|
#include "internal/x509_int.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/buffer.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
X509 *X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
X509 *ret = NULL;
|
|
|
|
X509_CINF *xi = NULL;
|
|
|
|
X509_NAME *xn;
|
2016-03-17 07:15:48 +08:00
|
|
|
EVP_PKEY *pubkey = NULL;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((ret = X509_new()) == NULL) {
|
|
|
|
X509err(X509_F_X509_REQ_TO_X509, ERR_R_MALLOC_FAILURE);
|
2016-03-17 07:15:48 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* duplicate the request */
|
2015-09-17 01:40:26 +08:00
|
|
|
xi = &ret->cert_info;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-09-17 01:46:16 +08:00
|
|
|
if (sk_X509_ATTRIBUTE_num(r->req_info.attributes) != 0) {
|
2015-03-14 12:16:42 +08:00
|
|
|
if ((xi->version = ASN1_INTEGER_new()) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (!ASN1_INTEGER_set(xi->version, 2))
|
|
|
|
goto err;
|
2015-01-17 08:06:54 +08:00
|
|
|
/*- xi->extensions=ri->attributes; <- bad, should not ever be done
|
|
|
|
ri->attributes=NULL; */
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
xn = X509_REQ_get_subject_name(r);
|
2016-03-17 07:15:48 +08:00
|
|
|
if (X509_set_subject_name(ret, xn) == 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-03-17 07:15:48 +08:00
|
|
|
if (X509_set_issuer_name(ret, xn) == 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-09-16 00:10:51 +08:00
|
|
|
if (X509_gmtime_adj(xi->validity.notBefore, 0) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-09-16 00:10:51 +08:00
|
|
|
if (X509_gmtime_adj(xi->validity.notAfter, (long)60 * 60 * 24 * days) ==
|
2015-01-22 11:40:55 +08:00
|
|
|
NULL)
|
|
|
|
goto err;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-04-04 05:37:32 +08:00
|
|
|
pubkey = X509_REQ_get0_pubkey(r);
|
|
|
|
if (pubkey == NULL || !X509_set_pubkey(ret, pubkey))
|
2016-03-17 07:15:48 +08:00
|
|
|
goto err;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!X509_sign(ret, pkey, EVP_md5()))
|
|
|
|
goto err;
|
2015-05-01 05:33:59 +08:00
|
|
|
return ret;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2015-05-01 05:33:59 +08:00
|
|
|
X509_free(ret);
|
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|