Commit d865d577 authored by filatov's avatar filatov
Browse files

openssl.1.1.0 fixes for generators

parent 518d6724
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ PROJECT = asn1certgen
DEBUG          = yes
bins           = asn1certgen
sources       := asn1certgen.c ecc_openssl.c
cflags        += -Wno-pointer-sign -Wno-enum-compare
sources-WIN32 := applink.c
packages      += cshared openssl
predirs       := asncodec
+25 −13
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ char _tbsHash[512]; // has space for issuer hash + signer hash
int     _tbsHashLength = 0;

char    _signerHashBuf[256]; // has space for issuer hash
char*  _signerHash = &_signerHashBuf[0];
const char* _signerHash = &_signerHashBuf[0];
int    _signerHashLength = 0;

static const char * const _key_formats[] = {
@@ -76,8 +76,7 @@ static copt_t _options [] = {
	{ NULL, NULL, COPT_END, NULL, NULL }
};

static int is_P384CurvePoint_empty(EccP384CurvePoint_t* point);
static int is_P256CurvePoint_empty(EccP256CurvePoint_t* point);
static int is_CurvePoint_empty(EccP256CurvePoint_t* point);

static void fill_curve_point_eccP256(EccP256CurvePoint_t* point, ecc_curve_id curveType, char * keyPath);
static void fill_curve_point_eccP384(EccP384CurvePoint_t* point, ecc_curve_id curveType, char * keyPath);
@@ -152,7 +151,7 @@ static asn_enc_rval_t Signature_oer_encoder(const asn_TYPE_descriptor_t *td,
	asn_app_consume_bytes_f *cb, void *app_key)
{
	Signature_t * s = (Signature_t *)sptr;
	if (is_P256CurvePoint_empty(&s->choice.ecdsaNistP256Signature.rSig)){
	if (is_CurvePoint_empty(&s->choice.ecdsaNistP256Signature.rSig)){
		// look for signer private key
		ecc_curve_id alg = _pk_type_to_curveid[s->present];
		ecc_hash_id hashId = _pk_type_to_hashid[s->present];
@@ -257,7 +256,7 @@ int main(int argc, char ** argv)

	rc_d = asn_decode(NULL, ATS_BASIC_XER, &asn_DEF_EtsiTs103097Certificate, (void**)&cert, buf, ebuf - buf);
	if (rc_d.code != RC_OK){
		fprintf(stderr, "%s: failed to load at position %d\n  %.30s\n", _profileName, rc_d.consumed, buf + rc_d.consumed);
		fprintf(stderr, "%s: failed to load at position %d\n  %.30s\n", _profileName, (int)rc_d.consumed, buf + rc_d.consumed);
		return -1;
	}

@@ -278,7 +277,7 @@ int main(int argc, char ** argv)
		EtsiTs103097Certificate_t * signer = NULL;
		rc_d = asn_decode(NULL, ATS_BASIC_OER, &asn_DEF_EtsiTs103097Certificate, (void**)&signer, buf, ebuf - buf);
		if (rc_d.code != RC_OK){
			fprintf(stderr, "%s: failed to load signer certificate at position %d\n  %.30s\n", _signerName, rc_d.consumed, buf + rc_d.consumed);
			fprintf(stderr, "%s: failed to load signer certificate at position %d\n  %.30s\n", _signerName, (int)rc_d.consumed, buf + rc_d.consumed);
			return -1;
		}
		switch (signer->toBeSigned.verifyKeyIndicator.present){
@@ -288,6 +287,9 @@ int main(int argc, char ** argv)
		case VerificationKeyIndicator_PR_reconstructionValue:
			hashType = PublicVerificationKey_PR_ecdsaNistP256;
			break;
		case VerificationKeyIndicator_PR_NOTHING:
			fprintf(stderr, "%s: signer verification indicator type is unknown\n", _signerName);
			return -1;
		}

		switch (hashType){
@@ -295,17 +297,22 @@ int main(int argc, char ** argv)
		case PublicVerificationKey_PR_ecdsaNistP256:
			if (cert->issuer.present == IssuerIdentifier_PR_NOTHING)
				cert->issuer.present = IssuerIdentifier_PR_sha256AndDigest;
			sha256_calculate(_signerHash, buf, ebuf - buf);
			sha256_calculate(_signerHashBuf, buf, ebuf - buf);
			_signerHash = &_signerHashBuf[0];
			_signerHashLength = sha256_hash_size;
			OCTET_STRING_fromBuf(&cert->issuer.choice.sha256AndDigest, &_signerHash[sha256_hash_size-8], 8);
			break;
		case PublicVerificationKey_PR_ecdsaBrainpoolP384r1:
			if (cert->issuer.present == IssuerIdentifier_PR_NOTHING)
				cert->issuer.present = IssuerIdentifier_PR_sha384AndDigest;
			sha384_calculate(_signerHash, buf, ebuf - buf);
			sha384_calculate(_signerHashBuf, buf, ebuf - buf);
			_signerHash = &_signerHashBuf[0];
			_signerHashLength = sha384_hash_size;
			OCTET_STRING_fromBuf(&cert->issuer.choice.sha384AndDigest, &_signerHash[sha384_hash_size - 8], 8);
			break;
		case PublicVerificationKey_PR_NOTHING:
			fprintf(stderr, "%s: signer verification key type curve is unknown\n", _signerName);
			return -1;
		}
		if (signer){
			ASN_STRUCT_FREE(asn_DEF_EtsiTs103097Certificate, signer);
@@ -320,6 +327,9 @@ int main(int argc, char ** argv)
		case VerificationKeyIndicator_PR_reconstructionValue:
			hashType = PublicVerificationKey_PR_ecdsaNistP256;
			break;
		case VerificationKeyIndicator_PR_NOTHING:
			fprintf(stderr, "%s: signer verification indicator type is unknown\n", _signerName);
			return -1;
		}
		switch (hashType) {
		case PublicVerificationKey_PR_ecdsaBrainpoolP256r1:
@@ -361,10 +371,12 @@ int main(int argc, char ** argv)
		}
		break;
	case VerificationKeyIndicator_PR_reconstructionValue:
		fprintf(stderr, "TODO: reconstruction value generation is unsupported\n");
		fprintf(stderr, "TODO: reconstruction value generation is unsupported yet\n");
		return -1;
	case VerificationKeyIndicator_PR_NOTHING:
	default:
		break;
		fprintf(stderr, "Unknown verification key indicator type\n");
		return -1;
	}
	if (cert->toBeSigned.encryptionKey){
		cvstrncpy(buf, CERT_MAX_SIZE, _keyPath, "/", _profileName, EXT_EKEY, NULL);
@@ -409,7 +421,7 @@ int main(int argc, char ** argv)
	return 0;
}

static int is_P256CurvePoint_empty(EccP256CurvePoint_t* point)
static int is_CurvePoint_empty(EccP256CurvePoint_t* point)
{
	switch (point->present){
	case EccP256CurvePoint_PR_x_only:
@@ -445,7 +457,7 @@ static void fill_curve_point_eccP256(EccP256CurvePoint_t* point, ecc_curve_id cu
	}
	ecc_key_free(key);
}

/*
static int is_P384CurvePoint_empty(EccP384CurvePoint_t* point){
	switch (point->present){
	case EccP256CurvePoint_PR_x_only:
@@ -459,7 +471,7 @@ static int is_P384CurvePoint_empty(EccP384CurvePoint_t* point){
	}
	return 1;
}

*/
static void fill_curve_point_eccP384(EccP384CurvePoint_t* point, ecc_curve_id curveType, char * keyPath)
{
	void * key;
+8 −8
Original line number Diff line number Diff line
@@ -501,8 +501,8 @@
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <LinkIncremental>true</LinkIncremental>
    <OutDir>$(ProjectDir)..\build\msvc\$(Platform)\$(Configuration)</OutDir>
    <IntDir>$(ProjectDir)..\build\msvc\$(Platform)\$(Configuration)\$(ProjectName)</IntDir>
    <OutDir>$(ProjectDir)..\build\msvc\$(Platform)\$(Configuration)\</OutDir>
    <IntDir>$(ProjectDir)..\build\msvc\$(Platform)\$(Configuration)\$(ProjectName)\</IntDir>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <LinkIncremental>false</LinkIncremental>
@@ -511,8 +511,8 @@
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <LinkIncremental>false</LinkIncremental>
    <OutDir>$(ProjectDir)..\build\msvc\$(Platform)\$(Configuration)</OutDir>
    <IntDir>$(ProjectDir)..\build\msvc\$(Platform)\$(Configuration)\$(ProjectName)</IntDir>
    <OutDir>$(ProjectDir)..\build\msvc\$(Platform)\$(Configuration)\</OutDir>
    <IntDir>$(ProjectDir)..\build\msvc\$(Platform)\$(Configuration)\$(ProjectName)\</IntDir>
  </PropertyGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
@@ -542,8 +542,8 @@
    <Link>
      <SubSystem>Console</SubSystem>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <AdditionalLibraryDirectories>C:\OpenSSL\Win64\lib\VC\static</AdditionalLibraryDirectories>
      <AdditionalDependencies>libeay32MDd.lib;ssleay32MDd.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <AdditionalLibraryDirectories>C:\OpenSSL\Win64\lib\VC</AdditionalLibraryDirectories>
      <AdditionalDependencies>ws2_32.lib;libcrypto64MDd.lib;libssl64MDd.lib;%(AdditionalDependencies)</AdditionalDependencies>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -582,8 +582,8 @@
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <EnableCOMDATFolding>true</EnableCOMDATFolding>
      <OptimizeReferences>true</OptimizeReferences>
      <AdditionalDependencies>libeay32MDd.lib;ssleay32MDd.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <AdditionalLibraryDirectories>C:\OpenSSL\Win64\lib\VC\static</AdditionalLibraryDirectories>
      <AdditionalDependencies> ws2_32.lib;libcrypto64MD.lib;libssl64MD.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <AdditionalLibraryDirectories>C:\OpenSSL\Win64\lib\VC</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+22 −13
Original line number Diff line number Diff line
@@ -137,7 +137,7 @@ int ecc_key_public(void* key, char * px, char * py, int * psign)
	const EC_GROUP * ecgroup;
	const EC_POINT * ecpoint;
	const EC_KEY   * eckey = (EC_KEY*)key;
	BIGNUM x, y;
	BIGNUM *x, *y;
	int fsize, bcount = -1;

	if( key && px && py ) {
@@ -146,20 +146,21 @@ int ecc_key_public(void* key, char * px, char * py, int * psign)
		fsize = (EC_GROUP_get_degree(ecgroup) + 7) / 8;

		//fill public key data
		BN_init(&x); BN_init(&y);
		if (EC_POINT_get_affine_coordinates_GFp(ecgroup, ecpoint, &x, &y, NULL)){
			bcount = BN_num_bytes(&x);
		x = BN_new();
		y = BN_new();
		if (EC_POINT_get_affine_coordinates_GFp(ecgroup, ecpoint, x, y, NULL)){
			bcount = BN_num_bytes(x);
			for(; bcount < fsize; bcount++)
				*(px++) = 0; // add padding with zeros
			BN_bn2bin(&x, (unsigned char*)px);
			BN_bn2bin(x, (unsigned char*)px);
	
			bcount = BN_num_bytes(&y);
			bcount = BN_num_bytes(y);
			for(; bcount < fsize; bcount++)
				*(py++) = 0; // add padding with zeros
			BN_bn2bin(&y, (unsigned char*)py);
			if (psign) * psign = BN_is_odd(&y);
			BN_bn2bin(y, (unsigned char*)py);
			if (psign) * psign = BN_is_odd(y);
		}
		BN_clear_free(&x); BN_clear_free(&y);
		BN_clear_free(x); BN_clear_free(y);
	}
	return bcount;
}
@@ -424,14 +425,22 @@ int ecc_sign(void * key, const char * hash, int hlength, char *r, char *s)
		EC_KEY_free(eckey);
		if (ecdsa){
			int i, bcount;
			bcount = BN_num_bytes(ecdsa->r);
			const BIGNUM* sr;
			const BIGNUM* ss;
# if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
			ECDSA_SIG_get0(ecdsa, &sr, &ss);
#else
			sr = ecdsa->r;
			ss = ecdsa->s;
#endif
			bcount = BN_num_bytes(sr);
			for(i=bcount; i < fsize; i++)
				*(r++) = 0; // add padding with zeros
			BN_bn2bin(ecdsa->r, r);
			bcount = BN_num_bytes(ecdsa->s);
			BN_bn2bin(sr, r);
			bcount = BN_num_bytes(ss);
			for(i=bcount; i < fsize; i++)
				*(s++) = 0; // add padding with zeros
			BN_bn2bin(ecdsa->s, s);
			BN_bn2bin(ss, s);
			ECDSA_SIG_free(ecdsa);
			return 0;
		}
+15 −4
Original line number Diff line number Diff line
@@ -282,7 +282,7 @@ int main(int argc, char ** argv)
				p += length;
			}
			else {
				fprintf(stderr, "Unsupported signer type: %d\n", ENUMSTRING(*p, _signer_types));
				fprintf(stderr, "Unsupported signer type: %s\n", ENUMSTRING(*p, _signer_types));
				return -1;
			}
			fprintf(stdout, "\t</signer>\n");
@@ -368,11 +368,22 @@ int main(int argc, char ** argv)
		ecdsa = ECDSA_SIG_new();
		const char * r = cert + certlen - 64;
		const char * s = cert + certlen - 32;

		if (ecdsa->r == BN_bin2bn((const unsigned char *)r, 32, ecdsa->r) &&
			ecdsa->s == BN_bin2bn((const unsigned char *)s, 32, ecdsa->s)){
		BIGNUM *bnr=NULL, *bns=NULL;
		BN_bin2bn((const unsigned char *)r, 32, bnr);
		BN_bin2bn((const unsigned char *)s, 32, bns);
		if (bnr && bns) {
#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
			ECDSA_SIG_set0(ecdsa, bnr,bns);
#else
			ecdsa->r = bnr;
			ecdsa->s = bns;
#endif
			rc = ECDSA_do_verify(hash, 32, ecdsa, key);
		}
		else{
			BN_clear_free(bnr);
			BN_clear_free(bns);
		}
		if (rc < 0){
			printf("ERROR");
			ERR_print_errors_fp(stderr);
Loading