2
0

cms_rsa.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. OPENSSL_free(label);
  91. goto err;
  92. }
  93. /* Carry on */
  94. rv = 1;
  95. err:
  96. RSA_OAEP_PARAMS_free(oaep);
  97. return rv;
  98. }
  99. static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
  100. {
  101. const EVP_MD *md, *mgf1md;
  102. RSA_OAEP_PARAMS *oaep = NULL;
  103. ASN1_STRING *os = NULL;
  104. ASN1_OCTET_STRING *los = NULL;
  105. X509_ALGOR *alg;
  106. EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  107. int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
  108. unsigned char *label;
  109. if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
  110. return 0;
  111. if (pkctx != NULL) {
  112. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  113. return 0;
  114. }
  115. if (pad_mode == RSA_PKCS1_PADDING)
  116. return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
  117. V_ASN1_NULL, NULL);
  118. /* Not supported */
  119. if (pad_mode != RSA_PKCS1_OAEP_PADDING)
  120. return 0;
  121. if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
  122. goto err;
  123. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  124. goto err;
  125. labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
  126. if (labellen < 0)
  127. goto err;
  128. oaep = RSA_OAEP_PARAMS_new();
  129. if (oaep == NULL)
  130. goto err;
  131. if (!ossl_x509_algor_new_from_md(&oaep->hashFunc, md))
  132. goto err;
  133. if (!ossl_x509_algor_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
  134. goto err;
  135. if (labellen > 0) {
  136. los = ASN1_OCTET_STRING_new();
  137. if (los == NULL)
  138. goto err;
  139. if (!ASN1_OCTET_STRING_set(los, label, labellen))
  140. goto err;
  141. oaep->pSourceFunc = ossl_X509_ALGOR_from_nid(NID_pSpecified,
  142. V_ASN1_OCTET_STRING, los);
  143. if (oaep->pSourceFunc == NULL)
  144. goto err;
  145. los = NULL;
  146. }
  147. /* create string with oaep parameter encoding. */
  148. if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
  149. goto err;
  150. if (!X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os))
  151. goto err;
  152. os = NULL;
  153. rv = 1;
  154. err:
  155. RSA_OAEP_PARAMS_free(oaep);
  156. ASN1_STRING_free(os);
  157. ASN1_OCTET_STRING_free(los);
  158. return rv;
  159. }
  160. int ossl_cms_rsa_envelope(CMS_RecipientInfo *ri, int decrypt)
  161. {
  162. assert(decrypt == 0 || decrypt == 1);
  163. if (decrypt == 1)
  164. return rsa_cms_decrypt(ri);
  165. if (decrypt == 0)
  166. return rsa_cms_encrypt(ri);
  167. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  168. return 0;
  169. }
  170. static int rsa_cms_sign(CMS_SignerInfo *si)
  171. {
  172. int pad_mode = RSA_PKCS1_PADDING;
  173. X509_ALGOR *alg;
  174. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  175. unsigned char aid[128];
  176. const unsigned char *pp = aid;
  177. size_t aid_len = 0;
  178. OSSL_PARAM params[2];
  179. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  180. if (pkctx != NULL) {
  181. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  182. return 0;
  183. }
  184. if (pad_mode == RSA_PKCS1_PADDING)
  185. return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
  186. V_ASN1_NULL, NULL);
  187. /* We don't support it */
  188. if (pad_mode != RSA_PKCS1_PSS_PADDING)
  189. return 0;
  190. if (evp_pkey_ctx_is_legacy(pkctx)) {
  191. /* No provider -> we cannot query it for algorithm ID. */
  192. ASN1_STRING *os = NULL;
  193. os = ossl_rsa_ctx_to_pss_string(pkctx);
  194. if (os == NULL)
  195. return 0;
  196. return X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
  197. }
  198. params[0] = OSSL_PARAM_construct_octet_string(
  199. OSSL_SIGNATURE_PARAM_ALGORITHM_ID, aid, sizeof(aid));
  200. params[1] = OSSL_PARAM_construct_end();
  201. if (EVP_PKEY_CTX_get_params(pkctx, params) <= 0)
  202. return 0;
  203. if ((aid_len = params[0].return_size) == 0)
  204. return 0;
  205. if (d2i_X509_ALGOR(&alg, &pp, aid_len) == NULL)
  206. return 0;
  207. return 1;
  208. }
  209. static int rsa_cms_verify(CMS_SignerInfo *si)
  210. {
  211. int nid, nid2;
  212. X509_ALGOR *alg;
  213. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  214. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pkctx);
  215. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  216. nid = OBJ_obj2nid(alg->algorithm);
  217. if (nid == EVP_PKEY_RSA_PSS)
  218. return ossl_rsa_pss_to_ctx(NULL, pkctx, alg, NULL) > 0;
  219. /* Only PSS allowed for PSS keys */
  220. if (EVP_PKEY_is_a(pkey, "RSA-PSS")) {
  221. ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  222. return 0;
  223. }
  224. if (nid == NID_rsaEncryption)
  225. return 1;
  226. /* Workaround for some implementation that use a signature OID */
  227. if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
  228. if (nid2 == NID_rsaEncryption)
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. int ossl_cms_rsa_sign(CMS_SignerInfo *si, int verify)
  234. {
  235. assert(verify == 0 || verify == 1);
  236. if (verify == 1)
  237. return rsa_cms_verify(si);
  238. if (verify == 0)
  239. return rsa_cms_sign(si);
  240. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  241. return 0;
  242. }