x_req.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/x509.h>
  13. #include "crypto/x509.h"
  14. DEFINE_STACK_OF(X509_ATTRIBUTE)
  15. /*-
  16. * X509_REQ_INFO is handled in an unusual way to get round
  17. * invalid encodings. Some broken certificate requests don't
  18. * encode the attributes field if it is empty. This is in
  19. * violation of PKCS#10 but we need to tolerate it. We do
  20. * this by making the attributes field OPTIONAL then using
  21. * the callback to initialise it to an empty STACK.
  22. *
  23. * This means that the field will be correctly encoded unless
  24. * we NULL out the field.
  25. *
  26. * As a result we no longer need the req_kludge field because
  27. * the information is now contained in the attributes field:
  28. * 1. If it is NULL then it's the invalid omission.
  29. * 2. If it is empty it is the correct encoding.
  30. * 3. If it is not empty then some attributes are present.
  31. *
  32. */
  33. static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  34. void *exarg)
  35. {
  36. X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval;
  37. if (operation == ASN1_OP_NEW_POST) {
  38. rinf->attributes = sk_X509_ATTRIBUTE_new_null();
  39. if (!rinf->attributes)
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. static int req_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  45. void *exarg)
  46. {
  47. #ifndef OPENSSL_NO_SM2
  48. X509_REQ *ret = (X509_REQ *)*pval;
  49. switch (operation) {
  50. case ASN1_OP_D2I_PRE:
  51. ASN1_OCTET_STRING_free(ret->distinguishing_id);
  52. /* fall thru */
  53. case ASN1_OP_NEW_POST:
  54. ret->distinguishing_id = NULL;
  55. break;
  56. case ASN1_OP_FREE_POST:
  57. ASN1_OCTET_STRING_free(ret->distinguishing_id);
  58. break;
  59. }
  60. #endif
  61. return 1;
  62. }
  63. ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = {
  64. ASN1_SIMPLE(X509_REQ_INFO, version, ASN1_INTEGER),
  65. ASN1_SIMPLE(X509_REQ_INFO, subject, X509_NAME),
  66. ASN1_SIMPLE(X509_REQ_INFO, pubkey, X509_PUBKEY),
  67. /* This isn't really OPTIONAL but it gets round invalid
  68. * encodings
  69. */
  70. ASN1_IMP_SET_OF_OPT(X509_REQ_INFO, attributes, X509_ATTRIBUTE, 0)
  71. } ASN1_SEQUENCE_END_enc(X509_REQ_INFO, X509_REQ_INFO)
  72. IMPLEMENT_ASN1_FUNCTIONS(X509_REQ_INFO)
  73. ASN1_SEQUENCE_ref(X509_REQ, req_cb) = {
  74. ASN1_EMBED(X509_REQ, req_info, X509_REQ_INFO),
  75. ASN1_EMBED(X509_REQ, sig_alg, X509_ALGOR),
  76. ASN1_SIMPLE(X509_REQ, signature, ASN1_BIT_STRING)
  77. } ASN1_SEQUENCE_END_ref(X509_REQ, X509_REQ)
  78. IMPLEMENT_ASN1_FUNCTIONS(X509_REQ)
  79. IMPLEMENT_ASN1_DUP_FUNCTION(X509_REQ)
  80. void X509_REQ_set0_distinguishing_id(X509_REQ *x, ASN1_OCTET_STRING *d_id)
  81. {
  82. ASN1_OCTET_STRING_free(x->distinguishing_id);
  83. x->distinguishing_id = d_id;
  84. }
  85. ASN1_OCTET_STRING *X509_REQ_get0_distinguishing_id(X509_REQ *x)
  86. {
  87. return x->distinguishing_id;
  88. }