| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/asn1.h> |
| 16 | #include <openssl/err.h> |
| 17 | #include <openssl/evp.h> |
| 18 | #include <openssl/obj.h> |
| 19 | #include <openssl/stack.h> |
| 20 | #include <openssl/x509.h> |
| 21 |
| 22 | #include "internal.h" |
| 23 |
| 24 |
| 25 | int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x) { |
| 26 | if (x == NULL) { |
| 27 | return 0; |
| 28 | } |
| 29 | return (int)sk_X509_EXTENSION_num(x); |
| 30 | } |
| 31 |
| 32 | int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid, |
| 33 | int lastpos) { |
| 34 | const ASN1_OBJECT *obj = OBJ_nid2obj(nid); |
| 35 | if (obj == NULL) { |
| 36 | return -1; |
| 37 | } |
| 38 | return X509v3_get_ext_by_OBJ(x, obj, lastpos); |
| 39 | } |
| 40 |
| 41 | int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk, |
| 42 | const ASN1_OBJECT *obj, int lastpos) { |
| 43 | if (sk == NULL) { |
| 44 | return -1; |
| 45 | } |
| 46 | lastpos++; |
| 47 | if (lastpos < 0) { |
| 48 | lastpos = 0; |
| 49 | } |
| 50 | int n = (int)sk_X509_EXTENSION_num(sk); |
| 51 | for (; lastpos < n; lastpos++) { |
| 52 | const X509_EXTENSION *ex = sk_X509_EXTENSION_value(sk, lastpos); |
| 53 | if (OBJ_cmp(ex->object, obj) == 0) { |
| 54 | return lastpos; |
| 55 | } |
| 56 | } |
| 57 | return -1; |
| 58 | } |
| 59 |
| 60 | int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit, |
| 61 | int lastpos) { |
| 62 | if (sk == NULL) { |
| 63 | return -1; |
| 64 | } |
| 65 |
| 66 | lastpos++; |
| 67 | if (lastpos < 0) { |
| 68 | lastpos = 0; |
| 69 | } |
| 70 |
| 71 | crit = !!crit; |
| 72 | int n = (int)sk_X509_EXTENSION_num(sk); |
| 73 | for (; lastpos < n; lastpos++) { |
| 74 | const X509_EXTENSION *ex = sk_X509_EXTENSION_value(sk, lastpos); |
| 75 | if (X509_EXTENSION_get_critical(ex) == crit) { |
| 76 | return lastpos; |
| 77 | } |
| 78 | } |
| 79 | return -1; |
| 80 | } |
| 81 |
| 82 | X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc) { |
| 83 | if (x == NULL || loc < 0 || sk_X509_EXTENSION_num(x) <= (size_t)loc) { |
| 84 | return NULL; |
| 85 | } else { |
| 86 | return sk_X509_EXTENSION_value(x, loc); |
| 87 | } |
| 88 | } |
| 89 |
| 90 | X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc) { |
| 91 | X509_EXTENSION *ret; |
| 92 |
| 93 | if (x == NULL || loc < 0 || sk_X509_EXTENSION_num(x) <= (size_t)loc) { |
| 94 | return NULL; |
| 95 | } |
| 96 | ret = sk_X509_EXTENSION_delete(x, loc); |
| 97 | return ret; |
| 98 | } |
| 99 |
| 100 | STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x, |
| 101 | const X509_EXTENSION *ex, int loc) { |
| 102 | X509_EXTENSION *new_ex = NULL; |
| 103 | STACK_OF(X509_EXTENSION) *sk = NULL; |
| 104 | int free_sk = 0, n; |
| 105 |
| 106 | if (x == NULL) { |
| 107 | OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); |
| 108 | goto err; |
| 109 | } |
| 110 |
| 111 | if (*x == NULL) { |
| 112 | if ((sk = sk_X509_EXTENSION_new_null()) == NULL) { |
| 113 | goto err; |
| 114 | } |
| 115 | free_sk = 1; |
| 116 | } else { |
| 117 | sk = *x; |
| 118 | } |
| 119 |
| 120 | n = (int)sk_X509_EXTENSION_num(sk); |
| 121 | if (loc > n) { |
| 122 | loc = n; |
| 123 | } else if (loc < 0) { |
| 124 | loc = n; |
| 125 | } |
| 126 |
| 127 | if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) { |
| 128 | goto err; |
| 129 | } |
| 130 | if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) { |
| 131 | goto err; |
| 132 | } |
| 133 | if (*x == NULL) { |
| 134 | *x = sk; |
| 135 | } |
| 136 | return sk; |
| 137 |
| 138 | err: |
| 139 | X509_EXTENSION_free(new_ex); |
| 140 | if (free_sk) { |
| 141 | sk_X509_EXTENSION_free(sk); |
| 142 | } |
| 143 | return NULL; |
| 144 | } |
| 145 |
| 146 | X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid, |
| 147 | int crit, |
| 148 | const ASN1_OCTET_STRING *data) { |
| 149 | const ASN1_OBJECT *obj; |
| 150 | X509_EXTENSION *ret; |
| 151 |
| 152 | obj = OBJ_nid2obj(nid); |
| 153 | if (obj == NULL) { |
| 154 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_NID); |
| 155 | return NULL; |
| 156 | } |
| 157 | ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data); |
| 158 | return ret; |
| 159 | } |
| 160 |
| 161 | X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex, |
| 162 | const ASN1_OBJECT *obj, int crit, |
| 163 | const ASN1_OCTET_STRING *data) { |
| 164 | X509_EXTENSION *ret; |
| 165 |
| 166 | if ((ex == NULL) || (*ex == NULL)) { |
| 167 | if ((ret = X509_EXTENSION_new()) == NULL) { |
| 168 | return NULL; |
| 169 | } |
| 170 | } else { |
| 171 | ret = *ex; |
| 172 | } |
| 173 |
| 174 | if (!X509_EXTENSION_set_object(ret, obj)) { |
| 175 | goto err; |
| 176 | } |
| 177 | if (!X509_EXTENSION_set_critical(ret, crit)) { |
| 178 | goto err; |
| 179 | } |
| 180 | if (!X509_EXTENSION_set_data(ret, data)) { |
| 181 | goto err; |
| 182 | } |
| 183 |
| 184 | if ((ex != NULL) && (*ex == NULL)) { |
| 185 | *ex = ret; |
| 186 | } |
| 187 | return ret; |
| 188 | err: |
| 189 | if ((ex == NULL) || (ret != *ex)) { |
| 190 | X509_EXTENSION_free(ret); |
| 191 | } |
| 192 | return NULL; |
| 193 | } |
| 194 |
| 195 | int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj) { |
| 196 | if ((ex == NULL) || (obj == NULL)) { |
| 197 | return 0; |
| 198 | } |
| 199 | ASN1_OBJECT_free(ex->object); |
| 200 | ex->object = OBJ_dup(obj); |
| 201 | return ex->object != NULL; |
| 202 | } |
| 203 |
| 204 | int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit) { |
| 205 | if (ex == NULL) { |
| 206 | return 0; |
| 207 | } |
| 208 | // The critical field is DEFAULT FALSE, so non-critical extensions should omit |
| 209 | // the value. |
| 210 | ex->critical = crit ? ASN1_BOOLEAN_TRUE : ASN1_BOOLEAN_NONE; |
| 211 | return 1; |
| 212 | } |
| 213 |
| 214 | int X509_EXTENSION_set_data(X509_EXTENSION *ex, const ASN1_OCTET_STRING *data) { |
| 215 | int i; |
| 216 |
| 217 | if (ex == NULL) { |
| 218 | return 0; |
| 219 | } |
| 220 | i = ASN1_OCTET_STRING_set(ex->value, data->data, data->length); |
| 221 | if (!i) { |
| 222 | return 0; |
| 223 | } |
| 224 | return 1; |
| 225 | } |
| 226 |
| 227 | ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex) { |
| 228 | if (ex == NULL) { |
| 229 | return NULL; |
| 230 | } |
| 231 | return ex->object; |
| 232 | } |
| 233 |
| 234 | ASN1_OCTET_STRING *X509_EXTENSION_get_data(const X509_EXTENSION *ex) { |
| 235 | if (ex == NULL) { |
| 236 | return NULL; |
| 237 | } |
| 238 | return ex->value; |
| 239 | } |
| 240 |
| 241 | int X509_EXTENSION_get_critical(const X509_EXTENSION *ex) { |
| 242 | if (ex == NULL) { |
| 243 | return 0; |
| 244 | } |
| 245 | if (ex->critical > 0) { |
| 246 | return 1; |
| 247 | } |
| 248 | return 0; |
| 249 | } |
| 250 |