]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
3e4b43b9 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 BM |
12 | #include <openssl/bn.h> |
13 | #include <openssl/evp.h> | |
14 | #include <openssl/asn1.h> | |
15 | #include <openssl/x509.h> | |
25f2138b | 16 | #include "crypto/x509.h" |
ec577822 BM |
17 | #include <openssl/objects.h> |
18 | #include <openssl/buffer.h> | |
d02b48c6 | 19 | |
852c2ed2 RS |
20 | DEFINE_STACK_OF(X509_ATTRIBUTE) |
21 | ||
6b691a5c | 22 | X509 *X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey) |
0f113f3e MC |
23 | { |
24 | X509 *ret = NULL; | |
25 | X509_CINF *xi = NULL; | |
8cc86b81 | 26 | const X509_NAME *xn; |
0517538d | 27 | EVP_PKEY *pubkey = NULL; |
d02b48c6 | 28 | |
0f113f3e MC |
29 | if ((ret = X509_new()) == NULL) { |
30 | X509err(X509_F_X509_REQ_TO_X509, ERR_R_MALLOC_FAILURE); | |
0517538d | 31 | return NULL; |
0f113f3e | 32 | } |
d02b48c6 | 33 | |
0f113f3e | 34 | /* duplicate the request */ |
5cf6abd8 | 35 | xi = &ret->cert_info; |
d02b48c6 | 36 | |
95ed0e7c | 37 | if (sk_X509_ATTRIBUTE_num(r->req_info.attributes) != 0) { |
f422a514 | 38 | if ((xi->version = ASN1_INTEGER_new()) == NULL) |
0f113f3e MC |
39 | goto err; |
40 | if (!ASN1_INTEGER_set(xi->version, 2)) | |
41 | goto err; | |
35a1cc90 MC |
42 | /*- xi->extensions=ri->attributes; <- bad, should not ever be done |
43 | ri->attributes=NULL; */ | |
0f113f3e | 44 | } |
d02b48c6 | 45 | |
0f113f3e | 46 | xn = X509_REQ_get_subject_name(r); |
0517538d | 47 | if (X509_set_subject_name(ret, xn) == 0) |
0f113f3e | 48 | goto err; |
0517538d | 49 | if (X509_set_issuer_name(ret, xn) == 0) |
0f113f3e | 50 | goto err; |
d02b48c6 | 51 | |
2869e79f | 52 | if (X509_gmtime_adj(xi->validity.notBefore, 0) == NULL) |
0f113f3e | 53 | goto err; |
2869e79f | 54 | if (X509_gmtime_adj(xi->validity.notAfter, (long)60 * 60 * 24 * days) == |
0f113f3e MC |
55 | NULL) |
56 | goto err; | |
d02b48c6 | 57 | |
c5137473 F |
58 | pubkey = X509_REQ_get0_pubkey(r); |
59 | if (pubkey == NULL || !X509_set_pubkey(ret, pubkey)) | |
0517538d F |
60 | goto err; |
61 | ||
0f113f3e MC |
62 | if (!X509_sign(ret, pkey, EVP_md5())) |
63 | goto err; | |
222561fe RS |
64 | return ret; |
65 | ||
0f113f3e | 66 | err: |
222561fe RS |
67 | X509_free(ret); |
68 | return NULL; | |
0f113f3e | 69 | } |