a_sign.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 1995-2022 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 <time.h>
  11. #include <sys/types.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/bn.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/objects.h>
  17. #include <openssl/buffer.h>
  18. #include <openssl/core_names.h>
  19. #include "crypto/asn1.h"
  20. #include "crypto/evp.h"
  21. #ifndef OPENSSL_NO_DEPRECATED_3_0
  22. int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
  23. ASN1_BIT_STRING *signature, char *data, EVP_PKEY *pkey,
  24. const EVP_MD *type)
  25. {
  26. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  27. unsigned char *p, *buf_in = NULL, *buf_out = NULL;
  28. int i, inl = 0, outl = 0;
  29. size_t inll = 0, outll = 0;
  30. X509_ALGOR *a;
  31. if (ctx == NULL) {
  32. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  33. goto err;
  34. }
  35. for (i = 0; i < 2; i++) {
  36. if (i == 0)
  37. a = algor1;
  38. else
  39. a = algor2;
  40. if (a == NULL)
  41. continue;
  42. if (type->pkey_type == NID_dsaWithSHA1) {
  43. /*
  44. * special case: RFC 2459 tells us to omit 'parameters' with
  45. * id-dsa-with-sha1
  46. */
  47. ASN1_TYPE_free(a->parameter);
  48. a->parameter = NULL;
  49. } else if ((a->parameter == NULL) ||
  50. (a->parameter->type != V_ASN1_NULL)) {
  51. ASN1_TYPE_free(a->parameter);
  52. if ((a->parameter = ASN1_TYPE_new()) == NULL)
  53. goto err;
  54. a->parameter->type = V_ASN1_NULL;
  55. }
  56. ASN1_OBJECT_free(a->algorithm);
  57. a->algorithm = OBJ_nid2obj(type->pkey_type);
  58. if (a->algorithm == NULL) {
  59. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_OBJECT_TYPE);
  60. goto err;
  61. }
  62. if (a->algorithm->length == 0) {
  63. ERR_raise(ERR_LIB_ASN1,
  64. ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
  65. goto err;
  66. }
  67. }
  68. inl = i2d(data, NULL);
  69. if (inl <= 0) {
  70. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  71. goto err;
  72. }
  73. inll = (size_t)inl;
  74. buf_in = OPENSSL_malloc(inll);
  75. outll = outl = EVP_PKEY_get_size(pkey);
  76. buf_out = OPENSSL_malloc(outll);
  77. if (buf_in == NULL || buf_out == NULL) {
  78. outl = 0;
  79. goto err;
  80. }
  81. p = buf_in;
  82. i2d(data, &p);
  83. if (!EVP_SignInit_ex(ctx, type, NULL)
  84. || !EVP_SignUpdate(ctx, (unsigned char *)buf_in, inl)
  85. || !EVP_SignFinal(ctx, (unsigned char *)buf_out,
  86. (unsigned int *)&outl, pkey)) {
  87. outl = 0;
  88. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  89. goto err;
  90. }
  91. ASN1_STRING_set0(signature, buf_out, outl);
  92. buf_out = NULL;
  93. /*
  94. * In the interests of compatibility, I'll make sure that the bit string
  95. * has a 'not-used bits' value of 0
  96. */
  97. ossl_asn1_string_set_bits_left(signature, 0);
  98. err:
  99. EVP_MD_CTX_free(ctx);
  100. OPENSSL_clear_free((char *)buf_in, inll);
  101. OPENSSL_clear_free((char *)buf_out, outll);
  102. return outl;
  103. }
  104. #endif
  105. int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
  106. ASN1_BIT_STRING *signature, const void *data,
  107. EVP_PKEY *pkey, const EVP_MD *md)
  108. {
  109. return ASN1_item_sign_ex(it, algor1, algor2, signature, data, NULL, pkey,
  110. md, NULL, NULL);
  111. }
  112. int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1,
  113. X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  114. const void *data, const ASN1_OCTET_STRING *id,
  115. EVP_PKEY *pkey, const EVP_MD *md, OSSL_LIB_CTX *libctx,
  116. const char *propq)
  117. {
  118. int rv = 0;
  119. EVP_MD_CTX *ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq);
  120. if (ctx == NULL) {
  121. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  122. return 0;
  123. }
  124. /* We can use the non _ex variant here since the pkey is already setup */
  125. if (!EVP_DigestSignInit(ctx, NULL, md, NULL, pkey))
  126. goto err;
  127. rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, data, ctx);
  128. err:
  129. EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx));
  130. EVP_MD_CTX_free(ctx);
  131. return rv;
  132. }
  133. int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
  134. X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  135. const void *data, EVP_MD_CTX *ctx)
  136. {
  137. const EVP_MD *md;
  138. EVP_PKEY *pkey;
  139. unsigned char *buf_in = NULL, *buf_out = NULL;
  140. size_t inl = 0, outl = 0, outll = 0;
  141. int signid, paramtype, buf_len = 0;
  142. int rv, pkey_id;
  143. md = EVP_MD_CTX_get0_md(ctx);
  144. pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx));
  145. if (pkey == NULL) {
  146. ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
  147. goto err;
  148. }
  149. if (pkey->ameth == NULL) {
  150. EVP_PKEY_CTX *pctx = EVP_MD_CTX_get_pkey_ctx(ctx);
  151. OSSL_PARAM params[2];
  152. unsigned char aid[128];
  153. size_t aid_len = 0;
  154. if (pctx == NULL
  155. || !EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
  156. ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
  157. goto err;
  158. }
  159. params[0] =
  160. OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID,
  161. aid, sizeof(aid));
  162. params[1] = OSSL_PARAM_construct_end();
  163. if (EVP_PKEY_CTX_get_params(pctx, params) <= 0)
  164. goto err;
  165. if ((aid_len = params[0].return_size) == 0) {
  166. ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
  167. goto err;
  168. }
  169. if (algor1 != NULL) {
  170. const unsigned char *pp = aid;
  171. if (d2i_X509_ALGOR(&algor1, &pp, aid_len) == NULL) {
  172. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  173. goto err;
  174. }
  175. }
  176. if (algor2 != NULL) {
  177. const unsigned char *pp = aid;
  178. if (d2i_X509_ALGOR(&algor2, &pp, aid_len) == NULL) {
  179. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  180. goto err;
  181. }
  182. }
  183. rv = 3;
  184. } else if (pkey->ameth->item_sign) {
  185. rv = pkey->ameth->item_sign(ctx, it, data, algor1, algor2, signature);
  186. if (rv == 1)
  187. outl = signature->length;
  188. /*-
  189. * Return value meanings:
  190. * <=0: error.
  191. * 1: method does everything.
  192. * 2: carry on as normal.
  193. * 3: ASN1 method sets algorithm identifiers: just sign.
  194. */
  195. if (rv <= 0)
  196. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  197. if (rv <= 1)
  198. goto err;
  199. } else {
  200. rv = 2;
  201. }
  202. if (rv == 2) {
  203. if (md == NULL) {
  204. ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
  205. goto err;
  206. }
  207. pkey_id =
  208. #ifndef OPENSSL_NO_SM2
  209. EVP_PKEY_get_id(pkey) == NID_sm2 ? NID_sm2 :
  210. #endif
  211. pkey->ameth->pkey_id;
  212. if (!OBJ_find_sigid_by_algs(&signid, EVP_MD_nid(md), pkey_id)) {
  213. ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
  214. goto err;
  215. }
  216. paramtype = pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL ?
  217. V_ASN1_NULL : V_ASN1_UNDEF;
  218. if (algor1 != NULL
  219. && !X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL))
  220. goto err;
  221. if (algor2 != NULL
  222. && !X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL))
  223. goto err;
  224. }
  225. buf_len = ASN1_item_i2d(data, &buf_in, it);
  226. if (buf_len <= 0) {
  227. outl = 0;
  228. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  229. goto err;
  230. }
  231. inl = buf_len;
  232. if (!EVP_DigestSign(ctx, NULL, &outll, buf_in, inl)) {
  233. outl = 0;
  234. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  235. goto err;
  236. }
  237. outl = outll;
  238. buf_out = OPENSSL_malloc(outll);
  239. if (buf_in == NULL || buf_out == NULL) {
  240. outl = 0;
  241. goto err;
  242. }
  243. if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) {
  244. outl = 0;
  245. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  246. goto err;
  247. }
  248. ASN1_STRING_set0(signature, buf_out, outl);
  249. buf_out = NULL;
  250. /*
  251. * In the interests of compatibility, I'll make sure that the bit string
  252. * has a 'not-used bits' value of 0
  253. */
  254. ossl_asn1_string_set_bits_left(signature, 0);
  255. err:
  256. OPENSSL_clear_free((char *)buf_in, inl);
  257. OPENSSL_clear_free((char *)buf_out, outll);
  258. return outl;
  259. }