[MBEDTLS] Update mbedtls to v2.1.1. By Ismael Ferreras Morezuelas with a fix by Roy...
[reactos.git] / reactos / dll / 3rdparty / mbedtls / x509write_csr.c
index 8f297a0..0b9a285 100644 (file)
@@ -1,23 +1,22 @@
 /*
  *  X.509 Certificate Signing Request writing
  *
- *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
+ *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
+ *  SPDX-License-Identifier: Apache-2.0
  *
- *  This file is part of mbed TLS (https://polarssl.org)
+ *  Licensed under the Apache License, Version 2.0 (the "License"); you may
+ *  not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ *  http://www.apache.org/licenses/LICENSE-2.0
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
  *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *  This file is part of mbed TLS (https://tls.mbed.org)
  */
 /*
  * References:
  * - attributes: PKCS#9 v2.0 aka RFC 2985
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
-#include "polarssl/config.h"
+#if !defined(MBEDTLS_CONFIG_FILE)
+#include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_X509_CSR_WRITE_C)
+#if defined(MBEDTLS_X509_CSR_WRITE_C)
 
-#include "polarssl/x509_csr.h"
-#include "polarssl/oid.h"
-#include "polarssl/asn1write.h"
-
-#if defined(POLARSSL_PEM_WRITE_C)
-#include "polarssl/pem.h"
-#endif
+#include "mbedtls/x509_csr.h"
+#include "mbedtls/oid.h"
+#include "mbedtls/asn1write.h"
 
 #include <string.h>
 #include <stdlib.h>
 
+#if defined(MBEDTLS_PEM_WRITE_C)
+#include "mbedtls/pem.h"
+#endif
+
 /* Implementation that should never be optimized out by the compiler */
-static void polarssl_zeroize( void *v, size_t n ) {
+static void mbedtls_zeroize( void *v, size_t n ) {
     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
 }
 
-void x509write_csr_init( x509write_csr *ctx )
+void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
 {
-    memset( ctx, 0, sizeof(x509write_csr) );
+    memset( ctx, 0, sizeof(mbedtls_x509write_csr) );
 }
 
-void x509write_csr_free( x509write_csr *ctx )
+void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
 {
-    asn1_free_named_data_list( &ctx->subject );
-    asn1_free_named_data_list( &ctx->extensions );
+    mbedtls_asn1_free_named_data_list( &ctx->subject );
+    mbedtls_asn1_free_named_data_list( &ctx->extensions );
 
-    polarssl_zeroize( ctx, sizeof(x509write_csr) );
+    mbedtls_zeroize( ctx, sizeof(mbedtls_x509write_csr) );
 }
 
-void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg )
+void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
 {
     ctx->md_alg = md_alg;
 }
 
-void x509write_csr_set_key( x509write_csr *ctx, pk_context *key )
+void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
 {
     ctx->key = key;
 }
 
-int x509write_csr_set_subject_name( x509write_csr *ctx,
+int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
                                     const char *subject_name )
 {
-    return x509_string_to_names( &ctx->subject, subject_name );
+    return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
 }
 
-int x509write_csr_set_extension( x509write_csr *ctx,
+int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
                                  const char *oid, size_t oid_len,
                                  const unsigned char *val, size_t val_len )
 {
-    return x509_set_extension( &ctx->extensions, oid, oid_len,
+    return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
                                0, val, val_len );
 }
 
-int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage )
+int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
 {
     unsigned char buf[4];
     unsigned char *c;
@@ -94,11 +93,11 @@ int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage )
 
     c = buf + 4;
 
-    if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
+    if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
         return( ret );
 
-    ret = x509write_csr_set_extension( ctx, OID_KEY_USAGE,
-                                       OID_SIZE( OID_KEY_USAGE ),
+    ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
+                                       MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
                                        buf, 4 );
     if( ret != 0 )
         return( ret );
@@ -106,7 +105,7 @@ int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage )
     return( 0 );
 }
 
-int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
+int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
                                     unsigned char ns_cert_type )
 {
     unsigned char buf[4];
@@ -115,11 +114,11 @@ int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
 
     c = buf + 4;
 
-    if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
+    if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
         return( ret );
 
-    ret = x509write_csr_set_extension( ctx, OID_NS_CERT_TYPE,
-                                       OID_SIZE( OID_NS_CERT_TYPE ),
+    ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
+                                       MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
                                        buf, 4 );
     if( ret != 0 )
         return( ret );
@@ -127,7 +126,7 @@ int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
     return( 0 );
 }
 
