How can I sign a CSR with a private key inside SE

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

How can I sign a CSR with a private key inside SE

ソリューションへジャンプ
6,960件の閲覧回数
antonio_mendes
Contributor III

I have a CSR inside a X509_REQ. In OpenSSL API, the CSR signing function is X509_REQ_sign.

Do I have to use sss_asymmetric_sign_digest? What are the arguments for this function?

0 件の賞賛
返信
1 解決策
6,858件の閲覧回数
msjcard
Contributor III

I pawed around in my old code and found this snippet.  Note the flag twiddling on the part of the ASN1_BIT_STRING object.

 

bitsiglen = i2d_ECSIG (mysig, NULL);

bitsig = OPENSSL_malloc(bitsiglen);
bufferp = bitsig;
i2d_ECSIG (mysig, &bufferp);

signature = ASN1_BIT_STRING_new();

/* Some clean up necessary here. Basically, even though this is
encoded as a bit string, all bits count. Fix it so there are 0 bits
left over */
ASN1_BIT_STRING_set (signature, bitsig, bitsiglen);
signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT |0x07);
signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;

OPENSSL_free(bitsig);
ECSIG_free(mysig);

元の投稿で解決策を見る

0 件の賞賛
返信
14 返答(返信)
6,932件の閲覧回数
Kan_Li
NXP TechSupport
NXP TechSupport

Hello @antonio_mendes ,

 

We provide a self-signed certificate demo in the latest MW, please kindly refer to the following for details.

se050 self signed cert.png

 

Hope that helps,

Have a great day,
Kan


-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 件の賞賛
返信
6,927件の閲覧回数
antonio_mendes
Contributor III

Yeah... I look at it and wrote my code based on it... But when I use the openssl req -inform pem -in test1.csr -text -verify, I get an error

281473390864048:error:0D0C50DC:asn1 encoding routines:ASN1_item_verify:invalid bit string bits left:../openssl-1.1.1d/crypto/asn1/a_verify.c:103:

0 件の賞賛
返信
6,917件の閲覧回数
antonio_mendes
Contributor III

@Kan_Li 

Noticed that sometimes work, others don't work. The verify operation works successfully sometimes.

 

0 件の賞賛
返信
6,909件の閲覧回数
Kan_Li
NXP TechSupport
NXP TechSupport

Hi @antonio_mendes ,

 

This demo is supported on Windows PC or Linux based platforms/systems like iMX6, iMX8, Raspberry Pi, would you please specify which platform you play with this demo? Is it possible to share any log for running this demo on your side?

 

Thanks for your patience!

 

Have a great day,
Kan


-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 件の賞賛
返信
6,899件の閲覧回数
antonio_mendes
Contributor III

Hi @Kan_Li .

I'm using an evaluation board from Variscite (DART-MX8M-MINI) and it's using Yocto linux 5.4.3. OPENSSL version is 1.1.1d.

In the zip, there are 2 CSR files, as well as the associated logs. These CSR files were create based on the example that you provided.

I also provide the C code for private key generation and CSR.

Both CSR were created based on the same private key.

Thank you

0 件の賞賛
返信
6,888件の閲覧回数
antonio_mendes
Contributor III

Hi again @Kan_Li .

So, I'm using a evaluation board from Variscite (DART-MX8M-MINI) using Yocto Linux 5.4.3.

Openssl version - 1.1.1d.

My CSR code generation is in an attachment as well a good and bad CSR with the associated logs.

The private key is the same for both CSR and the code is an attachment too.

Thank you.

0 件の賞賛
返信
6,880件の閲覧回数
msjcard
Contributor III

 

The bad CSRs are (probably) happening whenever your signature length is == 70 but that may or may not be a good indicator given only two examples.   The "extraBits" field of the ASN1 BIT STRING that represents the signature is being set to a non-zero value and that's causing your problem.   I'm not sure if this is an openssl (ASN1_BIT_STRING_set) problem or something else happening.  I'm thinking that you want to zero the ASN1_BIT_STRING structure after you get it from the request and before you set the signature.

EC signatures vary in length based on whether or not the R and S components have the high bit of the high byte set (its not as simple as this, but this covers all but about 2% of the signatures).   The valid signatures properly have the extra bit field of the BIT STRING set to 0 because the bit length of the encoded signature is always a multiple of 8.  The invalid one has it set to 1 and the ASN1 parser barfs because the signature has more than 1 bit set in its left most byte.

Mike

ps - here's the ASN1 dump of the bad csr

