cmp_protect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2007-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2019
  4. * Copyright Siemens AG 2015-2019
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include "cmp_local.h"
  12. #include "crypto/asn1.h"
  13. /* explicit #includes not strictly needed since implied by the above: */
  14. #include <openssl/asn1t.h>
  15. #include <openssl/cmp.h>
  16. #include <openssl/crmf.h>
  17. #include <openssl/err.h>
  18. #include <openssl/x509.h>
  19. /*
  20. * This function is also used by the internal verify_PBMAC() in cmp_vfy.c.
  21. *
  22. * Calculate protection for given PKImessage according to
  23. * the algorithm and parameters in the message header's protectionAlg
  24. * using the credentials, library context, and property criteria in the ctx.
  25. *
  26. * returns ASN1_BIT_STRING representing the protection on success, else NULL
  27. */
  28. ASN1_BIT_STRING *ossl_cmp_calc_protection(const OSSL_CMP_CTX *ctx,
  29. const OSSL_CMP_MSG *msg)
  30. {
  31. ASN1_BIT_STRING *prot = NULL;
  32. OSSL_CMP_PROTECTEDPART prot_part;
  33. const ASN1_OBJECT *algorOID = NULL;
  34. const void *ppval = NULL;
  35. int pptype = 0;
  36. if (!ossl_assert(ctx != NULL && msg != NULL))
  37. return NULL;
  38. /* construct data to be signed */
  39. prot_part.header = msg->header;
  40. prot_part.body = msg->body;
  41. if (msg->header->protectionAlg == NULL) {
  42. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_ALGORITHM_ID);
  43. return NULL;
  44. }
  45. X509_ALGOR_get0(&algorOID, &pptype, &ppval, msg->header->protectionAlg);
  46. if (OBJ_obj2nid(algorOID) == NID_id_PasswordBasedMAC) {
  47. int len;
  48. size_t prot_part_der_len;
  49. unsigned char *prot_part_der = NULL;
  50. size_t sig_len;
  51. unsigned char *protection = NULL;
  52. OSSL_CRMF_PBMPARAMETER *pbm = NULL;
  53. ASN1_STRING *pbm_str = NULL;
  54. const unsigned char *pbm_str_uc = NULL;
  55. if (ctx->secretValue == NULL) {
  56. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PBM_SECRET);
  57. return NULL;
  58. }
  59. if (ppval == NULL) {
  60. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CALCULATING_PROTECTION);
  61. return NULL;
  62. }
  63. len = i2d_OSSL_CMP_PROTECTEDPART(&prot_part, &prot_part_der);
  64. if (len < 0 || prot_part_der == NULL) {
  65. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CALCULATING_PROTECTION);
  66. goto end;
  67. }
  68. prot_part_der_len = (size_t)len;
  69. pbm_str = (ASN1_STRING *)ppval;
  70. pbm_str_uc = pbm_str->data;
  71. pbm = d2i_OSSL_CRMF_PBMPARAMETER(NULL, &pbm_str_uc, pbm_str->length);
  72. if (pbm == NULL) {
  73. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_ALGORITHM_OID);
  74. goto end;
  75. }
  76. if (!OSSL_CRMF_pbm_new(ctx->libctx, ctx->propq,
  77. pbm, prot_part_der, prot_part_der_len,
  78. ctx->secretValue->data, ctx->secretValue->length,
  79. &protection, &sig_len))
  80. goto end;
  81. if ((prot = ASN1_BIT_STRING_new()) == NULL)
  82. goto end;
  83. /* OpenSSL defaults all bit strings to be encoded as ASN.1 NamedBitList */
  84. ossl_asn1_string_set_bits_left(prot, 0);
  85. if (!ASN1_BIT_STRING_set(prot, protection, sig_len)) {
  86. ASN1_BIT_STRING_free(prot);
  87. prot = NULL;
  88. }
  89. end:
  90. OSSL_CRMF_PBMPARAMETER_free(pbm);
  91. OPENSSL_free(protection);
  92. OPENSSL_free(prot_part_der);
  93. return prot;
  94. } else {
  95. int md_nid;
  96. const EVP_MD *md = NULL;
  97. if (ctx->pkey == NULL) {
  98. ERR_raise(ERR_LIB_CMP,
  99. CMP_R_MISSING_KEY_INPUT_FOR_CREATING_PROTECTION);
  100. return NULL;
  101. }
  102. if (!OBJ_find_sigid_algs(OBJ_obj2nid(algorOID), &md_nid, NULL)
  103. || (md = EVP_get_digestbynid(md_nid)) == NULL) {
  104. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_ALGORITHM_ID);
  105. return NULL;
  106. }
  107. if ((prot = ASN1_BIT_STRING_new()) == NULL)
  108. return NULL;
  109. if (ASN1_item_sign_ex(ASN1_ITEM_rptr(OSSL_CMP_PROTECTEDPART), NULL,
  110. NULL, prot, &prot_part, NULL, ctx->pkey, md,
  111. ctx->libctx, ctx->propq))
  112. return prot;
  113. ASN1_BIT_STRING_free(prot);
  114. return NULL;
  115. }
  116. }
  117. int ossl_cmp_msg_add_extraCerts(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  118. {
  119. if (!ossl_assert(ctx != NULL && msg != NULL))
  120. return 0;
  121. /* Add first ctx->cert and its chain if using signature-based protection */
  122. if (!ctx->unprotectedSend && ctx->secretValue == NULL
  123. && ctx->cert != NULL && ctx->pkey != NULL) {
  124. int prepend = X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP
  125. | X509_ADD_FLAG_PREPEND | X509_ADD_FLAG_NO_SS;
  126. /* if not yet done try to build chain using available untrusted certs */
  127. if (ctx->chain == NULL) {
  128. ossl_cmp_debug(ctx,
  129. "trying to build chain for own CMP signer cert");
  130. ctx->chain = X509_build_chain(ctx->cert, ctx->untrusted, NULL, 0,
  131. ctx->libctx, ctx->propq);
  132. if (ctx->chain != NULL) {
  133. ossl_cmp_debug(ctx,
  134. "success building chain for own CMP signer cert");
  135. } else {
  136. /* dump errors to avoid confusion when printing further ones */
  137. OSSL_CMP_CTX_print_errors(ctx);
  138. ossl_cmp_warn(ctx,
  139. "could not build chain for own CMP signer cert");
  140. }
  141. }
  142. if (ctx->chain != NULL) {
  143. if (!ossl_x509_add_certs_new(&msg->extraCerts, ctx->chain, prepend))
  144. return 0;
  145. } else {
  146. /* make sure that at least our own signer cert is included first */
  147. if (!ossl_x509_add_cert_new(&msg->extraCerts, ctx->cert, prepend))
  148. return 0;
  149. ossl_cmp_debug(ctx, "fallback: adding just own CMP signer cert");
  150. }
  151. }
  152. /* add any additional certificates from ctx->extraCertsOut */
  153. if (!ossl_x509_add_certs_new(&msg->extraCerts, ctx->extraCertsOut,
  154. X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
  155. return 0;
  156. /* in case extraCerts are empty list avoid empty ASN.1 sequence */
  157. if (sk_X509_num(msg->extraCerts) == 0) {
  158. sk_X509_free(msg->extraCerts);
  159. msg->extraCerts = NULL;
  160. }
  161. return 1;
  162. }
  163. /*
  164. * Create an X509_ALGOR structure for PasswordBasedMAC protection based on
  165. * the pbm settings in the context
  166. */
  167. static X509_ALGOR *pbmac_algor(const OSSL_CMP_CTX *ctx)
  168. {
  169. OSSL_CRMF_PBMPARAMETER *pbm = NULL;
  170. unsigned char *pbm_der = NULL;
  171. int pbm_der_len;
  172. ASN1_STRING *pbm_str = NULL;
  173. X509_ALGOR *alg = NULL;
  174. if (!ossl_assert(ctx != NULL))
  175. return NULL;
  176. pbm = OSSL_CRMF_pbmp_new(ctx->libctx, ctx->pbm_slen,
  177. EVP_MD_get_type(ctx->pbm_owf), ctx->pbm_itercnt,
  178. ctx->pbm_mac);
  179. pbm_str = ASN1_STRING_new();
  180. if (pbm == NULL || pbm_str == NULL)
  181. goto err;
  182. if ((pbm_der_len = i2d_OSSL_CRMF_PBMPARAMETER(pbm, &pbm_der)) < 0)
  183. goto err;
  184. if (!ASN1_STRING_set(pbm_str, pbm_der, pbm_der_len))
  185. goto err;
  186. alg = ossl_X509_ALGOR_from_nid(NID_id_PasswordBasedMAC,
  187. V_ASN1_SEQUENCE, pbm_str);
  188. err:
  189. if (alg == NULL)
  190. ASN1_STRING_free(pbm_str);
  191. OPENSSL_free(pbm_der);
  192. OSSL_CRMF_PBMPARAMETER_free(pbm);
  193. return alg;
  194. }
  195. static X509_ALGOR *sig_algor(const OSSL_CMP_CTX *ctx)
  196. {
  197. int nid = 0;
  198. if (!OBJ_find_sigid_by_algs(&nid, EVP_MD_get_type(ctx->digest),
  199. EVP_PKEY_get_id(ctx->pkey))) {
  200. ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_KEY_TYPE);
  201. return 0;
  202. }
  203. return ossl_X509_ALGOR_from_nid(nid, V_ASN1_UNDEF, NULL);
  204. }
  205. static int set_senderKID(const OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg,
  206. const ASN1_OCTET_STRING *id)
  207. {
  208. if (id == NULL)
  209. id = ctx->referenceValue; /* standard for PBM, fallback for sig-based */
  210. return id == NULL || ossl_cmp_hdr_set1_senderKID(msg->header, id);
  211. }
  212. int ossl_cmp_msg_protect(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  213. {
  214. if (!ossl_assert(ctx != NULL && msg != NULL))
  215. return 0;
  216. /*
  217. * For the case of re-protection remove pre-existing protection.
  218. */
  219. X509_ALGOR_free(msg->header->protectionAlg);
  220. msg->header->protectionAlg = NULL;
  221. ASN1_BIT_STRING_free(msg->protection);
  222. msg->protection = NULL;
  223. if (ctx->unprotectedSend) {
  224. if (!set_senderKID(ctx, msg, NULL))
  225. goto err;
  226. } else if (ctx->secretValue != NULL) {
  227. /* use PasswordBasedMac according to 5.1.3.1 if secretValue is given */
  228. if ((msg->header->protectionAlg = pbmac_algor(ctx)) == NULL)
  229. goto err;
  230. if (!set_senderKID(ctx, msg, NULL))
  231. goto err;
  232. /*
  233. * will add any additional certificates from ctx->extraCertsOut
  234. * while not needed to validate the protection certificate,
  235. * the option to do this might be handy for certain use cases
  236. */
  237. } else if (ctx->cert != NULL && ctx->pkey != NULL) {
  238. /* use MSG_SIG_ALG according to 5.1.3.3 if client cert and key given */
  239. /* make sure that key and certificate match */
  240. if (!X509_check_private_key(ctx->cert, ctx->pkey)) {
  241. ERR_raise(ERR_LIB_CMP, CMP_R_CERT_AND_KEY_DO_NOT_MATCH);
  242. goto err;
  243. }
  244. if ((msg->header->protectionAlg = sig_algor(ctx)) == NULL)
  245. goto err;
  246. /* set senderKID to keyIdentifier of the cert according to 5.1.1 */
  247. if (!set_senderKID(ctx, msg, X509_get0_subject_key_id(ctx->cert)))
  248. goto err;
  249. /*
  250. * will add ctx->cert followed, if possible, by its chain built
  251. * from ctx->untrusted, and then ctx->extraCertsOut
  252. */
  253. } else {
  254. ERR_raise(ERR_LIB_CMP,
  255. CMP_R_MISSING_KEY_INPUT_FOR_CREATING_PROTECTION);
  256. goto err;
  257. }
  258. if (!ctx->unprotectedSend
  259. && ((msg->protection = ossl_cmp_calc_protection(ctx, msg)) == NULL))
  260. goto err;
  261. /*
  262. * For signature-based protection add ctx->cert followed by its chain.
  263. * Finally add any additional certificates from ctx->extraCertsOut;
  264. * even if not needed to validate the protection
  265. * the option to do this might be handy for certain use cases.
  266. */
  267. if (!ossl_cmp_msg_add_extraCerts(ctx, msg))
  268. goto err;
  269. /*
  270. * As required by RFC 4210 section 5.1.1., if the sender name is not known
  271. * to the client it set to NULL-DN. In this case for identification at least
  272. * the senderKID must be set, where we took the referenceValue as fallback.
  273. */
  274. if (!(ossl_cmp_general_name_is_NULL_DN(msg->header->sender)
  275. && msg->header->senderKID == NULL))
  276. return 1;
  277. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_SENDER_IDENTIFICATION);
  278. err:
  279. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROTECTING_MESSAGE);
  280. return 0;
  281. }