x509_req.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 <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_ex(x->libctx, x->propq);
  27. if (ret == NULL) {
  28. ERR_raise(ERR_LIB_X509, 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_eq(xk, k)) {
  76. case 1:
  77. ok = 1;
  78. break;
  79. case 0:
  80. ERR_raise(ERR_LIB_X509, X509_R_KEY_VALUES_MISMATCH);
  81. break;
  82. case -1:
  83. ERR_raise(ERR_LIB_X509, X509_R_KEY_TYPE_MISMATCH);
  84. break;
  85. case -2:
  86. ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_KEY_TYPE);
  87. }
  88. EVP_PKEY_free(xk);
  89. return ok;
  90. }
  91. /*
  92. * It seems several organisations had the same idea of including a list of
  93. * extensions in a certificate request. There are at least two OIDs that are
  94. * used and there may be more: so the list is configurable.
  95. */
  96. static int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef };
  97. static int *ext_nids = ext_nid_list;
  98. int X509_REQ_extension_nid(int req_nid)
  99. {
  100. int i, nid;
  101. for (i = 0;; i++) {
  102. nid = ext_nids[i];
  103. if (nid == NID_undef)
  104. return 0;
  105. else if (req_nid == nid)
  106. return 1;
  107. }
  108. }
  109. int *X509_REQ_get_extension_nids(void)
  110. {
  111. return ext_nids;
  112. }
  113. void X509_REQ_set_extension_nids(int *nids)
  114. {
  115. ext_nids = nids;
  116. }
  117. STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req)
  118. {
  119. X509_ATTRIBUTE *attr;
  120. ASN1_TYPE *ext = NULL;
  121. int idx, *pnid;
  122. const unsigned char *p;
  123. if ((req == NULL) || !ext_nids)
  124. return NULL;
  125. for (pnid = ext_nids; *pnid != NID_undef; pnid++) {
  126. idx = X509_REQ_get_attr_by_NID(req, *pnid, -1);
  127. if (idx == -1)
  128. continue;
  129. attr = X509_REQ_get_attr(req, idx);
  130. ext = X509_ATTRIBUTE_get0_type(attr, 0);
  131. break;
  132. }
  133. if (ext == NULL) /* no extensions is not an error */
  134. return sk_X509_EXTENSION_new_null();
  135. if (ext->type != V_ASN1_SEQUENCE)
  136. return NULL;
  137. p = ext->value.sequence->data;
  138. return (STACK_OF(X509_EXTENSION) *)
  139. ASN1_item_d2i(NULL, &p, ext->value.sequence->length,
  140. ASN1_ITEM_rptr(X509_EXTENSIONS));
  141. }
  142. /*
  143. * Add a STACK_OF extensions to a certificate request: allow alternative OIDs
  144. * in case we want to create a non standard one.
  145. */
  146. int X509_REQ_add_extensions_nid(X509_REQ *req,
  147. const STACK_OF(X509_EXTENSION) *exts, int nid)
  148. {
  149. int extlen;
  150. int rv = 0;
  151. unsigned char *ext = NULL;
  152. /* Generate encoding of extensions */
  153. extlen = ASN1_item_i2d((const ASN1_VALUE *)exts, &ext,
  154. ASN1_ITEM_rptr(X509_EXTENSIONS));
  155. if (extlen <= 0)
  156. return 0;
  157. rv = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, extlen);
  158. OPENSSL_free(ext);
  159. return rv;
  160. }
  161. /* This is the normal usage: use the "official" OID */
  162. int X509_REQ_add_extensions(X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts)
  163. {
  164. return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
  165. }
  166. /* Request attribute functions */
  167. int X509_REQ_get_attr_count(const X509_REQ *req)
  168. {
  169. return X509at_get_attr_count(req->req_info.attributes);
  170. }
  171. int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos)
  172. {
  173. return X509at_get_attr_by_NID(req->req_info.attributes, nid, lastpos);
  174. }
  175. int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj,
  176. int lastpos)
  177. {
  178. return X509at_get_attr_by_OBJ(req->req_info.attributes, obj, lastpos);
  179. }
  180. X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc)
  181. {
  182. return X509at_get_attr(req->req_info.attributes, loc);
  183. }
  184. X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)
  185. {
  186. return X509at_delete_attr(req->req_info.attributes, loc);
  187. }
  188. int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
  189. {
  190. if (X509at_add1_attr(&req->req_info.attributes, attr))
  191. return 1;
  192. return 0;
  193. }
  194. int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
  195. const ASN1_OBJECT *obj, int type,
  196. const unsigned char *bytes, int len)
  197. {
  198. if (X509at_add1_attr_by_OBJ(&req->req_info.attributes, obj,
  199. type, bytes, len))
  200. return 1;
  201. return 0;
  202. }
  203. int X509_REQ_add1_attr_by_NID(X509_REQ *req,
  204. int nid, int type,
  205. const unsigned char *bytes, int len)
  206. {
  207. if (X509at_add1_attr_by_NID(&req->req_info.attributes, nid,
  208. type, bytes, len))
  209. return 1;
  210. return 0;
  211. }
  212. int X509_REQ_add1_attr_by_txt(X509_REQ *req,
  213. const char *attrname, int type,
  214. const unsigned char *bytes, int len)
  215. {
  216. if (X509at_add1_attr_by_txt(&req->req_info.attributes, attrname,
  217. type, bytes, len))
  218. return 1;
  219. return 0;
  220. }
  221. long X509_REQ_get_version(const X509_REQ *req)
  222. {
  223. return ASN1_INTEGER_get(req->req_info.version);
  224. }
  225. X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req)
  226. {
  227. return req->req_info.subject;
  228. }
  229. void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
  230. const X509_ALGOR **palg)
  231. {
  232. if (psig != NULL)
  233. *psig = req->signature;
  234. if (palg != NULL)
  235. *palg = &req->sig_alg;
  236. }
  237. void X509_REQ_set0_signature(X509_REQ *req, ASN1_BIT_STRING *psig)
  238. {
  239. if (req->signature)
  240. ASN1_BIT_STRING_free(req->signature);
  241. req->signature = psig;
  242. }
  243. int X509_REQ_set1_signature_algo(X509_REQ *req, X509_ALGOR *palg)
  244. {
  245. return X509_ALGOR_copy(&req->sig_alg, palg);
  246. }
  247. int X509_REQ_get_signature_nid(const X509_REQ *req)
  248. {
  249. return OBJ_obj2nid(req->sig_alg.algorithm);
  250. }
  251. int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp)
  252. {
  253. req->req_info.enc.modified = 1;
  254. return i2d_X509_REQ_INFO(&req->req_info, pp);
  255. }