cmp_protect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright 2007-2020 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. /* explicit #includes not strictly needed since implied by the above: */
  13. #include <openssl/asn1t.h>
  14. #include <openssl/cmp.h>
  15. #include <openssl/crmf.h>
  16. #include <openssl/err.h>
  17. #include <openssl/x509.h>
  18. DEFINE_STACK_OF(X509)
  19. /*
  20. * This function is also used for verification from cmp_vfy.
  21. *
  22. * Calculate protection for given PKImessage utilizing the given credentials
  23. * and the algorithm parameters set inside the message header's protectionAlg.
  24. *
  25. * secret or pkey must be set. Attempts doing PBMAC in case 'secret' is set
  26. * and else signature if 'pkey' is set - but will only
  27. * do the protection already marked in msg->header->protectionAlg.
  28. *
  29. * returns ptr to ASN1_BIT_STRING containing protection on success, else NULL
  30. */
  31. ASN1_BIT_STRING *ossl_cmp_calc_protection(const OSSL_CMP_MSG *msg,
  32. const ASN1_OCTET_STRING *secret,
  33. EVP_PKEY *pkey)
  34. {
  35. ASN1_BIT_STRING *prot = NULL;
  36. OSSL_CMP_PROTECTEDPART prot_part;
  37. const ASN1_OBJECT *algorOID = NULL;
  38. int len;
  39. size_t prot_part_der_len;
  40. unsigned char *prot_part_der = NULL;
  41. size_t sig_len;
  42. unsigned char *protection = NULL;
  43. const void *ppval = NULL;
  44. int pptype = 0;
  45. OSSL_CRMF_PBMPARAMETER *pbm = NULL;
  46. ASN1_STRING *pbm_str = NULL;
  47. const unsigned char *pbm_str_uc = NULL;
  48. EVP_MD_CTX *evp_ctx = NULL;
  49. int md_NID;
  50. const EVP_MD *md = NULL;
  51. if (!ossl_assert(msg != NULL))
  52. return NULL;
  53. /* construct data to be signed */
  54. prot_part.header = msg->header;
  55. prot_part.body = msg->body;
  56. len = i2d_OSSL_CMP_PROTECTEDPART(&prot_part, &prot_part_der);
  57. if (len < 0 || prot_part_der == NULL) {
  58. CMPerr(0, CMP_R_ERROR_CALCULATING_PROTECTION);
  59. goto end;
  60. }
  61. prot_part_der_len = (size_t) len;
  62. if (msg->header->protectionAlg == NULL) {
  63. CMPerr(0, CMP_R_UNKNOWN_ALGORITHM_ID);
  64. goto end;
  65. }
  66. X509_ALGOR_get0(&algorOID, &pptype, &ppval, msg->header->protectionAlg);
  67. if (secret != NULL) {
  68. if (ppval == NULL) {
  69. CMPerr(0, CMP_R_ERROR_CALCULATING_PROTECTION);
  70. goto end;
  71. }
  72. if (NID_id_PasswordBasedMAC != OBJ_obj2nid(algorOID)) {
  73. CMPerr(0, CMP_R_WRONG_ALGORITHM_OID);
  74. goto end;
  75. }
  76. pbm_str = (ASN1_STRING *)ppval;
  77. pbm_str_uc = pbm_str->data;
  78. pbm = d2i_OSSL_CRMF_PBMPARAMETER(NULL, &pbm_str_uc, pbm_str->length);
  79. if (pbm == NULL) {
  80. CMPerr(0, CMP_R_WRONG_ALGORITHM_OID);
  81. goto end;
  82. }
  83. if (!OSSL_CRMF_pbm_new(pbm, prot_part_der, prot_part_der_len,
  84. secret->data, secret->length,
  85. &protection, &sig_len))
  86. goto end;
  87. } else if (pkey != NULL) {
  88. /* TODO combine this with large parts of CRMF_poposigningkey_init() */
  89. /* EVP_DigestSignInit() checks that pkey type is correct for the alg */
  90. if (!OBJ_find_sigid_algs(OBJ_obj2nid(algorOID), &md_NID, NULL)
  91. || (md = EVP_get_digestbynid(md_NID)) == NULL
  92. || (evp_ctx = EVP_MD_CTX_new()) == NULL) {
  93. CMPerr(0, CMP_R_UNKNOWN_ALGORITHM_ID);
  94. goto end;
  95. }
  96. if (EVP_DigestSignInit(evp_ctx, NULL, md, NULL, pkey) <= 0
  97. || EVP_DigestSignUpdate(evp_ctx, prot_part_der,
  98. prot_part_der_len) <= 0
  99. || EVP_DigestSignFinal(evp_ctx, NULL, &sig_len) <= 0
  100. || (protection = OPENSSL_malloc(sig_len)) == NULL
  101. || EVP_DigestSignFinal(evp_ctx, protection, &sig_len) <= 0) {
  102. CMPerr(0, CMP_R_ERROR_CALCULATING_PROTECTION);
  103. goto end;
  104. }
  105. } else {
  106. CMPerr(0, CMP_R_INVALID_ARGS);
  107. goto end;
  108. }
  109. if ((prot = ASN1_BIT_STRING_new()) == NULL)
  110. goto end;
  111. /* OpenSSL defaults all bit strings to be encoded as ASN.1 NamedBitList */
  112. prot->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  113. prot->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  114. if (!ASN1_BIT_STRING_set(prot, protection, sig_len)) {
  115. ASN1_BIT_STRING_free(prot);
  116. prot = NULL;
  117. }
  118. end:
  119. OSSL_CRMF_PBMPARAMETER_free(pbm);
  120. EVP_MD_CTX_free(evp_ctx);
  121. OPENSSL_free(protection);
  122. OPENSSL_free(prot_part_der);
  123. return prot;
  124. }
  125. int ossl_cmp_msg_add_extraCerts(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  126. {
  127. if (!ossl_assert(ctx != NULL && msg != NULL))
  128. return 0;
  129. if (msg->extraCerts == NULL
  130. && (msg->extraCerts = sk_X509_new_null()) == NULL)
  131. return 0;
  132. if (ctx->cert != NULL && ctx->pkey != NULL) {
  133. /* make sure that our own cert is included in the first position */
  134. if (!ossl_cmp_sk_X509_add1_cert(msg->extraCerts, ctx->cert, 1, 1))
  135. return 0;
  136. /* if we have untrusted certs, try to add intermediate certs */
  137. if (ctx->untrusted_certs != NULL) {
  138. STACK_OF(X509) *chain =
  139. ossl_cmp_build_cert_chain(ctx->untrusted_certs, ctx->cert);
  140. int res = ossl_cmp_sk_X509_add1_certs(msg->extraCerts, chain,
  141. 1 /* no self-issued */,
  142. 1 /* no duplicates */, 0);
  143. sk_X509_pop_free(chain, X509_free);
  144. if (res == 0)
  145. return 0;
  146. }
  147. }
  148. /* add any additional certificates from ctx->extraCertsOut */
  149. if (!ossl_cmp_sk_X509_add1_certs(msg->extraCerts, ctx->extraCertsOut, 0,
  150. 1 /* no duplicates */, 0))
  151. return 0;
  152. /* if none was found avoid empty ASN.1 sequence */
  153. if (sk_X509_num(msg->extraCerts) == 0) {
  154. sk_X509_free(msg->extraCerts);
  155. msg->extraCerts = NULL;
  156. }
  157. return 1;
  158. }
  159. /*
  160. * Create an X509_ALGOR structure for PasswordBasedMAC protection based on
  161. * the pbm settings in the context
  162. * returns pointer to X509_ALGOR on success, NULL on error
  163. */
  164. static X509_ALGOR *create_pbmac_algor(OSSL_CMP_CTX *ctx)
  165. {
  166. X509_ALGOR *alg = NULL;
  167. OSSL_CRMF_PBMPARAMETER *pbm = NULL;
  168. unsigned char *pbm_der = NULL;
  169. int pbm_der_len;
  170. ASN1_STRING *pbm_str = NULL;
  171. if (!ossl_assert(ctx != NULL))
  172. return NULL;
  173. alg = X509_ALGOR_new();
  174. pbm = OSSL_CRMF_pbmp_new(ctx->pbm_slen, ctx->pbm_owf, ctx->pbm_itercnt,
  175. ctx->pbm_mac);
  176. pbm_str = ASN1_STRING_new();
  177. if (alg == NULL || pbm == NULL || pbm_str == NULL)
  178. goto err;
  179. if ((pbm_der_len = i2d_OSSL_CRMF_PBMPARAMETER(pbm, &pbm_der)) < 0)
  180. goto err;
  181. if (!ASN1_STRING_set(pbm_str, pbm_der, pbm_der_len))
  182. goto err;
  183. OPENSSL_free(pbm_der);
  184. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_id_PasswordBasedMAC),
  185. V_ASN1_SEQUENCE, pbm_str);
  186. OSSL_CRMF_PBMPARAMETER_free(pbm);
  187. return alg;
  188. err:
  189. ASN1_STRING_free(pbm_str);
  190. X509_ALGOR_free(alg);
  191. OPENSSL_free(pbm_der);
  192. OSSL_CRMF_PBMPARAMETER_free(pbm);
  193. return NULL;
  194. }
  195. int ossl_cmp_msg_protect(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  196. {
  197. if (!ossl_assert(ctx != NULL && msg != NULL))
  198. return 0;
  199. /*
  200. * For the case of re-protection remove pre-existing protection.
  201. * TODO: Consider also removing any pre-existing extraCerts.
  202. */
  203. X509_ALGOR_free(msg->header->protectionAlg);
  204. msg->header->protectionAlg = NULL;
  205. ASN1_BIT_STRING_free(msg->protection);
  206. msg->protection = NULL;
  207. if (ctx->unprotectedSend)
  208. return 1;
  209. /* use PasswordBasedMac according to 5.1.3.1 if secretValue is given */
  210. if (ctx->secretValue != NULL) {
  211. if ((msg->header->protectionAlg = create_pbmac_algor(ctx)) == NULL)
  212. goto err;
  213. if (ctx->referenceValue != NULL
  214. && !ossl_cmp_hdr_set1_senderKID(msg->header,
  215. ctx->referenceValue))
  216. goto err;
  217. } else if (ctx->cert != NULL && ctx->pkey != NULL) {
  218. /*
  219. * use MSG_SIG_ALG according to 5.1.3.3 if client Certificate and
  220. * private key is given
  221. */
  222. const ASN1_OCTET_STRING *subjKeyIDStr = NULL;
  223. int algNID = 0;
  224. ASN1_OBJECT *alg = NULL;
  225. /* make sure that key and certificate match */
  226. if (!X509_check_private_key(ctx->cert, ctx->pkey)) {
  227. CMPerr(0, CMP_R_CERT_AND_KEY_DO_NOT_MATCH);
  228. goto err;
  229. }
  230. if ((msg->header->protectionAlg = X509_ALGOR_new()) == NULL)
  231. goto err;
  232. if (!OBJ_find_sigid_by_algs(&algNID, ctx->digest,
  233. EVP_PKEY_id(ctx->pkey))) {
  234. CMPerr(0, CMP_R_UNSUPPORTED_KEY_TYPE);
  235. goto err;
  236. }
  237. if ((alg = OBJ_nid2obj(algNID)) == NULL)
  238. goto err;
  239. if (!X509_ALGOR_set0(msg->header->protectionAlg, alg,
  240. V_ASN1_UNDEF, NULL)) {
  241. ASN1_OBJECT_free(alg);
  242. goto err;
  243. }
  244. /*
  245. * set senderKID to keyIdentifier of the used certificate according
  246. * to section 5.1.1
  247. */
  248. subjKeyIDStr = X509_get0_subject_key_id(ctx->cert);
  249. if (subjKeyIDStr == NULL)
  250. subjKeyIDStr = ctx->referenceValue; /* fallback */
  251. if (subjKeyIDStr != NULL
  252. && !ossl_cmp_hdr_set1_senderKID(msg->header, subjKeyIDStr))
  253. goto err;
  254. } else {
  255. CMPerr(0, CMP_R_MISSING_KEY_INPUT_FOR_CREATING_PROTECTION);
  256. goto err;
  257. }
  258. if ((msg->protection =
  259. ossl_cmp_calc_protection(msg, ctx->secretValue, ctx->pkey)) == NULL)
  260. goto err;
  261. /*
  262. * If present, add ctx->cert followed by its chain as far as possible.
  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. CMPerr(0, CMP_R_MISSING_SENDER_IDENTIFICATION);
  277. else
  278. return 1;
  279. err:
  280. CMPerr(0, CMP_R_ERROR_PROTECTING_MESSAGE);
  281. return 0;
  282. }