x509_req.c 7.5 KB

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