0 261: SEQUENCE {
4 171: SEQUENCE {
7 1: INTEGER 0
10 73: SEQUENCE {
12 11: SET {
14 9: SEQUENCE {
16 3: OBJECT IDENTIFIER '2 5 4 6'
21 2: PrintableString 'PT'
: }
: }
25 16: SET {
27 14: SEQUENCE {
29 3: OBJECT IDENTIFIER '2 5 4 8'
34 7: UTF8String 'Setubal'
: }
: }
43 12: SET {
45 10: SEQUENCE {
47 3: OBJECT IDENTIFIER '2 5 4 10'
52 3: UTF8String 'EID'
: }
: }
57 12: SET {
59 10: SEQUENCE {
61 3: OBJECT IDENTIFIER '2 5 4 11'
66 3: UTF8String 'UCN'
: }
: }
71 12: SET {
73 10: SEQUENCE {
75 3: OBJECT IDENTIFIER '2 5 4 3'
80 3: UTF8String 'US1'
: }
: }
: }
85 89: SEQUENCE {
87 19: SEQUENCE {
89 7: OBJECT IDENTIFIER '1 2 840 10045 2 1'
98 8: OBJECT IDENTIFIER '1 2 840 10045 3 1 7'
: }
108 66: BIT STRING
: 04 2D 9E CA 7B 22 98 3E 23 CC CE 0C 4C F5 19 08
: F9 EA C9 56 24 55 AE B1 31 58 8B EA E8 F5 15 37
: 54 67 FB 09 91 7C AD 67 BB 9A 95 29 EC 60 2B CD
: C8 DA CA 5E 41 63 73 E4 DA 3E D4 00 EF 25 D2 D6
: BA
: }
176 0: [0]
: Error: Object has zero length.
: }
178 12: SEQUENCE {
180 8: OBJECT IDENTIFIER '1 2 840 10045 4 3 2'
190 0: NULL
: }
192 71: BIT STRING 1 unused bit, encapsulates {
195 68: SEQUENCE {
197 32: INTEGER
: 1C F6 0B 66 DE 11 EF 22 D8 0E 17 39 13 E3 3C 83
: B4 84 35 00 20 C6 F6 B6 E3 57 E8 58 88 77 AE 9E
231 32: INTEGER
: 66 C8 C7 73 13 67 C4 BD 1B 4B 46 12 C3 15 4B C8
: 89 BB 60 2C 61 F0 53 37 17 C0 A4 B6 7A 0E 76 4A
: }
: }
: }

0 件の賞賛
返信
6,865件の閲覧回数
antonio_mendes
Contributor III

Hi @msjcard

Thanks for your support.

I generated more CSR and some with length signature = 71 fails too... I'm going to share more examples.

When dumping bad_csr1.csr, I verify that its missing an 00 in the signature. The following error is diplayed:

281473519351472:error:0D07209B:asn1 encoding routines:ASN1_get_object:too long:../openssl-1.1.1d/crypto/asn1/asn1_lib.c:91:
281473519351472:error:0D068066:asn1 encoding routines:asn1_check_tlen:bad object header:../openssl-1.1.1d/crypto/asn1/tasn_dec.c:1118:
281473519351472:error:0D07803A:asn1 encoding routines:asn1_item_embed_d2i:nested asn1 error:../openssl-1.1.1d/crypto/asn1/tasn_dec.c:290:Type=ECDSA_SIG
281473519351472:error:0D0C5006:asn1 encoding routines:ASN1_item_verify:EVP lib:../openssl-1.1.1d/crypto/asn1/a_verify.c:170:

 

The others csr I check that extraBits field is being set to non-zero.

I tried to set ASN1 BIT STRING to zero with no success but maybe I'm doing it wrong.

X509_REQ_get0_signature((const X509_REQ *)csr_out, (const ASN1_BIT_STRING **)&sig, &tmp_algo);
memset(sig, 0, sizeof(ASN1_BIT_STRING));
X509_ALGOR *algo = (X509_ALGOR *)tmp_algo;
X509_ALGOR_set0(algo, OBJ_nid2obj(type), V_ASN1_NULL, NULL);
len = ASN1_BIT_STRING_set(sig, p_signature, signatureLen);

Thanks in advance

0 件の賞賛
返信
6,859件の閲覧回数
msjcard
Contributor III

I pawed around in my old code and found this snippet.  Note the flag twiddling on the part of the ASN1_BIT_STRING object.

 

bitsiglen = i2d_ECSIG (mysig, NULL);

bitsig = OPENSSL_malloc(bitsiglen);
bufferp = bitsig;
i2d_ECSIG (mysig, &bufferp);

signature = ASN1_BIT_STRING_new();

/* Some clean up necessary here. Basically, even though this is
encoded as a bit string, all bits count. Fix it so there are 0 bits
left over */
ASN1_BIT_STRING_set (signature, bitsig, bitsiglen);
signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT |0x07);
signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;

OPENSSL_free(bitsig);
ECSIG_free(mysig);

0 件の賞賛
返信
6,841件の閲覧回数
antonio_mendes
Contributor III

It worked! Thank you @msjcard .

 

@Kan_Li , maybe add this part to demo?

0 件の賞賛
返信
6,953件の閲覧回数
antonio_mendes
Contributor III

I came to this solution. Is it correct?

0 件の賞賛
返信
6,945件の閲覧回数
msjcard
Contributor III

Easiest way to verify your solution is to use openssl command line:

openssl req -inform der -in <fname> -text -verify

(Assumes you write the output to a file as binary DER - if you output PEM - base64, change the -inform to 'pem'

Mike

 

 

 

0 件の賞賛
返信
6,926件の閲覧回数
antonio_mendes
Contributor III

Thank you. I'm getting this error. File is written in PEM format.

81473390864048:error:0D0C50DC:asn1 encoding routines:ASN1_item_verify:invalid bit string bits left:../openssl-1.1.1d/crypto/asn1/a_verify.c:103:

 

0 件の賞賛
返信
6,919件の閲覧回数
antonio_mendes
Contributor III

Noticed that sometimes work, others don't work.

0 件の賞賛
返信