a_sign.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_MALLOC_FAILURE);
  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. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  80. goto err;
  81. }
  82. p = buf_in;
  83. i2d(data, &p);
  84. if (!EVP_SignInit_ex(ctx, type, NULL)
  85. || !EVP_SignUpdate(ctx, (unsigned char *)buf_in, inl)
  86. || !EVP_SignFinal(ctx, (unsigned char *)buf_out,
  87. (unsigned int *)&outl, pkey)) {
  88. outl = 0;
  89. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  90. goto err;
  91. }
  92. OPENSSL_free(signature->data);
  93. signature->data = buf_out;
  94. buf_out = NULL;
  95. signature->length = outl;
  96. /*
  97. * In the interests of compatibility, I'll make sure that the bit string
  98. * has a 'not-used bits' value of 0
  99. */
  100. signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  101. signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  102. err:
  103. EVP_MD_CTX_free(ctx);
  104. OPENSSL_clear_free((char *)buf_in, inll);
  105. OPENSSL_clear_free((char *)buf_out, outll);
  106. return outl;
  107. }
  108. #endif
  109. int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
  110. ASN1_BIT_STRING *signature, const void *data,
  111. EVP_PKEY *pkey, const EVP_MD *md)
  112. {
  113. return ASN1_item_sign_ex(it, algor1, algor2, signature, data, NULL, pkey,
  114. md, NULL, NULL);
  115. }
  116. int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1,
  117. X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  118. const void *data, const ASN1_OCTET_STRING *id,
  119. EVP_PKEY *pkey, const EVP_MD *md, OSSL_LIB_CTX *libctx,
  120. const char *propq)
  121. {
  122. int rv = 0;
  123. EVP_MD_CTX *ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq);
  124. if (ctx == NULL) {
  125. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  126. return 0;
  127. }
  128. /* We can use the non _ex variant here since the pkey is already setup */
  129. if (!EVP_DigestSignInit(ctx, NULL, md, NULL, pkey))
  130. goto err;
  131. rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, data, ctx);
  132. err:
  133. EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx));
  134. EVP_MD_CTX_free(ctx);
  135. return rv;
  136. }
  137. int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
  138. X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  139. const void *data, EVP_MD_CTX *ctx)
  140. {
  141. const EVP_MD *md;
  142. EVP_PKEY *pkey;
  143. unsigned char *buf_in = NULL, *buf_out = NULL;
  144. size_t inl = 0, outl = 0, outll = 0;
  145. int signid, paramtype, buf_len = 0;
  146. int rv, pkey_id;
  147. md = EVP_MD_CTX_get0_md(ctx);
  148. pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx));
  149. if (pkey == NULL) {
  150. ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
  151. goto err;
  152. }
  153. if (pkey->ameth == NULL) {
  154. EVP_PKEY_CTX *pctx = EVP_MD_CTX_get_pkey_ctx(ctx);
  155. OSSL_PARAM params[2];
  156. unsigned char aid[128];
  157. size_t aid_len = 0;
  158. if (pctx == NULL
  159. || !EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
  160. ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
  161. goto err;
  162. }
  163. params[0] =
  164. OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID,
  165. aid, sizeof(aid));
  166. params[1] = OSSL_PARAM_construct_end();
  167. if (EVP_PKEY_CTX_get_params(pctx, params) <= 0)
  168. goto err;
  169. if ((aid_len = params[0].return_size) == 0) {
  170. ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
  171. goto err;
  172. }
  173. if (algor1 != NULL) {
  174. const unsigned char *pp = aid;
  175. if (d2i_X509_ALGOR(&algor1, &pp, aid_len) == NULL) {
  176. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  177. goto err;
  178. }
  179. }
  180. if (algor2 != NULL) {
  181. const unsigned char *pp = aid;
  182. if (d2i_X509_ALGOR(&algor2, &pp, aid_len) == NULL) {
  183. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  184. goto err;
  185. }
  186. }
  187. rv = 3;
  188. } else if (pkey->ameth->item_sign) {
  189. rv = pkey->ameth->item_sign(ctx, it, data, algor1, algor2, signature);
  190. if (rv == 1)
  191. outl = signature->length;
  192. /*-
  193. * Return value meanings:
  194. * <=0: error.
  195. * 1: method does everything.
  196. * 2: carry on as normal.
  197. * 3: ASN1 method sets algorithm identifiers: just sign.
  198. */
  199. if (rv <= 0)
  200. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  201. if (rv <= 1)
  202. goto err;
  203. } else {
  204. rv = 2;
  205. }
  206. if (rv == 2) {
  207. if (md == NULL) {
  208. ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED);
  209. goto err;
  210. }
  211. pkey_id =
  212. #ifndef OPENSSL_NO_SM2
  213. EVP_PKEY_get_id(pkey) == NID_sm2 ? NID_sm2 :
  214. #endif
  215. pkey->ameth->pkey_id;
  216. if (!OBJ_find_sigid_by_algs(&signid, EVP_MD_nid(md), pkey_id)) {
  217. ERR_raise(ERR_LIB_ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
  218. goto err;
  219. }
  220. paramtype = pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL ?
  221. V_ASN1_NULL : V_ASN1_UNDEF;
  222. if (algor1 != NULL
  223. && !X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL))
  224. goto err;
  225. if (algor2 != NULL
  226. && !X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL))
  227. goto err;
  228. }
  229. buf_len = ASN1_item_i2d(data, &buf_in, it);
  230. if (buf_len <= 0) {
  231. outl = 0;
  232. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  233. goto err;
  234. }
  235. inl = buf_len;
  236. if (!EVP_DigestSign(ctx, NULL, &outll, buf_in, inl)) {
  237. outl = 0;
  238. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  239. goto err;
  240. }
  241. outl = outll;
  242. buf_out = OPENSSL_malloc(outll);
  243. if (buf_in == NULL || buf_out == NULL) {
  244. outl = 0;
  245. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  246. goto err;
  247. }
  248. if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) {
  249. outl = 0;
  250. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  251. goto err;
  252. }
  253. OPENSSL_free(signature->data);
  254. signature->data = buf_out;
  255. buf_out = NULL;
  256. signature->length = outl;
  257. /*
  258. * In the interests of compatibility, I'll make sure that the bit string
  259. * has a 'not-used bits' value of 0
  260. */
  261. signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  262. signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  263. err:
  264. OPENSSL_clear_free((char *)buf_in, inl);
  265. OPENSSL_clear_free((char *)buf_out, outll);
  266. return outl;
  267. }