x509_v3.c 5.8 KB

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