2
0

x509_v3.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 1995-2016 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/stack.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. sk_X509_EXTENSION_free(sk);
  116. return (NULL);
  117. }
  118. X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
  119. int crit,
  120. ASN1_OCTET_STRING *data)
  121. {
  122. ASN1_OBJECT *obj;
  123. X509_EXTENSION *ret;
  124. obj = OBJ_nid2obj(nid);
  125. if (obj == NULL) {
  126. X509err(X509_F_X509_EXTENSION_CREATE_BY_NID, X509_R_UNKNOWN_NID);
  127. return (NULL);
  128. }
  129. ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
  130. if (ret == NULL)
  131. ASN1_OBJECT_free(obj);
  132. return (ret);
  133. }
  134. X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
  135. const ASN1_OBJECT *obj, int crit,
  136. ASN1_OCTET_STRING *data)
  137. {
  138. X509_EXTENSION *ret;
  139. if ((ex == NULL) || (*ex == NULL)) {
  140. if ((ret = X509_EXTENSION_new()) == NULL) {
  141. X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,
  142. ERR_R_MALLOC_FAILURE);
  143. return (NULL);
  144. }
  145. } else
  146. ret = *ex;
  147. if (!X509_EXTENSION_set_object(ret, obj))
  148. goto err;
  149. if (!X509_EXTENSION_set_critical(ret, crit))
  150. goto err;
  151. if (!X509_EXTENSION_set_data(ret, data))
  152. goto err;
  153. if ((ex != NULL) && (*ex == NULL))
  154. *ex = ret;
  155. return (ret);
  156. err:
  157. if ((ex == NULL) || (ret != *ex))
  158. X509_EXTENSION_free(ret);
  159. return (NULL);
  160. }
  161. int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
  162. {
  163. if ((ex == NULL) || (obj == NULL))
  164. return (0);
  165. ASN1_OBJECT_free(ex->object);
  166. ex->object = OBJ_dup(obj);
  167. return ex->object != NULL;
  168. }
  169. int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
  170. {
  171. if (ex == NULL)
  172. return (0);
  173. ex->critical = (crit) ? 0xFF : -1;
  174. return (1);
  175. }
  176. int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
  177. {
  178. int i;
  179. if (ex == NULL)
  180. return (0);
  181. i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
  182. if (!i)
  183. return (0);
  184. return (1);
  185. }
  186. ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
  187. {
  188. if (ex == NULL)
  189. return (NULL);
  190. return (ex->object);
  191. }
  192. ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
  193. {
  194. if (ex == NULL)
  195. return (NULL);
  196. return &ex->value;
  197. }
  198. int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
  199. {
  200. if (ex == NULL)
  201. return (0);
  202. if (ex->critical > 0)
  203. return 1;
  204. return 0;
  205. }