v3_akey.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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/conf.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/x509v3.h>
  15. #include "ext_dat.h"
  16. static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  17. AUTHORITY_KEYID *akeyid,
  18. STACK_OF(CONF_VALUE)
  19. *extlist);
  20. static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  21. X509V3_CTX *ctx,
  22. STACK_OF(CONF_VALUE) *values);
  23. const X509V3_EXT_METHOD v3_akey_id = {
  24. NID_authority_key_identifier,
  25. X509V3_EXT_MULTILINE, ASN1_ITEM_ref(AUTHORITY_KEYID),
  26. 0, 0, 0, 0,
  27. 0, 0,
  28. (X509V3_EXT_I2V) i2v_AUTHORITY_KEYID,
  29. (X509V3_EXT_V2I)v2i_AUTHORITY_KEYID,
  30. 0, 0,
  31. NULL
  32. };
  33. static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  34. AUTHORITY_KEYID *akeyid,
  35. STACK_OF(CONF_VALUE)
  36. *extlist)
  37. {
  38. char *tmp = NULL;
  39. STACK_OF(CONF_VALUE) *origextlist = extlist, *tmpextlist;
  40. if (akeyid->keyid) {
  41. tmp = OPENSSL_buf2hexstr(akeyid->keyid->data, akeyid->keyid->length);
  42. if (tmp == NULL) {
  43. X509V3err(X509V3_F_I2V_AUTHORITY_KEYID, ERR_R_MALLOC_FAILURE);
  44. return NULL;
  45. }
  46. if (!X509V3_add_value("keyid", tmp, &extlist)) {
  47. OPENSSL_free(tmp);
  48. X509V3err(X509V3_F_I2V_AUTHORITY_KEYID, ERR_R_X509_LIB);
  49. goto err;
  50. }
  51. OPENSSL_free(tmp);
  52. }
  53. if (akeyid->issuer) {
  54. tmpextlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist);
  55. if (tmpextlist == NULL) {
  56. X509V3err(X509V3_F_I2V_AUTHORITY_KEYID, ERR_R_X509_LIB);
  57. goto err;
  58. }
  59. extlist = tmpextlist;
  60. }
  61. if (akeyid->serial) {
  62. tmp = OPENSSL_buf2hexstr(akeyid->serial->data, akeyid->serial->length);
  63. if (tmp == NULL) {
  64. X509V3err(X509V3_F_I2V_AUTHORITY_KEYID, ERR_R_MALLOC_FAILURE);
  65. goto err;
  66. }
  67. if (!X509V3_add_value("serial", tmp, &extlist)) {
  68. OPENSSL_free(tmp);
  69. X509V3err(X509V3_F_I2V_AUTHORITY_KEYID, ERR_R_X509_LIB);
  70. goto err;
  71. }
  72. OPENSSL_free(tmp);
  73. }
  74. return extlist;
  75. err:
  76. if (origextlist == NULL)
  77. sk_CONF_VALUE_pop_free(extlist, X509V3_conf_free);
  78. return NULL;
  79. }
  80. /*-
  81. * Currently two options:
  82. * keyid: use the issuers subject keyid, the value 'always' means its is
  83. * an error if the issuer certificate doesn't have a key id.
  84. * issuer: use the issuers cert issuer and serial number. The default is
  85. * to only use this if keyid is not present. With the option 'always'
  86. * this is always included.
  87. */
  88. static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  89. X509V3_CTX *ctx,
  90. STACK_OF(CONF_VALUE) *values)
  91. {
  92. char keyid = 0, issuer = 0;
  93. int i;
  94. CONF_VALUE *cnf;
  95. ASN1_OCTET_STRING *ikeyid = NULL;
  96. X509_NAME *isname = NULL;
  97. GENERAL_NAMES *gens = NULL;
  98. GENERAL_NAME *gen = NULL;
  99. ASN1_INTEGER *serial = NULL;
  100. X509_EXTENSION *ext;
  101. X509 *cert;
  102. AUTHORITY_KEYID *akeyid;
  103. for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
  104. cnf = sk_CONF_VALUE_value(values, i);
  105. if (strcmp(cnf->name, "keyid") == 0) {
  106. keyid = 1;
  107. if (cnf->value && strcmp(cnf->value, "always") == 0)
  108. keyid = 2;
  109. } else if (strcmp(cnf->name, "issuer") == 0) {
  110. issuer = 1;
  111. if (cnf->value && strcmp(cnf->value, "always") == 0)
  112. issuer = 2;
  113. } else {
  114. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID, X509V3_R_UNKNOWN_OPTION);
  115. ERR_add_error_data(2, "name=", cnf->name);
  116. return NULL;
  117. }
  118. }
  119. if (!ctx || !ctx->issuer_cert) {
  120. if (ctx && (ctx->flags == CTX_TEST))
  121. return AUTHORITY_KEYID_new();
  122. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,
  123. X509V3_R_NO_ISSUER_CERTIFICATE);
  124. return NULL;
  125. }
  126. cert = ctx->issuer_cert;
  127. if (keyid) {
  128. i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
  129. if ((i >= 0) && (ext = X509_get_ext(cert, i)))
  130. ikeyid = X509V3_EXT_d2i(ext);
  131. if (keyid == 2 && !ikeyid) {
  132. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,
  133. X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
  134. return NULL;
  135. }
  136. }
  137. if ((issuer && !ikeyid) || (issuer == 2)) {
  138. isname = X509_NAME_dup(X509_get_issuer_name(cert));
  139. serial = ASN1_INTEGER_dup(X509_get_serialNumber(cert));
  140. if (!isname || !serial) {
  141. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,
  142. X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
  143. goto err;
  144. }
  145. }
  146. if ((akeyid = AUTHORITY_KEYID_new()) == NULL)
  147. goto err;
  148. if (isname) {
  149. if ((gens = sk_GENERAL_NAME_new_null()) == NULL
  150. || (gen = GENERAL_NAME_new()) == NULL
  151. || !sk_GENERAL_NAME_push(gens, gen)) {
  152. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID, ERR_R_MALLOC_FAILURE);
  153. goto err;
  154. }
  155. gen->type = GEN_DIRNAME;
  156. gen->d.dirn = isname;
  157. }
  158. akeyid->issuer = gens;
  159. gen = NULL;
  160. gens = NULL;
  161. akeyid->serial = serial;
  162. akeyid->keyid = ikeyid;
  163. return akeyid;
  164. err:
  165. sk_GENERAL_NAME_free(gens);
  166. GENERAL_NAME_free(gen);
  167. X509_NAME_free(isname);
  168. ASN1_INTEGER_free(serial);
  169. ASN1_OCTET_STRING_free(ikeyid);
  170. return NULL;
  171. }