linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier
@ 2013-02-20  6:17 Chun-Yi Lee
  2013-02-20 12:49 ` David Howells
  0 siblings, 1 reply; 9+ messages in thread
From: Chun-Yi Lee @ 2013-02-20  6:17 UTC (permalink / raw)
  To: rusty, dhowells
  Cc: linux-kernel, Chun-Yi Lee, Josh Boyer, Randy Dunlap, Herbert Xu,
	David S. Miller

Per X.509 spec in 4.2.1.1 section, the structure of Authority Key
Identifier Extension is:

   AuthorityKeyIdentifier ::= SEQUENCE {
      keyIdentifier             [0] KeyIdentifier           OPTIONAL,
      authorityCertIssuer       [1] GeneralNames            OPTIONAL,
      authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }

   KeyIdentifier ::= OCTET STRING

When a certificate also provides
authorityCertIssuer and authorityCertSerialNumber then the length of
AuthorityKeyIdentifier SEQUENCE is likely to long form format.
e.g.
   The example certificate demos/tunala/A-server.pem in openssl source:

X509v3 Authority Key Identifier:
    keyid:49:FB:45:72:12:C4:CC:E1:45:A1:D3:08:9E:95:C4:2C:6D:55:3F:17
    DirName:/C=NZ/L=Wellington/O=Really Irresponsible Authorisation Authority (RIAA)/OU=Cert-stamping/CN=Jackov al-Trades/emailAddress=none@fake.domain
    serial:00

Current parsing rule of OID_authorityKeyIdentifier only take care the
short form format, it causes load certificate to modsign_keyring fail:

[   12.061147] X.509: Extension: 47
[   12.075121] MODSIGN: Problem loading in-kernel X.509 certificate (-74)

So, this patch add the parsing rule for support long form format against
Authority Key Identifier.

v2:
 - Removed comma from author's name.
 - Moved 'Short Form length' comment inside the if-body.
 - Changed the type of sub to size_t.
 - Use ASN1_INDEFINITE_LENGTH rather than writing 0x80 and 127.
 - Moved the key_len's value assignment before alter v.
 - Fixed the typo of octets.
 - Add 2 to v before entering the loop for calculate the length.
 - Removed the comment of check vlen.

Cc: David Howells <dhowells@redhat.com>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Josh Boyer <jwboyer@redhat.com>
Cc: Randy Dunlap <rdunlap@xenotime.net>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Chun-Yi Lee <jlee@suse.com>
---
 crypto/asymmetric_keys/x509_cert_parser.c |   55 ++++++++++++++++++++++++----
 1 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
index 7fabc4c..59ab6d2 100644
--- a/crypto/asymmetric_keys/x509_cert_parser.c
+++ b/crypto/asymmetric_keys/x509_cert_parser.c
@@ -373,6 +373,9 @@ int rsa_extract_mpi(void *context, size_t hdrlen,
 	return 0;
 }
 
+/* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
+#define SEQ_TAG_KEYID (ASN1_CONT << 6)
+
 /*
  * Process certificate extensions that are used to qualify the certificate.
  */
@@ -407,21 +410,57 @@ int x509_process_extension(void *context, size_t hdrlen,
 	}
 
 	if (ctx->last_oid == OID_authorityKeyIdentifier) {
+		size_t key_len;
+
 		/* Get hold of the CA key fingerprint */
 		if (vlen < 5)
 			return -EBADMSG;
-		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)) ||
-		    v[1] != vlen - 2 ||
-		    v[2] != (ASN1_CONT << 6) ||
-		    v[3] != vlen - 4)
+
+		/* Authority Key Identifier must be a Constructed SEQUENCE */
+		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)))
 			return -EBADMSG;
-		v += 4;
-		vlen -= 4;
 
-		f = kmalloc(vlen * 2 + 1, GFP_KERNEL);
+		/* Authority Key Identifier is not indefinite length */
+		if (unlikely(vlen == ASN1_INDEFINITE_LENGTH))
+			return -EBADMSG;
+
+		if (vlen < ASN1_INDEFINITE_LENGTH) {
+			/* Short Form length */
+			if (v[1] != vlen - 2 ||
+			    v[2] != SEQ_TAG_KEYID ||
+			    v[3] != vlen - 4)
+				return -EBADMSG;
+
+			key_len = v[3];
+			v += 4;
+		} else {
+			/* Long Form length */
+			size_t seq_len = 0;
+			size_t sub = v[1] - ASN1_INDEFINITE_LENGTH;
+
+			if (sub > 2)
+				return -EBADMSG;
+
+			/* calculate the length from subsequent octets */
+			v += 2;
+			for (i = 0; i < sub; i++) {
+				seq_len <<= 8;
+				seq_len |= v[i];
+			}
+
+			if (seq_len != vlen - 2 - sub ||
+			    v[sub] != SEQ_TAG_KEYID ||
+			    v[sub + 1] > vlen - 4 - sub)
+				return -EBADMSG;
+
+			key_len = v[sub + 1];
+			v += (sub + 2);
+		}
+
+		f = kmalloc(key_len * 2 + 1, GFP_KERNEL);
 		if (!f)
 			return -ENOMEM;
-		for (i = 0; i < vlen; i++)
+		for (i = 0; i < key_len; i++)
 			sprintf(f + i * 2, "%02x", v[i]);
 		pr_debug("authority   %s\n", f);
 		ctx->cert->authority = f;
-- 
1.6.4.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier
  2013-02-20  6:17 [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier Chun-Yi Lee
@ 2013-02-20 12:49 ` David Howells
  2013-02-21  3:33   ` joeyli
  0 siblings, 1 reply; 9+ messages in thread
From: David Howells @ 2013-02-20 12:49 UTC (permalink / raw)
  To: Chun-Yi Lee
  Cc: dhowells, rusty, linux-kernel, Chun-Yi Lee, Josh Boyer,
	Randy Dunlap, Herbert Xu, David S. Miller

Chun-Yi Lee <joeyli.kernel@gmail.com> wrote:

> Per X.509 spec in 4.2.1.1 section, the structure of Authority Key
> Identifier Extension is:
> 
>    AuthorityKeyIdentifier ::= SEQUENCE {
>       keyIdentifier             [0] KeyIdentifier           OPTIONAL,
>       authorityCertIssuer       [1] GeneralNames            OPTIONAL,
>       authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }
> 
>    KeyIdentifier ::= OCTET STRING
> 
> When a certificate also provides
> authorityCertIssuer and authorityCertSerialNumber then the length of
> AuthorityKeyIdentifier SEQUENCE is likely to long form format.
> e.g.
>    The example certificate demos/tunala/A-server.pem in openssl source:
> 
> X509v3 Authority Key Identifier:
>     keyid:49:FB:45:72:12:C4:CC:E1:45:A1:D3:08:9E:95:C4:2C:6D:55:3F:17
>     DirName:/C=NZ/L=Wellington/O=Really Irresponsible Authorisation Authority (RIAA)/OU=Cert-stamping/CN=Jackov al-Trades/emailAddress=none@fake.domain
>     serial:00
> 
> Current parsing rule of OID_authorityKeyIdentifier only take care the
> short form format, it causes load certificate to modsign_keyring fail:
> 
> [   12.061147] X.509: Extension: 47
> [   12.075121] MODSIGN: Problem loading in-kernel X.509 certificate (-74)
> 
> So, this patch add the parsing rule for support long form format against
> Authority Key Identifier.
> 
> v2:
>  - Removed comma from author's name.
>  - Moved 'Short Form length' comment inside the if-body.
>  - Changed the type of sub to size_t.
>  - Use ASN1_INDEFINITE_LENGTH rather than writing 0x80 and 127.
>  - Moved the key_len's value assignment before alter v.
>  - Fixed the typo of octets.
>  - Add 2 to v before entering the loop for calculate the length.
>  - Removed the comment of check vlen.
> 
> Cc: Rusty Russell <rusty@rustcorp.com.au>
> Cc: Josh Boyer <jwboyer@redhat.com>
> Cc: Randy Dunlap <rdunlap@xenotime.net>
> Cc: Herbert Xu <herbert@gondor.apana.org.au>
> Cc: "David S. Miller" <davem@davemloft.net>
> Signed-off-by: Chun-Yi Lee <jlee@suse.com>

Acked-by: David Howells <dhowells@redhat.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier
  2013-02-20 12:49 ` David Howells