-int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
+int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
                        int (*f_rng)(void *, unsigned char *, size_t),
                        void *p_rng )
 {
@@ -136,42 +135,42 @@ int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
     size_t sig_oid_len = 0;
     unsigned char *c, *c2;
     unsigned char hash[64];
-    unsigned char sig[POLARSSL_MPI_MAX_SIZE];
+    unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
     unsigned char tmp_buf[2048];
     size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
     size_t len = 0;
-    pk_type_t pk_alg;
+    mbedtls_pk_type_t pk_alg;
 
     /*
      * Prepare data to be signed in tmp_buf
      */
     c = tmp_buf + sizeof( tmp_buf );
 
-    ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
 
     if( len )
     {
-        ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
-        ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
-                                                        ASN1_SEQUENCE ) );
+        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
+        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
+                                                        MBEDTLS_ASN1_SEQUENCE ) );
 
-        ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
-        ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
-                                                        ASN1_SET ) );
+        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
+        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
+                                                        MBEDTLS_ASN1_SET ) );
 
-        ASN1_CHK_ADD( len, asn1_write_oid( &c, tmp_buf, OID_PKCS9_CSR_EXT_REQ,
-                                          OID_SIZE( OID_PKCS9_CSR_EXT_REQ ) ) );
+        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
+                                          MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
 
-        ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
-        ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
-                                                        ASN1_SEQUENCE ) );
+        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
+        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
+                                                        MBEDTLS_ASN1_SEQUENCE ) );
     }
 
-    ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
-                                                    ASN1_CONTEXT_SPECIFIC ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
+                                                    MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
 
-    ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->key,
+    MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
                                                 tmp_buf, c - tmp_buf ) );
     c -= pub_len;
     len += pub_len;
@@ -179,29 +178,29 @@ int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
     /*
      *  Subject  ::=  Name
      */
-    ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
 
     /*
      *  Version  ::=  INTEGER  {  v1(0), v2(1), v3(2)  }
      */
-    ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
 
-    ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
-                                                    ASN1_SEQUENCE ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
+                                                    MBEDTLS_ASN1_SEQUENCE ) );
 
     /*
      * Prepare signature
      */
-    md( md_info_from_type( ctx->md_alg ), c, len, hash );
+    mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
 
-    pk_alg = pk_get_type( ctx->key );
-    if( pk_alg == POLARSSL_PK_ECKEY )
-        pk_alg = POLARSSL_PK_ECDSA;
+    pk_alg = mbedtls_pk_get_type( ctx->key );
+    if( pk_alg == MBEDTLS_PK_ECKEY )
+        pk_alg = MBEDTLS_PK_ECDSA;
 
-    if( ( ret = pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
+    if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
                          f_rng, p_rng ) ) != 0 ||
-        ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
+        ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
                                         &sig_oid, &sig_oid_len ) ) != 0 )
     {
         return( ret );
@@ -211,16 +210,16 @@ int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
      * Write data to output buffer
      */
     c2 = buf + size;
-    ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
+    MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
                                         sig_oid, sig_oid_len, sig, sig_len ) );
 
     c2 -= len;
     memcpy( c2, c, len );
 
     len += sig_and_oid_len;
-    ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED |
-                                                 ASN1_SEQUENCE ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
+                                                 MBEDTLS_ASN1_SEQUENCE ) );
 
     return( (int) len );
 }
@@ -228,8 +227,8 @@ int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
 #define PEM_BEGIN_CSR           "-----BEGIN CERTIFICATE REQUEST-----\n"
 #define PEM_END_CSR             "-----END CERTIFICATE REQUEST-----\n"
 
-#if defined(POLARSSL_PEM_WRITE_C)
-int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
+#if defined(MBEDTLS_PEM_WRITE_C)
+int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
                        int (*f_rng)(void *, unsigned char *, size_t),
                        void *p_rng )
 {
@@ -237,13 +236,13 @@ int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
     unsigned char output_buf[4096];
     size_t olen = 0;
 
-    if( ( ret = x509write_csr_der( ctx, output_buf, sizeof(output_buf),
+    if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
                                    f_rng, p_rng ) ) < 0 )
     {
         return( ret );
     }
 
-    if( ( ret = pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
+    if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
                                   output_buf + sizeof(output_buf) - ret,
                                   ret, buf, size, &olen ) ) != 0 )
     {
@@ -252,6 +251,6 @@ int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
 
     return( 0 );
 }
-#endif /* POLARSSL_PEM_WRITE_C */
+#endif /* MBEDTLS_PEM_WRITE_C */
 
-#endif /* POLARSSL_X509_CSR_WRITE_C */
+#endif /* MBEDTLS_X509_CSR_WRITE_C */