Skip to content

Commit 68b9d6b

Browse files
davidbennebeid
authored andcommitted
Document or unexport some more of x509.h
Get the remaining config APIs, extensions accessors, and the get1_email family. I'm not sure yet whether the various remaining extension-specific functions should get their own sections (probably), in which case, maybe we should move the accessors these into their sections? Put them with the rest of the certificate getters for now. As part of this, deduplicate the X509v3_KU_* and KU_* constants. See openssl/openssl#22955 Bug: 426 Change-Id: I31a9b887eb1e6cfa272f04d2ee80dbb5a9ed98f7 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/64256 Reviewed-by: Bob Beck <[email protected]> Commit-Queue: Bob Beck <[email protected]> (cherry picked from commit 314c2520eab450615d8e78df21169c090d6f51e5)
1 parent d3470a2 commit 68b9d6b

File tree

9 files changed

+315
-230
lines changed

9 files changed

+315
-230
lines changed

crypto/x509/internal.h

+10
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,16 @@ OPENSSL_EXPORT int GENERAL_NAME_cmp(const GENERAL_NAME *a,
543543
// |name|, or NULL if no such name is defined.
544544
const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name);
545545

546+
GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
547+
const X509V3_CTX *ctx, const CONF_VALUE *cnf);
548+
GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
549+
const X509V3_EXT_METHOD *method,
550+
const X509V3_CTX *ctx, const CONF_VALUE *cnf,
551+
int is_nc);
552+
GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method,
553+
const X509V3_CTX *ctx,
554+
const STACK_OF(CONF_VALUE) *nval);
555+
546556

547557
#if defined(__cplusplus)
548558
} // extern C

crypto/x509/v3_alt.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,10 @@ GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
446446
return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
447447
}
448448

449-
GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
450-
const X509V3_EXT_METHOD *method,
451-
const X509V3_CTX *ctx, int gen_type,
452-
const char *value, int is_nc) {
449+
static GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
450+
const X509V3_EXT_METHOD *method,
451+
const X509V3_CTX *ctx, int gen_type,
452+
const char *value, int is_nc) {
453453
if (!value) {
454454
OPENSSL_PUT_ERROR(X509V3, X509V3_R_MISSING_VALUE);
455455
return NULL;

crypto/x509/v3_info.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
#include <openssl/obj.h>
6868
#include <openssl/x509.h>
6969

70+
#include "internal.h"
71+
72+
7073
static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(
7174
const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret);
7275
static void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method,
@@ -206,8 +209,3 @@ static void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method,
206209
sk_ACCESS_DESCRIPTION_pop_free(ainfo, ACCESS_DESCRIPTION_free);
207210
return NULL;
208211
}
209-
210-
int i2a_ACCESS_DESCRIPTION(BIO *bp, const ACCESS_DESCRIPTION *a) {
211-
i2a_ASN1_OBJECT(bp, a->method);
212-
return 2;
213-
}

crypto/x509/v3_purp.c

+21-12
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ int x509v3_cache_extensions(X509 *x) {
500500
x->ex_flags |= EXFLAG_SI;
501501
// If SKID matches AKID also indicate self signed
502502
if (X509_check_akid(x, x->akid) == X509_V_OK &&
503-
!ku_reject(x, KU_KEY_CERT_SIGN)) {
503+
!ku_reject(x, X509v3_KU_KEY_CERT_SIGN)) {
504504
x->ex_flags |= EXFLAG_SS;
505505
}
506506
}
@@ -539,7 +539,7 @@ int x509v3_cache_extensions(X509 *x) {
539539
// otherwise.
540540
static int check_ca(const X509 *x) {
541541
// keyUsage if present should allow cert signing
542-
if (ku_reject(x, KU_KEY_CERT_SIGN)) {
542+
if (ku_reject(x, X509v3_KU_KEY_CERT_SIGN)) {
543543
return 0;
544544
}
545545
// Version 1 certificates are considered CAs and don't have extensions.
@@ -566,7 +566,7 @@ static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
566566
return check_ca(x);
567567
}
568568
// We need to do digital signatures or key agreement
569-
if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT)) {
569+
if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_AGREEMENT)) {
570570
return 0;
571571
}
572572
// nsCertType if present should allow SSL client use
@@ -579,7 +579,9 @@ static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
579579
// Key usage needed for TLS/SSL server: digital signature, encipherment or
580580
// key agreement. The ssl code can check this more thoroughly for individual
581581
// key types.
582-
#define KU_TLS (KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT)
582+
#define X509v3_KU_TLS \
583+
(X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_ENCIPHERMENT | \
584+
X509v3_KU_KEY_AGREEMENT)
583585

