summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjob <>2023-04-23 21:31:16 +0000
committerjob <>2023-04-23 21:31:16 +0000
commit4c20d9bfcef952ee5a32034ed25c792413465fde (patch)
tree6845cb5f4e2722640f5effb72fa388651a08a748
parentaedbb67da548df0585f48b0c49d758c2366fea7f (diff)
downloadopenbsd-4c20d9bfcef952ee5a32034ed25c792413465fde.tar.gz
openbsd-4c20d9bfcef952ee5a32034ed25c792413465fde.tar.bz2
openbsd-4c20d9bfcef952ee5a32034ed25c792413465fde.zip
Add compliance checks for the X.509 version field
Check whether the X.509 version is in the range of valid version values, and also checks whether the version is consistent with fields new to those versions (such as X.509 v3 extensions). X.690 section 11.5 states: "The encoding of a set value or a sequence value shall not include an encoding for any component value which is equal to its default value." However, enforcing version 1 (value 0) to be absent reportedly caused some issues as recent as July 2020, so accept version 1 even if it is explicitly encoded. OK tb@ beck@
-rw-r--r--src/lib/libcrypto/asn1/x_x509.c29
-rw-r--r--src/lib/libcrypto/x509/x509.h3
-rw-r--r--src/lib/libcrypto/x509/x509_err.c3
3 files changed, 31 insertions, 4 deletions
diff --git a/src/lib/libcrypto/asn1/x_x509.c b/src/lib/libcrypto/asn1/x_x509.c
index 227af88e82..87b714877f 100644
--- a/src/lib/libcrypto/asn1/x_x509.c
+++ b/src/lib/libcrypto/asn1/x_x509.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: x_x509.c,v 1.31 2022/11/26 16:08:50 tb Exp $ */ 1/* $OpenBSD: x_x509.c,v 1.32 2023/04/23 21:31:16 job Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -61,6 +61,7 @@
61#include <openssl/opensslconf.h> 61#include <openssl/opensslconf.h>
62 62
63#include <openssl/asn1t.h> 63#include <openssl/asn1t.h>
64#include <openssl/err.h>
64#include <openssl/evp.h> 65#include <openssl/evp.h>
65#include <openssl/x509.h> 66#include <openssl/x509.h>
66#include <openssl/x509v3.h> 67#include <openssl/x509v3.h>
@@ -194,10 +195,34 @@ x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
194 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); 195 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
195 break; 196 break;
196 197
197 case ASN1_OP_D2I_POST: 198 case ASN1_OP_D2I_POST: {
199 const ASN1_BIT_STRING *issuerUID = NULL, *subjectUID = NULL;
200 long version;
201
202 version = X509_get_version(ret);
203 /* accept 0 despite DER requiring omission of default values */
204 if (version < 0 || version > 2) {
205 X509error(X509_R_INVALID_VERSION);
206 return 0;
207 }
208
209 /* RFC 5280 section 4.1.2.8, these fields require v2 or v3 */
210 X509_get0_uids(ret, &issuerUID, &subjectUID);
211 if ((issuerUID != NULL || subjectUID != NULL) && version == 0) {
212 X509error(X509_R_INVALID_VERSION);
213 return 0;
214 }
215
216 /* RFC 5280 section 4.1.2.9, extensions require v3. */
217 if (X509_get_ext_count(ret) != 0 && version != 2) {
218 X509error(X509_R_INVALID_VERSION);
219 return 0;
220 }
221
198 free(ret->name); 222 free(ret->name);
199 ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0); 223 ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0);
200 break; 224 break;
225 }
201 226
202 case ASN1_OP_FREE_POST: 227 case ASN1_OP_FREE_POST:
203 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); 228 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
diff --git a/src/lib/libcrypto/x509/x509.h b/src/lib/libcrypto/x509/x509.h
index 9f87700c60..e8cedaae13 100644
--- a/src/lib/libcrypto/x509/x509.h
+++ b/src/lib/libcrypto/x509/x509.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: x509.h,v 1.96 2023/04/18 08:47:28 tb Exp $ */ 1/* $OpenBSD: x509.h,v 1.97 2023/04/23 21:31:16 job Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -1200,6 +1200,7 @@ void ERR_load_X509_strings(void);
1200#define X509_R_UNSUPPORTED_ALGORITHM 111 1200#define X509_R_UNSUPPORTED_ALGORITHM 111
1201#define X509_R_WRONG_LOOKUP_TYPE 112 1201#define X509_R_WRONG_LOOKUP_TYPE 112
1202#define X509_R_WRONG_TYPE 122 1202#define X509_R_WRONG_TYPE 122
1203#define X509_R_INVALID_VERSION 123
1203 1204
1204#ifdef __cplusplus 1205#ifdef __cplusplus
1205} 1206}
diff --git a/src/lib/libcrypto/x509/x509_err.c b/src/lib/libcrypto/x509/x509_err.c
index 272d2894d8..84328df62a 100644
--- a/src/lib/libcrypto/x509/x509_err.c
+++ b/src/lib/libcrypto/x509/x509_err.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: x509_err.c,v 1.19 2023/02/16 08:38:17 tb Exp $ */ 1/* $OpenBSD: x509_err.c,v 1.20 2023/04/23 21:31:16 job Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -104,6 +104,7 @@ static ERR_STRING_DATA X509_str_reasons[] = {
104 {ERR_REASON(X509_R_UNSUPPORTED_ALGORITHM), "unsupported algorithm"}, 104 {ERR_REASON(X509_R_UNSUPPORTED_ALGORITHM), "unsupported algorithm"},
105 {ERR_REASON(X509_R_WRONG_LOOKUP_TYPE) , "wrong lookup type"}, 105 {ERR_REASON(X509_R_WRONG_LOOKUP_TYPE) , "wrong lookup type"},
106 {ERR_REASON(X509_R_WRONG_TYPE) , "wrong type"}, 106 {ERR_REASON(X509_R_WRONG_TYPE) , "wrong type"},
107 {ERR_REASON(X509_R_INVALID_VERSION) , "wrong x509 version"},
107 {0, NULL} 108 {0, NULL}
108}; 109};
109 110