cms_rsa.c 7.9 KB

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