a_sign.c 6.9 KB

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