2016-05-17 18:52:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2017-02-23 13:52:23 +00:00
|
|
|
#include <openssl/stack.h>
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/asn1.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/x509.h>
|
1999-11-29 01:09:25 +00:00
|
|
|
#include <openssl/x509v3.h>
|
2015-03-15 13:43:56 +00:00
|
|
|
#include "x509_lcl.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-05-02 21:36:58 +00:00
|
|
|
int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (x == NULL)
|
|
|
|
return (0);
|
|
|
|
return (sk_X509_EXTENSION_num(x));
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-05-02 21:36:58 +00:00
|
|
|
int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
|
2015-01-22 03:40:55 +00:00
|
|
|
int lastpos)
|
|
|
|
{
|
|
|
|
ASN1_OBJECT *obj;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
obj = OBJ_nid2obj(nid);
|
|
|
|
if (obj == NULL)
|
|
|
|
return (-2);
|
|
|
|
return (X509v3_get_ext_by_OBJ(x, obj, lastpos));
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
|
2016-07-31 17:38:09 +00:00
|
|
|
const ASN1_OBJECT *obj, int lastpos)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
X509_EXTENSION *ex;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (sk == NULL)
|
|
|
|
return (-1);
|
|
|
|
lastpos++;
|
|
|
|
if (lastpos < 0)
|
|
|
|
lastpos = 0;
|
|
|
|
n = sk_X509_EXTENSION_num(sk);
|
|
|
|
for (; lastpos < n; lastpos++) {
|
|
|
|
ex = sk_X509_EXTENSION_value(sk, lastpos);
|
|
|
|
if (OBJ_cmp(ex->object, obj) == 0)
|
|
|
|
return (lastpos);
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-05-02 21:36:58 +00:00
|
|
|
int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
|
2015-01-22 03:40:55 +00:00
|
|
|
int lastpos)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
X509_EXTENSION *ex;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (sk == NULL)
|
|
|
|
return (-1);
|
|
|
|
lastpos++;
|
|
|
|
if (lastpos < 0)
|
|
|
|
lastpos = 0;
|
|
|
|
n = sk_X509_EXTENSION_num(sk);
|
|
|
|
for (; lastpos < n; lastpos++) {
|
|
|
|
ex = sk_X509_EXTENSION_value(sk, lastpos);
|
|
|
|
if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
|
|
|
|
return (lastpos);
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-05-02 21:36:58 +00:00
|
|
|
X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return sk_X509_EXTENSION_value(x, loc);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-05-02 21:36:58 +00:00
|
|
|
X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
X509_EXTENSION *ret;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
|
|
|
|
return (NULL);
|
|
|
|
ret = sk_X509_EXTENSION_delete(x, loc);
|
|
|
|
return (ret);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-05-02 21:36:58 +00:00
|
|
|
STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
|
2015-01-22 03:40:55 +00:00
|
|
|
X509_EXTENSION *ex, int loc)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *new_ex = NULL;
|
|
|
|
int n;
|
|
|
|
STACK_OF(X509_EXTENSION) *sk = NULL;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (x == NULL) {
|
|
|
|
X509err(X509_F_X509V3_ADD_EXT, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
goto err2;
|
|
|
|
}
|
2005-07-26 21:10:34 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (*x == NULL) {
|
|
|
|
if ((sk = sk_X509_EXTENSION_new_null()) == NULL)
|
|
|
|
goto err;
|
|
|
|
} else
|
|
|
|
sk = *x;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
n = sk_X509_EXTENSION_num(sk);
|
|
|
|
if (loc > n)
|
|
|
|
loc = n;
|
|
|
|
else if (loc < 0)
|
|
|
|
loc = n;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((new_ex = X509_EXTENSION_dup(ex)) == NULL)
|
|
|
|
goto err2;
|
|
|
|
if (!sk_X509_EXTENSION_insert(sk, new_ex, loc))
|
|
|
|
goto err;
|
|
|
|
if (*x == NULL)
|
|
|
|
*x = sk;
|
|
|
|
return (sk);
|
|
|
|
err:
|
|
|
|
X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE);
|
|
|
|
err2:
|
2015-04-30 21:33:59 +00:00
|
|
|
X509_EXTENSION_free(new_ex);
|
|
|
|
sk_X509_EXTENSION_free(sk);
|
2015-01-22 03:40:55 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
|
2015-01-22 03:40:55 +00:00
|
|
|
int crit,
|
|
|
|
ASN1_OCTET_STRING *data)
|
|
|
|
{
|
|
|
|
ASN1_OBJECT *obj;
|
|
|
|
X509_EXTENSION *ret;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
obj = OBJ_nid2obj(nid);
|
|
|
|
if (obj == NULL) {
|
|
|
|
X509err(X509_F_X509_EXTENSION_CREATE_BY_NID, X509_R_UNKNOWN_NID);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
|
|
|
|
if (ret == NULL)
|
|
|
|
ASN1_OBJECT_free(obj);
|
|
|
|
return (ret);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
|
2016-07-31 17:38:09 +00:00
|
|
|
const ASN1_OBJECT *obj, int crit,
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_OCTET_STRING *data)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ret;
|
|
|
|
|
|
|
|
if ((ex == NULL) || (*ex == NULL)) {
|
|
|
|
if ((ret = X509_EXTENSION_new()) == NULL) {
|
|
|
|
X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,
|
|
|
|
ERR_R_MALLOC_FAILURE);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ret = *ex;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!X509_EXTENSION_set_object(ret, obj))
|
|
|
|
goto err;
|
|
|
|
if (!X509_EXTENSION_set_critical(ret, crit))
|
|
|
|
goto err;
|
|
|
|
if (!X509_EXTENSION_set_data(ret, data))
|
|
|
|
goto err;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((ex != NULL) && (*ex == NULL))
|
|
|
|
*ex = ret;
|
|
|
|
return (ret);
|
|
|
|
err:
|
|
|
|
if ((ex == NULL) || (ret != *ex))
|
|
|
|
X509_EXTENSION_free(ret);
|
|
|
|
return (NULL);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-07-31 17:38:09 +00:00
|
|
|
int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if ((ex == NULL) || (obj == NULL))
|
|
|
|
return (0);
|
|
|
|
ASN1_OBJECT_free(ex->object);
|
|
|
|
ex->object = OBJ_dup(obj);
|
2016-05-11 22:29:22 +00:00
|
|
|
return ex->object != NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (ex == NULL)
|
|
|
|
return (0);
|
|
|
|
ex->critical = (crit) ? 0xFF : -1;
|
|
|
|
return (1);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ex == NULL)
|
|
|
|
return (0);
|
2015-10-11 19:44:07 +00:00
|
|
|
i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!i)
|
|
|
|
return (0);
|
|
|
|
return (1);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (ex == NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (ex->object);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (ex == NULL)
|
|
|
|
return (NULL);
|
2015-10-11 19:44:07 +00:00
|
|
|
return &ex->value;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-07-09 22:37:58 +00:00
|
|
|
int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (ex == NULL)
|
|
|
|
return (0);
|
|
|
|
if (ex->critical > 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|