cmp_hdr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. STACK_OF(OSSL_CMP_ITAV)
  64. *OSSL_CMP_HDR_get0_geninfo_ITAVs(const OSSL_CMP_PKIHEADER *hdr)
  65. {
  66. if (hdr == NULL) {
  67. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  68. return NULL;
  69. }
  70. return hdr->generalInfo;
  71. }
  72. /* a NULL-DN as an empty sequence of RDNs */
  73. int ossl_cmp_general_name_is_NULL_DN(GENERAL_NAME *name)
  74. {
  75. return name == NULL
  76. || (name->type == GEN_DIRNAME && IS_NULL_DN(name->d.directoryName));
  77. }
  78. /* assign to *tgt a copy of src (which may be NULL to indicate an empty DN) */
  79. static int set1_general_name(GENERAL_NAME **tgt, const X509_NAME *src)
  80. {
  81. GENERAL_NAME *name;
  82. if (!ossl_assert(tgt != NULL))
  83. return 0;
  84. if ((name = GENERAL_NAME_new()) == NULL)
  85. goto err;
  86. name->type = GEN_DIRNAME;
  87. if (src == NULL) { /* NULL-DN */
  88. if ((name->d.directoryName = X509_NAME_new()) == NULL)
  89. goto err;
  90. } else if (!X509_NAME_set(&name->d.directoryName, src)) {
  91. goto err;
  92. }
  93. GENERAL_NAME_free(*tgt);
  94. *tgt = name;
  95. return 1;
  96. err:
  97. GENERAL_NAME_free(name);
  98. return 0;
  99. }
  100. /*
  101. * Set the sender name in PKIHeader.
  102. * when nm is NULL, sender is set to an empty string
  103. * returns 1 on success, 0 on error
  104. */
  105. int ossl_cmp_hdr_set1_sender(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm)
  106. {
  107. if (!ossl_assert(hdr != NULL))
  108. return 0;
  109. return set1_general_name(&hdr->sender, nm);
  110. }
  111. int ossl_cmp_hdr_set1_recipient(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm)
  112. {
  113. if (!ossl_assert(hdr != NULL))
  114. return 0;
  115. return set1_general_name(&hdr->recipient, nm);
  116. }
  117. int ossl_cmp_hdr_update_messageTime(OSSL_CMP_PKIHEADER *hdr)
  118. {
  119. if (!ossl_assert(hdr != NULL))
  120. return 0;
  121. if (hdr->messageTime == NULL
  122. && (hdr->messageTime = ASN1_GENERALIZEDTIME_new()) == NULL)
  123. return 0;
  124. return ASN1_GENERALIZEDTIME_set(hdr->messageTime, time(NULL)) != NULL;
  125. }
  126. /* assign to *tgt a random byte array of given length */
  127. static int set_random(ASN1_OCTET_STRING **tgt, OSSL_CMP_CTX *ctx, size_t len)
  128. {
  129. unsigned char *bytes = OPENSSL_malloc(len);
  130. int res = 0;
  131. if (bytes == NULL || RAND_bytes_ex(ctx->libctx, bytes, len, 0) <= 0)
  132. ERR_raise(ERR_LIB_CMP, CMP_R_FAILURE_OBTAINING_RANDOM);
  133. else
  134. res = ossl_cmp_asn1_octet_string_set1_bytes(tgt, bytes, len);
  135. OPENSSL_free(bytes);
  136. return res;
  137. }
  138. int ossl_cmp_hdr_set1_senderKID(OSSL_CMP_PKIHEADER *hdr,
  139. const ASN1_OCTET_STRING *senderKID)
  140. {
  141. if (!ossl_assert(hdr != NULL))
  142. return 0;
  143. return ossl_cmp_asn1_octet_string_set1(&hdr->senderKID, senderKID);
  144. }
  145. /* push the given text string to the given PKIFREETEXT ft */
  146. int ossl_cmp_hdr_push0_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 sk_ASN1_UTF8STRING_push(hdr->freeText, text);
  154. }
  155. int ossl_cmp_hdr_push1_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text)
  156. {
  157. if (!ossl_assert(hdr != NULL && text != NULL))
  158. return 0;
  159. if (hdr->freeText == NULL
  160. && (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL)
  161. return 0;
  162. return
  163. ossl_cmp_sk_ASN1_UTF8STRING_push_str(hdr->freeText, (char *)text->data,
  164. text->length);
  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. const 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_has_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. /*
  229. * set ctx->transactionID in CMP header
  230. * if ctx->transactionID is NULL, a random one is created with 128 bit
  231. * according to section 5.1.1:
  232. *
  233. * It is RECOMMENDED that the clients fill the transactionID field with
  234. * 128 bits of (pseudo-) random data for the start of a transaction to
  235. * reduce the probability of having the transactionID in use at the server.
  236. */
  237. int ossl_cmp_hdr_set_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr)
  238. {
  239. if (ctx->transactionID == NULL) {
  240. char *tid;
  241. if (!set_random(&ctx->transactionID, ctx,
  242. OSSL_CMP_TRANSACTIONID_LENGTH))
  243. return 0;
  244. tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID);
  245. if (tid != NULL)
  246. ossl_cmp_log1(DEBUG, ctx,
  247. "Starting new transaction with ID=%s", tid);
  248. OPENSSL_free(tid);
  249. }
  250. return ossl_cmp_asn1_octet_string_set1(&hdr->transactionID,
  251. ctx->transactionID);
  252. }
  253. /* fill in all fields of the hdr according to the info given in ctx */
  254. int ossl_cmp_hdr_init(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr)
  255. {
  256. const X509_NAME *sender;
  257. const X509_NAME *rcp = NULL;
  258. if (!ossl_assert(ctx != NULL && hdr != NULL))
  259. return 0;
  260. /* set the CMP version */
  261. if (!ossl_cmp_hdr_set_pvno(hdr, OSSL_CMP_PVNO))
  262. return 0;
  263. /*
  264. * If no protection cert nor oldCert nor CSR nor subject is given,
  265. * sender name is not known to the client and thus set to NULL-DN
  266. */
  267. sender = ctx->cert != NULL ? X509_get_subject_name(ctx->cert) :
  268. ctx->oldCert != NULL ? X509_get_subject_name(ctx->oldCert) :
  269. ctx->p10CSR != NULL ? X509_REQ_get_subject_name(ctx->p10CSR) :
  270. ctx->subjectName;
  271. if (!ossl_cmp_hdr_set1_sender(hdr, sender))
  272. return 0;
  273. /* determine recipient entry in PKIHeader */
  274. if (ctx->recipient != NULL)
  275. rcp = ctx->recipient;
  276. else if (ctx->srvCert != NULL)
  277. rcp = X509_get_subject_name(ctx->srvCert);
  278. else if (ctx->issuer != NULL)
  279. rcp = ctx->issuer;
  280. else if (ctx->oldCert != NULL)
  281. rcp = X509_get_issuer_name(ctx->oldCert);
  282. else if (ctx->cert != NULL)
  283. rcp = X509_get_issuer_name(ctx->cert);
  284. if (!ossl_cmp_hdr_set1_recipient(hdr, rcp))
  285. return 0;
  286. /* set current time as message time */
  287. if (!ossl_cmp_hdr_update_messageTime(hdr))
  288. return 0;
  289. if (ctx->recipNonce != NULL
  290. && !ossl_cmp_asn1_octet_string_set1(&hdr->recipNonce,
  291. ctx->recipNonce))
  292. return 0;
  293. if (!ossl_cmp_hdr_set_transactionID(ctx, hdr))
  294. return 0;
  295. /*-
  296. * set random senderNonce
  297. * according to section 5.1.1:
  298. *
  299. * senderNonce present
  300. * -- 128 (pseudo-)random bits
  301. * The senderNonce and recipNonce fields protect the PKIMessage against
  302. * replay attacks. The senderNonce will typically be 128 bits of
  303. * (pseudo-) random data generated by the sender, whereas the recipNonce
  304. * is copied from the senderNonce of the previous message in the
  305. * transaction.
  306. */
  307. if (!set_random(&hdr->senderNonce, ctx, 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. }