x509_v3.c 5.7 KB

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