cms_rsa.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 2006-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 <assert.h>
  10. #include <openssl/cms.h>
  11. #include <openssl/err.h>
  12. #include "crypto/asn1.h"
  13. #include "crypto/rsa.h"
  14. #include "cms_local.h"
  15. static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
  16. {
  17. RSA_OAEP_PARAMS *oaep;
  18. oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
  19. alg->parameter);
  20. if (oaep == NULL)
  21. return NULL;
  22. if (oaep->maskGenFunc != NULL) {
  23. oaep->maskHash = x509_algor_mgf1_decode(oaep->maskGenFunc);
  24. if (oaep->maskHash == NULL) {
  25. RSA_OAEP_PARAMS_free(oaep);
  26. return NULL;
  27. }
  28. }
  29. return oaep;
  30. }
  31. static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
  32. {
  33. EVP_PKEY_CTX *pkctx;
  34. X509_ALGOR *cmsalg;
  35. int nid;
  36. int rv = -1;
  37. unsigned char *label = NULL;
  38. int labellen = 0;
  39. const EVP_MD *mgf1md = NULL, *md = NULL;
  40. RSA_OAEP_PARAMS *oaep;
  41. pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  42. if (pkctx == NULL)
  43. return 0;
  44. if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
  45. return -1;
  46. nid = OBJ_obj2nid(cmsalg->algorithm);
  47. if (nid == NID_rsaEncryption)
  48. return 1;
  49. if (nid != NID_rsaesOaep) {
  50. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_ENCRYPTION_TYPE);
  51. return -1;
  52. }
  53. /* Decode OAEP parameters */
  54. oaep = rsa_oaep_decode(cmsalg);
  55. if (oaep == NULL) {
  56. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_OAEP_PARAMETERS);
  57. goto err;
  58. }
  59. mgf1md = x509_algor_get_md(oaep->maskHash);
  60. if (mgf1md == NULL)
  61. goto err;
  62. md = x509_algor_get_md(oaep->hashFunc);
  63. if (md == NULL)
  64. goto err;
  65. if (oaep->pSourceFunc != NULL) {
  66. X509_ALGOR *plab = oaep->pSourceFunc;
  67. if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
  68. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_LABEL_SOURCE);
  69. goto err;
  70. }
  71. if (plab->parameter->type != V_ASN1_OCTET_STRING) {
  72. ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_LABEL);
  73. goto err;
  74. }
  75. label = plab->parameter->value.octet_string->data;
  76. /* Stop label being freed when OAEP parameters are freed */
  77. plab->parameter->value.octet_string->data = NULL;
  78. labellen = plab->parameter->value.octet_string->length;
  79. }
  80. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
  81. goto err;
  82. if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
  83. goto err;
  84. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  85. goto err;
  86. if (label != NULL
  87. && EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
  88. goto err;
  89. /* Carry on */
  90. rv = 1;
  91. err:
  92. RSA_OAEP_PARAMS_free(oaep);
  93. return rv;
  94. }
  95. static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
  96. {
  97. const EVP_MD *md, *mgf1md;
  98. RSA_OAEP_PARAMS *oaep = NULL;
  99. ASN1_STRING *os = NULL;
  100. X509_ALGOR *alg;
  101. EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  102. int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
  103. unsigned char *label;
  104. if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
  105. return 0;
  106. if (pkctx != NULL) {
  107. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  108. return 0;
  109. }
  110. if (pad_mode == RSA_PKCS1_PADDING) {
  111. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  112. return 1;
  113. }
  114. /* Not supported */
  115. if (pad_mode != RSA_PKCS1_OAEP_PADDING)
  116. return 0;
  117. if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
  118. goto err;
  119. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  120. goto err;
  121. labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
  122. if (labellen < 0)
  123. goto err;
  124. oaep = RSA_OAEP_PARAMS_new();
  125. if (oaep == NULL)
  126. goto err;
  127. if (!x509_algor_new_from_md(&oaep->hashFunc, md))
  128. goto err;
  129. if (!x509_algor_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
  130. goto err;
  131. if (labellen > 0) {
  132. ASN1_OCTET_STRING *los;
  133. oaep->pSourceFunc = X509_ALGOR_new();
  134. if (oaep->pSourceFunc == NULL)
  135. goto err;
  136. los = ASN1_OCTET_STRING_new();
  137. if (los == NULL)
  138. goto err;
  139. if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
  140. ASN1_OCTET_STRING_free(los);
  141. goto err;
  142. }
  143. X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
  144. V_ASN1_OCTET_STRING, los);
  145. }
  146. /* create string with pss parameter encoding. */
  147. if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
  148. goto err;
  149. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
  150. os = NULL;
  151. rv = 1;
  152. err:
  153. RSA_OAEP_PARAMS_free(oaep);
  154. ASN1_STRING_free(os);
  155. return rv;
  156. }
  157. int cms_rsa_envelope(CMS_RecipientInfo *ri, int decrypt)
  158. {
  159. assert(decrypt == 0 || decrypt == 1);
  160. if (decrypt == 1)
  161. return rsa_cms_decrypt(ri);
  162. if (decrypt == 0)
  163. return rsa_cms_encrypt(ri);
  164. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  165. return 0;
  166. }
  167. static int rsa_cms_sign(CMS_SignerInfo *si)
  168. {
  169. int pad_mode = RSA_PKCS1_PADDING;
  170. X509_ALGOR *alg;
  171. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  172. ASN1_STRING *os = NULL;
  173. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  174. if (pkctx != NULL) {
  175. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  176. return 0;
  177. }
  178. if (pad_mode == RSA_PKCS1_PADDING) {
  179. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  180. return 1;
  181. }
  182. /* We don't support it */
  183. if (pad_mode != RSA_PKCS1_PSS_PADDING)
  184. return 0;
  185. os = ossl_rsa_ctx_to_pss_string(pkctx);
  186. if (os == NULL)
  187. return 0;
  188. X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
  189. return 1;
  190. }
  191. static int rsa_cms_verify(CMS_SignerInfo *si)
  192. {
  193. int nid, nid2;
  194. X509_ALGOR *alg;
  195. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  196. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pkctx);
  197. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  198. nid = OBJ_obj2nid(alg->algorithm);
  199. if (nid == EVP_PKEY_RSA_PSS)
  200. return ossl_rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
  201. /* Only PSS allowed for PSS keys */
  202. if (EVP_PKEY_is_a(pkey, "RSA-PSS")) {
  203. ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  204. return 0;
  205. }
  206. if (nid == NID_rsaEncryption)
  207. return 1;
  208. /* Workaround for some implementation that use a signature OID */
  209. if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
  210. if (nid2 == NID_rsaEncryption)
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. int cms_rsa_sign(CMS_SignerInfo *si, int verify)
  216. {
  217. assert(verify == 0 || verify == 1);
  218. if (verify == 1)
  219. return rsa_cms_verify(si);
  220. if (verify == 0)
  221. return rsa_cms_sign(si);
  222. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  223. return 0;
  224. }