x509_req.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright 1995-2022 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/bn.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/asn1.h>
  14. #include <openssl/asn1t.h>
  15. #include <openssl/x509.h>
  16. #include "crypto/x509.h"
  17. #include <openssl/objects.h>
  18. #include <openssl/buffer.h>
  19. #include <openssl/pem.h>
  20. X509_REQ *X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
  21. {
  22. X509_REQ *ret;
  23. X509_REQ_INFO *ri;
  24. int i;
  25. EVP_PKEY *pktmp;
  26. ret = X509_REQ_new();
  27. if (ret == NULL) {
  28. X509err(X509_F_X509_TO_X509_REQ, ERR_R_MALLOC_FAILURE);
  29. goto err;
  30. }
  31. ri = &ret->req_info;
  32. ri->version->length = 1;
  33. ri->version->data = OPENSSL_malloc(1);
  34. if (ri->version->data == NULL)
  35. goto err;
  36. ri->version->data[0] = 0; /* version == 0 */
  37. if (!X509_REQ_set_subject_name(ret, X509_get_subject_name(x)))
  38. goto err;
  39. pktmp = X509_get0_pubkey(x);
  40. if (pktmp == NULL)
  41. goto err;
  42. i = X509_REQ_set_pubkey(ret, pktmp);
  43. if (!i)
  44. goto err;
  45. if (pkey != NULL) {
  46. if (!X509_REQ_sign(ret, pkey, md))
  47. goto err;
  48. }
  49. return ret;
  50. err:
  51. X509_REQ_free(ret);
  52. return NULL;
  53. }
  54. EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req)
  55. {
  56. if (req == NULL)
  57. return NULL;
  58. return X509_PUBKEY_get(req->req_info.pubkey);
  59. }
  60. EVP_PKEY *X509_REQ_get0_pubkey(X509_REQ *req)
  61. {
  62. if (req == NULL)
  63. return NULL;
  64. return X509_PUBKEY_get0(req->req_info.pubkey);
  65. }
  66. X509_PUBKEY *X509_REQ_get_X509_PUBKEY(X509_REQ *req)
  67. {
  68. return req->req_info.pubkey;
  69. }
  70. int X509_REQ_check_private_key(X509_REQ *x, EVP_PKEY *k)
  71. {
  72. EVP_PKEY *xk = NULL;
  73. int ok = 0;
  74. xk = X509_REQ_get_pubkey(x);
  75. switch (EVP_PKEY_cmp(xk, k)) {
  76. case 1:
  77. ok = 1;
  78. break;
  79. case 0:
  80. X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,
  81. X509_R_KEY_VALUES_MISMATCH);
  82. break;
  83. case -1:
  84. X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
  85. break;
  86. case -2:
  87. #ifndef OPENSSL_NO_EC
  88. if (EVP_PKEY_id(k) == EVP_PKEY_EC) {
  89. X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, ERR_R_EC_LIB);
  90. break;
  91. }
  92. #endif
  93. #ifndef OPENSSL_NO_DH
  94. if (EVP_PKEY_id(k) == EVP_PKEY_DH) {
  95. /* No idea */
  96. X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,
  97. X509_R_CANT_CHECK_DH_KEY);
  98. break;
  99. }
  100. #endif
  101. X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
  102. }
  103. EVP_PKEY_free(xk);
  104. return ok;
  105. }
  106. /*
  107. * It seems several organisations had the same idea of including a list of
  108. * extensions in a certificate request. There are at least two OIDs that are
  109. * used and there may be more: so the list is configurable.
  110. */
  111. static int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef };
  112. static int *ext_nids = ext_nid_list;
  113. int X509_REQ_extension_nid(int req_nid)
  114. {
  115. int i, nid;
  116. for (i = 0;; i++) {
  117. nid = ext_nids[i];
  118. if (nid == NID_undef)
  119. return 0;
  120. else if (req_nid == nid)
  121. return 1;
  122. }
  123. }
  124. int *X509_REQ_get_extension_nids(void)
  125. {
  126. return ext_nids;
  127. }
  128. void X509_REQ_set_extension_nids(int *nids)
  129. {
  130. ext_nids = nids;
  131. }
  132. STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req)
  133. {
  134. X509_ATTRIBUTE *attr;
  135. ASN1_TYPE *ext = NULL;
  136. int idx, *pnid;
  137. const unsigned char *p;
  138. if ((req == NULL) || !ext_nids)
  139. return NULL;
  140. for (pnid = ext_nids; *pnid != NID_undef; pnid++) {
  141. idx = X509_REQ_get_attr_by_NID(req, *pnid, -1);
  142. if (idx == -1)
  143. continue;
  144. attr = X509_REQ_get_attr(req, idx);
  145. ext = X509_ATTRIBUTE_get0_type(attr, 0);
  146. break;
  147. }
  148. if (ext == NULL) /* no extensions is not an error */
  149. return sk_X509_EXTENSION_new_null();
  150. if (ext->type != V_ASN1_SEQUENCE)
  151. return NULL;
  152. p = ext->value.sequence->data;
  153. return (STACK_OF(X509_EXTENSION) *)
  154. ASN1_item_d2i(NULL, &p, ext->value.sequence->length,
  155. ASN1_ITEM_rptr(X509_EXTENSIONS));
  156. }
  157. /*
  158. * Add a STACK_OF extensions to a certificate request: allow alternative OIDs
  159. * in case we want to create a non standard one.
  160. */
  161. int X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts,
  162. int nid)
  163. {
  164. int extlen;
  165. int rv = 0;
  166. unsigned char *ext = NULL;
  167. /* Generate encoding of extensions */
  168. extlen = ASN1_item_i2d((ASN1_VALUE *)exts, &ext,
  169. ASN1_ITEM_rptr(X509_EXTENSIONS));
  170. if (extlen <= 0)
  171. return 0;
  172. rv = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, extlen);
  173. OPENSSL_free(ext);
  174. return rv;
  175. }
  176. /* This is the normal usage: use the "official" OID */
  177. int X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts)
  178. {
  179. return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
  180. }
  181. /* Request attribute functions */
  182. int X509_REQ_get_attr_count(const X509_REQ *req)
  183. {
  184. return X509at_get_attr_count(req->req_info.attributes);
  185. }
  186. int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos)
  187. {
  188. return X509at_get_attr_by_NID(req->req_info.attributes, nid, lastpos);
  189. }
  190. int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj,
  191. int lastpos)
  192. {
  193. return X509at_get_attr_by_OBJ(req->req_info.attributes, obj, lastpos);
  194. }
  195. X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc)
  196. {
  197. return X509at_get_attr(req->req_info.attributes, loc);
  198. }
  199. X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)
  200. {
  201. X509_ATTRIBUTE *attr = X509at_delete_attr(req->req_info.attributes, loc);
  202. if (attr != NULL)
  203. req->req_info.enc.modified = 1;
  204. return attr;
  205. }
  206. int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
  207. {
  208. if (!X509at_add1_attr(&req->req_info.attributes, attr))
  209. return 0;
  210. req->req_info.enc.modified = 1;
  211. return 1;
  212. }
  213. int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
  214. const ASN1_OBJECT *obj, int type,
  215. const unsigned char *bytes, int len)
  216. {
  217. if (!X509at_add1_attr_by_OBJ(&req->req_info.attributes, obj,
  218. type, bytes, len))
  219. return 0;
  220. req->req_info.enc.modified = 1;
  221. return 1;
  222. }
  223. int X509_REQ_add1_attr_by_NID(X509_REQ *req,
  224. int nid, int type,
  225. const unsigned char *bytes, int len)
  226. {
  227. if (!X509at_add1_attr_by_NID(&req->req_info.attributes, nid,
  228. type, bytes, len))
  229. return 0;
  230. req->req_info.enc.modified = 1;
  231. return 1;
  232. }
  233. int X509_REQ_add1_attr_by_txt(X509_REQ *req,
  234. const char *attrname, int type,
  235. const unsigned char *bytes, int len)
  236. {
  237. if (!X509at_add1_attr_by_txt(&req->req_info.attributes, attrname,
  238. type, bytes, len))
  239. return 0;
  240. req->req_info.enc.modified = 1;
  241. return 1;
  242. }
  243. long X509_REQ_get_version(const X509_REQ *req)
  244. {
  245. return ASN1_INTEGER_get(req->req_info.version);
  246. }
  247. X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req)
  248. {
  249. return req->req_info.subject;
  250. }
  251. void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
  252. const X509_ALGOR **palg)
  253. {
  254. if (psig != NULL)
  255. *psig = req->signature;
  256. if (palg != NULL)
  257. *palg = &req->sig_alg;
  258. }
  259. void X509_REQ_set0_signature(X509_REQ *req, ASN1_BIT_STRING *psig)
  260. {
  261. if (req->signature)
  262. ASN1_BIT_STRING_free(req->signature);
  263. req->signature = psig;
  264. }
  265. int X509_REQ_set1_signature_algo(X509_REQ *req, X509_ALGOR *palg)
  266. {
  267. return X509_ALGOR_copy(&req->sig_alg, palg);
  268. }
  269. int X509_REQ_get_signature_nid(const X509_REQ *req)
  270. {
  271. return OBJ_obj2nid(req->sig_alg.algorithm);
  272. }
  273. int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp)
  274. {
  275. req->req_info.enc.modified = 1;
  276. return i2d_X509_REQ_INFO(&req->req_info, pp);
  277. }