x509_set.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 1995-2017 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 "internal/refcount.h"
  12. #include <openssl/asn1.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/x509v3.h>
  17. #include "crypto/asn1.h"
  18. #include "crypto/x509.h"
  19. #include "x509_local.h"
  20. int X509_set_version(X509 *x, long version)
  21. {
  22. if (x == NULL)
  23. return 0;
  24. if (version == 0) {
  25. ASN1_INTEGER_free(x->cert_info.version);
  26. x->cert_info.version = NULL;
  27. return 1;
  28. }
  29. if (x->cert_info.version == NULL) {
  30. if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL)
  31. return 0;
  32. }
  33. return ASN1_INTEGER_set(x->cert_info.version, version);
  34. }
  35. int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
  36. {
  37. ASN1_INTEGER *in;
  38. if (x == NULL)
  39. return 0;
  40. in = &x->cert_info.serialNumber;
  41. if (in != serial)
  42. return ASN1_STRING_copy(in, serial);
  43. return 1;
  44. }
  45. int X509_set_issuer_name(X509 *x, X509_NAME *name)
  46. {
  47. if (x == NULL)
  48. return 0;
  49. return X509_NAME_set(&x->cert_info.issuer, name);
  50. }
  51. int X509_set_subject_name(X509 *x, X509_NAME *name)
  52. {
  53. if (x == NULL)
  54. return 0;
  55. return X509_NAME_set(&x->cert_info.subject, name);
  56. }
  57. int x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm)
  58. {
  59. ASN1_TIME *in;
  60. in = *ptm;
  61. if (in != tm) {
  62. in = ASN1_STRING_dup(tm);
  63. if (in != NULL) {
  64. ASN1_TIME_free(*ptm);
  65. *ptm = in;
  66. }
  67. }
  68. return (in != NULL);
  69. }
  70. int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm)
  71. {
  72. if (x == NULL)
  73. return 0;
  74. return x509_set1_time(&x->cert_info.validity.notBefore, tm);
  75. }
  76. int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm)
  77. {
  78. if (x == NULL)
  79. return 0;
  80. return x509_set1_time(&x->cert_info.validity.notAfter, tm);
  81. }
  82. int X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
  83. {
  84. if (x == NULL)
  85. return 0;
  86. return X509_PUBKEY_set(&(x->cert_info.key), pkey);
  87. }
  88. int X509_up_ref(X509 *x)
  89. {
  90. int i;
  91. if (CRYPTO_UP_REF(&x->references, &i, x->lock) <= 0)
  92. return 0;
  93. REF_PRINT_COUNT("X509", x);
  94. REF_ASSERT_ISNT(i < 2);
  95. return ((i > 1) ? 1 : 0);
  96. }
  97. long X509_get_version(const X509 *x)
  98. {
  99. return ASN1_INTEGER_get(x->cert_info.version);
  100. }
  101. const ASN1_TIME *X509_get0_notBefore(const X509 *x)
  102. {
  103. return x->cert_info.validity.notBefore;
  104. }
  105. const ASN1_TIME *X509_get0_notAfter(const X509 *x)
  106. {
  107. return x->cert_info.validity.notAfter;
  108. }
  109. ASN1_TIME *X509_getm_notBefore(const X509 *x)
  110. {
  111. return x->cert_info.validity.notBefore;
  112. }
  113. ASN1_TIME *X509_getm_notAfter(const X509 *x)
  114. {
  115. return x->cert_info.validity.notAfter;
  116. }
  117. int X509_get_signature_type(const X509 *x)
  118. {
  119. return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm));
  120. }
  121. X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x)
  122. {
  123. return x->cert_info.key;
  124. }
  125. const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
  126. {
  127. return x->cert_info.extensions;
  128. }
  129. void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid,
  130. const ASN1_BIT_STRING **psuid)
  131. {
  132. if (piuid != NULL)
  133. *piuid = x->cert_info.issuerUID;
  134. if (psuid != NULL)
  135. *psuid = x->cert_info.subjectUID;
  136. }
  137. const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
  138. {
  139. return &x->cert_info.signature;
  140. }
  141. int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid,
  142. int *secbits, uint32_t *flags)
  143. {
  144. if (mdnid != NULL)
  145. *mdnid = siginf->mdnid;
  146. if (pknid != NULL)
  147. *pknid = siginf->pknid;
  148. if (secbits != NULL)
  149. *secbits = siginf->secbits;
  150. if (flags != NULL)
  151. *flags = siginf->flags;
  152. return (siginf->flags & X509_SIG_INFO_VALID) != 0;
  153. }
  154. void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid,
  155. int secbits, uint32_t flags)
  156. {
  157. siginf->mdnid = mdnid;
  158. siginf->pknid = pknid;
  159. siginf->secbits = secbits;
  160. siginf->flags = flags;
  161. }
  162. int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits,
  163. uint32_t *flags)
  164. {
  165. X509_check_purpose(x, -1, -1);
  166. return X509_SIG_INFO_get(&x->siginf, mdnid, pknid, secbits, flags);
  167. }
  168. static void x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  169. const ASN1_STRING *sig)
  170. {
  171. int pknid, mdnid;
  172. const EVP_MD *md;
  173. siginf->mdnid = NID_undef;
  174. siginf->pknid = NID_undef;
  175. siginf->secbits = -1;
  176. siginf->flags = 0;
  177. if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)
  178. || pknid == NID_undef)
  179. return;
  180. siginf->pknid = pknid;
  181. if (mdnid == NID_undef) {
  182. /* If we have one, use a custom handler for this algorithm */
  183. const EVP_PKEY_ASN1_METHOD *ameth = EVP_PKEY_asn1_find(NULL, pknid);
  184. if (ameth == NULL || ameth->siginf_set == NULL
  185. || ameth->siginf_set(siginf, alg, sig) == 0)
  186. return;
  187. siginf->flags |= X509_SIG_INFO_VALID;
  188. return;
  189. }
  190. siginf->flags |= X509_SIG_INFO_VALID;
  191. siginf->mdnid = mdnid;
  192. md = EVP_get_digestbynid(mdnid);
  193. if (md == NULL)
  194. return;
  195. /* Security bits: half number of bits in digest */
  196. siginf->secbits = EVP_MD_size(md) * 4;
  197. switch (mdnid) {
  198. case NID_sha1:
  199. case NID_sha256:
  200. case NID_sha384:
  201. case NID_sha512:
  202. siginf->flags |= X509_SIG_INFO_TLS;
  203. }
  204. }
  205. void x509_init_sig_info(X509 *x)
  206. {
  207. x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature);
  208. }