x509_set.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright 1995-2023 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 == X509_get_version(x))
  25. return 1; /* avoid needless modification even re-allocation */
  26. if (version == X509_VERSION_1) {
  27. ASN1_INTEGER_free(x->cert_info.version);
  28. x->cert_info.version = NULL;
  29. x->cert_info.enc.modified = 1;
  30. return 1;
  31. }
  32. if (x->cert_info.version == NULL) {
  33. if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL)
  34. return 0;
  35. }
  36. if (!ASN1_INTEGER_set(x->cert_info.version, version))
  37. return 0;
  38. x->cert_info.enc.modified = 1;
  39. return 1;
  40. }
  41. int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
  42. {
  43. ASN1_INTEGER *in;
  44. if (x == NULL)
  45. return 0;
  46. in = &x->cert_info.serialNumber;
  47. if (in != serial)
  48. return ASN1_STRING_copy(in, serial);
  49. x->cert_info.enc.modified = 1;
  50. return 1;
  51. }
  52. int X509_set_issuer_name(X509 *x, const X509_NAME *name)
  53. {
  54. if (x == NULL || !X509_NAME_set(&x->cert_info.issuer, name))
  55. return 0;
  56. x->cert_info.enc.modified = 1;
  57. return 1;
  58. }
  59. int X509_set_subject_name(X509 *x, const X509_NAME *name)
  60. {
  61. if (x == NULL || !X509_NAME_set(&x->cert_info.subject, name))
  62. return 0;
  63. x->cert_info.enc.modified = 1;
  64. return 1;
  65. }
  66. int ossl_x509_set1_time(int *modified, ASN1_TIME **ptm, const ASN1_TIME *tm)
  67. {
  68. ASN1_TIME *new;
  69. if (*ptm == tm)
  70. return 1;
  71. new = ASN1_STRING_dup(tm);
  72. if (tm != NULL && new == NULL)
  73. return 0;
  74. ASN1_TIME_free(*ptm);
  75. *ptm = new;
  76. if (modified != NULL)
  77. *modified = 1;
  78. return 1;
  79. }
  80. int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm)
  81. {
  82. if (x == NULL || tm == NULL)
  83. return 0;
  84. return ossl_x509_set1_time(&x->cert_info.enc.modified,
  85. &x->cert_info.validity.notBefore, tm);
  86. }
  87. int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm)
  88. {
  89. if (x == NULL || tm == NULL)
  90. return 0;
  91. return ossl_x509_set1_time(&x->cert_info.enc.modified,
  92. &x->cert_info.validity.notAfter, tm);
  93. }
  94. int X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
  95. {
  96. if (x == NULL)
  97. return 0;
  98. if (!X509_PUBKEY_set(&(x->cert_info.key), pkey))
  99. return 0;
  100. x->cert_info.enc.modified = 1;
  101. return 1;
  102. }
  103. int X509_up_ref(X509 *x)
  104. {
  105. int i;
  106. if (CRYPTO_UP_REF(&x->references, &i) <= 0)
  107. return 0;
  108. REF_PRINT_COUNT("X509", x);
  109. REF_ASSERT_ISNT(i < 2);
  110. return i > 1;
  111. }
  112. long X509_get_version(const X509 *x)
  113. {
  114. return ASN1_INTEGER_get(x->cert_info.version);
  115. }
  116. const ASN1_TIME *X509_get0_notBefore(const X509 *x)
  117. {
  118. return x->cert_info.validity.notBefore;
  119. }
  120. const ASN1_TIME *X509_get0_notAfter(const X509 *x)
  121. {
  122. return x->cert_info.validity.notAfter;
  123. }
  124. ASN1_TIME *X509_getm_notBefore(const X509 *x)
  125. {
  126. return x->cert_info.validity.notBefore;
  127. }
  128. ASN1_TIME *X509_getm_notAfter(const X509 *x)
  129. {
  130. return x->cert_info.validity.notAfter;
  131. }
  132. int X509_get_signature_type(const X509 *x)
  133. {
  134. return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm));
  135. }
  136. X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x)
  137. {
  138. return x->cert_info.key;
  139. }
  140. const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
  141. {
  142. return x->cert_info.extensions;
  143. }
  144. void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid,
  145. const ASN1_BIT_STRING **psuid)
  146. {
  147. if (piuid != NULL)
  148. *piuid = x->cert_info.issuerUID;
  149. if (psuid != NULL)
  150. *psuid = x->cert_info.subjectUID;
  151. }
  152. const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
  153. {
  154. return &x->cert_info.signature;
  155. }
  156. int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid,
  157. int *secbits, uint32_t *flags)
  158. {
  159. if (mdnid != NULL)
  160. *mdnid = siginf->mdnid;
  161. if (pknid != NULL)
  162. *pknid = siginf->pknid;
  163. if (secbits != NULL)
  164. *secbits = siginf->secbits;
  165. if (flags != NULL)
  166. *flags = siginf->flags;
  167. return (siginf->flags & X509_SIG_INFO_VALID) != 0;
  168. }
  169. void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid,
  170. int secbits, uint32_t flags)
  171. {
  172. siginf->mdnid = mdnid;
  173. siginf->pknid = pknid;
  174. siginf->secbits = secbits;
  175. siginf->flags = flags;
  176. }
  177. int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits,
  178. uint32_t *flags)
  179. {
  180. X509_check_purpose(x, -1, -1);
  181. return X509_SIG_INFO_get(&x->siginf, mdnid, pknid, secbits, flags);
  182. }
  183. /* Modify *siginf according to alg and sig. Return 1 on success, else 0. */
  184. static int x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  185. const ASN1_STRING *sig, const EVP_PKEY *pubkey)
  186. {
  187. int pknid, mdnid;
  188. const EVP_MD *md;
  189. const EVP_PKEY_ASN1_METHOD *ameth;
  190. siginf->mdnid = NID_undef;
  191. siginf->pknid = NID_undef;
  192. siginf->secbits = -1;
  193. siginf->flags = 0;
  194. if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)
  195. || pknid == NID_undef) {
  196. ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
  197. return 0;
  198. }
  199. siginf->mdnid = mdnid;
  200. siginf->pknid = pknid;
  201. switch (mdnid) {
  202. case NID_undef:
  203. /* If we have one, use a custom handler for this algorithm */
  204. ameth = EVP_PKEY_asn1_find(NULL, pknid);
  205. if (ameth != NULL && ameth->siginf_set != NULL
  206. && ameth->siginf_set(siginf, alg, sig))
  207. break;
  208. if (pubkey != NULL) {
  209. int secbits;
  210. secbits = EVP_PKEY_get_security_bits(pubkey);
  211. if (secbits != 0) {
  212. siginf->secbits = secbits;
  213. break;
  214. }
  215. }
  216. ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET);
  217. return 0;
  218. /*
  219. * SHA1 and MD5 are known to be broken. Reduce security bits so that
  220. * they're no longer accepted at security level 1.
  221. * The real values don't really matter as long as they're lower than 80,
  222. * which is our security level 1.
  223. */
  224. case NID_sha1:
  225. /*
  226. * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack
  227. * for SHA1 at2^63.4
  228. */
  229. siginf->secbits = 63;
  230. break;
  231. case NID_md5:
  232. /*
  233. * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
  234. * puts a chosen-prefix attack for MD5 at 2^39.
  235. */
  236. siginf->secbits = 39;
  237. break;
  238. case NID_id_GostR3411_94:
  239. /*
  240. * There is a collision attack on GOST R 34.11-94 at 2^105, see
  241. * https://link.springer.com/chapter/10.1007%2F978-3-540-85174-5_10
  242. */
  243. siginf->secbits = 105;
  244. break;
  245. default:
  246. /* Security bits: half number of bits in digest */
  247. if ((md = EVP_get_digestbynid(mdnid)) == NULL) {
  248. ERR_raise(ERR_LIB_X509, X509_R_ERROR_GETTING_MD_BY_NID);
  249. return 0;
  250. }
  251. siginf->secbits = EVP_MD_get_size(md) * 4;
  252. break;
  253. }
  254. switch (mdnid) {
  255. case NID_sha1:
  256. case NID_sha256:
  257. case NID_sha384:
  258. case NID_sha512:
  259. siginf->flags |= X509_SIG_INFO_TLS;
  260. }
  261. siginf->flags |= X509_SIG_INFO_VALID;
  262. return 1;
  263. }
  264. /* Returns 1 on success, 0 on failure */
  265. int ossl_x509_init_sig_info(X509 *x)
  266. {
  267. return x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature,
  268. X509_PUBKEY_get0(x->cert_info.key));
  269. }