x509_set.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright 1995-2021 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 "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, const 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, const X509_NAME *name)
  52. {
  53. if (x == NULL)
  54. return 0;
  55. return X509_NAME_set(&x->cert_info.subject, name);
  56. }
  57. int ossl_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 ossl_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 ossl_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. /* Modify *siginf according to alg and sig. Return 1 on success, else 0. */
  169. static int x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  170. const ASN1_STRING *sig)
  171. {
  172. int pknid, mdnid;
  173. const EVP_MD *md;
  174. const EVP_PKEY_ASN1_METHOD *ameth;
  175. siginf->mdnid = NID_undef;
  176. siginf->pknid = NID_undef;
  177. siginf->secbits = -1;
  178. siginf->flags = 0;
  179. if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)
  180. || pknid == NID_undef) {
  181. ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
  182. return 0;
  183. }
  184. siginf->mdnid = mdnid;
  185. siginf->pknid = pknid;
  186. switch (mdnid) {
  187. case NID_undef:
  188. /* If we have one, use a custom handler for this algorithm */
  189. ameth = EVP_PKEY_asn1_find(NULL, pknid);
  190. if (ameth == NULL || ameth->siginf_set == NULL
  191. || !ameth->siginf_set(siginf, alg, sig)) {
  192. ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET);
  193. return 0;
  194. }
  195. break;
  196. /*
  197. * SHA1 and MD5 are known to be broken. Reduce security bits so that
  198. * they're no longer accepted at security level 1.
  199. * The real values don't really matter as long as they're lower than 80,
  200. * which is our security level 1.
  201. */
  202. case NID_sha1:
  203. /*
  204. * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack
  205. * for SHA1 at2^63.4
  206. */
  207. siginf->secbits = 63;
  208. break;
  209. case NID_md5:
  210. /*
  211. * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
  212. * puts a chosen-prefix attack for MD5 at 2^39.
  213. */
  214. siginf->secbits = 39;
  215. break;
  216. case NID_id_GostR3411_94:
  217. /*
  218. * There is a collision attack on GOST R 34.11-94 at 2^105, see
  219. * https://link.springer.com/chapter/10.1007%2F978-3-540-85174-5_10
  220. */
  221. siginf->secbits = 105;
  222. break;
  223. default:
  224. /* Security bits: half number of bits in digest */
  225. if ((md = EVP_get_digestbynid(mdnid)) == NULL) {
  226. ERR_raise(ERR_LIB_X509, X509_R_ERROR_GETTING_MD_BY_NID);
  227. return 0;
  228. }
  229. siginf->secbits = EVP_MD_get_size(md) * 4;
  230. break;
  231. }
  232. switch (mdnid) {
  233. case NID_sha1:
  234. case NID_sha256:
  235. case NID_sha384:
  236. case NID_sha512:
  237. siginf->flags |= X509_SIG_INFO_TLS;
  238. }
  239. siginf->flags |= X509_SIG_INFO_VALID;
  240. return 1;
  241. }
  242. /* Returns 1 on success, 0 on failure */
  243. int ossl_x509_init_sig_info(X509 *x)
  244. {
  245. return x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature);
  246. }