Hi Eyal,
What you have is an incomplete CSR, which is missing the signature. When comparing the one you generated with one from openssl the only difference was the missing signature at the bottom:
openssl asn1parse -inform der -in mycsr.der -dump
...
248:d=1 hl=2 l= 9 cons: SEQUENCE
250:d=2 hl=2 l= 7 prim: OBJECT :ecdsa-with-SHA1
259:d=1 hl=2 l= 72 prim: BIT STRING
Here is a complete code example for creating a valid CSR.
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/asn_public.h>
#define MAX_TEMP_SIZE 1024
/* Build using:
gcc -lwolfssl -o makecsr makecsr.c
*/
int main(void)
{
ecc_key key;
WC_RNG rng;
Cert req;
byte der[MAX_TEMP_SIZE], pem[MAX_TEMP_SIZE];
int derSz, pemSz;
wc_ecc_init(&key);
wc_InitRng(&rng);
wc_ecc_make_key_ex(&rng, 32, &key, ECC_SECP256R1);
derSz = wc_EccKeyToDer(&key, der, sizeof(der));
memset(pem, 0, sizeof(pem));
pemSz = wc_DerToPem(der, derSz, pem, sizeof(pem), ECC_PRIVATEKEY_TYPE);
printf("%s", pem);
wc_InitCert(&req);
strncpy(req.subject.country, "US", CTC_NAME_SIZE);
strncpy(req.subject.state, "OR", CTC_NAME_SIZE);
strncpy(req.subject.locality, "Portland", CTC_NAME_SIZE);
strncpy(req.subject.org, "yaSSL", CTC_NAME_SIZE);
strncpy(req.subject.unit, "Development", CTC_NAME_SIZE);
strncpy(req.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
strncpy(req.subject.email, "info@wolfssl.com", CTC_NAME_SIZE);
derSz = wc_MakeCertReq(&req, der, sizeof(der), NULL, &key);
req.sigType = CTC_SHA256wECDSA;
derSz = wc_SignCert(req.bodySz, req.sigType, der, sizeof(der), NULL, &key, &rng);
pemSz = wc_DerToPem(der, derSz, pem, sizeof(pem), CERTREQ_TYPE);
printf("%s", pem);
wc_ecc_free(&key);
wc_FreeRng(&rng);
return 0;
}
gcc -lwolfssl -o makecsr makecsr.c
./makecsr > mycsr.csr
cat mycsr.csr
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIJEIx4rTDoZFi7lsCiOX7XyTXnXOvj4VPDoQDGeD4p8woAoGCCqGSM49
AwEHoUQDQgAEMZygsN1xZH/QRxMszDej8bL84by+XQ4sziXUT83HGkTUx9puoNeI
UxlctiaTT1IgwCg9GCpC52SXU/e/NtvoUw==
-----END EC PRIVATE KEY-----
-----BEGIN CERTIFICATE REQUEST-----
MIIBSTCB8QIBAjCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAk9SMREwDwYDVQQH
DAhQb3J0bGFuZDEOMAwGA1UECgwFeWFTU0wxFDASBgNVBAsMC0RldmVsb3BtZW50
MRgwFgYDVQQDDA93d3cud29sZnNzbC5jb20xHzAdBgkqhkiG9w0BCQEWEGluZm9A
d29sZnNzbC5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQxnKCw3XFkf9BH
EyzMN6PxsvzhvL5dDizOJdRPzccaRNTH2m6g14hTGVy2JpNPUiDAKD0YKkLnZJdT
97822+hToAAwCgYIKoZIzj0EAwIDRwAwRAIgMdviSP9zLvYaNm1hcCVqG8jOvJz2
T34DlP3XXKI3cZ4CIFsNEquqCPbIkKGC8pOW9fUcmf6sWlgrra6uedL+PaCF
-----END CERTIFICATE REQUEST-----
openssl req -in mycsr.csr -text -noout
Certificate Request:
Data:
Version: 2 (0x2)
Subject: C=US, ST=OR, L=Portland, O=yaSSL, OU=Development, CN=www.wolfssl.com/emailAddress=info@wolfssl.com
Subject Public Key Info:
Public Key Algorithm: id-ecPublicKey
EC Public Key:
pub:
04:31:9c:a0:b0:dd:71:64:7f:d0:47:13:2c:cc:37:
a3:f1:b2:fc:e1:bc:be:5d:0e:2c:ce:25:d4:4f:cd:
c7:1a:44:d4:c7:da:6e:a0:d7:88:53:19:5c:b6:26:
93:4f:52:20:c0:28:3d:18:2a:42:e7:64:97:53:f7:
bf:36:db:e8:53
ASN1 OID: prime256v1
Attributes:
a0:00
Signature Algorithm: ecdsa-with-SHA256
30:44:02:20:31:db:e2:48:ff:73:2e:f6:1a:36:6d:61:70:25:
6a:1b:c8:ce:bc:9c:f6:4f:7e:03:94:fd:d7:5c:a2:37:71:9e:
02:20:5b:0d:12:ab:aa:08:f6:c8:90:a1:82:f2:93:96:f5:f5:
1c:99:fe:ac:5a:58:2b:ad:ae:ae:79:d2:fe:3d:a0:85
Let me know if you have any questions.
Thanks,
David Garske, wolfSSL