A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
9.0 KiB

17 years ago
  1. /*
  2. * Copyright 1995-2020 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. ASN1err(ASN1_F_ASN1_SIGN, 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. ASN1err(ASN1_F_ASN1_SIGN, ASN1_R_UNKNOWN_OBJECT_TYPE);
  60. goto err;
  61. }
  62. if (a->algorithm->length == 0) {
  63. ASN1err(ASN1_F_ASN1_SIGN,
  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. ASN1err(ASN1_F_ASN1_SIGN, ERR_R_INTERNAL_ERROR);
  71. goto err;
  72. }
  73. inll = (size_t)inl;
  74. buf_in = OPENSSL_malloc(inll);
  75. outll = outl = EVP_PKEY_size(pkey);
  76. buf_out = OPENSSL_malloc(outll);
  77. if (buf_in == NULL || buf_out == NULL) {
  78. outl = 0;
  79. ASN1err(ASN1_F_ASN1_SIGN, 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. ASN1err(ASN1_F_ASN1_SIGN, 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. ASN1err(0, ERR_R_MALLOC_FAILURE);
  126. return 0;
  127. }
  128. if (!EVP_DigestSignInit(ctx, NULL, md, NULL, pkey))
  129. goto err;
  130. rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, data, ctx);
  131. err:
  132. EVP_PKEY_CTX_free(EVP_MD_CTX_pkey_ctx(ctx));
  133. EVP_MD_CTX_free(ctx);
  134. return rv;
  135. }
  136. int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
  137. X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  138. const void *data, EVP_MD_CTX *ctx)
  139. {
  140. const EVP_MD *md;
  141. EVP_PKEY *pkey;
  142. unsigned char *buf_in = NULL, *buf_out = NULL;
  143. size_t inl = 0, outl = 0, outll = 0;
  144. int signid, paramtype, buf_len = 0;
  145. int rv, pkey_id;
  146. md = EVP_MD_CTX_md(ctx);
  147. pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
  148. if (pkey == NULL) {
  149. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
  150. goto err;
  151. }
  152. if (pkey->ameth == NULL) {
  153. EVP_PKEY_CTX *pctx = EVP_MD_CTX_pkey_ctx(ctx);
  154. OSSL_PARAM params[2];
  155. unsigned char aid[128];
  156. size_t aid_len = 0;
  157. if (pctx == NULL
  158. || !EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
  159. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
  160. goto err;
  161. }
  162. params[0] =
  163. OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID,
  164. aid, sizeof(aid));
  165. params[1] = OSSL_PARAM_construct_end();
  166. if (EVP_PKEY_CTX_get_params(pctx, params) <= 0)
  167. goto err;
  168. if ((aid_len = params[0].return_size) == 0) {
  169. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX,
  170. 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. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, 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. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, 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. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, 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. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
  209. goto err;
  210. }
  211. pkey_id =
  212. #ifndef OPENSSL_NO_SM2
  213. EVP_PKEY_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. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX,
  218. ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
  219. goto err;
  220. }
  221. if (pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL)
  222. paramtype = V_ASN1_NULL;
  223. else
  224. paramtype = V_ASN1_UNDEF;
  225. if (algor1)
  226. X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL);
  227. if (algor2)
  228. X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL);
  229. }
  230. buf_len = ASN1_item_i2d(data, &buf_in, it);
  231. if (buf_len <= 0) {
  232. outl = 0;
  233. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_INTERNAL_ERROR);
  234. goto err;
  235. }
  236. inl = buf_len;
  237. if (!EVP_DigestSign(ctx, NULL, &outll, buf_in, inl)) {
  238. outl = 0;
  239. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB);
  240. goto err;
  241. }
  242. outl = outll;
  243. buf_out = OPENSSL_malloc(outll);
  244. if (buf_in == NULL || buf_out == NULL) {
  245. outl = 0;
  246. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_MALLOC_FAILURE);
  247. goto err;
  248. }
  249. if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) {
  250. outl = 0;
  251. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB);
  252. goto err;
  253. }
  254. OPENSSL_free(signature->data);
  255. signature->data = buf_out;
  256. buf_out = NULL;
  257. signature->length = outl;
  258. /*
  259. * In the interests of compatibility, I'll make sure that the bit string
  260. * has a 'not-used bits' value of 0
  261. */
  262. signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  263. signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  264. err:
  265. OPENSSL_clear_free((char *)buf_in, inl);
  266. OPENSSL_clear_free((char *)buf_out, outll);
  267. return outl;
  268. }