Skip to content

Commit 8c55580

Browse files
committed
PROV: Add DERlib support for DSA
This replaces crypto/dsa/dsa_aid.c with new code and generated OIDs Reviewed-by: Matt Caswell <[email protected]> (Merged from openssl#11450)
1 parent 6f5837d commit 8c55580

File tree

8 files changed

+145
-85
lines changed

8 files changed

+145
-85
lines changed

crypto/dsa/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
LIBS=../../libcrypto
22

3-
$COMMON=dsa_sign.c dsa_vrf.c dsa_lib.c dsa_ossl.c dsa_aid.c dsa_check.c \
3+
$COMMON=dsa_sign.c dsa_vrf.c dsa_lib.c dsa_ossl.c dsa_check.c \
44
dsa_key.c dsa_backend.c
55

66
SOURCE[../../libcrypto]=$COMMON\

crypto/dsa/dsa_aid.c

Lines changed: 0 additions & 70 deletions
This file was deleted.

providers/common/der/DSA.asn1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- -------------------------------------------------------------------
2+
-- Taken from RFC 3279, 3 ASN.1 Module
3+
-- (https://www.rfc-editor.org/rfc/rfc3279.html#section-3)
4+
5+
-- OID for DSA public key
6+
7+
id-dsa OBJECT IDENTIFIER ::= {
8+
iso(1) member-body(2) us(840) x9-57(10040) x9algorithm(4) 1 }
9+
10+
-- OID for DSA signature generated with SHA-1 hash
11+
12+
id-dsa-with-sha1 OBJECT IDENTIFIER ::= {
13+
iso(1) member-body(2) us(840) x9-57 (10040) x9algorithm(4) 3 }
14+
15+
16+
-- -------------------------------------------------------------------
17+
-- Taken from https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration
18+
19+
sigAlgs OBJECT IDENTIFIER ::= { 2 16 840 1 101 3 4 3 }
20+
21+
id-dsa-with-sha224 OBJECT IDENTIFIER ::= { sigAlgs 1 }
22+
id-dsa-with-sha256 OBJECT IDENTIFIER ::= { sigAlgs 2 }
23+
id-dsa-with-sha384 OBJECT IDENTIFIER ::= { sigAlgs 3 }
24+
id-dsa-with-sha512 OBJECT IDENTIFIER ::= { sigAlgs 4 }
25+
26+
id-dsa-with-sha3-224 OBJECT IDENTIFIER ::= { sigAlgs 5 }
27+
id-dsa-with-sha3-256 OBJECT IDENTIFIER ::= { sigAlgs 6 }
28+
id-dsa-with-sha3-384 OBJECT IDENTIFIER ::= { sigAlgs 7 }
29+
id-dsa-with-sha3-512 OBJECT IDENTIFIER ::= { sigAlgs 8 }

providers/common/der/build.info

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$FIPSABLE=der_rsa.c
1+
$FIPSABLE=der_rsa.c der_dsa.c
22

33
SOURCE[../../libfips.a]=$FIPSABLE
44
SOURCE[../../libnonfips.a]=$FIPSABLE
@@ -9,3 +9,10 @@ DEPEND[der_rsa.c]=oids_to_c.pm
99
DEPEND[der_rsa.o]=../include/prov/der_rsa.h
1010
GENERATE[../include/prov/der_rsa.h]=der_rsa.h.in
1111
DEPEND[../include/prov/der_rsa.h]=oids_to_c.pm
12+
13+
GENERATE[der_dsa.c]=der_dsa.c.in
14+
DEPEND[der_dsa.c]=oids_to_c.pm
15+
16+
DEPEND[der_dsa.o]=../include/prov/der_dsa.h
17+
GENERATE[../include/prov/der_dsa.h]=der_dsa.h.in
18+
DEPEND[../include/prov/der_dsa.h]=oids_to_c.pm

providers/common/der/der_dsa.c.in

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. You can obtain a copy
6+
* in the file LICENSE in the source distribution or at
7+
* https://www.openssl.org/source/license.html
8+
*/
9+
10+
#include <openssl/bn.h>
11+
#include <openssl/obj_mac.h>
12+
#include "prov/der_dsa.h"
13+
14+
/* Well known OIDs precompiled */
15+
{-
16+
$OUT = oids_to_c::process_leaves('providers/common/der/DSA.asn1',
17+
{ dir => $config{sourcedir},
18+
filter => \&oids_to_c::filter_to_C });
19+
-}
20+
21+
int DER_w_algorithmIdentifier_DSA(WPACKET *pkt, int tag, DSA *dsa)
22+
{
23+
return DER_w_begin_sequence(pkt, tag)
24+
/* No parameters (yet?) */
25+
&& DER_w_precompiled(pkt, -1, der_oid_id_dsa, sizeof(der_oid_id_dsa))
26+
&& DER_w_end_sequence(pkt, tag);
27+
}
28+
29+
#define MD_CASE(name) \
30+
case NID_##name: \
31+
precompiled = der_oid_id_dsa_with_##name; \
32+
precompiled_sz = sizeof(der_oid_id_dsa_with_##name); \
33+
break;
34+
35+
int DER_w_algorithmIdentifier_DSA_with(WPACKET *pkt, int tag,
36+
DSA *dsa, int mdnid)
37+
{
38+
const unsigned char *precompiled = NULL;
39+
size_t precompiled_sz = 0;
40+
41+
switch (mdnid) {
42+
MD_CASE(sha1);
43+
MD_CASE(sha224);
44+
MD_CASE(sha256);
45+
MD_CASE(sha384);
46+
MD_CASE(sha512);
47+
MD_CASE(sha3_224);
48+
MD_CASE(sha3_256);
49+
MD_CASE(sha3_384);
50+
MD_CASE(sha3_512);
51+
default:
52+
return 0;
53+
}
54+
55+
return DER_w_begin_sequence(pkt, tag)
56+
/* No parameters (yet?) */
57+
&& DER_w_precompiled(pkt, -1, precompiled, precompiled_sz)
58+
&& DER_w_end_sequence(pkt, tag);
59+
}

providers/common/der/der_dsa.h.in

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. You can obtain a copy
6+
* in the file LICENSE in the source distribution or at
7+
* https://www.openssl.org/source/license.html
8+
*/
9+
10+
#include "internal/der.h"
11+
12+
/* Well known OIDs precompiled */
13+
{-
14+
$OUT = oids_to_c::process_leaves('providers/common/der/DSA.asn1',
15+
{ dir => $config{sourcedir},
16+
filter => \&oids_to_c::filter_to_H });
17+
-}
18+
19+
int DER_w_algorithmIdentifier_DSA(WPACKET *pkt, int tag, DSA *dsa);
20+
int DER_w_algorithmIdentifier_DSA_with(WPACKET *pkt, int tag,
21+
DSA *dsa, int mdnid);

providers/implementations/signature/build.info

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ SOURCE[../../libfips.a]=rsa.c
1818
SOURCE[../../libnonfips.a]=rsa.c
1919

2020
DEPEND[rsa.o]=../../common/include/prov/der_rsa.h
21+
DEPEND[dsa.o]=../../common/include/prov/der_dsa.h

providers/implementations/signature/dsa.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
#include <openssl/err.h>
2626
#include "internal/nelem.h"
2727
#include "internal/sizes.h"
28+
#include "internal/cryptlib.h"
2829
#include "prov/providercommonerr.h"
2930
#include "prov/implementations.h"
3031
#include "prov/providercommonerr.h"
3132
#include "prov/provider_ctx.h"
3233
#include "crypto/dsa.h"
34+
#include "prov/der_dsa.h"
3335

3436
static OSSL_OP_signature_newctx_fn dsa_newctx;
3537
static OSSL_OP_signature_sign_init_fn dsa_signature_init;
@@ -74,7 +76,8 @@ typedef struct {
7476
char mdname[OSSL_MAX_NAME_SIZE];
7577

7678
/* The Algorithm Identifier of the combined signature algorithm */
77-
unsigned char aid[OSSL_MAX_ALGORITHM_ID_SIZE];
79+
unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
80+
unsigned char *aid;
7881
size_t aid_len;
7982

8083
/* main digest */
@@ -146,25 +149,35 @@ static int dsa_setup_md(PROV_DSA_CTX *ctx,
146149
if (mdname != NULL) {
147150
EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
148151
int md_nid = dsa_get_md_nid(md);
149-
size_t algorithmidentifier_len = 0;
150-
const unsigned char *algorithmidentifier;
152+
WPACKET pkt;
151153

152-
EVP_MD_free(ctx->md);
153-
ctx->md = NULL;
154-
ctx->mdname[0] = '\0';
155-
156-
algorithmidentifier =
157-
dsa_algorithmidentifier_encoding(md_nid, &algorithmidentifier_len);
158-
159-
if (algorithmidentifier == NULL) {
154+
if (md == NULL || md_nid == NID_undef) {
160155
EVP_MD_free(md);
161156
return 0;
162157
}
163158

159+
EVP_MD_CTX_free(ctx->mdctx);
160+
EVP_MD_free(ctx->md);
161+
162+
/*
163+
* TODO(3.0) Should we care about DER writing errors?
164+
* All it really means is that for some reason, there's no
165+
* AlgorithmIdentifier to be had, but the operation itself is
166+
* still valid, just as long as it's not used to construct
167+
* anything that needs an AlgorithmIdentifier.
168+
*/
169+
ctx->aid_len = 0;
170+
if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
171+
&& DER_w_algorithmIdentifier_DSA_with(&pkt, -1, ctx->dsa, md_nid)
172+
&& WPACKET_finish(&pkt)) {
173+
WPACKET_get_total_written(&pkt, &ctx->aid_len);
174+
ctx->aid = WPACKET_get_curr(&pkt);
175+
}
176+
WPACKET_cleanup(&pkt);
177+
178+
ctx->mdctx = NULL;
164179
ctx->md = md;
165180
OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
166-
memcpy(ctx->aid, algorithmidentifier, algorithmidentifier_len);
167-
ctx->aid_len = algorithmidentifier_len;
168181
}
169182
return 1;
170183
}

0 commit comments

Comments
 (0)