x509_req.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 1995-2020 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 || (ext->type != V_ASN1_SEQUENCE))
  149. return NULL;
  150. p = ext->value.sequence->data;
  151. return (STACK_OF(X509_EXTENSION) *)
  152. ASN1_item_d2i(NULL, &p, ext->value.sequence->length,
  153. ASN1_ITEM_rptr(X509_EXTENSIONS));
  154. }
  155. /*
  156. * Add a STACK_OF extensions to a certificate request: allow alternative OIDs
  157. * in case we want to create a non standard one.
  158. */
  159. int X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts,
  160. int nid)
  161. {
  162. int extlen;
  163. int rv = 0;
  164. unsigned char *ext = NULL;
  165. /* Generate encoding of extensions */
  166. extlen = ASN1_item_i2d((ASN1_VALUE *)exts, &ext,
  167. ASN1_ITEM_rptr(X509_EXTENSIONS));
  168. if (extlen <= 0)
  169. return 0;
  170. rv = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, extlen);
  171. OPENSSL_free(ext);
  172. return rv;
  173. }
  174. /* This is the normal usage: use the "official" OID */
  175. int X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts)
  176. {
  177. return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
  178. }
  179. /* Request attribute functions */
  180. int X509_REQ_get_attr_count(const X509_REQ *req)
  181. {
  182. return X509at_get_attr_count(req->req_info.attributes);
  183. }
  184. int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos)
  185. {
  186. return X509at_get_attr_by_NID(req->req_info.attributes, nid, lastpos);
  187. }
  188. int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj,
  189. int lastpos)
  190. {
  191. return X509at_get_attr_by_OBJ(req->req_info.attributes, obj, lastpos);
  192. }
  193. X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc)
  194. {
  195. return X509at_get_attr(req->req_info.attributes, loc);
  196. }
  197. X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)
  198. {
  199. return X509at_delete_attr(req->req_info.attributes, loc);
  200. }
  201. int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
  202. {
  203. if (X509at_add1_attr(&req->req_info.attributes, attr))
  204. return 1;
  205. return 0;
  206. }
  207. int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
  208. const ASN1_OBJECT *obj, int type,
  209. const unsigned char *bytes, int len)
  210. {
  211. if (X509at_add1_attr_by_OBJ(&req->req_info.attributes, obj,
  212. type, bytes, len))
  213. return 1;
  214. return 0;
  215. }
  216. int X509_REQ_add1_attr_by_NID(X509_REQ *req,
  217. int nid, int type,
  218. const unsigned char *bytes, int len)
  219. {
  220. if (X509at_add1_attr_by_NID(&req->req_info.attributes, nid,
  221. type, bytes, len))
  222. return 1;
  223. return 0;
  224. }
  225. int X509_REQ_add1_attr_by_txt(X509_REQ *req,
  226. const char *attrname, int type,
  227. const unsigned char *bytes, int len)
  228. {
  229. if (X509at_add1_attr_by_txt(&req->req_info.attributes, attrname,
  230. type, bytes, len))
  231. return 1;
  232. return 0;
  233. }
  234. long X509_REQ_get_version(const X509_REQ *req)
  235. {
  236. return ASN1_INTEGER_get(req->req_info.version);
  237. }
  238. X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req)
  239. {
  240. return req->req_info.subject;
  241. }
  242. void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
  243. const X509_ALGOR **palg)
  244. {
  245. if (psig != NULL)
  246. *psig = req->signature;
  247. if (palg != NULL)
  248. *palg = &req->sig_alg;
  249. }
  250. void X509_REQ_set0_signature(X509_REQ *req, ASN1_BIT_STRING *psig)
  251. {
  252. if (req->signature)
  253. ASN1_BIT_STRING_free(req->signature);
  254. req->signature = psig;
  255. }
  256. int X509_REQ_set1_signature_algo(X509_REQ *req, X509_ALGOR *palg)
  257. {
  258. return X509_ALGOR_copy(&req->sig_alg, palg);
  259. }
  260. int X509_REQ_get_signature_nid(const X509_REQ *req)
  261. {
  262. return OBJ_obj2nid(req->sig_alg.algorithm);
  263. }
  264. int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp)
  265. {
  266. req->req_info.enc.modified = 1;
  267. return i2d_X509_REQ_INFO(&req->req_info, pp);
  268. }