2
0

v3_akey.c 5.5 KB

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