cmp_hdr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. /* 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. int ossl_cmp_hdr_get_protection_nid(const OSSL_CMP_PKIHEADER *hdr)
  34. {
  35. if (!ossl_assert(hdr != NULL)
  36. || hdr->protectionAlg == NULL)
  37. return NID_undef;
  38. return OBJ_obj2nid(hdr->protectionAlg->algorithm);
  39. }
  40. ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_transactionID(const
  41. OSSL_CMP_PKIHEADER *hdr)
  42. {
  43. if (hdr == NULL) {
  44. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  45. return NULL;
  46. }
  47. return hdr->transactionID;
  48. }
  49. ASN1_OCTET_STRING *ossl_cmp_hdr_get0_senderNonce(const OSSL_CMP_PKIHEADER *hdr)
  50. {
  51. if (!ossl_assert(hdr != NULL))
  52. return NULL;
  53. return hdr->senderNonce;
  54. }
  55. ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_recipNonce(const OSSL_CMP_PKIHEADER *hdr)
  56. {
  57. if (hdr == NULL) {
  58. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  59. return NULL;
  60. }
  61. return hdr->recipNonce;
  62. }
  63. /* a NULL-DN as an empty sequence of RDNs */
  64. int ossl_cmp_general_name_is_NULL_DN(GENERAL_NAME *name)
  65. {
  66. return name == NULL
  67. || (name->type == GEN_DIRNAME && IS_NULL_DN(name->d.directoryName));
  68. }
  69. /* assign to *tgt a copy of src (which may be NULL to indicate an empty DN) */
  70. static int set1_general_name(GENERAL_NAME **tgt, const X509_NAME *src)
  71. {
  72. GENERAL_NAME *name;
  73. if (!ossl_assert(tgt != NULL))
  74. return 0;
  75. if ((name = GENERAL_NAME_new()) == NULL)
  76. goto err;
  77. name->type = GEN_DIRNAME;
  78. if (src == NULL) { /* NULL-DN */
  79. if ((name->d.directoryName = X509_NAME_new()) == NULL)
  80. goto err;
  81. } else if (!X509_NAME_set(&name->d.directoryName, src)) {
  82. goto err;
  83. }
  84. GENERAL_NAME_free(*tgt);
  85. *tgt = name;
  86. return 1;
  87. err:
  88. GENERAL_NAME_free(name);
  89. return 0;
  90. }
  91. /*
  92. * Set the sender name in PKIHeader.
  93. * when nm is NULL, sender is set to an empty string
  94. * returns 1 on success, 0 on error
  95. */
  96. int ossl_cmp_hdr_set1_sender(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm)
  97. {
  98. if (!ossl_assert(hdr != NULL))
  99. return 0;
  100. return set1_general_name(&hdr->sender, nm);
  101. }
  102. int ossl_cmp_hdr_set1_recipient(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm)
  103. {
  104. if (!ossl_assert(hdr != NULL))
  105. return 0;
  106. return set1_general_name(&hdr->recipient, nm);
  107. }
  108. int ossl_cmp_hdr_update_messageTime(OSSL_CMP_PKIHEADER *hdr)
  109. {
  110. if (!ossl_assert(hdr != NULL))
  111. return 0;
  112. if (hdr->messageTime == NULL
  113. && (hdr->messageTime = ASN1_GENERALIZEDTIME_new()) == NULL)
  114. return 0;
  115. return ASN1_GENERALIZEDTIME_set(hdr->messageTime, time(NULL)) != NULL;
  116. }
  117. /* assign to *tgt a random byte array of given length */
  118. static int set_random(ASN1_OCTET_STRING **tgt, OSSL_CMP_CTX *ctx, size_t len)
  119. {
  120. unsigned char *bytes = OPENSSL_malloc(len);
  121. int res = 0;
  122. if (bytes == NULL || RAND_bytes_ex(ctx->libctx, bytes, len, 0) <= 0)
  123. ERR_raise(ERR_LIB_CMP, CMP_R_FAILURE_OBTAINING_RANDOM);
  124. else
  125. res = ossl_cmp_asn1_octet_string_set1_bytes(tgt, bytes, len);
  126. OPENSSL_free(bytes);
  127. return res;
  128. }
  129. int ossl_cmp_hdr_set1_senderKID(OSSL_CMP_PKIHEADER *hdr,
  130. const ASN1_OCTET_STRING *senderKID)
  131. {
  132. if (!ossl_assert(hdr != NULL))
  133. return 0;
  134. return ossl_cmp_asn1_octet_string_set1(&hdr->senderKID, senderKID);
  135. }
  136. /* push the given text string to the given PKIFREETEXT ft */
  137. int ossl_cmp_hdr_push0_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text)
  138. {
  139. if (!ossl_assert(hdr != NULL && text != NULL))
  140. return 0;
  141. if (hdr->freeText == NULL
  142. && (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL)
  143. return 0;
  144. return sk_ASN1_UTF8STRING_push(hdr->freeText, text);
  145. }
  146. int ossl_cmp_hdr_push1_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text)
  147. {
  148. if (!ossl_assert(hdr != NULL && text != NULL))
  149. return 0;
  150. if (hdr->freeText == NULL
  151. && (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL)
  152. return 0;
  153. return
  154. ossl_cmp_sk_ASN1_UTF8STRING_push_str(hdr->freeText, (char *)text->data,
  155. text->length);
  156. }
  157. int ossl_cmp_hdr_generalInfo_push0_item(OSSL_CMP_PKIHEADER *hdr,
  158. OSSL_CMP_ITAV *itav)
  159. {
  160. if (!ossl_assert(hdr != NULL && itav != NULL))
  161. return 0;
  162. return OSSL_CMP_ITAV_push0_stack_item(&hdr->generalInfo, itav);
  163. }
  164. int ossl_cmp_hdr_generalInfo_push1_items(OSSL_CMP_PKIHEADER *hdr,
  165. const STACK_OF(OSSL_CMP_ITAV) *itavs)
  166. {
  167. int i;
  168. OSSL_CMP_ITAV *itav;
  169. if (!ossl_assert(hdr != NULL))
  170. return 0;
  171. for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) {
  172. itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i));
  173. if (itav == NULL)
  174. return 0;
  175. if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav)) {
  176. OSSL_CMP_ITAV_free(itav);
  177. return 0;
  178. }
  179. }
  180. return 1;
  181. }
  182. int ossl_cmp_hdr_set_implicitConfirm(OSSL_CMP_PKIHEADER *hdr)
  183. {
  184. OSSL_CMP_ITAV *itav;
  185. ASN1_TYPE *asn1null;
  186. if (!ossl_assert(hdr != NULL))
  187. return 0;
  188. asn1null = (ASN1_TYPE *)ASN1_NULL_new();
  189. if (asn1null == NULL)
  190. return 0;
  191. if ((itav = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm),
  192. asn1null)) == NULL)
  193. goto err;
  194. if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav))
  195. goto err;
  196. return 1;
  197. err:
  198. ASN1_TYPE_free(asn1null);
  199. OSSL_CMP_ITAV_free(itav);
  200. return 0;
  201. }
  202. /* return 1 if implicitConfirm in the generalInfo field of the header is set */
  203. int ossl_cmp_hdr_has_implicitConfirm(const OSSL_CMP_PKIHEADER *hdr)
  204. {
  205. int itavCount;
  206. int i;
  207. OSSL_CMP_ITAV *itav;
  208. if (!ossl_assert(hdr != NULL))
  209. return 0;
  210. itavCount = sk_OSSL_CMP_ITAV_num(hdr->generalInfo);
  211. for (i = 0; i < itavCount; i++) {
  212. itav = sk_OSSL_CMP_ITAV_value(hdr->generalInfo, i);
  213. if (itav != NULL
  214. && OBJ_obj2nid(itav->infoType) == NID_id_it_implicitConfirm)
  215. return 1;
  216. }
  217. return 0;
  218. }
  219. /*
  220. * set ctx->transactionID in CMP header
  221. * if ctx->transactionID is NULL, a random one is created with 128 bit
  222. * according to section 5.1.1:
  223. *
  224. * It is RECOMMENDED that the clients fill the transactionID field with
  225. * 128 bits of (pseudo-) random data for the start of a transaction to
  226. * reduce the probability of having the transactionID in use at the server.
  227. */
  228. int ossl_cmp_hdr_set_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr)
  229. {
  230. if (ctx->transactionID == NULL) {
  231. char *tid;
  232. if (!set_random(&ctx->transactionID, ctx,
  233. OSSL_CMP_TRANSACTIONID_LENGTH))
  234. return 0;
  235. tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID);
  236. if (tid != NULL)
  237. ossl_cmp_log1(DEBUG, ctx,
  238. "Starting new transaction with ID=%s", tid);
  239. OPENSSL_free(tid);
  240. }
  241. return ossl_cmp_asn1_octet_string_set1(&hdr->transactionID,
  242. ctx->transactionID);
  243. }
  244. /* fill in all fields of the hdr according to the info given in ctx */
  245. int ossl_cmp_hdr_init(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr)
  246. {
  247. const X509_NAME *sender;
  248. const X509_NAME *rcp = NULL;
  249. if (!ossl_assert(ctx != NULL && hdr != NULL))
  250. return 0;
  251. /* set the CMP version */
  252. if (!ossl_cmp_hdr_set_pvno(hdr, OSSL_CMP_PVNO))
  253. return 0;
  254. /*
  255. * If no protection cert nor oldCert nor CSR nor subject is given,
  256. * sender name is not known to the client and thus set to NULL-DN
  257. */
  258. sender = ctx->cert != NULL ? X509_get_subject_name(ctx->cert) :
  259. ctx->oldCert != NULL ? X509_get_subject_name(ctx->oldCert) :
  260. ctx->p10CSR != NULL ? X509_REQ_get_subject_name(ctx->p10CSR) :
  261. ctx->subjectName;
  262. if (!ossl_cmp_hdr_set1_sender(hdr, sender))
  263. return 0;
  264. /* determine recipient entry in PKIHeader */
  265. if (ctx->recipient != NULL)
  266. rcp = ctx->recipient;
  267. else if (ctx->srvCert != NULL)
  268. rcp = X509_get_subject_name(ctx->srvCert);
  269. else if (ctx->issuer != NULL)
  270. rcp = ctx->issuer;
  271. else if (ctx->oldCert != NULL)
  272. rcp = X509_get_issuer_name(ctx->oldCert);
  273. else if (ctx->cert != NULL)
  274. rcp = X509_get_issuer_name(ctx->cert);
  275. if (!ossl_cmp_hdr_set1_recipient(hdr, rcp))
  276. return 0;
  277. /* set current time as message time */
  278. if (!ossl_cmp_hdr_update_messageTime(hdr))
  279. return 0;
  280. if (ctx->recipNonce != NULL
  281. && !ossl_cmp_asn1_octet_string_set1(&hdr->recipNonce,
  282. ctx->recipNonce))
  283. return 0;
  284. if (!ossl_cmp_hdr_set_transactionID(ctx, hdr))
  285. return 0;
  286. /*-
  287. * set random senderNonce
  288. * according to section 5.1.1:
  289. *
  290. * senderNonce present
  291. * -- 128 (pseudo-)random bits
  292. * The senderNonce and recipNonce fields protect the PKIMessage against
  293. * replay attacks. The senderNonce will typically be 128 bits of
  294. * (pseudo-) random data generated by the sender, whereas the recipNonce
  295. * is copied from the senderNonce of the previous message in the
  296. * transaction.
  297. */
  298. if (!set_random(&hdr->senderNonce, ctx, OSSL_CMP_SENDERNONCE_LENGTH))
  299. return 0;
  300. /* store senderNonce - for cmp with recipNonce in next outgoing msg */
  301. if (!OSSL_CMP_CTX_set1_senderNonce(ctx, hdr->senderNonce))
  302. return 0;
  303. /*-
  304. * freeText [7] PKIFreeText OPTIONAL,
  305. * -- this may be used to indicate context-specific instructions
  306. * -- (this field is intended for human consumption)
  307. */
  308. if (ctx->freeText != NULL
  309. && !ossl_cmp_hdr_push1_freeText(hdr, ctx->freeText))
  310. return 0;
  311. return 1;
  312. }