x509_r2x.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/bn.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/asn1.h>
  14. #include <openssl/x509.h>
  15. #include "crypto/x509.h"
  16. #include <openssl/objects.h>
  17. #include <openssl/buffer.h>
  18. DEFINE_STACK_OF(X509_ATTRIBUTE)
  19. X509 *X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey)
  20. {
  21. X509 *ret = NULL;
  22. X509_CINF *xi = NULL;
  23. const X509_NAME *xn;
  24. EVP_PKEY *pubkey = NULL;
  25. if ((ret = X509_new()) == NULL) {
  26. X509err(X509_F_X509_REQ_TO_X509, ERR_R_MALLOC_FAILURE);
  27. return NULL;
  28. }
  29. /* duplicate the request */
  30. xi = &ret->cert_info;
  31. if (sk_X509_ATTRIBUTE_num(r->req_info.attributes) != 0) {
  32. if ((xi->version = ASN1_INTEGER_new()) == NULL)
  33. goto err;
  34. if (!ASN1_INTEGER_set(xi->version, 2))
  35. goto err;
  36. /*- xi->extensions=ri->attributes; <- bad, should not ever be done
  37. ri->attributes=NULL; */
  38. }
  39. xn = X509_REQ_get_subject_name(r);
  40. if (X509_set_subject_name(ret, xn) == 0)
  41. goto err;
  42. if (X509_set_issuer_name(ret, xn) == 0)
  43. goto err;
  44. if (X509_gmtime_adj(xi->validity.notBefore, 0) == NULL)
  45. goto err;
  46. if (X509_gmtime_adj(xi->validity.notAfter, (long)60 * 60 * 24 * days) ==
  47. NULL)
  48. goto err;
  49. pubkey = X509_REQ_get0_pubkey(r);
  50. if (pubkey == NULL || !X509_set_pubkey(ret, pubkey))
  51. goto err;
  52. if (!X509_sign(ret, pkey, EVP_md5()))
  53. goto err;
  54. return ret;
  55. err:
  56. X509_free(ret);
  57. return NULL;
  58. }