cmp_hdr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright 2007-2019 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. /* CMP functions for PKIHeader handling */
  12. #include "cmp_local.h"
  13. #include <openssl/rand.h>
  14. /* explicit #includes not strictly needed since implied by the above: */
  15. #include <openssl/asn1t.h>
  16. #include <openssl/cmp.h>
  17. #include <openssl/err.h>
  18. int ossl_cmp_hdr_set_pvno(OSSL_CMP_PKIHEADER *hdr, int pvno)
  19. {
  20. if (!ossl_assert(hdr != NULL))
  21. return 0;
  22. return ASN1_INTEGER_set(hdr->pvno, pvno);
  23. }
  24. int ossl_cmp_hdr_get_pvno(const OSSL_CMP_PKIHEADER *hdr)
  25. {
  26. int64_t pvno;
  27. if (!ossl_assert(hdr != NULL))
  28. return -1;
  29. if (!ASN1_INTEGER_get_int64(&pvno, hdr->pvno) || pvno < 0 || pvno > INT_MAX)
  30. return -1;
  31. return (int)pvno;
  32. }
  33. ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_transactionID(const OSSL_CMP_PKIHEADER *hdr)
  34. {
  35. if (hdr == NULL) {
  36. CMPerr(0, CMP_R_NULL_ARGUMENT);
  37. return NULL;
  38. }
  39. return hdr->transactionID;
  40. }
  41. ASN1_OCTET_STRING *ossl_cmp_hdr_get0_senderNonce(const OSSL_CMP_PKIHEADER *hdr)
  42. {
  43. if (!ossl_assert(hdr != NULL))
  44. return NULL;
  45. return hdr->senderNonce;
  46. }
  47. ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_recipNonce(const OSSL_CMP_PKIHEADER *hdr)
  48. {
  49. if (hdr == NULL) {
  50. CMPerr(0, CMP_R_NULL_ARGUMENT);
  51. return NULL;
  52. }
  53. return hdr->recipNonce;
  54. }
  55. /* assign to *tgt a copy of src (which may be NULL to indicate an empty DN) */
  56. static int set1_general_name(GENERAL_NAME **tgt, const X509_NAME *src)
  57. {
  58. GENERAL_NAME *gen;
  59. if (!ossl_assert(tgt != NULL))
  60. return 0;
  61. if ((gen = GENERAL_NAME_new()) == NULL)
  62. goto err;
  63. gen->type = GEN_DIRNAME;
  64. if (src == NULL) { /* NULL-DN */
  65. if ((gen->d.directoryName = X509_NAME_new()) == NULL)
  66. goto err;
  67. } else if (!X509_NAME_set(&gen->d.directoryName, src)) {
  68. goto err;
  69. }
  70. GENERAL_NAME_free(*tgt);
  71. *tgt = gen;
  72. return 1;
  73. err:
  74. GENERAL_NAME_free(gen);
  75. return 0;
  76. }
  77. /*
  78. * Set the sender name in PKIHeader.
  79. * when nm is NULL, sender is set to an empty string
  80. * returns 1 on success, 0 on error
  81. */
  82. int ossl_cmp_hdr_set1_sender(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm)
  83. {
  84. if (!ossl_assert(hdr != NULL))
  85. return 0;
  86. return set1_general_name(&hdr->sender, nm);
  87. }
  88. int ossl_cmp_hdr_set1_recipient(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm)
  89. {
  90. if (!ossl_assert(hdr != NULL))
  91. return 0;
  92. return set1_general_name(&hdr->recipient, nm);
  93. }
  94. int ossl_cmp_hdr_update_messageTime(OSSL_CMP_PKIHEADER *hdr)
  95. {
  96. if (!ossl_assert(hdr != NULL))
  97. return 0;
  98. if (hdr->messageTime == NULL
  99. && (hdr->messageTime = ASN1_GENERALIZEDTIME_new()) == NULL)
  100. return 0;
  101. return ASN1_GENERALIZEDTIME_set(hdr->messageTime, time(NULL)) != NULL;
  102. }
  103. /* assign to *tgt a copy of src (or if NULL a random byte array of given len) */
  104. static int set1_aostr_else_random(ASN1_OCTET_STRING **tgt,
  105. const ASN1_OCTET_STRING *src, size_t len)
  106. {
  107. unsigned char *bytes = NULL;
  108. int res = 0;
  109. if (src == NULL) { /* generate a random value if src == NULL */
  110. if ((bytes = OPENSSL_malloc(len)) == NULL)
  111. goto err;
  112. if (RAND_bytes(bytes, len) <= 0) {
  113. CMPerr(0, CMP_R_FAILURE_OBTAINING_RANDOM);
  114. goto err;
  115. }
  116. res = ossl_cmp_asn1_octet_string_set1_bytes(tgt, bytes, len);
  117. } else {
  118. res = ossl_cmp_asn1_octet_string_set1(tgt, src);
  119. }
  120. err:
  121. OPENSSL_free(bytes);
  122. return res;
  123. }
  124. int ossl_cmp_hdr_set1_senderKID(OSSL_CMP_PKIHEADER *hdr,
  125. const ASN1_OCTET_STRING *senderKID)
  126. {
  127. if (!ossl_assert(hdr != NULL))
  128. return 0;
  129. return ossl_cmp_asn1_octet_string_set1(&hdr->senderKID, senderKID);
  130. }
  131. /* push the given text string to the given PKIFREETEXT ft */
  132. int ossl_cmp_pkifreetext_push_str(OSSL_CMP_PKIFREETEXT *ft, const char *text)
  133. {
  134. ASN1_UTF8STRING *utf8string;
  135. if (!ossl_assert(ft != NULL && text != NULL))
  136. return 0;
  137. if ((utf8string = ASN1_UTF8STRING_new()) == NULL)
  138. return 0;
  139. if (!ASN1_STRING_set(utf8string, text, -1))
  140. goto err;
  141. if (!sk_ASN1_UTF8STRING_push(ft, utf8string))
  142. goto err;
  143. return 1;
  144. err:
  145. ASN1_UTF8STRING_free(utf8string);
  146. return 0;
  147. }
  148. int ossl_cmp_hdr_push0_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text)
  149. {
  150. if (!ossl_assert(hdr != NULL && text != NULL))
  151. return 0;
  152. if (hdr->freeText == NULL
  153. && (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL)
  154. return 0;
  155. return sk_ASN1_UTF8STRING_push(hdr->freeText, text);
  156. }
  157. int ossl_cmp_hdr_push1_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text)
  158. {
  159. if (!ossl_assert(hdr != NULL && text != NULL))
  160. return 0;
  161. if (hdr->freeText == NULL
  162. && (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL)
  163. return 0;
  164. return ossl_cmp_pkifreetext_push_str(hdr->freeText, (char *)text->data);
  165. }
  166. int ossl_cmp_hdr_generalInfo_push0_item(OSSL_CMP_PKIHEADER *hdr,
  167. OSSL_CMP_ITAV *itav)
  168. {
  169. if (!ossl_assert(hdr != NULL && itav != NULL))
  170. return 0;
  171. return OSSL_CMP_ITAV_push0_stack_item(&hdr->generalInfo, itav);
  172. }
  173. int ossl_cmp_hdr_generalInfo_push1_items(OSSL_CMP_PKIHEADER *hdr,
  174. STACK_OF(OSSL_CMP_ITAV) *itavs)
  175. {
  176. int i;
  177. OSSL_CMP_ITAV *itav;
  178. if (!ossl_assert(hdr != NULL))
  179. return 0;
  180. for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
  181. itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i));
  182. if (itav == NULL)
  183. return 0;
  184. if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav)) {
  185. OSSL_CMP_ITAV_free(itav);
  186. return 0;
  187. }
  188. }
  189. return 1;
  190. }
  191. int ossl_cmp_hdr_set_implicitConfirm(OSSL_CMP_PKIHEADER *hdr)
  192. {
  193. OSSL_CMP_ITAV *itav;
  194. ASN1_TYPE *asn1null;
  195. if (!ossl_assert(hdr != NULL))
  196. return 0;
  197. asn1null = (ASN1_TYPE *)ASN1_NULL_new();
  198. if (asn1null == NULL)
  199. return 0;
  200. if ((itav = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm),
  201. asn1null)) == NULL)
  202. goto err;
  203. if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav))
  204. goto err;
  205. return 1;
  206. err:
  207. ASN1_TYPE_free(asn1null);
  208. OSSL_CMP_ITAV_free(itav);
  209. return 0;
  210. }
  211. /* return 1 if implicitConfirm in the generalInfo field of the header is set */
  212. int ossl_cmp_hdr_check_implicitConfirm(const OSSL_CMP_PKIHEADER *hdr)
  213. {
  214. int itavCount;
  215. int i;
  216. OSSL_CMP_ITAV *itav;
  217. if (!ossl_assert(hdr != NULL))
  218. return 0;
  219. itavCount = sk_OSSL_CMP_ITAV_num(hdr->generalInfo);
  220. for (i = 0; i < itavCount; i++) {
  221. itav = sk_OSSL_CMP_ITAV_value(hdr->generalInfo, i);
  222. if (itav != NULL
  223. && OBJ_obj2nid(itav->infoType) == NID_id_it_implicitConfirm)
  224. return 1;
  225. }
  226. return 0;
  227. }
  228. /* fill in all fields of the hdr according to the info given in ctx */
  229. int ossl_cmp_hdr_init(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr)
  230. {
  231. X509_NAME *sender;
  232. X509_NAME *rcp = NULL;
  233. if (!ossl_assert(ctx != NULL && hdr != NULL))
  234. return 0;
  235. /* set the CMP version */
  236. if (!ossl_cmp_hdr_set_pvno(hdr, OSSL_CMP_PVNO))
  237. return 0;
  238. sender = ctx->clCert != NULL ?
  239. X509_get_subject_name(ctx->clCert) : ctx->subjectName;
  240. /*
  241. * The sender name is copied from the subject of the client cert, if any,
  242. * or else from the the subject name provided for certification requests.
  243. * As required by RFC 4210 section 5.1.1., if the sender name is not known
  244. * to the client it set to NULL-DN. In this case for identification at least
  245. * the senderKID must be set, which we take from any referenceValue given.
  246. */
  247. if (sender == NULL && ctx->referenceValue == NULL) {
  248. CMPerr(0, CMP_R_MISSING_SENDER_IDENTIFICATION);
  249. return 0;
  250. }
  251. if (!ossl_cmp_hdr_set1_sender(hdr, sender))
  252. return 0;
  253. /* determine recipient entry in PKIHeader */
  254. if (ctx->srvCert != NULL) {
  255. rcp = X509_get_subject_name(ctx->srvCert);
  256. /* set also as expected_sender of responses unless set explicitly */
  257. if (ctx->expected_sender == NULL && rcp != NULL
  258. && !OSSL_CMP_CTX_set1_expected_sender(ctx, rcp))
  259. return 0;
  260. } else if (ctx->recipient != NULL) {
  261. rcp = ctx->recipient;
  262. } else if (ctx->issuer != NULL) {
  263. rcp = ctx->issuer;
  264. } else if (ctx->oldCert != NULL) {
  265. rcp = X509_get_issuer_name(ctx->oldCert);
  266. } else if (ctx->clCert != NULL) {
  267. rcp = X509_get_issuer_name(ctx->clCert);
  268. }
  269. if (!ossl_cmp_hdr_set1_recipient(hdr, rcp))
  270. return 0;
  271. /* set current time as message time */
  272. if (!ossl_cmp_hdr_update_messageTime(hdr))
  273. return 0;
  274. if (ctx->recipNonce != NULL
  275. && !ossl_cmp_asn1_octet_string_set1(&hdr->recipNonce,
  276. ctx->recipNonce))
  277. return 0;
  278. /*
  279. * set ctx->transactionID in CMP header
  280. * if ctx->transactionID is NULL, a random one is created with 128 bit
  281. * according to section 5.1.1:
  282. *
  283. * It is RECOMMENDED that the clients fill the transactionID field with
  284. * 128 bits of (pseudo-) random data for the start of a transaction to
  285. * reduce the probability of having the transactionID in use at the server.
  286. */
  287. if (ctx->transactionID == NULL
  288. && !set1_aostr_else_random(&ctx->transactionID, NULL,
  289. OSSL_CMP_TRANSACTIONID_LENGTH))
  290. return 0;
  291. if (!ossl_cmp_asn1_octet_string_set1(&hdr->transactionID,
  292. ctx->transactionID))
  293. return 0;
  294. /*-
  295. * set random senderNonce
  296. * according to section 5.1.1:
  297. *
  298. * senderNonce present
  299. * -- 128 (pseudo-)random bits
  300. * The senderNonce and recipNonce fields protect the PKIMessage against
  301. * replay attacks. The senderNonce will typically be 128 bits of
  302. * (pseudo-) random data generated by the sender, whereas the recipNonce
  303. * is copied from the senderNonce of the previous message in the
  304. * transaction.
  305. */
  306. if (!set1_aostr_else_random(&hdr->senderNonce, NULL,
  307. OSSL_CMP_SENDERNONCE_LENGTH))
  308. return 0;
  309. /* store senderNonce - for cmp with recipNonce in next outgoing msg */
  310. if (!OSSL_CMP_CTX_set1_senderNonce(ctx, hdr->senderNonce))
  311. return 0;
  312. /*-
  313. * freeText [7] PKIFreeText OPTIONAL,
  314. * -- this may be used to indicate context-specific instructions
  315. * -- (this field is intended for human consumption)
  316. */
  317. if (ctx->freeText != NULL
  318. && !ossl_cmp_hdr_push1_freeText(hdr, ctx->freeText))
  319. return 0;
  320. return 1;
  321. }