@ 2013-02-21  3:33   ` joeyli
  2013-02-21  5:05     ` Rusty Russell
  0 siblings, 1 reply; 9+ messages in thread
From: joeyli @ 2013-02-21  3:33 UTC (permalink / raw)
  To: David Howells
  Cc: rusty, linux-kernel, Josh Boyer, Randy Dunlap, Herbert Xu,
	David S. Miller

於 三,2013-02-20 於 12:49 +0000,David Howells 提到:
> Chun-Yi Lee <joeyli.kernel@gmail.com> wrote:
> 
> > Per X.509 spec in 4.2.1.1 section, the structure of Authority Key
> > Identifier Extension is:
> > 
> >    AuthorityKeyIdentifier ::= SEQUENCE {
> >       keyIdentifier             [0] KeyIdentifier           OPTIONAL,
> >       authorityCertIssuer       [1] GeneralNames            OPTIONAL,
> >       authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }
> > 
> >    KeyIdentifier ::= OCTET STRING
> > 
> > When a certificate also provides
> > authorityCertIssuer and authorityCertSerialNumber then the length of
> > AuthorityKeyIdentifier SEQUENCE is likely to long form format.
> > e.g.
> >    The example certificate demos/tunala/A-server.pem in openssl source:
> > 
> > X509v3 Authority Key Identifier:
> >     keyid:49:FB:45:72:12:C4:CC:E1:45:A1:D3:08:9E:95:C4:2C:6D:55:3F:17
> >     DirName:/C=NZ/L=Wellington/O=Really Irresponsible Authorisation Authority (RIAA)/OU=Cert-stamping/CN=Jackov al-Trades/emailAddress=none@fake.domain
> >     serial:00
> > 
> > Current parsing rule of OID_authorityKeyIdentifier only take care the
> > short form format, it causes load certificate to modsign_keyring fail:
> > 
> > [   12.061147] X.509: Extension: 47
> > [   12.075121] MODSIGN: Problem loading in-kernel X.509 certificate (-74)
> > 
> > So, this patch add the parsing rule for support long form format against
> > Authority Key Identifier.
> > 
> > v2:
> >  - Removed comma from author's name.
> >  - Moved 'Short Form length' comment inside the if-body.
> >  - Changed the type of sub to size_t.
> >  - Use ASN1_INDEFINITE_LENGTH rather than writing 0x80 and 127.
> >  - Moved the key_len's value assignment before alter v.
> >  - Fixed the typo of octets.
> >  - Add 2 to v before entering the loop for calculate the length.
> >  - Removed the comment of check vlen.
> > 
> > Cc: Rusty Russell <rusty@rustcorp.com.au>
> > Cc: Josh Boyer <jwboyer@redhat.com>
> > Cc: Randy Dunlap <rdunlap@xenotime.net>
> > Cc: Herbert Xu <herbert@gondor.apana.org.au>
> > Cc: "David S. Miller" <davem@davemloft.net>
> > Signed-off-by: Chun-Yi Lee <jlee@suse.com>
> 
> Acked-by: David Howells <dhowells@redhat.com>
> 