584586
static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
585587
int ca) {
@@ -593,7 +595,7 @@ static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
593595
if (ns_reject(x, NS_SSL_SERVER)) {
594596
return 0;
595597
}
596-
if (ku_reject(x, KU_TLS)) {
598+
if (ku_reject(x, X509v3_KU_TLS)) {
597599
return 0;
598600
}
599601

@@ -608,7 +610,7 @@ static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
608610
return ret;
609611
}
610612
// We need to encipher or Netscape complains
611-
if (ku_reject(x, KU_KEY_ENCIPHERMENT)) {
613+
if (ku_reject(x, X509v3_KU_KEY_ENCIPHERMENT)) {
612614
return 0;
613615
}
614616
return ret;
@@ -641,7 +643,7 @@ static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
641643
if (!ret || ca) {
642644
return ret;
643645
}
644-
if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION)) {
646+
if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_NON_REPUDIATION)) {
645647
return 0;
646648
}
647649
return ret;
@@ -654,7 +656,7 @@ static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
654656
if (!ret || ca) {
655657
return ret;
656658
}
657-
if (ku_reject(x, KU_KEY_ENCIPHERMENT)) {
659+
if (ku_reject(x, X509v3_KU_KEY_ENCIPHERMENT)) {
658660
return 0;
659661
}
660662
return ret;
@@ -665,7 +667,7 @@ static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
665667
if (ca) {
666668
return check_ca(x);
667669
}
668-
if (ku_reject(x, KU_CRL_SIGN)) {
670+
if (ku_reject(x, X509v3_KU_CRL_SIGN)) {
669671
return 0;
670672
}
671673
return 1;
@@ -696,8 +698,10 @@ static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
696698
// and/or nonRepudiation (other values are not consistent and shall
697699
// be rejected).
698700
if ((x->ex_flags & EXFLAG_KUSAGE) &&
699-
((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
700-
!(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)))) {
701+
((x->ex_kusage &
702+
~(X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)) ||
703+
!(x->ex_kusage &
704+
(X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)))) {
701705
return 0;
702706
}
703707

@@ -744,7 +748,7 @@ int X509_check_issued(X509 *issuer, X509 *subject) {
744748
}
745749
}
746750

747-
if (ku_reject(issuer, KU_KEY_CERT_SIGN)) {
751+
if (ku_reject(issuer, X509v3_KU_KEY_CERT_SIGN)) {
748752
return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
749753
}
750754
return X509_V_OK;
@@ -803,6 +807,9 @@ uint32_t X509_get_key_usage(X509 *x) {
803807
if (x->ex_flags & EXFLAG_KUSAGE) {
804808
return x->ex_kusage;
805809
}
810+
// If there is no extension, key usage is unconstrained, so set all bits to
811+
// one. Note that, although we use |UINT32_MAX|, |ex_kusage| only contains the
812+
// first 16 bits when the extension is present.
806813
return UINT32_MAX;
807814
}
808815

@@ -813,6 +820,8 @@ uint32_t X509_get_extended_key_usage(X509 *x) {
813820
if (x->ex_flags & EXFLAG_XKUSAGE) {
814821
return x->ex_xkusage;
815822
}
823+
// If there is no extension, extended key usage is unconstrained, so set all
824+
// bits to one.
816825
return UINT32_MAX;
817826
}
818827

crypto/x509/v3_utl.c

+5-9
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ static int sk_strcmp(const char *const *a, const char *const *b) {
555555
return strcmp(*a, *b);
556556
}
557557

558-
STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x) {
558+
STACK_OF(OPENSSL_STRING) *X509_get1_email(const X509 *x) {
559559
GENERAL_NAMES *gens;
560560
STACK_OF(OPENSSL_STRING) *ret;
561561

@@ -565,7 +565,7 @@ STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x) {
565565
return ret;
566566
}
567567

568-
STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x) {
568+
STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(const X509 *x) {
569569
AUTHORITY_INFO_ACCESS *info;
570570
STACK_OF(OPENSSL_STRING) *ret = NULL;
571571
size_t i;
@@ -588,7 +588,7 @@ STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x) {
588588
return ret;
589589
}
590590

591-
STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x) {
591+
STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(const X509_REQ *x) {
592592
GENERAL_NAMES *gens;
593593
STACK_OF(X509_EXTENSION) *exts;
594594
STACK_OF(OPENSSL_STRING) *ret;
@@ -1155,12 +1155,8 @@ ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc) {
11551155
return ret;
11561156

11571157
err:
1158-
if (iptmp) {
1159-
OPENSSL_free(iptmp);
1160-
}
1161-
if (ret) {
1162-
ASN1_OCTET_STRING_free(ret);
1163-
}
1158+
OPENSSL_free(iptmp);
1159+
ASN1_OCTET_STRING_free(ret);
11641160
return NULL;
11651161
}
11661162

crypto/x509/x509_req.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ int X509_REQ_extension_nid(int req_nid) {
123123
return req_nid == NID_ext_req || req_nid == NID_ms_ext_req;
124124
}
125125

126-
STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req) {
126+
STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(const X509_REQ *req) {
127127
if (req == NULL || req->req_info == NULL) {
128128
return NULL;
129129
}
@@ -136,8 +136,10 @@ STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req) {
136136
return NULL;
137137
}
138138

139-
X509_ATTRIBUTE *attr = X509_REQ_get_attr(req, idx);
140-
ASN1_TYPE *ext = X509_ATTRIBUTE_get0_type(attr, 0);
139+
const X509_ATTRIBUTE *attr = X509_REQ_get_attr(req, idx);
140+
// TODO(davidben): |X509_ATTRIBUTE_get0_type| is not const-correct. It should
141+
// take and return a const pointer.
142+
const ASN1_TYPE *ext = X509_ATTRIBUTE_get0_type((X509_ATTRIBUTE *)attr, 0);
141143
if (!ext || ext->type != V_ASN1_SEQUENCE) {
142144
return NULL;
143145
}

crypto/x509/x509_vfy.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) {
12091209
if (issuer) {
12101210
// Check for cRLSign bit if keyUsage present
12111211
if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1212-
!(issuer->ex_kusage & KU_CRL_SIGN)) {
1212+
!(issuer->ex_kusage & X509v3_KU_CRL_SIGN)) {
12131213
ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
12141214
ok = ctx->verify_cb(0, ctx);
12151215
if (!ok) {

include/openssl/base.h

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ typedef struct trust_token_client_st TRUST_TOKEN_CLIENT;
378378
typedef struct trust_token_issuer_st TRUST_TOKEN_ISSUER;
379379
typedef struct trust_token_method_st TRUST_TOKEN_METHOD;
380380
typedef struct v3_ext_ctx X509V3_CTX;
381+
typedef struct v3_ext_method X509V3_EXT_METHOD;
381382
typedef struct x509_attributes_st X509_ATTRIBUTE;
382383
typedef struct x509_lookup_st X509_LOOKUP;
383384
typedef struct x509_lookup_method_st X509_LOOKUP_METHOD;

0 commit comments

Comments
 (0)