x509_set.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/x509.h>
  15. #include "internal/x509_int.h"
  16. int X509_set_version(X509 *x, long version)
  17. {
  18. if (x == NULL)
  19. return (0);
  20. if (version == 0) {
  21. ASN1_INTEGER_free(x->cert_info.version);
  22. x->cert_info.version = NULL;
  23. return (1);
  24. }
  25. if (x->cert_info.version == NULL) {
  26. if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL)
  27. return (0);
  28. }
  29. return (ASN1_INTEGER_set(x->cert_info.version, version));
  30. }
  31. int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
  32. {
  33. ASN1_INTEGER *in;
  34. if (x == NULL)
  35. return 0;
  36. in = &x->cert_info.serialNumber;
  37. if (in != serial)
  38. return ASN1_STRING_copy(in, serial);
  39. return 1;
  40. }
  41. int X509_set_issuer_name(X509 *x, X509_NAME *name)
  42. {
  43. if (x == NULL)
  44. return (0);
  45. return (X509_NAME_set(&x->cert_info.issuer, name));
  46. }
  47. int X509_set_subject_name(X509 *x, X509_NAME *name)
  48. {
  49. if (x == NULL)
  50. return (0);
  51. return (X509_NAME_set(&x->cert_info.subject, name));
  52. }
  53. int x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm)
  54. {
  55. ASN1_TIME *in;
  56. in = *ptm;
  57. if (in != tm) {
  58. in = ASN1_STRING_dup(tm);
  59. if (in != NULL) {
  60. ASN1_TIME_free(*ptm);
  61. *ptm = in;
  62. }
  63. }
  64. return (in != NULL);
  65. }
  66. int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm)
  67. {
  68. if (x == NULL)
  69. return 0;
  70. return x509_set1_time(&x->cert_info.validity.notBefore, tm);
  71. }
  72. int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm)
  73. {
  74. if (x == NULL)
  75. return 0;
  76. return x509_set1_time(&x->cert_info.validity.notAfter, tm);
  77. }
  78. int X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
  79. {
  80. if (x == NULL)
  81. return (0);
  82. return (X509_PUBKEY_set(&(x->cert_info.key), pkey));
  83. }
  84. int X509_up_ref(X509 *x)
  85. {
  86. int i;
  87. if (CRYPTO_UP_REF(&x->references, &i, x->lock) <= 0)
  88. return 0;
  89. REF_PRINT_COUNT("X509", x);
  90. REF_ASSERT_ISNT(i < 2);
  91. return ((i > 1) ? 1 : 0);
  92. }
  93. long X509_get_version(const X509 *x)
  94. {
  95. return ASN1_INTEGER_get(x->cert_info.version);
  96. }
  97. const ASN1_TIME *X509_get0_notBefore(const X509 *x)
  98. {
  99. return x->cert_info.validity.notBefore;
  100. }
  101. const ASN1_TIME *X509_get0_notAfter(const X509 *x)
  102. {
  103. return x->cert_info.validity.notAfter;
  104. }
  105. ASN1_TIME *X509_getm_notBefore(const X509 *x)
  106. {
  107. return x->cert_info.validity.notBefore;
  108. }
  109. ASN1_TIME *X509_getm_notAfter(const X509 *x)
  110. {
  111. return x->cert_info.validity.notAfter;
  112. }
  113. int X509_get_signature_type(const X509 *x)
  114. {
  115. return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm));
  116. }
  117. X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x)
  118. {
  119. return x->cert_info.key;
  120. }
  121. const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
  122. {
  123. return x->cert_info.extensions;
  124. }
  125. void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid,
  126. const ASN1_BIT_STRING **psuid)
  127. {
  128. if (piuid != NULL)
  129. *piuid = x->cert_info.issuerUID;
  130. if (psuid != NULL)
  131. *psuid = x->cert_info.subjectUID;
  132. }
  133. const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
  134. {
  135. return &x->cert_info.signature;
  136. }