Thanks for David's review and confirm.

Joey Lee



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier
  2013-02-21  3:33   ` joeyli
@ 2013-02-21  5:05     ` Rusty Russell
  2013-02-21 10:07       ` joeyli
  0 siblings, 1 reply; 9+ messages in thread
From: Rusty Russell @ 2013-02-21  5:05 UTC (permalink / raw)
  To: joeyli, David Howells
  Cc: linux-kernel, Josh Boyer, Randy Dunlap, Herbert Xu, David S. Miller

joeyli <jlee@suse.com> writes:
> 於 三,2013-02-20 於 12:49 +0000,David Howells 提到:
>> Acked-by: David Howells <dhowells@redhat.com>
>> 
>
> Thanks for David's review and confirm.

Should this be CC stable?

Thanks,
Rusty.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier
  2013-02-21  5:05     ` Rusty Russell
@ 2013-02-21 10:07       ` joeyli
  0 siblings, 0 replies; 9+ messages in thread
From: joeyli @ 2013-02-21 10:07 UTC (permalink / raw)
  To: Rusty Russell
  Cc: David Howells, linux-kernel, Josh Boyer, Randy Dunlap,
	Herbert Xu, David S. Miller

於 四,2013-02-21 於 15:35 +1030,Rusty Russell 提到:
> joeyli <jlee@suse.com> writes:
> > 於 三,2013-02-20 於 12:49 +0000,David Howells 提到:
> >> Acked-by: David Howells <dhowells@redhat.com>
> >> 
> >
> > Thanks for David's review and confirm.
> 
> Should this be CC stable?
> 
> Thanks,
> Rusty.
> 

IMHO this patch need Cc stable, because kernel got problem to load a
certificate that also provides authorityCertIssuer and
authorityCertSerialNumber.


Thanks a lot!
Joey Lee





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier
  2013-03-14  7:34 Lee, Chun-Yi
  2013-03-26  4:44 ` joeyli
