x509_r2x.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. X509 *X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey)
  19. {
  20. X509 *ret = NULL;
  21. X509_CINF *xi = NULL;
  22. const X509_NAME *xn;
  23. EVP_PKEY *pubkey = NULL;
  24. if ((ret = X509_new()) == NULL) {
  25. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  26. return NULL;
  27. }
  28. /* duplicate the request */
  29. xi = &ret->cert_info;
  30. if (sk_X509_ATTRIBUTE_num(r->req_info.attributes) != 0) {
  31. if ((xi->version = ASN1_INTEGER_new()) == NULL)
  32. goto err;
  33. if (!ASN1_INTEGER_set(xi->version, 2))
  34. goto err;
  35. /*- xi->extensions=ri->attributes; <- bad, should not ever be done
  36. ri->attributes=NULL; */
  37. }
  38. xn = X509_REQ_get_subject_name(r);
  39. if (X509_set_subject_name(ret, xn) == 0)
  40. goto err;
  41. if (X509_set_issuer_name(ret, xn) == 0)
  42. goto err;
  43. if (X509_gmtime_adj(xi->validity.notBefore, 0) == NULL)
  44. goto err;
  45. if (X509_gmtime_adj(xi->validity.notAfter, (long)60 * 60 * 24 * days) ==
  46. NULL)
  47. goto err;
  48. pubkey = X509_REQ_get0_pubkey(r);
  49. if (pubkey == NULL || !X509_set_pubkey(ret, pubkey))
  50. goto err;
  51. if (!X509_sign(ret, pkey, EVP_md5()))
  52. goto err;
  53. return ret;
  54. err:
  55. X509_free(ret);
  56. return NULL;
  57. }