x509_v3.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/safestack.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/x509v3.h>
  17. #include "x509_local.h"
  18. DEFINE_STACK_OF(X509_EXTENSION)
  19. int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
  20. {
  21. if (x == NULL)
  22. return 0;
  23. return sk_X509_EXTENSION_num(x);
  24. }
  25. int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
  26. int lastpos)
  27. {
  28. ASN1_OBJECT *obj;
  29. obj = OBJ_nid2obj(nid);
  30. if (obj == NULL)
  31. return -2;
  32. return X509v3_get_ext_by_OBJ(x, obj, lastpos);
  33. }
  34. int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
  35. const ASN1_OBJECT *obj, int lastpos)
  36. {
  37. int n;
  38. X509_EXTENSION *ex;
  39. if (sk == NULL)
  40. return -1;
  41. lastpos++;
  42. if (lastpos < 0)
  43. lastpos = 0;
  44. n = sk_X509_EXTENSION_num(sk);
  45. for (; lastpos < n; lastpos++) {
  46. ex = sk_X509_EXTENSION_value(sk, lastpos);
  47. if (OBJ_cmp(ex->object, obj) == 0)
  48. return lastpos;
  49. }
  50. return -1;
  51. }
  52. int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
  53. int lastpos)
  54. {
  55. int n;
  56. X509_EXTENSION *ex;
  57. if (sk == NULL)
  58. return -1;
  59. lastpos++;
  60. if (lastpos < 0)
  61. lastpos = 0;
  62. n = sk_X509_EXTENSION_num(sk);
  63. for (; lastpos < n; lastpos++) {
  64. ex = sk_X509_EXTENSION_value(sk, lastpos);
  65. if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
  66. return lastpos;
  67. }
  68. return -1;
  69. }
  70. X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
  71. {
  72. if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
  73. return NULL;
  74. else
  75. return sk_X509_EXTENSION_value(x, loc);
  76. }
  77. X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
  78. {
  79. X509_EXTENSION *ret;
  80. if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
  81. return NULL;
  82. ret = sk_X509_EXTENSION_delete(x, loc);
  83. return ret;
  84. }
  85. STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
  86. X509_EXTENSION *ex, int loc)
  87. {
  88. X509_EXTENSION *new_ex = NULL;
  89. int n;
  90. STACK_OF(X509_EXTENSION) *sk = NULL;
  91. if (x == NULL) {
  92. X509err(X509_F_X509V3_ADD_EXT, ERR_R_PASSED_NULL_PARAMETER);
  93. goto err2;
  94. }
  95. if (*x == NULL) {
  96. if ((sk = sk_X509_EXTENSION_new_null()) == NULL)
  97. goto err;
  98. } else
  99. sk = *x;
  100. n = sk_X509_EXTENSION_num(sk);
  101. if (loc > n)
  102. loc = n;
  103. else if (loc < 0)
  104. loc = n;
  105. if ((new_ex = X509_EXTENSION_dup(ex)) == NULL)
  106. goto err2;
  107. if (!sk_X509_EXTENSION_insert(sk, new_ex, loc))
  108. goto err;
  109. if (*x == NULL)
  110. *x = sk;
  111. return sk;
  112. err:
  113. X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE);
  114. err2:
  115. X509_EXTENSION_free(new_ex);
  116. if (x != NULL && *x == NULL)
  117. sk_X509_EXTENSION_free(sk);
  118. return NULL;
  119. }
  120. X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
  121. int crit,
  122. ASN1_OCTET_STRING *data)
  123. {
  124. ASN1_OBJECT *obj;
  125. X509_EXTENSION *ret;
  126. obj = OBJ_nid2obj(nid);
  127. if (obj == NULL) {
  128. X509err(X509_F_X509_EXTENSION_CREATE_BY_NID, X509_R_UNKNOWN_NID);
  129. return NULL;
  130. }
  131. ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
  132. if (ret == NULL)
  133. ASN1_OBJECT_free(obj);
  134. return ret;
  135. }
  136. X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
  137. const ASN1_OBJECT *obj, int crit,
  138. ASN1_OCTET_STRING *data)
  139. {
  140. X509_EXTENSION *ret;
  141. if ((ex == NULL) || (*ex == NULL)) {
  142. if ((ret = X509_EXTENSION_new()) == NULL) {
  143. X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,
  144. ERR_R_MALLOC_FAILURE);
  145. return NULL;
  146. }
  147. } else
  148. ret = *ex;
  149. if (!X509_EXTENSION_set_object(ret, obj))
  150. goto err;
  151. if (!X509_EXTENSION_set_critical(ret, crit))
  152. goto err;
  153. if (!X509_EXTENSION_set_data(ret, data))
  154. goto err;
  155. if ((ex != NULL) && (*ex == NULL))
  156. *ex = ret;
  157. return ret;
  158. err:
  159. if ((ex == NULL) || (ret != *ex))
  160. X509_EXTENSION_free(ret);
  161. return NULL;
  162. }
  163. int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
  164. {
  165. if ((ex == NULL) || (obj == NULL))
  166. return 0;
  167. ASN1_OBJECT_free(ex->object);
  168. ex->object = OBJ_dup(obj);
  169. return ex->object != NULL;
  170. }
  171. int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
  172. {
  173. if (ex == NULL)
  174. return 0;
  175. ex->critical = (crit) ? 0xFF : -1;
  176. return 1;
  177. }
  178. int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
  179. {
  180. int i;
  181. if (ex == NULL)
  182. return 0;
  183. i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
  184. if (!i)
  185. return 0;
  186. return 1;
  187. }
  188. ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
  189. {
  190. if (ex == NULL)
  191. return NULL;
  192. return ex->object;
  193. }
  194. ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
  195. {
  196. if (ex == NULL)
  197. return NULL;
  198. return &ex->value;
  199. }
  200. int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
  201. {
  202. if (ex == NULL)
  203. return 0;
  204. if (ex->critical > 0)
  205. return 1;
  206. return 0;
  207. }