mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33a20e650c | ||
|
|
65f8c60ab3 | ||
|
|
1863cc9dcf | ||
|
|
6d9c36b4be | ||
|
|
a56d70a286 | ||
|
|
3ea0bdb8fd | ||
|
|
d1047c1c9a | ||
|
|
401a7a8ba7 | ||
|
|
944428373d | ||
|
|
5a1b404183 | ||
|
|
3c4009af05 | ||
|
|
22a06540cc | ||
|
|
7d7419ebba | ||
|
|
f2125b625f | ||
|
|
959c1fe152 | ||
|
|
36d5ec4458 | ||
|
|
6be84be0bd | ||
|
|
25a68d63b7 | ||
|
|
5d8b14d227 | ||
|
|
da0fe44138 |
No files matched your search
+2
-2
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version:5.5
|
||||
// swift-tools-version:5.6
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
@@ -20,7 +20,7 @@
|
||||
// Sources/CCryptoBoringSSL directory. The source repository is at
|
||||
// https://boringssl.googlesource.com/boringssl.
|
||||
//
|
||||
// BoringSSL Commit: 45b8d7bbd771cbf7e116db2ba1f1cc7af959497e
|
||||
// BoringSSL Commit: abfd5ebc87ddca0fab9fca067c9d7edbc355eae8
|
||||
|
||||
import PackageDescription
|
||||
|
||||
|
||||
@@ -108,13 +108,14 @@ If you believe you have identified a vulnerability in Swift Crypto, please [repo
|
||||
|
||||
### Swift versions
|
||||
|
||||
The most recent versions of Swift Crypto support Swift 5.5 and newer. The minimum Swift version supported by Swift Crypto releases are detailed below:
|
||||
The most recent versions of Swift Crypto support Swift 5.6 and newer. The minimum Swift version supported by Swift Crypto releases are detailed below:
|
||||
|
||||
Swift Crypto | Minimum Swift Version
|
||||
------------------|----------------------
|
||||
`2.0.0 ..< 2.1.0` | 5.2
|
||||
`2.1.0 ..< 2.2.0` | 5.4
|
||||
`2.2.0 ...` | 5.5
|
||||
`2.2.0 ..< 2.4.2` | 5.5
|
||||
`2.4.2 ...` | 5.6
|
||||
|
||||
### Compatibility
|
||||
|
||||
|
||||
+68
-71
@@ -82,7 +82,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/cpu_arm_freebsd.c"
|
||||
"crypto/cpu_arm_linux.c"
|
||||
"crypto/cpu_intel.c"
|
||||
"crypto/cpu_ppc64le.c"
|
||||
"crypto/crypto.c"
|
||||
"crypto/curve25519/curve25519.c"
|
||||
"crypto/curve25519/spake25519.c"
|
||||
@@ -190,7 +189,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/fipsmodule/self_check/fips.c"
|
||||
"crypto/fipsmodule/self_check/self_check.c"
|
||||
"crypto/fipsmodule/service_indicator/service_indicator.c"
|
||||
"crypto/fipsmodule/sha/sha1-altivec.c"
|
||||
"crypto/fipsmodule/sha/sha1.c"
|
||||
"crypto/fipsmodule/sha/sha256.c"
|
||||
"crypto/fipsmodule/sha/sha512.c"
|
||||
@@ -198,6 +196,8 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/hkdf/hkdf.c"
|
||||
"crypto/hpke/hpke.c"
|
||||
"crypto/hrss/hrss.c"
|
||||
"crypto/kyber/keccak.c"
|
||||
"crypto/kyber/kyber.c"
|
||||
"crypto/lhash/lhash.c"
|
||||
"crypto/mem.c"
|
||||
"crypto/obj/obj.c"
|
||||
@@ -229,6 +229,7 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/refcount_c11.c"
|
||||
"crypto/refcount_lock.c"
|
||||
"crypto/rsa_extra/rsa_asn1.c"
|
||||
"crypto/rsa_extra/rsa_crypt.c"
|
||||
"crypto/rsa_extra/rsa_print.c"
|
||||
"crypto/siphash/siphash.c"
|
||||
"crypto/stack/stack.c"
|
||||
@@ -248,6 +249,7 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/x509/by_file.c"
|
||||
"crypto/x509/i2d_pr.c"
|
||||
"crypto/x509/name_print.c"
|
||||
"crypto/x509/policy.c"
|
||||
"crypto/x509/rsa_pss.c"
|
||||
"crypto/x509/t_crl.c"
|
||||
"crypto/x509/t_req.c"
|
||||
@@ -287,11 +289,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/x509/x_val.c"
|
||||
"crypto/x509/x_x509.c"
|
||||
"crypto/x509/x_x509a.c"
|
||||
"crypto/x509v3/pcy_cache.c"
|
||||
"crypto/x509v3/pcy_data.c"
|
||||
"crypto/x509v3/pcy_map.c"
|
||||
"crypto/x509v3/pcy_node.c"
|
||||
"crypto/x509v3/pcy_tree.c"
|
||||
"crypto/x509v3/v3_akey.c"
|
||||
"crypto/x509v3/v3_akeya.c"
|
||||
"crypto/x509v3/v3_alt.c"
|
||||
@@ -309,8 +306,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/x509v3/v3_lib.c"
|
||||
"crypto/x509v3/v3_ncons.c"
|
||||
"crypto/x509v3/v3_ocsp.c"
|
||||
"crypto/x509v3/v3_pci.c"
|
||||
"crypto/x509v3/v3_pcia.c"
|
||||
"crypto/x509v3/v3_pcons.c"
|
||||
"crypto/x509v3/v3_pmaps.c"
|
||||
"crypto/x509v3/v3_prn.c"
|
||||
@@ -320,74 +315,76 @@ add_library(CCryptoBoringSSL STATIC
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-x86_64.mac.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64.mac.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm.mac.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm.mac.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2.mac.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5.mac.x86_64.S)
|
||||
crypto/chacha/chacha-x86_64-mac.mac.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64-mac.mac.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5-mac.mac.x86_64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android" AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-x86_64.linux.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64.linux.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm.linux.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm.linux.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2.linux.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5.linux.x86_64.S)
|
||||
crypto/chacha/chacha-x86_64-linux.linux.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64-linux.linux.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5-linux.linux.x86_64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-armv8.ios.aarch64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armx64.ios.aarch64.S
|
||||
crypto/fipsmodule/aesv8-gcm-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont.ios.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S
|
||||
crypto/fipsmodule/p256-armv8-asm.ios.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm.ios.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8.ios.aarch64.S)
|
||||
crypto/chacha/chacha-armv8-ios.ios.aarch64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/aesv8-gcm-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/bn-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/p256-armv8-asm-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8-ios.ios.aarch64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android" AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-armv8.linux.aarch64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armx64.linux.aarch64.S
|
||||
crypto/fipsmodule/aesv8-gcm-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont.linux.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S
|
||||
crypto/fipsmodule/p256-armv8-asm.linux.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm.linux.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8.linux.aarch64.S)
|
||||
crypto/chacha/chacha-armv8-linux.linux.aarch64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/aesv8-gcm-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/bn-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/p256-armv8-asm-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8-linux.linux.aarch64.S)
|
||||
endif()
|
||||
|
||||
target_include_directories(CCryptoBoringSSL PUBLIC
|
||||
|
||||
@@ -184,7 +184,6 @@ ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
|
||||
if (len > 0) {
|
||||
s = OPENSSL_memdup(p, len);
|
||||
if (s == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
p += len;
|
||||
@@ -236,7 +235,6 @@ int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) {
|
||||
c = (unsigned char *)OPENSSL_realloc(a->data, w + 1);
|
||||
}
|
||||
if (c == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
if (w + 1 - a->length > 0) {
|
||||
|
||||
@@ -56,65 +56,40 @@
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **pp) {
|
||||
int r;
|
||||
unsigned char *p, *allocated = NULL;
|
||||
#include "../bytestring/internal.h"
|
||||
|
||||
r = ASN1_object_size(0, 1, V_ASN1_BOOLEAN);
|
||||
if (pp == NULL) {
|
||||
return r;
|
||||
|
||||
int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **outp) {
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 3) || //
|
||||
!CBB_add_asn1_bool(&cbb, a != ASN1_BOOLEAN_FALSE)) {
|
||||
CBB_cleanup(&cbb);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(r)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, 1, V_ASN1_BOOLEAN, V_ASN1_UNIVERSAL);
|
||||
*p = a ? 0xff : 0x00;
|
||||
|
||||
// If a new buffer was allocated, just return it back.
|
||||
// If not, return the incremented buffer pointer.
|
||||
*pp = allocated != NULL ? allocated : p + 1;
|
||||
return r;
|
||||
return CBB_finish_i2d(&cbb, outp);
|
||||
}
|
||||
|
||||
ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *a, const unsigned char **pp,
|
||||
long length) {
|
||||
const unsigned char *p = *pp;
|
||||
long len;
|
||||
int inf, tag, xclass;
|
||||
inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
return -1;
|
||||
ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *out, const unsigned char **inp,
|
||||
long len) {
|
||||
if (len < 0) {
|
||||
return ASN1_BOOLEAN_NONE;
|
||||
}
|
||||
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return -1;
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
int val;
|
||||
if (!CBS_get_asn1_bool(&cbs, &val)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
|
||||
return ASN1_BOOLEAN_NONE;
|
||||
}
|
||||
|
||||
if (tag != V_ASN1_BOOLEAN || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_A_BOOLEAN);
|
||||
return -1;
|
||||
ASN1_BOOLEAN ret = val ? ASN1_BOOLEAN_TRUE : ASN1_BOOLEAN_FALSE;
|
||||
if (out != NULL) {
|
||||
*out = ret;
|
||||
}
|
||||
|
||||
if (len != 1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
ASN1_BOOLEAN ret = (ASN1_BOOLEAN) * (p++);
|
||||
if (a != NULL) {
|
||||
(*a) = ret;
|
||||
}
|
||||
*pp = p;
|
||||
*inp = CBS_data(&cbs);
|
||||
return ret;
|
||||
}
|
||||
@@ -75,7 +75,6 @@ void *ASN1_item_dup(const ASN1_ITEM *it, void *x) {
|
||||
|
||||
i = ASN1_item_i2d(x, &b, it);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
p = b;
|
||||
|
||||
@@ -58,6 +58,7 @@
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_time.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
@@ -98,15 +99,15 @@ int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) {
|
||||
}
|
||||
|
||||
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
|
||||
time_t t) {
|
||||
return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
|
||||
int64_t posix_time) {
|
||||
return ASN1_GENERALIZEDTIME_adj(s, posix_time, 0, 0);
|
||||
}
|
||||
|
||||
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
|
||||
time_t t, int offset_day,
|
||||
int64_t posix_time, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm data;
|
||||
if (!OPENSSL_gmtime(&t, &data)) {
|
||||
if (!OPENSSL_posix_to_tm(posix_time, &data)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,6 @@ int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x) {
|
||||
unsigned char *b = NULL;
|
||||
int n = ASN1_item_i2d(x, &b, it);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -122,14 +122,17 @@ int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) {
|
||||
// |ASN1_INTEGER|s should be represented minimally, but it is possible to
|
||||
// construct invalid ones. Skip leading zeros so this does not produce an
|
||||
// invalid encoding or break invariants.
|
||||
int start = 0;
|
||||
while (start < in->length && in->data[start] == 0) {
|
||||
start++;
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, in->data, in->length);
|
||||
while (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0) {
|
||||
CBS_skip(&cbs, 1);
|
||||
}
|
||||
|
||||
int is_negative = (in->type & V_ASN1_NEG) != 0;
|
||||
int pad;
|
||||
if (start >= in->length) {
|
||||
size_t pad;
|
||||
CBS copy = cbs;
|
||||
uint8_t msb;
|
||||
if (!CBS_get_u8(©, &msb)) {
|
||||
// Zero is represented as a single byte.
|
||||
is_negative = 0;
|
||||
pad = 1;
|
||||
@@ -138,20 +141,19 @@ int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) {
|
||||
// through 0x00...01 and need an extra byte to be negative.
|
||||
// 0x01...00 through 0x80...00 have a two's complement of 0xfe...ff
|
||||
// through 0x80...00 and can be negated as-is.
|
||||
pad = in->data[start] > 0x80 ||
|
||||
(in->data[start] == 0x80 &&
|
||||
!is_all_zeros(in->data + start + 1, in->length - start - 1));
|
||||
pad = msb > 0x80 ||
|
||||
(msb == 0x80 && !is_all_zeros(CBS_data(©), CBS_len(©)));
|
||||
} else {
|
||||
// If the high bit is set, the signed representation needs an extra
|
||||
// byte to be positive.
|
||||
pad = (in->data[start] & 0x80) != 0;
|
||||
pad = (msb & 0x80) != 0;
|
||||
}
|
||||
|
||||
if (in->length - start > INT_MAX - pad) {
|
||||
if (CBS_len(&cbs) > INT_MAX - pad) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
int len = pad + in->length - start;
|
||||
int len = (int)(pad + CBS_len(&cbs));
|
||||
assert(len > 0);
|
||||
if (outp == NULL) {
|
||||
return len;
|
||||
@@ -160,7 +162,7 @@ int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) {
|
||||
if (pad) {
|
||||
(*outp)[0] = 0;
|
||||
}
|
||||
OPENSSL_memcpy(*outp + pad, in->data + start, in->length - start);
|
||||
OPENSSL_memcpy(*outp + pad, CBS_data(&cbs), CBS_len(&cbs));
|
||||
if (is_negative) {
|
||||
negate_twos_complement(*outp, len);
|
||||
assert((*outp)[0] >= 0x80);
|
||||
|
||||
@@ -85,11 +85,6 @@ OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UTF8STRING)
|
||||
int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
int inform, unsigned long mask, long minsize,
|
||||
long maxsize) {
|
||||
int str_type;
|
||||
char free_out;
|
||||
ASN1_STRING *dest;
|
||||
size_t nchar = 0;
|
||||
char strbuf[32];
|
||||
if (len == -1) {
|
||||
len = strlen((const char *)in);
|
||||
}
|
||||
@@ -128,7 +123,7 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
// Check |minsize| and |maxsize| and work out the minimal type, if any.
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, in, len);
|
||||
size_t utf8_len = 0;
|
||||
size_t utf8_len = 0, nchar = 0;
|
||||
while (CBS_len(&cbs) != 0) {
|
||||
uint32_t c;
|
||||
if (!decode_func(&cbs, &c)) {
|
||||
@@ -167,23 +162,21 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
|
||||
nchar++;
|
||||
utf8_len += cbb_get_utf8_len(c);
|
||||
if (maxsize > 0 && nchar > (size_t)maxsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
ERR_add_error_dataf("maxsize=%ld", maxsize);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (minsize > 0 && nchar < (size_t)minsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
|
||||
BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
|
||||
ERR_add_error_data(2, "minsize=", strbuf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (maxsize > 0 && nchar > (size_t)maxsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
|
||||
ERR_add_error_data(2, "maxsize=", strbuf);
|
||||
ERR_add_error_dataf("minsize=%ld", minsize);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Now work out output format and string type
|
||||
int str_type;
|
||||
int (*encode_func)(CBB *, uint32_t) = cbb_add_latin1;
|
||||
size_t size_estimate = nchar;
|
||||
int outform = MBSTRING_ASC;
|
||||
@@ -216,37 +209,31 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
if (!out) {
|
||||
return str_type;
|
||||
}
|
||||
|
||||
int free_dest = 0;
|
||||
ASN1_STRING *dest;
|
||||
if (*out) {
|
||||
free_out = 0;
|
||||
dest = *out;
|
||||
if (dest->data) {
|
||||
dest->length = 0;
|
||||
OPENSSL_free(dest->data);
|
||||
dest->data = NULL;
|
||||
}
|
||||
dest->type = str_type;
|
||||
} else {
|
||||
free_out = 1;
|
||||
free_dest = 1;
|
||||
dest = ASN1_STRING_type_new(str_type);
|
||||
if (!dest) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
*out = dest;
|
||||
}
|
||||
|
||||
// If both the same type just copy across
|
||||
if (inform == outform) {
|
||||
if (!ASN1_STRING_set(dest, in, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
return str_type;
|
||||
}
|
||||
|
||||
CBB cbb;
|
||||
CBB_zero(&cbb);
|
||||
// If both the same type just copy across
|
||||
if (inform == outform) {
|
||||
if (!ASN1_STRING_set(dest, in, len)) {
|
||||
goto err;
|
||||
}
|
||||
dest->type = str_type;
|
||||
*out = dest;
|
||||
return str_type;
|
||||
}
|
||||
if (!CBB_init(&cbb, size_estimate + 1)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
CBS_init(&cbs, in, len);
|
||||
@@ -267,12 +254,13 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
OPENSSL_free(data);
|
||||
goto err;
|
||||
}
|
||||
dest->length = (int)(data_len - 1);
|
||||
dest->data = data;
|
||||
dest->type = str_type;
|
||||
ASN1_STRING_set0(dest, data, (int)data_len - 1);
|
||||
*out = dest;
|
||||
return str_type;
|
||||
|
||||
err:
|
||||
if (free_out) {
|
||||
if (free_dest) {
|
||||
ASN1_STRING_free(dest);
|
||||
}
|
||||
CBB_cleanup(&cbb);
|
||||
@@ -283,10 +271,7 @@ int asn1_is_printable(uint32_t value) {
|
||||
if (value > 0x7f) {
|
||||
return 0;
|
||||
}
|
||||
// Note we cannot use |isalnum| because it is locale-dependent.
|
||||
return ('a' <= value && value <= 'z') || //
|
||||
('A' <= value && value <= 'Z') || //
|
||||
('0' <= value && value <= '9') || //
|
||||
return OPENSSL_isalnum(value) || //
|
||||
value == ' ' || value == '\'' || value == '(' || value == ')' ||
|
||||
value == '+' || value == ',' || value == '-' || value == '.' ||
|
||||
value == '/' || value == ':' || value == '=' || value == '?';
|
||||
|
||||
@@ -59,47 +59,36 @@
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "../bytestring/internal.h"
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) {
|
||||
if (a == NULL) {
|
||||
int i2d_ASN1_OBJECT(const ASN1_OBJECT *in, unsigned char **outp) {
|
||||
if (in == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (a->length == 0) {
|
||||
if (in->length <= 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
|
||||
if (pp == NULL || objsize == -1) {
|
||||
return objsize;
|
||||
CBB cbb, child;
|
||||
if (!CBB_init(&cbb, (size_t)in->length + 2) ||
|
||||
!CBB_add_asn1(&cbb, &child, CBS_ASN1_OBJECT) ||
|
||||
!CBB_add_bytes(&child, in->data, in->length)) {
|
||||
CBB_cleanup(&cbb);
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char *p, *allocated = NULL;
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
|
||||
OPENSSL_memcpy(p, a->data, a->length);
|
||||
|
||||
// If a new buffer was allocated, just return it back.
|
||||
// If not, return the incremented buffer pointer.
|
||||
*pp = allocated != NULL ? allocated : p + a->length;
|
||||
return objsize;
|
||||
return CBB_finish_i2d(&cbb, outp);
|
||||
}
|
||||
|
||||
int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a) {
|
||||
@@ -141,106 +130,55 @@ int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
long length) {
|
||||
long len;
|
||||
int tag, xclass;
|
||||
const unsigned char *p = *pp;
|
||||
int inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **out, const unsigned char **inp,
|
||||
long len) {
|
||||
if (len < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
CBS cbs, child;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
if (!CBS_get_asn1(&cbs, &child, CBS_ASN1_OBJECT)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (tag != V_ASN1_OBJECT || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_AN_OBJECT);
|
||||
return NULL;
|
||||
}
|
||||
ASN1_OBJECT *ret = c2i_ASN1_OBJECT(a, &p, len);
|
||||
if (ret) {
|
||||
*pp = p;
|
||||
const uint8_t *contents = CBS_data(&child);
|
||||
ASN1_OBJECT *ret = c2i_ASN1_OBJECT(out, &contents, CBS_len(&child));
|
||||
if (ret != NULL) {
|
||||
// |c2i_ASN1_OBJECT| should have consumed the entire input.
|
||||
assert(CBS_data(&cbs) == contents);
|
||||
*inp = CBS_data(&cbs);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **out, const unsigned char **inp,
|
||||
long len) {
|
||||
ASN1_OBJECT *ret = NULL;
|
||||
const unsigned char *p;
|
||||
unsigned char *data;
|
||||
int i, length;
|
||||
|
||||
// Sanity check OID encoding. Need at least one content octet. MSB must
|
||||
// be clear in the last octet. can't have leading 0x80 in subidentifiers,
|
||||
// see: X.690 8.19.2
|
||||
if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
|
||||
p[len - 1] & 0x80) {
|
||||
if (len < 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
// Now 0 < len <= INT_MAX, so the cast is safe.
|
||||
length = (int)len;
|
||||
for (i = 0; i < length; i++, p++) {
|
||||
if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
if (!CBS_is_valid_asn1_oid(&cbs)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((a == NULL) || ((*a) == NULL) ||
|
||||
!((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
|
||||
if ((ret = ASN1_OBJECT_new()) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret = (*a);
|
||||
ASN1_OBJECT *ret = ASN1_OBJECT_create(NID_undef, *inp, (size_t)len,
|
||||
/*sn=*/NULL, /*ln=*/NULL);
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p = *pp;
|
||||
// detach data from object
|
||||
data = (unsigned char *)ret->data;
|
||||
ret->data = NULL;
|
||||
// once detached we can change it
|
||||
if ((data == NULL) || (ret->length < length)) {
|
||||
ret->length = 0;
|
||||
OPENSSL_free(data);
|
||||
data = (unsigned char *)OPENSSL_malloc(length);
|
||||
if (data == NULL) {
|
||||
i = ERR_R_MALLOC_FAILURE;
|
||||
goto err;
|
||||
}
|
||||
ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
if (out != NULL) {
|
||||
ASN1_OBJECT_free(*out);
|
||||
*out = ret;
|
||||
}
|
||||
OPENSSL_memcpy(data, p, length);
|
||||
// If there are dynamic strings, free them here, and clear the flag
|
||||
if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
|
||||
OPENSSL_free((char *)ret->sn);
|
||||
OPENSSL_free((char *)ret->ln);
|
||||
ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
|
||||
}
|
||||
// reattach data to object, after which it remains const
|
||||
ret->data = data;
|
||||
ret->length = length;
|
||||
ret->sn = NULL;
|
||||
ret->ln = NULL;
|
||||
p += length;
|
||||
|
||||
if (a != NULL) {
|
||||
(*a) = ret;
|
||||
}
|
||||
*pp = p;
|
||||
*inp += len; // All bytes were consumed.
|
||||
return ret;
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, i);
|
||||
if ((ret != NULL) && ((a == NULL) || (*a != ret))) {
|
||||
ASN1_OBJECT_free(ret);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_new(void) {
|
||||
@@ -248,7 +186,6 @@ ASN1_OBJECT *ASN1_OBJECT_new(void) {
|
||||
|
||||
ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
ret->length = 0;
|
||||
@@ -279,16 +216,20 @@ void ASN1_OBJECT_free(ASN1_OBJECT *a) {
|
||||
}
|
||||
}
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, int len,
|
||||
ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, size_t len,
|
||||
const char *sn, const char *ln) {
|
||||
ASN1_OBJECT o;
|
||||
if (len > INT_MAX) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ASN1_OBJECT o;
|
||||
o.sn = sn;
|
||||
o.ln = ln;
|
||||
o.data = data;
|
||||
o.nid = nid;
|
||||
o.length = len;
|
||||
o.length = (int)len;
|
||||
o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
|
||||
ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
return (OBJ_dup(&o));
|
||||
return OBJ_dup(&o);
|
||||
}
|
||||
@@ -74,29 +74,31 @@ IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
|
||||
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const(ASN1_TIME)
|
||||
|
||||
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t) {
|
||||
return ASN1_TIME_adj(s, t, 0, 0);
|
||||
ASN1_TIME *ASN1_TIME_set_posix(ASN1_TIME *s, int64_t posix_time) {
|
||||
return ASN1_TIME_adj(s, posix_time, 0, 0);
|
||||
}
|
||||
|
||||
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm *ts;
|
||||
struct tm data;
|
||||
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t time) {
|
||||
return ASN1_TIME_adj(s, time, 0, 0);
|
||||
}
|
||||
|
||||
ts = OPENSSL_gmtime(&t, &data);
|
||||
if (ts == NULL) {
|
||||
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, int64_t posix_time, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm tm;
|
||||
|
||||
if (!OPENSSL_posix_to_tm(posix_time, &tm)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ERROR_GETTING_TIME);
|
||||
return NULL;
|
||||
}
|
||||
if (offset_day || offset_sec) {
|
||||
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) {
|
||||
if (!OPENSSL_gmtime_adj(&tm, offset_day, offset_sec)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if ((ts->tm_year >= 50) && (ts->tm_year < 150)) {
|
||||
return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
|
||||
if ((tm.tm_year >= 50) && (tm.tm_year < 150)) {
|
||||
return ASN1_UTCTIME_adj(s, posix_time, offset_day, offset_sec);
|
||||
}
|
||||
return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
|
||||
return ASN1_GENERALIZEDTIME_adj(s, posix_time, offset_day, offset_sec);
|
||||
}
|
||||
|
||||
int ASN1_TIME_check(const ASN1_TIME *t) {
|
||||
@@ -172,9 +174,7 @@ int ASN1_TIME_set_string(ASN1_TIME *s, const char *str) {
|
||||
static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t,
|
||||
int allow_timezone_offset) {
|
||||
if (t == NULL) {
|
||||
time_t now_t;
|
||||
time(&now_t);
|
||||
if (OPENSSL_gmtime(&now_t, tm)) {
|
||||
if (OPENSSL_posix_to_tm(time(NULL), tm)) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -65,31 +65,65 @@
|
||||
|
||||
|
||||
int ASN1_TYPE_get(const ASN1_TYPE *a) {
|
||||
if (a->type == V_ASN1_BOOLEAN || a->type == V_ASN1_NULL ||
|
||||
a->value.ptr != NULL) {
|
||||
return a->type;
|
||||
switch (a->type) {
|
||||
case V_ASN1_NULL:
|
||||
case V_ASN1_BOOLEAN:
|
||||
return a->type;
|
||||
case V_ASN1_OBJECT:
|
||||
return a->value.object != NULL ? a->type : 0;
|
||||
default:
|
||||
return a->value.asn1_string != NULL ? a->type : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a) {
|
||||
if (a->type == V_ASN1_BOOLEAN) {
|
||||
return a->value.boolean ? (void *)0xff : NULL;
|
||||
switch (a->type) {
|
||||
case V_ASN1_NULL:
|
||||
return NULL;
|
||||
case V_ASN1_BOOLEAN:
|
||||
return a->value.boolean ? (void *)0xff : NULL;
|
||||
case V_ASN1_OBJECT:
|
||||
return a->value.object;
|
||||
default:
|
||||
return a->value.asn1_string;
|
||||
}
|
||||
if (a->type == V_ASN1_NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void asn1_type_cleanup(ASN1_TYPE *a) {
|
||||
switch (a->type) {
|
||||
case V_ASN1_NULL:
|
||||
a->value.ptr = NULL;
|
||||
break;
|
||||
case V_ASN1_BOOLEAN:
|
||||
a->value.boolean = ASN1_BOOLEAN_NONE;
|
||||
break;
|
||||
case V_ASN1_OBJECT:
|
||||
ASN1_OBJECT_free(a->value.object);
|
||||
a->value.object = NULL;
|
||||
break;
|
||||
default:
|
||||
ASN1_STRING_free(a->value.asn1_string);
|
||||
a->value.asn1_string = NULL;
|
||||
break;
|
||||
}
|
||||
return a->value.ptr;
|
||||
}
|
||||
|
||||
void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value) {
|
||||
ASN1_TYPE **tmp_a = &a;
|
||||
ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
|
||||
asn1_type_cleanup(a);
|
||||
a->type = type;
|
||||
if (type == V_ASN1_BOOLEAN) {
|
||||
a->value.boolean = value ? 0xff : 0;
|
||||
} else {
|
||||
a->value.ptr = value;
|
||||
switch (type) {
|
||||
case V_ASN1_NULL:
|
||||
a->value.ptr = NULL;
|
||||
break;
|
||||
case V_ASN1_BOOLEAN:
|
||||
a->value.boolean = value ? ASN1_BOOLEAN_TRUE : ASN1_BOOLEAN_FALSE;
|
||||
break;
|
||||
case V_ASN1_OBJECT:
|
||||
a->value.object = value;
|
||||
break;
|
||||
default:
|
||||
a->value.asn1_string = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,7 @@
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_time.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
@@ -98,14 +99,14 @@ int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t) {
|
||||
return ASN1_UTCTIME_adj(s, t, 0, 0);
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, int64_t posix_time) {
|
||||
return ASN1_UTCTIME_adj(s, posix_time, 0, 0);
|
||||
}
|
||||
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, int offset_day,
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, int64_t posix_time, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm data;
|
||||
if (!OPENSSL_gmtime(&t, &data)) {
|
||||
if (!OPENSSL_posix_to_tm(posix_time, &data)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -151,7 +152,7 @@ int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (!OPENSSL_gmtime(&t, &ttm)) {
|
||||
if (!OPENSSL_posix_to_tm(t, &ttm)) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
|
||||
@@ -111,20 +111,10 @@ int ASN1_get_object(const unsigned char **inp, long *out_len, int *out_tag,
|
||||
return 0x80;
|
||||
}
|
||||
|
||||
// TODO(https://crbug.com/boringssl/354): This should use |CBS_get_asn1| to
|
||||
// reject non-minimal lengths, which are only allowed in BER. However,
|
||||
// Android sometimes needs allow a non-minimal length in certificate
|
||||
// signature fields (see b/18228011). Make this only apply to that field,
|
||||
// while requiring DER elsewhere. Better yet, it should be limited to an
|
||||
// preprocessing step in that part of Android.
|
||||
CBS_ASN1_TAG tag;
|
||||
size_t header_len;
|
||||
int indefinite;
|
||||
CBS cbs, body;
|
||||
CBS_init(&cbs, *inp, (size_t)in_len);
|
||||
if (!CBS_get_any_ber_asn1_element(&cbs, &body, &tag, &header_len,
|
||||
/*out_ber_found=*/NULL, &indefinite) ||
|
||||
indefinite || !CBS_skip(&body, header_len) ||
|
||||
if (!CBS_get_any_asn1(&cbs, &body, &tag) ||
|
||||
// Bound the length to comfortably fit in an int. Lengths in this
|
||||
// module often switch between int and long without overflow checks.
|
||||
CBS_len(&body) > INT_MAX / 2) {
|
||||
@@ -299,7 +289,6 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, ossl_ssize_t len_s) {
|
||||
}
|
||||
|
||||
if (str->data == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
str->data = c;
|
||||
return 0;
|
||||
}
|
||||
@@ -331,7 +320,6 @@ ASN1_STRING *ASN1_STRING_type_new(int type) {
|
||||
|
||||
ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
ret->length = 0;
|
||||
|
||||
@@ -72,7 +72,6 @@ ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **out) {
|
||||
if (out == NULL || *out == NULL) {
|
||||
ret = ASN1_STRING_new();
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_free(new_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef OPENSSL_HEADER_ASN1_ASN1_LOCL_H
|
||||
#define OPENSSL_HEADER_ASN1_ASN1_LOCL_H
|
||||
#ifndef OPENSSL_HEADER_ASN1_INTERNAL_H
|
||||
#define OPENSSL_HEADER_ASN1_INTERNAL_H
|
||||
|
||||
#include <time.h>
|
||||
|
||||
@@ -122,20 +122,16 @@ struct asn1_object_st {
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_new(void);
|
||||
|
||||
// ASN1_ENCODING structure: this is used to save the received
|
||||
// encoding of an ASN1 type. This is useful to get round
|
||||
// problems with invalid encodings which can break signatures.
|
||||
// ASN1_ENCODING is used to save the received encoding of an ASN.1 type. This
|
||||
// avoids problems with invalid encodings that break signatures.
|
||||
typedef struct ASN1_ENCODING_st {
|
||||
unsigned char *enc; // DER encoding
|
||||
long len; // Length of encoding, or zero if not present.
|
||||
// alias_only is zero if |enc| owns the buffer that it points to
|
||||
// (although |enc| may still be NULL). If one, |enc| points into a
|
||||
// buffer that is owned elsewhere.
|
||||
unsigned alias_only : 1;
|
||||
// alias_only_on_next_parse is one iff the next parsing operation
|
||||
// should avoid taking a copy of the input and rather set
|
||||
// |alias_only|.
|
||||
unsigned alias_only_on_next_parse : 1;
|
||||
// enc is the saved DER encoding. Its ownership is determined by |buf|.
|
||||
uint8_t *enc;
|
||||
// len is the length of |enc|. If zero, there is no saved encoding.
|
||||
size_t len;
|
||||
// buf, if non-NULL, is the |CRYPTO_BUFFER| that |enc| points into. If NULL,
|
||||
// |enc| must be released with |OPENSSL_free|.
|
||||
CRYPTO_BUFFER *buf;
|
||||
} ASN1_ENCODING;
|
||||
|
||||
OPENSSL_EXPORT int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
|
||||
@@ -143,16 +139,22 @@ OPENSSL_EXPORT int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
|
||||
OPENSSL_EXPORT int asn1_generalizedtime_to_tm(struct tm *tm,
|
||||
const ASN1_GENERALIZEDTIME *d);
|
||||
|
||||
void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine);
|
||||
|
||||
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
|
||||
// ASN1_item_ex_d2i parses |len| bytes from |*in| as a structure of type |it|
|
||||
// and writes the result to |*pval|. If |tag| is non-negative, |it| is
|
||||
// implicitly tagged with the tag specified by |tag| and |aclass|. If |opt| is
|
||||
// non-zero, the value is optional. If |buf| is non-NULL, |*in| must point into
|
||||
// |buf|.
|
||||
//
|
||||
// This function returns one and advances |*in| if an object was successfully
|
||||
// parsed, -1 if an optional value was successfully skipped, and zero on error.
|
||||
int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it, int tag, int aclass, char opt,
|
||||
ASN1_TLC *ctx);
|
||||
CRYPTO_BUFFER *buf);
|
||||
|
||||
// ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the
|
||||
// i2d output convention. It returns a non-zero length on success and -1 on
|
||||
@@ -194,8 +196,11 @@ void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
|
||||
const ASN1_ITEM *it);
|
||||
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
|
||||
const ASN1_ITEM *it);
|
||||
// asn1_enc_save saves |inlen| bytes from |in| as |*pval|'s saved encoding. It
|
||||
// returns one on success and zero on error. If |buf| is non-NULL, |in| must
|
||||
// point into |buf|.
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const uint8_t *in, size_t inlen,
|
||||
const ASN1_ITEM *it, CRYPTO_BUFFER *buf);
|
||||
|
||||
// asn1_encoding_clear clears the cached encoding in |enc|.
|
||||
void asn1_encoding_clear(ASN1_ENCODING *enc);
|
||||
@@ -205,6 +210,10 @@ void asn1_encoding_clear(ASN1_ENCODING *enc);
|
||||
// a pointer.
|
||||
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a);
|
||||
|
||||
// asn1_type_cleanup releases memory associated with |a|'s value, without
|
||||
// freeing |a| itself.
|
||||
void asn1_type_cleanup(ASN1_TYPE *a);
|
||||
|
||||
// asn1_is_printable returns one if |value| is a valid Unicode codepoint for an
|
||||
// ASN.1 PrintableString, and zero otherwise.
|
||||
int asn1_is_printable(uint32_t value);
|
||||
@@ -230,9 +239,31 @@ typedef struct {
|
||||
OPENSSL_EXPORT void asn1_get_string_table_for_testing(
|
||||
const ASN1_STRING_TABLE **out_ptr, size_t *out_len);
|
||||
|
||||
typedef ASN1_VALUE *ASN1_new_func(void);
|
||||
typedef void ASN1_free_func(ASN1_VALUE *a);
|
||||
typedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in,
|
||||
long length);
|
||||
typedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in);
|
||||
|
||||
typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it, int opt, ASN1_TLC *ctx);
|
||||
|
||||
typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it);
|
||||
typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
typedef struct ASN1_EXTERN_FUNCS_st {
|
||||
ASN1_ex_new_func *asn1_ex_new;
|
||||
ASN1_ex_free_func *asn1_ex_free;
|
||||
ASN1_ex_free_func *asn1_ex_clear;
|
||||
ASN1_ex_d2i *asn1_ex_d2i;
|
||||
ASN1_ex_i2d *asn1_ex_i2d;
|
||||
} ASN1_EXTERN_FUNCS;
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
#endif
|
||||
|
||||
#endif // OPENSSL_HEADER_ASN1_ASN1_LOCL_H
|
||||
#endif // OPENSSL_HEADER_ASN1_INTERNAL_H
|
||||
@@ -59,7 +59,9 @@
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_pool.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -79,18 +81,18 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
|
||||
|
||||
static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
long len, const ASN1_TEMPLATE *tt, char opt,
|
||||
int depth);
|
||||
CRYPTO_BUFFER *buf, int depth);
|
||||
static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
long len, const ASN1_TEMPLATE *tt, char opt,
|
||||
int depth);
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
CRYPTO_BUFFER *buf, int depth);
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
|
||||
int utype, const ASN1_ITEM *it);
|
||||
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
|
||||
long len, const ASN1_ITEM *it, int tag,
|
||||
int aclass, char opt);
|
||||
static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
long len, const ASN1_ITEM *it, int tag, int aclass,
|
||||
char opt, int depth);
|
||||
char opt, CRYPTO_BUFFER *buf, int depth);
|
||||
|
||||
// Table to convert tags to bit values, used for MSTRING type
|
||||
static const unsigned long tag2bit[31] = {
|
||||
@@ -134,6 +136,23 @@ unsigned long ASN1_tag2bit(int tag) {
|
||||
return tag2bit[tag];
|
||||
}
|
||||
|
||||
static int is_supported_universal_type(int tag, int aclass) {
|
||||
if (aclass != V_ASN1_UNIVERSAL) {
|
||||
return 0;
|
||||
}
|
||||
return tag == V_ASN1_OBJECT || tag == V_ASN1_NULL || tag == V_ASN1_BOOLEAN ||
|
||||
tag == V_ASN1_BIT_STRING || tag == V_ASN1_INTEGER ||
|
||||
tag == V_ASN1_ENUMERATED || tag == V_ASN1_OCTET_STRING ||
|
||||
tag == V_ASN1_NUMERICSTRING || tag == V_ASN1_PRINTABLESTRING ||
|
||||
tag == V_ASN1_T61STRING || tag == V_ASN1_VIDEOTEXSTRING ||
|
||||
tag == V_ASN1_IA5STRING || tag == V_ASN1_UTCTIME ||
|
||||
tag == V_ASN1_GENERALIZEDTIME || tag == V_ASN1_GRAPHICSTRING ||
|
||||
tag == V_ASN1_VISIBLESTRING || tag == V_ASN1_GENERALSTRING ||
|
||||
tag == V_ASN1_UNIVERSALSTRING || tag == V_ASN1_BMPSTRING ||
|
||||
tag == V_ASN1_UTF8STRING || tag == V_ASN1_SET ||
|
||||
tag == V_ASN1_SEQUENCE;
|
||||
}
|
||||
|
||||
// Macro to initialize and invalidate the cache
|
||||
|
||||
// Decode an ASN1 item, this currently behaves just like a standard 'd2i'
|
||||
@@ -143,25 +162,40 @@ unsigned long ASN1_tag2bit(int tag) {
|
||||
|
||||
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it) {
|
||||
ASN1_VALUE *ptmpval = NULL;
|
||||
if (!pval) {
|
||||
pval = &ptmpval;
|
||||
ASN1_VALUE *ret = NULL;
|
||||
if (asn1_item_ex_d2i(&ret, in, len, it, /*tag=*/-1, /*aclass=*/0, /*opt=*/0,
|
||||
/*buf=*/NULL, /*depth=*/0) <= 0) {
|
||||
// Clean up, in case the caller left a partial object.
|
||||
//
|
||||
// TODO(davidben): I don't think it can leave one, but the codepaths below
|
||||
// are a bit inconsistent. Revisit this when rewriting this function.
|
||||
ASN1_item_ex_free(&ret, it);
|
||||
}
|
||||
|
||||
if (asn1_item_ex_d2i(pval, in, len, it, -1, 0, 0, 0) > 0) {
|
||||
return *pval;
|
||||
// If the caller supplied an output pointer, free the old one and replace it
|
||||
// with |ret|. This differs from OpenSSL slightly in that we don't support
|
||||
// object reuse. We run this on both success and failure. On failure, even
|
||||
// with object reuse, OpenSSL destroys the previous object.
|
||||
if (pval != NULL) {
|
||||
ASN1_item_ex_free(pval, it);
|
||||
*pval = ret;
|
||||
}
|
||||
return NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
|
||||
// tag mismatch return -1 to handle OPTIONAL
|
||||
//
|
||||
// TODO(davidben): Historically, all functions in this file had to account for
|
||||
// |*pval| containing an arbitrary existing value. This is no longer the case
|
||||
// because |ASN1_item_d2i| now always starts from NULL. As part of rewriting
|
||||
// this function, take the simplified assumptions into account. Though we must
|
||||
// still account for the internal calls to |ASN1_item_ex_new|.
|
||||
|
||||
static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
long len, const ASN1_ITEM *it, int tag, int aclass,
|
||||
char opt, int depth) {
|
||||
char opt, CRYPTO_BUFFER *buf, int depth) {
|
||||
const ASN1_TEMPLATE *tt, *errtt = NULL;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
const unsigned char *p = NULL, *q;
|
||||
unsigned char oclass;
|
||||
char cst, isopt;
|
||||
@@ -169,12 +203,15 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
int otag;
|
||||
int ret = 0;
|
||||
ASN1_VALUE **pchptr;
|
||||
int combine = aclass & ASN1_TFLG_COMBINE;
|
||||
aclass &= ~ASN1_TFLG_COMBINE;
|
||||
if (!pval) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (buf != NULL) {
|
||||
assert(CRYPTO_BUFFER_data(buf) <= *in &&
|
||||
*in + len <= CRYPTO_BUFFER_data(buf) + CRYPTO_BUFFER_len(buf));
|
||||
}
|
||||
|
||||
// Bound |len| to comfortably fit in an int. Lengths in this module often
|
||||
// switch between int and long without overflow checks.
|
||||
if (len > INT_MAX / 2) {
|
||||
@@ -197,7 +234,8 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
|
||||
goto err;
|
||||
}
|
||||
return asn1_template_ex_d2i(pval, in, len, it->templates, opt, depth);
|
||||
return asn1_template_ex_d2i(pval, in, len, it->templates, opt, buf,
|
||||
depth);
|
||||
}
|
||||
return asn1_d2i_ex_primitive(pval, in, len, it, tag, aclass, opt);
|
||||
break;
|
||||
@@ -238,10 +276,15 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
}
|
||||
return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0);
|
||||
|
||||
case ASN1_ITYPE_EXTERN:
|
||||
// Use new style d2i
|
||||
ef = it->funcs;
|
||||
return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, NULL);
|
||||
case ASN1_ITYPE_EXTERN: {
|
||||
// We don't support implicit tagging with external types.
|
||||
if (tag != -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
goto err;
|
||||
}
|
||||
const ASN1_EXTERN_FUNCS *ef = it->funcs;
|
||||
return ef->asn1_ex_d2i(pval, in, len, it, opt, NULL);
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_CHOICE: {
|
||||
// It never makes sense for CHOICE types to have implicit tagging, so if
|
||||
@@ -275,7 +318,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
|
||||
pchptr = asn1_get_field_ptr(pval, tt);
|
||||
// We mark field as OPTIONAL so its absence can be recognised.
|
||||
ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, depth);
|
||||
ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, buf, depth);
|
||||
// If field not present, try the next one
|
||||
if (ret == -1) {
|
||||
continue;
|
||||
@@ -381,7 +424,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
}
|
||||
// attempt to read in field, allowing each to be OPTIONAL
|
||||
|
||||
ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, depth);
|
||||
ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, buf, depth);
|
||||
if (!ret) {
|
||||
errtt = seqtt;
|
||||
goto err;
|
||||
@@ -420,7 +463,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
}
|
||||
}
|
||||
// Save encoding
|
||||
if (!asn1_enc_save(pval, *in, p - *in, it)) {
|
||||
if (!asn1_enc_save(pval, *in, p - *in, it, buf)) {
|
||||
goto auxerr;
|
||||
}
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) {
|
||||
@@ -436,9 +479,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
auxerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
|
||||
err:
|
||||
if (combine == 0) {
|
||||
ASN1_item_ex_free(pval, it);
|
||||
}
|
||||
ASN1_item_ex_free(pval, it);
|
||||
if (errtt) {
|
||||
ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname);
|
||||
} else {
|
||||
@@ -449,8 +490,9 @@ err:
|
||||
|
||||
int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it, int tag, int aclass, char opt,
|
||||
ASN1_TLC *ctx) {
|
||||
return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, 0);
|
||||
CRYPTO_BUFFER *buf) {
|
||||
return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, buf,
|
||||
/*depth=*/0);
|
||||
}
|
||||
|
||||
// Templates are handled with two separate functions. One handles any
|
||||
@@ -458,7 +500,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
|
||||
static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
long inlen, const ASN1_TEMPLATE *tt, char opt,
|
||||
int depth) {
|
||||
CRYPTO_BUFFER *buf, int depth) {
|
||||
int aclass;
|
||||
int ret;
|
||||
long len;
|
||||
@@ -490,7 +532,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
return 0;
|
||||
}
|
||||
// We've found the field so it can't be OPTIONAL now
|
||||
ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, depth);
|
||||
ret = asn1_template_noexp_d2i(val, &p, len, tt, /*opt=*/0, buf, depth);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
return 0;
|
||||
@@ -503,7 +545,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
return asn1_template_noexp_d2i(val, in, inlen, tt, opt, depth);
|
||||
return asn1_template_noexp_d2i(val, in, inlen, tt, opt, buf, depth);
|
||||
}
|
||||
|
||||
*in = p;
|
||||
@@ -516,7 +558,7 @@ err:
|
||||
|
||||
static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
long len, const ASN1_TEMPLATE *tt, char opt,
|
||||
int depth) {
|
||||
CRYPTO_BUFFER *buf, int depth) {
|
||||
int aclass;
|
||||
int ret;
|
||||
const unsigned char *p;
|
||||
@@ -565,7 +607,6 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
}
|
||||
|
||||
if (!*val) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -574,22 +615,21 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
ASN1_VALUE *skfield;
|
||||
const unsigned char *q = p;
|
||||
skfield = NULL;
|
||||
if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item), -1, 0,
|
||||
0, depth)) {
|
||||
if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item),
|
||||
/*tag=*/-1, /*aclass=*/0, /*opt=*/0, buf, depth)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
}
|
||||
len -= p - q;
|
||||
if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
|
||||
ASN1_item_ex_free(&skfield, ASN1_ITEM_ptr(tt->item));
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
} else if (flags & ASN1_TFLG_IMPTAG) {
|
||||
// IMPLICIT tagging
|
||||
ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag,
|
||||
aclass, opt, depth);
|
||||
aclass, opt, buf, depth);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
@@ -598,8 +638,8 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
}
|
||||
} else {
|
||||
// Nothing special
|
||||
ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), -1,
|
||||
tt->flags & ASN1_TFLG_COMBINE, opt, depth);
|
||||
ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), /*tag=*/-1,
|
||||
/*aclass=*/0, opt, buf, depth);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
@@ -654,7 +694,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
return 0;
|
||||
}
|
||||
if (oclass != V_ASN1_UNIVERSAL) {
|
||||
if (!is_supported_universal_type(utype, oclass)) {
|
||||
utype = V_ASN1_OTHER;
|
||||
}
|
||||
}
|
||||
@@ -709,7 +749,7 @@ err:
|
||||
|
||||
// Translate ASN1 content octets into a structure
|
||||
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
|
||||
int utype, const ASN1_ITEM *it) {
|
||||
ASN1_VALUE **opval = NULL;
|
||||
ASN1_STRING *stmp;
|
||||
@@ -798,6 +838,12 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
case V_ASN1_OTHER:
|
||||
case V_ASN1_SET:
|
||||
case V_ASN1_SEQUENCE:
|
||||
// TODO(crbug.com/boringssl/412): This default case should be removed, now
|
||||
// that we've resolved https://crbug.com/boringssl/561. However, it is still
|
||||
// needed to support some edge cases in |ASN1_PRINTABLE|. |ASN1_PRINTABLE|
|
||||
// broadly doesn't tolerate unrecognized universal tags, but except for
|
||||
// eight values that map to |B_ASN1_UNKNOWN| instead of zero. See the
|
||||
// X509Test.NameAttributeValues test.
|
||||
default: {
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, cont, (size_t)len);
|
||||
@@ -847,7 +893,6 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
if (!*pval) {
|
||||
stmp = ASN1_STRING_type_new(utype);
|
||||
if (!stmp) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
*pval = (ASN1_VALUE *)stmp;
|
||||
@@ -856,7 +901,6 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
stmp->type = utype;
|
||||
}
|
||||
if (!ASN1_STRING_set(stmp, cont, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
ASN1_STRING_free(stmp);
|
||||
*pval = NULL;
|
||||
goto err;
|
||||
|
||||
@@ -78,7 +78,8 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *out_omit,
|
||||
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
||||
int skcontlen, const ASN1_ITEM *item, int do_sort);
|
||||
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_TEMPLATE *tt, int tag, int aclass);
|
||||
const ASN1_TEMPLATE *tt, int tag, int aclass,
|
||||
int optional);
|
||||
|
||||
// Top level i2d equivalents
|
||||
|
||||
@@ -91,12 +92,12 @@ int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) {
|
||||
}
|
||||
buf = OPENSSL_malloc(len);
|
||||
if (!buf) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
p = buf;
|
||||
int len2 = ASN1_item_ex_i2d(&val, &p, it, /*tag=*/-1, /*aclass=*/0);
|
||||
if (len2 <= 0) {
|
||||
OPENSSL_free(buf);
|
||||
return len2;
|
||||
}
|
||||
assert(len == len2);
|
||||
@@ -144,11 +145,13 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
switch (it->itype) {
|
||||
case ASN1_ITYPE_PRIMITIVE:
|
||||
if (it->templates) {
|
||||
// This is an |ASN1_ITEM_TEMPLATE|.
|
||||
if (it->templates->flags & ASN1_TFLG_OPTIONAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
return asn1_template_ex_i2d(pval, out, it->templates, tag, aclass);
|
||||
return asn1_template_ex_i2d(pval, out, it->templates, tag, aclass,
|
||||
optional);
|
||||
}
|
||||
return asn1_i2d_ex_primitive(pval, out, it, tag, aclass, optional);
|
||||
|
||||
@@ -179,13 +182,17 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
return -1;
|
||||
}
|
||||
ASN1_VALUE **pchval = asn1_get_field_ptr(pval, chtt);
|
||||
return asn1_template_ex_i2d(pchval, out, chtt, -1, 0);
|
||||
return asn1_template_ex_i2d(pchval, out, chtt, -1, 0, /*optional=*/0);
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_EXTERN: {
|
||||
// If new style i2d it does all the work
|
||||
// We don't support implicit tagging with external types.
|
||||
if (tag != -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
const ASN1_EXTERN_FUNCS *ef = it->funcs;
|
||||
int ret = ef->asn1_ex_i2d(pval, out, it, tag, aclass);
|
||||
int ret = ef->asn1_ex_i2d(pval, out, it);
|
||||
if (ret == 0) {
|
||||
// |asn1_ex_i2d| should never return zero. We have already checked
|
||||
// for optional values generically, and |ASN1_ITYPE_EXTERN| fields
|
||||
@@ -223,7 +230,8 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
return -1;
|
||||
}
|
||||
pseqval = asn1_get_field_ptr(pval, seqtt);
|
||||
tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, 0);
|
||||
tmplen =
|
||||
asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, 0, /*optional=*/0);
|
||||
if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen)) {
|
||||
return -1;
|
||||
}
|
||||
@@ -244,7 +252,8 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
return -1;
|
||||
}
|
||||
pseqval = asn1_get_field_ptr(pval, seqtt);
|
||||
if (asn1_template_ex_i2d(pseqval, out, seqtt, -1, 0) < 0) {
|
||||
if (asn1_template_ex_i2d(pseqval, out, seqtt, -1, 0, /*optional=*/0) <
|
||||
0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -259,10 +268,10 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
|
||||
// asn1_template_ex_i2d behaves like |asn1_item_ex_i2d_opt| but uses an
|
||||
// |ASN1_TEMPLATE| instead of an |ASN1_ITEM|. An |ASN1_TEMPLATE| wraps an
|
||||
// |ASN1_ITEM| with modifiers such as tagging, SEQUENCE or SET, etc. Instead of
|
||||
// taking an |optional| parameter, it uses the |ASN1_TFLG_OPTIONAL| flag.
|
||||
// |ASN1_ITEM| with modifiers such as tagging, SEQUENCE or SET, etc.
|
||||
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_TEMPLATE *tt, int tag, int iclass) {
|
||||
const ASN1_TEMPLATE *tt, int tag, int iclass,
|
||||
int optional) {
|
||||
int i, ret, ttag, tclass;
|
||||
size_t j;
|
||||
uint32_t flags = tt->flags;
|
||||
@@ -294,7 +303,12 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
tclass = 0;
|
||||
}
|
||||
|
||||
const int optional = (flags & ASN1_TFLG_OPTIONAL) != 0;
|
||||
// The template may itself by marked as optional, or this may be the template
|
||||
// of an |ASN1_ITEM_TEMPLATE| type which was contained inside an outer
|
||||
// optional template. (They cannot both be true because the
|
||||
// |ASN1_ITEM_TEMPLATE| codepath rejects optional templates.)
|
||||
assert(!optional || (flags & ASN1_TFLG_OPTIONAL) == 0);
|
||||
optional = optional || (flags & ASN1_TFLG_OPTIONAL) != 0;
|
||||
|
||||
// At this point 'ttag' contains the outer tag to use, and 'tclass' is the
|
||||
// class.
|
||||
@@ -447,7 +461,6 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
||||
unsigned char *const buf = OPENSSL_malloc(skcontlen);
|
||||
DER_ENC *encoded = OPENSSL_malloc(sk_ASN1_VALUE_num(sk) * sizeof(*encoded));
|
||||
if (encoded == NULL || buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -631,7 +644,7 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *out_omit,
|
||||
|
||||
case V_ASN1_BOOLEAN:
|
||||
tbool = (ASN1_BOOLEAN *)pval;
|
||||
if (*tbool == -1) {
|
||||
if (*tbool == ASN1_BOOLEAN_NONE) {
|
||||
*out_omit = 1;
|
||||
return 0;
|
||||
}
|
||||
@@ -678,12 +691,19 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *out_omit,
|
||||
case V_ASN1_UTF8STRING:
|
||||
case V_ASN1_SEQUENCE:
|
||||
case V_ASN1_SET:
|
||||
// This is not a valid |ASN1_ITEM| type, but it appears in |ASN1_TYPE|.
|
||||
case V_ASN1_OTHER:
|
||||
// TODO(crbug.com/boringssl/412): This default case should be removed, now
|
||||
// that we've resolved https://crbug.com/boringssl/561. However, it is still
|
||||
// needed to support some edge cases in |ASN1_PRINTABLE|. |ASN1_PRINTABLE|
|
||||
// broadly doesn't tolerate unrecognized universal tags, but except for
|
||||
// eight values that map to |B_ASN1_UNKNOWN| instead of zero. See the
|
||||
// X509Test.NameAttributeValues test.
|
||||
default:
|
||||
// All based on ASN1_STRING and handled the same
|
||||
strtmp = (ASN1_STRING *)*pval;
|
||||
cont = strtmp->data;
|
||||
len = strtmp->length;
|
||||
|
||||
break;
|
||||
}
|
||||
if (cout && len) {
|
||||
|
||||
@@ -66,15 +66,10 @@
|
||||
// Free up an ASN1 structure
|
||||
|
||||
void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) {
|
||||
asn1_item_combine_free(&val, it, 0);
|
||||
ASN1_item_ex_free(&val, it);
|
||||
}
|
||||
|
||||
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
asn1_item_combine_free(pval, it, 0);
|
||||
}
|
||||
|
||||
void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine) {
|
||||
const ASN1_TEMPLATE *tt = NULL, *seqtt;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
int i;
|
||||
@@ -117,10 +112,8 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
if (asn1_cb) {
|
||||
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
|
||||
}
|
||||
if (!combine) {
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
}
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -160,57 +153,33 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
if (asn1_cb) {
|
||||
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
|
||||
}
|
||||
if (!combine) {
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
}
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
size_t i;
|
||||
if (tt->flags & ASN1_TFLG_SK_MASK) {
|
||||
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
|
||||
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
ASN1_VALUE *vtmp;
|
||||
vtmp = sk_ASN1_VALUE_value(sk, i);
|
||||
asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0);
|
||||
for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
|
||||
ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
|
||||
}
|
||||
sk_ASN1_VALUE_free(sk);
|
||||
*pval = NULL;
|
||||
} else {
|
||||
asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
|
||||
tt->flags & ASN1_TFLG_COMBINE);
|
||||
ASN1_item_ex_free(pval, ASN1_ITEM_ptr(tt->item));
|
||||
}
|
||||
}
|
||||
|
||||
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
int utype;
|
||||
// Historically, |it->funcs| for primitive types contained an
|
||||
// |ASN1_PRIMITIVE_FUNCS| table of calbacks.
|
||||
assert(it == NULL || it->funcs == NULL);
|
||||
// Special case: if 'it' is NULL free contents of ASN1_TYPE
|
||||
if (!it) {
|
||||
ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
|
||||
utype = typ->type;
|
||||
pval = &typ->value.asn1_value;
|
||||
if (utype != V_ASN1_BOOLEAN && !*pval) {
|
||||
return;
|
||||
}
|
||||
} else if (it->itype == ASN1_ITYPE_MSTRING) {
|
||||
utype = -1;
|
||||
if (!*pval) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
utype = it->utype;
|
||||
if ((utype != V_ASN1_BOOLEAN) && !*pval) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
assert(it->funcs == NULL);
|
||||
|
||||
int utype = it->itype == ASN1_ITYPE_MSTRING ? -1 : it->utype;
|
||||
switch (utype) {
|
||||
case V_ASN1_OBJECT:
|
||||
ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
|
||||
@@ -220,7 +189,7 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
if (it) {
|
||||
*(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size;
|
||||
} else {
|
||||
*(ASN1_BOOLEAN *)pval = -1;
|
||||
*(ASN1_BOOLEAN *)pval = ASN1_BOOLEAN_NONE;
|
||||
}
|
||||
return;
|
||||
|
||||
@@ -228,8 +197,10 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
break;
|
||||
|
||||
case V_ASN1_ANY:
|
||||
ASN1_primitive_free(pval, NULL);
|
||||
OPENSSL_free(*pval);
|
||||
if (*pval != NULL) {
|
||||
asn1_type_cleanup((ASN1_TYPE *)*pval);
|
||||
OPENSSL_free(*pval);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@@ -67,8 +67,6 @@
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine);
|
||||
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
@@ -86,11 +84,6 @@ ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) {
|
||||
// Allocate an ASN1 structure
|
||||
|
||||
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
return asn1_item_ex_combine_new(pval, it, 0);
|
||||
}
|
||||
|
||||
static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine) {
|
||||
const ASN1_TEMPLATE *tt = NULL;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
ASN1_VALUE **pseqval;
|
||||
@@ -134,13 +127,11 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (!combine) {
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
asn1_set_choice_selector(pval, -1, it);
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) {
|
||||
goto auxerr2;
|
||||
@@ -160,15 +151,13 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (!combine) {
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
asn1_refcount_set_one(pval, it);
|
||||
asn1_enc_init(pval, it);
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
asn1_refcount_set_one(pval, it);
|
||||
asn1_enc_init(pval, it);
|
||||
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
|
||||
pseqval = asn1_get_field_ptr(pval, tt);
|
||||
if (!ASN1_template_new(pseqval, tt)) {
|
||||
@@ -184,13 +173,12 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
return 1;
|
||||
|
||||
memerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
ASN1_item_ex_free(pval, it);
|
||||
memerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
|
||||
auxerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
ASN1_item_ex_free(pval, it);
|
||||
auxerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
|
||||
return 0;
|
||||
@@ -246,7 +234,6 @@ static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
STACK_OF(ASN1_VALUE) *skval;
|
||||
skval = sk_ASN1_VALUE_new_null();
|
||||
if (!skval) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
ret = 0;
|
||||
goto done;
|
||||
}
|
||||
@@ -255,7 +242,7 @@ static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
goto done;
|
||||
}
|
||||
// Otherwise pass it back to the item routine
|
||||
ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
|
||||
ret = ASN1_item_ex_new(pval, it);
|
||||
done:
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -107,9 +107,9 @@ IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_STRING, DIRECTORYSTRING,
|
||||
DIRECTORYSTRING)
|
||||
|
||||
// Three separate BOOLEAN type: normal, DEFAULT TRUE and DEFAULT FALSE
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_BOOLEAN, ASN1_BOOLEAN, -1)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_TBOOLEAN, ASN1_BOOLEAN, 1)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_FBOOLEAN, ASN1_BOOLEAN, 0)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_BOOLEAN, ASN1_BOOLEAN, ASN1_BOOLEAN_NONE)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_TBOOLEAN, ASN1_BOOLEAN, ASN1_BOOLEAN_TRUE)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_FBOOLEAN, ASN1_BOOLEAN, ASN1_BOOLEAN_FALSE)
|
||||
|
||||
ASN1_ITEM_TEMPLATE(ASN1_SEQUENCE_ANY) =
|
||||
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY)
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
#include <CCryptoBoringSSL_pool.h>
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
|
||||
#include "../internal.h"
|
||||
@@ -135,8 +136,7 @@ void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
if (enc) {
|
||||
enc->enc = NULL;
|
||||
enc->len = 0;
|
||||
enc->alias_only = 0;
|
||||
enc->alias_only_on_next_parse = 0;
|
||||
enc->buf = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,42 +147,41 @@ void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
}
|
||||
}
|
||||
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
|
||||
const ASN1_ITEM *it) {
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const uint8_t *in, size_t in_len,
|
||||
const ASN1_ITEM *it, CRYPTO_BUFFER *buf) {
|
||||
ASN1_ENCODING *enc;
|
||||
enc = asn1_get_enc_ptr(pval, it);
|
||||
if (!enc) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!enc->alias_only) {
|
||||
OPENSSL_free(enc->enc);
|
||||
}
|
||||
|
||||
enc->alias_only = enc->alias_only_on_next_parse;
|
||||
enc->alias_only_on_next_parse = 0;
|
||||
|
||||
if (enc->alias_only) {
|
||||
asn1_encoding_clear(enc);
|
||||
if (buf != NULL) {
|
||||
assert(CRYPTO_BUFFER_data(buf) <= in &&
|
||||
in + in_len <= CRYPTO_BUFFER_data(buf) + CRYPTO_BUFFER_len(buf));
|
||||
CRYPTO_BUFFER_up_ref(buf);
|
||||
enc->buf = buf;
|
||||
enc->enc = (uint8_t *)in;
|
||||
} else {
|
||||
enc->enc = OPENSSL_memdup(in, inlen);
|
||||
enc->enc = OPENSSL_memdup(in, in_len);
|
||||
if (!enc->enc) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
enc->len = inlen;
|
||||
enc->len = in_len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void asn1_encoding_clear(ASN1_ENCODING *enc) {
|
||||
if (!enc->alias_only) {
|
||||
if (enc->buf != NULL) {
|
||||
CRYPTO_BUFFER_free(enc->buf);
|
||||
} else {
|
||||
OPENSSL_free(enc->enc);
|
||||
}
|
||||
enc->enc = NULL;
|
||||
enc->len = 0;
|
||||
enc->alias_only = 0;
|
||||
enc->alias_only_on_next_parse = 0;
|
||||
enc->buf = NULL;
|
||||
}
|
||||
|
||||
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
|
||||
@@ -203,11 +202,7 @@ int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
|
||||
|
||||
// Given an ASN1_TEMPLATE get a pointer to a field
|
||||
ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
ASN1_VALUE **pvaltmp;
|
||||
if (tt->flags & ASN1_TFLG_COMBINE) {
|
||||
return pval;
|
||||
}
|
||||
pvaltmp = offset2ptr(*pval, tt->offset);
|
||||
ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
|
||||
// NOTE for BOOLEAN types the field is just a plain int so we can't return
|
||||
// int **, so settle for (int *).
|
||||
return pvaltmp;
|
||||
|
||||
@@ -72,7 +72,6 @@
|
||||
BIO *BIO_new(const BIO_METHOD *method) {
|
||||
BIO *ret = OPENSSL_malloc(sizeof(BIO));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -424,7 +423,7 @@ int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
|
||||
}
|
||||
|
||||
static int print_bio(const char *str, size_t len, void *bio) {
|
||||
return BIO_write((BIO *)bio, str, len);
|
||||
return BIO_write_all((BIO *)bio, str, len);
|
||||
}
|
||||
|
||||
void ERR_print_errors(BIO *bio) {
|
||||
@@ -463,9 +462,11 @@ static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
|
||||
OPENSSL_free(*out);
|
||||
return 0;
|
||||
}
|
||||
const size_t todo = len - done;
|
||||
assert(todo < INT_MAX);
|
||||
const int n = BIO_read(bio, *out + done, todo);
|
||||
size_t todo = len - done;
|
||||
if (todo > INT_MAX) {
|
||||
todo = INT_MAX;
|
||||
}
|
||||
const int n = BIO_read(bio, *out + done, (int)todo);
|
||||
if (n == 0) {
|
||||
*out_len = done;
|
||||
return 1;
|
||||
@@ -609,7 +610,6 @@ int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
|
||||
|
||||
*out = OPENSSL_malloc(len);
|
||||
if (*out == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_memcpy(*out, header, header_len);
|
||||
|
||||
@@ -130,13 +130,15 @@ static int mem_free(BIO *bio) {
|
||||
}
|
||||
|
||||
static int mem_read(BIO *bio, char *out, int outl) {
|
||||
int ret;
|
||||
BUF_MEM *b = (BUF_MEM*) bio->ptr;
|
||||
|
||||
BIO_clear_retry_flags(bio);
|
||||
ret = outl;
|
||||
if (b->length < INT_MAX && ret > (int)b->length) {
|
||||
ret = b->length;
|
||||
if (outl <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
BUF_MEM *b = bio->ptr;
|
||||
int ret = outl;
|
||||
if ((size_t)ret > b->length) {
|
||||
ret = (int)b->length;
|
||||
}
|
||||
|
||||
if (ret > 0) {
|
||||
@@ -157,65 +159,49 @@ static int mem_read(BIO *bio, char *out, int outl) {
|
||||
}
|
||||
|
||||
static int mem_write(BIO *bio, const char *in, int inl) {
|
||||
int ret = -1;
|
||||
int blen;
|
||||
BUF_MEM *b;
|
||||
|
||||
b = (BUF_MEM *)bio->ptr;
|
||||
BIO_clear_retry_flags(bio);
|
||||
if (inl <= 0) {
|
||||
return 0; // Successfully write zero bytes.
|
||||
}
|
||||
|
||||
if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
|
||||
OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
|
||||
goto err;
|
||||
return -1;
|
||||
}
|
||||
|
||||
BIO_clear_retry_flags(bio);
|
||||
blen = b->length;
|
||||
if (INT_MAX - blen < inl) {
|
||||
goto err;
|
||||
BUF_MEM *b = bio->ptr;
|
||||
if (!BUF_MEM_append(b, in, inl)) {
|
||||
return -1;
|
||||
}
|
||||
if (BUF_MEM_grow_clean(b, blen + inl) != ((size_t) blen) + inl) {
|
||||
goto err;
|
||||
}
|
||||
OPENSSL_memcpy(&b->data[blen], in, inl);
|
||||
ret = inl;
|
||||
|
||||
err:
|
||||
return ret;
|
||||
return inl;
|
||||
}
|
||||
|
||||
static int mem_gets(BIO *bio, char *buf, int size) {
|
||||
int i, j;
|
||||
char *p;
|
||||
BUF_MEM *b = (BUF_MEM *)bio->ptr;
|
||||
|
||||
BIO_clear_retry_flags(bio);
|
||||
j = b->length;
|
||||
if (size - 1 < j) {
|
||||
j = size - 1;
|
||||
}
|
||||
if (j <= 0) {
|
||||
if (size > 0) {
|
||||
*buf = 0;
|
||||
}
|
||||
if (size <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
p = b->data;
|
||||
for (i = 0; i < j; i++) {
|
||||
if (p[i] == '\n') {
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
// The buffer size includes space for the trailing NUL, so we can read at most
|
||||
// one fewer byte.
|
||||
BUF_MEM *b = bio->ptr;
|
||||
int ret = size - 1;
|
||||
if ((size_t)ret > b->length) {
|
||||
ret = (int)b->length;
|
||||
}
|
||||
|
||||
// i is now the max num of bytes to copy, either j or up to and including the
|
||||
// first newline
|
||||
|
||||
i = mem_read(bio, buf, i);
|
||||
if (i > 0) {
|
||||
buf[i] = '\0';
|
||||
// Stop at the first newline.
|
||||
const char *newline = OPENSSL_memchr(b->data, '\n', ret);
|
||||
if (newline != NULL) {
|
||||
ret = (int)(newline - b->data + 1);
|
||||
}
|
||||
return i;
|
||||
|
||||
ret = mem_read(bio, buf, ret);
|
||||
if (ret >= 0) {
|
||||
buf[ret] = '\0';
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
|
||||
|
||||
@@ -241,20 +241,24 @@ static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
|
||||
}
|
||||
|
||||
static int fd_gets(BIO *bp, char *buf, int size) {
|
||||
char *ptr = buf;
|
||||
char *end = buf + size - 1;
|
||||
|
||||
if (size <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (ptr < end && fd_read(bp, ptr, 1) > 0 && ptr[0] != '\n') {
|
||||
char *ptr = buf;
|
||||
char *end = buf + size - 1;
|
||||
while (ptr < end && fd_read(bp, ptr, 1) > 0) {
|
||||
char c = ptr[0];
|
||||
ptr++;
|
||||
if (c == '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ptr[0] = '\0';
|
||||
|
||||
return ptr - buf;
|
||||
// The output length is bounded by |size|.
|
||||
return (int)(ptr - buf);
|
||||
}
|
||||
|
||||
static const BIO_METHOD methods_fdp = {
|
||||
|
||||
@@ -157,13 +157,11 @@ static int file_read(BIO *b, char *out, int outl) {
|
||||
}
|
||||
|
||||
static int file_write(BIO *b, const char *in, int inl) {
|
||||
int ret = 0;
|
||||
|
||||
if (!b->init) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = fwrite(in, inl, 1, (FILE *)b->ptr);
|
||||
int ret = (int)fwrite(in, inl, 1, (FILE *)b->ptr);
|
||||
if (ret > 0) {
|
||||
ret = inl;
|
||||
}
|
||||
@@ -253,20 +251,18 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
|
||||
}
|
||||
|
||||
static int file_gets(BIO *bp, char *buf, int size) {
|
||||
int ret = 0;
|
||||
|
||||
if (size == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!fgets(buf, size, (FILE *)bp->ptr)) {
|
||||
buf[0] = 0;
|
||||
goto err;
|
||||
// TODO(davidben): This doesn't distinguish error and EOF. This should check
|
||||
// |ferror| as in |file_read|.
|
||||
return 0;
|
||||
}
|
||||
ret = strlen(buf);
|
||||
|
||||
err:
|
||||
return ret;
|
||||
return (int)strlen(buf);
|
||||
}
|
||||
|
||||
static const BIO_METHOD methods_filep = {
|
||||
|
||||
@@ -221,7 +221,8 @@ static int bio_read(BIO *bio, char *buf, int size_) {
|
||||
rest -= chunk;
|
||||
} while (rest);
|
||||
|
||||
return size;
|
||||
// |size| is bounded by the buffer size, which fits in |int|.
|
||||
return (int)size;
|
||||
}
|
||||
|
||||
static int bio_write(BIO *bio, const char *buf, int num_) {
|
||||
@@ -293,7 +294,8 @@ static int bio_write(BIO *bio, const char *buf, int num_) {
|
||||
buf += chunk;
|
||||
} while (rest);
|
||||
|
||||
return num;
|
||||
// |num| is bounded by the buffer size, which fits in |int|.
|
||||
return (int)num;
|
||||
}
|
||||
|
||||
static int bio_make_pair(BIO *bio1, BIO *bio2, size_t writebuf1_len,
|
||||
@@ -317,7 +319,6 @@ static int bio_make_pair(BIO *bio1, BIO *bio2, size_t writebuf1_len,
|
||||
}
|
||||
b1->buf = OPENSSL_malloc(b1->size);
|
||||
if (b1->buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
b1->len = 0;
|
||||
@@ -330,7 +331,6 @@ static int bio_make_pair(BIO *bio1, BIO *bio2, size_t writebuf1_len,
|
||||
}
|
||||
b2->buf = OPENSSL_malloc(b2->size);
|
||||
if (b2->buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
b2->len = 0;
|
||||
|
||||
@@ -83,7 +83,6 @@ int BIO_printf(BIO *bio, const char *format, ...) {
|
||||
out = OPENSSL_malloc(requested_len + 1);
|
||||
out_malloced = 1;
|
||||
if (out == NULL) {
|
||||
OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
va_start(args, format);
|
||||
|
||||
@@ -105,8 +105,12 @@ void BLAKE2B256_Init(BLAKE2B_CTX *b2b) {
|
||||
}
|
||||
|
||||
void BLAKE2B256_Update(BLAKE2B_CTX *b2b, const void *in_data, size_t len) {
|
||||
const uint8_t *data = (const uint8_t *)in_data;
|
||||
if (len == 0) {
|
||||
// Work around a C language bug. See https://crbug.com/1019588.
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t *data = in_data;
|
||||
size_t todo = sizeof(b2b->block.bytes) - b2b->block_used;
|
||||
if (todo > len) {
|
||||
todo = len;
|
||||
|
||||
@@ -81,7 +81,6 @@ char *BN_bn2hex(const BIGNUM *bn) {
|
||||
char *buf = OPENSSL_malloc(1 /* leading '-' */ + 1 /* zero is non-empty */ +
|
||||
width * BN_BYTES * 2 + 1 /* trailing NUL */);
|
||||
if (buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -133,18 +132,9 @@ static int decode_hex(BIGNUM *bn, const char *in, int in_len) {
|
||||
BN_ULONG word = 0;
|
||||
int j;
|
||||
for (j = todo; j > 0; j--) {
|
||||
char c = in[in_len - j];
|
||||
|
||||
BN_ULONG hex;
|
||||
if (c >= '0' && c <= '9') {
|
||||
hex = c - '0';
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
hex = c - 'a' + 10;
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
hex = c - 'A' + 10;
|
||||
} else {
|
||||
hex = 0;
|
||||
// This shouldn't happen. The caller checks |isxdigit|.
|
||||
uint8_t hex = 0;
|
||||
if (!OPENSSL_fromxdigit(&hex, in[in_len - j])) {
|
||||
// This shouldn't happen. The caller checks |OPENSSL_isxdigit|.
|
||||
assert(0);
|
||||
}
|
||||
word = (word << 4) | hex;
|
||||
@@ -240,7 +230,7 @@ err:
|
||||
}
|
||||
|
||||
int BN_hex2bn(BIGNUM **outp, const char *in) {
|
||||
return bn_x2bn(outp, in, decode_hex, isxdigit);
|
||||
return bn_x2bn(outp, in, decode_hex, OPENSSL_isxdigit);
|
||||
}
|
||||
|
||||
char *BN_bn2dec(const BIGNUM *a) {
|
||||
@@ -250,12 +240,12 @@ char *BN_bn2dec(const BIGNUM *a) {
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 16) ||
|
||||
!CBB_add_u8(&cbb, 0 /* trailing NUL */)) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (BN_is_zero(a)) {
|
||||
if (!CBB_add_u8(&cbb, '0')) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
copy = BN_dup(a);
|
||||
@@ -272,7 +262,7 @@ char *BN_bn2dec(const BIGNUM *a) {
|
||||
const int add_leading_zeros = !BN_is_zero(copy);
|
||||
for (int i = 0; i < BN_DEC_NUM && (add_leading_zeros || word != 0); i++) {
|
||||
if (!CBB_add_u8(&cbb, '0' + word % 10)) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
word /= 10;
|
||||
}
|
||||
@@ -282,13 +272,13 @@ char *BN_bn2dec(const BIGNUM *a) {
|
||||
|
||||
if (BN_is_negative(a) &&
|
||||
!CBB_add_u8(&cbb, '-')) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
|
||||
uint8_t *data;
|
||||
size_t len;
|
||||
if (!CBB_finish(&cbb, &data, &len)) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Reverse the buffer.
|
||||
@@ -301,8 +291,6 @@ char *BN_bn2dec(const BIGNUM *a) {
|
||||
BN_free(copy);
|
||||
return (char *)data;
|
||||
|
||||
cbb_err:
|
||||
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
||||
err:
|
||||
BN_free(copy);
|
||||
CBB_cleanup(&cbb);
|
||||
@@ -310,7 +298,7 @@ err:
|
||||
}
|
||||
|
||||
int BN_dec2bn(BIGNUM **outp, const char *in) {
|
||||
return bn_x2bn(outp, in, decode_dec, isdigit);
|
||||
return bn_x2bn(outp, in, decode_dec, OPENSSL_isdigit);
|
||||
}
|
||||
|
||||
int BN_asc2bn(BIGNUM **outp, const char *in) {
|
||||
@@ -436,7 +424,6 @@ BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out) {
|
||||
if (out == NULL) {
|
||||
out = BN_new();
|
||||
if (out == NULL) {
|
||||
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
out_is_alloced = 1;
|
||||
|
||||
@@ -69,7 +69,6 @@ BUF_MEM *BUF_MEM_new(void) {
|
||||
|
||||
ret = OPENSSL_malloc(sizeof(BUF_MEM));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -93,21 +92,18 @@ int BUF_MEM_reserve(BUF_MEM *buf, size_t cap) {
|
||||
|
||||
size_t n = cap + 3;
|
||||
if (n < cap) {
|
||||
// overflow
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
n = n / 3;
|
||||
size_t alloc_size = n * 4;
|
||||
if (alloc_size / 4 != n) {
|
||||
// overflow
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *new_buf = OPENSSL_realloc(buf->data, alloc_size);
|
||||
if (new_buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
|
||||
#include "../internal.h"
|
||||
|
||||
@@ -77,11 +78,13 @@ static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out,
|
||||
size_t newlen = base->len + len;
|
||||
if (newlen < base->len) {
|
||||
// Overflow
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (newlen > base->cap) {
|
||||
if (!base->can_resize) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -121,6 +124,7 @@ static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out,
|
||||
|
||||
int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) {
|
||||
if (cbb->is_child) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -191,6 +195,7 @@ int CBB_flush(CBB *cbb) {
|
||||
assert (child->pending_len_len == 1);
|
||||
|
||||
if (len > 0xfffffffe) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
|
||||
// Too large.
|
||||
goto err;
|
||||
} else if (len > 0xffffff) {
|
||||
@@ -229,6 +234,7 @@ int CBB_flush(CBB *cbb) {
|
||||
len >>= 8;
|
||||
}
|
||||
if (len != 0) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -560,30 +566,15 @@ int CBB_add_asn1_bool(CBB *cbb, int value) {
|
||||
// component and the dot, so |cbs| may be passed into the function again for the
|
||||
// next value.
|
||||
static int parse_dotted_decimal(CBS *cbs, uint64_t *out) {
|
||||
*out = 0;
|
||||
int seen_digit = 0;
|
||||
for (;;) {
|
||||
// Valid terminators for a component are the end of the string or a
|
||||
// non-terminal dot. If the string ends with a dot, this is not a valid OID
|
||||
// string.
|
||||
uint8_t u;
|
||||
if (!CBS_get_u8(cbs, &u) ||
|
||||
(u == '.' && CBS_len(cbs) > 0)) {
|
||||
break;
|
||||
}
|
||||
if (u < '0' || u > '9' ||
|
||||
// Forbid stray leading zeros.
|
||||
(seen_digit && *out == 0) ||
|
||||
// Check for overflow.
|
||||
*out > UINT64_MAX / 10 ||
|
||||
*out * 10 > UINT64_MAX - (u - '0')) {
|
||||
return 0;
|
||||
}
|
||||
*out = *out * 10 + (u - '0');
|
||||
seen_digit = 1;
|
||||
if (!CBS_get_u64_decimal(cbs, out)) {
|
||||
return 0;
|
||||
}
|
||||
// The empty string is not a legal OID component.
|
||||
return seen_digit;
|
||||
|
||||
// The integer must have either ended at the end of the string, or a
|
||||
// non-terminal dot, which should be consumed. If the string ends with a dot,
|
||||
// this is not a valid OID string.
|
||||
uint8_t dot;
|
||||
return !CBS_get_u8(cbs, &dot) || (dot == '.' && CBS_len(cbs) > 0);
|
||||
}
|
||||
|
||||
int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) {
|
||||
@@ -649,6 +640,7 @@ int CBB_flush_asn1_set_of(CBB *cbb) {
|
||||
CBS_init(&cbs, CBB_data(cbb), CBB_len(cbb));
|
||||
while (CBS_len(&cbs) != 0) {
|
||||
if (!CBS_get_any_asn1_element(&cbs, NULL, NULL, NULL)) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
num_children++;
|
||||
|
||||
@@ -227,6 +227,30 @@ int CBS_get_until_first(CBS *cbs, CBS *out, uint8_t c) {
|
||||
return CBS_get_bytes(cbs, out, split - CBS_data(cbs));
|
||||
}
|
||||
|
||||
int CBS_get_u64_decimal(CBS *cbs, uint64_t *out) {
|
||||
uint64_t v = 0;
|
||||
int seen_digit = 0;
|
||||
while (CBS_len(cbs) != 0) {
|
||||
uint8_t c = CBS_data(cbs)[0];
|
||||
if (!OPENSSL_isdigit(c)) {
|
||||
break;
|
||||
}
|
||||
CBS_skip(cbs, 1);
|
||||
if (// Forbid stray leading zeros.
|
||||
(v == 0 && seen_digit) ||
|
||||
// Check for overflow.
|
||||
v > UINT64_MAX / 10 || //
|
||||
v * 10 > UINT64_MAX - (c - '0')) {
|
||||
return 0;
|
||||
}
|
||||
v = v * 10 + (c - '0');
|
||||
seen_digit = 1;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return seen_digit;
|
||||
}
|
||||
|
||||
// parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets
|
||||
// |*out| to the result. This is the encoding used in DER for both high tag
|
||||
// number form and OID components.
|
||||
@@ -456,10 +480,6 @@ int CBS_get_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) {
|
||||
}
|
||||
|
||||
int CBS_peek_asn1_tag(const CBS *cbs, CBS_ASN1_TAG tag_value) {
|
||||
if (CBS_len(cbs) < 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
CBS copy = *cbs;
|
||||
CBS_ASN1_TAG actual_tag;
|
||||
return parse_asn1_tag(©, &actual_tag) && tag_value == actual_tag;
|
||||
@@ -678,6 +698,29 @@ static int add_decimal(CBB *out, uint64_t v) {
|
||||
return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
|
||||
}
|
||||
|
||||
int CBS_is_valid_asn1_oid(const CBS *cbs) {
|
||||
if (CBS_len(cbs) == 0) {
|
||||
return 0; // OID encodings cannot be empty.
|
||||
}
|
||||
|
||||
CBS copy = *cbs;
|
||||
uint8_t v, prev = 0;
|
||||
while (CBS_get_u8(©, &v)) {
|
||||
// OID encodings are a sequence of minimally-encoded base-128 integers (see
|
||||
// |parse_base128_integer|). If |prev|'s MSB was clear, it was the last byte
|
||||
// of an integer (or |v| is the first byte). |v| is then the first byte of
|
||||
// the next integer. If first byte of an integer is 0x80, it is not
|
||||
// minimally-encoded.
|
||||
if ((prev & 0x80) == 0 && v == 0x80) {
|
||||
return 0;
|
||||
}
|
||||
prev = v;
|
||||
}
|
||||
|
||||
// The last byte should must end an integer encoding.
|
||||
return (prev & 0x80) == 0;
|
||||
}
|
||||
|
||||
char *CBS_asn1_oid_to_text(const CBS *cbs) {
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 32)) {
|
||||
@@ -729,13 +772,13 @@ static int cbs_get_two_digits(CBS *cbs, int *out) {
|
||||
if (!CBS_get_u8(cbs, &first_digit)) {
|
||||
return 0;
|
||||
}
|
||||
if (!isdigit(first_digit)) {
|
||||
if (!OPENSSL_isdigit(first_digit)) {
|
||||
return 0;
|
||||
}
|
||||
if (!CBS_get_u8(cbs, &second_digit)) {
|
||||
return 0;
|
||||
}
|
||||
if (!isdigit(second_digit)) {
|
||||
if (!OPENSSL_isdigit(second_digit)) {
|
||||
return 0;
|
||||
}
|
||||
*out = (first_digit - '0') * 10 + (second_digit - '0');
|
||||
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+3
@@ -976,6 +976,9 @@ db 60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111
|
||||
db 114,103,62,0
|
||||
segment .bss
|
||||
common _OPENSSL_ia32cap_P 16
|
||||
%else
|
||||
; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738
|
||||
ret
|
||||
%endif
|
||||
#endif // defined(__i386__) && defined(_WIN32)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
+2
@@ -18,6 +18,7 @@
|
||||
.extern OPENSSL_ia32cap_P
|
||||
.hidden OPENSSL_ia32cap_P
|
||||
|
||||
.section .rodata
|
||||
.align 64
|
||||
.Lzero:
|
||||
.long 0,0,0,0
|
||||
@@ -47,6 +48,7 @@
|
||||
.Lsixteen:
|
||||
.long 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
|
||||
.byte 67,104,97,67,104,97,50,48,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
|
||||
.text
|
||||
.globl ChaCha20_ctr32
|
||||
.hidden ChaCha20_ctr32
|
||||
.type ChaCha20_ctr32,@function
|
||||
+2
@@ -17,6 +17,7 @@
|
||||
|
||||
|
||||
|
||||
.section __DATA,__const
|
||||
.p2align 6
|
||||
L$zero:
|
||||
.long 0,0,0,0
|
||||
@@ -46,6 +47,7 @@ L$incz:
|
||||
L$sixteen:
|
||||
.long 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
|
||||
.byte 67,104,97,67,104,97,50,48,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
|
||||
.text
|
||||
.globl _ChaCha20_ctr32
|
||||
.private_extern _ChaCha20_ctr32
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
.data
|
||||
.section .rodata
|
||||
|
||||
.align 16
|
||||
one:
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
.data
|
||||
.section __DATA,__const
|
||||
|
||||
.p2align 4
|
||||
one:
|
||||
File renamed without changes.
File renamed without changes.
+2
@@ -19,6 +19,7 @@
|
||||
|
||||
chacha20_poly1305_constants:
|
||||
|
||||
.section .rodata
|
||||
.align 64
|
||||
.Lchacha20_consts:
|
||||
.byte 'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k'
|
||||
@@ -56,6 +57,7 @@ chacha20_poly1305_constants:
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
|
||||
.text
|
||||
|
||||
.type poly_hash_ad_internal,@function
|
||||
.align 64
|
||||
+2
@@ -18,6 +18,7 @@
|
||||
|
||||
chacha20_poly1305_constants:
|
||||
|
||||
.section __DATA,__const
|
||||
.p2align 6
|
||||
L$chacha20_consts:
|
||||
.byte 'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k'
|
||||
@@ -55,6 +56,7 @@ L$and_masks:
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
|
||||
.text
|
||||
|
||||
|
||||
.p2align 6
|
||||
@@ -132,7 +132,6 @@ CONF *NCONF_new(void *method) {
|
||||
CONF_VALUE *CONF_VALUE_new(void) {
|
||||
CONF_VALUE *v = OPENSSL_malloc(sizeof(CONF_VALUE));
|
||||
if (!v) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
OPENSSL_memset(v, 0, sizeof(CONF_VALUE));
|
||||
@@ -340,7 +339,6 @@ static int str_copy(CONF *conf, char *section, char **pto, char *from) {
|
||||
goto err;
|
||||
}
|
||||
if (!BUF_MEM_grow_clean(buf, newsize)) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
while (*p) {
|
||||
@@ -552,7 +550,6 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
|
||||
section = OPENSSL_strdup(kDefaultSectionName);
|
||||
if (section == NULL) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -687,7 +684,6 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
}
|
||||
v->name = OPENSSL_strdup(pname);
|
||||
if (v->name == NULL) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
if (!str_copy(conf, psection, &(v->value), start)) {
|
||||
@@ -706,7 +702,6 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
tv = sv;
|
||||
}
|
||||
if (add_string(conf, tv, v) == 0) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
v = NULL;
|
||||
@@ -780,7 +775,7 @@ int CONF_parse_list(const char *list, char sep, int remove_whitespace,
|
||||
lstart = list;
|
||||
for (;;) {
|
||||
if (remove_whitespace) {
|
||||
while (*lstart && isspace((unsigned char)*lstart)) {
|
||||
while (*lstart && OPENSSL_isspace((unsigned char)*lstart)) {
|
||||
lstart++;
|
||||
}
|
||||
}
|
||||
@@ -794,7 +789,7 @@ int CONF_parse_list(const char *list, char sep, int remove_whitespace,
|
||||
tmpend = lstart + strlen(lstart) - 1;
|
||||
}
|
||||
if (remove_whitespace) {
|
||||
while (isspace((unsigned char)*tmpend)) {
|
||||
while (OPENSSL_isspace((unsigned char)*tmpend)) {
|
||||
tmpend--;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/auxv.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -26,13 +27,6 @@
|
||||
|
||||
#include "cpu_arm_linux.h"
|
||||
|
||||
#define AT_HWCAP 16
|
||||
#define AT_HWCAP2 26
|
||||
|
||||
// |getauxval| is not available on Android until API level 20. Link it as a weak
|
||||
// symbol and use other methods as fallback.
|
||||
unsigned long getauxval(unsigned long type) __attribute__((weak));
|
||||
|
||||
static int open_eintr(const char *path, int flags) {
|
||||
int ret;
|
||||
do {
|
||||
@@ -49,21 +43,6 @@ static ssize_t read_eintr(int fd, void *out, size_t len) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// read_full reads exactly |len| bytes from |fd| to |out|. On error or end of
|
||||
// file, it returns zero.
|
||||
static int read_full(int fd, void *out, size_t len) {
|
||||
char *outp = out;
|
||||
while (len > 0) {
|
||||
ssize_t ret = read_eintr(fd, outp, len);
|
||||
if (ret <= 0) {
|
||||
return 0;
|
||||
}
|
||||
outp += ret;
|
||||
len -= ret;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// read_file opens |path| and reads until end-of-file. On success, it returns
|
||||
// one and sets |*out_ptr| and |*out_len| to a newly-allocated buffer with the
|
||||
// contents. Otherwise, it returns zero.
|
||||
@@ -116,41 +95,14 @@ err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
// getauxval_proc behaves like |getauxval| but reads from /proc/self/auxv.
|
||||
static unsigned long getauxval_proc(unsigned long type) {
|
||||
int fd = open_eintr("/proc/self/auxv", O_RDONLY);
|
||||
if (fd < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct {
|
||||
unsigned long tag;
|
||||
unsigned long value;
|
||||
} entry;
|
||||
|
||||
for (;;) {
|
||||
if (!read_full(fd, &entry, sizeof(entry)) ||
|
||||
(entry.tag == 0 && entry.value == 0)) {
|
||||
break;
|
||||
}
|
||||
if (entry.tag == type) {
|
||||
close(fd);
|
||||
return entry.value;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
static int g_has_broken_neon, g_needs_hwcap2_workaround;
|
||||
static int g_needs_hwcap2_workaround;
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
// We ignore the return value of |read_file| and proceed with an empty
|
||||
// /proc/cpuinfo on error. If |getauxval| works, we will still detect
|
||||
// capabilities. There may be a false positive due to
|
||||
// |crypto_cpuinfo_has_broken_neon|, but this is now rare.
|
||||
// capabilities.
|
||||
char *cpuinfo_data = NULL;
|
||||
size_t cpuinfo_len = 0;
|
||||
read_file(&cpuinfo_data, &cpuinfo_len, "/proc/cpuinfo");
|
||||
@@ -158,37 +110,8 @@ void OPENSSL_cpuid_setup(void) {
|
||||
cpuinfo.data = cpuinfo_data;
|
||||
cpuinfo.len = cpuinfo_len;
|
||||
|
||||
// |getauxval| is not available on Android until API level 20. If it is
|
||||
// unavailable, read from /proc/self/auxv as a fallback. This is unreadable
|
||||
// on some versions of Android, so further fall back to /proc/cpuinfo.
|
||||
//
|
||||
// See
|
||||
// https://android.googlesource.com/platform/ndk/+/882ac8f3392858991a0e1af33b4b7387ec856bd2
|
||||
// and b/13679666 (Google-internal) for details.
|
||||
unsigned long hwcap = 0;
|
||||
if (getauxval != NULL) {
|
||||
hwcap = getauxval(AT_HWCAP);
|
||||
}
|
||||
if (hwcap == 0) {
|
||||
hwcap = getauxval_proc(AT_HWCAP);
|
||||
}
|
||||
if (hwcap == 0) {
|
||||
hwcap = crypto_get_arm_hwcap_from_cpuinfo(&cpuinfo);
|
||||
}
|
||||
|
||||
// Clear NEON support if known broken. Note, if NEON is available statically,
|
||||
// the non-NEON code is dropped and this workaround is a no-op.
|
||||
//
|
||||
// TODO(davidben): The Android NDK now builds with NEON statically available
|
||||
// by default. Cronet still has some consumers that support NEON-less devices
|
||||
// (b/150371744). Get metrics on whether they still see this CPU and, if not,
|
||||
// remove this check entirely.
|
||||
g_has_broken_neon = crypto_cpuinfo_has_broken_neon(&cpuinfo);
|
||||
if (g_has_broken_neon) {
|
||||
hwcap &= ~HWCAP_NEON;
|
||||
}
|
||||
|
||||
// Matching OpenSSL, only report other features if NEON is present.
|
||||
unsigned long hwcap = getauxval(AT_HWCAP);
|
||||
if (hwcap & HWCAP_NEON) {
|
||||
OPENSSL_armcap_P |= ARMV7_NEON;
|
||||
|
||||
@@ -197,10 +120,7 @@ void OPENSSL_cpuid_setup(void) {
|
||||
// this is now rare (see Chrome's Net.NeedsHWCAP2Workaround metric), but AES
|
||||
// and PMULL extensions are very useful, so we still carry the workaround
|
||||
// for now.
|
||||
unsigned long hwcap2 = 0;
|
||||
if (getauxval != NULL) {
|
||||
hwcap2 = getauxval(AT_HWCAP2);
|
||||
}
|
||||
unsigned long hwcap2 = getauxval(AT_HWCAP2);
|
||||
if (hwcap2 == 0) {
|
||||
hwcap2 = crypto_get_arm_hwcap2_from_cpuinfo(&cpuinfo);
|
||||
g_needs_hwcap2_workaround = hwcap2 != 0;
|
||||
@@ -223,7 +143,7 @@ void OPENSSL_cpuid_setup(void) {
|
||||
OPENSSL_free(cpuinfo_data);
|
||||
}
|
||||
|
||||
int CRYPTO_has_broken_NEON(void) { return g_has_broken_neon; }
|
||||
int CRYPTO_has_broken_NEON(void) { return 0; }
|
||||
|
||||
int CRYPTO_needs_hwcap2_workaround(void) { return g_needs_hwcap2_workaround; }
|
||||
|
||||
|
||||
@@ -118,13 +118,6 @@ static int extract_cpuinfo_field(STRING_PIECE *out, const STRING_PIECE *in,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cpuinfo_field_equals(const STRING_PIECE *cpuinfo, const char *field,
|
||||
const char *value) {
|
||||
STRING_PIECE extracted;
|
||||
return extract_cpuinfo_field(&extracted, cpuinfo, field) &&
|
||||
STRING_PIECE_equals(&extracted, value);
|
||||
}
|
||||
|
||||
// has_list_item treats |list| as a space-separated list of items and returns
|
||||
// one if |item| is contained in |list| and zero otherwise.
|
||||
static int has_list_item(const STRING_PIECE *list, const char *item) {
|
||||
@@ -137,27 +130,6 @@ static int has_list_item(const STRING_PIECE *list, const char *item) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// crypto_get_arm_hwcap_from_cpuinfo returns an equivalent ARM |AT_HWCAP| value
|
||||
// from |cpuinfo|.
|
||||
static unsigned long crypto_get_arm_hwcap_from_cpuinfo(
|
||||
const STRING_PIECE *cpuinfo) {
|
||||
if (cpuinfo_field_equals(cpuinfo, "CPU architecture", "8")) {
|
||||
// This is a 32-bit ARM binary running on a 64-bit kernel. NEON is always
|
||||
// available on ARMv8. Linux omits required features, so reading the
|
||||
// "Features" line does not work. (For simplicity, use strict equality. We
|
||||
// assume everything running on future ARM architectures will have a
|
||||
// working |getauxval|.)
|
||||
return HWCAP_NEON;
|
||||
}
|
||||
|
||||
STRING_PIECE features;
|
||||
if (extract_cpuinfo_field(&features, cpuinfo, "Features") &&
|
||||
has_list_item(&features, "neon")) {
|
||||
return HWCAP_NEON;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// crypto_get_arm_hwcap2_from_cpuinfo returns an equivalent ARM |AT_HWCAP2|
|
||||
// value from |cpuinfo|.
|
||||
static unsigned long crypto_get_arm_hwcap2_from_cpuinfo(
|
||||
@@ -183,16 +155,6 @@ static unsigned long crypto_get_arm_hwcap2_from_cpuinfo(
|
||||
return ret;
|
||||
}
|
||||
|
||||
// crypto_cpuinfo_has_broken_neon returns one if |cpuinfo| matches a CPU known
|
||||
// to have broken NEON unit and zero otherwise. See https://crbug.com/341598.
|
||||
static int crypto_cpuinfo_has_broken_neon(const STRING_PIECE *cpuinfo) {
|
||||
return cpuinfo_field_equals(cpuinfo, "CPU implementer", "0x51") &&
|
||||
cpuinfo_field_equals(cpuinfo, "CPU architecture", "7") &&
|
||||
cpuinfo_field_equals(cpuinfo, "CPU variant", "0x1") &&
|
||||
cpuinfo_field_equals(cpuinfo, "CPU part", "0x04d") &&
|
||||
cpuinfo_field_equals(cpuinfo, "CPU revision", "0");
|
||||
}
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/* Copyright (c) 2016, Google Inc.
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#if defined(OPENSSL_PPC64LE)
|
||||
|
||||
#include <sys/auxv.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
#if !defined(PPC_FEATURE2_HAS_VCRYPTO)
|
||||
// PPC_FEATURE2_HAS_VCRYPTO was taken from section 4.1.2.3 of the “OpenPOWER
|
||||
// ABI for Linux Supplement”.
|
||||
#define PPC_FEATURE2_HAS_VCRYPTO 0x02000000
|
||||
#endif
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
OPENSSL_ppc64le_hwcap2 = getauxval(AT_HWCAP2);
|
||||
}
|
||||
|
||||
int CRYPTO_is_PPC64LE_vcrypto_capable(void) {
|
||||
return (OPENSSL_ppc64le_hwcap2 & PPC_FEATURE2_HAS_VCRYPTO) != 0;
|
||||
}
|
||||
|
||||
#endif // OPENSSL_PPC64LE
|
||||
@@ -25,12 +25,10 @@ static_assert(sizeof(ossl_ssize_t) == sizeof(size_t),
|
||||
"ossl_ssize_t should be the same size as size_t");
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \
|
||||
(defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
|
||||
defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) || \
|
||||
defined(OPENSSL_PPC64LE))
|
||||
// x86, x86_64, the ARMs and ppc64le need to record the result of a
|
||||
// cpuid/getauxval call for the asm to work correctly, unless compiled without
|
||||
// asm code.
|
||||
(defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
|
||||
defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
|
||||
// x86, x86_64, and the ARMs need to record the result of a cpuid/getauxval call
|
||||
// for the asm to work correctly, unless compiled without asm code.
|
||||
#define NEED_CPUID
|
||||
|
||||
#else
|
||||
@@ -41,8 +39,7 @@ static_assert(sizeof(ossl_ssize_t) == sizeof(size_t),
|
||||
#define BORINGSSL_NO_STATIC_INITIALIZER
|
||||
#endif
|
||||
|
||||
#endif // !NO_ASM && !STATIC_ARMCAP &&
|
||||
// (X86 || X86_64 || ARM || AARCH64 || PPC64LE)
|
||||
#endif // !NO_ASM && !STATIC_ARMCAP && (X86 || X86_64 || ARM || AARCH64)
|
||||
|
||||
|
||||
// Our assembly does not use the GOT to reference symbols, which means
|
||||
@@ -81,10 +78,6 @@ HIDDEN uint8_t BORINGSSL_function_hit[7] = {0};
|
||||
// This value must be explicitly initialized to zero. See similar comment above.
|
||||
HIDDEN uint32_t OPENSSL_ia32cap_P[4] = {0};
|
||||
|
||||
#elif defined(OPENSSL_PPC64LE)
|
||||
|
||||
HIDDEN unsigned long OPENSSL_ppc64le_hwcap2 = 0;
|
||||
|
||||
#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
@@ -35,10 +35,6 @@
|
||||
// Various pre-computed constants.
|
||||
#include "./curve25519_tables.h"
|
||||
|
||||
#if defined(OPENSSL_NO_ASM)
|
||||
#define FIAT_25519_NO_ASM
|
||||
#endif
|
||||
|
||||
#if defined(BORINGSSL_CURVE25519_64BIT)
|
||||
#include "../../third_party/fiat/curve25519_64.h"
|
||||
#else
|
||||
|
||||
@@ -200,7 +200,6 @@ int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) {
|
||||
CBB algorithm, oid, null;
|
||||
if (!CBB_add_asn1(cbb, &algorithm, CBS_ASN1_SEQUENCE) ||
|
||||
!CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) {
|
||||
OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -209,7 +208,6 @@ int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) {
|
||||
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMDOIDs); i++) {
|
||||
if (nid == kMDOIDs[i].nid) {
|
||||
if (!CBB_add_bytes(&oid, kMDOIDs[i].oid, kMDOIDs[i].oid_len)) {
|
||||
OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
found = 1;
|
||||
@@ -224,7 +222,6 @@ int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) {
|
||||
|
||||
if (!CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
|
||||
!CBB_flush(cbb)) {
|
||||
OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,6 @@ static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
|
||||
DSA *DSA_new(void) {
|
||||
DSA *dsa = OPENSSL_malloc(sizeof(DSA));
|
||||
if (dsa == NULL) {
|
||||
OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -589,7 +588,12 @@ static int mod_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
|
||||
}
|
||||
|
||||
DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) {
|
||||
if (!dsa_check_parameters(dsa)) {
|
||||
if (!dsa_check_key(dsa)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (dsa->priv_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -610,6 +614,14 @@ DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Cap iterations so that invalid parameters do not infinite loop. This does
|
||||
// not impact valid parameters because the probability of requiring even one
|
||||
// retry is negligible, let alone 32. Unfortunately, DSA was mis-specified, so
|
||||
// invalid parameters are reachable from most callers handling untrusted
|
||||
// private keys. (The |dsa_check_key| call above is not sufficient. Checking
|
||||
// whether arbitrary paremeters form a valid DSA group is expensive.)
|
||||
static const int kMaxIterations = 32;
|
||||
int iters = 0;
|
||||
redo:
|
||||
if (!dsa_sign_setup(dsa, ctx, &kinv, &r)) {
|
||||
goto err;
|
||||
@@ -649,8 +661,14 @@ redo:
|
||||
// Redo if r or s is zero as required by FIPS 186-3: this is
|
||||
// very unlikely.
|
||||
if (BN_is_zero(r) || BN_is_zero(s)) {
|
||||
iters++;
|
||||
if (iters > kMaxIterations) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_TOO_MANY_ITERATIONS);
|
||||
goto err;
|
||||
}
|
||||
goto redo;
|
||||
}
|
||||
|
||||
ret = DSA_SIG_new();
|
||||
if (ret == NULL) {
|
||||
goto err;
|
||||
@@ -684,7 +702,12 @@ int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
|
||||
int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
|
||||
size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
|
||||
*out_valid = 0;
|
||||
if (!dsa_check_parameters(dsa)) {
|
||||
if (!dsa_check_key(dsa)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dsa->pub_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -843,6 +866,10 @@ static size_t der_len_len(size_t len) {
|
||||
}
|
||||
|
||||
int DSA_size(const DSA *dsa) {
|
||||
if (dsa->q == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t order_len = BN_num_bytes(dsa->q);
|
||||
// Compute the maximum length of an |order_len| byte integer. Defensively
|
||||
// assume that the leading 0x00 is included.
|
||||
@@ -865,11 +892,6 @@ int DSA_size(const DSA *dsa) {
|
||||
|
||||
static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv,
|
||||
BIGNUM **out_r) {
|
||||
if (!dsa->p || !dsa->q || !dsa->g) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
BIGNUM k;
|
||||
BN_init(&k);
|
||||
|
||||
@@ -70,15 +70,25 @@
|
||||
// This function is in dsa_asn1.c rather than dsa.c because it is reachable from
|
||||
// |EVP_PKEY| parsers. This makes it easier for the static linker to drop most
|
||||
// of the DSA implementation.
|
||||
int dsa_check_parameters(const DSA *dsa) {
|
||||
int dsa_check_key(const DSA *dsa) {
|
||||
if (!dsa->p || !dsa->q || !dsa->g) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Reject invalid parameters. In particular, signing will infinite loop if |g|
|
||||
// is zero.
|
||||
if (BN_is_zero(dsa->p) || BN_is_zero(dsa->q) || BN_is_zero(dsa->g)) {
|
||||
// Fully checking for invalid DSA groups is expensive, so security and
|
||||
// correctness of the signature scheme depend on how |dsa| was computed. I.e.
|
||||
// we leave "assurance of domain parameter validity" from FIPS 186-4 to the
|
||||
// caller. However, we check bounds on all values to avoid DoS vectors even
|
||||
// when domain parameters are invalid. In particular, signing will infinite
|
||||
// loop if |g| is zero.
|
||||
if (BN_is_negative(dsa->p) || BN_is_negative(dsa->q) || BN_is_zero(dsa->p) ||
|
||||
BN_is_zero(dsa->q) || !BN_is_odd(dsa->p) || !BN_is_odd(dsa->q) ||
|
||||
// |q| must be a prime divisor of |p - 1|, which implies |q < p|.
|
||||
BN_cmp(dsa->q, dsa->p) >= 0 ||
|
||||
// |g| is in the multiplicative group of |p|.
|
||||
BN_is_negative(dsa->g) || BN_is_zero(dsa->g) ||
|
||||
BN_cmp(dsa->g, dsa->p) >= 0) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
@@ -97,6 +107,25 @@ int dsa_check_parameters(const DSA *dsa) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dsa->pub_key != NULL) {
|
||||
// The public key is also in the multiplicative group of |p|.
|
||||
if (BN_is_negative(dsa->pub_key) || BN_is_zero(dsa->pub_key) ||
|
||||
BN_cmp(dsa->pub_key, dsa->p) >= 0) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (dsa->priv_key != NULL) {
|
||||
// The private key is a non-zero element of the scalar field, determined by
|
||||
// |q|.
|
||||
if (BN_is_negative(dsa->priv_key) || BN_is_zero(dsa->priv_key) ||
|
||||
BN_cmp(dsa->priv_key, dsa->q) >= 0) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -162,7 +191,7 @@ DSA *DSA_parse_public_key(CBS *cbs) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (!dsa_check_parameters(ret)) {
|
||||
if (!dsa_check_key(ret)) {
|
||||
goto err;
|
||||
}
|
||||
return ret;
|
||||
@@ -200,7 +229,7 @@ DSA *DSA_parse_parameters(CBS *cbs) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (!dsa_check_parameters(ret)) {
|
||||
if (!dsa_check_key(ret)) {
|
||||
goto err;
|
||||
}
|
||||
return ret;
|
||||
@@ -251,9 +280,10 @@ DSA *DSA_parse_private_key(CBS *cbs) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (!dsa_check_parameters(ret)) {
|
||||
if (!dsa_check_key(ret)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
err:
|
||||
|
||||
@@ -22,9 +22,9 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// dsa_check_parameters checks that |dsa|'s group is within DoS bounds. It
|
||||
// returns one on success and zero on error.
|
||||
int dsa_check_parameters(const DSA *dsa);
|
||||
// dsa_check_key performs cheap self-checks on |dsa|, and ensures it is within
|
||||
// DoS bounds. It returns one on success and zero on error.
|
||||
int dsa_check_key(const DSA *dsa);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
||||
@@ -504,7 +504,6 @@ EC_KEY *o2i_ECPublicKey(EC_KEY **keyp, const uint8_t **inp, long len) {
|
||||
ret = *keyp;
|
||||
if (ret->pub_key == NULL &&
|
||||
(ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
if (!EC_POINT_oct2point(ret->group, ret->pub_key, *inp, len, NULL)) {
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
|
||||
// This file implements hash-to-curve, as described in
|
||||
// draft-irtf-cfrg-hash-to-curve-07.
|
||||
// draft-irtf-cfrg-hash-to-curve-16.
|
||||
//
|
||||
// This hash-to-curve implementation is written generically with the
|
||||
// expectation that we will eventually wish to support other curves. If it
|
||||
@@ -48,11 +48,17 @@
|
||||
// templates to make specializing more convenient.
|
||||
|
||||
// expand_message_xmd implements the operation described in section 5.3.1 of
|
||||
// draft-irtf-cfrg-hash-to-curve-07. It returns one on success and zero on
|
||||
// allocation failure or if |out_len| was too large.
|
||||
// draft-irtf-cfrg-hash-to-curve-16. It returns one on success and zero on
|
||||
// error.
|
||||
static int expand_message_xmd(const EVP_MD *md, uint8_t *out, size_t out_len,
|
||||
const uint8_t *msg, size_t msg_len,
|
||||
const uint8_t *dst, size_t dst_len) {
|
||||
// See https://github.com/cfrg/draft-irtf-cfrg-hash-to-curve/issues/352
|
||||
if (dst_len == 0) {
|
||||
OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
const size_t block_size = EVP_MD_block_size(md);
|
||||
const size_t md_size = EVP_MD_size(md);
|
||||
@@ -132,7 +138,7 @@ err:
|
||||
|
||||
// num_bytes_to_derive determines the number of bytes to derive when hashing to
|
||||
// a number modulo |modulus|. See the hash_to_field operation defined in
|
||||
// section 5.2 of draft-irtf-cfrg-hash-to-curve-07.
|
||||
// section 5.2 of draft-irtf-cfrg-hash-to-curve-16.
|
||||
static int num_bytes_to_derive(size_t *out, const BIGNUM *modulus, unsigned k) {
|
||||
size_t bits = BN_num_bits(modulus);
|
||||
size_t L = (bits + k + 7) / 8;
|
||||
@@ -165,7 +171,7 @@ static void big_endian_to_words(BN_ULONG *out, size_t num_words,
|
||||
}
|
||||
|
||||
// hash_to_field implements the operation described in section 5.2
|
||||
// of draft-irtf-cfrg-hash-to-curve-07, with count = 2. |k| is the security
|
||||
// of draft-irtf-cfrg-hash-to-curve-16, with count = 2. |k| is the security
|
||||
// factor.
|
||||
static int hash_to_field2(const EC_GROUP *group, const EVP_MD *md,
|
||||
EC_FELEM *out1, EC_FELEM *out2, const uint8_t *dst,
|
||||
@@ -214,90 +220,126 @@ static inline void mul_A(const EC_GROUP *group, EC_FELEM *out,
|
||||
ec_felem_sub(group, out, in, &tmp); // out = -3*in
|
||||
}
|
||||
|
||||
static inline void mul_minus_A(const EC_GROUP *group, EC_FELEM *out,
|
||||
const EC_FELEM *in) {
|
||||
assert(group->a_is_minus3);
|
||||
EC_FELEM tmp;
|
||||
ec_felem_add(group, &tmp, in, in); // tmp = 2*in
|
||||
ec_felem_add(group, out, &tmp, in); // out = 3*in
|
||||
}
|
||||
|
||||
// sgn0_le implements the operation described in section 4.1.2 of
|
||||
// draft-irtf-cfrg-hash-to-curve-07.
|
||||
static BN_ULONG sgn0_le(const EC_GROUP *group, const EC_FELEM *a) {
|
||||
// sgn0 implements the operation described in section 4.1.2 of
|
||||
// draft-irtf-cfrg-hash-to-curve-16.
|
||||
static BN_ULONG sgn0(const EC_GROUP *group, const EC_FELEM *a) {
|
||||
uint8_t buf[EC_MAX_BYTES];
|
||||
size_t len;
|
||||
ec_felem_to_bytes(group, buf, &len, a);
|
||||
return buf[len - 1] & 1;
|
||||
}
|
||||
|
||||
// map_to_curve_simple_swu implements the operation described in section 6.6.2
|
||||
// of draft-irtf-cfrg-hash-to-curve-07, using the optimization in appendix
|
||||
// D.2.1. It returns one on success and zero on error.
|
||||
static int map_to_curve_simple_swu(const EC_GROUP *group, const EC_FELEM *Z,
|
||||
const BN_ULONG *c1, size_t num_c1,
|
||||
const EC_FELEM *c2, EC_RAW_POINT *out,
|
||||
const EC_FELEM *u) {
|
||||
OPENSSL_UNUSED static int is_3mod4(const EC_GROUP *group) {
|
||||
return group->field.width > 0 && (group->field.d[0] & 3) == 3;
|
||||
}
|
||||
|
||||
// sqrt_ratio_3mod4 implements the operation described in appendix F.2.1.2
|
||||
// of draft-irtf-cfrg-hash-to-curve-16.
|
||||
static BN_ULONG sqrt_ratio_3mod4(const EC_GROUP *group, const EC_FELEM *Z,
|
||||
const BN_ULONG *c1, size_t num_c1,
|
||||
const EC_FELEM *c2, EC_FELEM *out_y,
|
||||
const EC_FELEM *u, const EC_FELEM *v) {
|
||||
assert(is_3mod4(group));
|
||||
|
||||
void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
|
||||
const EC_FELEM *b) = group->meth->felem_mul;
|
||||
void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
|
||||
group->meth->felem_sqr;
|
||||
|
||||
EC_FELEM tv1, tv2, tv3, y1, y2;
|
||||
felem_sqr(group, &tv1, v); // 1. tv1 = v^2
|
||||
felem_mul(group, &tv2, u, v); // 2. tv2 = u * v
|
||||
felem_mul(group, &tv1, &tv1, &tv2); // 3. tv1 = tv1 * tv2
|
||||
group->meth->felem_exp(group, &y1, &tv1, c1, num_c1); // 4. y1 = tv1^c1
|
||||
felem_mul(group, &y1, &y1, &tv2); // 5. y1 = y1 * tv2
|
||||
felem_mul(group, &y2, &y1, c2); // 6. y2 = y1 * c2
|
||||
felem_sqr(group, &tv3, &y1); // 7. tv3 = y1^2
|
||||
felem_mul(group, &tv3, &tv3, v); // 8. tv3 = tv3 * v
|
||||
|
||||
// 9. isQR = tv3 == u
|
||||
// 10. y = CMOV(y2, y1, isQR)
|
||||
// 11. return (isQR, y)
|
||||
//
|
||||
// Note the specification's CMOV function and our |ec_felem_select| have the
|
||||
// opposite argument order.
|
||||
ec_felem_sub(group, &tv1, &tv3, u);
|
||||
const BN_ULONG isQR = ~ec_felem_non_zero_mask(group, &tv1);
|
||||
ec_felem_select(group, out_y, isQR, &y1, &y2);
|
||||
return isQR;
|
||||
}
|
||||
|
||||
// map_to_curve_simple_swu implements the operation described in section 6.6.2
|
||||
// of draft-irtf-cfrg-hash-to-curve-16, using the straight-line implementation
|
||||
// in appendix F.2.
|
||||
static void map_to_curve_simple_swu(const EC_GROUP *group, const EC_FELEM *Z,
|
||||
const BN_ULONG *c1, size_t num_c1,
|
||||
const EC_FELEM *c2, EC_RAW_POINT *out,
|
||||
const EC_FELEM *u) {
|
||||
// This function requires the prime be 3 mod 4, and that A = -3.
|
||||
if (group->field.width == 0 || (group->field.d[0] & 3) != 3 ||
|
||||
!group->a_is_minus3) {
|
||||
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
assert(is_3mod4(group));
|
||||
assert(group->a_is_minus3);
|
||||
|
||||
EC_FELEM tv1, tv2, tv3, tv4, xd, x1n, x2n, tmp, gxd, gx1, y1, y2;
|
||||
felem_sqr(group, &tv1, u); // tv1 = u^2
|
||||
felem_mul(group, &tv3, Z, &tv1); // tv3 = Z * tv1
|
||||
felem_sqr(group, &tv2, &tv3); // tv2 = tv3^2
|
||||
ec_felem_add(group, &xd, &tv2, &tv3); // xd = tv2 + tv3
|
||||
ec_felem_add(group, &x1n, &xd, &group->one); // x1n = xd + 1
|
||||
felem_mul(group, &x1n, &x1n, &group->b); // x1n = x1n * B
|
||||
mul_minus_A(group, &xd, &xd); // xd = -A * xd
|
||||
BN_ULONG e1 = ec_felem_non_zero_mask(group, &xd); // e1 = xd == 0 [flipped]
|
||||
mul_A(group, &tmp, Z);
|
||||
ec_felem_select(group, &xd, e1, &xd, &tmp); // xd = CMOV(xd, Z * A, e1)
|
||||
felem_sqr(group, &tv2, &xd); // tv2 = xd^2
|
||||
felem_mul(group, &gxd, &tv2, &xd); // gxd = tv2 * xd = xd^3
|
||||
mul_A(group, &tv2, &tv2); // tv2 = A * tv2
|
||||
felem_sqr(group, &gx1, &x1n); // gx1 = x1n^2
|
||||
ec_felem_add(group, &gx1, &gx1, &tv2); // gx1 = gx1 + tv2
|
||||
felem_mul(group, &gx1, &gx1, &x1n); // gx1 = gx1 * x1n
|
||||
felem_mul(group, &tv2, &group->b, &gxd); // tv2 = B * gxd
|
||||
ec_felem_add(group, &gx1, &gx1, &tv2); // gx1 = gx1 + tv2
|
||||
felem_sqr(group, &tv4, &gxd); // tv4 = gxd^2
|
||||
felem_mul(group, &tv2, &gx1, &gxd); // tv2 = gx1 * gxd
|
||||
felem_mul(group, &tv4, &tv4, &tv2); // tv4 = tv4 * tv2
|
||||
group->meth->felem_exp(group, &y1, &tv4, c1, num_c1); // y1 = tv4^c1
|
||||
felem_mul(group, &y1, &y1, &tv2); // y1 = y1 * tv2
|
||||
felem_mul(group, &x2n, &tv3, &x1n); // x2n = tv3 * x1n
|
||||
felem_mul(group, &y2, &y1, c2); // y2 = y1 * c2
|
||||
felem_mul(group, &y2, &y2, &tv1); // y2 = y2 * tv1
|
||||
felem_mul(group, &y2, &y2, u); // y2 = y2 * u
|
||||
felem_sqr(group, &tv2, &y1); // tv2 = y1^2
|
||||
felem_mul(group, &tv2, &tv2, &gxd); // tv2 = tv2 * gxd
|
||||
ec_felem_sub(group, &tv3, &tv2, &gx1);
|
||||
BN_ULONG e2 =
|
||||
ec_felem_non_zero_mask(group, &tv3); // e2 = tv2 == gx1 [flipped]
|
||||
ec_felem_select(group, &x1n, e2, &x2n, &x1n); // xn = CMOV(x2n, x1n, e2)
|
||||
ec_felem_select(group, &y1, e2, &y2, &y1); // y = CMOV(y2, y1, e2)
|
||||
BN_ULONG sgn0_u = sgn0_le(group, u);
|
||||
BN_ULONG sgn0_y = sgn0_le(group, &y1);
|
||||
BN_ULONG e3 = sgn0_u ^ sgn0_y;
|
||||
e3 = ((BN_ULONG)0) - e3; // e3 = sgn0(u) == sgn0(y) [flipped]
|
||||
ec_felem_neg(group, &y2, &y1);
|
||||
ec_felem_select(group, &y1, e3, &y2, &y1); // y = CMOV(-y, y, e3)
|
||||
void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
|
||||
const EC_FELEM *b) = group->meth->felem_mul;
|
||||
void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
|
||||
group->meth->felem_sqr;
|
||||
|
||||
// Appendix D.1 describes how to convert (x1n, xd, y1, 1) to Jacobian
|
||||
// coordinates. Note yd = 1. Also note that gxd computed above is xd^3.
|
||||
felem_mul(group, &out->X, &x1n, &xd); // X = xn * xd
|
||||
felem_mul(group, &out->Y, &y1, &gxd); // Y = yn * gxd = yn * xd^3
|
||||
out->Z = xd; // Z = xd
|
||||
return 1;
|
||||
EC_FELEM tv1, tv2, tv3, tv4, tv5, tv6, x, y, y1;
|
||||
felem_sqr(group, &tv1, u); // 1. tv1 = u^2
|
||||
felem_mul(group, &tv1, Z, &tv1); // 2. tv1 = Z * tv1
|
||||
felem_sqr(group, &tv2, &tv1); // 3. tv2 = tv1^2
|
||||
ec_felem_add(group, &tv2, &tv2, &tv1); // 4. tv2 = tv2 + tv1
|
||||
ec_felem_add(group, &tv3, &tv2, &group->one); // 5. tv3 = tv2 + 1
|
||||
felem_mul(group, &tv3, &group->b, &tv3); // 6. tv3 = B * tv3
|
||||
|
||||
// 7. tv4 = CMOV(Z, -tv2, tv2 != 0)
|
||||
const BN_ULONG tv2_non_zero = ec_felem_non_zero_mask(group, &tv2);
|
||||
ec_felem_neg(group, &tv4, &tv2);
|
||||
ec_felem_select(group, &tv4, tv2_non_zero, &tv4, Z);
|
||||
|
||||
mul_A(group, &tv4, &tv4); // 8. tv4 = A * tv4
|
||||
felem_sqr(group, &tv2, &tv3); // 9. tv2 = tv3^2
|
||||
felem_sqr(group, &tv6, &tv4); // 10. tv6 = tv4^2
|
||||
mul_A(group, &tv5, &tv6); // 11. tv5 = A * tv6
|
||||
ec_felem_add(group, &tv2, &tv2, &tv5); // 12. tv2 = tv2 + tv5
|
||||
felem_mul(group, &tv2, &tv2, &tv3); // 13. tv2 = tv2 * tv3
|
||||
felem_mul(group, &tv6, &tv6, &tv4); // 14. tv6 = tv6 * tv4
|
||||
felem_mul(group, &tv5, &group->b, &tv6); // 15. tv5 = B * tv6
|
||||
ec_felem_add(group, &tv2, &tv2, &tv5); // 16. tv2 = tv2 + tv5
|
||||
felem_mul(group, &x, &tv1, &tv3); // 17. x = tv1 * tv3
|
||||
|
||||
// 18. (is_gx1_square, y1) = sqrt_ratio(tv2, tv6)
|
||||
const BN_ULONG is_gx1_square =
|
||||
sqrt_ratio_3mod4(group, Z, c1, num_c1, c2, &y1, &tv2, &tv6);
|
||||
|
||||
felem_mul(group, &y, &tv1, u); // 19. y = tv1 * u
|
||||
felem_mul(group, &y, &y, &y1); // 20. y = y * y1
|
||||
|
||||
// 21. x = CMOV(x, tv3, is_gx1_square)
|
||||
ec_felem_select(group, &x, is_gx1_square, &tv3, &x);
|
||||
// 22. y = CMOV(y, y1, is_gx1_square)
|
||||
ec_felem_select(group, &y, is_gx1_square, &y1, &y);
|
||||
|
||||
// 23. e1 = sgn0(u) == sgn0(y)
|
||||
BN_ULONG sgn0_u = sgn0(group, u);
|
||||
BN_ULONG sgn0_y = sgn0(group, &y);
|
||||
BN_ULONG not_e1 = sgn0_u ^ sgn0_y;
|
||||
not_e1 = ((BN_ULONG)0) - not_e1;
|
||||
|
||||
// 24. y = CMOV(-y, y, e1)
|
||||
ec_felem_neg(group, &tv1, &y);
|
||||
ec_felem_select(group, &y, not_e1, &tv1, &y);
|
||||
|
||||
// 25. x = x / tv4
|
||||
//
|
||||
// Our output is in projective coordinates, so rather than inverting |tv4|
|
||||
// now, represent (x / tv4, y) as (x * tv4, y * tv4^3, tv4). This is much more
|
||||
// efficient if the caller will do further computation on the output. (If the
|
||||
// caller will immediately convert to affine coordinates, it is slightly less
|
||||
// efficient, but only by a few field multiplications.)
|
||||
felem_mul(group, &out->X, &x, &tv4);
|
||||
felem_mul(group, &out->Y, &y, &tv6);
|
||||
out->Z = tv4;
|
||||
}
|
||||
|
||||
static int hash_to_curve(const EC_GROUP *group, const EVP_MD *md,
|
||||
@@ -318,10 +360,8 @@ static int hash_to_curve(const EC_GROUP *group, const EVP_MD *md,
|
||||
bn_rshift_words(c1, c1, /*shift=*/2, /*num=*/num_c1);
|
||||
|
||||
EC_RAW_POINT Q0, Q1;
|
||||
if (!map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q0, &u0) ||
|
||||
!map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q1, &u1)) {
|
||||
return 0;
|
||||
}
|
||||
map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q0, &u0);
|
||||
map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q1, &u1);
|
||||
|
||||
group->meth->add(group, out, &Q0, &Q1); // R = Q0 + Q1
|
||||
// All our curves have cofactor one, so |clear_cofactor| is a no-op.
|
||||
@@ -335,6 +375,109 @@ static int felem_from_u8(const EC_GROUP *group, EC_FELEM *out, uint8_t a) {
|
||||
return ec_felem_from_bytes(group, out, bytes, len);
|
||||
}
|
||||
|
||||
// kP256Sqrt10 is sqrt(10) in P-256's field. It was computed as follows in
|
||||
// python3:
|
||||
//
|
||||
// p = 2**256 - 2**224 + 2**192 + 2**96 - 1
|
||||
// c2 = pow(10, (p+1)//4, p)
|
||||
// assert pow(c2, 2, p) == 10
|
||||
// ", ".join("0x%02x" % b for b in c2.to_bytes(256//8, 'big'))
|
||||
static const uint8_t kP256Sqrt10[] = {
|
||||
0xda, 0x53, 0x8e, 0x3b, 0xe1, 0xd8, 0x9b, 0x99, 0xc9, 0x78, 0xfc,
|
||||
0x67, 0x51, 0x80, 0xaa, 0xb2, 0x7b, 0x8d, 0x1f, 0xf8, 0x4c, 0x55,
|
||||
0xd5, 0xb6, 0x2c, 0xcd, 0x34, 0x27, 0xe4, 0x33, 0xc4, 0x7f};
|
||||
|
||||
// kP384Sqrt12 is sqrt(12) in P-384's field. It was computed as follows in
|
||||
// python3:
|
||||
//
|
||||
// p = 2**384 - 2**128 - 2**96 + 2**32 - 1
|
||||
// c2 = pow(12, (p+1)//4, p)
|
||||
// assert pow(c2, 2, p) == 12
|
||||
// ", ".join("0x%02x" % b for b in c2.to_bytes(384//8, 'big'))
|
||||
static const uint8_t kP384Sqrt12[] = {
|
||||
0x2a, 0xcc, 0xb4, 0xa6, 0x56, 0xb0, 0x24, 0x9c, 0x71, 0xf0, 0x50, 0x0e,
|
||||
0x83, 0xda, 0x2f, 0xdd, 0x7f, 0x98, 0xe3, 0x83, 0xd6, 0x8b, 0x53, 0x87,
|
||||
0x1f, 0x87, 0x2f, 0xcb, 0x9c, 0xcb, 0x80, 0xc5, 0x3c, 0x0d, 0xe1, 0xf8,
|
||||
0xa8, 0x0f, 0x7e, 0x19, 0x14, 0xe2, 0xec, 0x69, 0xf5, 0xa6, 0x26, 0xb3};
|
||||
|
||||
int ec_hash_to_curve_p256_xmd_sha256_sswu(const EC_GROUP *group,
|
||||
EC_RAW_POINT *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg,
|
||||
size_t msg_len) {
|
||||
// See section 8.3 of draft-irtf-cfrg-hash-to-curve-16.
|
||||
if (EC_GROUP_get_curve_name(group) != NID_X9_62_prime256v1) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Z = -10, c2 = sqrt(10)
|
||||
EC_FELEM Z, c2;
|
||||
if (!felem_from_u8(group, &Z, 10) ||
|
||||
!ec_felem_from_bytes(group, &c2, kP256Sqrt10, sizeof(kP256Sqrt10))) {
|
||||
return 0;
|
||||
}
|
||||
ec_felem_neg(group, &Z, &Z);
|
||||
|
||||
return hash_to_curve(group, EVP_sha256(), &Z, &c2, /*k=*/128, out, dst,
|
||||
dst_len, msg, msg_len);
|
||||
}
|
||||
|
||||
int EC_hash_to_curve_p256_xmd_sha256_sswu(const EC_GROUP *group, EC_POINT *out,
|
||||
const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len) {
|
||||
if (EC_GROUP_cmp(group, out->group, NULL) != 0) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
|
||||
return 0;
|
||||
}
|
||||
return ec_hash_to_curve_p256_xmd_sha256_sswu(group, &out->raw, dst, dst_len,
|
||||
msg, msg_len);
|
||||
}
|
||||
|
||||
int ec_hash_to_curve_p384_xmd_sha384_sswu(const EC_GROUP *group,
|
||||
EC_RAW_POINT *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg,
|
||||
size_t msg_len) {
|
||||
// See section 8.3 of draft-irtf-cfrg-hash-to-curve-16.
|
||||
if (EC_GROUP_get_curve_name(group) != NID_secp384r1) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Z = -12, c2 = sqrt(12)
|
||||
EC_FELEM Z, c2;
|
||||
if (!felem_from_u8(group, &Z, 12) ||
|
||||
!ec_felem_from_bytes(group, &c2, kP384Sqrt12, sizeof(kP384Sqrt12))) {
|
||||
return 0;
|
||||
}
|
||||
ec_felem_neg(group, &Z, &Z);
|
||||
|
||||
return hash_to_curve(group, EVP_sha384(), &Z, &c2, /*k=*/192, out, dst,
|
||||
dst_len, msg, msg_len);
|
||||
}
|
||||
|
||||
int EC_hash_to_curve_p384_xmd_sha384_sswu(const EC_GROUP *group, EC_POINT *out,
|
||||
const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len) {
|
||||
if (EC_GROUP_cmp(group, out->group, NULL) != 0) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
|
||||
return 0;
|
||||
}
|
||||
return ec_hash_to_curve_p384_xmd_sha384_sswu(group, &out->raw, dst, dst_len,
|
||||
msg, msg_len);
|
||||
}
|
||||
|
||||
int ec_hash_to_scalar_p384_xmd_sha384(
|
||||
const EC_GROUP *group, EC_SCALAR *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len) {
|
||||
if (EC_GROUP_get_curve_name(group) != NID_secp384r1) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return hash_to_scalar(group, EVP_sha384(), out, dst, dst_len, /*k=*/192, msg,
|
||||
msg_len);
|
||||
}
|
||||
|
||||
int ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
|
||||
const EC_GROUP *group, EC_RAW_POINT *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg, size_t msg_len) {
|
||||
@@ -344,25 +487,10 @@ int ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
|
||||
return 0;
|
||||
}
|
||||
|
||||
// kSqrt1728 was computed as follows in python3:
|
||||
//
|
||||
// p = 2**384 - 2**128 - 2**96 + 2**32 - 1
|
||||
// z3 = 12**3
|
||||
// c2 = pow(z3, (p+1)//4, p)
|
||||
// assert z3 == pow(c2, 2, p)
|
||||
// ", ".join("0x%02x" % b for b in c2.to_bytes(384//8, 'big')
|
||||
|
||||
static const uint8_t kSqrt1728[] = {
|
||||
0x01, 0x98, 0x77, 0xcc, 0x10, 0x41, 0xb7, 0x55, 0x57, 0x43, 0xc0, 0xae,
|
||||
0x2e, 0x3a, 0x3e, 0x61, 0xfb, 0x2a, 0xaa, 0x2e, 0x0e, 0x87, 0xea, 0x55,
|
||||
0x7a, 0x56, 0x3d, 0x8b, 0x59, 0x8a, 0x09, 0x40, 0xd0, 0xa6, 0x97, 0xa9,
|
||||
0xe0, 0xb9, 0xe9, 0x2c, 0xfa, 0xa3, 0x14, 0xf5, 0x83, 0xc9, 0xd0, 0x66
|
||||
};
|
||||
|
||||
// Z = -12, c2 = sqrt(1728)
|
||||
// Z = -12, c2 = sqrt(12)
|
||||
EC_FELEM Z, c2;
|
||||
if (!felem_from_u8(group, &Z, 12) ||
|
||||
!ec_felem_from_bytes(group, &c2, kSqrt1728, sizeof(kSqrt1728))) {
|
||||
!ec_felem_from_bytes(group, &c2, kP384Sqrt12, sizeof(kP384Sqrt12))) {
|
||||
return 0;
|
||||
}
|
||||
ec_felem_neg(group, &Z, &Z);
|
||||
|
||||
@@ -26,16 +26,38 @@ extern "C" {
|
||||
|
||||
// Hash-to-curve.
|
||||
//
|
||||
// The following functions implement primitives from
|
||||
// draft-irtf-cfrg-hash-to-curve. The |dst| parameter in each function is the
|
||||
// domain separation tag and must be unique for each protocol and between the
|
||||
// |hash_to_curve| and |hash_to_scalar| variants. See section 3.1 of the spec
|
||||
// for additional guidance on this parameter.
|
||||
// Internal |EC_RAW_POINT| versions of the corresponding public APIs.
|
||||
|
||||
// ec_hash_to_curve_p256_xmd_sha256_sswu hashes |msg| to a point on |group| and
|
||||
// writes the result to |out|, implementing the P256_XMD:SHA-256_SSWU_RO_ suite
|
||||
// from draft-irtf-cfrg-hash-to-curve-16. It returns one on success and zero on
|
||||
// error.
|
||||
OPENSSL_EXPORT int ec_hash_to_curve_p256_xmd_sha256_sswu(
|
||||
const EC_GROUP *group, EC_RAW_POINT *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg, size_t msg_len);
|
||||
|
||||
// ec_hash_to_curve_p384_xmd_sha384_sswu hashes |msg| to a point on |group| and
|
||||
// writes the result to |out|, implementing the P384_XMD:SHA-384_SSWU_RO_ suite
|
||||
// from draft-irtf-cfrg-hash-to-curve-16. It returns one on success and zero on
|
||||
// error.
|
||||
OPENSSL_EXPORT int ec_hash_to_curve_p384_xmd_sha384_sswu(
|
||||
const EC_GROUP *group, EC_RAW_POINT *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg, size_t msg_len);
|
||||
|
||||
// ec_hash_to_scalar_p384_xmd_sha384 hashes |msg| to a scalar on |group|
|
||||
// and writes the result to |out|, using the hash_to_field operation from the
|
||||
// P384_XMD:SHA-384_SSWU_RO_ suite from draft-irtf-cfrg-hash-to-curve-16, but
|
||||
// generating a value modulo the group order rather than a field element.
|
||||
OPENSSL_EXPORT int ec_hash_to_scalar_p384_xmd_sha384(
|
||||
const EC_GROUP *group, EC_SCALAR *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len);
|
||||
|
||||
// ec_hash_to_curve_p384_xmd_sha512_sswu_draft07 hashes |msg| to a point on
|
||||
// |group| and writes the result to |out|, implementing the
|
||||
// P384_XMD:SHA-512_SSWU_RO_ suite from draft-irtf-cfrg-hash-to-curve-07. It
|
||||
// returns one on success and zero on error.
|
||||
//
|
||||
// TODO(https://crbug.com/1414562): Migrate this to the final version.
|
||||
OPENSSL_EXPORT int ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
|
||||
const EC_GROUP *group, EC_RAW_POINT *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg, size_t msg_len);
|
||||
@@ -44,6 +66,8 @@ OPENSSL_EXPORT int ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
|
||||
// and writes the result to |out|, using the hash_to_field operation from the
|
||||
// P384_XMD:SHA-512_SSWU_RO_ suite from draft-irtf-cfrg-hash-to-curve-07, but
|
||||
// generating a value modulo the group order rather than a field element.
|
||||
//
|
||||
// TODO(https://crbug.com/1414562): Migrate this to the final version.
|
||||
OPENSSL_EXPORT int ec_hash_to_scalar_p384_xmd_sha512_draft07(
|
||||
const EC_GROUP *group, EC_SCALAR *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len);
|
||||
|
||||
@@ -106,11 +106,15 @@
|
||||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||
* Hudson (tjh@cryptsoft.com). */
|
||||
|
||||
// Ensure we can't call OPENSSL_malloc circularly.
|
||||
#define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
@@ -129,8 +133,8 @@ OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
struct err_error_st {
|
||||
// file contains the filename where the error occurred.
|
||||
const char *file;
|
||||
// data contains a NUL-terminated string with optional data. It must be freed
|
||||
// with |OPENSSL_free|.
|
||||
// data contains a NUL-terminated string with optional data. It is allocated
|
||||
// with system |malloc| and must be freed with |free| (not |OPENSSL_free|)
|
||||
char *data;
|
||||
// packed contains the error library and reason, as packed by ERR_PACK.
|
||||
uint32_t packed;
|
||||
@@ -162,7 +166,7 @@ extern const char kOpenSSLReasonStringData[];
|
||||
|
||||
// err_clear clears the given queued error.
|
||||
static void err_clear(struct err_error_st *error) {
|
||||
OPENSSL_free(error->data);
|
||||
free(error->data);
|
||||
OPENSSL_memset(error, 0, sizeof(struct err_error_st));
|
||||
}
|
||||
|
||||
@@ -170,12 +174,19 @@ static void err_copy(struct err_error_st *dst, const struct err_error_st *src) {
|
||||
err_clear(dst);
|
||||
dst->file = src->file;
|
||||
if (src->data != NULL) {
|
||||
dst->data = OPENSSL_strdup(src->data);
|
||||
// Disable deprecated functions on msvc so it doesn't complain about strdup.
|
||||
OPENSSL_MSVC_PRAGMA(warning(push))
|
||||
OPENSSL_MSVC_PRAGMA(warning(disable : 4996))
|
||||
// We can't use OPENSSL_strdup because we don't want to call OPENSSL_malloc,
|
||||
// which can affect the error stack.
|
||||
dst->data = strdup(src->data);
|
||||
OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
}
|
||||
dst->packed = src->packed;
|
||||
dst->line = src->line;
|
||||
}
|
||||
|
||||
|
||||
// global_next_library contains the next custom library value to return.
|
||||
static int global_next_library = ERR_NUM_LIBS;
|
||||
|
||||
@@ -194,15 +205,15 @@ static void err_state_free(void *statep) {
|
||||
for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) {
|
||||
err_clear(&state->errors[i]);
|
||||
}
|
||||
OPENSSL_free(state->to_free);
|
||||
OPENSSL_free(state);
|
||||
free(state->to_free);
|
||||
free(state);
|
||||
}
|
||||
|
||||
// err_get_state gets the ERR_STATE object for the current thread.
|
||||
static ERR_STATE *err_get_state(void) {
|
||||
ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR);
|
||||
if (state == NULL) {
|
||||
state = OPENSSL_malloc(sizeof(ERR_STATE));
|
||||
state = malloc(sizeof(ERR_STATE));
|
||||
if (state == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -258,7 +269,10 @@ static uint32_t get_error_values(int inc, int top, const char **file, int *line,
|
||||
} else {
|
||||
*data = error->data;
|
||||
if (flags != NULL) {
|
||||
*flags = ERR_FLAG_STRING;
|
||||
// Without |ERR_FLAG_MALLOCED|, rust-openssl assumes the string has a
|
||||
// static lifetime. In both cases, we retain ownership of the string,
|
||||
// and the caller is not expected to free it.
|
||||
*flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED;
|
||||
}
|
||||
// If this error is being removed, take ownership of data from
|
||||
// the error. The semantics are such that the caller doesn't
|
||||
@@ -267,7 +281,7 @@ static uint32_t get_error_values(int inc, int top, const char **file, int *line,
|
||||
// error queue.
|
||||
if (inc) {
|
||||
if (error->data != NULL) {
|
||||
OPENSSL_free(state->to_free);
|
||||
free(state->to_free);
|
||||
state->to_free = error->data;
|
||||
}
|
||||
error->data = NULL;
|
||||
@@ -335,7 +349,7 @@ void ERR_clear_error(void) {
|
||||
for (i = 0; i < ERR_NUM_ERRORS; i++) {
|
||||
err_clear(&state->errors[i]);
|
||||
}
|
||||
OPENSSL_free(state->to_free);
|
||||
free(state->to_free);
|
||||
state->to_free = NULL;
|
||||
|
||||
state->top = state->bottom = 0;
|
||||
@@ -629,13 +643,13 @@ static void err_set_error_data(char *data) {
|
||||
struct err_error_st *error;
|
||||
|
||||
if (state == NULL || state->top == state->bottom) {
|
||||
OPENSSL_free(data);
|
||||
free(data);
|
||||
return;
|
||||
}
|
||||
|
||||
error = &state->errors[state->top];
|
||||
|
||||
OPENSSL_free(error->data);
|
||||
free(error->data);
|
||||
error->data = data;
|
||||
}
|
||||
|
||||
@@ -672,48 +686,42 @@ void ERR_put_error(int library, int unused, int reason, const char *file,
|
||||
// concatenates them and sets the result as the data on the most recent
|
||||
// error.
|
||||
static void err_add_error_vdata(unsigned num, va_list args) {
|
||||
size_t alloced, new_len, len = 0, substr_len;
|
||||
char *buf;
|
||||
size_t total_size = 0;
|
||||
const char *substr;
|
||||
unsigned i;
|
||||
char *buf;
|
||||
|
||||
alloced = 80;
|
||||
buf = OPENSSL_malloc(alloced + 1);
|
||||
if (buf == NULL) {
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
for (size_t i = 0; i < num; i++) {
|
||||
substr = va_arg(args_copy, const char *);
|
||||
if (substr == NULL) {
|
||||
continue;
|
||||
}
|
||||
size_t substr_len = strlen(substr);
|
||||
if (SIZE_MAX - total_size < substr_len) {
|
||||
return; // Would overflow.
|
||||
}
|
||||
total_size += substr_len;
|
||||
}
|
||||
va_end(args_copy);
|
||||
if (total_size == SIZE_MAX) {
|
||||
return; // Would overflow.
|
||||
}
|
||||
total_size += 1; // NUL terminator.
|
||||
if ((buf = malloc(total_size)) == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < num; i++) {
|
||||
buf[0] = '\0';
|
||||
for (size_t i = 0; i < num; i++) {
|
||||
substr = va_arg(args, const char *);
|
||||
if (substr == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
substr_len = strlen(substr);
|
||||
new_len = len + substr_len;
|
||||
if (new_len > alloced) {
|
||||
char *new_buf;
|
||||
|
||||
if (alloced + 20 + 1 < alloced) {
|
||||
// overflow.
|
||||
OPENSSL_free(buf);
|
||||
return;
|
||||
}
|
||||
|
||||
alloced = new_len + 20;
|
||||
new_buf = OPENSSL_realloc(buf, alloced + 1);
|
||||
if (new_buf == NULL) {
|
||||
OPENSSL_free(buf);
|
||||
return;
|
||||
}
|
||||
buf = new_buf;
|
||||
if (OPENSSL_strlcat(buf, substr, total_size) >= total_size) {
|
||||
assert(0); // should not be possible.
|
||||
}
|
||||
|
||||
OPENSSL_memcpy(buf + len, substr, substr_len);
|
||||
len = new_len;
|
||||
}
|
||||
|
||||
buf[len] = 0;
|
||||
va_end(args);
|
||||
err_set_error_data(buf);
|
||||
}
|
||||
|
||||
@@ -725,21 +733,13 @@ void ERR_add_error_data(unsigned count, ...) {
|
||||
}
|
||||
|
||||
void ERR_add_error_dataf(const char *format, ...) {
|
||||
char *buf = NULL;
|
||||
va_list ap;
|
||||
char *buf;
|
||||
static const unsigned buf_len = 256;
|
||||
|
||||
// A fixed-size buffer is used because va_copy (which would be needed in
|
||||
// order to call vsnprintf twice and measure the buffer) wasn't defined until
|
||||
// C99.
|
||||
buf = OPENSSL_malloc(buf_len + 1);
|
||||
if (buf == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_start(ap, format);
|
||||
BIO_vsnprintf(buf, buf_len, format, ap);
|
||||
buf[buf_len] = 0;
|
||||
if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) {
|
||||
return;
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
err_set_error_data(buf);
|
||||
@@ -751,13 +751,20 @@ void ERR_set_error_data(char *data, int flags) {
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
// Disable deprecated functions on msvc so it doesn't complain about strdup.
|
||||
OPENSSL_MSVC_PRAGMA(warning(push))
|
||||
OPENSSL_MSVC_PRAGMA(warning(disable : 4996))
|
||||
// We can not use OPENSSL_strdup because we don't want to call OPENSSL_malloc,
|
||||
// which can affect the error stack.
|
||||
char *copy = strdup(data);
|
||||
OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
if (copy != NULL) {
|
||||
err_set_error_data(copy);
|
||||
}
|
||||
if (flags & ERR_FLAG_MALLOCED) {
|
||||
err_set_error_data(data);
|
||||
} else {
|
||||
char *copy = OPENSSL_strdup(data);
|
||||
if (copy != NULL) {
|
||||
err_set_error_data(copy);
|
||||
}
|
||||
// We can not take ownership of |data| directly because it is allocated with
|
||||
// |OPENSSL_malloc| and we will free it with system |free| later.
|
||||
OPENSSL_free(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -819,8 +826,8 @@ void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) {
|
||||
for (size_t i = 0; i < state->num_errors; i++) {
|
||||
err_clear(&state->errors[i]);
|
||||
}
|
||||
OPENSSL_free(state->errors);
|
||||
OPENSSL_free(state);
|
||||
free(state->errors);
|
||||
free(state);
|
||||
}
|
||||
|
||||
ERR_SAVE_STATE *ERR_save_state(void) {
|
||||
@@ -829,7 +836,7 @@ ERR_SAVE_STATE *ERR_save_state(void) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ERR_SAVE_STATE *ret = OPENSSL_malloc(sizeof(ERR_SAVE_STATE));
|
||||
ERR_SAVE_STATE *ret = malloc(sizeof(ERR_SAVE_STATE));
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -839,9 +846,9 @@ ERR_SAVE_STATE *ERR_save_state(void) {
|
||||
? state->top - state->bottom
|
||||
: ERR_NUM_ERRORS + state->top - state->bottom;
|
||||
assert(num_errors < ERR_NUM_ERRORS);
|
||||
ret->errors = OPENSSL_malloc(num_errors * sizeof(struct err_error_st));
|
||||
ret->errors = malloc(num_errors * sizeof(struct err_error_st));
|
||||
if (ret->errors == NULL) {
|
||||
OPENSSL_free(ret);
|
||||
free(ret);
|
||||
return NULL;
|
||||
}
|
||||
OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st));
|
||||
|
||||
@@ -193,6 +193,7 @@ const uint32_t kOpenSSLReasonValues[] = {
|
||||
0x283480b9,
|
||||
0x283500f7,
|
||||
0x28358cb7,
|
||||
0x2836099a,
|
||||
0x2c3232bf,
|
||||
0x2c329351,
|
||||
0x2c3332cd,
|
||||
@@ -205,38 +206,39 @@ const uint32_t kOpenSSLReasonValues[] = {
|
||||
0x2c36833a,
|
||||
0x2c37336f,
|
||||
0x2c37b39b,
|
||||
0x2c3833c0,
|
||||
0x2c38b3d7,
|
||||
0x2c3933f5,
|
||||
0x2c39b405,
|
||||
0x2c3a3417,
|
||||
0x2c3ab42b,
|
||||
0x2c3b343c,
|
||||
0x2c3bb45b,
|
||||
0x2c3833d9,
|
||||
0x2c38b3f0,
|
||||
0x2c39340e,
|
||||
0x2c39b41e,
|
||||
0x2c3a3430,
|
||||
0x2c3ab444,
|
||||
0x2c3b3455,
|
||||
0x2c3bb474,
|
||||
0x2c3c1363,
|
||||
0x2c3c9379,
|
||||
0x2c3d34a0,
|
||||
0x2c3d34b9,
|
||||
0x2c3d9392,
|
||||
0x2c3e34ca,
|
||||
0x2c3eb4d8,
|
||||
0x2c3f34f0,
|
||||
0x2c3fb508,
|
||||
0x2c403532,
|
||||
0x2c3e34e3,
|
||||
0x2c3eb4f1,
|
||||
0x2c3f3509,
|
||||
0x2c3fb521,
|
||||
0x2c40354b,
|
||||
0x2c409264,
|
||||
0x2c413543,
|
||||
0x2c41b556,
|
||||
0x2c41355c,
|
||||
0x2c41b56f,
|
||||
0x2c42122a,
|
||||
0x2c42b567,
|
||||
0x2c42b580,
|
||||
0x2c43076d,
|
||||
0x2c43b44d,
|
||||
0x2c43b466,
|
||||
0x2c4433ae,
|
||||
0x2c44b515,
|
||||
0x2c44b52e,
|
||||
0x2c453345,
|
||||
0x2c45b381,
|
||||
0x2c4633e5,
|
||||
0x2c46b46f,
|
||||
0x2c473484,
|
||||
0x2c47b4bd,
|
||||
0x2c4633fe,
|
||||
0x2c46b488,
|
||||
0x2c47349d,
|
||||
0x2c47b4d6,
|
||||
0x2c4833c0,
|
||||
0x30320000,
|
||||
0x30328015,
|
||||
0x3033001f,
|
||||
@@ -685,71 +687,71 @@ const uint32_t kOpenSSLReasonValues[] = {
|
||||
0x4c41945b,
|
||||
0x4c4215c4,
|
||||
0x4c4293a3,
|
||||
0x50323579,
|
||||
0x5032b588,
|
||||
0x50333593,
|
||||
0x5033b5a3,
|
||||
0x503435bc,
|
||||
0x5034b5d6,
|
||||
0x503535e4,
|
||||
0x5035b5fa,
|
||||
0x5036360c,
|
||||
0x5036b622,
|
||||
0x5037363b,
|
||||
0x5037b64e,
|
||||
0x50383666,
|
||||
0x5038b677,
|
||||
0x5039368c,
|
||||
0x5039b6a0,
|
||||
0x503a36c0,
|
||||
0x503ab6d6,
|
||||
0x503b36ee,
|
||||
0x503bb700,
|
||||
0x503c371c,
|
||||
0x503cb733,
|
||||
0x503d374c,
|
||||
0x503db762,
|
||||
0x503e376f,
|
||||
0x503eb785,
|
||||
0x503f3797,
|
||||
0x50323592,
|
||||
0x5032b5a1,
|
||||
0x503335ac,
|
||||
0x5033b5bc,
|
||||
0x503435d5,
|
||||
0x5034b5ef,
|
||||
0x503535fd,
|
||||
0x5035b613,
|
||||
0x50363625,
|
||||
0x5036b63b,
|
||||
0x50373654,
|
||||
0x5037b667,
|
||||
0x5038367f,
|
||||
0x5038b690,
|
||||
0x503936a5,
|
||||
0x5039b6b9,
|
||||
0x503a36d9,
|
||||
0x503ab6ef,
|
||||
0x503b3707,
|
||||
0x503bb719,
|
||||
0x503c3735,
|
||||
0x503cb74c,
|
||||
0x503d3765,
|
||||
0x503db77b,
|
||||
0x503e3788,
|
||||
0x503eb79e,
|
||||
0x503f37b0,
|
||||
0x503f83b3,
|
||||
0x504037aa,
|
||||
0x5040b7ba,
|
||||
0x504137d4,
|
||||
0x5041b7e3,
|
||||
0x504237fd,
|
||||
0x5042b81a,
|
||||
0x5043382a,
|
||||
0x5043b83a,
|
||||
0x50443857,
|
||||
0x504037c3,
|
||||
0x5040b7d3,
|
||||
0x504137ed,
|
||||
0x5041b7fc,
|
||||
0x50423816,
|
||||
0x5042b833,
|
||||
0x50433843,
|
||||
0x5043b853,
|
||||
0x50443870,
|
||||
0x50448469,
|
||||
0x5045386b,
|
||||
0x5045b889,
|
||||
0x5046389c,
|
||||
0x5046b8b2,
|
||||
0x504738c4,
|
||||
0x5047b8d9,
|
||||
0x504838ff,
|
||||
0x5048b90d,
|
||||
0x50493920,
|
||||
0x5049b935,
|
||||
0x504a394b,
|
||||
0x504ab95b,
|
||||
0x504b397b,
|
||||
0x504bb98e,
|
||||
0x504c39b1,
|
||||
0x504cb9df,
|
||||
0x504d3a0c,
|
||||
0x504dba29,
|
||||
0x504e3a44,
|
||||
0x504eba60,
|
||||
0x504f3a72,
|
||||
0x504fba89,
|
||||
0x50503a98,
|
||||
0x50453884,
|
||||
0x5045b8a2,
|
||||
0x504638b5,
|
||||
0x5046b8cb,
|
||||
0x504738dd,
|
||||
0x5047b8f2,
|
||||
0x50483918,
|
||||
0x5048b926,
|
||||
0x50493939,
|
||||
0x5049b94e,
|
||||
0x504a3964,
|
||||
0x504ab974,
|
||||
0x504b3994,
|
||||
0x504bb9a7,
|
||||
0x504c39ca,
|
||||
0x504cb9f8,
|
||||
0x504d3a25,
|
||||
0x504dba42,
|
||||
0x504e3a5d,
|
||||
0x504eba79,
|
||||
0x504f3a8b,
|
||||
0x504fbaa2,
|
||||
0x50503ab1,
|
||||
0x50508729,
|
||||
0x50513aab,
|
||||
0x5051b849,
|
||||
0x505239f1,
|
||||
0x50513ac4,
|
||||
0x5051b862,
|
||||
0x50523a0a,
|
||||
0x58320fb0,
|
||||
0x68320f72,
|
||||
0x68328cca,
|
||||
@@ -757,6 +759,7 @@ const uint32_t kOpenSSLReasonValues[] = {
|
||||
0x68338f80,
|
||||
0x68340f90,
|
||||
0x683480f7,
|
||||
0x6835099a,
|
||||
0x6c320f38,
|
||||
0x6c328c81,
|
||||
0x6c330f43,
|
||||
@@ -1416,6 +1419,7 @@ const char kOpenSSLReasonStringData[] =
|
||||
"INVALID_FIELD_FOR_VERSION\0"
|
||||
"INVALID_FIELD_NAME\0"
|
||||
"INVALID_PARAMETER\0"
|
||||
"INVALID_POLICY_EXTENSION\0"
|
||||
"INVALID_PSS_PARAMETERS\0"
|
||||
"INVALID_TRUST\0"
|
||||
"INVALID_VERSION\0"
|
||||
|
||||
@@ -85,7 +85,6 @@ EVP_PKEY *EVP_PKEY_new(void) {
|
||||
|
||||
ret = OPENSSL_malloc(sizeof(EVP_PKEY));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -99,7 +98,7 @@ EVP_PKEY *EVP_PKEY_new(void) {
|
||||
static void free_it(EVP_PKEY *pkey) {
|
||||
if (pkey->ameth && pkey->ameth->pkey_free) {
|
||||
pkey->ameth->pkey_free(pkey);
|
||||
pkey->pkey.ptr = NULL;
|
||||
pkey->pkey = NULL;
|
||||
pkey->type = EVP_PKEY_NONE;
|
||||
}
|
||||
}
|
||||
@@ -255,7 +254,7 @@ RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
|
||||
return NULL;
|
||||
}
|
||||
return pkey->pkey.rsa;
|
||||
return pkey->pkey;
|
||||
}
|
||||
|
||||
RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) {
|
||||
@@ -283,7 +282,7 @@ DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
|
||||
return NULL;
|
||||
}
|
||||
return pkey->pkey.dsa;
|
||||
return pkey->pkey;
|
||||
}
|
||||
|
||||
DSA *EVP_PKEY_get1_DSA(const EVP_PKEY *pkey) {
|
||||
@@ -311,7 +310,7 @@ EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY);
|
||||
return NULL;
|
||||
}
|
||||
return pkey->pkey.ec;
|
||||
return pkey->pkey;
|
||||
}
|
||||
|
||||
EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey) {
|
||||
@@ -329,14 +328,14 @@ int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
|
||||
if (!EVP_PKEY_set_type(pkey, type)) {
|
||||
return 0;
|
||||
}
|
||||
pkey->pkey.ptr = key;
|
||||
pkey->pkey = key;
|
||||
return key != NULL;
|
||||
}
|
||||
|
||||
int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
|
||||
const EVP_PKEY_ASN1_METHOD *ameth;
|
||||
|
||||
if (pkey && pkey->pkey.ptr) {
|
||||
if (pkey && pkey->pkey) {
|
||||
free_it(pkey);
|
||||
}
|
||||
|
||||
@@ -448,7 +447,7 @@ int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
|
||||
void *EVP_PKEY_get0(const EVP_PKEY *pkey) {
|
||||
// Node references, but never calls this function, so for now we return NULL.
|
||||
// If other projects require complete support, call |EVP_PKEY_get0_RSA|, etc.,
|
||||
// rather than reading |pkey->pkey.ptr| directly. This avoids problems if our
|
||||
// rather than reading |pkey->pkey| directly. This avoids problems if our
|
||||
// internal representation does not match the type the caller expects from
|
||||
// OpenSSL.
|
||||
return NULL;
|
||||
|
||||
@@ -336,11 +336,11 @@ EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, long len) {
|
||||
int i2d_PublicKey(const EVP_PKEY *key, uint8_t **outp) {
|
||||
switch (key->type) {
|
||||
case EVP_PKEY_RSA:
|
||||
return i2d_RSAPublicKey(key->pkey.rsa, outp);
|
||||
return i2d_RSAPublicKey(EVP_PKEY_get0_RSA(key), outp);
|
||||
case EVP_PKEY_DSA:
|
||||
return i2d_DSAPublicKey(key->pkey.dsa, outp);
|
||||
return i2d_DSAPublicKey(EVP_PKEY_get0_DSA(key), outp);
|
||||
case EVP_PKEY_EC:
|
||||
return i2o_ECPublicKey(key->pkey.ec, outp);
|
||||
return i2o_ECPublicKey(EVP_PKEY_get0_EC_KEY(key), outp);
|
||||
default:
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
|
||||
return -1;
|
||||
|
||||
@@ -88,7 +88,6 @@ static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e,
|
||||
const EVP_PKEY_METHOD *pmeth) {
|
||||
EVP_PKEY_CTX *ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
OPENSSL_memset(ret, 0, sizeof(EVP_PKEY_CTX));
|
||||
|
||||
@@ -66,6 +66,9 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD;
|
||||
typedef struct evp_pkey_method_st EVP_PKEY_METHOD;
|
||||
|
||||
struct evp_pkey_asn1_method_st {
|
||||
int pkey_id;
|
||||
uint8_t oid[9];
|
||||
@@ -128,6 +131,20 @@ struct evp_pkey_asn1_method_st {
|
||||
void (*pkey_free)(EVP_PKEY *pkey);
|
||||
} /* EVP_PKEY_ASN1_METHOD */;
|
||||
|
||||
struct evp_pkey_st {
|
||||
CRYPTO_refcount_t references;
|
||||
|
||||
// type contains one of the EVP_PKEY_* values or NID_undef and determines
|
||||
// the type of |pkey|.
|
||||
int type;
|
||||
|
||||
// pkey contains a pointer to a structure dependent on |type|.
|
||||
void *pkey;
|
||||
|
||||
// ameth contains a pointer to a method table that contains many ASN.1
|
||||
// methods for the key type.
|
||||
const EVP_PKEY_ASN1_METHOD *ameth;
|
||||
} /* EVP_PKEY */;
|
||||
|
||||
#define EVP_PKEY_OP_UNDEFINED 0
|
||||
#define EVP_PKEY_OP_KEYGEN (1 << 2)
|
||||
|
||||
@@ -61,6 +61,7 @@
|
||||
#include <CCryptoBoringSSL_dsa.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
|
||||
#include "../dsa/internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
@@ -102,7 +103,7 @@ err:
|
||||
}
|
||||
|
||||
static int dsa_pub_encode(CBB *out, const EVP_PKEY *key) {
|
||||
const DSA *dsa = key->pkey.dsa;
|
||||
const DSA *dsa = key->pkey;
|
||||
const int has_params = dsa->p != NULL && dsa->q != NULL && dsa->g != NULL;
|
||||
|
||||
// See RFC 5480, section 2.
|
||||
@@ -136,25 +137,27 @@ static int dsa_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
|
||||
}
|
||||
|
||||
dsa->priv_key = BN_new();
|
||||
dsa->pub_key = BN_new();
|
||||
if (dsa->priv_key == NULL || dsa->pub_key == NULL) {
|
||||
if (dsa->priv_key == NULL) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_parse_asn1_unsigned(key, dsa->priv_key) ||
|
||||
CBS_len(key) != 0) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Decode the key. To avoid DoS attacks when importing private keys, we bound
|
||||
// |dsa->priv_key| against |dsa->q|, which itself bound by
|
||||
// |DSA_parse_parameters|. (We cannot call |BN_num_bits| on |dsa->priv_key|.
|
||||
// That would leak a secret bit width.)
|
||||
if (!BN_parse_asn1_unsigned(key, dsa->priv_key) ||
|
||||
CBS_len(key) != 0 ||
|
||||
BN_cmp(dsa->priv_key, dsa->q) >= 0) {
|
||||
// To avoid DoS attacks when importing private keys, check bounds on |dsa|.
|
||||
// This bounds |dsa->priv_key| against |dsa->q| and bounds |dsa->q|'s bit
|
||||
// width.
|
||||
if (!dsa_check_key(dsa)) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Calculate the public key.
|
||||
ctx = BN_CTX_new();
|
||||
if (ctx == NULL ||
|
||||
dsa->pub_key = BN_new();
|
||||
if (ctx == NULL || dsa->pub_key == NULL ||
|
||||
!BN_mod_exp_mont_consttime(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p,
|
||||
ctx, NULL)) {
|
||||
goto err;
|
||||
@@ -171,7 +174,7 @@ err:
|
||||
}
|
||||
|
||||
static int dsa_priv_encode(CBB *out, const EVP_PKEY *key) {
|
||||
const DSA *dsa = key->pkey.dsa;
|
||||
const DSA *dsa = key->pkey;
|
||||
if (dsa == NULL || dsa->priv_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
|
||||
return 0;
|
||||
@@ -196,17 +199,19 @@ static int dsa_priv_encode(CBB *out, const EVP_PKEY *key) {
|
||||
}
|
||||
|
||||
static int int_dsa_size(const EVP_PKEY *pkey) {
|
||||
return DSA_size(pkey->pkey.dsa);
|
||||
const DSA *dsa = pkey->pkey;
|
||||
return DSA_size(dsa);
|
||||
}
|
||||
|
||||
static int dsa_bits(const EVP_PKEY *pkey) {
|
||||
return BN_num_bits(pkey->pkey.dsa->p);
|
||||
const DSA *dsa = pkey->pkey;
|
||||
return BN_num_bits(DSA_get0_p(dsa));
|
||||
}
|
||||
|
||||
static int dsa_missing_parameters(const EVP_PKEY *pkey) {
|
||||
DSA *dsa;
|
||||
dsa = pkey->pkey.dsa;
|
||||
if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
|
||||
const DSA *dsa = pkey->pkey;
|
||||
if (DSA_get0_p(dsa) == NULL || DSA_get0_q(dsa) == NULL ||
|
||||
DSA_get0_g(dsa) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@@ -226,9 +231,11 @@ static int dup_bn_into(BIGNUM **out, BIGNUM *src) {
|
||||
}
|
||||
|
||||
static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
|
||||
if (!dup_bn_into(&to->pkey.dsa->p, from->pkey.dsa->p) ||
|
||||
!dup_bn_into(&to->pkey.dsa->q, from->pkey.dsa->q) ||
|
||||
!dup_bn_into(&to->pkey.dsa->g, from->pkey.dsa->g)) {
|
||||
DSA *to_dsa = to->pkey;
|
||||
const DSA *from_dsa = from->pkey;
|
||||
if (!dup_bn_into(&to_dsa->p, from_dsa->p) ||
|
||||
!dup_bn_into(&to_dsa->q, from_dsa->q) ||
|
||||
!dup_bn_into(&to_dsa->g, from_dsa->g)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -236,16 +243,23 @@ static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
|
||||
}
|
||||
|
||||
static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
return BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) == 0 &&
|
||||
BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) == 0 &&
|
||||
BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g) == 0;
|
||||
const DSA *a_dsa = a->pkey;
|
||||
const DSA *b_dsa = b->pkey;
|
||||
return BN_cmp(DSA_get0_p(a_dsa), DSA_get0_p(b_dsa)) == 0 &&
|
||||
BN_cmp(DSA_get0_q(a_dsa), DSA_get0_q(b_dsa)) == 0 &&
|
||||
BN_cmp(DSA_get0_g(a_dsa), DSA_get0_g(b_dsa)) == 0;
|
||||
}
|
||||
|
||||
static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
|
||||
const DSA *a_dsa = a->pkey;
|
||||
const DSA *b_dsa = b->pkey;
|
||||
return BN_cmp(DSA_get0_pub_key(b_dsa), DSA_get0_pub_key(a_dsa)) == 0;
|
||||
}
|
||||
|
||||
static void int_dsa_free(EVP_PKEY *pkey) { DSA_free(pkey->pkey.dsa); }
|
||||
static void int_dsa_free(EVP_PKEY *pkey) {
|
||||
DSA_free(pkey->pkey);
|
||||
pkey->pkey = NULL;
|
||||
}
|
||||
|
||||
const EVP_PKEY_ASN1_METHOD dsa_asn1_meth = {
|
||||
EVP_PKEY_DSA,
|
||||
|
||||
@@ -117,9 +117,7 @@ static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx) {
|
||||
|
||||
static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
|
||||
const uint8_t *tbs, size_t tbslen) {
|
||||
unsigned int sltmp;
|
||||
EC_KEY *ec = ctx->pkey->pkey.ec;
|
||||
|
||||
const EC_KEY *ec = ctx->pkey->pkey;
|
||||
if (!sig) {
|
||||
*siglen = ECDSA_size(ec);
|
||||
return 1;
|
||||
@@ -128,6 +126,7 @@ static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int sltmp;
|
||||
if (!ECDSA_sign(0, tbs, tbslen, sig, &sltmp, ec)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -137,37 +136,32 @@ static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
|
||||
|
||||
static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen,
|
||||
const uint8_t *tbs, size_t tbslen) {
|
||||
return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->pkey->pkey.ec);
|
||||
const EC_KEY *ec_key = ctx->pkey->pkey;
|
||||
return ECDSA_verify(0, tbs, tbslen, sig, siglen, ec_key);
|
||||
}
|
||||
|
||||
static int pkey_ec_derive(EVP_PKEY_CTX *ctx, uint8_t *key,
|
||||
size_t *keylen) {
|
||||
int ret;
|
||||
size_t outlen;
|
||||
const EC_POINT *pubkey = NULL;
|
||||
EC_KEY *eckey;
|
||||
|
||||
if (!ctx->pkey || !ctx->peerkey) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
eckey = ctx->pkey->pkey.ec;
|
||||
|
||||
const EC_KEY *eckey = ctx->pkey->pkey;
|
||||
if (!key) {
|
||||
const EC_GROUP *group;
|
||||
group = EC_KEY_get0_group(eckey);
|
||||
*keylen = (EC_GROUP_get_degree(group) + 7) / 8;
|
||||
return 1;
|
||||
}
|
||||
pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
|
||||
|
||||
const EC_KEY *eckey_peer = ctx->peerkey->pkey;
|
||||
const EC_POINT *pubkey = EC_KEY_get0_public_key(eckey_peer);
|
||||
|
||||
// NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is
|
||||
// not an error, the result is truncated.
|
||||
|
||||
outlen = *keylen;
|
||||
|
||||
ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
|
||||
size_t outlen = *keylen;
|
||||
int ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
|
||||
if (ret < 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -224,7 +218,7 @@ static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NO_PARAMETERS_SET);
|
||||
return 0;
|
||||
}
|
||||
group = EC_KEY_get0_group(ctx->pkey->pkey.ec);
|
||||
group = EC_KEY_get0_group(ctx->pkey->pkey);
|
||||
}
|
||||
EC_KEY *ec = EC_KEY_new();
|
||||
if (ec == NULL ||
|
||||
|
||||
@@ -66,7 +66,7 @@
|
||||
|
||||
|
||||
static int eckey_pub_encode(CBB *out, const EVP_PKEY *key) {
|
||||
const EC_KEY *ec_key = key->pkey.ec;
|
||||
const EC_KEY *ec_key = key->pkey;
|
||||
const EC_GROUP *group = EC_KEY_get0_group(ec_key);
|
||||
const EC_POINT *public_key = EC_KEY_get0_public_key(ec_key);
|
||||
|
||||
@@ -118,11 +118,12 @@ err:
|
||||
}
|
||||
|
||||
static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
int r;
|
||||
const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
|
||||
const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
|
||||
*pb = EC_KEY_get0_public_key(b->pkey.ec);
|
||||
r = EC_POINT_cmp(group, pa, pb, NULL);
|
||||
const EC_KEY *a_ec = a->pkey;
|
||||
const EC_KEY *b_ec = b->pkey;
|
||||
const EC_GROUP *group = EC_KEY_get0_group(b_ec);
|
||||
const EC_POINT *pa = EC_KEY_get0_public_key(a_ec),
|
||||
*pb = EC_KEY_get0_public_key(b_ec);
|
||||
int r = EC_POINT_cmp(group, pa, pb, NULL);
|
||||
if (r == 0) {
|
||||
return 1;
|
||||
} else if (r == 1) {
|
||||
@@ -154,7 +155,7 @@ static int eckey_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
|
||||
}
|
||||
|
||||
static int eckey_priv_encode(CBB *out, const EVP_PKEY *key) {
|
||||
const EC_KEY *ec_key = key->pkey.ec;
|
||||
const EC_KEY *ec_key = key->pkey;
|
||||
|
||||
// Omit the redundant copy of the curve name. This contradicts RFC 5915 but
|
||||
// aligns with PKCS #11. SEC 1 only says they may be omitted if known by other
|
||||
@@ -182,7 +183,7 @@ static int eckey_priv_encode(CBB *out, const EVP_PKEY *key) {
|
||||
|
||||
static int eckey_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
|
||||
size_t len) {
|
||||
EC_KEY *ec_key = pkey->pkey.ec;
|
||||
EC_KEY *ec_key = pkey->pkey;
|
||||
if (ec_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
|
||||
return 0;
|
||||
@@ -193,7 +194,7 @@ static int eckey_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
|
||||
|
||||
static size_t eckey_get1_tls_encodedpoint(const EVP_PKEY *pkey,
|
||||
uint8_t **out_ptr) {
|
||||
const EC_KEY *ec_key = pkey->pkey.ec;
|
||||
const EC_KEY *ec_key = pkey->pkey;
|
||||
if (ec_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
|
||||
return 0;
|
||||
@@ -203,11 +204,13 @@ static size_t eckey_get1_tls_encodedpoint(const EVP_PKEY *pkey,
|
||||
}
|
||||
|
||||
static int int_ec_size(const EVP_PKEY *pkey) {
|
||||
return ECDSA_size(pkey->pkey.ec);
|
||||
const EC_KEY *ec_key = pkey->pkey;
|
||||
return ECDSA_size(ec_key);
|
||||
}
|
||||
|
||||
static int ec_bits(const EVP_PKEY *pkey) {
|
||||
const EC_GROUP *group = EC_KEY_get0_group(pkey->pkey.ec);
|
||||
const EC_KEY *ec_key = pkey->pkey;
|
||||
const EC_GROUP *group = EC_KEY_get0_group(ec_key);
|
||||
if (group == NULL) {
|
||||
ERR_clear_error();
|
||||
return 0;
|
||||
@@ -216,34 +219,38 @@ static int ec_bits(const EVP_PKEY *pkey) {
|
||||
}
|
||||
|
||||
static int ec_missing_parameters(const EVP_PKEY *pkey) {
|
||||
return pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL;
|
||||
const EC_KEY *ec_key = pkey->pkey;
|
||||
return ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL;
|
||||
}
|
||||
|
||||
static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
|
||||
if (from->pkey.ec == NULL) {
|
||||
const EC_KEY *from_key = from->pkey;
|
||||
if (from_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
|
||||
return 0;
|
||||
}
|
||||
const EC_GROUP *group = EC_KEY_get0_group(from->pkey.ec);
|
||||
const EC_GROUP *group = EC_KEY_get0_group(from_key);
|
||||
if (group == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
if (to->pkey.ec == NULL) {
|
||||
to->pkey.ec = EC_KEY_new();
|
||||
if (to->pkey.ec == NULL) {
|
||||
if (to->pkey == NULL) {
|
||||
to->pkey = EC_KEY_new();
|
||||
if (to->pkey == NULL) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return EC_KEY_set_group(to->pkey.ec, group);
|
||||
return EC_KEY_set_group(to->pkey, group);
|
||||
}
|
||||
|
||||
static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
if (a->pkey.ec == NULL || b->pkey.ec == NULL) {
|
||||
const EC_KEY *a_ec = a->pkey;
|
||||
const EC_KEY *b_ec = b->pkey;
|
||||
if (a_ec == NULL || b_ec == NULL) {
|
||||
return -2;
|
||||
}
|
||||
const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
|
||||
*group_b = EC_KEY_get0_group(b->pkey.ec);
|
||||
const EC_GROUP *group_a = EC_KEY_get0_group(a_ec),
|
||||
*group_b = EC_KEY_get0_group(b_ec);
|
||||
if (group_a == NULL || group_b == NULL) {
|
||||
return -2;
|
||||
}
|
||||
@@ -254,10 +261,14 @@ static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void int_ec_free(EVP_PKEY *pkey) { EC_KEY_free(pkey->pkey.ec); }
|
||||
static void int_ec_free(EVP_PKEY *pkey) {
|
||||
EC_KEY_free(pkey->pkey);
|
||||
pkey->pkey = NULL;
|
||||
}
|
||||
|
||||
static int eckey_opaque(const EVP_PKEY *pkey) {
|
||||
return EC_KEY_is_opaque(pkey->pkey.ec);
|
||||
const EC_KEY *ec_key = pkey->pkey;
|
||||
return EC_KEY_is_opaque(ec_key);
|
||||
}
|
||||
|
||||
const EVP_PKEY_ASN1_METHOD ec_asn1_meth = {
|
||||
|
||||
@@ -27,7 +27,6 @@ static int pkey_ed25519_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { return 1; }
|
||||
static int pkey_ed25519_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
|
||||
ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
|
||||
if (key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -40,15 +39,15 @@ static int pkey_ed25519_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
|
||||
ED25519_keypair(pubkey_unused, key->key);
|
||||
key->has_private = 1;
|
||||
|
||||
OPENSSL_free(pkey->pkey.ptr);
|
||||
pkey->pkey.ptr = key;
|
||||
OPENSSL_free(pkey->pkey);
|
||||
pkey->pkey = key;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int pkey_ed25519_sign_message(EVP_PKEY_CTX *ctx, uint8_t *sig,
|
||||
size_t *siglen, const uint8_t *tbs,
|
||||
size_t tbslen) {
|
||||
ED25519_KEY *key = ctx->pkey->pkey.ptr;
|
||||
const ED25519_KEY *key = ctx->pkey->pkey;
|
||||
if (!key->has_private) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
|
||||
return 0;
|
||||
@@ -75,7 +74,7 @@ static int pkey_ed25519_sign_message(EVP_PKEY_CTX *ctx, uint8_t *sig,
|
||||
static int pkey_ed25519_verify_message(EVP_PKEY_CTX *ctx, const uint8_t *sig,
|
||||
size_t siglen, const uint8_t *tbs,
|
||||
size_t tbslen) {
|
||||
ED25519_KEY *key = ctx->pkey->pkey.ptr;
|
||||
const ED25519_KEY *key = ctx->pkey->pkey;
|
||||
if (siglen != 64 ||
|
||||
!ED25519_verify(tbs, tbslen, sig, key->key + ED25519_PUBLIC_KEY_OFFSET)) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SIGNATURE);
|
||||
|
||||
@@ -24,8 +24,8 @@
|
||||
|
||||
|
||||
static void ed25519_free(EVP_PKEY *pkey) {
|
||||
OPENSSL_free(pkey->pkey.ptr);
|
||||
pkey->pkey.ptr = NULL;
|
||||
OPENSSL_free(pkey->pkey);
|
||||
pkey->pkey = NULL;
|
||||
}
|
||||
|
||||
static int ed25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
@@ -36,7 +36,6 @@ static int ed25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
|
||||
ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
|
||||
if (key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -47,7 +46,7 @@ static int ed25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
key->has_private = 1;
|
||||
|
||||
ed25519_free(pkey);
|
||||
pkey->pkey.ptr = key;
|
||||
pkey->pkey = key;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -59,7 +58,6 @@ static int ed25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
|
||||
ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
|
||||
if (key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -67,13 +65,13 @@ static int ed25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
key->has_private = 0;
|
||||
|
||||
ed25519_free(pkey);
|
||||
pkey->pkey.ptr = key;
|
||||
pkey->pkey = key;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ed25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
|
||||
size_t *out_len) {
|
||||
const ED25519_KEY *key = pkey->pkey.ptr;
|
||||
const ED25519_KEY *key = pkey->pkey;
|
||||
if (!key->has_private) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
|
||||
return 0;
|
||||
@@ -97,7 +95,7 @@ static int ed25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
|
||||
|
||||
static int ed25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
|
||||
size_t *out_len) {
|
||||
const ED25519_KEY *key = pkey->pkey.ptr;
|
||||
const ED25519_KEY *key = pkey->pkey;
|
||||
if (out == NULL) {
|
||||
*out_len = 32;
|
||||
return 1;
|
||||
@@ -126,7 +124,7 @@ static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
|
||||
}
|
||||
|
||||
static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
|
||||
const ED25519_KEY *key = pkey->pkey.ptr;
|
||||
const ED25519_KEY *key = pkey->pkey;
|
||||
|
||||
// See RFC 8410, section 4.
|
||||
CBB spki, algorithm, oid, key_bitstring;
|
||||
@@ -147,8 +145,8 @@ static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
|
||||
}
|
||||
|
||||
static int ed25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
const ED25519_KEY *a_key = a->pkey.ptr;
|
||||
const ED25519_KEY *b_key = b->pkey.ptr;
|
||||
const ED25519_KEY *a_key = a->pkey;
|
||||
const ED25519_KEY *b_key = b->pkey;
|
||||
return OPENSSL_memcmp(a_key->key + ED25519_PUBLIC_KEY_OFFSET,
|
||||
b_key->key + ED25519_PUBLIC_KEY_OFFSET, 32) == 0;
|
||||
}
|
||||
@@ -170,7 +168,7 @@ static int ed25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
|
||||
}
|
||||
|
||||
static int ed25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
|
||||
ED25519_KEY *key = pkey->pkey.ptr;
|
||||
const ED25519_KEY *key = pkey->pkey;
|
||||
if (!key->has_private) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
|
||||
return 0;
|
||||
|
||||
@@ -37,13 +37,11 @@ typedef struct {
|
||||
static int pkey_hkdf_init(EVP_PKEY_CTX *ctx) {
|
||||
HKDF_PKEY_CTX *hctx = OPENSSL_malloc(sizeof(HKDF_PKEY_CTX));
|
||||
if (hctx == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
OPENSSL_memset(hctx, 0, sizeof(HKDF_PKEY_CTX));
|
||||
if (!CBB_init(&hctx->info, 0)) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_free(hctx);
|
||||
return 0;
|
||||
}
|
||||
@@ -64,8 +62,7 @@ static int pkey_hkdf_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
|
||||
|
||||
if (hctx_src->key_len != 0) {
|
||||
hctx_dst->key = OPENSSL_memdup(hctx_src->key, hctx_src->key_len);
|
||||
if (hctx_src->key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
if (hctx_dst->key == NULL) {
|
||||
return 0;
|
||||
}
|
||||
hctx_dst->key_len = hctx_src->key_len;
|
||||
@@ -73,8 +70,7 @@ static int pkey_hkdf_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
|
||||
|
||||
if (hctx_src->salt_len != 0) {
|
||||
hctx_dst->salt = OPENSSL_memdup(hctx_src->salt, hctx_src->salt_len);
|
||||
if (hctx_src->salt == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
if (hctx_dst->salt == NULL) {
|
||||
return 0;
|
||||
}
|
||||
hctx_dst->salt_len = hctx_src->salt_len;
|
||||
@@ -82,7 +78,6 @@ static int pkey_hkdf_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
|
||||
|
||||
if (!CBB_add_bytes(&hctx_dst->info, CBB_data(&hctx_src->info),
|
||||
CBB_len(&hctx_src->info))) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -159,7 +154,6 @@ static int pkey_hkdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
|
||||
case EVP_PKEY_CTRL_HKDF_KEY: {
|
||||
const CBS *key = p2;
|
||||
if (!CBS_stow(key, &hctx->key, &hctx->key_len)) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
@@ -167,7 +161,6 @@ static int pkey_hkdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
|
||||
case EVP_PKEY_CTRL_HKDF_SALT: {
|
||||
const CBS *salt = p2;
|
||||
if (!CBS_stow(salt, &hctx->salt, &hctx->salt_len)) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
@@ -177,7 +170,6 @@ static int pkey_hkdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
|
||||
// |EVP_PKEY_CTX_add1_hkdf_info| appends to the info string, rather than
|
||||
// replacing it.
|
||||
if (!CBB_add_bytes(&hctx->info, CBS_data(info), CBS_len(info))) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
#include <CCryptoBoringSSL_rsa.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "../fipsmodule/rsa/internal.h"
|
||||
#include "../rsa_extra/internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
@@ -171,7 +171,7 @@ static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) {
|
||||
static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
|
||||
const uint8_t *tbs, size_t tbslen) {
|
||||
RSA_PKEY_CTX *rctx = ctx->data;
|
||||
RSA *rsa = ctx->pkey->pkey.rsa;
|
||||
RSA *rsa = ctx->pkey->pkey;
|
||||
const size_t key_len = EVP_PKEY_size(ctx->pkey);
|
||||
|
||||
if (!sig) {
|
||||
@@ -210,7 +210,7 @@ static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
|
||||
size_t siglen, const uint8_t *tbs,
|
||||
size_t tbslen) {
|
||||
RSA_PKEY_CTX *rctx = ctx->data;
|
||||
RSA *rsa = ctx->pkey->pkey.rsa;
|
||||
RSA *rsa = ctx->pkey->pkey;
|
||||
|
||||
if (rctx->md) {
|
||||
switch (rctx->pad_mode) {
|
||||
@@ -243,7 +243,7 @@ static int pkey_rsa_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out,
|
||||
size_t *out_len, const uint8_t *sig,
|
||||
size_t sig_len) {
|
||||
RSA_PKEY_CTX *rctx = ctx->data;
|
||||
RSA *rsa = ctx->pkey->pkey.rsa;
|
||||
RSA *rsa = ctx->pkey->pkey;
|
||||
const size_t key_len = EVP_PKEY_size(ctx->pkey);
|
||||
|
||||
if (out == NULL) {
|
||||
@@ -307,7 +307,7 @@ static int pkey_rsa_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out,
|
||||
static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
|
||||
const uint8_t *in, size_t inlen) {
|
||||
RSA_PKEY_CTX *rctx = ctx->data;
|
||||
RSA *rsa = ctx->pkey->pkey.rsa;
|
||||
RSA *rsa = ctx->pkey->pkey;
|
||||
const size_t key_len = EVP_PKEY_size(ctx->pkey);
|
||||
|
||||
if (!out) {
|
||||
@@ -339,7 +339,7 @@ static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
|
||||
size_t *outlen, const uint8_t *in,
|
||||
size_t inlen) {
|
||||
RSA_PKEY_CTX *rctx = ctx->data;
|
||||
RSA *rsa = ctx->pkey->pkey.rsa;
|
||||
RSA *rsa = ctx->pkey->pkey;
|
||||
const size_t key_len = EVP_PKEY_size(ctx->pkey);
|
||||
|
||||
if (!out) {
|
||||
|
||||
@@ -68,6 +68,7 @@
|
||||
|
||||
static int rsa_pub_encode(CBB *out, const EVP_PKEY *key) {
|
||||
// See RFC 3279, section 2.3.1.
|
||||
const RSA *rsa = key->pkey;
|
||||
CBB spki, algorithm, oid, null, key_bitstring;
|
||||
if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
|
||||
!CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
|
||||
@@ -76,7 +77,7 @@ static int rsa_pub_encode(CBB *out, const EVP_PKEY *key) {
|
||||
!CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
|
||||
!CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
|
||||
!CBB_add_u8(&key_bitstring, 0 /* padding */) ||
|
||||
!RSA_marshal_public_key(&key_bitstring, key->pkey.rsa) ||
|
||||
!RSA_marshal_public_key(&key_bitstring, rsa) ||
|
||||
!CBB_flush(out)) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
@@ -109,11 +110,14 @@ static int rsa_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
|
||||
}
|
||||
|
||||
static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
return BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) == 0 &&
|
||||
BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) == 0;
|
||||
const RSA *a_rsa = a->pkey;
|
||||
const RSA *b_rsa = b->pkey;
|
||||
return BN_cmp(RSA_get0_n(b_rsa), RSA_get0_n(a_rsa)) == 0 &&
|
||||
BN_cmp(RSA_get0_e(b_rsa), RSA_get0_e(a_rsa)) == 0;
|
||||
}
|
||||
|
||||
static int rsa_priv_encode(CBB *out, const EVP_PKEY *key) {
|
||||
const RSA *rsa = key->pkey;
|
||||
CBB pkcs8, algorithm, oid, null, private_key;
|
||||
if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
|
||||
!CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
|
||||
@@ -122,7 +126,7 @@ static int rsa_priv_encode(CBB *out, const EVP_PKEY *key) {
|
||||
!CBB_add_bytes(&oid, rsa_asn1_meth.oid, rsa_asn1_meth.oid_len) ||
|
||||
!CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
|
||||
!CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
|
||||
!RSA_marshal_private_key(&private_key, key->pkey.rsa) ||
|
||||
!RSA_marshal_private_key(&private_key, rsa) ||
|
||||
!CBB_flush(out)) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
@@ -153,18 +157,24 @@ static int rsa_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
|
||||
}
|
||||
|
||||
static int rsa_opaque(const EVP_PKEY *pkey) {
|
||||
return RSA_is_opaque(pkey->pkey.rsa);
|
||||
const RSA *rsa = pkey->pkey;
|
||||
return RSA_is_opaque(rsa);
|
||||
}
|
||||
|
||||
static int int_rsa_size(const EVP_PKEY *pkey) {
|
||||
return RSA_size(pkey->pkey.rsa);
|
||||
const RSA *rsa = pkey->pkey;
|
||||
return RSA_size(rsa);
|
||||
}
|
||||
|
||||
static int rsa_bits(const EVP_PKEY *pkey) {
|
||||
return RSA_bits(pkey->pkey.rsa);
|
||||
const RSA *rsa = pkey->pkey;
|
||||
return RSA_bits(rsa);
|
||||
}
|
||||
|
||||
static void int_rsa_free(EVP_PKEY *pkey) { RSA_free(pkey->pkey.rsa); }
|
||||
static void int_rsa_free(EVP_PKEY *pkey) {
|
||||
RSA_free(pkey->pkey);
|
||||
pkey->pkey = NULL;
|
||||
}
|
||||
|
||||
const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = {
|
||||
EVP_PKEY_RSA,
|
||||
|
||||
@@ -27,7 +27,6 @@ static int pkey_x25519_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { return 1; }
|
||||
static int pkey_x25519_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
|
||||
X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY));
|
||||
if (key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -39,8 +38,8 @@ static int pkey_x25519_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
|
||||
X25519_keypair(key->pub, key->priv);
|
||||
key->has_private = 1;
|
||||
|
||||
OPENSSL_free(pkey->pkey.ptr);
|
||||
pkey->pkey.ptr = key;
|
||||
OPENSSL_free(pkey->pkey);
|
||||
pkey->pkey = key;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -51,8 +50,8 @@ static int pkey_x25519_derive(EVP_PKEY_CTX *ctx, uint8_t *out,
|
||||
return 0;
|
||||
}
|
||||
|
||||
const X25519_KEY *our_key = ctx->pkey->pkey.ptr;
|
||||
const X25519_KEY *peer_key = ctx->peerkey->pkey.ptr;
|
||||
const X25519_KEY *our_key = ctx->pkey->pkey;
|
||||
const X25519_KEY *peer_key = ctx->peerkey->pkey;
|
||||
if (our_key == NULL || peer_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET);
|
||||
return 0;
|
||||
|
||||
@@ -24,8 +24,8 @@
|
||||
|
||||
|
||||
static void x25519_free(EVP_PKEY *pkey) {
|
||||
OPENSSL_free(pkey->pkey.ptr);
|
||||
pkey->pkey.ptr = NULL;
|
||||
OPENSSL_free(pkey->pkey);
|
||||
pkey->pkey = NULL;
|
||||
}
|
||||
|
||||
static int x25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
@@ -36,7 +36,6 @@ static int x25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
|
||||
X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY));
|
||||
if (key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -45,7 +44,7 @@ static int x25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
key->has_private = 1;
|
||||
|
||||
x25519_free(pkey);
|
||||
pkey->pkey.ptr = key;
|
||||
pkey->pkey = key;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -57,7 +56,6 @@ static int x25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
|
||||
X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY));
|
||||
if (key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -65,13 +63,13 @@ static int x25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
|
||||
key->has_private = 0;
|
||||
|
||||
x25519_free(pkey);
|
||||
pkey->pkey.ptr = key;
|
||||
pkey->pkey = key;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int x25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
|
||||
size_t *out_len) {
|
||||
const X25519_KEY *key = pkey->pkey.ptr;
|
||||
size_t *out_len) {
|
||||
const X25519_KEY *key = pkey->pkey;
|
||||
if (!key->has_private) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
|
||||
return 0;
|
||||
@@ -94,7 +92,7 @@ static int x25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
|
||||
|
||||
static int x25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
|
||||
size_t *out_len) {
|
||||
const X25519_KEY *key = pkey->pkey.ptr;
|
||||
const X25519_KEY *key = pkey->pkey;
|
||||
if (out == NULL) {
|
||||
*out_len = 32;
|
||||
return 1;
|
||||
@@ -117,7 +115,7 @@ static int x25519_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
|
||||
|
||||
static size_t x25519_get1_tls_encodedpoint(const EVP_PKEY *pkey,
|
||||
uint8_t **out_ptr) {
|
||||
const X25519_KEY *key = pkey->pkey.ptr;
|
||||
const X25519_KEY *key = pkey->pkey;
|
||||
if (key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
|
||||
return 0;
|
||||
@@ -140,7 +138,7 @@ static int x25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
|
||||
}
|
||||
|
||||
static int x25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
|
||||
const X25519_KEY *key = pkey->pkey.ptr;
|
||||
const X25519_KEY *key = pkey->pkey;
|
||||
|
||||
// See RFC 8410, section 4.
|
||||
CBB spki, algorithm, oid, key_bitstring;
|
||||
@@ -160,8 +158,8 @@ static int x25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
|
||||
}
|
||||
|
||||
static int x25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
const X25519_KEY *a_key = a->pkey.ptr;
|
||||
const X25519_KEY *b_key = b->pkey.ptr;
|
||||
const X25519_KEY *a_key = a->pkey;
|
||||
const X25519_KEY *b_key = b->pkey;
|
||||
return OPENSSL_memcmp(a_key->pub, b_key->pub, 32) == 0;
|
||||
}
|
||||
|
||||
@@ -182,7 +180,7 @@ static int x25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
|
||||
}
|
||||
|
||||
static int x25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
|
||||
X25519_KEY *key = pkey->pkey.ptr;
|
||||
const X25519_KEY *key = pkey->pkey;
|
||||
if (!key->has_private) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
|
||||
return 0;
|
||||
|
||||
@@ -50,6 +50,8 @@
|
||||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||
* Hudson (tjh@cryptsoft.com). */
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <CCryptoBoringSSL_evp.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
@@ -117,7 +119,6 @@ static int bn_print(BIO *bp, const char *name, const BIGNUM *num, int off) {
|
||||
size_t len = BN_num_bytes(num);
|
||||
uint8_t *buf = OPENSSL_malloc(len + 1);
|
||||
if (buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -182,11 +183,11 @@ static int do_rsa_print(BIO *out, const RSA *rsa, int off,
|
||||
}
|
||||
|
||||
static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
|
||||
return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
|
||||
return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 0);
|
||||
}
|
||||
|
||||
static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
|
||||
return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
|
||||
return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -226,15 +227,15 @@ static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
|
||||
}
|
||||
|
||||
static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
|
||||
return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
|
||||
return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 0);
|
||||
}
|
||||
|
||||
static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
|
||||
return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
|
||||
return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 1);
|
||||
}
|
||||
|
||||
static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
|
||||
return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
|
||||
return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 2);
|
||||
}
|
||||
|
||||
|
||||
@@ -294,16 +295,16 @@ static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
|
||||
}
|
||||
|
||||
static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
|
||||
return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0);
|
||||
return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 0);
|
||||
}
|
||||
|
||||
static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
|
||||
return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1);
|
||||
return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 1);
|
||||
}
|
||||
|
||||
|
||||
static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
|
||||
return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2);
|
||||
return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 2);
|
||||
}
|
||||
|
||||
|
||||
@@ -355,7 +356,7 @@ static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
|
||||
|
||||
int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
|
||||
ASN1_PCTX *pctx) {
|
||||
EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
|
||||
EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
|
||||
if (method != NULL && method->pub_print != NULL) {
|
||||
return method->pub_print(out, pkey, indent);
|
||||
}
|
||||
@@ -364,7 +365,7 @@ int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
|
||||
|
||||
int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
|
||||
ASN1_PCTX *pctx) {
|
||||
EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
|
||||
EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
|
||||
if (method != NULL && method->priv_print != NULL) {
|
||||
return method->priv_print(out, pkey, indent);
|
||||
}
|
||||
@@ -373,7 +374,7 @@ int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
|
||||
|
||||
int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
|
||||
ASN1_PCTX *pctx) {
|
||||
EVP_PKEY_PRINT_METHOD *method = find_method(pkey->type);
|
||||
EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
|
||||
if (method != NULL && method->param_print != NULL) {
|
||||
return method->param_print(out, pkey, indent);
|
||||
}
|
||||
|
||||
@@ -177,7 +177,6 @@ int EVP_PBE_scrypt(const char *password, size_t password_len,
|
||||
size_t V_blocks = N * 2 * r;
|
||||
block_t *B = OPENSSL_malloc((B_blocks + T_blocks + V_blocks) * sizeof(block_t));
|
||||
if (B == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +137,6 @@ int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class, int *out_index,
|
||||
|
||||
funcs = OPENSSL_malloc(sizeof(CRYPTO_EX_DATA_FUNCS));
|
||||
if (funcs == NULL) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -152,7 +151,6 @@ int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class, int *out_index,
|
||||
}
|
||||
|
||||
if (ex_data_class->meth == NULL) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -164,7 +162,6 @@ int CRYPTO_get_ex_new_index(CRYPTO_EX_DATA_CLASS *ex_data_class, int *out_index,
|
||||
}
|
||||
|
||||
if (!sk_CRYPTO_EX_DATA_FUNCS_push(ex_data_class->meth, funcs)) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
funcs = NULL; // |sk_CRYPTO_EX_DATA_FUNCS_push| takes ownership.
|
||||
@@ -190,7 +187,6 @@ int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val) {
|
||||
if (ad->sk == NULL) {
|
||||
ad->sk = sk_void_new_null();
|
||||
if (ad->sk == NULL) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -198,7 +194,6 @@ int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val) {
|
||||
// Add NULL values until the stack is long enough.
|
||||
for (size_t i = sk_void_num(ad->sk); i <= (size_t)index; i++) {
|
||||
if (!sk_void_push(ad->sk, NULL)) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -235,7 +230,6 @@ static int get_func_pointers(STACK_OF(CRYPTO_EX_DATA_FUNCS) **out,
|
||||
CRYPTO_STATIC_MUTEX_unlock_read(&ex_data_class->lock);
|
||||
|
||||
if (n > 0 && *out == NULL) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,12 +59,6 @@ OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); }
|
||||
OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); }
|
||||
#endif
|
||||
|
||||
#elif defined(OPENSSL_PPC64LE)
|
||||
#define HWAES
|
||||
|
||||
OPENSSL_INLINE int hwaes_capable(void) {
|
||||
return CRYPTO_is_PPC64LE_vcrypto_capable();
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // !NO_ASM
|
||||
|
||||
+72
-42
@@ -223,7 +223,7 @@ _aesni_ctr32_ghash_6x:
|
||||
movbeq 0(%r14),%r12
|
||||
vaesenc %xmm1,%xmm14,%xmm14
|
||||
vmovups 160-128(%rcx),%xmm1
|
||||
cmpl $11,%ebp
|
||||
cmpl $11,%r10d
|
||||
jb .Lenc_tail
|
||||
|
||||
vaesenc %xmm15,%xmm9,%xmm9
|
||||
@@ -322,7 +322,7 @@ _aesni_ctr32_ghash_6x:
|
||||
vaesenclast %xmm3,%xmm14,%xmm14
|
||||
vpaddb %xmm2,%xmm7,%xmm3
|
||||
|
||||
addq $0x60,%r10
|
||||
addq $0x60,%rax
|
||||
subq $0x6,%rdx
|
||||
jc .L6x_done
|
||||
|
||||
@@ -354,27 +354,35 @@ _aesni_ctr32_ghash_6x:
|
||||
.align 32
|
||||
aesni_gcm_decrypt:
|
||||
.cfi_startproc
|
||||
xorq %r10,%r10
|
||||
|
||||
xorq %rax,%rax
|
||||
|
||||
|
||||
|
||||
cmpq $0x60,%rdx
|
||||
jb .Lgcm_dec_abort
|
||||
|
||||
leaq (%rsp),%rax
|
||||
.cfi_def_cfa_register %rax
|
||||
pushq %rbx
|
||||
.cfi_offset %rbx,-16
|
||||
pushq %rbp
|
||||
.cfi_offset %rbp,-24
|
||||
.cfi_adjust_cfa_offset 8
|
||||
.cfi_offset %rbp,-16
|
||||
|
||||
movq %rsp,%rbp
|
||||
.cfi_def_cfa_register %rbp
|
||||
pushq %rbx
|
||||
.cfi_offset %rbx,-24
|
||||
|
||||
pushq %r12
|
||||
.cfi_offset %r12,-32
|
||||
|
||||
pushq %r13
|
||||
.cfi_offset %r13,-40
|
||||
|
||||
pushq %r14
|
||||
.cfi_offset %r14,-48
|
||||
|
||||
pushq %r15
|
||||
.cfi_offset %r15,-56
|
||||
|
||||
vzeroupper
|
||||
|
||||
vmovdqu (%r8),%xmm1
|
||||
@@ -388,7 +396,7 @@ aesni_gcm_decrypt:
|
||||
vmovdqu (%r11),%xmm0
|
||||
leaq 128(%rcx),%rcx
|
||||
leaq 32+32(%r9),%r9
|
||||
movl 240-128(%rcx),%ebp
|
||||
movl 240-128(%rcx),%r10d
|
||||
vpshufb %xmm0,%xmm8,%xmm8
|
||||
|
||||
andq %r15,%r14
|
||||
@@ -401,7 +409,7 @@ aesni_gcm_decrypt:
|
||||
.Ldec_no_key_aliasing:
|
||||
|
||||
vmovdqu 80(%rdi),%xmm7
|
||||
leaq (%rdi),%r14
|
||||
movq %rdi,%r14
|
||||
vmovdqu 64(%rdi),%xmm4
|
||||
|
||||
|
||||
@@ -414,7 +422,7 @@ aesni_gcm_decrypt:
|
||||
|
||||
vmovdqu 48(%rdi),%xmm5
|
||||
shrq $4,%rdx
|
||||
xorq %r10,%r10
|
||||
xorq %rax,%rax
|
||||
vmovdqu 32(%rdi),%xmm6
|
||||
vpshufb %xmm0,%xmm7,%xmm7
|
||||
vmovdqu 16(%rdi),%xmm2
|
||||
@@ -443,23 +451,29 @@ aesni_gcm_decrypt:
|
||||
vmovdqu %xmm8,-64(%r9)
|
||||
|
||||
vzeroupper
|
||||
movq -48(%rax),%r15
|
||||
leaq -40(%rbp),%rsp
|
||||
.cfi_def_cfa %rsp, 0x38
|
||||
popq %r15
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %r15
|
||||
movq -40(%rax),%r14
|
||||
popq %r14
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %r14
|
||||
movq -32(%rax),%r13
|
||||
popq %r13
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %r13
|
||||
movq -24(%rax),%r12
|
||||
popq %r12
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %r12
|
||||
movq -16(%rax),%rbp
|
||||
.cfi_restore %rbp
|
||||
movq -8(%rax),%rbx
|
||||
popq %rbx
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %rbx
|
||||
leaq (%rax),%rsp
|
||||
.cfi_def_cfa_register %rsp
|
||||
popq %rbp
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %rbp
|
||||
.Lgcm_dec_abort:
|
||||
movq %r10,%rax
|
||||
.byte 0xf3,0xc3
|
||||
|
||||
.cfi_endproc
|
||||
.size aesni_gcm_decrypt,.-aesni_gcm_decrypt
|
||||
.type _aesni_ctr32_6x,@function
|
||||
@@ -468,7 +482,7 @@ _aesni_ctr32_6x:
|
||||
.cfi_startproc
|
||||
vmovdqu 0-128(%rcx),%xmm4
|
||||
vmovdqu 32(%r11),%xmm2
|
||||
leaq -1(%rbp),%r13
|
||||
leaq -1(%r10),%r13
|
||||
vmovups 16-128(%rcx),%xmm15
|
||||
leaq 32-128(%rcx),%r12
|
||||
vpxor %xmm4,%xmm1,%xmm9
|
||||
@@ -561,12 +575,13 @@ _aesni_ctr32_6x:
|
||||
.align 32
|
||||
aesni_gcm_encrypt:
|
||||
.cfi_startproc
|
||||
|
||||
#ifdef BORINGSSL_DISPATCH_TEST
|
||||
.extern BORINGSSL_function_hit
|
||||
.hidden BORINGSSL_function_hit
|
||||
movb $1,BORINGSSL_function_hit+2(%rip)
|
||||
#endif
|
||||
xorq %r10,%r10
|
||||
xorq %rax,%rax
|
||||
|
||||
|
||||
|
||||
@@ -574,20 +589,27 @@ aesni_gcm_encrypt:
|
||||
cmpq $288,%rdx
|
||||
jb .Lgcm_enc_abort
|
||||
|
||||
leaq (%rsp),%rax
|
||||
.cfi_def_cfa_register %rax
|
||||
pushq %rbx
|
||||
.cfi_offset %rbx,-16
|
||||
pushq %rbp
|
||||
.cfi_offset %rbp,-24
|
||||
.cfi_adjust_cfa_offset 8
|
||||
.cfi_offset %rbp,-16
|
||||
|
||||
movq %rsp,%rbp
|
||||
.cfi_def_cfa_register %rbp
|
||||
pushq %rbx
|
||||
.cfi_offset %rbx,-24
|
||||
|
||||
pushq %r12
|
||||
.cfi_offset %r12,-32
|
||||
|
||||
pushq %r13
|
||||
.cfi_offset %r13,-40
|
||||
|
||||
pushq %r14
|
||||
.cfi_offset %r14,-48
|
||||
|
||||
pushq %r15
|
||||
.cfi_offset %r15,-56
|
||||
|
||||
vzeroupper
|
||||
|
||||
vmovdqu (%r8),%xmm1
|
||||
@@ -599,7 +621,7 @@ aesni_gcm_encrypt:
|
||||
leaq 128(%rcx),%rcx
|
||||
vmovdqu (%r11),%xmm0
|
||||
andq $-128,%rsp
|
||||
movl 240-128(%rcx),%ebp
|
||||
movl 240-128(%rcx),%r10d
|
||||
|
||||
andq %r15,%r14
|
||||
andq %rsp,%r15
|
||||
@@ -610,7 +632,7 @@ aesni_gcm_encrypt:
|
||||
subq %r15,%rsp
|
||||
.Lenc_no_key_aliasing:
|
||||
|
||||
leaq (%rsi),%r14
|
||||
movq %rsi,%r14
|
||||
|
||||
|
||||
|
||||
@@ -641,7 +663,7 @@ aesni_gcm_encrypt:
|
||||
vmovdqu (%r9),%xmm8
|
||||
leaq 32+32(%r9),%r9
|
||||
subq $12,%rdx
|
||||
movq $192,%r10
|
||||
movq $192,%rax
|
||||
vpshufb %xmm0,%xmm8,%xmm8
|
||||
|
||||
call _aesni_ctr32_ghash_6x
|
||||
@@ -821,25 +843,32 @@ aesni_gcm_encrypt:
|
||||
vmovdqu %xmm8,-64(%r9)
|
||||
|
||||
vzeroupper
|
||||
movq -48(%rax),%r15
|
||||
leaq -40(%rbp),%rsp
|
||||
.cfi_def_cfa %rsp, 0x38
|
||||
popq %r15
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %r15
|
||||
movq -40(%rax),%r14
|
||||
popq %r14
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %r14
|
||||
movq -32(%rax),%r13
|
||||
popq %r13
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %r13
|
||||
movq -24(%rax),%r12
|
||||
popq %r12
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %r12
|
||||
movq -16(%rax),%rbp
|
||||
.cfi_restore %rbp
|
||||
movq -8(%rax),%rbx
|
||||
popq %rbx
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %rbx
|
||||
leaq (%rax),%rsp
|
||||
.cfi_def_cfa_register %rsp
|
||||
popq %rbp
|
||||
.cfi_adjust_cfa_offset -8
|
||||
.cfi_restore %rbp
|
||||
.Lgcm_enc_abort:
|
||||
movq %r10,%rax
|
||||
.byte 0xf3,0xc3
|
||||
|
||||
.cfi_endproc
|
||||
.size aesni_gcm_encrypt,.-aesni_gcm_encrypt
|
||||
.size aesni_gcm_decrypt,.-aesni_gcm_decrypt
|
||||
.section .rodata
|
||||
.align 64
|
||||
.Lbswap_mask:
|
||||
.byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
|
||||
@@ -853,6 +882,7 @@ aesni_gcm_encrypt:
|
||||
.byte 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
||||
.byte 65,69,83,45,78,73,32,71,67,77,32,109,111,100,117,108,101,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
|
||||
.align 64
|
||||
.text
|
||||
#endif
|
||||
#if defined(__ELF__)
|
||||
// See https://www.airs.com/blog/archives/518.
|
||||
+47
-31
@@ -223,7 +223,7 @@ L$resume_ctr32:
|
||||
movbeq 0(%r14),%r12
|
||||
vaesenc %xmm1,%xmm14,%xmm14
|
||||
vmovups 160-128(%rcx),%xmm1
|
||||
cmpl $11,%ebp
|
||||
cmpl $11,%r10d
|
||||
jb L$enc_tail
|
||||
|
||||
vaesenc %xmm15,%xmm9,%xmm9
|
||||
@@ -322,7 +322,7 @@ L$enc_tail:
|
||||
vaesenclast %xmm3,%xmm14,%xmm14
|
||||
vpaddb %xmm2,%xmm7,%xmm3
|
||||
|
||||
addq $0x60,%r10
|
||||
addq $0x60,%rax
|
||||
subq $0x6,%rdx
|
||||
jc L$6x_done
|
||||
|
||||
@@ -354,27 +354,34 @@ L$6x_done:
|
||||
.p2align 5
|
||||
_aesni_gcm_decrypt:
|
||||
|
||||
xorq %r10,%r10
|
||||
|
||||
xorq %rax,%rax
|
||||
|
||||
|
||||
|
||||
cmpq $0x60,%rdx
|
||||
jb L$gcm_dec_abort
|
||||
|
||||
leaq (%rsp),%rax
|
||||
pushq %rbp
|
||||
|
||||
|
||||
movq %rsp,%rbp
|
||||
|
||||
pushq %rbx
|
||||
|
||||
pushq %rbp
|
||||
|
||||
pushq %r12
|
||||
|
||||
|
||||
pushq %r13
|
||||
|
||||
|
||||
pushq %r14
|
||||
|
||||
|
||||
pushq %r15
|
||||
|
||||
|
||||
vzeroupper
|
||||
|
||||
vmovdqu (%r8),%xmm1
|
||||
@@ -388,7 +395,7 @@ _aesni_gcm_decrypt:
|
||||
vmovdqu (%r11),%xmm0
|
||||
leaq 128(%rcx),%rcx
|
||||
leaq 32+32(%r9),%r9
|
||||
movl 240-128(%rcx),%ebp
|
||||
movl 240-128(%rcx),%r10d
|
||||
vpshufb %xmm0,%xmm8,%xmm8
|
||||
|
||||
andq %r15,%r14
|
||||
@@ -401,7 +408,7 @@ _aesni_gcm_decrypt:
|
||||
L$dec_no_key_aliasing:
|
||||
|
||||
vmovdqu 80(%rdi),%xmm7
|
||||
leaq (%rdi),%r14
|
||||
movq %rdi,%r14
|
||||
vmovdqu 64(%rdi),%xmm4
|
||||
|
||||
|
||||
@@ -414,7 +421,7 @@ L$dec_no_key_aliasing:
|
||||
|
||||
vmovdqu 48(%rdi),%xmm5
|
||||
shrq $4,%rdx
|
||||
xorq %r10,%r10
|
||||
xorq %rax,%rax
|
||||
vmovdqu 32(%rdi),%xmm6
|
||||
vpshufb %xmm0,%xmm7,%xmm7
|
||||
vmovdqu 16(%rdi),%xmm2
|
||||
@@ -443,32 +450,32 @@ L$dec_no_key_aliasing:
|
||||
vmovdqu %xmm8,-64(%r9)
|
||||
|
||||
vzeroupper
|
||||
movq -48(%rax),%r15
|
||||
leaq -40(%rbp),%rsp
|
||||
|
||||
movq -40(%rax),%r14
|
||||
popq %r15
|
||||
|
||||
movq -32(%rax),%r13
|
||||
popq %r14
|
||||
|
||||
movq -24(%rax),%r12
|
||||
popq %r13
|
||||
|
||||
movq -16(%rax),%rbp
|
||||
popq %r12
|
||||
|
||||
movq -8(%rax),%rbx
|
||||
popq %rbx
|
||||
|
||||
leaq (%rax),%rsp
|
||||
popq %rbp
|
||||
|
||||
L$gcm_dec_abort:
|
||||
movq %r10,%rax
|
||||
.byte 0xf3,0xc3
|
||||
|
||||
|
||||
|
||||
|
||||
.p2align 5
|
||||
_aesni_ctr32_6x:
|
||||
|
||||
vmovdqu 0-128(%rcx),%xmm4
|
||||
vmovdqu 32(%r11),%xmm2
|
||||
leaq -1(%rbp),%r13
|
||||
leaq -1(%r10),%r13
|
||||
vmovups 16-128(%rcx),%xmm15
|
||||
leaq 32-128(%rcx),%r12
|
||||
vpxor %xmm4,%xmm1,%xmm9
|
||||
@@ -561,11 +568,12 @@ L$handle_ctr32_2:
|
||||
.p2align 5
|
||||
_aesni_gcm_encrypt:
|
||||
|
||||
|
||||
#ifdef BORINGSSL_DISPATCH_TEST
|
||||
|
||||
movb $1,_BORINGSSL_function_hit+2(%rip)
|
||||
#endif
|
||||
xorq %r10,%r10
|
||||
xorq %rax,%rax
|
||||
|
||||
|
||||
|
||||
@@ -573,20 +581,26 @@ _aesni_gcm_encrypt:
|
||||
cmpq $288,%rdx
|
||||
jb L$gcm_enc_abort
|
||||
|
||||
leaq (%rsp),%rax
|
||||
pushq %rbp
|
||||
|
||||
|
||||
movq %rsp,%rbp
|
||||
|
||||
pushq %rbx
|
||||
|
||||
pushq %rbp
|
||||
|
||||
pushq %r12
|
||||
|
||||
|
||||
pushq %r13
|
||||
|
||||
|
||||
pushq %r14
|
||||
|
||||
|
||||
pushq %r15
|
||||
|
||||
|
||||
vzeroupper
|
||||
|
||||
vmovdqu (%r8),%xmm1
|
||||
@@ -598,7 +612,7 @@ _aesni_gcm_encrypt:
|
||||
leaq 128(%rcx),%rcx
|
||||
vmovdqu (%r11),%xmm0
|
||||
andq $-128,%rsp
|
||||
movl 240-128(%rcx),%ebp
|
||||
movl 240-128(%rcx),%r10d
|
||||
|
||||
andq %r15,%r14
|
||||
andq %rsp,%r15
|
||||
@@ -609,7 +623,7 @@ _aesni_gcm_encrypt:
|
||||
subq %r15,%rsp
|
||||
L$enc_no_key_aliasing:
|
||||
|
||||
leaq (%rsi),%r14
|
||||
movq %rsi,%r14
|
||||
|
||||
|
||||
|
||||
@@ -640,7 +654,7 @@ L$enc_no_key_aliasing:
|
||||
vmovdqu (%r9),%xmm8
|
||||
leaq 32+32(%r9),%r9
|
||||
subq $12,%rdx
|
||||
movq $192,%r10
|
||||
movq $192,%rax
|
||||
vpshufb %xmm0,%xmm8,%xmm8
|
||||
|
||||
call _aesni_ctr32_ghash_6x
|
||||
@@ -820,25 +834,26 @@ L$enc_no_key_aliasing:
|
||||
vmovdqu %xmm8,-64(%r9)
|
||||
|
||||
vzeroupper
|
||||
movq -48(%rax),%r15
|
||||
leaq -40(%rbp),%rsp
|
||||
|
||||
movq -40(%rax),%r14
|
||||
popq %r15
|
||||
|
||||
movq -32(%rax),%r13
|
||||
popq %r14
|
||||
|
||||
movq -24(%rax),%r12
|
||||
popq %r13
|
||||
|
||||
movq -16(%rax),%rbp
|
||||
popq %r12
|
||||
|
||||
movq -8(%rax),%rbx
|
||||
popq %rbx
|
||||
|
||||
leaq (%rax),%rsp
|
||||
popq %rbp
|
||||
|
||||
L$gcm_enc_abort:
|
||||
movq %r10,%rax
|
||||
.byte 0xf3,0xc3
|
||||
|
||||
|
||||
|
||||
.section __DATA,__const
|
||||
.p2align 6
|
||||
L$bswap_mask:
|
||||
.byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
|
||||
@@ -852,6 +867,7 @@ L$one_lsb:
|
||||
.byte 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
||||
.byte 65,69,83,45,78,73,32,71,67,77,32,109,111,100,117,108,101,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
|
||||
.p2align 6
|
||||
.text
|
||||
#endif
|
||||
#if defined(__ELF__)
|
||||
// See https://www.airs.com/blog/archives/518.
|
||||
File renamed without changes.
+3
@@ -2462,6 +2462,9 @@ db 32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115
|
||||
db 115,108,46,111,114,103,62,0
|
||||
segment .bss
|
||||
common _OPENSSL_ia32cap_P 16
|
||||
%else
|
||||
; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738
|
||||
ret
|
||||
%endif
|
||||
#endif // defined(__i386__) && defined(_WIN32)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
+2
@@ -2482,6 +2482,7 @@ __aesni_set_encrypt_key:
|
||||
.byte 0xf3,0xc3
|
||||
.size aes_hw_set_encrypt_key,.-aes_hw_set_encrypt_key
|
||||
.size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
|
||||
.section .rodata
|
||||
.align 64
|
||||
.Lbswap_mask:
|
||||
.byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
|
||||
@@ -2504,6 +2505,7 @@ __aesni_set_encrypt_key:
|
||||
|
||||
.byte 65,69,83,32,102,111,114,32,73,110,116,101,108,32,65,69,83,45,78,73,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
|
||||
.align 64
|
||||
.text
|
||||
#endif
|
||||
#if defined(__ELF__)
|
||||
// See https://www.airs.com/blog/archives/518.
|
||||
+2
@@ -2480,6 +2480,7 @@ L$key_expansion_256b:
|
||||
.byte 0xf3,0xc3
|
||||
|
||||
|
||||
.section __DATA,__const
|
||||
.p2align 6
|
||||
L$bswap_mask:
|
||||
.byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
|
||||
@@ -2502,6 +2503,7 @@ L$key_rcon1b:
|
||||
|
||||
.byte 65,69,83,32,102,111,114,32,73,110,116,101,108,32,65,69,83,45,78,73,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
|
||||
.p2align 6
|
||||
.text
|
||||
#endif
|
||||
#if defined(__ELF__)
|
||||
// See https://www.airs.com/blog/archives/518.
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+3
@@ -839,6 +839,9 @@ L$016aw_end:
|
||||
pop ebx
|
||||
pop ebp
|
||||
ret
|
||||
%else
|
||||
; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738
|
||||
ret
|
||||
%endif
|
||||
#endif // defined(__i386__) && defined(_WIN32)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
@@ -0,0 +1,108 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__aarch64__) && defined(__APPLE__)
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if !defined(__has_feature)
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(__AARCH64EL__) && defined(__APPLE__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
.text
|
||||
|
||||
// BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
|
||||
// size_t num);
|
||||
|
||||
.globl _bn_add_words
|
||||
.private_extern _bn_add_words
|
||||
.align 4
|
||||
_bn_add_words:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
# Clear the carry flag.
|
||||
cmn xzr, xzr
|
||||
|
||||
# aarch64 can load two registers at a time, so we do two loop iterations at
|
||||
# at a time. Split x3 = 2 * x8 + x3. This allows loop
|
||||
# operations to use CBNZ without clobbering the carry flag.
|
||||
lsr x8, x3, #1
|
||||
and x3, x3, #1
|
||||
|
||||
cbz x8, Ladd_tail
|
||||
Ladd_loop:
|
||||
ldp x4, x5, [x1], #16
|
||||
ldp x6, x7, [x2], #16
|
||||
sub x8, x8, #1
|
||||
adcs x4, x4, x6
|
||||
adcs x5, x5, x7
|
||||
stp x4, x5, [x0], #16
|
||||
cbnz x8, Ladd_loop
|
||||
|
||||
Ladd_tail:
|
||||
cbz x3, Ladd_exit
|
||||
ldr x4, [x1], #8
|
||||
ldr x6, [x2], #8
|
||||
adcs x4, x4, x6
|
||||
str x4, [x0], #8
|
||||
|
||||
Ladd_exit:
|
||||
cset x0, cs
|
||||
ret
|
||||
|
||||
|
||||
// BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
|
||||
// size_t num);
|
||||
|
||||
.globl _bn_sub_words
|
||||
.private_extern _bn_sub_words
|
||||
.align 4
|
||||
_bn_sub_words:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
# Set the carry flag. Arm's borrow bit is flipped from the carry flag,
|
||||
# so we want C = 1 here.
|
||||
cmp xzr, xzr
|
||||
|
||||
# aarch64 can load two registers at a time, so we do two loop iterations at
|
||||
# at a time. Split x3 = 2 * x8 + x3. This allows loop
|
||||
# operations to use CBNZ without clobbering the carry flag.
|
||||
lsr x8, x3, #1
|
||||
and x3, x3, #1
|
||||
|
||||
cbz x8, Lsub_tail
|
||||
Lsub_loop:
|
||||
ldp x4, x5, [x1], #16
|
||||
ldp x6, x7, [x2], #16
|
||||
sub x8, x8, #1
|
||||
sbcs x4, x4, x6
|
||||
sbcs x5, x5, x7
|
||||
stp x4, x5, [x0], #16
|
||||
cbnz x8, Lsub_loop
|
||||
|
||||
Lsub_tail:
|
||||
cbz x3, Lsub_exit
|
||||
ldr x4, [x1], #8
|
||||
ldr x6, [x2], #8
|
||||
sbcs x4, x4, x6
|
||||
str x4, [x0], #8
|
||||
|
||||
Lsub_exit:
|
||||
cset x0, cc
|
||||
ret
|
||||
|
||||
#endif // !OPENSSL_NO_ASM && defined(__AARCH64EL__) && defined(__APPLE__)
|
||||
#if defined(__ELF__)
|
||||
// See https://www.airs.com/blog/archives/518.
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif
|
||||
#endif // defined(__aarch64__) && defined(__APPLE__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif
|
||||
|
||||
Loaded 100 of 362 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user