diff options
| author | jsing <> | 2014-05-30 04:59:14 +0000 |
|---|---|---|
| committer | jsing <> | 2014-05-30 04:59:14 +0000 |
| commit | 3ed71ea57d886db3e61ad9358f22da39f61702ec (patch) | |
| tree | 83a3775428dad8776efbb7f8d6e494f3483987bd | |
| parent | cf5dbc23fb9a5ab9a6e1cea0260d75c0f010ca55 (diff) | |
| download | openbsd-3ed71ea57d886db3e61ad9358f22da39f61702ec.tar.gz openbsd-3ed71ea57d886db3e61ad9358f22da39f61702ec.tar.bz2 openbsd-3ed71ea57d886db3e61ad9358f22da39f61702ec.zip | |
Rework parse_name() so that variable declaration is separate from function
based initialisation, use more readable variable names and use a goto
rather than duplicating the frees for the error and non-error paths...
ok beck@
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libssl/src/apps/apps.c | 87 |
1 files changed, 50 insertions, 37 deletions
diff --git a/src/lib/libssl/src/apps/apps.c b/src/lib/libssl/src/apps/apps.c index 9deefd3737..e5e3b150b5 100644 --- a/src/lib/libssl/src/apps/apps.c +++ b/src/lib/libssl/src/apps/apps.c | |||
| @@ -1831,45 +1831,57 @@ parse_yesno(const char *str, int def) | |||
| 1831 | X509_NAME * | 1831 | X509_NAME * |
| 1832 | parse_name(char *subject, long chtype, int multirdn) | 1832 | parse_name(char *subject, long chtype, int multirdn) |
| 1833 | { | 1833 | { |
| 1834 | size_t buflen = strlen(subject) + 1; /* to copy the types and | 1834 | X509_NAME *name = NULL; |
| 1835 | * values into. due to | 1835 | size_t buflen, max_ne; |
| 1836 | * escaping, the copy can | 1836 | char **ne_types, **ne_values; |
| 1837 | * only become shorter */ | 1837 | char *buf, *bp, *sp; |
| 1838 | char *buf = malloc(buflen); | 1838 | int i, nid, ne_num = 0; |
| 1839 | size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */ | 1839 | int *mval; |
| 1840 | char **ne_types = reallocarray(NULL, max_ne, sizeof(char *)); | 1840 | |
| 1841 | char **ne_values = reallocarray(NULL, max_ne, sizeof(char *)); | 1841 | /* |
| 1842 | int *mval = reallocarray(NULL, max_ne, sizeof(int)); | 1842 | * Buffer to copy the types and values into. Due to escaping the |
| 1843 | 1843 | * copy can only become shorter. | |
| 1844 | char *sp = subject, *bp = buf; | 1844 | */ |
| 1845 | int i, ne_num = 0; | 1845 | buflen = strlen(subject) + 1; |
| 1846 | 1846 | buf = malloc(buflen); | |
| 1847 | X509_NAME *n = NULL; | 1847 | |
| 1848 | int nid; | 1848 | /* Maximum number of name elements. */ |
| 1849 | 1849 | max_ne = buflen / 2 + 1; | |
| 1850 | if (!buf || !ne_types || !ne_values || !mval) { | 1850 | ne_types = reallocarray(NULL, max_ne, sizeof(char *)); |
| 1851 | ne_values = reallocarray(NULL, max_ne, sizeof(char *)); | ||
| 1852 | mval = reallocarray(NULL, max_ne, sizeof(int)); | ||
| 1853 | |||
| 1854 | if (buf == NULL || ne_types == NULL || ne_values == NULL || | ||
| 1855 | mval == NULL) { | ||
| 1851 | BIO_printf(bio_err, "malloc error\n"); | 1856 | BIO_printf(bio_err, "malloc error\n"); |
| 1852 | goto error; | 1857 | goto error; |
| 1853 | } | 1858 | } |
| 1859 | |||
| 1860 | bp = buf; | ||
| 1861 | sp = subject; | ||
| 1862 | |||
| 1854 | if (*subject != '/') { | 1863 | if (*subject != '/') { |
| 1855 | BIO_printf(bio_err, "Subject does not start with '/'.\n"); | 1864 | BIO_printf(bio_err, "Subject does not start with '/'.\n"); |
| 1856 | goto error; | 1865 | goto error; |
| 1857 | } | 1866 | } |
| 1858 | sp++; /* skip leading / */ | ||
| 1859 | 1867 | ||
| 1860 | /* no multivalued RDN by default */ | 1868 | /* Skip leading '/'. */ |
| 1869 | sp++; | ||
| 1870 | |||
| 1871 | /* No multivalued RDN by default. */ | ||
| 1861 | mval[ne_num] = 0; | 1872 | mval[ne_num] = 0; |
| 1862 | 1873 | ||
| 1863 | while (*sp) { | 1874 | while (*sp) { |
| 1864 | /* collect type */ | 1875 | /* Collect type. */ |
| 1865 | ne_types[ne_num] = bp; | 1876 | ne_types[ne_num] = bp; |
| 1866 | while (*sp) { | 1877 | while (*sp) { |
| 1867 | if (*sp == '\\') { /* is there anything to | 1878 | /* is there anything to escape in the type...? */ |
| 1868 | * escape in the type...? */ | 1879 | if (*sp == '\\') { |
| 1869 | if (*++sp) | 1880 | if (*++sp) |
| 1870 | *bp++ = *sp++; | 1881 | *bp++ = *sp++; |
| 1871 | else { | 1882 | else { |
| 1872 | BIO_printf(bio_err, "escape character at end of string\n"); | 1883 | BIO_printf(bio_err, "escape character " |
| 1884 | "at end of string\n"); | ||
| 1873 | goto error; | 1885 | goto error; |
| 1874 | } | 1886 | } |
| 1875 | } else if (*sp == '=') { | 1887 | } else if (*sp == '=') { |
| @@ -1880,7 +1892,9 @@ parse_name(char *subject, long chtype, int multirdn) | |||
| 1880 | *bp++ = *sp++; | 1892 | *bp++ = *sp++; |
| 1881 | } | 1893 | } |
| 1882 | if (!*sp) { | 1894 | if (!*sp) { |
| 1883 | BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num); | 1895 | BIO_printf(bio_err, "end of string encountered while " |
| 1896 | "processing type of subject name element #%d\n", | ||
| 1897 | ne_num); | ||
| 1884 | goto error; | 1898 | goto error; |
| 1885 | } | 1899 | } |
| 1886 | ne_values[ne_num] = bp; | 1900 | ne_values[ne_num] = bp; |
| @@ -1889,7 +1903,8 @@ parse_name(char *subject, long chtype, int multirdn) | |||
| 1889 | if (*++sp) | 1903 | if (*++sp) |
| 1890 | *bp++ = *sp++; | 1904 | *bp++ = *sp++; |
| 1891 | else { | 1905 | else { |
| 1892 | BIO_printf(bio_err, "escape character at end of string\n"); | 1906 | BIO_printf(bio_err, "escape character " |
| 1907 | "at end of string\n"); | ||
| 1893 | goto error; | 1908 | goto error; |
| 1894 | } | 1909 | } |
| 1895 | } else if (*sp == '/') { | 1910 | } else if (*sp == '/') { |
| @@ -1909,7 +1924,7 @@ parse_name(char *subject, long chtype, int multirdn) | |||
| 1909 | ne_num++; | 1924 | ne_num++; |
| 1910 | } | 1925 | } |
| 1911 | 1926 | ||
| 1912 | if (!(n = X509_NAME_new())) | 1927 | if ((name = X509_NAME_new()) == NULL) |
| 1913 | goto error; | 1928 | goto error; |
| 1914 | 1929 | ||
| 1915 | for (i = 0; i < ne_num; i++) { | 1930 | for (i = 0; i < ne_num; i++) { |
| @@ -1920,29 +1935,27 @@ parse_name(char *subject, long chtype, int multirdn) | |||
| 1920 | continue; | 1935 | continue; |
| 1921 | } | 1936 | } |
| 1922 | if (!*ne_values[i]) { | 1937 | if (!*ne_values[i]) { |
| 1923 | BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]); | 1938 | BIO_printf(bio_err, "No value provided for Subject " |
| 1939 | "Attribute %s, skipped\n", ne_types[i]); | ||
| 1924 | continue; | 1940 | continue; |
| 1925 | } | 1941 | } |
| 1926 | if (!X509_NAME_add_entry_by_NID(n, nid, chtype, | 1942 | if (!X509_NAME_add_entry_by_NID(name, nid, chtype, |
| 1927 | (unsigned char *) ne_values[i], -1, -1, mval[i])) | 1943 | (unsigned char *) ne_values[i], -1, -1, mval[i])) |
| 1928 | goto error; | 1944 | goto error; |
| 1929 | } | 1945 | } |
| 1930 | 1946 | goto done; | |
| 1931 | free(ne_values); | ||
| 1932 | free(ne_types); | ||
| 1933 | free(buf); | ||
| 1934 | free(mval); | ||
| 1935 | |||
| 1936 | return n; | ||
| 1937 | 1947 | ||
| 1938 | error: | 1948 | error: |
| 1939 | X509_NAME_free(n); | 1949 | X509_NAME_free(name); |
| 1950 | name = NULL; | ||
| 1951 | |||
| 1952 | done: | ||
| 1940 | free(ne_values); | 1953 | free(ne_values); |
| 1941 | free(ne_types); | 1954 | free(ne_types); |
| 1942 | free(mval); | 1955 | free(mval); |
| 1943 | free(buf); | 1956 | free(buf); |
| 1944 | 1957 | ||
| 1945 | return NULL; | 1958 | return name; |
| 1946 | } | 1959 | } |
| 1947 | 1960 | ||
| 1948 | int | 1961 | int |
