2
0

a_sign.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "crypto/asn1.h"
  19. #include "crypto/evp.h"
  20. #ifndef NO_ASN1_OLD
  21. int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, X509_ALGOR *algor2,
  22. ASN1_BIT_STRING *signature, char *data, EVP_PKEY *pkey,
  23. const EVP_MD *type)
  24. {
  25. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  26. unsigned char *p, *buf_in = NULL, *buf_out = NULL;
  27. int i, inl = 0, outl = 0;
  28. size_t inll = 0, outll = 0;
  29. X509_ALGOR *a;
  30. if (ctx == NULL) {
  31. ASN1err(ASN1_F_ASN1_SIGN, ERR_R_MALLOC_FAILURE);
  32. goto err;
  33. }
  34. for (i = 0; i < 2; i++) {
  35. if (i == 0)
  36. a = algor1;
  37. else
  38. a = algor2;
  39. if (a == NULL)
  40. continue;
  41. if (type->pkey_type == NID_dsaWithSHA1) {
  42. /*
  43. * special case: RFC 2459 tells us to omit 'parameters' with
  44. * id-dsa-with-sha1
  45. */
  46. ASN1_TYPE_free(a->parameter);
  47. a->parameter = NULL;
  48. } else if ((a->parameter == NULL) ||
  49. (a->parameter->type != V_ASN1_NULL)) {
  50. ASN1_TYPE_free(a->parameter);
  51. if ((a->parameter = ASN1_TYPE_new()) == NULL)
  52. goto err;
  53. a->parameter->type = V_ASN1_NULL;
  54. }
  55. ASN1_OBJECT_free(a->algorithm);
  56. a->algorithm = OBJ_nid2obj(type->pkey_type);
  57. if (a->algorithm == NULL) {
  58. ASN1err(ASN1_F_ASN1_SIGN, ASN1_R_UNKNOWN_OBJECT_TYPE);
  59. goto err;
  60. }
  61. if (a->algorithm->length == 0) {
  62. ASN1err(ASN1_F_ASN1_SIGN,
  63. ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
  64. goto err;
  65. }
  66. }
  67. inl = i2d(data, NULL);
  68. if (inl <= 0) {
  69. ASN1err(ASN1_F_ASN1_SIGN, ERR_R_INTERNAL_ERROR);
  70. goto err;
  71. }
  72. inll = (size_t)inl;
  73. buf_in = OPENSSL_malloc(inll);
  74. outll = outl = EVP_PKEY_size(pkey);
  75. buf_out = OPENSSL_malloc(outll);
  76. if (buf_in == NULL || buf_out == NULL) {
  77. outl = 0;
  78. ASN1err(ASN1_F_ASN1_SIGN, ERR_R_MALLOC_FAILURE);
  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. ASN1err(ASN1_F_ASN1_SIGN, ERR_R_EVP_LIB);
  89. goto err;
  90. }
  91. OPENSSL_free(signature->data);
  92. signature->data = buf_out;
  93. buf_out = NULL;
  94. signature->length = outl;
  95. /*
  96. * In the interests of compatibility, I'll make sure that the bit string
  97. * has a 'not-used bits' value of 0
  98. */
  99. signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  100. signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  101. err:
  102. EVP_MD_CTX_free(ctx);
  103. OPENSSL_clear_free((char *)buf_in, inll);
  104. OPENSSL_clear_free((char *)buf_out, outll);
  105. return outl;
  106. }
  107. #endif
  108. int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
  109. X509_ALGOR *algor2, ASN1_BIT_STRING *signature, void *asn,
  110. EVP_PKEY *pkey, const EVP_MD *type)
  111. {
  112. int rv;
  113. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  114. if (ctx == NULL) {
  115. ASN1err(ASN1_F_ASN1_ITEM_SIGN, ERR_R_MALLOC_FAILURE);
  116. return 0;
  117. }
  118. if (!EVP_DigestSignInit(ctx, NULL, type, NULL, pkey)) {
  119. EVP_MD_CTX_free(ctx);
  120. return 0;
  121. }
  122. rv = ASN1_item_sign_ctx(it, algor1, algor2, signature, asn, ctx);
  123. EVP_MD_CTX_free(ctx);
  124. return rv;
  125. }
  126. int ASN1_item_sign_ctx(const ASN1_ITEM *it,
  127. X509_ALGOR *algor1, X509_ALGOR *algor2,
  128. ASN1_BIT_STRING *signature, void *asn, EVP_MD_CTX *ctx)
  129. {
  130. const EVP_MD *type;
  131. EVP_PKEY *pkey;
  132. unsigned char *buf_in = NULL, *buf_out = NULL;
  133. size_t inl = 0, outl = 0, outll = 0;
  134. int signid, paramtype, buf_len = 0;
  135. int rv;
  136. type = EVP_MD_CTX_md(ctx);
  137. pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
  138. if (pkey == NULL) {
  139. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
  140. goto err;
  141. }
  142. if (pkey->ameth == NULL) {
  143. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
  144. goto err;
  145. }
  146. if (pkey->ameth->item_sign) {
  147. rv = pkey->ameth->item_sign(ctx, it, asn, algor1, algor2, signature);
  148. if (rv == 1)
  149. outl = signature->length;
  150. /*-
  151. * Return value meanings:
  152. * <=0: error.
  153. * 1: method does everything.
  154. * 2: carry on as normal.
  155. * 3: ASN1 method sets algorithm identifiers: just sign.
  156. */
  157. if (rv <= 0)
  158. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB);
  159. if (rv <= 1)
  160. goto err;
  161. } else {
  162. rv = 2;
  163. }
  164. if (rv == 2) {
  165. if (type == NULL) {
  166. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ASN1_R_CONTEXT_NOT_INITIALISED);
  167. goto err;
  168. }
  169. if (!OBJ_find_sigid_by_algs(&signid,
  170. EVP_MD_nid(type),
  171. pkey->ameth->pkey_id)) {
  172. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX,
  173. ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
  174. goto err;
  175. }
  176. if (pkey->ameth->pkey_flags & ASN1_PKEY_SIGPARAM_NULL)
  177. paramtype = V_ASN1_NULL;
  178. else
  179. paramtype = V_ASN1_UNDEF;
  180. if (algor1)
  181. X509_ALGOR_set0(algor1, OBJ_nid2obj(signid), paramtype, NULL);
  182. if (algor2)
  183. X509_ALGOR_set0(algor2, OBJ_nid2obj(signid), paramtype, NULL);
  184. }
  185. buf_len = ASN1_item_i2d(asn, &buf_in, it);
  186. if (buf_len <= 0) {
  187. outl = 0;
  188. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_INTERNAL_ERROR);
  189. goto err;
  190. }
  191. inl = buf_len;
  192. outll = outl = EVP_PKEY_size(pkey);
  193. buf_out = OPENSSL_malloc(outll);
  194. if (buf_in == NULL || buf_out == NULL) {
  195. outl = 0;
  196. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_MALLOC_FAILURE);
  197. goto err;
  198. }
  199. if (!EVP_DigestSign(ctx, buf_out, &outl, buf_in, inl)) {
  200. outl = 0;
  201. ASN1err(ASN1_F_ASN1_ITEM_SIGN_CTX, ERR_R_EVP_LIB);
  202. goto err;
  203. }
  204. OPENSSL_free(signature->data);
  205. signature->data = buf_out;
  206. buf_out = NULL;
  207. signature->length = outl;
  208. /*
  209. * In the interests of compatibility, I'll make sure that the bit string
  210. * has a 'not-used bits' value of 0
  211. */
  212. signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  213. signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  214. err:
  215. OPENSSL_clear_free((char *)buf_in, inl);
  216. OPENSSL_clear_free((char *)buf_out, outll);
  217. return outl;
  218. }