/src/openssl/crypto/x509/x509_r2x.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
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 "crypto/x509.h" |
17 | | #include <openssl/objects.h> |
18 | | #include <openssl/buffer.h> |
19 | | |
20 | | X509 *X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey) |
21 | 0 | { |
22 | 0 | X509 *ret = NULL; |
23 | 0 | X509_CINF *xi = NULL; |
24 | 0 | const X509_NAME *xn; |
25 | 0 | EVP_PKEY *pubkey = NULL; |
26 | |
|
27 | 0 | if ((ret = X509_new()) == NULL) { |
28 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
29 | 0 | return NULL; |
30 | 0 | } |
31 | | |
32 | | /* duplicate the request */ |
33 | 0 | xi = &ret->cert_info; |
34 | |
|
35 | 0 | if (sk_X509_ATTRIBUTE_num(r->req_info.attributes) != 0) { |
36 | 0 | if ((xi->version = ASN1_INTEGER_new()) == NULL) |
37 | 0 | goto err; |
38 | 0 | if (!ASN1_INTEGER_set(xi->version, 2)) |
39 | 0 | goto err; |
40 | | /*- xi->extensions=ri->attributes; <- bad, should not ever be done |
41 | | ri->attributes=NULL; */ |
42 | 0 | } |
43 | | |
44 | 0 | xn = X509_REQ_get_subject_name(r); |
45 | 0 | if (X509_set_subject_name(ret, xn) == 0) |
46 | 0 | goto err; |
47 | 0 | if (X509_set_issuer_name(ret, xn) == 0) |
48 | 0 | goto err; |
49 | | |
50 | 0 | if (X509_gmtime_adj(xi->validity.notBefore, 0) == NULL) |
51 | 0 | goto err; |
52 | 0 | if (X509_gmtime_adj(xi->validity.notAfter, (long)60 * 60 * 24 * days) == |
53 | 0 | NULL) |
54 | 0 | goto err; |
55 | | |
56 | 0 | pubkey = X509_REQ_get0_pubkey(r); |
57 | 0 | if (pubkey == NULL || !X509_set_pubkey(ret, pubkey)) |
58 | 0 | goto err; |
59 | | |
60 | 0 | if (!X509_sign(ret, pkey, EVP_md5())) |
61 | 0 | goto err; |
62 | 0 | return ret; |
63 | | |
64 | 0 | err: |
65 | 0 | X509_free(ret); |
66 | 0 | return NULL; |
67 | 0 | } |