x509aset.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2021-2024 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 <openssl/err.h>
  10. #include <openssl/x509.h>
  11. #include <openssl/x509v3.h>
  12. #include "x509_acert.h"
  13. static int replace_gentime(ASN1_STRING **dest, const ASN1_GENERALIZEDTIME *src)
  14. {
  15. ASN1_STRING *s;
  16. if (src->type != V_ASN1_GENERALIZEDTIME)
  17. return 0;
  18. if (*dest == src)
  19. return 1;
  20. s = ASN1_STRING_dup(src);
  21. if (s == NULL) {
  22. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  23. return 0;
  24. }
  25. ASN1_STRING_free(*dest);
  26. *dest = s;
  27. return 1;
  28. }
  29. static int replace_dirName(GENERAL_NAMES **names, const X509_NAME *dirName)
  30. {
  31. GENERAL_NAME *gen_name = NULL;
  32. STACK_OF(GENERAL_NAME) *new_names = NULL;
  33. X509_NAME *name_copy;
  34. if ((name_copy = X509_NAME_dup(dirName)) == NULL) {
  35. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  36. goto err;
  37. }
  38. if ((new_names = sk_GENERAL_NAME_new_null()) == NULL) {
  39. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  40. goto err;
  41. }
  42. if ((gen_name = GENERAL_NAME_new()) == NULL) {
  43. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  44. goto err;
  45. }
  46. if (sk_GENERAL_NAME_push(new_names, gen_name) <= 0) {
  47. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  48. goto err;
  49. }
  50. GENERAL_NAME_set0_value(gen_name, GEN_DIRNAME, name_copy);
  51. GENERAL_NAMES_free(*names);
  52. *names = new_names;
  53. return 1;
  54. err:
  55. GENERAL_NAME_free(gen_name);
  56. sk_GENERAL_NAME_free(new_names);
  57. X509_NAME_free(name_copy);
  58. return 0;
  59. }
  60. int OSSL_OBJECT_DIGEST_INFO_set1_digest(OSSL_OBJECT_DIGEST_INFO *o,
  61. int digestedObjectType,
  62. X509_ALGOR *digestAlgorithm,
  63. ASN1_BIT_STRING *digest)
  64. {
  65. if (ASN1_ENUMERATED_set(&o->digestedObjectType, digestedObjectType) <= 0)
  66. return 0;
  67. if (X509_ALGOR_copy(&o->digestAlgorithm, digestAlgorithm) <= 0)
  68. return 0;
  69. if (ASN1_STRING_copy(&o->objectDigest, digest) <= 0)
  70. return 0;
  71. return 1;
  72. }
  73. int OSSL_ISSUER_SERIAL_set1_issuer(OSSL_ISSUER_SERIAL *isss,
  74. const X509_NAME *issuer)
  75. {
  76. return replace_dirName(&isss->issuer, issuer);
  77. }
  78. int OSSL_ISSUER_SERIAL_set1_serial(OSSL_ISSUER_SERIAL *isss,
  79. const ASN1_INTEGER *serial)
  80. {
  81. return ASN1_STRING_copy(&isss->serial, serial);
  82. }
  83. int OSSL_ISSUER_SERIAL_set1_issuerUID(OSSL_ISSUER_SERIAL *isss,
  84. const ASN1_BIT_STRING *uid)
  85. {
  86. ASN1_BIT_STRING_free(isss->issuerUID);
  87. isss->issuerUID = ASN1_STRING_dup(uid);
  88. if (isss->issuerUID == NULL) {
  89. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  90. return 0;
  91. }
  92. return 1;
  93. }
  94. int X509_ACERT_set_version(X509_ACERT *x, long version)
  95. {
  96. return ASN1_INTEGER_set(&x->acinfo->version, version);
  97. }
  98. void X509_ACERT_set0_holder_entityName(X509_ACERT *x, GENERAL_NAMES *names)
  99. {
  100. GENERAL_NAMES_free(x->acinfo->holder.entityName);
  101. x->acinfo->holder.entityName = names;
  102. }
  103. void X509_ACERT_set0_holder_baseCertId(X509_ACERT *x,
  104. OSSL_ISSUER_SERIAL *isss)
  105. {
  106. OSSL_ISSUER_SERIAL_free(x->acinfo->holder.baseCertificateID);
  107. x->acinfo->holder.baseCertificateID = isss;
  108. }
  109. void X509_ACERT_set0_holder_digest(X509_ACERT *x,
  110. OSSL_OBJECT_DIGEST_INFO *dinfo)
  111. {
  112. OSSL_OBJECT_DIGEST_INFO_free(x->acinfo->holder.objectDigestInfo);
  113. x->acinfo->holder.objectDigestInfo = dinfo;
  114. }
  115. int X509_ACERT_set1_issuerName(X509_ACERT *x, const X509_NAME *name)
  116. {
  117. X509_ACERT_ISSUER_V2FORM *v2Form;
  118. v2Form = x->acinfo->issuer.u.v2Form;
  119. /* only v2Form is supported, so always create that version */
  120. if (v2Form == NULL) {
  121. v2Form = X509_ACERT_ISSUER_V2FORM_new();
  122. if (v2Form == NULL) {
  123. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  124. return 0;
  125. }
  126. x->acinfo->issuer.u.v2Form = v2Form;
  127. x->acinfo->issuer.type = X509_ACERT_ISSUER_V2;
  128. }
  129. return replace_dirName(&v2Form->issuerName, name);
  130. }
  131. int X509_ACERT_set1_serialNumber(X509_ACERT *x, const ASN1_INTEGER *serial)
  132. {
  133. return ASN1_STRING_copy(&x->acinfo->serialNumber, serial);
  134. }
  135. int X509_ACERT_set1_notBefore(X509_ACERT *x, const ASN1_GENERALIZEDTIME *time)
  136. {
  137. return replace_gentime(&x->acinfo->validityPeriod.notBefore, time);
  138. }
  139. int X509_ACERT_set1_notAfter(X509_ACERT *x, const ASN1_GENERALIZEDTIME *time)
  140. {
  141. return replace_gentime(&x->acinfo->validityPeriod.notAfter, time);
  142. }