Rich Salz | b132225 | 2016-05-17 14:52:22 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3 | * |
Rich Salz | b132225 | 2016-05-17 14:52:22 -0400 | [diff] [blame] | 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 |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
Richard Levitte | b39fc56 | 2015-05-14 16:56:48 +0200 | [diff] [blame] | 11 | #include "internal/cryptlib.h" |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 12 | #include <openssl/bn.h> |
| 13 | #include <openssl/evp.h> |
| 14 | #include <openssl/asn1.h> |
| 15 | #include <openssl/x509.h> |
Dr. Stephen Henson | 124055a | 2015-08-31 12:58:07 +0100 | [diff] [blame] | 16 | #include "internal/x509_int.h" |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 17 | #include <openssl/objects.h> |
| 18 | #include <openssl/buffer.h> |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 19 | |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 20 | X509 *X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 21 | { |
| 22 | X509 *ret = NULL; |
| 23 | X509_CINF *xi = NULL; |
| 24 | X509_NAME *xn; |
FdaSilvaYY | 0517538 | 2016-03-17 00:15:48 +0100 | [diff] [blame] | 25 | EVP_PKEY *pubkey = NULL; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 26 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 27 | if ((ret = X509_new()) == NULL) { |
| 28 | X509err(X509_F_X509_REQ_TO_X509, ERR_R_MALLOC_FAILURE); |
FdaSilvaYY | 0517538 | 2016-03-17 00:15:48 +0100 | [diff] [blame] | 29 | return NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 30 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 31 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 32 | /* duplicate the request */ |
Dr. Stephen Henson | 5cf6abd | 2015-09-16 18:40:26 +0100 | [diff] [blame] | 33 | xi = &ret->cert_info; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 34 | |
Dr. Stephen Henson | 95ed0e7 | 2015-09-16 18:46:16 +0100 | [diff] [blame] | 35 | if (sk_X509_ATTRIBUTE_num(r->req_info.attributes) != 0) { |
Dr. Stephen Henson | f422a51 | 2015-03-14 04:16:42 +0000 | [diff] [blame] | 36 | if ((xi->version = ASN1_INTEGER_new()) == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 37 | goto err; |
| 38 | if (!ASN1_INTEGER_set(xi->version, 2)) |
| 39 | goto err; |
Matt Caswell | 35a1cc9 | 2015-01-17 00:06:54 +0000 | [diff] [blame] | 40 | /*- xi->extensions=ri->attributes; <- bad, should not ever be done |
| 41 | ri->attributes=NULL; */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 42 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 43 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 44 | xn = X509_REQ_get_subject_name(r); |
FdaSilvaYY | 0517538 | 2016-03-17 00:15:48 +0100 | [diff] [blame] | 45 | if (X509_set_subject_name(ret, xn) == 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 46 | goto err; |
FdaSilvaYY | 0517538 | 2016-03-17 00:15:48 +0100 | [diff] [blame] | 47 | if (X509_set_issuer_name(ret, xn) == 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 48 | goto err; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 49 | |
Dr. Stephen Henson | 2869e79 | 2015-09-15 17:10:51 +0100 | [diff] [blame] | 50 | if (X509_gmtime_adj(xi->validity.notBefore, 0) == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 51 | goto err; |
Dr. Stephen Henson | 2869e79 | 2015-09-15 17:10:51 +0100 | [diff] [blame] | 52 | if (X509_gmtime_adj(xi->validity.notAfter, (long)60 * 60 * 24 * days) == |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 53 | NULL) |
| 54 | goto err; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 55 | |
FdaSilvaYY | c513747 | 2016-04-03 23:37:32 +0200 | [diff] [blame] | 56 | pubkey = X509_REQ_get0_pubkey(r); |
| 57 | if (pubkey == NULL || !X509_set_pubkey(ret, pubkey)) |
FdaSilvaYY | 0517538 | 2016-03-17 00:15:48 +0100 | [diff] [blame] | 58 | goto err; |
| 59 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 60 | if (!X509_sign(ret, pkey, EVP_md5())) |
| 61 | goto err; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 62 | return ret; |
| 63 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 64 | err: |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 65 | X509_free(ret); |
| 66 | return NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 67 | } |