@ 2013-03-29  8:51 ` joeyli
  1 sibling, 0 replies; 9+ messages in thread
From: joeyli @ 2013-03-29  8:51 UTC (permalink / raw)
  To: rusty, dhowells
  Cc: linux-kernel, Josh Boyer, Randy Dunlap, Herbert Xu, David S. Miller

於 四,2013-03-14 於 15:34 +0800,Lee, Chun-Yi 提到:
> From: Chun-Yi Lee <jlee@suse.com>
> 
> Per X.509 spec in 4.2.1.1 section, the structure of Authority Key
> Identifier Extension is:
> 
>    AuthorityKeyIdentifier ::= SEQUENCE {
>       keyIdentifier             [0] KeyIdentifier           OPTIONAL,
>       authorityCertIssuer       [1] GeneralNames            OPTIONAL,
>       authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }
> 
>    KeyIdentifier ::= OCTET STRING
> 
> When a certificate also provides
> authorityCertIssuer and authorityCertSerialNumber then the length of
> AuthorityKeyIdentifier SEQUENCE is likely to long form format.
> e.g.
>    The example certificate demos/tunala/A-server.pem in openssl source:
> 
> X509v3 Authority Key Identifier:
>     keyid:49:FB:45:72:12:C4:CC:E1:45:A1:D3:08:9E:95:C4:2C:6D:55:3F:17
>     DirName:/C=NZ/L=Wellington/O=Really Irresponsible Authorisation Authority (RIAA)/OU=Cert-stamping/CN=Jackov al-Trades/emailAddress=none@fake.domain
>     serial:00
> 
> Current parsing rule of OID_authorityKeyIdentifier only take care the
> short form format, it causes load certificate to modsign_keyring fail:
> 
> [   12.061147] X.509: Extension: 47
> [   12.075121] MODSIGN: Problem loading in-kernel X.509 certificate (-74)
> 
> So, this patch add the parsing rule for support long form format against
> Authority Key Identifier.
> 
> v2:
>  - Removed comma from author's name.
>  - Moved 'Short Form length' comment inside the if-body.
>  - Changed the type of sub to size_t.
>  - Use ASN1_INDEFINITE_LENGTH rather than writing 0x80 and 127.
>  - Moved the key_len's value assignment before alter v.
>  - Fixed the typo of octets.
>  - Add 2 to v before entering the loop for calculate the length.
>  - Removed the comment of check vlen.
> 
> Cc: Rusty Russell <rusty@rustcorp.com.au>
> Cc: Josh Boyer <jwboyer@redhat.com>
> Cc: Randy Dunlap <rdunlap@xenotime.net>
> Cc: Herbert Xu <herbert@gondor.apana.org.au>
> Cc: "David S. Miller" <davem@davemloft.net>
> Acked-by: David Howells <dhowells@redhat.com>
> Signed-off-by: Chun-Yi Lee <jlee@suse.com>
> ---
>  crypto/asymmetric_keys/x509_cert_parser.c |   55 ++++++++++++++++++++++++----
>  1 files changed, 47 insertions(+), 8 deletions(-)
> 
> diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
> index 7fabc4c..59ab6d2 100644
> --- a/crypto/asymmetric_keys/x509_cert_parser.c
> +++ b/crypto/asymmetric_keys/x509_cert_parser.c
> @@ -373,6 +373,9 @@ int rsa_extract_mpi(void *context, size_t hdrlen,
>  	return 0;
>  }
>  
> +/* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
> +#define SEQ_TAG_KEYID (ASN1_CONT << 6)
> +
>  /*
>   * Process certificate extensions that are used to qualify the certificate.
>   */
> @@ -407,21 +410,57 @@ int x509_process_extension(void *context, size_t hdrlen,
>  	}
>  
>  	if (ctx->last_oid == OID_authorityKeyIdentifier) {
> +		size_t key_len;
> +
>  		/* Get hold of the CA key fingerprint */
>  		if (vlen < 5)
>  			return -EBADMSG;
> -		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)) ||
> -		    v[1] != vlen - 2 ||
> -		    v[2] != (ASN1_CONT << 6) ||
> -		    v[3] != vlen - 4)
> +
> +		/* Authority Key Identifier must be a Constructed SEQUENCE */
> +		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)))
>  			return -EBADMSG;
> -		v += 4;
> -		vlen -= 4;
>  
> -		f = kmalloc(vlen * 2 + 1, GFP_KERNEL);
> +		/* Authority Key Identifier is not indefinite length */
> +		if (unlikely(vlen == ASN1_INDEFINITE_LENGTH))
> +			return -EBADMSG;
> +
> +		if (vlen < ASN1_INDEFINITE_LENGTH) {
> +			/* Short Form length */
> +			if (v[1] != vlen - 2 ||
> +			    v[2] != SEQ_TAG_KEYID ||
> +			    v[3] != vlen - 4)

In "Short Form length" case, there still possible have
authorityCertIssuer and authorityCertSerialNumber
in AuthorityKeyIdentifier sequence. In the case, the v[3], length of
keyIdentifier doesn't equal to "vlen - 4".

So, we should change the size check to "v[3] > vlen - 4" for cover
this situation.

I will send out the version 3 patch.


Thanks a lot!
Joey Lee

> +				return -EBADMSG;
> +
> +			key_len = v[3];
> +			v += 4;
> +		} else {
> +			/* Long Form length */
> +			size_t seq_len = 0;
> +			size_t sub = v[1] - ASN1_INDEFINITE_LENGTH;
> +
> +			if (sub > 2)
> +				return -EBADMSG;
> +
> +			/* calculate the length from subsequent octets */
> +			v += 2;
> +			for (i = 0; i < sub; i++) {
> +				seq_len <<= 8;
> +				seq_len |= v[i];
> +			}
> +
> +			if (seq_len != vlen - 2 - sub ||
> +			    v[sub] != SEQ_TAG_KEYID ||
> +			    v[sub + 1] > vlen - 4 - sub)
> +				return -EBADMSG;
> +
> +			key_len = v[sub + 1];
> +			v += (sub + 2);
> +		}
> +
> +		f = kmalloc(key_len * 2 + 1, GFP_KERNEL);
>  		if (!f)
>  			return -ENOMEM;
> -		for (i = 0; i < vlen; i++)
> +		for (i = 0; i < key_len; i++)
>  			sprintf(f + i * 2, "%02x", v[i]);
>  		pr_debug("authority   %s\n", f);
>  		ctx->cert->authority = f;



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier
  2013-03-14  7:34 Lee, Chun-Yi
@ 2013-03-26  4:44 ` joeyli
  2013-03-29  8:51 ` joeyli
  1 sibling, 0 replies; 9+ messages in thread
From: joeyli @ 2013-03-26  4:44 UTC (permalink / raw)
  To: rusty, dhowells, linux-kernel, Randy Dunlap, Josh Boyer,
	David S. Miller, Herbert Xu

Hi experts, 

I didn't this patch merged to any git tree or maybe I missed.
Where is the right place for send X.509 patch?


Thanks a lot!
Joey Lee

於 四,2013-03-14 於 15:34 +0800,Lee, Chun-Yi 提到:
> From: Chun-Yi Lee <jlee@suse.com>
> 
> Per X.509 spec in 4.2.1.1 section, the structure of Authority Key
> Identifier Extension is:
> 
>    AuthorityKeyIdentifier ::= SEQUENCE {
>       keyIdentifier             [0] KeyIdentifier           OPTIONAL,
>       authorityCertIssuer       [1] GeneralNames            OPTIONAL,
>       authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }
> 
>    KeyIdentifier ::= OCTET STRING
> 
> When a certificate also provides
> authorityCertIssuer and authorityCertSerialNumber then the length of
> AuthorityKeyIdentifier SEQUENCE is likely to long form format.
> e.g.
>    The example certificate demos/tunala/A-server.pem in openssl source:
> 
> X509v3 Authority Key Identifier:
>     keyid:49:FB:45:72:12:C4:CC:E1:45:A1:D3:08:9E:95:C4:2C:6D:55:3F:17
>     DirName:/C=NZ/L=Wellington/O=Really Irresponsible Authorisation Authority (RIAA)/OU=Cert-stamping/CN=Jackov al-Trades/emailAddress=none@fake.domain
>     serial:00
> 
> Current parsing rule of OID_authorityKeyIdentifier only take care the
> short form format, it causes load certificate to modsign_keyring fail:
> 
> [   12.061147] X.509: Extension: 47
> [   12.075121] MODSIGN: Problem loading in-kernel X.509 certificate (-74)
> 
> So, this patch add the parsing rule for support long form format against
> Authority Key Identifier.
> 
> v2:
>  - Removed comma from author's name.
>  - Moved 'Short Form length' comment inside the if-body.
>  - Changed the type of sub to size_t.
>  - Use ASN1_INDEFINITE_LENGTH rather than writing 0x80 and 127.
>  - Moved the key_len's value assignment before alter v.
>  - Fixed the typo of octets.
>  - Add 2 to v before entering the loop for calculate the length.
>  - Removed the comment of check vlen.
> 
> Cc: Rusty Russell <rusty@rustcorp.com.au>
> Cc: Josh Boyer <jwboyer@redhat.com>
> Cc: Randy Dunlap <rdunlap@xenotime.net>
> Cc: Herbert Xu <herbert@gondor.apana.org.au>
> Cc: "David S. Miller" <davem@davemloft.net>
> Acked-by: David Howells <dhowells@redhat.com>
> Signed-off-by: Chun-Yi Lee <jlee@suse.com>
> ---
>  crypto/asymmetric_keys/x509_cert_parser.c |   55 ++++++++++++++++++++++++----
>  1 files changed, 47 insertions(+), 8 deletions(-)
> 
> diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
> index 7fabc4c..59ab6d2 100644
> --- a/crypto/asymmetric_keys/x509_cert_parser.c
> +++ b/crypto/asymmetric_keys/x509_cert_parser.c
> @@ -373,6 +373,9 @@ int rsa_extract_mpi(void *context, size_t hdrlen,
>  	return 0;
>  }
>  
> +/* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
> +#define SEQ_TAG_KEYID (ASN1_CONT << 6)
> +
>  /*
>   * Process certificate extensions that are used to qualify the certificate.
>   */
> @@ -407,21 +410,57 @@ int x509_process_extension(void *context, size_t hdrlen,
>  	}
>  
>  	if (ctx->last_oid == OID_authorityKeyIdentifier) {
> +		size_t key_len;
> +
>  		/* Get hold of the CA key fingerprint */
>  		if (vlen < 5)
>  			return -EBADMSG;
> -		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)) ||
> -		    v[1] != vlen - 2 ||
> -		    v[2] != (ASN1_CONT << 6) ||
> -		    v[3] != vlen - 4)
> +
> +		/* Authority Key Identifier must be a Constructed SEQUENCE */
> +		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)))
>  			return -EBADMSG;
> -		v += 4;
> -		vlen -= 4;
>  
> -		f = kmalloc(vlen * 2 + 1, GFP_KERNEL);
> +		/* Authority Key Identifier is not indefinite length */
> +		if (unlikely(vlen == ASN1_INDEFINITE_LENGTH))
> +			return -EBADMSG;
> +
> +		if (vlen < ASN1_INDEFINITE_LENGTH) {
> +			/* Short Form length */
> +			if (v[1] != vlen - 2 ||
> +			    v[2] != SEQ_TAG_KEYID ||
> +			    v[3] != vlen - 4)
> +				return -EBADMSG;
> +
> +			key_len = v[3];
> +			v += 4;
> +		} else {
> +			/* Long Form length */
> +			size_t seq_len = 0;
> +			size_t sub = v[1] - ASN1_INDEFINITE_LENGTH;
> +
> +			if (sub > 2)
> +				return -EBADMSG;
> +
> +			/* calculate the length from subsequent octets */
> +			v += 2;
> +			for (i = 0; i < sub; i++) {
> +				seq_len <<= 8;
> +				seq_len |= v[i];
> +			}
> +
> +			if (seq_len != vlen - 2 - sub ||
> +			    v[sub] != SEQ_TAG_KEYID ||
> +			    v[sub + 1] > vlen - 4 - sub)
> +				return -EBADMSG;
> +
> +			key_len = v[sub + 1];
> +			v += (sub + 2);
> +		}
> +
> +		f = kmalloc(key_len * 2 + 1, GFP_KERNEL);
>  		if (!f)
>  			return -ENOMEM;
> -		for (i = 0; i < vlen; i++)
> +		for (i = 0; i < key_len; i++)
>  			sprintf(f + i * 2, "%02x", v[i]);
>  		pr_debug("authority   %s\n", f);
>  		ctx->cert->authority = f;



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier
@ 2013-03-14  7:34 Lee, Chun-Yi
  2013-03-26  4:44 ` joeyli
  2013-03-29  8:51 ` joeyli
  0 siblings, 2 replies; 9+ messages in thread
From: Lee, Chun-Yi @ 2013-03-14  7:34 UTC (permalink / raw)
  To: rusty, dhowells
  Cc: linux-kernel, Chun-Yi Lee, Josh Boyer, Randy Dunlap, Herbert Xu,
	David S. Miller

From: Chun-Yi Lee <jlee@suse.com>

Per X.509 spec in 4.2.1.1 section, the structure of Authority Key
Identifier Extension is:

   AuthorityKeyIdentifier ::= SEQUENCE {
      keyIdentifier             [0] KeyIdentifier           OPTIONAL,
      authorityCertIssuer       [1] GeneralNames            OPTIONAL,
      authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }

   KeyIdentifier ::= OCTET STRING

When a certificate also provides
authorityCertIssuer and authorityCertSerialNumber then the length of
AuthorityKeyIdentifier SEQUENCE is likely to long form format.
e.g.
   The example certificate demos/tunala/A-server.pem in openssl source:

X509v3 Authority Key Identifier:
    keyid:49:FB:45:72:12:C4:CC:E1:45:A1:D3:08:9E:95:C4:2C:6D:55:3F:17
    DirName:/C=NZ/L=Wellington/O=Really Irresponsible Authorisation Authority (RIAA)/OU=Cert-stamping/CN=Jackov al-Trades/emailAddress=none@fake.domain
    serial:00

Current parsing rule of OID_authorityKeyIdentifier only take care the
short form format, it causes load certificate to modsign_keyring fail:

[   12.061147] X.509: Extension: 47
[   12.075121] MODSIGN: Problem loading in-kernel X.509 certificate (-74)

So, this patch add the parsing rule for support long form format against
Authority Key Identifier.

v2:
 - Removed comma from author's name.
 - Moved 'Short Form length' comment inside the if-body.
 - Changed the type of sub to size_t.
 - Use ASN1_INDEFINITE_LENGTH rather than writing 0x80 and 127.
 - Moved the key_len's value assignment before alter v.
 - Fixed the typo of octets.
 - Add 2 to v before entering the loop for calculate the length.
 - Removed the comment of check vlen.

Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Josh Boyer <jwboyer@redhat.com>
Cc: Randy Dunlap <rdunlap@xenotime.net>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: "David S. Miller" <davem@davemloft.net>
Acked-by: David Howells <dhowells@redhat.com>
Signed-off-by: Chun-Yi Lee <jlee@suse.com>
---
 crypto/asymmetric_keys/x509_cert_parser.c |   55 ++++++++++++++++++++++++----
 1 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
index 7fabc4c..59ab6d2 100644
--- a/crypto/asymmetric_keys/x509_cert_parser.c
+++ b/crypto/asymmetric_keys/x509_cert_parser.c
@@ -373,6 +373,9 @@ int rsa_extract_mpi(void *context, size_t hdrlen,
 	return 0;
 }
 
+/* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
+#define SEQ_TAG_KEYID (ASN1_CONT << 6)
+
 /*
  * Process certificate extensions that are used to qualify the certificate.
  */
@@ -407,21 +410,57 @@ int x509_process_extension(void *context, size_t hdrlen,
 	}
 
 	if (ctx->last_oid == OID_authorityKeyIdentifier) {
+		size_t key_len;
+
 		/* Get hold of the CA key fingerprint */
 		if (vlen < 5)
 			return -EBADMSG;
-		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)) ||
-		    v[1] != vlen - 2 ||
-		    v[2] != (ASN1_CONT << 6) ||
-		    v[3] != vlen - 4)
+
+		/* Authority Key Identifier must be a Constructed SEQUENCE */
+		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)))
 			return -EBADMSG;
-		v += 4;
-		vlen -= 4;
 
-		f = kmalloc(vlen * 2 + 1, GFP_KERNEL);
+		/* Authority Key Identifier is not indefinite length */
+		if (unlikely(vlen == ASN1_INDEFINITE_LENGTH))
+			return -EBADMSG;
+
+		if (vlen < ASN1_INDEFINITE_LENGTH) {
+			/* Short Form length */
+			if (v[1] != vlen - 2 ||
+			    v[2] != SEQ_TAG_KEYID ||
+			    v[3] != vlen - 4)
+				return -EBADMSG;
+
+			key_len = v[3];
+			v += 4;
+		} else {
+			/* Long Form length */
+			size_t seq_len = 0;
+			size_t sub = v[1] - ASN1_INDEFINITE_LENGTH;
+
+			if (sub > 2)
+				return -EBADMSG;
+
+			/* calculate the length from subsequent octets */
+			v += 2;
+			for (i = 0; i < sub; i++) {
+				seq_len <<= 8;
+				seq_len |= v[i];
+			}
+
+			if (seq_len != vlen - 2 - sub ||
+			    v[sub] != SEQ_TAG_KEYID ||
+			    v[sub + 1] > vlen - 4 - sub)
+				return -EBADMSG;
+
+			key_len = v[sub + 1];
+			v += (sub + 2);
+		}
+
+		f = kmalloc(key_len * 2 + 1, GFP_KERNEL);
 		if (!f)
 			return -ENOMEM;
-		for (i = 0; i < vlen; i++)
+		for (i = 0; i < key_len; i++)
 			sprintf(f + i * 2, "%02x", v[i]);
 		pr_debug("authority   %s\n", f);
 		ctx->cert->authority = f;
-- 
1.6.4.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier
@ 2013-02-14 13:29 Lee, Chun-Yi
  0 siblings, 0 replies; 9+ messages in thread
From: Lee, Chun-Yi @ 2013-02-14 13:29 UTC (permalink / raw)
  To: rusty, dhowells
  Cc: linux-kernel, Chun-Yi Lee, Josh Boyer, Randy Dunlap, Herbert Xu,
	David S. Miller

From: Chun-Yi Lee <jlee@suse.com>

Per X.509 spec in 4.2.1.1 section, the structure of Authority Key
Identifier Extension is:

   AuthorityKeyIdentifier ::= SEQUENCE {
      keyIdentifier             [0] KeyIdentifier           OPTIONAL,
      authorityCertIssuer       [1] GeneralNames            OPTIONAL,
      authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL  }

   KeyIdentifier ::= OCTET STRING

When a certificate also provides
authorityCertIssuer and authorityCertSerialNumber then the length of
AuthorityKeyIdentifier SEQUENCE is likely to long form format.
e.g.
   The example certificate demos/tunala/A-server.pem in openssl source:

X509v3 Authority Key Identifier:
    keyid:49:FB:45:72:12:C4:CC:E1:45:A1:D3:08:9E:95:C4:2C:6D:55:3F:17
    DirName:/C=NZ/L=Wellington/O=Really Irresponsible Authorisation Authority (RIAA)/OU=Cert-stamping/CN=Jackov al-Trades/emailAddress=none@fake.domain
    serial:00

Current parsing rule of OID_authorityKeyIdentifier only take care the
short form format, it causes load certificate to modsign_keyring fail:

[   12.061147] X.509: Extension: 47
[   12.075121] MODSIGN: Problem loading in-kernel X.509 certificate (-74)

So, this patch add the parsing rule for support long form format against
Authority Key Identifier.

v2:
 - Removed comma from author's name.
 - Moved 'Short Form length' comment inside the if-body.
 - Changed the type of sub to size_t.
 - Use ASN1_INDEFINITE_LENGTH rather than writing 0x80 and 127.
 - Moved the key_len's value assignment before alter v.
 - Fixed the typo of octets.
 - Add 2 to v before entering the loop for calculate the length.
 - Removed the comment of check vlen.

Cc: David Howells <dhowells@redhat.com>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Josh Boyer <jwboyer@redhat.com>
Cc: Randy Dunlap <rdunlap@xenotime.net>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Chun-Yi Lee <jlee@suse.com>
---
 crypto/asymmetric_keys/x509_cert_parser.c |   55 ++++++++++++++++++++++++----
 1 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
index 7fabc4c..59ab6d2 100644
--- a/crypto/asymmetric_keys/x509_cert_parser.c
+++ b/crypto/asymmetric_keys/x509_cert_parser.c
@@ -373,6 +373,9 @@ int rsa_extract_mpi(void *context, size_t hdrlen,
 	return 0;
 }
 
+/* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
+#define SEQ_TAG_KEYID (ASN1_CONT << 6)
+
 /*
  * Process certificate extensions that are used to qualify the certificate.
  */
@@ -407,21 +410,57 @@ int x509_process_extension(void *context, size_t hdrlen,
 	}
 
 	if (ctx->last_oid == OID_authorityKeyIdentifier) {
+		size_t key_len;
+
 		/* Get hold of the CA key fingerprint */
 		if (vlen < 5)
 			return -EBADMSG;
-		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)) ||
-		    v[1] != vlen - 2 ||
-		    v[2] != (ASN1_CONT << 6) ||
-		    v[3] != vlen - 4)
+
+		/* Authority Key Identifier must be a Constructed SEQUENCE */
+		if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)))
 			return -EBADMSG;
-		v += 4;
-		vlen -= 4;
 
-		f = kmalloc(vlen * 2 + 1, GFP_KERNEL);
+		/* Authority Key Identifier is not indefinite length */
+		if (unlikely(vlen == ASN1_INDEFINITE_LENGTH))
+			return -EBADMSG;
+
+		if (vlen < ASN1_INDEFINITE_LENGTH) {
+			/* Short Form length */
+			if (v[1] != vlen - 2 ||
+			    v[2] != SEQ_TAG_KEYID ||
+			    v[3] != vlen - 4)
+				return -EBADMSG;
+
+			key_len = v[3];
+			v += 4;
+		} else {
+			/* Long Form length */
+			size_t seq_len = 0;
+			size_t sub = v[1] - ASN1_INDEFINITE_LENGTH;
+
+			if (sub > 2)
+				return -EBADMSG;
+
+			/* calculate the length from subsequent octets */
+			v += 2;
+			for (i = 0; i < sub; i++) {
+				seq_len <<= 8;
+				seq_len |= v[i];
+			}
+
+			if (seq_len != vlen - 2 - sub ||
+			    v[sub] != SEQ_TAG_KEYID ||
+			    v[sub + 1] > vlen - 4 - sub)
+				return -EBADMSG;
+
+			key_len = v[sub + 1];
+			v += (sub + 2);
+		}
+
+		f = kmalloc(key_len * 2 + 1, GFP_KERNEL);
 		if (!f)
 			return -ENOMEM;
-		for (i = 0; i < vlen; i++)
+		for (i = 0; i < key_len; i++)
 			sprintf(f + i * 2, "%02x", v[i]);
 		pr_debug("authority   %s\n", f);
 		ctx->cert->authority = f;
-- 
1.6.4.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2013-03-29  8:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-20  6:17 [PATCH v2] X.509: Support parse long form of length octets in Authority Key Identifier Chun-Yi Lee
2013-02-20 12:49 ` David Howells
2013-02-21  3:33   ` joeyli
2013-02-21  5:05     ` Rusty Russell
2013-02-21 10:07       ` joeyli
  -- strict thread matches above, loose matches on Subject: below --
2013-03-14  7:34 Lee, Chun-Yi
2013-03-26  4:44 ` joeyli
2013-03-29  8:51 ` joeyli
2013-02-14 13:29 Lee, Chun-Yi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).